|
56 | 56 | import java.io.ByteArrayOutputStream; |
57 | 57 | import java.io.IOException; |
58 | 58 | import java.io.InputStream; |
| 59 | +import java.net.URI; |
59 | 60 | import java.util.HashMap; |
60 | 61 | import java.util.HashSet; |
61 | 62 | import java.util.Iterator; |
@@ -488,9 +489,14 @@ private void handleBadRequest(String uri, RestRequest.Method method, RestChannel |
488 | 489 | try (XContentBuilder builder = channel.newErrorBuilder()) { |
489 | 490 | builder.startObject(); |
490 | 491 | { |
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 | + } |
494 | 500 | } |
495 | 501 | builder.endObject(); |
496 | 502 | channel.sendResponse(new BytesRestResponse(BAD_REQUEST, builder)); |
@@ -580,24 +586,4 @@ private static CircuitBreaker inFlightRequestsBreaker(CircuitBreakerService circ |
580 | 586 | // We always obtain a fresh breaker to reflect changes to the breaker configuration. |
581 | 587 | return circuitBreakerService.getBreaker(CircuitBreaker.IN_FLIGHT_REQUESTS); |
582 | 588 | } |
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 | | - } |
603 | 589 | } |
0 commit comments