From d4059d801ad7df97a0433bab0c01956c8158407c Mon Sep 17 00:00:00 2001 From: David Santos Date: Wed, 21 Oct 2026 18:16:05 +0100 Subject: [PATCH] fix(hub): BIT-31442 route /v1/paperclip alias to admin API + accept X-Api-Key header 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. --- admin-ui/next.config.ts | 9 ++++ api/src/lib/errorMonitoringAuth.ts | 16 +++++++ api/src/routes/public/recentErrors.test.ts | 56 ++++++++++++++++++++++ docs/HTTP_CONTRACTS.md | 4 ++ docs/permission-matrix.md | 5 +- 5 files changed, 89 insertions(+), 1 deletion(-) diff --git a/admin-ui/next.config.ts b/admin-ui/next.config.ts index 001dc8d..5527414 100644 --- a/admin-ui/next.config.ts +++ b/admin-ui/next.config.ts @@ -23,6 +23,15 @@ const nextConfig: NextConfig = { async rewrites() { return [ { source: "/health", destination: `${INGEST_API_URL}/health` }, + // BIT-31442: the recent-errors handler (api/src/routes/public/recentErrors.ts) + // registers the /v1/paperclip/recent-errors alias on the admin API (port 3200), + // not on ingest-api. The generic /v1/* rule below would otherwise send it to + // ingest-api (port 3041), which has no such route and returns 404. This more + // specific rule must come first; Next.js evaluates rewrites in order. + { + source: "/v1/paperclip/:path*", + destination: `${ADMIN_API_URL}/v1/paperclip/:path*`, + }, { source: "/v1/:path*", destination: `${INGEST_API_URL}/v1/:path*` }, { source: "/api/error-monitoring/:path*", diff --git a/api/src/lib/errorMonitoringAuth.ts b/api/src/lib/errorMonitoringAuth.ts index b0ccf42..ea66770 100644 --- a/api/src/lib/errorMonitoringAuth.ts +++ b/api/src/lib/errorMonitoringAuth.ts @@ -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', diff --git a/api/src/routes/public/recentErrors.test.ts b/api/src/routes/public/recentErrors.test.ts index 53ceee6..1094b14 100644 --- a/api/src/routes/public/recentErrors.test.ts +++ b/api/src/routes/public/recentErrors.test.ts @@ -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(); + }); }); diff --git a/docs/HTTP_CONTRACTS.md b/docs/HTTP_CONTRACTS.md index dd7f914..d8cda19 100644 --- a/docs/HTTP_CONTRACTS.md +++ b/docs/HTTP_CONTRACTS.md @@ -59,6 +59,10 @@ Alias: `GET /v1/paperclip/recent-errors` (same handler). **Query:** `api_key` *or* `view_errors`, plus optional `limit`, `offset`, `status`, `severity`, `project_slug`, `q`, `order`. +**Header (BIT-31442):** `X-Api-Key: ` is accepted as an alternative to the `api_key`/`view_errors` query params, so programmatic callers can avoid leaking the key in URLs and access logs. The header may carry either `ERROR_RESOLVE_API_KEY`/`ERROR_REPORTING_API_KEY` or the read-only `DEBUG_VIEW_ERRORS_KEY`. + +> **Proxy note (BIT-31442):** the `/v1/paperclip/*` alias is served by the admin API (port 3200), not ingest-api (port 3041). The `admin-ui` Next.js rewrite routes `/v1/paperclip/:path*` to the admin API *before* the generic `/v1/:path*` → ingest-api rule. If the alias returns 404 in production, verify that rewrite order in `admin-ui/next.config.ts`. + **Response 200** ```json diff --git a/docs/permission-matrix.md b/docs/permission-matrix.md index adb4fb2..2fa1346 100644 --- a/docs/permission-matrix.md +++ b/docs/permission-matrix.md @@ -50,11 +50,14 @@ These routes mirror the CRM error-monitoring module for Paperclip and automation ### Read: `GET /api/error-monitoring/recent-errors` +Alias: `GET /v1/paperclip/recent-errors` (same handler; served by the admin API via the `admin-ui` rewrite — BIT-31442). + | Credential | Notes | |------------|--------| | `api_key` query | Must match `ERROR_RESOLVE_API_KEY` or `ERROR_REPORTING_API_KEY` | | `view_errors` query | Must match `DEBUG_VIEW_ERRORS_KEY` (read-oriented) | -| Admin session | Not used on this path; query credentials only | +| `X-Api-Key` header (BIT-31442) | Carries either the resolve/reporting key or `DEBUG_VIEW_ERRORS_KEY`; alternative to the query params to avoid key leakage in URLs/logs | +| Admin session | Not used on this path; query/header credentials only | At least one of the server secrets above must be configured or the endpoint returns **503**.