Skip to content

fix(it): tamper the decoded GCM tag, not a base64 padding bit - #121

Merged
cuioss-oliver merged 1 commit into
mainfrom
fix/bff-cookie-tamper-padding-bits
Jul 28, 2026
Merged

fix(it): tamper the decoded GCM tag, not a base64 padding bit#121
cuioss-oliver merged 1 commit into
mainfrom
fix/bff-cookie-tamper-padding-bits

Conversation

@cuioss-oliver

Copy link
Copy Markdown
Collaborator

Problem

BffCookieStatelessnessIT.thePeerRefusesACookieItCannotUnseal failed post-merge on main (341ab9e, run 30334562679):

BffCookieStatelessnessIT.thePeerRefusesACookieItCannotUnseal:138 1 expectation failed.
Expected status code <401> but was <200>.

This is a defect in the test, not in the gateway. The peer was right to answer 200.

Root cause

The test broke the sealed cookie's authentication tag by flipping the final base64url character:

char last = sealed.charAt(sealed.length() - 1);
return sealed.substring(0, sealed.length() - 1) + (last == 'A' ? 'B' : 'A');

That is not reliably a mutation. The sealed payload is version(1) || key-id(1) || nonce(12) || ciphertext || tag(16). When its byte length is not a multiple of three, the final base64url character carries 2 or 4 unused padding bits, and Base64.getUrlDecoder() discards them without validating. 'A' (0b000000) → 'B' (0b000001) flips only the lowest bit — a padding bit in that case — so the value re-decodes to the identical ciphertext and tag. The GCM tag still authenticates, the peer unseals the session correctly, and the route returns 200.

Verified directly:

len(bytes)=32 len%3=2
encoded  = AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHgA
tampered = AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHgB   (last 'A' -> 'B')
decodes to IDENTICAL bytes? true

The no-op requires the last character to be 'A' and the sealed length to be a non-multiple of 3 — roughly 4% of runs, since the ciphertext length tracks the JWT size and varies per login. That is why the suite passed on the PR head (ba3edbc) and failed on main shortly after.

Fix

Mutate a byte of the decoded 16-byte GCM tag instead:

byte[] raw = Base64.getUrlDecoder().decode(sealed);
raw[raw.length - 1] ^= 0x01;
return Base64.getUrlEncoder().withoutPadding().encodeToString(raw);

Always a real tag mutation, so the probabilistic branch is gone entirely. The Javadoc records the padding-bit trap so the original shape is not reintroduced.

Verification

  • verify -Ppre-commit green
  • test-compile -pl integration-tests -am green
  • Integration Tests on this PR are the authoritative check — the same job that caught the failure

Not in scope

Performance Benchmark is also red on main, but it is pre-existing and unrelated: the same run-k6-passthrough-relay-benchmark checks, http_req_failed threshold crossing (exit 99) fails on PR #119 (a workflow-version bump) and on #117 / #116 before it. benchmarks/** is another plan's surface; reported, not touched here.

Note

Supersedes #120, which was closed unmerged. Same commit, no code change — reopened so the review bots get a clean run after both CodeRabbit and Sourcery hit account rate limits on the original.

🤖 Generated with Claude Code

https://claude.ai/code/session_01G99PK9iS9vgHvttFZ7ueir

BffCookieStatelessnessIT.thePeerRefusesACookieItCannotUnseal flipped the
final base64url character of the sealed cookie ('A' -> 'B') to break the
authentication tag. That is not reliably a mutation: when the sealed
length is not a multiple of three, the final character carries 2 or 4
unused padding bits, and Base64.getUrlDecoder() discards them without
validating. Flipping only those bits re-decodes to the identical
ciphertext and tag, so the peer unseals the cookie successfully and
answers 200 instead of the expected 401.

The no-op needs the last character to be 'A' AND the sealed length to be
non-multiple-of-3 — roughly 4% of runs, since the ciphertext length
tracks the JWT size. That is why the suite passed on the PR head and
failed post-merge on main.

Mutate a byte of the decoded 16-byte GCM tag instead, which is
unambiguous and removes the probabilistic branch entirely.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G99PK9iS9vgHvttFZ7ueir

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @cuioss-oliver, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@cuioss-oliver, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 46 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository: cuioss/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 4e5c126e-2b21-4e4e-912f-e36a803d4247

📥 Commits

Reviewing files that changed from the base of the PR and between 341ab9e and ff5bad7.

📒 Files selected for processing (1)
  • integration-tests/src/test/java/de/cuioss/sheriff/gateway/integration/BffCookieStatelessnessIT.java

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cuioss-review-bot

Copy link
Copy Markdown

PR Reviewer Guide 🔍

🧪 PR contains tests
🔒 No security concerns identified
⚡ No major issues detected

@cuioss-oliver
cuioss-oliver added this pull request to the merge queue Jul 28, 2026
Merged via the queue into main with commit 8d85779 Jul 28, 2026
44 checks passed
@cuioss-oliver
cuioss-oliver deleted the fix/bff-cookie-tamper-padding-bits branch July 28, 2026 07:41
cuioss-oliver added a commit that referenced this pull request Jul 29, 2026
…fix its tamper helper

Two fixes in the cookie-session IT:

1. sealedCookieValueCarriesNoReadableToken asserted the sealed cookie's
   leading version byte is 1. FORMAT_VERSION is now 2, so assert 2.

2. tamperedCookieIsUnauthenticatedNeverServerError used the base64-padding-bit
   tamper helper that PR #121 replaced in BffCookieStatelessnessIT but left in
   place here. Flipping the final base64url character is not reliably a
   mutation: when the sealed length is not a multiple of three that character
   carries unused padding bits which the decoder discards, so the value
   re-decodes identically and the peer correctly answers 200. Adding the
   9th sealed-payload field shifted the length so the degenerate case became
   deterministic rather than the ~4% flake #121 described. Apply the same
   decoded-GCM-tag mutation the sibling IT already uses.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013EfShpgB9HbUSf9n6zhP9r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant