test(metrics): cover equal-length wrong-token + malformed-header paths (#342)#343
Merged
Merged
Conversation
…paths
`checkMetricsAuth` in `app/middleware/metrics.js` branches three
ways:
1. Missing Authorization header → 401.
2. Non-"Bearer <token>" shape → 401.
3. Bearer-prefixed but token doesn't match:
a. Equal-length supplied vs required → timingSafeEqual.
b. Different-length → sha256-of-both fallback, returning false.
Pre-existing tests covered (1), (3b) (the "wrong token" case used
a value of different length than the configured token), and the
happy path. Missing:
- (2) — a header without the `Bearer ` prefix. Without an explicit
test, a future "lenient parser" refactor that fell through to
treat the raw header value as the token could slip past CI.
- (3a) — the timingSafeEqual branch with a same-length wrong
token. A future "optimization" that returned true on equal
bytes for the wrong reason would be invisible until a security
scanner caught it.
Adds both cases; test count 788 → 790.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #342.
Summary
Adds two missing test cases for
checkMetricsAuth: (a) non-Bearerprefix, (b) equal-length wrong token (thetimingSafeEqualbranch — different-length tokens hit a different code path that was already covered).Test plan
npm run lint && npm test— 790 passing (was 788).Proudly Made in Nebraska. Go Big Red! 🌽 https://xkcd.com/2347/