Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions dev/codeserver/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ test_suite(
tests = [
":RecompilerTest",
":SourceHandlerTest",
":WebServerSecurityTest",
],
)

Expand All @@ -83,6 +84,14 @@ java_test(
],
)

java_test(
Comment thread
zbynek marked this conversation as resolved.
Outdated
name = "WebServerSecurityTest",
test_class = "com.google.gwt.dev.codeserver.WebServerSecurityTest",
runtime_deps = [
":testlib",
],
)

# Repackaged codeserver for google3.
AugmentedJar(
name = "codeserver",
Expand Down
51 changes: 50 additions & 1 deletion dev/codeserver/java/com/google/gwt/dev/codeserver/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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:

  • reject entire header on any malformed entry or
  • ignore invalid entries and continue parsing

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=")) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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".)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2026 Google Inc.
Comment thread
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}.
Comment thread
zbynek marked this conversation as resolved.
Outdated
*/
public class WebServerSecurityTest extends TestCase {

public void testAcceptsGzipEncodingRejectsNullOrEmptyHeader() {
assertFalse(WebServer.acceptsGzipEncoding(null));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

If no Accept-Encoding header field is in the request, any content coding is considered acceptable by the user agent.

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"));
}
}