-
Notifications
You must be signed in to change notification settings - Fork 166
Issue #1883: accept non-public TLDs in redirect URLs. #1894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mbert
wants to merge
1
commit into
mosip:master
Choose a base branch
from
mbert:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 48 additions & 8 deletions
56
esignet-core/src/main/java/io/mosip/esignet/core/validator/RedirectURLValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,64 @@ | ||
| package io.mosip.esignet.core.validator; | ||
|
|
||
| import org.apache.commons.validator.routines.RegexValidator; | ||
| import org.apache.commons.validator.routines.UrlValidator; | ||
| import org.hibernate.validator.constraints.URL; | ||
| import static org.apache.commons.validator.routines.UrlValidator.ALLOW_ALL_SCHEMES; | ||
| import static org.apache.commons.validator.routines.UrlValidator.ALLOW_LOCAL_URLS; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import jakarta.validation.ConstraintValidator; | ||
| import jakarta.validation.ConstraintValidatorContext; | ||
|
|
||
| import static org.apache.commons.validator.routines.UrlValidator.ALLOW_ALL_SCHEMES; | ||
| import static org.apache.commons.validator.routines.UrlValidator.ALLOW_LOCAL_URLS; | ||
|
|
||
| /** | ||
| * @class RedirectURLValidator uses a customised Apache {@link UrlValidator} | ||
| * to check syntactical validity of redirect URLs, allowing any scheme and local URLs, | ||
| * but restricting the authority part to valid IPv4/IPv6 addresses, localhost, or domain | ||
| * names with any TLD. | ||
| */ | ||
| @Component | ||
| public class RedirectURLValidator implements ConstraintValidator<RedirectURL, String> { | ||
|
|
||
| private final UrlValidator urlValidator = new UrlValidator(ALLOW_ALL_SCHEMES+ALLOW_LOCAL_URLS); | ||
| // IPv6 address in brackets – strict RFC 4291 alternation covering all | ||
| // compressed (::) and full (8-group) forms; rejects bare garbage like [::::] | ||
| private static final String IPV6_REGEX = | ||
| "\\[(?:" + | ||
| "(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}" + // full 8-group, no :: | ||
| "|(?:[0-9a-fA-F]{1,4}:){1,7}:" + // trailing :: | ||
| "|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}" + // 6+1 around :: | ||
| "|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}" + // 5+1-2 | ||
| "|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}" + // 4+1-3 | ||
| "|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}" + // 3+1-4 | ||
| "|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}" + // 2+1-5 | ||
| "|[0-9a-fA-F]{1,4}:(?::[0-9a-fA-F]{1,4}){1,6}" + // 1+1-6 | ||
| "|:(?::[0-9a-fA-F]{1,4}){1,7}" + // leading :: | ||
| "|::" + // all-zeros | ||
| ")\\]"; | ||
| // IPv4 address | ||
| private static final String IPV4_REGEX = "((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])"; | ||
| // localhost | ||
| private static final String LOCALHOST_REGEX = "localhost"; | ||
| // Domain name with any TLD (at least two letters). | ||
| // Each label must start and end with an alphanumeric character (RFC 1123); | ||
| // hyphens are only permitted in the interior of a label. | ||
| private static final String DOMAIN_REGEX = "([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,}"; | ||
| // Optional port — restricted to the valid TCP/UDP range 0–65535 | ||
| private static final String PORT_REGEX = "(:(6553[0-5]|655[0-2]\\d|65[0-4]\\d{2}|6[0-4]\\d{3}|[1-5]\\d{4}|\\d{1,4}))?"; | ||
|
|
||
| // The resulting regular expression validates the authority part of the URL (host and optional port) while allowing any TLD in the domain name. | ||
| private static final String AUTHORITY_PART_RX = "^(" + IPV6_REGEX + "|" + IPV4_REGEX + "|" + LOCALHOST_REGEX + "|" + DOMAIN_REGEX + ")" + PORT_REGEX + "$"; | ||
|
|
||
| private final UrlValidator urlValidator = new UrlValidator(new RegexValidator(AUTHORITY_PART_RX), ALLOW_ALL_SCHEMES+ALLOW_LOCAL_URLS); | ||
|
|
||
| /** | ||
| * Validates redirect URLs while allowing private/non-IANA TLDs. | ||
| * | ||
| * @param redirectUrl redirect URL to validate | ||
| * @param constraintValidatorContext validation context, unused | ||
| * @return true if the redirect URL is valid | ||
| */ | ||
| @Override | ||
| public boolean isValid(String redirectUrl, ConstraintValidatorContext constraintValidatorContext) { | ||
| return urlValidator.isValid(redirectUrl); | ||
| public boolean isValid(final String redirectUrl, final ConstraintValidatorContext constraintValidatorContext) { | ||
| return this.urlValidator.isValid(redirectUrl); | ||
| } | ||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
esignet-core/src/test/java/io/mosip/esignet/core/validator/RedirectURLValidatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /* | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| */ | ||
| package io.mosip.esignet.core.validator; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Verifies that redirect URLs with standard public TLDs are accepted. | ||
| */ | ||
| public class RedirectURLValidatorTest { | ||
|
|
||
| private static final RedirectURLValidator REDIRECT_URL_VALIDATOR = new RedirectURLValidator(); | ||
|
|
||
| /** | ||
| * Tests that valid URLs with standard IANA-registered TLDs are accepted. | ||
| */ | ||
| @Test | ||
| public void standardIanaRegisteredTldsTest() { | ||
| // .com | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://example.com/callback", null)); | ||
| // .org | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://example.org/callback", null)); | ||
| // twoletter TLDs like .de | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://example.de/callback", null)); | ||
| // longer TLDs like .technology | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://example.technology/callback", null)); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that valid URLs with non-IANA or custom TLDs are accepted. | ||
| */ | ||
| @Test | ||
| public void nonIanaOrCustomTldTest() { | ||
| // .xx is not a real IANA TLD but must be accepted | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://api.dev.mosip.xx/home/test", null)); | ||
| // .internal is commonly used for private networks | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://myservice.internal/callback", null)); | ||
| // .local is used in mDNS / private environments | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://myapp.local/callback", null)); | ||
| // .test is an RFC 2606 reserved name for testing | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://app.test/callback", null)); | ||
| // private TLD used inside the MOSIP ecosystem | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://api.service.mosip/callback", null)); | ||
| // non IANA TLD .corp | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://auth.sso.corp/callback", null)); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that valid URLs with IPv4 and IPv6 addresses, as well as localhost, are accepted. | ||
| */ | ||
| @Test | ||
| public void ipAddressV4V6Test() { | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("http://192.168.1.1/callback", null)); | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("http://10.0.0.1:8080/callback", null)); | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("http://localhost/callback", null)); | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("http://localhost:8080/callback", null)); | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("http://[::1]/callback", null)); | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("http://[2001:db8::1]/callback", null)); | ||
| // invalid: only colons, no hex digits | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("http://[::::]/callback", null)); | ||
| // invalid: group exceeds four hex digits | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("http://[12345::1]/callback", null)); | ||
| // invalid: nine groups (too many) | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("http://[1:2:3:4:5:6:7:8:9]/callback", null)); | ||
| // invalid: non-hex character | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("http://[::gggg]/callback", null)); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that valid URLs with ports are accepted and invalid ports are rejected. | ||
| */ | ||
| @Test | ||
| public void portsTest() { | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://example.com:443/callback", null)); | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://example.com:8443/callback", null)); | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://example.com:65535/callback", null)); | ||
| // first port value above the valid range must be rejected | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("https://example.com:65536/callback", null)); | ||
| // port 0 is accepted (enforcement is left to upper layers) | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://example.com:0/callback", null)); | ||
| // clearly out-of-range port must be rejected | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("https://example.com:99999/callback", null)); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that valid URLs with various schemes are accepted. | ||
| */ | ||
| @Test | ||
| public void schemesTest() { | ||
| // http | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("http://example.com/callback", null)); | ||
| // ftp | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("ftp://example.com/callback", null)); | ||
| // Mobile deep-link used by MOSIP resident app | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("io.mosip.residentapp://oauth", null)); | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("myapp://auth.internal/callback", null)); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that valid URLs with various path variations, query strings, and fragments are accepted. | ||
| */ | ||
| @Test | ||
| public void pathVariationsAndQueryStringsTest() { | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://api.dev.mosip.net/home/testament?rr=rrr", null)); | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid( | ||
| "https://api.dev.mosip.net/home/werrrwqfdsfg5fgs34sdffggdfgsdfg?state=reefdf", null)); | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://example.com/page#section", null)); | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://a.b.c.example.com/callback", null)); | ||
| Assertions.assertTrue(REDIRECT_URL_VALIDATOR.isValid("https://api.dev.mosip.net/home/test", null)); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that invalid URLs are rejected. | ||
| */ | ||
| @Test | ||
| public void invalidUrlTest() { | ||
| // null | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid(null, null)); | ||
| // empty | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("", null)); | ||
| // A URL without a scheme is not valid | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("example.com/callback", null)); | ||
| // TLD must be at least two letters; single-char TLD must be rejected | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("https://example.c/callback", null)); | ||
| // TLD must consist of letters only, not digits | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("https://example.123/callback", null)); | ||
| // 256 is not a valid octet | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("http://256.0.0.1/callback", null)); | ||
| // space in host | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("https://exam ple.com/callback", null)); | ||
| // label must not start with a hyphen (RFC 1123) | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("https://-bad.example.com/callback", null)); | ||
| // label must not end with a hyphen (RFC 1123) | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("https://bad-.example.com/callback", null)); | ||
| // only scheme | ||
| Assertions.assertFalse(REDIRECT_URL_VALIDATOR.isValid("https://", null)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.