207 lines
7.2 KiB
TypeScript
207 lines
7.2 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { describe, expect, mock, test } from 'bun:test';
|
|
import { Hono } from 'hono';
|
|
import { createHttpSecurity } from '@stock-bot/di';
|
|
import type { IServiceContainer } from '@stock-bot/services';
|
|
import { createStrategyRoutes } from '../../apps/stock/web-api/src/routes/strategies.routes';
|
|
|
|
const tokens = {
|
|
operator: 'catalog-admin-fixture-not-a-real-secret-1234',
|
|
service: 'catalog-service-fixture-not-a-real-secret-1234',
|
|
read: 'catalog-read-fixture-not-a-real-secret-1234',
|
|
};
|
|
function fixture(security: 'enabled' | 'disabled' | 'absent' = 'enabled') {
|
|
const database = {
|
|
query: mock(async () => {
|
|
throw new Error('Rejected request reached storage');
|
|
}),
|
|
transaction: mock(async () => {
|
|
throw new Error('Rejected request reached transaction');
|
|
}),
|
|
};
|
|
const logger = { error: mock(() => {}) };
|
|
const app = new Hono();
|
|
if (security !== 'absent') {
|
|
createHttpSecurity({
|
|
adminToken: tokens.operator,
|
|
serviceToken: tokens.service,
|
|
readToken: tokens.read,
|
|
environment: 'test',
|
|
hostname: '127.0.0.1',
|
|
disabled: security === 'disabled',
|
|
}).mount(app);
|
|
}
|
|
app.route(
|
|
'/',
|
|
createStrategyRoutes({ postgres: database, logger } as unknown as IServiceContainer)
|
|
);
|
|
app.post('/api/v2/unrelated', c => c.json({ ok: true }));
|
|
return {
|
|
database,
|
|
app,
|
|
request(
|
|
path: string,
|
|
body: unknown,
|
|
role?: keyof typeof tokens,
|
|
extra: Record<string, string> = {},
|
|
method = 'POST'
|
|
) {
|
|
return app.request(`/api/v2/${path}`, {
|
|
method,
|
|
headers: {
|
|
...(role ? { Authorization: `Bearer ${tokens[role]}` } : {}),
|
|
'Content-Type': 'application/json',
|
|
...extra,
|
|
},
|
|
body: JSON.stringify(body),
|
|
});
|
|
},
|
|
};
|
|
}
|
|
const strategyId = randomUUID();
|
|
const versionId = randomUUID();
|
|
const writes = [
|
|
{ path: 'strategies', body: { name: 'Candidate' } },
|
|
{ path: `strategies/${strategyId}`, body: { archived: true }, method: 'PATCH' },
|
|
{ path: `strategies/${strategyId}/versions`, body: { package: {} } },
|
|
{ path: `strategy-versions/${versionId}/clone`, body: { name: 'Clone' } },
|
|
{
|
|
path: `strategy-versions/${versionId}/reviews`,
|
|
body: { decision: 'approve', reason: 'Review' },
|
|
},
|
|
{ path: `strategy-versions/${versionId}/validate`, body: { parameters: {} } },
|
|
];
|
|
|
|
describe('strategy catalog HTTP credential boundary', () => {
|
|
test('NUL and unpaired Unicode surrogates reject before storage instead of failing JSONB persistence', async () => {
|
|
const f = fixture();
|
|
for (const text of ['bad\u0000text', 'bad\ud800text', 'bad\udc00text']) {
|
|
const cases = [
|
|
{ path: 'strategies', body: { name: text } },
|
|
{ path: `strategies/${strategyId}`, body: { description: text }, method: 'PATCH' },
|
|
{
|
|
path: `strategies/${strategyId}/versions`,
|
|
body: { package: { artifact: { content: text } } },
|
|
},
|
|
{ path: `strategy-versions/${versionId}/clone`, body: { name: text } },
|
|
{
|
|
path: `strategy-versions/${versionId}/reviews`,
|
|
body: { decision: 'approve', reason: text },
|
|
},
|
|
];
|
|
for (const item of cases) {
|
|
expect(
|
|
(
|
|
await f.request(
|
|
item.path,
|
|
item.body,
|
|
'operator',
|
|
{ 'Idempotency-Key': 'unicode' },
|
|
item.method
|
|
)
|
|
).status
|
|
).toBe(400);
|
|
}
|
|
}
|
|
expect(f.database.query.mock.calls).toHaveLength(0);
|
|
expect(f.database.transaction.mock.calls).toHaveLength(0);
|
|
});
|
|
test('catalog middleware does not alter unrelated routes on a shared listener', async () => {
|
|
const f = fixture('disabled');
|
|
const result = await f.request('unrelated', {});
|
|
expect(result.status).toBe(200);
|
|
expect(result.headers.get('Cache-Control')).toBeNull();
|
|
});
|
|
test('every mutation requires verified credentials even with loopback auth bypass or no middleware', async () => {
|
|
for (const mode of ['enabled', 'disabled', 'absent'] as const) {
|
|
const f = fixture(mode);
|
|
for (const item of writes) {
|
|
const result = await f.request(
|
|
item.path,
|
|
item.body,
|
|
undefined,
|
|
{ 'X-Actor': 'operator', 'X-Principal': 'operator', 'Idempotency-Key': 'forged' },
|
|
item.method
|
|
);
|
|
expect(result.status).toBe(401);
|
|
}
|
|
expect(f.database.query.mock.calls).toHaveLength(0);
|
|
expect(f.database.transaction.mock.calls).toHaveLength(0);
|
|
}
|
|
});
|
|
test('read credentials cannot mutate and service credentials cannot append human review', async () => {
|
|
const f = fixture();
|
|
for (const item of writes) {
|
|
expect((await f.request(item.path, item.body, 'read', {}, item.method)).status).toBe(403);
|
|
}
|
|
const review = await f.request(
|
|
`strategy-versions/${versionId}/reviews?actor=operator`,
|
|
{ decision: 'approve', reason: 'forged', actor: 'operator' },
|
|
'service',
|
|
{ 'X-Principal': 'operator' }
|
|
);
|
|
expect(review.status).toBe(403);
|
|
expect(f.database.query.mock.calls).toHaveLength(0);
|
|
expect(f.database.transaction.mock.calls).toHaveLength(0);
|
|
});
|
|
test('strict transport bodies reject privilege fields and missing or malformed receipt keys before storage', async () => {
|
|
const f = fixture();
|
|
const cases = [
|
|
{ path: 'strategies', body: { name: 'Candidate', ownerPrincipal: 'operator' } },
|
|
{
|
|
path: `strategies/${strategyId}`,
|
|
body: { name: 'Rename', origin: 'builtin' },
|
|
method: 'PATCH',
|
|
},
|
|
{
|
|
path: `strategies/${strategyId}/versions`,
|
|
body: { package: {}, origin: 'builtin' },
|
|
key: 'key',
|
|
},
|
|
{ path: `strategies/${strategyId}/versions`, body: { package: {} } },
|
|
{ path: `strategies/${strategyId}/versions`, body: { package: {} }, key: 'has spaces' },
|
|
{
|
|
path: `strategy-versions/${versionId}/clone`,
|
|
body: { name: 'Copy', reviewed: true },
|
|
key: 'key',
|
|
},
|
|
{
|
|
path: `strategy-versions/${versionId}/reviews`,
|
|
body: { decision: 'approve', reason: 'Review', actor: 'operator' },
|
|
},
|
|
{ path: `strategy-versions/${versionId}/validate`, body: { parameters: {}, execute: true } },
|
|
];
|
|
for (const item of cases) {
|
|
expect(
|
|
(
|
|
await f.request(
|
|
item.path,
|
|
item.body,
|
|
'operator',
|
|
item.key ? { 'Idempotency-Key': item.key } : {},
|
|
item.method
|
|
)
|
|
).status
|
|
).toBe(400);
|
|
}
|
|
expect(f.database.query.mock.calls).toHaveLength(0);
|
|
expect(f.database.transaction.mock.calls).toHaveLength(0);
|
|
});
|
|
test('malformed JSON is a client error and no seed or execution endpoint exists', async () => {
|
|
const f = fixture();
|
|
const response = await f.app.request('/api/v2/strategies', {
|
|
method: 'POST',
|
|
headers: { Authorization: `Bearer ${tokens.operator}` },
|
|
body: '{',
|
|
});
|
|
expect(response.status).toBe(400);
|
|
for (const path of [
|
|
'strategies/seed',
|
|
`strategy-versions/${versionId}/execute`,
|
|
`strategy-versions/${versionId}/deploy`,
|
|
]) {
|
|
expect((await f.request(path, {}, 'operator')).status).toBe(404);
|
|
}
|
|
expect(f.database.query.mock.calls).toHaveLength(0);
|
|
});
|
|
});
|