CI / App (npm ci + Build, ESLint, Stylelint, Jest) (pull_request) Successful in 1m42s
- M1: Start bricht ab, wenn oidc+naturalcrit aktiv, nicht lokal und Upstream-secret Standard/leer - M2: README.md wieder mit CRLF (Diff nur +4) - S1: /auth/oidc/login zeigt interne Fehlertexte nicht mehr an - S2: http://-Issuer nur bei http://-HB_PUBLIC_URL Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
49 lines
2.4 KiB
JavaScript
49 lines
2.4 KiB
JavaScript
import jwt from 'jsonwebtoken';
|
|
import { readAuthConfig } from './config.js';
|
|
import { readSession, sealTx, openTx, safeRedirect, SESSION_COOKIE } from './session.js';
|
|
|
|
const config = readAuthConfig({
|
|
HB_PUBLIC_URL : 'http://localhost:8000', HB_OIDC_ISSUER : 'http://idp', HB_OIDC_CLIENT_ID : 'id',
|
|
HB_OIDC_CLIENT_SECRET : 's', HB_SESSION_SECRET : 'a'.repeat(32)
|
|
}, { upstreamSecret: 'eigenes-secret' });
|
|
const req = (token)=>({ cookies: { [SESSION_COOKIE]: token } });
|
|
const payload = { v: 1, uid: 'u', username: 'bob', iss: 'i', sub: 's' };
|
|
|
|
describe('Session-Cookie', ()=>{
|
|
test('gültiges Token', ()=>{
|
|
expect(readSession(req(jwt.sign(payload, config.sessionSecret, { expiresIn: 60 })), config)).toMatchObject({ username: 'bob' });
|
|
});
|
|
test('abgelaufen, falsches Secret, alg none und Müll werden abgelehnt', ()=>{
|
|
expect(readSession(req(jwt.sign(payload, config.sessionSecret, { expiresIn: -10 })), config)).toBeNull();
|
|
expect(readSession(req(jwt.sign(payload, 'b'.repeat(32), { expiresIn: 60 })), config)).toBeNull();
|
|
expect(readSession(req(jwt.sign(payload, '', { algorithm: 'none' })), config)).toBeNull();
|
|
expect(readSession(req(jwt.sign(payload, config.sessionSecret, { algorithm: 'HS512', expiresIn: 60 })), config)).toBeNull();
|
|
expect(readSession(req('müll'), config)).toBeNull();
|
|
expect(readSession({ cookies: {} }, config)).toBeNull();
|
|
});
|
|
test('Upstream-JWT (nc_session-Format ohne v/uid) wird nicht als Session akzeptiert', ()=>{
|
|
expect(readSession(req(jwt.sign({ username: 'bob' }, config.sessionSecret, { expiresIn: 60 })), config)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('Transaktions-Cookie', ()=>{
|
|
test('Roundtrip und Manipulationsschutz', ()=>{
|
|
const sealed = sealTx(config, { a: 1 });
|
|
expect(openTx(config, sealed)).toEqual({ a: 1 });
|
|
const broken = sealed.slice(0, -2) + (sealed.endsWith('AA') ? 'BB' : 'AA');
|
|
expect(openTx(config, broken)).toBeNull();
|
|
expect(openTx({ ...config, sessionSecret: 'c'.repeat(32) }, sealed)).toBeNull();
|
|
expect(openTx(config, 'kurz')).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('safeRedirect', ()=>{
|
|
test.each([
|
|
['/brews', '/brews'], ['/edit/abc?x=1#y', '/edit/abc?x=1#y'], ['/', '/'],
|
|
['//evil.com', '/'], ['https://evil.com', '/'], ['/\\evil.com', '/'], ['javascript:alert(1)', '/'],
|
|
['/a\r\nSet-Cookie: x=y', '/'], [undefined, '/'], [['/a'], '/']
|
|
])('%j -> %j', (input, expected)=>{
|
|
expect(safeRedirect(input)).toBe(expected);
|
|
});
|
|
});
|