Skip to content

Commit

Permalink
Fix URL validation on long/internationalized URLs (twentyhq#1423)
Browse files Browse the repository at this point in the history
* Add URL validation tests

Add test for longer TLDs
Add test for internationalized TLDs

* Fix URL validation with long TLDs

* TLD max size match RFC 1034

---------

Co-authored-by: Jérémy M <jeremy.magrin@gmail.com>
  • Loading branch information
xprnio and magrinj authored Sep 4, 2023
1 parent a46210b commit c998c03
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions front/src/utils/__tests__/is-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,19 @@ describe('isURL', () => {
),
).toBeTruthy();
});

it('should return true if the TLD is long', () => {
expect(isURL('https://example.travelinsurance')).toBeTruthy();
});

it('should return true if the TLD is internationalized', () => {
// The longest TLD as of now
// https://stackoverflow.com/questions/9238640/how-long-can-a-tld-possibly-be
// curl -s http://data.iana.org/TLD/tlds-alpha-by-domain.txt \
// | tail -n+2 \
// | awk '{ print length, $0 }' \
// | sort --numeric-sort --reverse \
// | head -n 5
expect(isURL('https://example.xn--vermgensberatung-pwb')).toBeTruthy();
});
});
2 changes: 1 addition & 1 deletion front/src/utils/is-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function isURL(url: string | undefined | null) {
return (
isDefined(url) &&
url.match(
/^(https?:\/\/)?(www.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/i,
/^(https?:\/\/)?(www.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-z]{2,63}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/i,
)
);
}

0 comments on commit c998c03

Please sign in to comment.