feat(backend): add an application-layer gate to the admin API (#63)
Authorization for the admin panel and the admin API has lived entirely in one auth_request regex in an Nginx Proxy Manager config outside this repository. That control is real and it works — nothing is publicly exposed today — but it is invisible from the code, untested here, and not reviewed when this code changes. Three things follow from that, and the first is the one worth the change. An admin route added at a path the regex does not match is unprotected the moment it is written, and nothing in Express indicates that. Anything reaching the published container port directly bypasses authentik entirely. And locally there is no gate at all, so no developer ever sees the boundary being enforced. requireAdminGate is attached to each admin router rather than to a path prefix, which is what makes it useful rather than merely redundant with the proxy. An admin router added later at some other path inherits the gate; because the proxy only injects the header on paths its regex matches, that router refuses on its first request instead of being quietly public. A 403 in that situation is the boundary reporting that it has drifted. The gate is optional, and unset means exactly today's behaviour. That keeps local development and all 113 existing admin test call sites working untouched, and means shipping the image before configuring the proxy cannot take the admin panel down. What it does not do is stay silent about it: the server warns at boot when the gate is inactive, naming what is unprotected. This project has been bitten repeatedly by controls that report success while doing nothing, and an unconfigured gate should be a visible choice rather than an invisible one. An empty value is treated as unset rather than as a secret, because enforcing an empty secret would admit any caller sending an empty header. Comparison is timing-safe over SHA-256 digests of both sides: timingSafeEqual throws on buffers of unequal length, so comparing raw values would turn a short header into a 500 rather than a 403, and a length check first would leak the secret's length. Turning it on requires the secret in two places at once — the stack environment and a proxy_set_header line on the gated location in NPM. Setting only one gives 403s until the other catches up. That coupling, and the three consequences above, are now written into the README beside the deployment section, since none of it is visible from the code. Verified over real HTTP as well as in tests. Booting without the secret logs the warning and serves admin normally; booting with it returns 403 for a missing header, 403 for a wrong one, 200 for the right one, and leaves the public storefront at 200 throughout, with each refusal logged distinguishably and without echoing the value it was sent. 8 new unit tests, 9 new integration tests covering every admin router separately — a correct middleware nobody mounted would pass the unit tests and protect nothing. 106 unit and 153 integration passing, lint 0 errors and 8 warnings unchanged. Refs #63 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import request from 'supertest';
|
||||
import app from '../../src/app';
|
||||
import { pool } from '../../src/db';
|
||||
import { resetDb, closeDb } from './setup/testDb';
|
||||
|
||||
// The unit tests prove the middleware decides correctly. This proves it is
|
||||
// actually attached to the admin routers — a correct function nobody mounted
|
||||
// would pass every unit test and protect nothing.
|
||||
//
|
||||
// ADMIN_GATE_SECRET is read per request rather than at import, so it can be set
|
||||
// here without reloading the app. It is restored after each test so this file
|
||||
// cannot change how anything running later in the same process behaves.
|
||||
|
||||
const SECRET = 'integration-gate-secret';
|
||||
const original = process.env.ADMIN_GATE_SECRET;
|
||||
let warn: jest.SpyInstance;
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetDb();
|
||||
warn = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
warn.mockRestore();
|
||||
if (original === undefined) {
|
||||
delete process.env.ADMIN_GATE_SECRET;
|
||||
} else {
|
||||
process.env.ADMIN_GATE_SECRET = original;
|
||||
}
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await pool.end();
|
||||
await closeDb();
|
||||
});
|
||||
|
||||
describe('the admin gate, mounted on the real routers', () => {
|
||||
it('refuses an admin request with no gate header once a secret is configured', async () => {
|
||||
process.env.ADMIN_GATE_SECRET = SECRET;
|
||||
|
||||
const res = await request(app).get('/api/admin/items');
|
||||
|
||||
expect(res.status).toBe(403);
|
||||
expect(res.body.error).toBe('forbidden');
|
||||
});
|
||||
|
||||
it('serves the same request when it carries the gate header', async () => {
|
||||
process.env.ADMIN_GATE_SECRET = SECRET;
|
||||
|
||||
const res = await request(app).get('/api/admin/items').set('X-Admin-Gate', SECRET);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(Array.isArray(res.body)).toBe(true);
|
||||
});
|
||||
|
||||
// Every admin router has to be covered, not just the one mounted last. These
|
||||
// are separate app.use calls, so one of them missing the middleware is an
|
||||
// easy and completely silent mistake.
|
||||
it.each([
|
||||
['/api/admin/customers', 'customers'],
|
||||
['/api/admin/categories', 'categories'],
|
||||
['/api/admin/tags', 'tags'],
|
||||
['/api/admin/settings', 'settings'],
|
||||
['/api/admin/items', 'inventory']
|
||||
])('gates %s', async (path) => {
|
||||
process.env.ADMIN_GATE_SECRET = SECRET;
|
||||
|
||||
const refused = await request(app).get(path);
|
||||
expect(refused.status).toBe(403);
|
||||
|
||||
const allowed = await request(app).get(path).set('X-Admin-Gate', SECRET);
|
||||
expect(allowed.status).not.toBe(403);
|
||||
});
|
||||
|
||||
// The state every existing admin test relies on, and the state production
|
||||
// runs in until the proxy is configured.
|
||||
it('leaves admin reachable when no secret is configured', async () => {
|
||||
delete process.env.ADMIN_GATE_SECRET;
|
||||
|
||||
const res = await request(app).get('/api/admin/items');
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
|
||||
// The storefront must not be caught by the gate — it is public by design and
|
||||
// the proxy does not add the header to it in production.
|
||||
it('does not gate the public storefront', async () => {
|
||||
process.env.ADMIN_GATE_SECRET = SECRET;
|
||||
|
||||
const res = await request(app).get('/api/items');
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user