Repository navigation
Fix/1746 admin auth - #1755
Merged
Merged
Fix/1746 admin auth#1755
Conversation
Author
|
@Baskarayelu Pls review and merge |
Author
|
@Baskarayelu conflict resolved and if you still get one more for, i will really appreciate to do one more contribution. Thanking you in advance |
Author
|
@Baskarayelu be mindful what you merge, I have a friend who's project was attacked with malware today, pls check carefully before you merge any contibutors code |
10 tasks
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.
security(admin): implement constant-time header-only admin guard (#1746)
Closes #1746
Summary of Changes
Re-implemented
@/lib/admin/auth(lib/admin/auth.ts) and its unit test suite (lib/admin/__tests__/auth.test.ts) to provide an administrative authentication guard for protected endpoints (such asapp/api/cache/invalidate/route.ts).The public interface matches the contract expected by consumer routes and existing integration tests:
isAdminAuthorized(request): booleangetAdminIdentity(request): stringSecurity Problems in the Prior Implementation & How They Are Resolved
1. Cross-Site Request Forgery (CSRF)
admin_keyoradmin_secretbrowser cookies. Browsers attach cookies automatically to cross-site requests. Consequently, an admin visiting an untrusted website could have a cross-origin form or script execute aPOST /api/cache/invalidaterequest with{"clearAll": true}, wiping the contract cache.x-admin-keyHTTP header. Cookies and URL query parameters are strictly ignored.2. Length-Oracle Timing Attack
crypto.timingSafeEqualdirectly on the provided and expected secret byte buffers. When inputs differed in length,crypto.timingSafeEqualthrew an immediate exception. Catching this mismatch and returningfalseintroduced a measurable timing difference between wrong-length inputs (which failed instantly) and equal-length inputs (which proceeded to byte comparison), leaking the secret's character count to attackers.crypto.createHmac("sha256", PROCESS_HMAC_KEY)). Because HMAC-SHA256 digests are uniformly 32 bytes regardless of input length,crypto.timingSafeEqualalways compares equal-length buffers in constant time without throwing exceptions.3. Weak Secret and Whitespace Handling
"a") and called.trim()on the header input, silently ignoring whitespace discrepancies.ADMIN_SECRETmust be at least 32 characters long. If unset or shorter than 32 characters, all requests are rejected and a single startupconsole.erroris logged naming the variable (ADMIN_SECRET) without leaking its value. The incomingx-admin-keyheader is evaluated as-is without.trim(), so any trailing or leading whitespace properly fails closed. Key rotation is supported via comma-separated secrets inADMIN_SECRET.4. Audit Attribution
header:x-admin-key) regardless of which key was used, even if invalid.getAdminIdentity(req)returnsadmin:<hash>(where<hash>is the first 8 hex characters of the SHA-256 digest of the matched secret), providing a stable identifier across calls without disclosing the secret itself. Unauthenticated requests return'anonymous'.Comparison Code