Adds prettier 3.9.8 (exact) with a shared config (single quotes, 120 columns, trailing commas), .prettierignore, .editorconfig and the `format` / `format:check` scripts, then formats all TypeScript, JavaScript, CSS, JSON and YAML sources. Purely mechanical: no behaviour change. Typecheck passes, the vitest suite and the 74 IDE node tests produce the same results as before (socket-binding tests still cannot run inside this sandbox). Previously the longest source line was ~3000 characters; reviewing the following semantic changes now produces readable diffs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
135 lines
6.0 KiB
TypeScript
135 lines
6.0 KiB
TypeScript
import { it, expect, vi } from 'vitest';
|
|
import { mkdtemp, rm, writeFile, symlink, mkdir } from 'node:fs/promises';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { GitWorkspace } from '@wishforge/runtime';
|
|
import { createGitService, issueGitToken, verifyGitToken } from '../src/service.js';
|
|
|
|
it('refuses the development HTTP override in production before creating storage', async () => {
|
|
vi.stubEnv('NODE_ENV', 'production');
|
|
try {
|
|
await expect(
|
|
createGitService({
|
|
root: '/unused-production-fixture',
|
|
publicBaseUrl: 'http://127.0.0.1:4504',
|
|
adminToken: 'admin-'.repeat(8),
|
|
tokenSecret: 'signing-'.repeat(8),
|
|
allowLocalHttp: true,
|
|
}),
|
|
).rejects.toMatchObject({ code: 'INSECURE_ENDPOINT' });
|
|
} finally {
|
|
vi.unstubAllEnvs();
|
|
}
|
|
});
|
|
|
|
it('enforces repository, permission and expiry scope on short-lived Git tokens', () => {
|
|
const secret = 's'.repeat(32);
|
|
const token = issueGitToken(
|
|
{ aud: 'wishforge-git', tenantId: 'tenant', projectId: 'project', permission: 'read', iat: 100, exp: 400 },
|
|
secret,
|
|
);
|
|
expect(
|
|
verifyGitToken(token, secret, { tenantId: 'tenant', projectId: 'project', write: false }, 200).permission,
|
|
).toBe('read');
|
|
expect(() => verifyGitToken(token, secret, { tenantId: 'other', projectId: 'project', write: false }, 200)).toThrow();
|
|
expect(() => verifyGitToken(token, secret, { tenantId: 'tenant', projectId: 'project', write: true }, 200)).toThrow();
|
|
expect(() =>
|
|
verifyGitToken(token, secret, { tenantId: 'tenant', projectId: 'project', write: false }, 500),
|
|
).toThrow();
|
|
});
|
|
|
|
it('provisions a real bare repository and serves authenticated clone/CAS push over smart HTTP', async () => {
|
|
const root = await mkdtemp(path.join(os.tmpdir(), 'wishforge-git-service-'));
|
|
const admin = 'admin-'.repeat(8);
|
|
const server = await createGitService({
|
|
root: path.join(root, 'repos'),
|
|
publicBaseUrl: 'http://127.0.0.1:4504',
|
|
adminToken: admin,
|
|
tokenSecret: 'signing-'.repeat(8),
|
|
allowLocalHttp: true,
|
|
});
|
|
await new Promise<void>((resolve, reject) => {
|
|
server.once('error', reject);
|
|
server.listen(0, '127.0.0.1', resolve);
|
|
});
|
|
const address = server.address();
|
|
if (!address || typeof address === 'string') throw new Error('no port');
|
|
const base = `http://127.0.0.1:${address.port}`;
|
|
const post = async (route: string, body: unknown) => {
|
|
const response = await fetch(`${base}${route}`, {
|
|
method: 'POST',
|
|
headers: { authorization: `Bearer ${admin}`, 'content-type': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
});
|
|
expect(response.status).toBe(200);
|
|
return response.json() as Promise<Record<string, string>>;
|
|
};
|
|
try {
|
|
const repo = await post('/repos', { tenantId: 'tenant', projectId: 'project', defaultBranch: 'release/v1' });
|
|
expect(repo.initialCommit).toMatch(/^[a-f0-9]{40}$/);
|
|
expect(repo.defaultBranch).toBe('release/v1');
|
|
const repeated = await post('/repos', { tenantId: 'tenant', projectId: 'project', defaultBranch: 'main' });
|
|
expect(repeated.defaultBranch).toBe('release/v1');
|
|
expect(repeated.initialCommit).toBe(repo.initialCommit);
|
|
expect((await post('/repos', { tenantId: 'tenant', projectId: 'default-main' })).defaultBranch).toBe('main');
|
|
const repositoryUrl = `${base}/git/tenant/project.git`;
|
|
const reader = await post('/tokens', { tenantId: 'tenant', projectId: 'project', permission: 'read' });
|
|
const writer = await post('/tokens', { tenantId: 'tenant', projectId: 'project', permission: 'write' });
|
|
const git = new GitWorkspace(undefined, {
|
|
allowLocalHttp: true,
|
|
credential: { token: reader.token!, repositoryUrl },
|
|
});
|
|
const checkout = await git.create({
|
|
root: path.join(root, 'clones'),
|
|
runId: 'run',
|
|
repository: repositoryUrl,
|
|
targetBranch: 'release/v1',
|
|
});
|
|
await writeFile(path.join(checkout.workspace, 'README.md'), 'A persistent project\n');
|
|
const checkpoint = await git.checkpoint(checkout.workspace, checkout.baseCommit, 'run', 'test');
|
|
const push = {
|
|
repository: checkout.workspace,
|
|
targetRef: 'refs/heads/release/v1',
|
|
expectedTargetCommit: checkout.baseCommit,
|
|
candidateCommit: checkpoint.sourceCommit,
|
|
verifiedCommit: checkpoint.sourceCommit,
|
|
};
|
|
await expect(git.pushVerifiedCandidate(push)).rejects.toThrow();
|
|
git.setCredential({ token: writer.token!, repositoryUrl });
|
|
await git.pushVerifiedCandidate(push);
|
|
const second = await git.create({
|
|
root: path.join(root, 'clones'),
|
|
runId: 'second',
|
|
repository: repositoryUrl,
|
|
targetBranch: 'release/v1',
|
|
});
|
|
expect(second.baseCommit).toBe(checkpoint.sourceCommit);
|
|
const afterPush = await post('/repos', { tenantId: 'tenant', projectId: 'project', defaultBranch: 'other' });
|
|
expect(afterPush.defaultBranch).toBe('release/v1');
|
|
expect(afterPush.initialCommit).toBe(checkpoint.sourceCommit);
|
|
const unsafe = await fetch(`${base}/repos`, {
|
|
method: 'POST',
|
|
headers: { authorization: `Bearer ${admin}`, 'content-type': 'application/json' },
|
|
body: JSON.stringify({ tenantId: 'tenant', projectId: 'invalid-branch', defaultBranch: 'main:other' }),
|
|
});
|
|
expect(unsafe.status).toBe(400);
|
|
const cross = await fetch(`${base}/git/other/project.git/info/refs?service=git-upload-pack`, {
|
|
headers: { authorization: `Bearer ${reader.token!}` },
|
|
});
|
|
expect(cross.status).toBe(401);
|
|
await mkdir(path.join(root, 'outside.git'));
|
|
await symlink(path.join(root, 'outside.git'), path.join(root, 'repos', 'tenant', 'escape.git'));
|
|
const escaped = await fetch(`${base}/repos`, {
|
|
method: 'POST',
|
|
headers: { authorization: `Bearer ${admin}`, 'content-type': 'application/json' },
|
|
body: JSON.stringify({ tenantId: 'tenant', projectId: 'escape' }),
|
|
});
|
|
expect(escaped.status).toBe(400);
|
|
expect((await escaped.json()).error).toBe('REPOSITORY_SCOPE');
|
|
} finally {
|
|
server.closeAllConnections();
|
|
await new Promise<void>(resolve => server.close(() => resolve()));
|
|
await rm(root, { recursive: true, force: true });
|
|
}
|
|
}, 15000);
|