Skip to content

fix(design): create OpenAI key file owner-only to close write-then-chmod race - #2468

Open
bunlongheng wants to merge 1 commit into
garrytan:mainfrom
bunlongheng:fix/security-apikey-file-perms-toctou
Open

fix(design): create OpenAI key file owner-only to close write-then-chmod race#2468
bunlongheng wants to merge 1 commit into
garrytan:mainfrom
bunlongheng:fix/security-apikey-file-perms-toctou

Conversation

@bunlongheng

Copy link
Copy Markdown

What

saveApiKey() in design/src/auth.ts persists the OpenAI API key to ~/.gstack/openai.json by writing at the process default umask and only tightening permissions afterwards:

fs.writeFileSync(configPath(), JSON.stringify({ api_key: key }, null, 2));
fs.chmodSync(configPath(), 0o600);

On a typical umask (022) the file is created 0644 (group/world-readable). Between the writeFileSync and the chmodSync there 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 } to writeFileSync, so it is never briefly world-readable. The trailing chmodSync is kept as a backstop that still tightens a pre-existing loose file.

fs.writeFileSync(configPath(), JSON.stringify({ api_key: key }, null, 2), { mode: 0o600 });
fs.chmodSync(configPath(), 0o600);

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 saveApiKey regression test that writes the key under a deliberately permissive umask (0o000) and asserts the resulting file is exactly 0600 with no group/other bits.

$ bun test design/test/auth.test.ts
 7 pass
 0 fail

Scope

Two files, design/src/auth.ts (+3/-1) and design/test/auth.test.ts (+21). No behavior change beyond the file-creation mode.

…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.
@trunk-io

trunk-io Bot commented Aug 6, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant