-
Notifications
You must be signed in to change notification settings - Fork 384
Harden Accept-Encoding parsing to prevent NPE #10307
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ | |
| import java.util.Date; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.regex.Matcher; | ||
|
|
@@ -372,7 +373,7 @@ public void send(HttpServletRequest request, HttpServletResponse response, TreeL | |
| } | ||
|
|
||
| if (contentEncoding != null) { | ||
| if (!request.getHeader("Accept-Encoding").contains("gzip")) { | ||
| if (!acceptsGzipEncoding(request.getHeader("Accept-Encoding"))) { | ||
| response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); | ||
| logger.log(TreeLogger.WARN, "client doesn't accept gzip; bailing"); | ||
| return; | ||
|
|
@@ -543,6 +544,54 @@ static String guessMimeType(String filename) { | |
| return mimeType != null ? mimeType : ""; | ||
| } | ||
|
|
||
| /* visible for testing */ | ||
| static boolean acceptsGzipEncoding(String acceptEncodingHeader) { | ||
| if (acceptEncodingHeader == null || acceptEncodingHeader.trim().isEmpty()) { | ||
| return false; | ||
| } | ||
|
|
||
| Double gzipQValue = null; | ||
| Double wildcardQValue = null; | ||
|
|
||
| for (String encodingSpec : acceptEncodingHeader.split(",")) { | ||
| String[] parts = encodingSpec.trim().split(";"); | ||
| if (parts.length == 0) { | ||
| continue; | ||
| } | ||
|
|
||
| String encoding = parts[0].trim().toLowerCase(Locale.ROOT); | ||
| if (encoding.isEmpty()) { | ||
| continue; | ||
|
Member
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. contents don't adhere to the spec, can we continue parsing at all? should this return false instead?
Author
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 current implementation takes a more tolerant approach: it ignores malformed entries and continues parsing valid ones so a single bad token doesn’t cause the entire Accept-Encoding header to be rejected. i will align with whichever behavior you prefer here:
Let me know which direction you’d like |
||
| } | ||
|
|
||
| double qValue = 1.0; | ||
| for (int i = 1; i < parts.length; i++) { | ||
| String parameter = parts[i].trim().toLowerCase(Locale.ROOT); | ||
| if (parameter.startsWith("q=")) { | ||
|
Member
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 seems to imply that other parameters are supported, is that correct? By spec it doesn't seem allowed. |
||
| String qValueText = parameter.substring("q=".length()).trim(); | ||
| try { | ||
| qValue = Double.parseDouble(qValueText); | ||
| } catch (NumberFormatException e) { | ||
| qValue = 0.0; | ||
|
Member
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. Allowed range is explicitly [0-1], and precision is <=3 decimal places https://httpwg.org/specs/rfc9110.html#quality.values - we should perhaps fail if outside that range? |
||
| } | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (encoding.equals("gzip")) { | ||
| gzipQValue = qValue; | ||
|
Member
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. can't we return early here? do we need to track the q values at all after we check a given entry? if the encoding is valid but isn't * or gzip, we ignore it. if it is either * or gzip and has a q value greater than zero, we return true. if no * or gzip had a value greater than zero, we return false. what am I missing that we can't substantially trim this down? |
||
| } else if (encoding.equals("*")) { | ||
| wildcardQValue = qValue; | ||
| } | ||
| } | ||
|
|
||
| if (gzipQValue != null) { | ||
| return gzipQValue > 0.0; | ||
| } | ||
|
|
||
| return wildcardQValue != null && wildcardQValue > 0.0; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the binding properties from the web page where dev mode is being used. (As passed in | ||
| * by dev_mode_on.js in a JSONP request to "/recompile".) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Copyright 2026 Google Inc. | ||
|
zbynek marked this conversation as resolved.
Outdated
|
||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package com.google.gwt.dev.codeserver; | ||
|
|
||
| import junit.framework.TestCase; | ||
|
|
||
| /** | ||
| * Security-focused tests for request header parsing in {@link WebServer}. | ||
|
zbynek marked this conversation as resolved.
Outdated
|
||
| */ | ||
| public class WebServerSecurityTest extends TestCase { | ||
|
|
||
| public void testAcceptsGzipEncodingRejectsNullOrEmptyHeader() { | ||
| assertFalse(WebServer.acceptsGzipEncoding(null)); | ||
|
Member
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 is wrong, isn't it? https://httpwg.org/specs/rfc9110.html#field.accept-encoding
|
||
| assertFalse(WebServer.acceptsGzipEncoding("")); | ||
| assertFalse(WebServer.acceptsGzipEncoding(" ")); | ||
| } | ||
|
|
||
| public void testAcceptsGzipEncodingAcceptsSimpleGzip() { | ||
| assertTrue(WebServer.acceptsGzipEncoding("gzip")); | ||
| assertTrue(WebServer.acceptsGzipEncoding("deflate, gzip")); | ||
| assertTrue(WebServer.acceptsGzipEncoding("GZIP")); | ||
| } | ||
|
|
||
| public void testAcceptsGzipEncodingRejectsExplicitGzipZeroQValue() { | ||
| assertFalse(WebServer.acceptsGzipEncoding("gzip;q=0")); | ||
| assertFalse(WebServer.acceptsGzipEncoding("deflate, gzip; q=0.0")); | ||
| } | ||
|
|
||
| public void testAcceptsGzipEncodingHonorsWildcardWhenGzipAbsent() { | ||
| assertTrue(WebServer.acceptsGzipEncoding("*")); | ||
| assertTrue(WebServer.acceptsGzipEncoding("br;q=0.2, *;q=0.7")); | ||
| assertFalse(WebServer.acceptsGzipEncoding("*;q=0")); | ||
| } | ||
|
|
||
| public void testAcceptsGzipEncodingPrefersExplicitGzipOverWildcard() { | ||
| assertFalse(WebServer.acceptsGzipEncoding("gzip;q=0, *;q=1")); | ||
| assertTrue(WebServer.acceptsGzipEncoding("gzip;q=1, *;q=0")); | ||
| } | ||
|
|
||
| public void testAcceptsGzipEncodingRejectsMalformedQValue() { | ||
| assertFalse(WebServer.acceptsGzipEncoding("gzip;q=not-a-number")); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.