Skip to content

Commit ed72382

Browse files
committed
sync with code skills project update
1 parent 8591d7d commit ed72382

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

src/main/java/eu/righettod/SecurityUtils.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -401,19 +401,25 @@ public static void clearPDFMetadata(PDDocument document) {
401401
*/
402402
public static boolean isRelativeURL(String targetUrl) {
403403
boolean isValid = false;
404-
//Reject any URL encoded content and URL starting with a double slash
405-
//Reject any URL contains credentials or fragment to prevent potential bypasses
406404
String work = targetUrl;
407-
if (!work.contains("%") && !work.contains("@") && !work.contains("#") && !work.startsWith("//")) {
408-
//Creation of a URL object must fail
409-
try {
410-
new URL(work);
411-
isValid = false;
412-
} catch (MalformedURLException mf) {
413-
//Last check to be sure (for prod usage compile the pattern one time)
414-
isValid = Pattern.compile("^/[a-z0-9]+", Pattern.CASE_INSENSITIVE).matcher(work).find();
405+
Pattern startingPrefix = Pattern.compile("^[/a-zA-Z0-9\\-_].*");
406+
//Reject any URL no starting with a slash, letter, number, dash, or underscore
407+
if (startingPrefix.matcher(work).find()) {
408+
//Reject any URL encoded content and URL starting with a double slash
409+
if (!work.startsWith("//") && !work.contains("%")) {
410+
//Try to create en URI object
411+
try {
412+
URI u = new URI(work);
413+
//Scheme must be null
414+
if (u.getScheme() == null) {
415+
isValid = (!u.isAbsolute());
416+
}
417+
} catch (URISyntaxException mf) {
418+
isValid = false;
419+
}
415420
}
416421
}
422+
417423
return isValid;
418424
}
419425

src/test/java/eu/righettod/TestSecurityUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ public void isZIPSafe() {
166166

167167
@Test
168168
public void isRelativeURL() {
169-
List<String> nonRelativeURLList = Arrays.asList("//righettod.eu", "http://righettod.eu", "https://righettod.eu", "ssh://righettod.eu", "http://login:pass@righettod.eu");
169+
List<String> nonRelativeURLList = Arrays.asList("//righettod.eu", "http://righettod.eu", "https://righettod.eu", "ssh://righettod.eu", "ssh://righettod.eu%23");
170170
nonRelativeURLList.forEach(u -> {
171171
assertFalse(SecurityUtils.isRelativeURL(u), String.format("URL '%s' must be detected as NOT relative!", u));
172172
});
173-
List<String> relativeURLList = Arrays.asList("/righettod.eu", "/test.jsp");
173+
List<String> relativeURLList = Arrays.asList("/righettod.eu", "/test.jsp", "/test.jsp?a=b");
174174
relativeURLList.forEach(u -> {
175175
assertTrue(SecurityUtils.isRelativeURL(u), String.format("URL '%s' must be detected as relative!", u));
176176
});

0 commit comments

Comments
 (0)