fix: prevent OAuth CSRF via missing state parameter in authorization flow#1135
Open
kuranikaran wants to merge 1 commit intogoogle:masterfrom
Open
fix: prevent OAuth CSRF via missing state parameter in authorization flow#1135kuranikaran wants to merge 1 commit intogoogle:masterfrom
kuranikaran wants to merge 1 commit intogoogle:masterfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
0c6a56b to
7ecda25
Compare
Author
|
@sqrrrl this fixes an OAuth CSRF vulnerability in the login flow (missing state parameter per RFC 6749 §10.12) and hardens credential file permissions. Would appreciate a review when you get a chance. |
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.
Title
fix: prevent OAuth CSRF via missing state parameter in authorization flow
Body
This PR fixes an OAuth 2.0 CSRF vulnerability in the authorization code flow. Previously,
clasp logingenerated an authorization URL without astateparameter, and the callback handlers accepted anycodevalue without verifying its origin. This allowed an attacker to inject their own authorization code, causing the victim to authenticate as the attacker (account context confusion).Additionally,
.clasprc.jsonwas written with default0644permissions, making OAuth tokens readable by any local user or process.Changes
Added CSRF State Validation:
src/auth/auth_code_flow.ts—authorize()now generates a cryptographically randomstatevalue (32 bytes, base64url-encoded) and includes it in the authorization URL.parseAuthResponseUrl()now extracts thestateparameter from callback URLs.src/auth/localhost_auth_code_flow.ts— The local HTTP callback validates the returnedstateagainst the expected value. Mismatched or missing state returns HTTP 400 and rejects the authorization attempt.src/auth/serverless_auth_code_flow.ts— The pasted redirect URL is checked for a matchingstateparameter before accepting the authorization code.Hardened Credential Storage:
src/auth/file_credential_store.ts—writeFile()now writes.clasprc.jsonwith0600permissions and callschmodSyncto tighten existing files that may have been created with broader permissions.Added Tests:
test/auth/auth_code_flow.ts— Tests forgenerateState()uniqueness, URL-safety, andparseAuthResponseUrl()extraction of code, state, and error parameters.test/auth/file_credential_store.ts— Tests that new credential files are created with0600permissions and that existing files with0644are tightened on write.Impact
This fix prevents an attacker from injecting their own OAuth authorization code into a victim's
clasp loginsession. Without this fix, a successful attack could result in:clasp pushto attacker's projectclasp pulland receives attacker-controlled code.clasprc.jsongrant ongoing access to the victim's Apps Script projects and Drive filesViolates OAuth 2.0 security best practices per RFC 6749 Section 10.12 (CSRF Protection). CWE-352 (Cross-Site Request Forgery), CWE-732 (Incorrect Permission Assignment for Critical Resource).
Verification
A mock-based regression checker is included as
poc-csrf-demo.cjs:Against the original source: 4 of 6 checks fail (accept without state validation).
Against the patched source: all 6 checks pass.