fix(hub): BIT-31442 route /v1/paperclip alias to admin API + accept X-Api-Key header
CI / Test ingest-api (pull_request) Successful in 41s
CI / Test api (pull_request) Successful in 44s
CI / Lint and typecheck admin-ui (pull_request) Successful in 1m24s
CI / Validate Docker build (pull_request) Successful in 6m2s

The recent-errors handler (api/src/routes/public/recentErrors.ts) registers
both GET /api/error-monitoring/recent-errors and the alias
GET /v1/paperclip/recent-errors on the admin API (port 3200). However the
admin-ui Next.js rewrite sent all /v1/* to ingest-api (port 3041), which has
no such route, so the alias returned 404 in production while the
/api/error-monitoring path worked (401 without a key).

Changes:
- admin-ui/next.config.ts: add a specific /v1/paperclip/:path* -> admin API
  rewrite before the generic /v1/:path* -> ingest-api rule (rewrite order
  matters; first match wins).
- api/src/lib/errorMonitoringAuth.ts: authorizeReadQuery now also accepts an
  X-Api-Key header carrying either ERROR_RESOLVE_API_KEY/ERROR_REPORTING_API_KEY
  or DEBUG_VIEW_ERRORS_KEY, so programmatic callers avoid leaking keys in
  URLs/access logs. Backward compatible with the existing query params.
- api/src/routes/public/recentErrors.test.ts: cover the alias path and the
  X-Api-Key header (resolve key, view key, invalid key).
- docs/HTTP_CONTRACTS.md, docs/permission-matrix.md: document the header and
  the proxy ordering requirement.
This commit is contained in:
David Santos committed 2026-10-21 18:16:05 +01:00
1 parent 954b0ced2e
commit d4059d801a
5 files changed
+89 -1

No files matched your search

+16
View File
@@ -45,6 +45,22 @@ export function authorizeReadQuery(request: FastifyRequest, reply: FastifyReply)
return true;
}
// BIT-31442: accept X-Api-Key header as an alternative to the query param so
// programmatic callers (Paperclip, QA) can avoid leaking the key in URLs/logs.
// The header may carry either the resolve/reporting key or the read-only
// DEBUG_VIEW_ERRORS_KEY, mirroring the query-param semantics.
const headerRaw = request.headers['x-api-key'];
const headerVal = Array.isArray(headerRaw) ? headerRaw[0] : headerRaw;
const providedHeader = typeof headerVal === 'string' ? headerVal.trim() : '';
if (providedHeader !== '') {
if (apiKey && safeEqual(providedHeader, apiKey)) {
return true;
}
if (viewKey && safeEqual(providedHeader, viewKey)) {
return true;
}
}
reply.code(401).send({
success: false,
message: 'Unauthorized',
@@ -171,4 +171,60 @@ describe('GET /api/error-monitoring/recent-errors', () => {
// ['resolved', '%boom%', '%boom%', '%boom%', limit, offset]
expect(params).toEqual(['resolved', '%boom%', '%boom%', '%boom%', 10, 0]);
});
it('serves the /v1/paperclip/recent-errors alias from the same handler (BIT-31442)', async () => {
const execute = vi.fn().mockResolvedValue([[]]);
const app = await buildApp(execute);
const res = await app.inject({
method: 'GET',
url: '/v1/paperclip/recent-errors?api_key=test-recent-errors-key',
});
expect(res.statusCode).toBe(200);
expect(execute).toHaveBeenCalledTimes(1);
});
it('accepts the resolve key via X-Api-Key header (BIT-31442)', async () => {
const execute = vi.fn().mockResolvedValue([[]]);
const app = await buildApp(execute);
const res = await app.inject({
method: 'GET',
url: '/api/error-monitoring/recent-errors',
headers: { 'x-api-key': 'test-recent-errors-key' },
});
expect(res.statusCode).toBe(200);
expect(execute).toHaveBeenCalledTimes(1);
});
it('accepts the read-only view key via X-Api-Key header (BIT-31442)', async () => {
const prevView = process.env.DEBUG_VIEW_ERRORS_KEY;
process.env.DEBUG_VIEW_ERRORS_KEY = 'test-view-key';
try {
const execute = vi.fn().mockResolvedValue([[]]);
const app = await buildApp(execute);
const res = await app.inject({
method: 'GET',
url: '/api/error-monitoring/recent-errors',
headers: { 'x-api-key': 'test-view-key' },
});
expect(res.statusCode).toBe(200);
} finally {
if (prevView === undefined) {
delete process.env.DEBUG_VIEW_ERRORS_KEY;
} else {
process.env.DEBUG_VIEW_ERRORS_KEY = prevView;
}
}
});
it('rejects an invalid X-Api-Key header with 401 (BIT-31442)', async () => {
const execute = vi.fn().mockResolvedValue([[]]);
const app = await buildApp(execute);
const res = await app.inject({
method: 'GET',
url: '/api/error-monitoring/recent-errors',
headers: { 'x-api-key': 'wrong-key' },
});
expect(res.statusCode).toBe(401);
expect(execute).not.toHaveBeenCalled();
});
});