Http server returns invalid HTTP response codes

XMLWordPrintable

    • Type: Bug
    • Resolution: Unresolved
    • Priority: P4
    • None
    • Affects Version/s: 6
    • Component/s: core-libs
    • None

      There is currently no validation done on the responseCode parameter to HttpExchange.sendResponseHeaders(). Any invalid positive value or even negative integer will be returned to the client.

      The response code should be validated to be within the range 100-599. The 1XX codes perhaps should also be excluded. The following is a simple demonstration.

      HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 5);
              server.setExecutor(Executors.newCachedThreadPool());
              var ctx = server.createContext("/", (exchange -> {
                  exchange.sendResponseHeaders(-5, -1);
                  exchange.close();
              }));
              server.start();
              int port = server.getAddress().getPort();
              URL url = new URL("http://127.0.0.1:" + Integer.toString(port) + "/foo");
              HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
              System.out.println(urlc.getResponseCode());

            Assignee:
            Michael McMahon
            Reporter:
            Michael McMahon
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: