✨ Read project code and commit generated pages through the git service

The Copilot needs to look at code, and the Wiki needs to write the pages it
generates. Both go through the git service, which owns the bare repository: the
code view reads the object store (no checkout, no working tree), and a generated
page is committed with the platform as its author.

Paths that hold credentials or operator data are refused in the service, not in
the caller: .env*, *.pem/*.key, kubeconfig, secrets/, local-data/ and .vault-*.
This commit is contained in:
wishforge 2026-11-09 10:09:39 -08:00
parent 062a86aef9
commit b8c3c8e5e9
2 changed files with 317 additions and 5 deletions

View File

@ -0,0 +1,199 @@
import { RuntimeError } from '@wishforge/runtime';
/**
* Read-only views of a project's repository, for the Copilot's code tools. Everything here is a
* `git` plumbing call against the bare repository: no working tree is created, nothing is written,
* and no path outside the repository can be named.
*
* A customer routinely asks about a failing page or a report they cannot export. Answering that
* needs a look at the code, but not at the deployment: credentials, environment files and local
* operator data are unreadable here, whatever the model asks for.
*/
const DENIED =
/(?:^|\/)(?:\.env(?:\.[^/]*)?|secrets?|local-data|\.vault-[^/]*|\.git|\.ssh)(?:\/|$)|\.(?:pem|key|p12|pfx)$/i;
const DENIED_NAME = /(?:kubeconfig|id_rsa|id_ed25519|credentials)/i;
/** A path the code tools may not show. Checked on the way in and again on every listed entry. */
export function deniedCodePath(value: string) {
const normalized = value.replace(/\\/g, '/');
return DENIED.test(normalized) || DENIED_NAME.test(normalized.split('/').at(-1) ?? '');
}
export function codePath(value: unknown) {
const raw = typeof value === 'string' ? value.trim() : '';
const normalized = raw.replace(/^\/+/, '').replace(/\\/g, '/');
if (
!normalized ||
normalized.length > 512 ||
normalized.startsWith('-') ||
normalized.startsWith('~') ||
normalized.split('/').some(part => part === '..' || part === '.') ||
deniedCodePath(normalized)
)
throw new RuntimeError('INVALID_PATH', 'Use a repository path that is not a credential or local operator file');
return normalized;
}
/** A branch, a tag or a commit: anything the repository itself can resolve to a tree. */
export function codeRef(value: unknown) {
const ref = typeof value === 'string' ? value.trim() : '';
if (
!ref ||
ref.length > 200 ||
ref.startsWith('-') ||
ref.includes('..') ||
ref.includes('\\') ||
!/^[A-Za-z0-9._/-]+$/.test(ref)
)
throw new RuntimeError('INVALID_REF', 'Use a branch, a tag or a commit id');
return ref;
}
const sha = (value: unknown) => {
const id = typeof value === 'string' ? value.trim().toLowerCase() : '';
if (!/^[0-9a-f]{7,64}$/.test(id)) throw new RuntimeError('INVALID_REF', 'Use a hexadecimal commit id');
return id;
};
/** A regular file, from `git ls-tree -l`: symlinks and submodules are never read. */
function regularBlob(meta: string) {
const [mode = '', type = ''] = meta.split(' ');
return mode !== '120000' && type === 'blob';
}
export const CODE_LIMITS = {
tree: 500,
blobLines: 2000,
searchMatches: 50,
logEntries: 20,
showBytes: 200 * 1024,
blobBytes: 512 * 1024,
} as const;
export interface CodeReaderConfig {
/** Runs one `git` command in the repository; supplied by the git service so the hooks stay off. */
run: (args: string[], timeoutMs?: number) => Promise<string>;
}
export interface CodeEntry {
path: string;
type: 'file' | 'dir';
size?: number;
}
export function codeReader({ run }: CodeReaderConfig) {
return {
/** The files under a directory, at most `depth` levels below it. */
async tree(input: { ref: string; path?: string; depth?: number }) {
const ref = codeRef(input.ref);
const base = input.path === undefined || input.path === '' ? '' : codePath(input.path);
const depth = Math.min(Math.max(Number.isInteger(input.depth) ? input.depth! : 2, 1), 4);
const output = await run(
['ls-tree', '-r', '-l', '-z', '--full-tree', ref, ...(base ? ['--', base] : [])],
10_000,
);
const entries: CodeEntry[] = [];
for (const record of output.split('\u0000').filter(Boolean)) {
const tab = record.indexOf('\t');
if (tab < 0) continue;
const meta = record.slice(0, tab);
const file = record.slice(tab + 1);
if (!regularBlob(meta) || deniedCodePath(file)) continue;
if (base && !(file === base || file.startsWith(`${base}/`))) continue;
const relative = base ? file.slice(base.length).replace(/^\//, '') : file;
if (relative.split('/').length - 1 > depth) continue;
const size = Number(meta.split(/\s+/)[3]);
entries.push({ path: file, type: 'file', ...(Number.isFinite(size) ? { size } : {}) });
}
return {
entries: entries.slice(0, CODE_LIMITS.tree),
truncated: entries.length > CODE_LIMITS.tree,
ref,
path: base,
};
},
/** One file's text, optionally a 1-based line window. */
async blob(input: { ref: string; path: string; from?: number; to?: number }) {
const ref = codeRef(input.ref);
const file = codePath(input.path);
const size = Number((await run(['cat-file', '-s', `${ref}:${file}`], 5_000)).trim());
if (!Number.isFinite(size)) throw new RuntimeError('NOT_FOUND', 'File not found at this revision');
if (size > CODE_LIMITS.blobBytes) throw new RuntimeError('PAYLOAD_TOO_LARGE', 'File is too large to read');
const content = await run(['cat-file', '-p', `${ref}:${file}`], 5_000);
if (content.includes('\u0000')) return { ref, path: file, binary: true, content: '', totalLines: 0 };
const lines = content.split('\n');
const from = Math.max(1, Number.isInteger(input.from) ? input.from! : 1);
const to = Math.min(lines.length, Number.isInteger(input.to) ? input.to! : from + CODE_LIMITS.blobLines - 1);
return {
ref,
path: file,
binary: false,
from,
to: Math.max(from, to),
totalLines: lines.length,
content: lines.slice(from - 1, Math.max(from, to)).join('\n'),
};
},
/** A fixed-string search through the tree; `git grep` reads the object store, never a checkout. */
async search(input: { ref: string; pattern: string; glob?: string }) {
const ref = codeRef(input.ref);
const pattern = typeof input.pattern === 'string' ? input.pattern.trim() : '';
if (!pattern || pattern.length > 200 || pattern.startsWith('-'))
throw new RuntimeError('INVALID_PATTERN', 'Use a search text of at most 200 characters');
const glob = input.glob === undefined || input.glob === '' ? undefined : codePath(input.glob);
const output = await run(
['grep', '-F', '-n', '-I', '-e', pattern, ref, ...(glob ? ['--', glob] : [])],
10_000,
).catch(error => {
// `git grep` exits 1 when nothing matches; that is an empty result, not a failure.
if (String(error instanceof Error ? error.message : error).includes('exited 1')) return '';
throw error;
});
const matches = output
.split('\n')
.filter(Boolean)
.map(line => {
const [at, ...rest] = line.split(':');
const path = (at ?? '').replace(new RegExp(`^${ref.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}:`), '');
const number = Number(rest.shift() ?? 0);
return { path, line: Number.isFinite(number) ? number : 0, text: rest.join(':').slice(0, 400) };
})
.filter(match => !deniedCodePath(match.path));
return {
ref,
matches: matches.slice(0, CODE_LIMITS.searchMatches),
truncated: matches.length > CODE_LIMITS.searchMatches,
};
},
/** The most recent commits, newest first. */
async log(input: { ref: string; since?: string; limit?: number }) {
const ref = codeRef(input.ref);
const limit = Math.min(Math.max(Number.isInteger(input.limit) ? input.limit! : CODE_LIMITS.logEntries, 1), 50);
const since = input.since === undefined || input.since === '' ? undefined : String(input.since);
if (since && !/^\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2})?(?:Z|[+-]\d{2}:\d{2})?)?$/.test(since))
throw new RuntimeError('INVALID_DATE', 'Use a date like 2026-01-31 or 2026-01-31T09:00:00Z');
const output = await run(
['log', `--format=%H%x09%an%x09%aI%x09%s`, `-n${limit}`, ...(since ? [`--since=${since}`] : []), ref],
10_000,
).catch(error => {
if (String(error instanceof Error ? error.message : error).includes('exited 128')) return '';
throw error;
});
return {
ref,
commits: output
.split('\n')
.filter(Boolean)
.map(line => {
const [id, author, date, ...subject] = line.split('\t');
return { sha: id ?? '', author: author ?? '', date: date ?? '', subject: subject.join('\t').slice(0, 300) };
}),
};
},
/** One commit, or one file inside it. */
async show(input: { sha: string; path?: string }) {
const id = sha(input.sha);
const file = input.path === undefined || input.path === '' ? undefined : codePath(input.path);
const target = file ? `${id}:${file}` : id;
const size = Number((await run(['cat-file', '-s', target], 8_000)).trim());
if (!Number.isFinite(size)) throw new RuntimeError('NOT_FOUND', 'Commit or file not found');
if (size > CODE_LIMITS.showBytes)
throw new RuntimeError('PAYLOAD_TOO_LARGE', 'This change is too large to show in full');
const content = await run(['cat-file', '-p', target], 8_000);
return { sha: id, path: file ?? null, truncated: size >= CODE_LIMITS.showBytes, content };
},
};
}
export type CodeReader = ReturnType<typeof codeReader>;

