Skip to content

Commit 503dc6a

Browse files
authored
Fixed: IndexOutOfBoundsException in UtilHttp for unlisted TLDs (OFBIZ-13389) (#1185)
Fixed: IndexOutOfBoundsException in UtilHttp for unlisted TLDs (OFBIZ-13389). - Exact URL match had redundant indexOf checks for protocols which `UtilValidate.isValidUrl()` already validates, causing https urls to slip thru. Removed redundant checks. - The `extractUrls()` regex had a hardcoded list of TLDs, made the regex generic and validating the URL with `UtilValidate.isValidUrl()` now. - Added empty check to avoid `IndexOutOfBoundsException`. Thanks: Vitaly Repetenko for reporting the issue.
1 parent a0a373b commit 503dc6a

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

  • framework/base/src/main/java/org/apache/ofbiz/base/util

framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,19 @@ public static Map<String, Object> canonicalizeParameterMap(Map<String, Object> p
428428
if (stringValues.length > 0 && !paramEntry.getKey().equals("DUMMYPAGE")) {
429429
for (String s : stringValues) {
430430
// if the string contains only an URL beginning by http or ftp => no change to keep special chars
431-
if (UtilValidate.isValidUrl(s) && (s.indexOf("://") == 4 || s.indexOf("://") == 3)) {
431+
if (UtilValidate.isValidUrl(s)) {
432432
params = params + s + " ";
433433
} else if (UtilValidate.isUrlInString(s) && !s.isEmpty()) {
434434
// if the string contains not only an URL => concatenate possible canonicalized before and after, w/o changing the URL
435-
String url = extractUrls(s).get(0); // There should be only 1 URL in a block, makes no sense else
436-
int start = s.indexOf(url);
437-
String after = (String) s.subSequence(start + url.length(), s.length());
438-
params = params + canonicalizeParameter((String) s.subSequence(0, start)) + url + canonicalizeParameter(after) + " ";
435+
List<String> extractedUrls = extractUrls(s);
436+
if (!extractedUrls.isEmpty()) {
437+
String url = extractedUrls.get(0); // There should be only 1 URL in a block, makes no sense else
438+
int start = s.indexOf(url);
439+
String after = (String) s.subSequence(start + url.length(), s.length());
440+
params = params + canonicalizeParameter((String) s.subSequence(0, start)) + url + canonicalizeParameter(after) + " ";
441+
} else {
442+
params = params + canonicalizeParameter(s) + " ";
443+
}
439444
} else {
440445
// Simple string to canonicalize
441446
params = params + canonicalizeParameter(s) + " ";
@@ -1774,9 +1779,7 @@ public static List<String> extractUrls(String input) {
17741779

17751780
Pattern pattern = Pattern.compile(
17761781
"\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)"
1777-
+ "(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov"
1778-
+ "|mil|biz|info|mobi|name|aero|jobs|museum"
1779-
+ "|travel|[a-z]{2}))(:[\\d]{1,5})?"
1782+
+ "(\\w+:\\w+@)?(([-\\w]+\\.)+([a-zA-Z]{2,}))(:[\\d]{1,5})?"
17801783
+ "(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?"
17811784
+ "((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?"
17821785
+ "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)"
@@ -1794,7 +1797,10 @@ public static List<String> extractUrls(String input) {
17941797
if (result.isEmpty()) {
17951798
Matcher matcher = pattern.matcher(input);
17961799
while (matcher.find()) {
1797-
result.add(matcher.group());
1800+
String candidate = matcher.group();
1801+
if (UtilValidate.isValidUrl(candidate)) {
1802+
result.add(candidate);
1803+
}
17981804
}
17991805
}
18001806

0 commit comments

Comments
 (0)