Resolve Unsigned Authentication Bypass and Forgeable Sessions in agent-spend-permissions - #139
Open
mozluk wants to merge 1 commit into
Open
Resolve Unsigned Authentication Bypass and Forgeable Sessions in agent-spend-permissions#139mozluk wants to merge 1 commit into
agent-spend-permissions#139mozluk wants to merge 1 commit into
Conversation
…nt-spend-permissions`
### Description
This pull request completely overhauls the session and authentication layer of `agent-spend-permissions` to close multiple critical and high-severity access control vulnerabilities (F-02, F-04)[cite: 30]. Previously, the application authorized mainnet USDC spending based on an unsigned, trivial-to-forge base64 session token, and verified SIWE signatures without asserting domain bindings or tracking nonces safely[cite: 30].
### Key Changes & Remediations
#### 1. Forgeable Session Mitigation (F-02)
* **Cryptographic Signatures:** The application no longer relies on an unsigned `Buffer.from("0xADDRESS:TIMESTAMP").toString('base64')` cookie to authorize operations[cite: 30]. `src/lib/session.ts` now wraps payloads with a strict `v1.<payload>.<mac>` standard authenticated by a constant-time HMAC-SHA256 signature using a mandatory `SESSION_SECRET`[cite: 30].
* **Route Protection:** All endpoints (`/api/wallet/create`, `/api/search`, `/api/chat`) that manipulate or spend on behalf of server wallets have been strictly transitioned to read exclusively from the new, signature-verified tokens[cite: 30].
#### 2. SIWE Verification Hardening (F-04)
* **ERC-6492 / ERC-1271 Support:** The `verify` route now utilizes `viem`'s `parseSiweMessage` and `verifySiweMessage` rather than a weak regex match[cite: 30]. This fully supports Smart Wallet signature recovery while correctly checking issue/expiry timestamps[cite: 30].
* **Domain Binding:** `SIWE_DOMAIN` enforcement (or `Host` header fallback) guarantees signatures obtained for other sites can no longer be replayed here[cite: 30].
#### 3. Stateless Nonces & Memory Leak Prevention (F-04)
* **Self-Authenticating Nonces:** Replaced the previous module-level `Set<string>` — which introduced severe memory leaks and erratic cross-instance sign-in failures — with stateless, HMAC-verified nonces that carry their own built-in `uint32` expiry[cite: 30].
* **Safe Replay Bounds:** Tracked nonces are opportunistically pruned and safely bounded to `10,000` concurrent uses to prevent memory exhaustion vectors.
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.
Description
This pull request completely overhauls the session and authentication layer of
agent-spend-permissionsto close multiple critical and high-severity access control vulnerabilities (F-02, F-04)[cite: 30]. Previously, the application authorized mainnet USDC spending based on an unsigned, trivial-to-forge base64 session token, and verified SIWE signatures without asserting domain bindings or tracking nonces safely[cite: 30].Key Changes & Remediations
1. Forgeable Session Mitigation (F-02)
Buffer.from("0xADDRESS:TIMESTAMP").toString('base64')cookie to authorize operations[cite: 30].src/lib/session.tsnow wraps payloads with a strictv1.<payload>.<mac>standard authenticated by a constant-time HMAC-SHA256 signature using a mandatorySESSION_SECRET[cite: 30]./api/wallet/create,/api/search,/api/chat) that manipulate or spend on behalf of server wallets have been strictly transitioned to read exclusively from the new, signature-verified tokens[cite: 30].2. SIWE Verification Hardening (F-04)
verifyroute now utilizesviem'sparseSiweMessageandverifySiweMessagerather than a weak regex match[cite: 30]. This fully supports Smart Wallet signature recovery while correctly checking issue/expiry timestamps[cite: 30].SIWE_DOMAINenforcement (orHostheader fallback) guarantees signatures obtained for other sites can no longer be replayed here[cite: 30].3. Stateless Nonces & Memory Leak Prevention (F-04)
Set<string>— which introduced severe memory leaks and erratic cross-instance sign-in failures — with stateless, HMAC-verified nonces that carry their own built-inuint32expiry[cite: 30].10,000concurrent uses to prevent memory exhaustion vectors.