-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[CXF-8947] - Avoid expensive regex operations in Rfc3986UriValidator #1483
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,13 +93,18 @@ public final class HttpUtils { | |
// there are more of such characters, ex, '*' but '*' is not affected by UrlEncode | ||
private static final String PATH_RESERVED_CHARACTERS = "=@/:!$&\'(),;~"; | ||
private static final String QUERY_RESERVED_CHARACTERS = "?/,"; | ||
|
||
private static final Set<String> KNOWN_HTTP_VERBS_WITH_NO_REQUEST_CONTENT = | ||
new HashSet<>(Arrays.asList(new String[]{"GET", "HEAD", "OPTIONS", "TRACE"})); | ||
private static final Set<String> KNOWN_HTTP_VERBS_WITH_NO_RESPONSE_CONTENT = | ||
new HashSet<>(Arrays.asList(new String[]{"HEAD", "OPTIONS"})); | ||
|
||
private static final Pattern HTTP_SCHEME_PATTERN = Pattern.compile("^(?i)(http|https)$"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern is super straightforward to look up, what are exactly the gains here (vs adding the set + comparator + lowecasing)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @reta , Sorry for the delay. The reason we changed this was because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough, may be we could just have two constants instead? |
||
|
||
private static final Set<String> HTTP_SCHEMES = new HashSet<>(); | ||
|
||
static { | ||
HTTP_SCHEMES.add("http"); | ||
HTTP_SCHEMES.add("https"); | ||
} | ||
|
||
private HttpUtils() { | ||
} | ||
|
@@ -372,7 +377,7 @@ public static URI toAbsoluteUri(String relativePath, Message message) { | |
(HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST)); | ||
return URI.create(base + relativePath); | ||
} | ||
|
||
public static void setHttpRequestURI(Message message, String uriTemplate) { | ||
HttpServletRequest request = | ||
(HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST); | ||
|
@@ -479,8 +484,8 @@ public static String getBaseAddress(Message m) { | |
URI uri = new URI(endpointAddress); | ||
String path = uri.getRawPath(); | ||
String scheme = uri.getScheme(); | ||
// RFC-3986: the scheme and host are case-insensitive and therefore should | ||
// be normalized to lowercase. | ||
// RFC-3986: the scheme and host are case-insensitive and therefore should | ||
// be normalized to lowercase. | ||
if (scheme != null && !scheme.toLowerCase().startsWith(HttpUtils.HTTP_SCHEME) | ||
&& HttpUtils.isHttpRequest(m)) { | ||
path = HttpUtils.toAbsoluteUri(path, m).getRawPath(); | ||
|
@@ -493,15 +498,15 @@ public static String getBaseAddress(Message m) { | |
|
||
public static String getEndpointUri(Message m) { | ||
final Object servletRequest = m.get(AbstractHTTPDestination.HTTP_REQUEST); | ||
|
||
if (servletRequest != null) { | ||
final Object property = ((jakarta.servlet.http.HttpServletRequest)servletRequest) | ||
.getAttribute("org.apache.cxf.transport.endpoint.uri"); | ||
if (property != null) { | ||
return property.toString(); | ||
} | ||
} | ||
|
||
return getEndpointAddress(m); | ||
} | ||
|
||
|
@@ -618,7 +623,7 @@ public static String getEncoding(MediaType mt, String defaultEncoding) { | |
|
||
public static String getMediaTypeCharsetParameter(MediaType mt) { | ||
String charset = mt.getParameters().get(CHARSET_PARAMETER); | ||
if (charset != null && charset.startsWith(DOUBLE_QUOTE) | ||
if (charset != null && charset.startsWith(DOUBLE_QUOTE) | ||
&& charset.endsWith(DOUBLE_QUOTE) && charset.length() > 1) { | ||
charset = charset.substring(1, charset.length() - 1); | ||
} | ||
|
@@ -699,7 +704,7 @@ public static boolean isPayloadEmpty(MultivaluedMap<String, String> headers) { | |
|
||
return false; | ||
} | ||
|
||
public static <T> T createServletResourceValue(Message m, Class<T> clazz) { | ||
|
||
Object value = null; | ||
|
@@ -721,12 +726,12 @@ public static <T> T createServletResourceValue(Message m, Class<T> clazz) { | |
public static boolean isMethodWithNoRequestContent(String method) { | ||
return KNOWN_HTTP_VERBS_WITH_NO_REQUEST_CONTENT.contains(method); | ||
} | ||
|
||
public static boolean isMethodWithNoResponseContent(String method) { | ||
return KNOWN_HTTP_VERBS_WITH_NO_RESPONSE_CONTENT.contains(method); | ||
} | ||
|
||
public static boolean isHttpScheme(final String scheme) { | ||
return scheme != null && HTTP_SCHEME_PATTERN.matcher(scheme).matches(); | ||
return scheme != null && HTTP_SCHEMES.contains(scheme.toLowerCase()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@WhiteCat22 the reason for this validator to exists sadly is the fact that Java's URI is not RFC-3986 complaint. The
host
is not trustful source here hence we validate it against the pattern.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a good point that we had not considered.