Skip to content

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

Closed
cuioss-oliver wants to merge 1 commit into
mainfrom
fix/bff-cookie-tamper-padding-bits
Closed

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

Conversation

@cuioss-oliver

@cuioss-oliver cuioss-oliver commented Jul 28, 2026

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.

🤖 Generated with Claude Code

https://claude.ai/code/session_01G99PK9iS9vgHvttFZ7ueir

Summary by CodeRabbit

  • Tests
    • Improved sealed-cookie tampering coverage by mutating the decoded authentication data directly.
    • Increased test reliability by avoiding false positives caused by Base64 encoding details.

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

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

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

Review profile: CHILL

Plan: Pro Plus

Run ID: 2e00d8fb-8cf1-436c-a2ab-9e28c8a1d231

📥 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

📝 Walkthrough

Walkthrough

The integration test’s sealed-cookie tampering helper now decodes the cookie, flips the final decoded byte, and re-encodes it as unpadded Base64URL. Its documentation and imports were updated accordingly.

Changes

Sealed-cookie tampering

Layer / File(s) Summary
Byte-level cookie mutation
integration-tests/src/test/java/de/cuioss/sheriff/gateway/integration/BffCookieStatelessnessIT.java
Adds Base64 support and changes tampering from altering the final encoded character to flipping the final decoded byte before unpadded Base64URL re-encoding.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: mutating the decoded GCM tag instead of a Base64 padding bit.

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

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@cuioss-oliver

Copy link
Copy Markdown
Collaborator Author

Closing unmerged and reopening as a fresh PR so the review bots get a clean run — CodeRabbit and Sourcery both hit account rate limits when this PR was first opened, so its review history is not representative. No code change: the branch is already rebased on main (341ab9e) with no conflicts.

@cuioss-oliver
cuioss-oliver deleted the fix/bff-cookie-tamper-padding-bits branch July 28, 2026 07:41
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