feat(git): Git-Backend über HTTPS + PAT (ADR 0003): Krypto, SSRF-Policy, git-Client ohne Arbeitskopie, Sync/Konflikte, /api/git/*, Hooks und Tests
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d2801b2d6c
commit
fe9efc155e
18 files changed
+1837
-9
No files matched your search
@@ -0,0 +1,106 @@
|
||||
// Lokales Git-Remote für Tests (ADR 0003, "Tests"): `git http-backend` hinter einem kleinen Node-HTTP-Server mit
|
||||
// Basic-Auth-Prüfung. Repos liegen als Bare-Repos mit uploadpack.allowFilter=true und http.receivepack=true.
|
||||
import http from 'http';
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import { execFileSync, spawn } from 'child_process';
|
||||
|
||||
const git = (args, opts = {})=>execFileSync('git', args, { encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'], ...opts }).trim();
|
||||
|
||||
export const startGitHttpServer = async ({ username = 'tester', token = 'secret-token-123' } = {})=>{
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'hb-git-remote-'));
|
||||
let hang = false;
|
||||
const requests = [];
|
||||
|
||||
const server = http.createServer((req, res)=>{
|
||||
requests.push(`${req.method} ${req.url}`);
|
||||
if(hang) return; // antwortet nie
|
||||
const auth = req.headers.authorization;
|
||||
const expected = `Basic ${Buffer.from(`${username}:${token}`).toString('base64')}`;
|
||||
if(auth !== expected) {
|
||||
res.writeHead(401, { 'WWW-Authenticate': 'Basic realm="git"' });
|
||||
return res.end('unauthorized');
|
||||
}
|
||||
const url = new URL(req.url, 'http://x');
|
||||
const chunks = [];
|
||||
req.on('data', (c)=>chunks.push(c));
|
||||
req.on('end', ()=>{
|
||||
const body = Buffer.concat(chunks);
|
||||
const cgi = spawn('git', ['http-backend'], {
|
||||
env : {
|
||||
PATH : process.env.PATH,
|
||||
GIT_PROJECT_ROOT : root,
|
||||
GIT_HTTP_EXPORT_ALL : '1',
|
||||
REQUEST_METHOD : req.method,
|
||||
PATH_INFO : url.pathname,
|
||||
QUERY_STRING : url.search.slice(1),
|
||||
CONTENT_TYPE : req.headers['content-type'] || '',
|
||||
CONTENT_LENGTH : String(body.length),
|
||||
REMOTE_USER : username,
|
||||
REMOTE_ADDR : '127.0.0.1',
|
||||
HTTP_CONTENT_ENCODING : req.headers['content-encoding'] || '',
|
||||
HTTP_GIT_PROTOCOL : req.headers['git-protocol'] || ''
|
||||
}
|
||||
});
|
||||
cgi.stdin.on('error', ()=>{});
|
||||
cgi.stdin.end(body);
|
||||
const out = [];
|
||||
cgi.stdout.on('data', (c)=>out.push(c));
|
||||
cgi.on('close', ()=>{
|
||||
const buf = Buffer.concat(out);
|
||||
const sep = buf.indexOf('\r\n\r\n');
|
||||
if(sep < 0) { res.writeHead(500); return res.end(); }
|
||||
const headers = {};
|
||||
let status = 200;
|
||||
for (const line of buf.subarray(0, sep).toString().split('\r\n')) {
|
||||
const i = line.indexOf(':');
|
||||
if(i < 0) continue;
|
||||
const k = line.slice(0, i).trim();
|
||||
const v = line.slice(i + 1).trim();
|
||||
if(k.toLowerCase() === 'status') status = parseInt(v, 10); else headers[k] = v;
|
||||
}
|
||||
res.writeHead(status, headers);
|
||||
res.end(buf.subarray(sep + 4));
|
||||
});
|
||||
});
|
||||
});
|
||||
await new Promise((resolve)=>server.listen(0, '127.0.0.1', resolve));
|
||||
const base = `http://127.0.0.1:${server.address().port}`;
|
||||
|
||||
return {
|
||||
base, username, token, requests, root,
|
||||
dirOf : (name)=>path.join(root, name),
|
||||
setHang : (v)=>{ hang = v; },
|
||||
// Legt ein leeres Bare-Repo an und liefert URL + Pfad
|
||||
createRepo : (name = 'r.git')=>{
|
||||
const dir = path.join(root, name);
|
||||
git(['init', '--bare', '-q', '-b', 'main', dir]);
|
||||
git(['-C', dir, 'config', 'uploadpack.allowFilter', 'true']);
|
||||
git(['-C', dir, 'config', 'uploadpack.allowAnySHA1InWant', 'true']);
|
||||
git(['-C', dir, 'config', 'http.receivepack', 'true']);
|
||||
return { url: `${base}/${name}`, dir };
|
||||
},
|
||||
// Commit direkt im Bare-Repo (simuliert Push eines Dritten). Liefert die Commit-ID.
|
||||
commitFile : (dir, branch, filePath, content, message = 'extern')=>{
|
||||
const env = { ...process.env, GIT_AUTHOR_NAME: 'Dritter', GIT_AUTHOR_EMAIL: 'd@example.test', GIT_COMMITTER_NAME: 'Dritter', GIT_COMMITTER_EMAIL: 'd@example.test', GIT_INDEX_FILE: path.join(dir, 'tmp-idx') };
|
||||
let parent;
|
||||
try { parent = git(['-C', dir, 'rev-parse', '--verify', `refs/heads/${branch}`]); } catch {}
|
||||
if(parent) git(['-C', dir, 'read-tree', parent], { env });
|
||||
const blob = git(['-C', dir, 'hash-object', '-w', '--stdin'], { input: content, env });
|
||||
git(['-C', dir, 'update-index', '--add', '--cacheinfo', `100644,${blob},${filePath}`], { env });
|
||||
const tree = git(['-C', dir, 'write-tree'], { env });
|
||||
const commit = git(['-C', dir, 'commit-tree', tree, ...(parent ? ['-p', parent] : []), '-m', message], { env });
|
||||
git(['-C', dir, 'update-ref', `refs/heads/${branch}`, commit]);
|
||||
fs.rmSync(env.GIT_INDEX_FILE, { force: true });
|
||||
return commit;
|
||||
},
|
||||
readFile : (dir, branch, filePath)=>git(['-C', dir, 'show', `refs/heads/${branch}:${filePath}`]),
|
||||
log : (dir, branch)=>git(['-C', dir, 'log', '--format=%an|%cn|%s', `refs/heads/${branch}`]).split('\n'),
|
||||
head : (dir, branch)=>git(['-C', dir, 'rev-parse', `refs/heads/${branch}`]),
|
||||
close : async ()=>{
|
||||
await new Promise((resolve)=>{ server.closeAllConnections?.(); server.close(resolve); });
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user