You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "sahvx655-wq (via GitHub)" <gi...@apache.org> on 2026/06/03 07:43:04 UTC
[PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
sahvx655-wq opened a new pull request, #383:
URL: https://github.com/apache/commons-validator/pull/383
### Summary
This PR fixes a path validation bypass in `UrlValidator` where percent-encoded path traversal sequences could evade the existing parent-directory checks performed by `isValidPath(String)`.
`UrlValidator` validates paths by normalizing the URL path and rejecting traversal attempts such as `/../`.
However, the validation logic operates on the raw path component. Percent-encoded traversal sequences (for example `%2e%2e` and `%2f`) are not interpreted during normalization and therefore are not collapsed into their equivalent `../` path segments.
As a result, inputs such as:
http://example.com/..%2fetc/passwd
http://example.com/%2e%2e/world
can bypass the existing traversal checks and be considered valid.
Decode traversal-relevant percent-encoded characters before normalization so that encoded traversal sequences are evaluated consistently with their literal equivalents.
This ensures that both literal and percent-encoded parent-directory traversal attempts are rejected by the existing validation logic.
Added regression tests covering:
* `..%2f`
* `..%2F`
* `%2e%2e/`
* `%2e%2e%2f`
* mixed-case encoded traversal sequences
The new tests fail prior to the fix and pass after the fix.
This change closes a validation gap in `UrlValidator` and ensures that directory traversal checks cannot be bypassed using percent-encoded path separators or dot segments.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "sebbASF (via GitHub)" <gi...@apache.org>.
sebbASF merged PR #383:
URL: https://github.com/apache/commons-validator/pull/383
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "sebbASF (via GitHub)" <gi...@apache.org>.
sebbASF commented on PR #383:
URL: https://github.com/apache/commons-validator/pull/383#issuecomment-4750240037
Please correct build failure
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "sahvx655-wq (via GitHub)" <gi...@apache.org>.
sahvx655-wq commented on PR #383:
URL: https://github.com/apache/commons-validator/pull/383#issuecomment-4750372135
I have fixed the build failure and pushed the update
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on PR #383:
URL: https://github.com/apache/commons-validator/pull/383#issuecomment-4707416481
@sahvx655-wq
Ping.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "sebbASF (via GitHub)" <gi...@apache.org>.
sebbASF commented on code in PR #383:
URL: https://github.com/apache/commons-validator/pull/383#discussion_r3369332546
##########
src/main/java/org/apache/commons/validator/routines/UrlValidator.java:
##########
@@ -487,7 +513,7 @@ protected boolean isValidPath(final String path) {
try {
// Don't omit host otherwise leading path may be taken as host if it starts with //
- final URI uri = new URI(null, "localhost", path, null);
+ final URI uri = new URI(null, "localhost", decodePath(path), null);
Review Comment:
Why not use URLDecoder directly on the path?
It does not seem right to provide a home-built percent decoder here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "sahvx655-wq (via GitHub)" <gi...@apache.org>.
sahvx655-wq commented on PR #383:
URL: https://github.com/apache/commons-validator/pull/383#issuecomment-4750201234
I have updated the PR based on the review feedback. The path decoding logic is now handled directly in isValidPath(), decoding/parsing failures are treated as validation failures, and the consecutive slash check is performed on the decoded path. I have also added regression tests covering the reported traversal and encoding scenarios.
I have pushed the changes for review
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "sebbASF (via GitHub)" <gi...@apache.org>.
sebbASF commented on code in PR #383:
URL: https://github.com/apache/commons-validator/pull/383#discussion_r3379991675
##########
src/main/java/org/apache/commons/validator/routines/UrlValidator.java:
##########
@@ -476,6 +479,14 @@ protected boolean isValidFragment(final String fragment) {
return isOff(NO_FRAGMENTS);
}
+ private String decodePath(final String path) {
+ try {
+ return URLDecoder.decode(path, StandardCharsets.UTF_8.name());
+ } catch (final UnsupportedEncodingException e) {
+ throw new RuntimeException(e); // UTF-8 is always supported
Review Comment:
Given that the exception is immediately caught by the caller, there is no need to rethrow.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "sahvx655-wq (via GitHub)" <gi...@apache.org>.
sahvx655-wq commented on PR #383:
URL: https://github.com/apache/commons-validator/pull/383#issuecomment-4749372934
@garydgregory,
Thanks for the ping.
I'm working through the remaining review comments from @sebbASF and will push an updated version shortly
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on code in PR #383:
URL: https://github.com/apache/commons-validator/pull/383#discussion_r3379975440
##########
src/main/java/org/apache/commons/validator/routines/UrlValidator.java:
##########
@@ -476,6 +479,14 @@ protected boolean isValidFragment(final String fragment) {
return isOff(NO_FRAGMENTS);
}
+ private String decodePath(final String path) {
+ try {
+ return URLDecoder.decode(path, StandardCharsets.UTF_8.name());
+ } catch (final UnsupportedEncodingException e) {
+ throw new RuntimeException(e); // UTF-8 is always supported
Review Comment:
Throwing RE is an anti-pattern, IAE is better here IMO. There's also UncheckedIOException if the IO aspect is more important.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #383:
URL: https://github.com/apache/commons-validator/pull/383#discussion_r3408173797
##########
src/main/java/org/apache/commons/validator/routines/UrlValidator.java:
##########
@@ -476,6 +479,14 @@ protected boolean isValidFragment(final String fragment) {
return isOff(NO_FRAGMENTS);
}
+ private String decodePath(final String path) {
+ try {
+ return URLDecoder.decode(path, StandardCharsets.UTF_8.name());
+ } catch (final UnsupportedEncodingException e) {
+ throw new RuntimeException(e); // UTF-8 is always supported
+ }
+ }
Review Comment:
URLDecoder is intended for application/x-www-form-urlencoded and will translate '+' to space, which is not correct for URL *path* decoding and can change the meaning of valid paths (e.g., "/foo+bar"). Preserve literal '+' before decoding (and prefer an IllegalStateException for the unreachable UTF-8 case).
##########
src/main/java/org/apache/commons/validator/routines/UrlValidator.java:
##########
@@ -489,13 +500,13 @@ protected boolean isValidPath(final String path) {
try {
// Don't omit host otherwise leading path may be taken as host if it starts with //
- final URI uri = new URI(null, "localhost", path, null);
+ final URI uri = new URI(null, "localhost", decodePath(path), null);
final String norm = uri.normalize().getPath();
Review Comment:
Path traversal is checked on the decoded path, but the ALLOW_2_SLASHES enforcement still counts "//" on the *raw* path. After this change, percent-encoded slashes (e.g., "%2F%2F") can produce consecutive slashes after decoding while bypassing the raw "//" check. Count consecutive slashes on the same decoded value used for normalization.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "sahvx655-wq (via GitHub)" <gi...@apache.org>.
sahvx655-wq commented on PR #383:
URL: https://github.com/apache/commons-validator/pull/383#issuecomment-4657571891
Thanks for the feedback. I've updated the PR to use `URLDecoder.decode` instead of the custom logic. Literal `+` characters are preserved, invalid encodings fall back to the original path,
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "sebbASF (via GitHub)" <gi...@apache.org>.
sebbASF commented on code in PR #383:
URL: https://github.com/apache/commons-validator/pull/383#discussion_r3379704839
##########
src/main/java/org/apache/commons/validator/routines/UrlValidator.java:
##########
@@ -474,6 +477,17 @@ protected boolean isValidFragment(final String fragment) {
return isOff(NO_FRAGMENTS);
}
+ private String decodePath(final String path) {
+ try {
+ // URLDecoder.decode converts '+' to a space character.
+ // Since '+' is a valid literal character in a URI path,
+ // we escape it to '%2B' before decoding to preserve it.
+ return URLDecoder.decode(path.replace("+", "%2B"), StandardCharsets.UTF_8.name());
+ } catch (final UnsupportedEncodingException | IllegalArgumentException e) {
+ return path;
Review Comment:
If the path cannot be decoded, it should be treated as invalid, same as is done for URISyntaxException in the method isValidPath
##########
src/main/java/org/apache/commons/validator/routines/UrlValidator.java:
##########
@@ -474,6 +477,17 @@ protected boolean isValidFragment(final String fragment) {
return isOff(NO_FRAGMENTS);
}
+ private String decodePath(final String path) {
+ try {
+ // URLDecoder.decode converts '+' to a space character.
+ // Since '+' is a valid literal character in a URI path,
+ // we escape it to '%2B' before decoding to preserve it.
Review Comment:
This is unnecessary
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "sebbASF (via GitHub)" <gi...@apache.org>.
sebbASF commented on PR #383:
URL: https://github.com/apache/commons-validator/pull/383#issuecomment-4751290374
Thanks for your patience in getting this resolved.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on PR #383:
URL: https://github.com/apache/commons-validator/pull/383#issuecomment-4751401625
@sahvx655-wq
Yes, thank you!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Prevent URL path traversal bypass via percent encoding in UrlValidator [commons-validator]
Posted by "sebbASF (via GitHub)" <gi...@apache.org>.
sebbASF commented on code in PR #383:
URL: https://github.com/apache/commons-validator/pull/383#discussion_r3379968569
##########
src/main/java/org/apache/commons/validator/routines/UrlValidator.java:
##########
@@ -476,6 +479,14 @@ protected boolean isValidFragment(final String fragment) {
return isOff(NO_FRAGMENTS);
}
+ private String decodePath(final String path) {
Review Comment:
Not sure there is any need to use a private method here.
It would be simpler to just catch UnsupportedEncodingException in isvalid
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org