Repository navigation
fix: restrict permissions before writing pulled secrets #341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { chmodSync, lstatSync, mkdtempSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from 'node:fs' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import { expect, it } from 'vitest' | ||
| import { secrets } from '../src/commands/secrets.js' | ||
|
|
||
| it.each([ | ||
| { name: 'new file', mode: undefined, linked: false }, | ||
| { name: 'existing readable file', mode: 0o644, linked: false }, | ||
| { name: 'existing write-only file', mode: 0o200, linked: false }, | ||
| { name: 'symlink target', mode: 0o644, linked: true }, | ||
| ])('writes private secret output: $name', async ({ mode, linked }) => { | ||
| if (linked && process.platform === 'win32') return | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: The symlink case is skipped unconditionally on Windows before any setup runs, so the fix's core behavior — rewriting credentials through an existing symlink without replacing it — is never exercised in the Windows CI job. GitHub Actions windows-latest runners run as an admin that can create symlinks; try the symlink and skip only when creation actually fails (e.g., EPERM on non-privileged dev machines), keeping the symlink target and lstat assertions on runners that support it. Prompt for AI agents |
||
| const dir = mkdtempSync(join(tmpdir(), 'insta-secrets-permissions-')) | ||
| const cwd = process.cwd() | ||
| const target = join(dir, 'secrets.env') | ||
| const output = linked ? join(dir, '.env') : target | ||
| try { | ||
| process.chdir(dir) | ||
| if (mode !== undefined) { | ||
| writeFileSync(target, 'OLD_SECRET=' + 'old-private-value'.repeat(20) + '\n') | ||
| chmodSync(target, mode) | ||
| } | ||
| if (linked) symlinkSync('secrets.env', output) | ||
| await secrets({ output }, { | ||
| projectId: 'p1', linkedBranch: 'main', | ||
| api: { rawRequest: async () => ({ status: 200, body: { secrets: { PASSWORD: 'private-value' } } }) }, | ||
| }) | ||
| if (process.platform !== 'win32') expect(statSync(target).mode & 0o777).toBe(mode === 0o200 ? 0o200 : 0o600) | ||
| if (linked) expect(lstatSync(output).isSymbolicLink()).toBe(true) | ||
| chmodSync(target, 0o600) | ||
| expect(readFileSync(target, 'utf8')).toBe('PASSWORD="private-value"\n') | ||
| } finally { | ||
| process.chdir(cwd) | ||
| rmSync(dir, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: This unconditionally chmods non-regular output targets such as
/dev/null, so a common discard destination can fail or have its device permissions changed. Restrict the chmod to regular files or reject non-regular destinations.Prompt for AI agents