Repository navigation
fix(ci): reject workflow symlinks that resolve outside the repository #623
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
Open
ayush-that
wants to merge
5
commits into
TestSprite:main
Choose a base branch
from
ayush-that:fix/ci-init-symlink-ancestors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a137c2c
fix(ci): reject workflow symlinks that resolve outside the repository
ayush-that ea78a02
test(ci): simplify symlink regression setup
ayush-that 32fce87
Merge branch 'main' into fix/ci-init-symlink-ancestors
ayush-that 6c37aa5
test(ci): cover symlinked workflow ancestors without force
ayush-that 1e3eb63
test(ci): run workflow symlink checks without sockets
ayush-that File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import path from 'node:path'; | ||
| import { CLIError } from './errors.js'; | ||
|
|
||
| export interface TargetPathFs { | ||
| lstat(p: string): Promise<{ isFile: boolean; isSymbolicLink: boolean } | null>; | ||
| } | ||
|
|
||
| /** | ||
| * Inspect existing components before creating a target. Symlinks are refused | ||
| * unless the caller supplies a policy that validates their destination. | ||
| * Returns null at the first missing component. A race between inspection and | ||
| * writing remains, as with the agent installer's original path guard. | ||
| */ | ||
| export async function inspectTargetPath( | ||
| agentFs: TargetPathFs, | ||
| root: string, | ||
| relPath: string, | ||
| allowSymlink?: (target: string) => Promise<void>, | ||
| ): Promise<{ isFile: boolean } | null> { | ||
| const segments = relPath.split(process.platform === 'win32' ? /[/\\]+/ : /\/+/).filter(Boolean); | ||
| let current = root; | ||
| let finalIsFile = false; | ||
| for (const [i, seg] of segments.entries()) { | ||
| current = path.join(current, seg); | ||
| const ls = await agentFs.lstat(current); | ||
| if (ls === null) { | ||
| // This component and everything below it does not exist yet. | ||
| return null; | ||
| } | ||
| if (ls.isSymbolicLink && allowSymlink) { | ||
| await allowSymlink(current); | ||
| } else if (ls.isSymbolicLink) { | ||
| const shown = segments.slice(0, i + 1).join('/'); | ||
| throw new CLIError( | ||
| `refusing to write through a symlink: "${shown}" — installing here could place files outside --dir. Remove the symlink or choose a different --dir.`, | ||
| 5, | ||
| ); | ||
| } | ||
| if (i < segments.length - 1 && ls.isFile) { | ||
| const shown = segments.slice(0, i + 1).join('/'); | ||
| throw new CLIError(`cannot create ${relPath}: "${shown}" exists and is not a directory.`, 5); | ||
| } | ||
| finalIsFile = ls.isFile; | ||
| } | ||
| return { isFile: finalIsFile }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /* eslint-disable security/detect-non-literal-fs-filename -- Every filesystem path belongs to the isolated mkdtemp fixture; symlinks deliberately exercise the real CLI boundary. */ | ||
| import { execFile } from 'node:child_process'; | ||
| import { | ||
| mkdtemp, | ||
| mkdir, | ||
| readFile, | ||
| readdir, | ||
| realpath, | ||
| rm, | ||
| symlink, | ||
| writeFile, | ||
| } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { dirname, join, resolve } from 'node:path'; | ||
| import { promisify } from 'node:util'; | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
|
|
||
| const binary = resolve('dist/index.js'); | ||
| const fetchFixture = resolve('test/helpers/ci-init-fetch.mjs'); | ||
| const execute = promisify(execFile); | ||
| let fixture: string; | ||
|
|
||
| afterEach(async () => { | ||
| if (fixture) await rm(fixture, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe.skipIf(process.platform === 'win32')('ci init symlink containment', () => { | ||
| async function setup() { | ||
| fixture = await realpath(await mkdtemp(join(tmpdir(), 'testsprite-ci-path-'))); | ||
| const repo = join(fixture, 'repo'); | ||
| const outside = join(fixture, 'outside'); | ||
| const home = join(fixture, 'home'); | ||
| await Promise.all([repo, outside, home].map(dir => mkdir(dir))); | ||
| return { repo, outside, home }; | ||
| } | ||
|
|
||
| function run(cwd: string, home: string, force = false, workflowPath?: string) { | ||
| const env: NodeJS.ProcessEnv = { ...process.env, HOME: home, USERPROFILE: home }; | ||
| for (const key of Object.keys(env)) { | ||
| if (key.startsWith('TESTSPRITE_')) delete env[key]; | ||
| } | ||
| return execute( | ||
| process.execPath, | ||
| [ | ||
| '--import', | ||
| fetchFixture, | ||
| binary, | ||
| '--endpoint-url', | ||
| 'http://127.0.0.1:9', | ||
| 'ci', | ||
| 'init', | ||
| 'github', | ||
| '--project', | ||
| 'dummy-project', | ||
| '--env', | ||
| 'staging', | ||
| ...(force ? ['--force'] : []), | ||
| ...(workflowPath ? ['--path', workflowPath] : []), | ||
| ], | ||
| { | ||
| cwd, | ||
| env: { | ||
| ...env, | ||
| TESTSPRITE_API_KEY: 'sk-user-local-only', | ||
| TESTSPRITE_NO_TELEMETRY: '1', | ||
| TESTSPRITE_NO_UPDATE_NOTIFIER: '1', | ||
| }, | ||
| timeout: 10000, | ||
| }, | ||
| ).then( | ||
| result => ({ ...result, code: 0 }), | ||
| error => ({ | ||
| stdout: error.stdout as string, | ||
| stderr: error.stderr as string, | ||
| code: error.code as number, | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| for (const [location, force] of [ | ||
| ['.github', false], | ||
| ['.github', true], | ||
| ['.github/workflows', false], | ||
| ['.github/workflows', true], | ||
| ['.github/workflows/testsprite.yml', true], | ||
| ['.github/workflows/testsprite.yml.bak', true], | ||
| ] as const) { | ||
| it(`refuses external ${location} ${force ? 'with' : 'without'} --force before writing`, async () => { | ||
| const { repo, outside, home } = await setup(); | ||
| const target = | ||
| location.endsWith('.yml') || location.endsWith('.bak') | ||
| ? join(outside, 'sentinel') | ||
| : outside; | ||
| if (target !== outside) await writeFile(target, 'unchanged'); | ||
| const link = join(repo, location); | ||
| await mkdir(dirname(link), { recursive: true }); | ||
| if (location.endsWith('.bak')) | ||
| await writeFile(join(repo, '.github/workflows/testsprite.yml'), 'original workflow'); | ||
| await symlink(target, link); | ||
| const result = await run(repo, home, force); | ||
| expect(result.code, result.stderr).toBe(5); | ||
| expect(result.stderr).toContain(target); | ||
| expect(await readdir(outside)).toEqual(target === outside ? [] : ['sentinel']); | ||
| if (target !== outside) expect(await readFile(target, 'utf8')).toBe('unchanged'); | ||
| if (location.endsWith('.bak')) | ||
| expect(await readFile(join(repo, '.github/workflows/testsprite.yml'), 'utf8')).toBe( | ||
| 'original workflow', | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| for (const location of ['.github', '.github/workflows', '.github/workflows/testsprite.yml']) { | ||
| it(`allows internal ${location}`, async () => { | ||
| const { repo, home } = await setup(); | ||
| const target = join(repo, 'internal'); | ||
| if (location.endsWith('.yml')) await writeFile(target, 'original workflow'); | ||
| else await mkdir(target); | ||
| const link = join(repo, location); | ||
| await mkdir(dirname(link), { recursive: true }); | ||
| await symlink(target, link); | ||
| const result = await run(repo, home, true); | ||
| expect(result.code, result.stderr).toBe(0); | ||
| expect(await readFile(join(repo, '.github/workflows/testsprite.yml'), 'utf8')).toContain( | ||
| 'dummy-project', | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| it('refuses a dangling external ancestor without creating its target', async () => { | ||
| const { repo, outside, home } = await setup(); | ||
| await symlink(join(outside, 'missing'), join(repo, '.github')); | ||
| const result = await run(repo, home); | ||
| expect(result.code, result.stderr).toBe(5); | ||
| expect(await readdir(outside)).toEqual([]); | ||
| }); | ||
|
|
||
| it('checks a literal backslash in a POSIX workflow filename', async () => { | ||
| const { repo, outside, home } = await setup(); | ||
| const workflowPath = '.github/workflows/a\\b.yml'; | ||
| const target = join(outside, 'sentinel'); | ||
| await mkdir(join(repo, '.github/workflows'), { recursive: true }); | ||
| await writeFile(target, 'unchanged'); | ||
| await symlink(target, join(repo, workflowPath)); | ||
| const result = await run(repo, home, true, workflowPath); | ||
| expect(result.code, result.stderr).toBe(5); | ||
| expect(result.stderr).toContain(target); | ||
| expect(await readFile(target, 'utf8')).toBe('unchanged'); | ||
| }); | ||
|
|
||
| it('allows a symlinked working directory', async () => { | ||
| const { repo, home } = await setup(); | ||
| const alias = join(fixture, 'alias'); | ||
| await symlink(repo, alias); | ||
| const result = await run(alias, home); | ||
| expect(result.code, result.stderr).toBe(0); | ||
| expect(await readFile(join(repo, '.github/workflows/testsprite.yml'), 'utf8')).toContain( | ||
| 'dummy-project', | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Preload in the CLI child so the real command runs without opening API sockets. | ||
| globalThis.fetch = async (url, init) => { | ||
| if ( | ||
| url !== 'http://127.0.0.1:9/api/cli/v1/projects/dummy-project/env' || | ||
| init?.method !== 'GET' | ||
| ) { | ||
| throw new Error(`Unexpected request in ci init fixture: ${init?.method} ${url}`); | ||
| } | ||
| return new Response(JSON.stringify({ environments: [{ name: 'staging', isDefault: true }] }), { | ||
| headers: { 'content-type': 'application/json' }, | ||
| }); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.