View File

@ -1,10 +1,11 @@
import { plainHttpAllowed } from '@wishforge/contracts';
import { createHmac, timingSafeEqual } from 'node:crypto';
import { createHmac, randomUUID, timingSafeEqual } from 'node:crypto';
import { spawn } from 'node:child_process';
import { mkdir, open, realpath, rename, stat, unlink, writeFile } from 'node:fs/promises';
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
import path from 'node:path';
import { checkedCommand, RuntimeError, safeIdentifier, requireBranchName } from '@wishforge/runtime';
import { codeReader } from './code.js';
export interface GitServiceConfig {
root: string;
@ -179,8 +180,14 @@ export async function createGitService(config: GitServiceConfig) {
await mkdir(config.root, { recursive: true, mode: 0o700 });
const root = await realpath(config.root);
const hooks = await installHooks(root);
const git = (cwd: string, args: string[], input?: string) =>
checkedCommand({ executable: 'git', args: ['-c', 'core.hooksPath=/dev/null', ...args], cwd, input, env: gitEnv() });
const git = (cwd: string, args: string[], input?: string, extraEnv?: Record<string, string | undefined>) =>
checkedCommand({
executable: 'git',
args: ['-c', 'core.hooksPath=/dev/null', ...args],
cwd,
input,
env: { ...gitEnv(), ...extraEnv },
});
const location = async (tenantId: string, projectId: string) => {
safeIdentifier(tenantId);
safeIdentifier(projectId);
@ -203,7 +210,21 @@ export async function createGitService(config: GitServiceConfig) {
json(response, 200, { status: 'ok', service: 'git-storage' });
return;
}
if (['/repos', '/tokens', '/refs', '/wiki'].includes(url.pathname) && request.method === 'POST') {
if (
[
'/repos',
'/tokens',
'/refs',
'/wiki',
'/wiki/write',
'/code/tree',
'/code/blob',
'/code/search',
'/code/log',
'/code/show',
].includes(url.pathname) &&
request.method === 'POST'
) {
const auth = request.headers.authorization ?? '';
if (!auth.startsWith('Bearer ') || !sameSecret(auth.slice(7), config.adminToken)) {
json(response, 401, { error: 'UNAUTHORIZED' });
@ -288,6 +309,88 @@ export async function createGitService(config: GitServiceConfig) {
if (Buffer.byteLength(content) <= 128 * 1024) files.push({ path: file, content });
}
json(response, 200, { version, files });
} else if (url.pathname === '/wiki/write') {
// The Wiki is written by the platform, never by a customer: a page the Copilot generated
// is committed on the project's own branch, with the platform as its author.
await stat(repository);
const branch = requireBranchName(String(body.branch ?? 'main'));
const raw = Array.isArray(body.files) ? body.files : [];
if (!raw.length || raw.length > 10)
throw new RuntimeError('INVALID_WIKI_BATCH', 'Write 1 to 10 pages at once');
const pages = raw.map(item => {
const entry = item as { path?: unknown; content?: unknown };
const file = wikiFile(entry.path);
if (!/^wiki\/code\/[A-Za-z0-9._-]+\.md$/.test(file))
throw new RuntimeError('INVALID_WIKI_PATH', 'Generated pages live under wiki/code/');
const content = typeof entry.content === 'string' ? entry.content : '';
if (!content || Buffer.byteLength(content) > 256 * 1024)
throw new RuntimeError('PAYLOAD_TOO_LARGE', 'A generated page must be 1 byte to 256 KB');
return { path: file, content };
});
const parent = (await git(repository, ['rev-parse', '--verify', `${branch}^{commit}`])).trim();
const index = `${repository}.index-${randomUUID()}`;
const env = { ...gitEnv(), GIT_INDEX_FILE: index };
try {
await git(repository, ['read-tree', branch], undefined, env);
for (const page of pages) {
const blob = (await git(repository, ['hash-object', '-w', '--stdin'], page.content, env)).trim();
await git(
repository,
['update-index', '--add', '--cacheinfo', `100644,${blob},${page.path}`],
undefined,
env,
);
}
const tree = (await git(repository, ['write-tree'], undefined, env)).trim();
const message = String(body.message ?? 'Update the generated project pages').slice(0, 200);
const commit = (
await git(repository, ['commit-tree', tree, '-p', parent, '-m', message], undefined, env)
).trim();
// Only advance the branch if nobody else moved it: a concurrent push must not be lost.
await git(repository, ['update-ref', `refs/heads/${branch}`, commit, parent]);
json(response, 200, { commit, branch, files: pages.map(page => page.path) });
} finally {
await unlink(index).catch(() => undefined);
}
} else if (url.pathname.startsWith('/code/')) {
// Read-only code views for the Copilot. The repository is never checked out: every call
// reads the object store, and a credential or operator file is refused before it is read.
await stat(repository);
const reader = codeReader({
run: (args, timeoutMs = 10_000) =>
checkedCommand({
executable: 'git',
args: ['-c', 'core.hooksPath=/dev/null', ...args],
cwd: repository,
env: gitEnv(),
timeoutMs,
}),
});
const ref = typeof body.ref === 'string' && body.ref ? body.ref : 'main';
const text = (value: unknown) => (typeof value === 'string' ? value : undefined);
const number = (value: unknown) => (typeof value === 'number' && Number.isInteger(value) ? value : undefined);
if (url.pathname === '/code/tree')
json(response, 200, await reader.tree({ ref, path: text(body.path), depth: number(body.depth) }));
else if (url.pathname === '/code/blob')
json(
response,
200,
await reader.blob({
ref,
path: String(body.path ?? ''),
from: number(body.from),
to: number(body.to),
}),
);
else if (url.pathname === '/code/search')
json(
response,
200,
await reader.search({ ref, pattern: String(body.pattern ?? ''), glob: text(body.glob) }),
);
else if (url.pathname === '/code/log')
json(response, 200, await reader.log({ ref, since: text(body.since), limit: number(body.limit) }));
else json(response, 200, await reader.show({ sha: String(body.sha ?? ''), path: text(body.path) }));
} else if (url.pathname === '/refs') {
await stat(repository);
json(response, 200, { branches: await branchesIn(repository, String(body.prefix ?? '')) });
@ -435,7 +538,17 @@ export async function createGitService(config: GitServiceConfig) {
return;
}
const code = error instanceof RuntimeError ? error.code : 'GIT_SERVICE_ERROR';
json(response, code === 'UNAUTHORIZED' ? 401 : code === 'REPOSITORY_BUSY' ? 409 : 400, { error: code });
const status =
code === 'UNAUTHORIZED'
? 401
: code === 'NOT_FOUND'
? 404
: code === 'REPOSITORY_BUSY'
? 409
: code === 'PAYLOAD_TOO_LARGE'
? 413
: 400;
json(response, status, { error: code });
});
});
return server;