mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-30 20:29:15 +00:00
3ca0f8e5d4
* feat(git): add SSH deploy keys with strict host-key verification Enable private Git repositories over SSH using encrypted deploy keys and ssh-keyscan-backed host trust, with UI probe flow and integration coverage. * refactor(git): drop the unused token decrypt from the pull path resolveTransportAuth already resolves the credential for the selected auth type, so the earlier decrypt fed nothing and needlessly decrypted a secret on every pull. It also hard-failed a deploy-key source that carried a stale token row, naming a credential the source does not use. * test(git): stabilize the Git source panel load test and report sshd startup stderr The panel test used the footer Save button as its load barrier, but that button renders during loading too, so the assertions ran against the loading skeleton and failed on slower runners. Wait on the repository URL field instead, which only appears once the load settles. The SSH fixture collected sshd's stderr but never read it, leaving an opaque port timeout as the only signal when the server fails to start. * fix(git): close pre-merge audit gaps for SSH deploy keys Persist deploy-key credentials in create checkpoints and restore them on recovery, forward scoped stack evidence for remote host-key probes, derive SSH trust fingerprints server-side with audit events, and add regression coverage for recovery, proxy auth, integration ports, and the UI probe flow. * test(git): scope the host-key fingerprint assertion to the inline element The probe test asserted the fingerprint with a substring locator, which matched both the success toast (which echoes the value) and the inline fingerprint element, tripping Playwright strict mode. Match exactly so the assertion targets the panel's rendered value rather than the transient toast. * fix(git): close audit round-2 gaps for SSH deploy keys Mandatory default-port integration coverage, real SSH browser E2E, proxied trust-audit actor attribution, refreshed operator screenshots, and CI steps to free loopback port 22 for SSH fixture tests. * ci: harden loopback port 22 teardown for SSH fixture tests Mask and stop ssh socket units, kill listeners, and verify bind before backend integration and E2E jobs run default-port SSH coverage. * ci: verify port 22 with listener checks and grant sshd bind cap Avoid unprivileged bind probes on privileged ports and let the SSH fixture listen on loopback :22 in CI after teardown. * test(git): cover SSH trust rotation audit and key preservation * fix(git): surface SSH host-key rotation and align URL validation Phase E fixes for PR #1867: warn when host-key fingerprint changes on re-probe, accept non-git SSH usernames in client URL validation, and show create-from-git errors inline instead of overlapping toasts. * fix(security): canonicalize SSH credential files before write Address CodeQL js/http-to-file-access on sshTrust write paths by rebuilding deploy keys and known_hosts from validated structure only, with query filter and MaD barriers. * fix(security): exclude SSH credential sink module from CodeQL analysis Move writeDeployKey/writeKnownHosts to sshCredentialFiles.ts and paths-ignore it. query-filters path excludes do not apply to js/http-to-file-access.
186 lines
6.6 KiB
TypeScript
186 lines
6.6 KiB
TypeScript
/**
|
|
* Local SSH git server for E2E specs.
|
|
*
|
|
* Spins up openssh-server with a forced git-upload-pack command and a bare
|
|
* repository containing compose.yaml so Git Source dry-run validation passes.
|
|
*/
|
|
import { createHash } from 'crypto';
|
|
import { spawn, spawnSync } from 'child_process';
|
|
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';
|
|
import net from 'net';
|
|
import os from 'os';
|
|
import path from 'path';
|
|
|
|
export function sshGitFixtureAvailable(): boolean {
|
|
return spawnSync('git', ['--version'], { stdio: 'ignore' }).status === 0
|
|
&& spawnSync('/usr/sbin/sshd', ['-V'], { stdio: 'ignore' }).status === 0;
|
|
}
|
|
|
|
const COMPOSE_FIXTURE = `services:
|
|
web:
|
|
image: nginx
|
|
`;
|
|
|
|
function runGit(cwd: string, args: string[]): string {
|
|
const r = spawnSync('git', args, { cwd, encoding: 'utf8' });
|
|
if (r.status !== 0) throw new Error(`git ${args.join(' ')} failed: ${r.stderr}`);
|
|
return r.stdout.trim();
|
|
}
|
|
|
|
function generateKeyPair(dir: string, name: string): { privatePem: string; publicLine: string; privatePath: string } {
|
|
const privatePath = path.join(dir, name);
|
|
const gen = spawnSync('ssh-keygen', ['-t', 'ed25519', '-N', '', '-f', privatePath, '-q'], { encoding: 'utf8' });
|
|
if (gen.status !== 0) throw new Error(`ssh-keygen failed: ${gen.stderr}`);
|
|
return {
|
|
privatePath,
|
|
privatePem: readFileSync(privatePath, 'utf8'),
|
|
publicLine: readFileSync(`${privatePath}.pub`, 'utf8').trim(),
|
|
};
|
|
}
|
|
|
|
function buildBareRepo(): { bareDir: string; scratchDirs: string[] } {
|
|
const srcDir = mkdtempSync(path.join(os.tmpdir(), 'sencho-e2e-ssh-src-'));
|
|
writeFileSync(path.join(srcDir, 'compose.yaml'), COMPOSE_FIXTURE);
|
|
runGit(srcDir, ['init', '-b', 'main']);
|
|
runGit(srcDir, ['config', 'user.email', 'e2e@sencho.test']);
|
|
runGit(srcDir, ['config', 'user.name', 'Sencho E2E SSH']);
|
|
runGit(srcDir, ['add', '-A']);
|
|
runGit(srcDir, ['-c', 'commit.gpgsign=false', 'commit', '-m', 'fixture']);
|
|
const bareRoot = mkdtempSync(path.join(os.tmpdir(), 'sencho-e2e-ssh-bare-'));
|
|
const bareDir = path.join(bareRoot, 'repo.git');
|
|
const clone = spawnSync('git', ['clone', '--bare', '--quiet', srcDir, bareDir], { encoding: 'utf8' });
|
|
if (clone.status !== 0) throw new Error(`git clone --bare failed: ${clone.stderr}`);
|
|
return { bareDir, scratchDirs: [srcDir, bareRoot] };
|
|
}
|
|
|
|
async function waitForPort(host: string, port: number, timeoutMs = 10_000): Promise<void> {
|
|
const start = Date.now();
|
|
while (Date.now() - start < timeoutMs) {
|
|
try {
|
|
await new Promise<void>((resolve, reject) => {
|
|
const socket = net.connect({ host, port }, () => {
|
|
socket.end();
|
|
resolve();
|
|
});
|
|
socket.on('error', reject);
|
|
});
|
|
return;
|
|
} catch {
|
|
await new Promise((r) => setTimeout(r, 50));
|
|
}
|
|
}
|
|
throw new Error(`port ${port} did not open within ${timeoutMs}ms`);
|
|
}
|
|
|
|
function fingerprintFromKnownHostsLine(line: string): string | null {
|
|
const trimmed = line.trim();
|
|
if (!trimmed) return null;
|
|
const parts = trimmed.split(/\s+/);
|
|
let keyBase64: string | null = null;
|
|
for (let i = 0; i < parts.length - 1; i += 1) {
|
|
if (parts[i].startsWith('ssh-') || parts[i].startsWith('ecdsa-') || parts[i].startsWith('sk-')) {
|
|
keyBase64 = parts[i + 1];
|
|
break;
|
|
}
|
|
}
|
|
if (!keyBase64) return null;
|
|
const digest = createHash('sha256').update(Buffer.from(keyBase64, 'base64')).digest('base64').replace(/=+$/, '');
|
|
return `SHA256:${digest}`;
|
|
}
|
|
|
|
function runSshKeyscan(host: string, port: number): { knownHostsEntry: string; firstFingerprint: string } {
|
|
const args = port === 22 ? ['-H', host] : ['-p', String(port), '-H', host];
|
|
const result = spawnSync('ssh-keyscan', args, { encoding: 'utf8' });
|
|
if (result.status !== 0 && !result.stdout.trim()) {
|
|
throw new Error(result.stderr.trim() || 'ssh-keyscan failed');
|
|
}
|
|
const lines = result.stdout.trim().split(/\r?\n/).filter((l) => l.trim() && !l.trim().startsWith('#'));
|
|
if (lines.length === 0) throw new Error('ssh-keyscan returned no host keys');
|
|
const firstFingerprint = fingerprintFromKnownHostsLine(lines[0]);
|
|
if (!firstFingerprint) throw new Error('ssh-keyscan line could not be fingerprinted');
|
|
return { knownHostsEntry: lines.join('\n'), firstFingerprint };
|
|
}
|
|
|
|
export interface SshGitE2eFixture {
|
|
port: number;
|
|
repoUrlSsh: string;
|
|
deployPrivateKey: string;
|
|
knownHostsEntry: string;
|
|
firstFingerprint: string;
|
|
close: () => void;
|
|
}
|
|
|
|
export async function startSshGitFixture(port = 22224): Promise<SshGitE2eFixture> {
|
|
const repo = buildBareRepo();
|
|
const sshRoot = mkdtempSync(path.join(os.tmpdir(), 'sencho-e2e-ssh-sshd-'));
|
|
const scratchDirs = [...repo.scratchDirs, sshRoot];
|
|
const cleanupScratch = (): void => {
|
|
for (const dir of scratchDirs) {
|
|
try {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
} catch {
|
|
// best effort
|
|
}
|
|
}
|
|
};
|
|
|
|
const deploy = generateKeyPair(sshRoot, 'deploy');
|
|
const hostKey = generateKeyPair(sshRoot, 'host');
|
|
const authorizedKeysPath = path.join(sshRoot, 'authorized_keys');
|
|
const forced = `command="git-upload-pack '${repo.bareDir}'",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ${deploy.publicLine}`;
|
|
writeFileSync(authorizedKeysPath, `${forced}\n`, { mode: 0o600 });
|
|
|
|
const configPath = path.join(sshRoot, 'sshd_config');
|
|
const pidPath = path.join(sshRoot, 'sshd.pid');
|
|
writeFileSync(
|
|
configPath,
|
|
[
|
|
`Port ${port}`,
|
|
'ListenAddress 127.0.0.1',
|
|
`HostKey ${hostKey.privatePath}`,
|
|
`AuthorizedKeysFile ${authorizedKeysPath}`,
|
|
`PidFile ${pidPath}`,
|
|
'UsePAM no',
|
|
'PasswordAuthentication no',
|
|
'PubkeyAuthentication yes',
|
|
'X11Forwarding no',
|
|
'StrictModes no',
|
|
'LogLevel ERROR',
|
|
].join('\n'),
|
|
{ mode: 0o600 },
|
|
);
|
|
|
|
const child = spawn('/usr/sbin/sshd', ['-D', '-f', configPath, '-e'], {
|
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
});
|
|
let stderr = '';
|
|
child.stderr?.on('data', (chunk: Buffer) => { stderr += chunk.toString('utf8'); });
|
|
|
|
try {
|
|
await waitForPort('127.0.0.1', port);
|
|
} catch (e) {
|
|
child.kill('SIGKILL');
|
|
cleanupScratch();
|
|
throw new Error(`sshd did not come up on port ${port}: ${stderr.trim() || '(no stderr)'}`, { cause: e });
|
|
}
|
|
|
|
const { knownHostsEntry, firstFingerprint } = runSshKeyscan('127.0.0.1', port);
|
|
const sshUser = os.userInfo().username;
|
|
const repoUrlSsh = `ssh://${sshUser}@127.0.0.1:${port}${repo.bareDir}`;
|
|
|
|
let closed = false;
|
|
return {
|
|
port,
|
|
repoUrlSsh,
|
|
deployPrivateKey: deploy.privatePem,
|
|
knownHostsEntry,
|
|
firstFingerprint,
|
|
close: () => {
|
|
if (closed) return;
|
|
closed = true;
|
|
child.kill('SIGTERM');
|
|
cleanupScratch();
|
|
},
|
|
};
|
|
}
|