🦮 fix: Confine Repository Symlinks for Argo CD (#18402)

Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
lia-by-librechat[bot] 2026-11-18 07:51:14 -05:00 • committed by GitHub
parent 445668a371
commit 9cfa807950
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 167 additions and 1 deletions

55
.github/scripts/check-symlinks.mjs vendored Normal file
View file

@ -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.');
}

80
.github/scripts/check-symlinks.test.mjs vendored Normal file
View file

@ -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/);
});

32
.github/workflows/repository-safety.yml vendored Normal file
View file

@ -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

View file

@ -1 +0,0 @@
/home/berry13/worktrees/librechat-theme-update/prep-stream-dev-restack