diff --git a/.github/scripts/check-symlinks.mjs b/.github/scripts/check-symlinks.mjs new file mode 100644 index 0000000000..f8fe2ad62c --- /dev/null +++ b/.github/scripts/check-symlinks.mjs @@ -0,0 +1,55 @@ +import { posix } from 'node:path'; +import { pathToFileURL } from 'node:url'; +import { execFileSync } from 'node:child_process'; + +export function unsafeSymlinks(cwd = process.cwd()) { + const git = (...args) => execFileSync('git', args, { cwd, encoding: 'utf8' }); + const links = new Map(); + // Read Git modes and blobs, including on checkouts with core.symlinks=false. + for (const entry of git('ls-files', '--stage', '-z').split('\0')) { + if (!entry.startsWith('120000 ')) continue; + const tab = entry.indexOf('\t'); + const [, hash] = entry.slice(0, tab).split(' '); + links.set(entry.slice(tab + 1), git('cat-file', 'blob', hash)); + } + + const unsafe = []; + for (const link of links.keys()) { + const pending = link.split('/'); + const resolved = []; + let followed = 0; + while (pending.length > 0) { + const component = pending.shift(); + if (component === '' || component === '.') continue; + if (component === '..') { + if (resolved.length === 0) { + unsafe.push(link); + break; + } + resolved.pop(); + continue; + } + resolved.push(component); + const target = links.get(resolved.join('/')); + if (target === undefined) continue; + if (posix.isAbsolute(target) || target === '' || ++followed > 40) { + unsafe.push(link); + break; + } + resolved.pop(); + pending.unshift(...target.split('/')); + } + } + return unsafe; +} + +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + const unsafe = unsafeSymlinks(); + for (const path of unsafe) { + console.error( + `Unsafe tracked symlink: ${JSON.stringify(path)}. Use a repository-relative, in-bounds target without cycles.`, + ); + } + if (unsafe.length > 0) process.exitCode = 1; + else console.log('All tracked symlinks stay within the repository.'); +} diff --git a/.github/scripts/check-symlinks.test.mjs b/.github/scripts/check-symlinks.test.mjs new file mode 100644 index 0000000000..2f74e816e1 --- /dev/null +++ b/.github/scripts/check-symlinks.test.mjs @@ -0,0 +1,80 @@ +import { test } from 'node:test'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import assert from 'node:assert/strict'; +import { fileURLToPath } from 'node:url'; +import { execFileSync, spawnSync } from 'node:child_process'; +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { unsafeSymlinks } from './check-symlinks.mjs'; + +const script = fileURLToPath(new URL('./check-symlinks.mjs', import.meta.url)); + +function fixture(t, links) { + const cwd = mkdtempSync(join(tmpdir(), 'librechat-symlinks-')); + t.after(() => rmSync(cwd, { recursive: true, force: true })); + const git = (...args) => execFileSync('git', args, { cwd, encoding: 'utf8' }); + git('init', '--quiet'); + git('config', 'core.symlinks', 'false'); + writeFileSync(join(cwd, 'file'), 'fixture'); + git('add', 'file'); + for (const [path, target] of Object.entries(links)) { + const hash = execFileSync('git', ['hash-object', '-w', '--stdin'], { + cwd, + input: target, + encoding: 'utf8', + }).trim(); + git('update-index', '--add', '--cacheinfo', `120000,${hash},${path}`); + } + return cwd; +} + +test('allows ordinary files and confined relative links', (t) => { + const cwd = fixture(t, { + alias: 'file', + 'nested/link': '../alias', + directory: 'nested', + chain: 'directory/link', + // An untracked or missing target cannot escape by itself. + dangling: 'missing', + }); + assert.deepEqual(unsafeSymlinks(cwd), []); +}); + +test('rejects the absolute developer-worktree link even without filesystem symlinks', (t) => { + const cwd = fixture(t, { 'prep-stream-dev-restack': '/home/developer/worktrees/restack' }); + assert.deepEqual(unsafeSymlinks(cwd), ['prep-stream-dev-restack']); + const result = spawnSync(process.execPath, [script], { cwd, encoding: 'utf8' }); + assert.equal(result.status, 1); + assert.match(result.stderr, /Unsafe tracked symlink: "prep-stream-dev-restack"/); +}); + +test('rejects parent traversal, including paths with whitespace and newlines', (t) => { + const cwd = fixture(t, { + 'nested/escape link\n': '../../outside', + sibling: '../repository-other/file', + }); + assert.deepEqual(unsafeSymlinks(cwd), ['nested/escape link\n', 'sibling']); +}); + +test('follows directory links before interpreting subsequent parent components', (t) => { + const cwd = fixture(t, { directory: '.', escape: 'directory/../outside' }); + assert.deepEqual(unsafeSymlinks(cwd), ['escape']); +}); + +test('rejects transitive escapes and cycles', (t) => { + const cwd = fixture(t, { + outside: '../outside', + indirect: 'outside/file', + cycle: 'nested/cycle', + 'nested/cycle': '../cycle', + }); + assert.deepEqual(unsafeSymlinks(cwd), ['cycle', 'indirect', 'nested/cycle', 'outside']); +}); + +test('checks tracked links only and exits successfully for a safe index', (t) => { + const cwd = fixture(t, { alias: 'file' }); + writeFileSync(join(cwd, 'untracked'), '/outside'); + const result = spawnSync(process.execPath, [script], { cwd, encoding: 'utf8' }); + assert.equal(result.status, 0, result.stderr); + assert.match(result.stdout, /All tracked symlinks stay within the repository/); +}); diff --git a/.github/workflows/repository-safety.yml b/.github/workflows/repository-safety.yml new file mode 100644 index 0000000000..a87a760867 --- /dev/null +++ b/.github/workflows/repository-safety.yml @@ -0,0 +1,32 @@ +name: Repository Safety + +on: + pull_request: + merge_group: + types: [checks_requested] + push: + branches: [dev, main] + +permissions: + contents: read + +concurrency: + group: repository-safety-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + symlinks: + name: Confine repository symlinks + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v5 + with: + persist-credentials: false + - uses: actions/setup-node@v5 + with: + node-version: '24.22.3' + - name: Test symlink guard + run: node --test .github/scripts/check-symlinks.test.mjs + - name: Reject out-of-bounds symlinks + run: node .github/scripts/check-symlinks.mjs diff --git a/prep-stream-dev-restack b/prep-stream-dev-restack deleted file mode 120000 index 6d9b7c550c..0000000000 --- a/prep-stream-dev-restack +++ /dev/null @@ -1 +0,0 @@ -/home/berry13/worktrees/librechat-theme-update/prep-stream-dev-restack \ No newline at end of file