fix(auth): PUT /api/user/rename auch für naturalcrit-Sitzungen absichern (F14)
CI / Storage (mongo) (pull_request) Successful in 1m13s
CI / Storage (postgres) (pull_request) Successful in 1m12s
CI / App (npm ci + Build, ESLint, Stylelint, Jest) (pull_request) Successful in 1m51s

Die Upstream-Route prüft nur, ob newUsername der eigenen Sitzung gehört,
nicht den alten Namen. Der Fork-Router beantwortet die Route jetzt vorher:

- OIDC-Sitzung: 403 A01 (unverändert)
- ohne Anmeldung: 403
- naturalcrit, HB_NATURALCRIT_RENAME=off (Standard): 403 A02
- naturalcrit, HB_NATURALCRIT_RENAME=signed: nur mit Nachweis oldSession
  (nc_session-Token des alten Namens, HS256 mit SECRET), sonst 403 A03

Mit signed startet die App außerhalb lokaler Umgebungen nicht, solange
SECRET der Standardwert ist. server/app.js bleibt unverändert.
ADR 0001: neuer Abschnitt 7, Konfiguration, Tests, Folgen, F14.

Closes #21

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
archimedes-bot BotandClaude Opus 5.5 authored and archimedes-bot committed 2026-11-26 01:36:21 +00:00
1 parent bbf838c5d6
commit 03455c5bf5
9 files changed
+251 -43

No files matched your search

