149 lines
5.2 KiB
TypeScript
149 lines
5.2 KiB
TypeScript
import { beforeAll, afterAll, expect, it } from 'vitest';
|
|
import { createHosted } from '../../src/hosted.js';
|
|
import { config } from '../../src/config.js';
|
|
import { hash, randomSecret } from '../../src/crypto.js';
|
|
import { readFixture } from '../fixture.js';
|
|
|
|
const f = readFixture();
|
|
let hosted: Awaited<ReturnType<typeof createHosted>>;
|
|
let clientId: string;
|
|
beforeAll(async () => {
|
|
hosted = await createHosted(config(f.env));
|
|
await hosted.store.account('security-test', 'security-test', {
|
|
access_token: f.readToken,
|
|
expires_at: Date.now() + 3600000,
|
|
});
|
|
const response = await fetch(f.origin + '/oauth/register', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
redirect_uris: ['http://127.0.0.1:19876/callback'],
|
|
token_endpoint_auth_method: 'none',
|
|
grant_types: ['authorization_code'],
|
|
response_types: ['code'],
|
|
}),
|
|
});
|
|
expect(response.status).toBe(201);
|
|
clientId = (await response.json()).client_id;
|
|
});
|
|
afterAll(async () => {
|
|
if (hosted) {
|
|
// This disposable fixture is shared sequentially with the browser suite.
|
|
// Give that suite its own registration budget after redirect-policy checks.
|
|
await hosted.store.sql`DELETE FROM rate_buckets WHERE key = ${hash('registration:127.0.0.1')}`;
|
|
await hosted.store.disconnect('security-test');
|
|
await hosted.close();
|
|
}
|
|
});
|
|
|
|
for (const scenario of [
|
|
'valid',
|
|
'wrong audience',
|
|
'wrong account',
|
|
'missing scope',
|
|
'expired token',
|
|
'revoked grant',
|
|
'expired grant',
|
|
]) {
|
|
it('enforces the HTTP credential boundary: ' + scenario, async () => {
|
|
const grantId = randomSecret();
|
|
await hosted.store.addGrant({
|
|
id: grantId,
|
|
accountId: 'security-test',
|
|
clientId,
|
|
clientName: 'Security tests',
|
|
repositories: [{ id: f.repoId, name: 'alice/project' }],
|
|
write: false,
|
|
});
|
|
const registered = await hosted.provider.Client.find(clientId);
|
|
const token = new hosted.provider.AccessToken({
|
|
client: registered!,
|
|
accountId: scenario === 'wrong account' ? 'other' : 'security-test',
|
|
grantId,
|
|
gty: 'authorization_code',
|
|
aud: scenario === 'wrong audience' ? 'https://other.example/mcp' : f.origin + '/mcp',
|
|
scope: scenario === 'missing scope' ? '' : 'gitza:access',
|
|
...(scenario === 'expired token' ? { exp: Math.floor(Date.now() / 1000) - 60 } : {}),
|
|
});
|
|
const bearer = await token.save();
|
|
if (scenario === 'revoked grant') await hosted.store.revoke(grantId);
|
|
if (scenario === 'expired grant')
|
|
await hosted.store
|
|
.sql`UPDATE mcp_grants SET created_at = now() - interval '31 days' WHERE id = ${grantId}`;
|
|
const response = await fetch(f.origin + '/mcp', {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: 'Bearer ' + bearer,
|
|
'Content-Type': 'application/json',
|
|
Accept: 'application/json, text/event-stream',
|
|
},
|
|
body: JSON.stringify({
|
|
jsonrpc: '2.0',
|
|
id: 1,
|
|
method: 'initialize',
|
|
params: {
|
|
protocolVersion: '2025-11-25',
|
|
capabilities: {},
|
|
clientInfo: { name: 'security', version: '1' },
|
|
},
|
|
}),
|
|
});
|
|
expect(response.status).toBe(scenario === 'valid' ? 200 : 401);
|
|
expect(await response.text()).not.toContain(bearer);
|
|
await hosted.store.revoke(grantId);
|
|
});
|
|
}
|
|
|
|
it('publishes OAuth discovery with PKCE and absolute endpoint URLs', async () => {
|
|
for (const path of [
|
|
'/.well-known/openid-configuration',
|
|
'/.well-known/oauth-authorization-server',
|
|
]) {
|
|
const response = await fetch(f.origin + path);
|
|
expect(response.status).toBe(200);
|
|
const metadata = await response.json();
|
|
expect(metadata.issuer).toBe(f.origin);
|
|
expect(metadata.authorization_endpoint).toBe(f.origin + '/oauth/authorize');
|
|
expect(metadata.code_challenge_methods_supported).toEqual(['S256']);
|
|
}
|
|
});
|
|
|
|
it('accepts native-client loopback callbacks while rejecting unsafe HTTP redirects', async () => {
|
|
for (const [redirect, status] of [
|
|
['http://localhost:19876/callback', 201],
|
|
['http://127.0.0.1:19876/callback', 201],
|
|
['http://[::1]:19876/callback', 201],
|
|
['https://client.example/callback', 201],
|
|
['http://localhost.example/callback', 400],
|
|
['http://example.com/callback', 400],
|
|
['http://user@localhost:19876/callback', 400],
|
|
['http://localhost:19876/callback#fragment', 400],
|
|
] as const) {
|
|
const response = await fetch(f.origin + '/oauth/register', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
redirect_uris: [redirect],
|
|
token_endpoint_auth_method: 'none',
|
|
grant_types: ['authorization_code'],
|
|
response_types: ['code'],
|
|
}),
|
|
});
|
|
expect(response.status, redirect).toBe(status);
|
|
}
|
|
});
|
|
|
|
it('rejects malformed and oversized JSON without echoing its contents', async () => {
|
|
for (const [body, status] of [
|
|
['{"secret-marker":', 400],
|
|
['x'.repeat(130 * 1024), 413],
|
|
] as const) {
|
|
const response = await fetch(f.origin + '/mcp', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body,
|
|
});
|
|
expect(response.status).toBe(status);
|
|
expect(await response.text()).not.toContain('secret-marker');
|
|
}
|
|
});
|