diff --git a/scripts/skills/update-ai-attribution.test.ts b/scripts/skills/update-ai-attribution.test.ts index 3c1a94bee54..d8b9351237c 100644 --- a/scripts/skills/update-ai-attribution.test.ts +++ b/scripts/skills/update-ai-attribution.test.ts @@ -1,8 +1,11 @@ import { + chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, + statSync, + symlinkSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' @@ -16,6 +19,41 @@ import { } from './update-ai-attribution' describe('updateAttributionSettings', () => { + it('creates settings files with owner-only permissions', () => { + const home = mkdtempSync(join(tmpdir(), 'update-ai-attribution-')) + try { + updateAttributionSettings(home) + + expect( + statSync(join(home, '.claude', 'settings.json')).mode & 0o777 + ).toBe(0o600) + expect( + statSync(join(home, '.config', 'amp', 'settings.json')).mode & 0o777 + ).toBe(0o600) + } finally { + rmSync(home, { recursive: true, force: true }) + } + }) + + it('does not follow a symlink at the old predictable temporary path', () => { + const home = mkdtempSync(join(tmpdir(), 'update-ai-attribution-')) + try { + const claudeDirectory = join(home, '.claude') + const claudePath = join(claudeDirectory, 'settings.json') + const targetPath = join(home, 'target') + mkdirSync(claudeDirectory, { recursive: true }) + writeFileSync(targetPath, 'unchanged') + symlinkSync(targetPath, `${claudePath}.${process.pid}.tmp`) + + const [claudeResult] = updateAttributionSettings(home) + + expect(claudeResult.outcome).toBe('updated') + expect(readFileSync(targetPath, 'utf8')).toBe('unchanged') + } finally { + rmSync(home, { recursive: true, force: true }) + } + }) + it('updates only attribution settings without exposing other values', () => { const home = mkdtempSync(join(tmpdir(), 'update-ai-attribution-')) try { @@ -34,6 +72,8 @@ describe('updateAttributionSettings', () => { ampPath, '{\n // Keep this comment.\n "token": "amp-secret"\n}\n' ) + chmodSync(claudePath, 0o640) + chmodSync(ampPath, 0o660) const firstResults = updateAttributionSettings(home) const output = formatResults(firstResults) @@ -63,6 +103,8 @@ describe('updateAttributionSettings', () => { 'amp.git.commit.ampThread.enabled': false, 'amp.git.commit.coauthor.enabled': false }) + expect(statSync(claudePath).mode & 0o777).toBe(0o640) + expect(statSync(ampPath).mode & 0o777).toBe(0o660) expect( updateAttributionSettings(home).map(({ outcome }) => outcome) ).toEqual([ diff --git a/scripts/skills/update-ai-attribution.ts b/scripts/skills/update-ai-attribution.ts index 6e57767adb0..f55f251c31b 100644 --- a/scripts/skills/update-ai-attribution.ts +++ b/scripts/skills/update-ai-attribution.ts @@ -1,4 +1,5 @@ #!/usr/bin/env tsx +import { randomUUID } from 'node:crypto' import { chmodSync, existsSync, @@ -82,13 +83,13 @@ function parseSettings(content: string): Record { function writeSettings(path: string, content: string) { mkdirSync(dirname(path), { recursive: true }) - const temporaryPath = `${path}.${process.pid}.tmp` - const mode = existsSync(path) ? statSync(path).mode : undefined + const temporaryPath = `${path}.${randomUUID()}.tmp` + const mode = existsSync(path) ? statSync(path).mode & 0o777 : 0o600 try { - writeFileSync(temporaryPath, content, { mode }) + writeFileSync(temporaryPath, content, { flag: 'wx', mode }) renameSync(temporaryPath, path) - if (mode !== undefined) chmodSync(path, mode) + chmodSync(path, mode) } finally { rmSync(temporaryPath, { force: true }) }