fix(design): create OpenAI key file owner-only to close write-then-chmod race - #2468
Open
bunlongheng wants to merge 1 commit into
Open
fix(design): create OpenAI key file owner-only to close write-then-chmod race#2468bunlongheng wants to merge 1 commit into
bunlongheng wants to merge 1 commit into
Conversation
…mod race
saveApiKey wrote ~/.gstack/openai.json at the default umask (typically
0644) and only tightened it to 0600 with a following chmodSync. Between
the write and the chmod the file containing the OpenAI API key is
group/world-readable, so any local user on a shared host can read the key
in that window (CWE-377 insecure file creation / CWE-367 TOCTOU).
Pass { mode: 0o600 } to writeFileSync so the file is created owner-only up
front, matching the convention already used for session files in
design/src/session.ts (garrytan#859). The trailing chmodSync is kept as a backstop
to tighten a pre-existing loose file.
Adds a regression test asserting the key file is 0600 (no group/other
bits) even when written under a permissive umask.
|
Merging to
After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here |
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.
What
saveApiKey()indesign/src/auth.tspersists the OpenAI API key to~/.gstack/openai.jsonby writing at the process default umask and only tightening permissions afterwards:On a typical umask (022) the file is created
0644(group/world-readable). Between thewriteFileSyncand thechmodSyncthere is a window where the file containing the OpenAI API key is readable by any other local user. This is CWE-377 (insecure file creation) / CWE-367 (TOCTOU) — low severity, only exploitable by a co-located local user on a shared/multi-user host, but it's a real key-disclosure window and a cheap thing to close.Fix
Create the file owner-only up front by passing
{ mode: 0o600 }towriteFileSync, so it is never briefly world-readable. The trailingchmodSyncis kept as a backstop that still tightens a pre-existing loose file.This matches the convention already used for session files in
design/src/session.ts({ mode: 0o600 }on create, landed in #859). #859 hardened the session files but the actual API key file kept the write-then-chmod pattern — this brings it in line.Test
Adds a
saveApiKeyregression test that writes the key under a deliberately permissive umask (0o000) and asserts the resulting file is exactly0600with no group/other bits.Scope
Two files,
design/src/auth.ts(+3/-1) anddesign/test/auth.test.ts(+21). No behavior change beyond the file-creation mode.