+85 -23
View File
@@ -21,13 +21,14 @@ const buildApp = (envOverrides = {}, depsOverrides = {})=>{
HB_OIDC_CLIENT_SECRET : CLIENT.client_secret, HB_SESSION_SECRET : SECRET, ...envOverrides
}, { upstreamSecret: 'eigenes-secret' });
const app = express();
app.use(express.json());
app.use(cookieParser());
// Stand-in für die Upstream-Account-Middleware: nc_session-Cookie == Benutzername
app.use((req, res, next)=>{ if(req.cookies.nc_session) req.account = { username: req.cookies.nc_session }; next(); });
app.use(accountMiddleware(config));
app.use(createAuthRouter(config, { getUsers: ()=>users, brewAuthorExists: async (n)=>authors.has(n), ...depsOverrides }));
app.get('/whoami', (req, res)=>res.json(req.account ?? null));
app.put('/api/user/rename', (req, res)=>{ renameCalled = true; res.json({ upstream: true }); });
app.put('/api/user/rename', (req, res)=>{ renameCalled = req.body; res.json({ upstream: true }); });
app.post('/local/login', (req, res)=>res.json('upstream-token'));
app.get('/', (req, res)=>res.send('home'));
delegate = app;
@@ -320,23 +321,93 @@ describe('Session-Gültigkeit', ()=>{
});
describe('PUT /api/user/rename', ()=>{
const UPSTREAM_SECRET = 'eigenes-secret'; // wie in buildApp()
const rename = (b, body)=>b.req('/api/user/rename', {
method : 'PUT', headers : { 'content-type': 'application/json' }, body : JSON.stringify(body)
});
const ncBrowser = (username)=>{ const b = browser(); b.jar.nc_session = username; return b; };
// nc_session-Token, wie naturalcrit es mit jwt-simple ausstellt (HS256, ohne exp)
const ncToken = (username, secret = UPSTREAM_SECRET, opts = {})=>jwt.sign({ username, issued: new Date().toISOString() }, secret, { noTimestamp: true, ...opts });
const expectDenied = async (res, code)=>{
expect(res.status).toBe(403);
const body = await res.json();
if(code) expect(body.HBErrorCode).toBe(code); else expect(body.HBErrorCode).toBeUndefined();
expect(renameCalled).toBe(false);
};
test('OIDC-Session → 403 A01, Upstream-Route wird nicht erreicht', async ()=>{
const { b } = await login('alice');
const res = await b.req('/api/user/rename', {
method : 'PUT', headers : { 'content-type': 'application/json' },
body : JSON.stringify({ username: 'victim', newUsername: 'alice' })
});
expect(res.status).toBe(403);
expect(await res.json()).toMatchObject({ HBErrorCode: 'A01' });
expect(renameCalled).toBe(false);
await expectDenied(await rename(b, { username: 'victim', newUsername: 'alice' }), 'A01');
});
test('naturalcrit-Session → Upstream-Verhalten', async ()=>{
const b = browser();
b.jar.nc_session = 'carol';
const res = await b.req('/api/user/rename', { method: 'PUT' });
expect(await res.json()).toEqual({ upstream: true });
expect(renameCalled).toBe(true);
test('OIDC-Session → 403 A01 auch mit HB_NATURALCRIT_RENAME=signed und gültigem Nachweis', async ()=>{
buildApp({ HB_NATURALCRIT_RENAME: 'signed' });
const { b } = await login('alice');
await expectDenied(await rename(b, { username: 'old', newUsername: 'alice', oldSession: ncToken('old') }), 'A01');
});
test.each([
['Standard (oidc,naturalcrit)', {}],
['nur oidc', { HB_AUTH_PROVIDERS: 'oidc' }],
['nur naturalcrit, signed', { HB_AUTH_PROVIDERS: 'naturalcrit', HB_NATURALCRIT_RENAME: 'signed' }]
])('ohne Anmeldung → 403 (%s)', async (name, env)=>{
buildApp(env);
await expectDenied(await rename(browser(), { username: 'victim', newUsername: 'mallory' }));
});
test('nur oidc: nc_session zählt nicht als Anmeldung → 403', async ()=>{
buildApp({ HB_AUTH_PROVIDERS: 'oidc', HB_NATURALCRIT_RENAME: 'signed' });
await expectDenied(await rename(ncBrowser('carol'), { username: 'carol-alt', newUsername: 'carol', oldSession: ncToken('carol-alt') }));
});
describe('naturalcrit, Standard HB_NATURALCRIT_RENAME=off', ()=>{
test('fremder alter Name → 403 A02', async ()=>{
await expectDenied(await rename(ncBrowser('mallory'), { username: 'victim', newUsername: 'mallory' }), 'A02');
});
test('auch mit gültigem Nachweis → 403 A02 (Endpunkt gesperrt)', async ()=>{
await expectDenied(await rename(ncBrowser('carol'), { username: 'carol-alt', newUsername: 'carol', oldSession: ncToken('carol-alt') }), 'A02');
});
});
describe('naturalcrit, HB_NATURALCRIT_RENAME=signed', ()=>{
beforeEach(()=>buildApp({ HB_NATURALCRIT_RENAME: 'signed' }));
test('eigener Rename mit Nachweis → 200, Upstream-Route erhält den Body ohne Token', async ()=>{
const res = await rename(ncBrowser('carol'), { username: 'carol-alt', newUsername: 'carol', oldSession: ncToken('carol-alt') });
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ upstream: true });
expect(renameCalled).toEqual({ username: 'carol-alt', newUsername: 'carol' });
});
test('fremder alter Name ohne Nachweis → 403 A03', async ()=>{
await expectDenied(await rename(ncBrowser('mallory'), { username: 'victim', newUsername: 'mallory' }), 'A03');
});
test('fremder alter Name mit eigenem Token als Nachweis → 403 A03', async ()=>{
await expectDenied(await rename(ncBrowser('mallory'), { username: 'victim', newUsername: 'mallory', oldSession: ncToken('mallory') }), 'A03');
});
test.each([
['fremder Schlüssel', ()=>ncToken('victim', 'x'.repeat(40))],
['alg HS512', ()=>ncToken('victim', UPSTREAM_SECRET, { algorithm: 'HS512' })],
['unsigniert (alg none)', ()=>jwt.sign({ username: 'victim' }, null, { algorithm: 'none' })],
['abgelaufen', ()=>ncToken('victim', UPSTREAM_SECRET, { expiresIn: -5 })],
['kein String', ()=>({ username: 'victim' })],
['Token ohne username', ()=>jwt.sign({ issued: 'x' }, UPSTREAM_SECRET)]
])('ungültiger Nachweis (%s) → 403 A03', async (name, token)=>{
await expectDenied(await rename(ncBrowser('mallory'), { username: 'victim', newUsername: 'mallory', oldSession: token() }), 'A03');
});
test('newUsername gehört nicht zur Sitzung → 403 A03', async ()=>{
await expectDenied(await rename(ncBrowser('mallory'), { username: 'mallory', newUsername: 'victim', oldSession: ncToken('mallory') }), 'A03');
});
test('fehlende Felder → 400', async ()=>{
const res = await rename(ncBrowser('carol'), { newUsername: 'carol' });
expect(res.status).toBe(400);
expect(renameCalled).toBe(false);
});
});
});
@@ -378,13 +449,4 @@ describe('Alt-Logins abschaltbar (HB_AUTH_PROVIDERS)', ()=>{
expect(await (await b.req('/whoami')).json()).toEqual({ username: 'carol', provider: 'naturalcrit' });
expect((await b.req('/local/login', { method: 'POST' })).status).toBe(200);
});
test('nur oidc: PUT /api/user/rename ohne OIDC-Session erreicht die Upstream-Route (dort greift die Kontoprüfung)', async ()=>{
buildApp({ HB_AUTH_PROVIDERS: 'oidc' });
const b = browser();
b.jar.nc_session = 'carol';
const res = await b.req('/api/user/rename', { method: 'PUT' });
expect(renameCalled).toBe(true);
expect(res.status).toBe(200);
});
});