[PR #6520] [MERGED] fix(nodetoken): make the corrupt-ciphertext test corrupt deterministically #6327

Closed
opened 2026-11-02 04:07:58 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6520
Author: @BlindMaster24
Created: 9/14/2026
Status: ✅ Merged
Merged: 9/14/2026
Merged by: @MHSanaei

Base: main ← Head: fix/nodetoken-deterministic-corruption


📝 Commits (2)

  • 6fc7b14 fix(nodetoken): make the corrupt-ciphertext test corrupt deterministically
  • 5ea96a1 style(nodetoken): trim the helper comment to the two-line limit

📊 Changes

1 file changed (+14 additions, -1 deletions)

View changed files

📝 internal/crypto/nodetoken/nodetoken_test.go (+14 -1)

📄 Description

Summary

TestEncryptedNeverFallsBackToPlaintext corrupts the ciphertext by replacing the last two characters of the base64 body with "AA". On average once every ~300 runs that edit changes nothing, and the test fails on a codec that is working exactly as designed. The corruption now flips a bit of the decoded blob instead.

Why

Codec.EncryptBound stores base64.RawURLEncoding of an nonce(12) || ciphertext || tag(16) blob. For the 31-byte blob this test produces, the encoding ends in a 2-character group: the first character carries 6 significant bits of the last byte, the second carries only 2 — its low 4 bits are always zero and are ignored by the decoder.

So enc[:len(enc)-2] + "AA" decodes to the original bytes whenever the final byte of the GCM tag is 0x00, i.e. 1 run in 256. The assertion then reads a tag that was never touched and the codec correctly falls back to… no fallback at all, which is precisely what the test is about, so the test reports a failure that is entirely its own.

Measured on this checkout, over 50 000 encryptions, comparing the decoded bytes before and after the edit:

no-op corruptions: 170/50000 (1 in 294)

and running the test itself 5000 times on the unmodified code: 19 failures, all of them nodetoken_test.go:101.

This is not theoretical: CI on #6519 failed go-test on exactly this test with corrupted ciphertext must fail, not fall back to plaintext, while race, golangci, the frontend job and every build passed. A flaky red on an unrelated PR costs a maintainer a real investigation, and re-running the job to get green hides it.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

(Multi-node token encryption at rest — internal/crypto/nodetoken.)

How was this tested?

The change is verified by repetition, since the bug is a probability:

  • unmodified test, go test ./internal/crypto/nodetoken/ -run TestEncryptedNeverFallsBackToPlaintext -count=5000 → 19 failures
  • with this change, -count=20000 → 0 failures
  • a throwaway probe that decoded both the original and the corrupted body and compared them byte for byte → 170 no-op edits in 50 000
  • go test ./internal/crypto/nodetoken/ and gofmt clean

(The second assertion in the test — an unknown key id — is a literal prefix replacement and was already deterministic; only the trailing-character edit was fragile.)

Screenshots / recordings

N/A.

Breaking changes

None — test-only.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally. (TestFileKeySourceRejectsLoosePerms fails on a Windows checkout because chmod 0600 is not reported there; it passes on Linux, which is where CI runs.)
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6520 **Author:** [@BlindMaster24](https://github.com/BlindMaster24) **Created:** 9/14/2026 **Status:** ✅ Merged **Merged:** 9/14/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/nodetoken-deterministic-corruption` --- ### 📝 Commits (2) - [`6fc7b14`](https://github.com/MHSanaei/3x-ui/commit/6fc7b14a24b576f7c8ea089b90f1d82d1d1e3470) fix(nodetoken): make the corrupt-ciphertext test corrupt deterministically - [`5ea96a1`](https://github.com/MHSanaei/3x-ui/commit/5ea96a1063caf5463f4d0ef956f75a5562674062) style(nodetoken): trim the helper comment to the two-line limit ### 📊 Changes **1 file changed** (+14 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `internal/crypto/nodetoken/nodetoken_test.go` (+14 -1) </details> ### 📄 Description ## Summary `TestEncryptedNeverFallsBackToPlaintext` corrupts the ciphertext by replacing the last two characters of the base64 body with `"AA"`. On average once every ~300 runs that edit changes nothing, and the test fails on a codec that is working exactly as designed. The corruption now flips a bit of the decoded blob instead. ## Why `Codec.EncryptBound` stores `base64.RawURLEncoding` of an `nonce(12) || ciphertext || tag(16)` blob. For the 31-byte blob this test produces, the encoding ends in a 2-character group: the first character carries 6 significant bits of the last byte, the second carries only 2 — its low 4 bits are always zero and are ignored by the decoder. So `enc[:len(enc)-2] + "AA"` decodes to the original bytes whenever the final byte of the GCM tag is `0x00`, i.e. 1 run in 256. The assertion then reads a tag that was never touched and the codec correctly falls back to… no fallback at all, which is precisely what the test is about, so the test reports a failure that is entirely its own. Measured on this checkout, over 50 000 encryptions, comparing the decoded bytes before and after the edit: ``` no-op corruptions: 170/50000 (1 in 294) ``` and running the test itself 5000 times on the unmodified code: **19 failures**, all of them `nodetoken_test.go:101`. This is not theoretical: CI on #6519 failed `go-test` on exactly this test with `corrupted ciphertext must fail, not fall back to plaintext`, while `race`, `golangci`, the frontend job and every build passed. A flaky red on an unrelated PR costs a maintainer a real investigation, and re-running the job to get green hides it. ## Type of change - [ ] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [x] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [ ] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot (Multi-node token encryption at rest — `internal/crypto/nodetoken`.) ## How was this tested? The change is verified by repetition, since the bug is a probability: - unmodified test, `go test ./internal/crypto/nodetoken/ -run TestEncryptedNeverFallsBackToPlaintext -count=5000` → **19 failures** - with this change, `-count=20000` → **0 failures** - a throwaway probe that decoded both the original and the corrupted body and compared them byte for byte → 170 no-op edits in 50 000 - `go test ./internal/crypto/nodetoken/` and `gofmt` clean (The second assertion in the test — an unknown key id — is a literal prefix replacement and was already deterministic; only the trailing-character edit was fragile.) ## Screenshots / recordings N/A. ## Breaking changes None — test-only. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. (`TestFileKeySourceRejectsLoosePerms` fails on a Windows checkout because `chmod 0600` is not reported there; it passes on Linux, which is where CI runs.) - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:07:58 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6327
No description provided.