gitoria
All repositories: gitoria
7.7 KB
// tests/pull-ssh.mjs — PULL REQUESTS OVER SSH (gitoria#40 addendum): the REAL sshd container (docker/sshd, built here) around a push.// gitoria-shell calls /__git/pushed before and after receive-pack: a |||PR commit pushed over ssh gets its number and refs/pull/N/head, the log// shows `opened` with the pusher's user id; a wrong secret is refused; a push to refs/pull/* over ssh is refused.// node tests/pull-ssh.mjs (GITORIA_PULLSSH_PORT default 8725, GITORIA_PULLSSH_SSH_PORT default 8726)import { spawn, spawnSync } from 'node:child_process';import { request as httpRequest } from 'node:http';import { rmSync, mkdirSync, writeFileSync, readFileSync, existsSync } from 'node:fs';import { dirname, join, resolve } from 'node:path';import { fileURLToPath } from 'node:url';const APP = resolve(dirname(fileURLToPath(import.meta.url)), '..');const PORT = Number(process.env.GITORIA_PULLSSH_PORT || 8725), SSHPORT = Number(process.env.GITORIA_PULLSSH_SSH_PORT || 8726);const STORE = join(APP, '.scratch', 'pullssh-store'), DB = join(STORE, 'db'), GIT = join(STORE, 'git'), WORK = join(STORE, 'work'), KEY = join(STORE, 'key');const LOG = join(DB, 'pull-changes.log'), SECRET = 'w40-' + Math.random().toString(16).slice(2) + 'ssh', CONTAINER = 'gitoria-sshd-w40gate';const BIN = join(APP, 'bin/hybriel');const env = { ...process.env, GITORIA_STORAGE: DB, GITORIA_GIT: GIT, GITORIA_PORT: String(PORT), GITORIA_WATCH: '0', GITORIA_PUBLIC_URL: `http://gitoria.test:${PORT}`, GITORIA_TICKETS_URL: 'http://127.0.0.1:1', GITORIA_SSH_SECRET: SECRET, GITORIA_SSH_PORT: String(SSHPORT) };let failures = 0, passes = 0;function check(label, ok, detail = '') { console.log(`${ok ? 'ok ' : 'FAIL'} ${label}${ok ? '' : ' — ' + String(detail).slice(0, 500)}`); if (ok) passes++; else failures++; }const sleep = (ms) => new Promise(r => setTimeout(r, ms));const raw = (path, headers) => new Promise((res, rej) => { const r = httpRequest({ host: '127.0.0.1', port: PORT, path, headers: { host: 'gitoria.test', ...headers } }, (x) => { let b = ''; x.on('data', d => b += d); x.on('end', () => res({ status: x.statusCode, body: b })); }); r.on('error', rej); r.end(); });const gitEnv = { ...process.env, GIT_TERMINAL_PROMPT: '0', GIT_AUTHOR_NAME: 'Dev', GIT_AUTHOR_EMAIL: '[email protected]', GIT_COMMITTER_NAME: 'Dev', GIT_COMMITTER_EMAIL: '[email protected]',GIT_SSH_COMMAND: `ssh -i ${KEY} -p ${SSHPORT} -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -o LogLevel=ERROR` };const git = (cwd, args) => { const r = spawnSync('git', args, { cwd, encoding: 'utf8', env: gitEnv }); return { ok: r.status === 0, out: (r.stdout || '').trim(), err: (r.stderr || '').trim() }; };const must = (cwd, args) => { const r = git(cwd, args); if (!r.ok) throw new Error('git ' + args.join(' ') + ': ' + r.err); return r.out; };const bareRef = (ref) => git(join(GIT, 'pr-ssh.git'), ['rev-parse', '--verify', '-q', ref]).out;const changes = () => existsSync(LOG) ? readFileSync(LOG, 'utf8').split('\n').filter(Boolean).map(l => JSON.parse(l)) : [];let server = null;const cleanup = () => { try { server && server.kill('SIGTERM'); } catch {} spawnSync('docker', ['rm', '-f', CONTAINER]); };process.on('exit', cleanup);try {rmSync(STORE, { recursive: true, force: true }); mkdirSync(DB, { recursive: true }); mkdirSync(GIT, { recursive: true }); mkdirSync(WORK, { recursive: true });spawnSync('ssh-keygen', ['-q', '-t', 'ed25519', '-N', '', '-C', 'w40@gate', '-f', KEY]);const pub = readFileSync(KEY + '.pub', 'utf8').trim();const seed = spawnSync(BIN, ['tests/pulls-seed.hl', 'pr-ssh', pub], { cwd: APP, env, encoding: 'utf8' });const USER = (/USER (\S+)/.exec(seed.stdout) || [])[1];check('seed: user, repo, ssh key', !!USER, seed.stdout + seed.stderr);server = spawn(BIN, ['project.hl'], { cwd: APP, env, stdio: ['ignore', 'pipe', 'pipe'] });let slog = ''; server.stdout.on('data', d => slog += d); server.stderr.on('data', d => slog += d);for (let i = 0; i < 960; i++) { try { if ((await raw('/api/repos')).status === 200) break; } catch {} await sleep(250); if (i === 959) throw new Error('server did not come up\n' + slog); }spawnSync('docker', ['rm', '-f', CONTAINER]);const built = spawnSync('docker', ['build', '-q', '-t', 'gitoria-sshd-w40gate', join(APP, 'docker/sshd')], { encoding: 'utf8' });check('sshd image builds', built.status === 0, built.stderr);const run = spawnSync('docker', ['run', '-d', '--rm', '--name', CONTAINER, '--network', 'host', '-e', 'GITORIA_SSH_SECRET=' + SECRET, '-e', 'GITORIA_SSH_PORT=' + SSHPORT, '-e', 'GITORIA_URL=http://127.0.0.1:' + PORT, '-v', GIT + ':/repos', 'gitoria-sshd-w40gate'], { encoding: 'utf8' });check('sshd container starts', run.status === 0, run.stderr);for (let i = 0; i < 60; i++) { const r = spawnSync('sh', ['-c', `ssh-keyscan -p ${SSHPORT} 127.0.0.1 2>/dev/null | grep -c ssh-`], { encoding: 'utf8' }); if (Number(r.stdout) > 0) break; await sleep(250); }check('/__git/pushed with a wrong secret is refused (403)', (await raw(`/__git/pushed?user=${USER}&slug=pr-ssh&phase=after`, { 'x-gitoria-secret': 'nope' })).status === 403);check('/__git/pushed without a secret is refused (403)', (await raw(`/__git/pushed?user=${USER}&slug=pr-ssh&phase=after`)).status === 403);check('/__git/pushed from another user is refused (403)', (await raw(`/__git/pushed?user=nobody&slug=pr-ssh&phase=after`, { 'x-gitoria-secret': SECRET })).status === 403);const W = join(WORK, 'w'); mkdirSync(W); must(W, ['init', '-q', '-b', 'main']);const U = `ssh://[email protected]:${SSHPORT}/pr-ssh.git`;writeFileSync(join(W, 'a.txt'), 'a\n'); must(W, ['add', '.']); must(W, ['commit', '-q', '-m', 'base']);let r = git(W, ['push', U, 'main']);check('ssh push of main works', r.ok, r.err);check('no PR → no change', changes().length === 0);must(W, ['checkout', '-q', '-b', 'f1']); writeFileSync(join(W, 'f1.txt'), '1\n'); must(W, ['add', '.']); must(W, ['commit', '-q', '-m', '|||PR over ssh']);const a = must(W, ['rev-parse', 'HEAD']);r = git(W, ['push', U, 'f1']);check('ssh push of a |||PR branch works (git status kept)', r.ok, r.err);const ch = changes();check('log: opened #1 with the pusher\'s user id', ch.length === 1 && ch[0].action === 'opened' && ch[0].number === 1 && ch[0].after === a && ch[0].sender && ch[0].sender.id === USER, JSON.stringify(ch));check('refs/pull/1/head = the head', bareRef('refs/pull/1/head') === a);must(W, ['commit', '-q', '--allow-empty', '-m', 'more']);const b = must(W, ['rev-parse', 'HEAD']);git(W, ['push', U, 'f1']);const c2 = changes();check('second ssh push → synchronize, not forced', c2.length === 2 && c2[1].action === 'synchronize' && c2[1].before === a && c2[1].after === b && c2[1].forced === false && c2[1].sender.id === USER, JSON.stringify(c2));for (const spec of ['main:refs/pull/1/head', 'main:refs/pull/7/head', ':refs/pull/1/head', `main:refs/pull/1/heads/${'b'.repeat(40)}`]) {r = git(W, ['push', U, spec]);check(`ssh push of ${spec.slice(0, 32)} is refused`, !r.ok && /hidden ref|rejected|deny/i.test(r.err), r.err);}check('refs/pull/1/head unchanged', bareRef('refs/pull/1/head') === b && bareRef('refs/pull/7/head') === '');check('refused pushes made no change', changes().length === 2);// old heads survive gcmust(W, ['reset', '-q', '--hard', 'HEAD~1']); must(W, ['commit', '-q', '--allow-empty', '-m', 'rewritten']);git(W, ['push', '--force', U, 'f1']);const gc = git(join(GIT, 'pr-ssh.git'), ['gc', '-q', '--prune=now']);check('gc ran', gc.ok, gc.err);check('old head (replaced by a force-push) still resolves after gc --prune=now', git(join(GIT, 'pr-ssh.git'), ['cat-file', '-e', b + '^{commit}']).ok);} catch (e) {check('gate ran to the end', false, String(e && e.stack || e));} finally { cleanup(); }console.log(`\n${passes} passed, ${failures} failed`);process.exit(failures ? 1 : 0);
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- 01b71c2edocs: neutral wording in missions, reports and logmre
- 3eeea733log: 2026-11-09 PR flow plan and membersmre
- e7c2ff1fdocs: neutral name for the apps-api reviewmre
- 120b7abdlog: step 3 queuedmre
- 358474bflog: P50 livemre
- 477d7bdcMerge branch 'p50'mre
- 00efc06fP50 reportmre
- e3f85afblog: commit mention slipmre
- 4d1a4cf4log: #25 numbersmre
- 1af0a60ddocs: H25 run() correctionmre
- eefa2dbcRepo setting: refuse direct pushes to the main branch (pre-receive over HTTPS and ssh)mre
- 8c8ba2f1log: PR flow announcedmre
- fa00cd35log: Kunibert pings provenmre
- 3a68cfaclog: components decisionmre
- 87de8e31log: Kunibert connected to 9 app reposmre
- c22aff69log: PR-only rollout startedmre
- 25309981log: STOP and paused workers 2026-11-08mre
- 5286803alog: worker briefs and reports of 2026-11-08mre
- 85c86605log: 2026-11-08 Arachne on list shapemre
- cb634512log: 2026-11-08 keyed lists livemre