Skip to content

Return failure on comment mismatch in CLI decompression#1453

Merged
copybara-service[bot] merged 4 commits intogoogle:masterfrom
0xazanul:fix/cli-comment-verification-return
Apr 15, 2026
Merged

Return failure on comment mismatch in CLI decompression#1453
copybara-service[bot] merged 4 commits intogoogle:masterfrom
0xazanul:fix/cli-comment-verification-return

Conversation

@0xazanul
Copy link
Copy Markdown
Contributor

Summary

  • Fix missing return BROTLI_FALSE in the final comment verification check of DecompressFile() (c/tools/brotli.c:1290-1297)
  • The -C flag now correctly rejects streams with missing or mismatched comments

The Bug

In DecompressFile(), the final comment state check at the end of successful decoding detects a mismatch but does not act on it:

/* Final check */
if (context->comment_state != COMMENT_OK) {
    fprintf(stderr, "corrupt input [%s]\n", ...);
    if (context->verbosity > 0) {
        fprintf(stderr, "reason: comment mismatch\n");
    }
}
return BROTLI_TRUE;  // ← unconditional, should be inside an else branch

The error message is printed to stderr, but the function returns BROTLI_TRUE regardless. The caller DecompressFiles() then keeps the output file and the process exits 0.

When this triggers

The early check (line 1231) only catches COMMENT_BAD mid-stream. The final check catches states that the early check misses:

Scenario State at final check Early check Final check
Stream has wrong comment bytes COMMENT_BAD ✅ caught n/a
Stream has no metadata block COMMENT_INIT missed prints error, returns TRUE
Stream has incomplete metadata COMMENT_READ missed prints error, returns TRUE

A tampered or re-encoded stream that strips the metadata block bypasses verification entirely.

Impact

Scripts relying on -C to validate stream integrity/fingerprint accept tampered streams:

# Intended: reject if comment doesn't match
brotli -d -C "$EXPECTED_FINGERPRINT" input.br -o output && process output
# Actual: always succeeds, "process output" always runs

The Fix

One-line change — add return BROTLI_FALSE inside the mismatch branch:

if (context->comment_state != COMMENT_OK) {
    fprintf(stderr, "corrupt input [%s]\n", ...);
    if (context->verbosity > 0) {
        fprintf(stderr, "reason: comment mismatch\n");
    }
    return BROTLI_FALSE;  // ← added
}
return BROTLI_TRUE;

This is consistent with every other error path in the function (lines 1237, 1246, 1280, 1307).

Test plan

  • Encode a file with -C <comment>, decode with matching -C → succeeds (no change)
  • Encode a file with -C <comment>, decode with wrong -C → now fails with exit code 1
  • Encode a file without -C, decode with -C <comment> → now fails (no metadata in stream, COMMENT_INIT at final check)
  • Decode without -C → succeeds (no change, comment_state initialized to COMMENT_OK)

0xazanul and others added 2 commits April 11, 2026 16:37
The final comment verification check in DecompressFile() detects when
comment_state is not COMMENT_OK but unconditionally returns BROTLI_TRUE.
This causes the -C (comment verification) flag to accept streams with
missing or mismatched comments, keeping the output file and exiting 0.

Add the missing return BROTLI_FALSE inside the mismatch branch so that
comment verification failures are properly propagated to the caller.
@eustas
Copy link
Copy Markdown
Collaborator

eustas commented Apr 13, 2026

Awesome, thanks!

@0xazanul
Copy link
Copy Markdown
Contributor Author

Hey if i report this in vrp google will this qualify?

@eustas
Copy link
Copy Markdown
Collaborator

eustas commented Apr 13, 2026

Likely not. This feature was requested by community,... but it seems to be not actually used (otherwise problem should have been already reported).

@copybara-service copybara-service bot merged commit 737ae47 into google:master Apr 15, 2026
94 checks passed
@0xazanul 0xazanul deleted the fix/cli-comment-verification-return branch April 15, 2026 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants