|
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; |
@@ -447,7 +448,9 @@ private void handleUnsupportedHttpMethod( |
447 | 448 | msg.append("Incorrect HTTP method for uri [").append(uri); |
448 | 449 | msg.append("] and method [").append(method).append("]"); |
449 | 450 | } else { |
450 | | - msg.append(exception.getMessage()); |
| 451 | + // Not using the error message directly from 'exception.getMessage()' to avoid unescaped HTML special characters, |
| 452 | + // in case false-positive cross site scripting vulnerability is detected by common security scanners. |
| 453 | + msg.append("Unexpected HTTP method"); |
451 | 454 | } |
452 | 455 | if (validMethodSet.isEmpty() == false) { |
453 | 456 | msg.append(", allowed: ").append(validMethodSet); |
@@ -488,7 +491,14 @@ private void handleBadRequest(String uri, RestRequest.Method method, RestChannel |
488 | 491 | try (XContentBuilder builder = channel.newErrorBuilder()) { |
489 | 492 | builder.startObject(); |
490 | 493 | { |
491 | | - builder.field("error", "no handler found for uri [" + uri + "] and method [" + method + "]"); |
| 494 | + try { |
| 495 | + // Validate input URI to filter out HTML special characters in the error message, |
| 496 | + // in case false-positive cross site scripting vulnerability is detected by common security scanners. |
| 497 | + uri = new URI(uri).getPath(); |
| 498 | + builder.field("error", "no handler found for uri [" + uri + "] and method [" + method + "]"); |
| 499 | + } catch (Exception e) { |
| 500 | + builder.field("error", "invalid uri has been requested"); |
| 501 | + } |
492 | 502 | } |
493 | 503 | builder.endObject(); |
494 | 504 | channel.sendResponse(new BytesRestResponse(BAD_REQUEST, builder)); |
|
0 commit comments