Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions scripts/skills/update-ai-attribution.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {
chmodSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
statSync,
symlinkSync,
writeFileSync
} from 'node:fs'
import { tmpdir } from 'node:os'
Expand All @@ -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)
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
} 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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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([
Expand Down
9 changes: 5 additions & 4 deletions scripts/skills/update-ai-attribution.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env tsx
import { randomUUID } from 'node:crypto'
import {
chmodSync,
existsSync,
Expand Down Expand Up @@ -82,13 +83,13 @@ function parseSettings(content: string): Record<string, unknown> {

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 })
}
Expand Down
Loading