Skip to content

Accept-Encoding: * wildcard is ignored during compression negotiation #193

@VikramAditya33

Description

@VikramAditya33

Per RFC 7231, Accept-Encoding: * means the client accepts any encoding. Currently, CompressionHandler.parseAcceptEncoding treats * as an unknown token with priority 0, so responses to Accept-Encoding: * requests are sent uncompressed.

Reproduction

const handler = new CompressionHandler({ brotli: true });
// Client says "I accept any encoding"
setupMock('*', 'text/plain');

const stream = handler.createCompressionStream(req, res, largeBody);
// Expected: brotli compression stream
// Actual: null (no compression applied)

Root Cause

parseAcceptEncoding parses * literally into the encodings list, but createCompressionStream's switch statement only matches br, gzip, and deflate. The * token falls through all cases.

Proposed Fix

Expand * into supported encodings that are not explicitly listed or explicitly rejected (q=0):

// In parseAcceptEncoding:
const rejected = new Set<string>();
let wildcardQuality: number | null = null;

// ...during parsing:
if (encoding.trim() === '*') {
  wildcardQuality = quality;
  continue;
}

if (quality > 0) {
  encodings.push({ ... });
} else {
  rejected.add(encoding.trim().toLowerCase());
}

// ...after parsing:
if (wildcardQuality !== null && wildcardQuality > 0) {
  for (const supported of ['br', 'gzip', 'deflate']) {
    if (!encodings.some((e) => e.encoding === supported) && !rejected.has(supported)) {
      encodings.push({
        encoding: supported,
        quality: wildcardQuality,
        position: Number.MAX_SAFE_INTEGER,
      });
    }
  }
}

Edge Case Handled

Accept-Encoding: gzip;q=0, * → wildcard expands to br and deflate only; gzip remains excluded because it was explicitly rejected.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggood first issueGood for newcomers

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions