normalize URI path

This commit is contained in:
JoeWang-Java 2026-08-03 18:19:04 +00:00
parent 9dec74fc4f
commit e30eb41109
2 changed files with 28 additions and 2 deletions

View File

@ -382,19 +382,42 @@ public class AccessRule {
return new PathPattern(pattern, true, false);
}
if (pattern.endsWith("/*")) {
return new PathPattern(pattern.substring(0, pattern.length() - 2), false, true);
return new PathPattern(normalizePath(pattern.substring(0, pattern.length() - 2)), false, true);
}
return new PathPattern(pattern, false, false);
return new PathPattern(normalizePath(pattern), false, false);
}
public boolean matches(String testPath) {
if (isAny) return true;
if (testPath == null) return false;
testPath = normalizePath(testPath);
if (isDirectory) {
// Path starts with this directory
return testPath.startsWith(pattern + "/") || testPath.equals(pattern);
}
return testPath.equals(pattern);
}
// Normalizes URI path for rule matching, consistent with URI.normalize().
private static String normalizePath(String path) {
boolean absolute = path.startsWith("/");
List<String> segments = new ArrayList<>();
for (String segment : path.split("/")) {
if (segment.isEmpty() || segment.equals(".")) {
continue;
}
if (segment.equals("..")) {
if (!segments.isEmpty()) {
segments.remove(segments.size() - 1);
} else if (!absolute) {
segments.add(segment);
}
} else {
segments.add(segment);
}
}
String normalizedPath = String.join("/", segments);
return absolute ? "/" + normalizedPath : normalizedPath;
}
}
}

View File

@ -63,6 +63,9 @@ public class AccessRuleTest {
"http://www.oracle.com/dtds/example.dtd; http://subdomains.oracle.com/dtds/example.dtd", true),
Arguments.of("file:/dtds/dtd1.dtd", "file:/dtds/dtd1.dtd", true),
Arguments.of("file:/dtds/dtd1.dtd, file:/xsds/*", "file:/dtds/dtd1.dtd; file:/xsds/example.xsd", true),
Arguments.of("file:/dir/*", "file:/dir/child.dtd", true),
Arguments.of("file:/dir/*", "file:/dir/../foo.dtd; file:/dir/%2e%2e/foo.dtd", false),
Arguments.of("file:/*", "file:/dir/../../foo.dtd; file:/../foo.dtd", true),
Arguments.of("http://www.oracle.com, file:/dtds/dtd1.dtd, file:/xsds/*",
"http://www.oracle.com/dtds/example.dtd; file:/dtds/dtd1.dtd; file:/xsds/example.xsd", true),
Arguments.of("http://[2001:db8::1]",