Serve the backend under a base path: one URL per environment, the backend under /api

The test deployment keeps one base URL per environment, as the sibling
ActivityPods apps do, so a pull-request preview lives on a single host: the
app at the root, the backend under /api on the same host, its /.well-known
routes at the origin root, the proxy passing paths through unchanged.

SemApps already works that way — it mounts every route it adds under the base
URL's path and keeps /.well-known at the root — but Festipod's own pieces were
written for a backend at the root of its own origin: its routes (/app,
/as/actor/app, /catalog) were mounted at fixed paths, the public-surface
allow-list matched fixed paths (so under /api every request would have been
refused), and the Solid-OIDC client_id joined the raw request path to the base
URL, which would have repeated the base path (/api/api/...).

base-path.js derives the base path from SEMAPPS_HOME_URL; the routes are
mounted under it; the allow-list matches paths relative to it, the two
/.well-known endpoints against the full path; the client_id is the requested
URI rebuilt from the base URL as written, equal byte for byte to the actor
URI and to what the front end derives. At the root every gate decision is
identical to before (compared over ~8M method/path cases). The rights matrix
probes /.well-known at the backend's origin root.

Verified: bun test (424 pass); the full gate at the root; and the built images
behind a local proxy routing /api and /.well-known unstripped with the Host
header kept — login flow, rights matrix 98/98, revocation.
This commit is contained in:
Sylvain Duchesne
2026-10-19 16:44:09 +02:00
parent 8d0670a637
commit 738d59596a
8 changed files with 214 additions and 33 deletions
+70 -4
View File
@@ -1,16 +1,33 @@
const path = require('path');
const { test, expect, describe, afterEach } = require('bun:test');
const { ServiceBroker } = require('moleculer');
const { isPublicRequest, publicSurfaceGate } = require('./public-surface');
const { isPublicRequest, createPublicSurfaceGate } = require('./public-surface');
const { basePathOf } = require('./base-path');
const UUID = 'd9b5f8ca-b2e2-41eb-af83-64e5ef1d4b20';
describe('the core service mounts the gate in front of every route', () => {
test('core.service.js wires publicSurfaceGate as a global gateway middleware, and no SPARQL endpoint', () => {
test('core.service.js wires the gate, for its own base path, as a global gateway middleware, and no SPARQL endpoint', () => {
const core = require('./core/core.service.js');
expect(core.settings.api.use).toContain(publicSurfaceGate);
const CONFIG = require('../config/config');
const base = basePathOf(CONFIG.HOME_URL);
expect(core.settings.api.use).toHaveLength(1);
const [gate] = core.settings.api.use;
expect(gate.name).toBe('publicSurfaceGate');
let passed;
gate({ method: 'GET', parsedUrl: `${base}catalog`, headers: {} }, {}, err => (passed = err === undefined));
expect(passed).toBe(true);
gate({ method: 'GET', parsedUrl: `${base}key`, headers: {} }, {}, err => (passed = err === undefined));
expect(passed).toBe(false);
expect(core.settings.sparqlEndpoint).toBe(false);
});
test("core.service.js mounts Festipod's own routes under its base path", () => {
const core = require('./core/core.service.js');
const CONFIG = require('../config/config');
const base = basePathOf(CONFIG.HOME_URL);
expect(core.settings.api.routes.map(route => route.path)).toEqual([`${base}app`, `${base}as/actor/app`, `${base}catalog`]);
});
});
describe('isPublicRequest — the allow-list', () => {
@@ -89,7 +106,56 @@ describe('isPublicRequest — the allow-list', () => {
});
});
describe('isPublicRequest — under a base path (the Coolify layout: the backend at /api/ on the front end origin)', () => {
const BASE = '/api/';
test('every public endpoint is public under the base path, the /.well-known ones at the origin root', () => {
for (const [method, p] of [
['GET', '/api/app'],
['GET', '/api/as/actor/app'],
['GET', '/api/catalog'],
['GET', `/api/public-key/${UUID}`],
['GET', `/api/interop/access-need-group/${UUID}`],
['GET', `/api/interop/access-need/${UUID}`],
['GET', '/api/nodeinfo/2.1'],
['POST', '/api/as/actor/app/inbox'],
['POST', `/api/.webhooks/${UUID}`],
['GET', '/.well-known/context.jsonld'],
['GET', '/.well-known/nodeinfo']
]) {
expect(`${method} ${p} ${isPublicRequest(method, p, BASE)}`).toBe(`${method} ${p} true`);
}
});
test('the same endpoints outside the base path, and the /.well-known ones under it, are refused', () => {
for (const [method, p] of [
['GET', '/app'],
['GET', '/as/actor/app'],
['GET', '/catalog'],
['GET', `/public-key/${UUID}`],
['POST', '/as/actor/app/inbox'],
['POST', `/.webhooks/${UUID}`],
['GET', '/api/.well-known/context.jsonld'],
['GET', '/api/.well-known/nodeinfo'],
['GET', '/api'],
['GET', '/apiapp'],
['GET', `/apipublic-key/${UUID}`]
]) {
expect(`${method} ${p} ${isPublicRequest(method, p, BASE)}`).toBe(`${method} ${p} false`);
}
});
test('what is refused at the root is refused under the base path', () => {
for (const p of ['/', '/key', `/key/${UUID}`, '/sparql', '/proxy', `/interop/access-grant/${UUID}`, '/as/actor/app/outbox', `/public-key/../key/${UUID}`, `//public-key/${UUID}`]) {
expect(`GET /api${p} ${isPublicRequest('GET', `/api${p}`, BASE)}`).toBe(`GET /api${p} false`);
}
expect(isPublicRequest('PUT', '/api/catalog', BASE)).toBe(false);
expect(isPublicRequest('POST', '/api/app', BASE)).toBe(false);
});
});
describe('publicSurfaceGate — the middleware', () => {
const publicSurfaceGate = createPublicSurfaceGate('/');
const run = req => {
let passed;
publicSurfaceGate(req, {}, err => {
@@ -163,7 +229,7 @@ describe('the gate covers routes added at runtime — the LDP catch-all that ser
});
test('with the gate: the key container, the root and any write are 404; a public key is served', async () => {
const base = await startGateway([publicSurfaceGate]);
const base = await startGateway([createPublicSurfaceGate('/')]);
expect(await status(base, 'GET', '/key')).toBe(404);
expect(await status(base, 'GET', `/key/${UUID}`)).toBe(404);
expect(await status(base, 'GET', '/')).toBe(404);