Skip to content

Commit 34f1b8f

Browse files
author
Tianli Feng
committed
Filter out invalid URI in the error message of no handler found for a REST request
Signed-off-by: Tianli Feng <[email protected]>
1 parent ef4e190 commit 34f1b8f

File tree

2 files changed

+10
-27
lines changed

2 files changed

+10
-27
lines changed

server/src/main/java/org/opensearch/rest/RestController.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import java.io.ByteArrayOutputStream;
5757
import java.io.IOException;
5858
import java.io.InputStream;
59+
import java.net.URI;
5960
import java.util.HashMap;
6061
import java.util.HashSet;
6162
import java.util.Iterator;
@@ -488,9 +489,14 @@ private void handleBadRequest(String uri, RestRequest.Method method, RestChannel
488489
try (XContentBuilder builder = channel.newErrorBuilder()) {
489490
builder.startObject();
490491
{
491-
// Escaping HTML special characters in the error message only aims to satisfy common security scanners.
492-
// There is no vulnerability without escaping, because the response MIME type is application/json, no scripts will be run.
493-
builder.field("error", "no handler found for uri [" + escapeHtml(uri) + "] and method [" + method + "]");
492+
try {
493+
// Validate input URI to filter out HTML special characters in the error message,
494+
// in case false-positive cross site scripting vulnerability is detected by common security scanners.
495+
uri = new URI(uri).getPath();
496+
builder.field("error", "no handler found for uri [" + uri + "] and method [" + method + "]");
497+
} catch (Exception e) {
498+
builder.field("error", "invalid uri has been requested");
499+
}
494500
}
495501
builder.endObject();
496502
channel.sendResponse(new BytesRestResponse(BAD_REQUEST, builder));
@@ -580,24 +586,4 @@ private static CircuitBreaker inFlightRequestsBreaker(CircuitBreakerService circ
580586
// We always obtain a fresh breaker to reflect changes to the breaker configuration.
581587
return circuitBreakerService.getBreaker(CircuitBreaker.IN_FLIGHT_REQUESTS);
582588
}
583-
584-
/**
585-
* Perform an HTML escape operation on a String input to prevent XSS vulnerability.
586-
* @param text the String to be escaped.
587-
* @return The escaped result String.
588-
*/
589-
private String escapeHtml(String text) {
590-
StringBuilder out = new StringBuilder();
591-
for (int i = 0; i < text.length(); i++) {
592-
char c = text.charAt(i);
593-
if (c == '"' || c == '\'' || c == '<' || c == '>' || c == '&') {
594-
out.append("&#");
595-
out.append((int) c);
596-
out.append(';');
597-
} else {
598-
out.append(c);
599-
}
600-
}
601-
return out.toString();
602-
}
603589
}

server/src/test/java/org/opensearch/rest/RestControllerTests.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,7 @@ public void testHandleBadRequestWithHtmlSpecialCharsInUri() {
559559
).build();
560560
final AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.BAD_REQUEST);
561561
restController.dispatchRequest(fakeRestRequest, channel, client.threadPool().getThreadContext());
562-
assertThat(
563-
channel.getRestResponse().content().utf8ToString(),
564-
containsString("/&#60;script&#62;alert(&#39;xss&#39;);alert(&#34;&#38;#x6A&#38;#x61&#38;#x76&#38;#x61&#34;);&#60;/script&#62;")
565-
);
562+
assertThat(channel.getRestResponse().content().utf8ToString(), containsString("invalid uri has been requested"));
566563
}
567564

568565
public void testDispatchUnsupportedHttpMethod() {

0 commit comments

Comments
 (0)