fix(auth): Review-Findings M1, M2, S1, S2
CI / App (npm ci + Build, ESLint, Stylelint, Jest) (pull_request) Successful in 1m42s
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>
This commit is contained in:
1 parent
fb9f4b0345
commit
66bb91cb3e
8 files changed
+224
-184
No files matched your search
+1
-1
@@ -116,7 +116,7 @@ export default async function createApp(vite) {
|
||||
});
|
||||
|
||||
// Fork: OIDC-Login (ADR 0001); ersetzt req.account nach der Upstream-Middleware
|
||||
const authConfig = readAuthConfig();
|
||||
const authConfig = readAuthConfig(process.env, { upstreamSecret: config.get('secret'), isLocal: isLocalEnvironment });
|
||||
app.use(accountMiddleware(authConfig));
|
||||
app.use(createAuthRouter(authConfig, {
|
||||
getUsers : ()=>getStorage().users,
|
||||
|
||||
@@ -19,7 +19,7 @@ const buildApp = (envOverrides = {}, depsOverrides = {})=>{
|
||||
const config = readAuthConfig({
|
||||
HB_PUBLIC_URL : appUrl, HB_OIDC_ISSUER : idp.issuer, HB_OIDC_CLIENT_ID : CLIENT.client_id,
|
||||
HB_OIDC_CLIENT_SECRET : CLIENT.client_secret, HB_SESSION_SECRET : SECRET, ...envOverrides
|
||||
});
|
||||
}, { upstreamSecret: 'eigenes-secret' });
|
||||
const app = express();
|
||||
app.use(cookieParser());
|
||||
// Stand-in für die Upstream-Account-Middleware: nc_session-Cookie == Benutzername
|
||||
@@ -203,7 +203,7 @@ describe('OIDC-Login (Authorization Code + PKCE) gegen Mock-IdP', ()=>{
|
||||
const b = browser();
|
||||
const start = await b.req('/auth/oidc/login');
|
||||
const { callbackUrl } = await loginAtIdp(start.headers.get('location'), { login: 'alice' });
|
||||
const config = readAuthConfig({ HB_PUBLIC_URL: appUrl, HB_OIDC_ISSUER: idp.issuer, HB_OIDC_CLIENT_ID: 'x', HB_OIDC_CLIENT_SECRET: 'x', HB_SESSION_SECRET: SECRET });
|
||||
const config = readAuthConfig({ HB_PUBLIC_URL: appUrl, HB_OIDC_ISSUER: idp.issuer, HB_OIDC_CLIENT_ID: 'x', HB_OIDC_CLIENT_SECRET: 'x', HB_SESSION_SECRET: SECRET }, { upstreamSecret: 'eigenes-secret' });
|
||||
const tx = openTx(config, b.jar[TX_COOKIE]);
|
||||
b.jar[TX_COOKIE] = sealTx(config, { ...tx, createdAt: Date.now() - 11 * 60 * 1000 });
|
||||
await expectErrorPage(await b.req(callbackUrl), 400);
|
||||
@@ -222,7 +222,7 @@ describe('OIDC-Login (Authorization Code + PKCE) gegen Mock-IdP', ()=>{
|
||||
const b = browser();
|
||||
const start = await b.req('/auth/oidc/login');
|
||||
const { callbackUrl } = await loginAtIdp(start.headers.get('location'), { login: 'alice' });
|
||||
const config = readAuthConfig({ HB_PUBLIC_URL: appUrl, HB_OIDC_ISSUER: idp.issuer, HB_OIDC_CLIENT_ID: 'x', HB_OIDC_CLIENT_SECRET: 'x', HB_SESSION_SECRET: SECRET });
|
||||
const config = readAuthConfig({ HB_PUBLIC_URL: appUrl, HB_OIDC_ISSUER: idp.issuer, HB_OIDC_CLIENT_ID: 'x', HB_OIDC_CLIENT_SECRET: 'x', HB_SESSION_SECRET: SECRET }, { upstreamSecret: 'eigenes-secret' });
|
||||
const tx = openTx(config, b.jar[TX_COOKIE]);
|
||||
b.jar[TX_COOKIE] = sealTx(config, { ...tx, nonce: 'anderer-nonce' });
|
||||
await expectErrorPage(await b.req(callbackUrl), 400);
|
||||
@@ -234,7 +234,7 @@ describe('OIDC-Login (Authorization Code + PKCE) gegen Mock-IdP', ()=>{
|
||||
const b = browser();
|
||||
const start = await b.req('/auth/oidc/login');
|
||||
const { callbackUrl } = await loginAtIdp(start.headers.get('location'), { login: 'alice' });
|
||||
const config = readAuthConfig({ HB_PUBLIC_URL: appUrl, HB_OIDC_ISSUER: idp.issuer, HB_OIDC_CLIENT_ID: 'x', HB_OIDC_CLIENT_SECRET: 'x', HB_SESSION_SECRET: SECRET });
|
||||
const config = readAuthConfig({ HB_PUBLIC_URL: appUrl, HB_OIDC_ISSUER: idp.issuer, HB_OIDC_CLIENT_ID: 'x', HB_OIDC_CLIENT_SECRET: 'x', HB_SESSION_SECRET: SECRET }, { upstreamSecret: 'eigenes-secret' });
|
||||
const tx = openTx(config, b.jar[TX_COOKIE]);
|
||||
b.jar[TX_COOKIE] = sealTx(config, { ...tx, codeVerifier: 'A'.repeat(43) });
|
||||
await expectErrorPage(await b.req(callbackUrl), 400);
|
||||
|
||||
@@ -18,7 +18,7 @@ export const parseTtl = (value, def = 7 * 24 * 3600)=>{
|
||||
return seconds;
|
||||
};
|
||||
|
||||
export const readAuthConfig = (env = process.env)=>{
|
||||
export const readAuthConfig = (env = process.env, { upstreamSecret, isLocal = false } = {})=>{
|
||||
const issuer = env.HB_OIDC_ISSUER || undefined;
|
||||
|
||||
let providers;
|
||||
@@ -63,7 +63,16 @@ export const readAuthConfig = (env = process.env)=>{
|
||||
throw new Error(`HB_SESSION_SECRET muss mindestens ${MIN_SECRET_BYTES} Byte lang sein`);
|
||||
}
|
||||
if(!/^https?:\/\//.test(config.publicUrl)) throw new Error('HB_PUBLIC_URL muss mit http:// oder https:// beginnen');
|
||||
try { new URL(config.oidc.issuer); } catch { throw new Error('HB_OIDC_ISSUER ist keine gültige URL'); }
|
||||
let issuerUrl;
|
||||
try { issuerUrl = new URL(config.oidc.issuer); } catch { throw new Error('HB_OIDC_ISSUER ist keine gültige URL'); }
|
||||
// http://-Issuer nur im Dev-Setup (HB_PUBLIC_URL selbst http://), sonst läuft Discovery/JWKS/Token unverschlüsselt
|
||||
if(issuerUrl.protocol === 'http:' && config.publicUrl.startsWith('https://')) {
|
||||
throw new Error('HB_OIDC_ISSUER muss https:// verwenden, wenn HB_PUBLIC_URL https:// ist');
|
||||
}
|
||||
// Upstream-nc_session wird mit config "secret" signiert; Standardwert => Sitzung fälschbar
|
||||
if(providers.includes('naturalcrit') && !isLocal && (!upstreamSecret || upstreamSecret === 'secret')) {
|
||||
throw new Error('Auth: "naturalcrit" ist neben "oidc" aktiv, aber das Upstream-"secret" ist der Standardwert. Entweder SECRET setzen oder HB_AUTH_PROVIDERS=oidc.');
|
||||
}
|
||||
}
|
||||
|
||||
return config;
|
||||
|
||||
@@ -5,6 +5,8 @@ const base = {
|
||||
HB_OIDC_CLIENT_ID : 'id', HB_OIDC_CLIENT_SECRET : 's', HB_SESSION_SECRET : 'x'.repeat(32)
|
||||
};
|
||||
|
||||
const own = { upstreamSecret: 'eigenes-secret' };
|
||||
|
||||
describe('Auth-Konfiguration', ()=>{
|
||||
test('Standard ohne Angaben: nur naturalcrit (Upstream-Verhalten)', ()=>{
|
||||
const c = readAuthConfig({});
|
||||
@@ -13,7 +15,7 @@ describe('Auth-Konfiguration', ()=>{
|
||||
});
|
||||
|
||||
test('Issuer gesetzt, Provider leer: oidc,naturalcrit mit Auswahlseite', ()=>{
|
||||
const c = readAuthConfig(base);
|
||||
const c = readAuthConfig(base, own);
|
||||
expect(c.providers).toEqual(['oidc', 'naturalcrit']);
|
||||
expect(c.publicUrl).toBe('https://brew.example.org');
|
||||
expect(c.secureCookies).toBe(true);
|
||||
@@ -32,12 +34,12 @@ describe('Auth-Konfiguration', ()=>{
|
||||
});
|
||||
|
||||
test('zu kurzes Session-Secret und unbekannter Provider brechen ab', ()=>{
|
||||
expect(()=>readAuthConfig({ ...base, HB_SESSION_SECRET: 'kurz' })).toThrow(/32 Byte/);
|
||||
expect(()=>readAuthConfig({ ...base, HB_SESSION_SECRET: 'kurz' }, own)).toThrow(/32 Byte/);
|
||||
expect(()=>readAuthConfig({ HB_AUTH_PROVIDERS: 'ldap' })).toThrow(/unbekannte/);
|
||||
});
|
||||
|
||||
test('http-URL: Cookies nicht Secure', ()=>{
|
||||
expect(readAuthConfig({ ...base, HB_PUBLIC_URL: 'http://localhost:8000' }).secureCookies).toBe(false);
|
||||
expect(readAuthConfig({ ...base, HB_PUBLIC_URL: 'http://localhost:8000' }, own).secureCookies).toBe(false);
|
||||
});
|
||||
|
||||
test('TTL-Formate', ()=>{
|
||||
@@ -50,6 +52,34 @@ describe('Auth-Konfiguration', ()=>{
|
||||
});
|
||||
|
||||
test('Secret taucht nicht in der Client-Konfiguration auf', ()=>{
|
||||
expect(JSON.stringify(clientConfig(readAuthConfig(base)))).not.toMatch(/secret|"s"/i);
|
||||
expect(JSON.stringify(clientConfig(readAuthConfig(base, own)))).not.toMatch(/secret|"s"/i);
|
||||
});
|
||||
|
||||
describe('Upstream-secret (fälschbare nc_session)', ()=>{
|
||||
test('Abbruch bei oidc+naturalcrit, Standard-secret, nicht lokal', ()=>{
|
||||
expect(()=>readAuthConfig(base, { upstreamSecret: 'secret', isLocal: false })).toThrow(/Upstream-"secret".*HB_AUTH_PROVIDERS=oidc/);
|
||||
expect(()=>readAuthConfig(base, { upstreamSecret: '', isLocal: false })).toThrow(/Upstream-"secret"/);
|
||||
expect(()=>readAuthConfig(base)).toThrow(/Upstream-"secret"/);
|
||||
});
|
||||
|
||||
test('kein Abbruch: lokal, nur oidc oder eigenes secret', ()=>{
|
||||
expect(()=>readAuthConfig(base, { upstreamSecret: 'secret', isLocal: true })).not.toThrow();
|
||||
expect(()=>readAuthConfig({ ...base, HB_AUTH_PROVIDERS: 'oidc' }, { upstreamSecret: 'secret', isLocal: false })).not.toThrow();
|
||||
expect(()=>readAuthConfig(base, { upstreamSecret: 'eigenes-secret', isLocal: false })).not.toThrow();
|
||||
});
|
||||
|
||||
test('reiner Upstream-Modus bleibt unverändert', ()=>{
|
||||
expect(()=>readAuthConfig({}, { upstreamSecret: 'secret', isLocal: false })).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Issuer-Schema', ()=>{
|
||||
test('http-Issuer mit https-HB_PUBLIC_URL bricht ab', ()=>{
|
||||
expect(()=>readAuthConfig({ ...base, HB_OIDC_ISSUER: 'http://idp.example' }, own)).toThrow(/https:\/\//);
|
||||
});
|
||||
|
||||
test('http-Issuer mit http-HB_PUBLIC_URL ist erlaubt (Dev/Compose)', ()=>{
|
||||
expect(()=>readAuthConfig({ ...base, HB_OIDC_ISSUER: 'http://idp:8080', HB_PUBLIC_URL: 'http://localhost:8000' }, own)).not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -98,7 +98,8 @@ export const createAuthRouter = (config, deps = {})=>{
|
||||
res.redirect(url);
|
||||
} catch (err) {
|
||||
console.error('OIDC login:', err);
|
||||
errorPage(res, err.status ?? 502, err.message ?? 'Anmeldung nicht möglich.');
|
||||
if(err instanceof OidcCallbackError) return errorPage(res, err.status, err.message);
|
||||
errorPage(res, 500, 'Anmeldung nicht möglich.');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import { readSession, sealTx, openTx, safeRedirect, SESSION_COOKIE } from './ses
|
||||
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' };
|
||||
|
||||
|
||||
Reference in new issue
Block a user