Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions settings.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,14 @@

"sso": {
"issuer": "${SSO_ISSUER:http://localhost:9001}",
/*
* Optional signing keys for the embedded OIDC provider's cookies. When
* omitted, Etherpad derives a secret key from the persisted session
* secret (SESSIONKEY.txt), which is stable across restarts and shared
* across horizontally-scaled pods. Set an explicit ordered array to
* rotate: the first key signs, the rest are still accepted for verify.
* "cookieKeys": ["${OIDC_COOKIE_KEY:}"],
*/
Comment on lines +970 to +975
"clients": [
Comment thread
qodo-free-for-open-source-projects[bot] marked this conversation as resolved.
Resolved
Comment on lines +969 to 977
{
"client_id": "${ADMIN_CLIENT:admin_client}",
Expand Down
31 changes: 24 additions & 7 deletions src/node/security/OAuth2Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {format} from 'url'
import {ParsedUrlQuery} from "node:querystring";
import {MapArrayType} from "../types/MapType";
import crypto from "node:crypto";
import {resolveOidcCookieKeys, isOriginAllowedForOidcClient} from "./OidcProviderSecurity";

// Small fixed delay applied to every failed interactive login, mirroring
// webaccess.authnFailureDelayMs, so failures take a consistent amount of time.
Expand Down Expand Up @@ -68,9 +69,13 @@ const configuration: Configuration = {
profile: ['name'],
admin: ['admin']
},
cookies: {
keys: ['oidc'],
},
// NOTE: cookies.keys is deliberately NOT set here. A committed literal key
// (historically ['oidc']) lets anyone with the public source forge valid
// OIDC provider `.sig` cookies. The real key material is resolved at
// provider-construction time in expressCreateServer() via
// resolveOidcCookieKeys(), which prefers an operator-supplied
// settings.sso.cookieKeys and otherwise derives a secret, stable key from
// the persisted session secret. See OidcProviderSecurity.ts.
Comment on lines +80 to +83
features:{
devInteractions: {enabled: false},
},
Expand All @@ -93,7 +98,16 @@ export const expressCreateServer = async (hookName: string, args: ArgsExpressTyp
privateKeyExported = privateKey

const oidc = new Provider(settings.sso.issuer, {
...configuration, jwks: {
...configuration,
cookies: {
// Secret, operator/deployment-stable signing keys — never a
// committed literal. See resolveOidcCookieKeys().
keys: resolveOidcCookieKeys({
cookieKeys: (settings.sso as {cookieKeys?: unknown}).cookieKeys,
sessionKey: settings.sessionKey,
}),
},
Comment thread
qodo-free-for-open-source-projects[bot] marked this conversation as resolved.
Resolved
jwks: {
keys: [
privateKeyJWK
],
Expand Down Expand Up @@ -127,9 +141,12 @@ export const expressCreateServer = async (hookName: string, args: ArgsExpressTyp
},
jwtResponseModes: {enabled: true},
},
clientBasedCORS: (ctx, origin, client) => {
return true
},
clientBasedCORS: (ctx, origin, client) =>
// Only allow cross-origin reads from an origin registered as one of
// the client's redirect URIs. Returning `true` unconditionally
// reflected any Origin into Access-Control-Allow-Origin. See
// isOriginAllowedForOidcClient().
isOriginAllowedForOidcClient(origin, client as {redirectUris?: unknown}),
extraParams: [],
extraTokenClaims: async (ctx, token) => {
if(token.kind === 'AccessToken') {
Expand Down
84 changes: 84 additions & 0 deletions src/node/security/OidcProviderSecurity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import crypto from 'node:crypto';

/**
* Security helpers for the embedded OIDC provider (`OAuth2Provider.ts`).
*
* Kept in a dependency-light module (only `node:crypto`) so the pure decisions
* can be unit-tested without constructing an `oidc-provider` instance.
*/

// Domain-separation label so the derived cookie key is cryptographically
// unrelated to any other use of the session secret. The trailing NUL keeps the
// label from ambiguously running into the appended secret.
export const COOKIE_KEY_DERIVATION_LABEL = 'etherpad-oidc-provider-cookie-signing-key\0';

/**
* Resolve the cookie-signing keys for the embedded OIDC provider.
*
* oidc-provider signs its short-lived interaction/session/grant cookies
* (`_interaction`, `_interaction_resume`, `_grant`, `_session`) with an HMAC
* keyed by these values. Shipping a hardcoded key (historically `['oidc']`) let
* anyone with the public source forge valid `.sig` cookies, defeating the
* provider's cookie-integrity guarantee. Reported by `meifukun`.
*
* Resolution order:
* 1. An explicit operator-supplied `settings.sso.cookieKeys` array — use this
* for controlled rotation: `[newKey, ...oldKeys]`.
* 2. A value derived from the persisted Etherpad session secret
* (`SESSIONKEY.txt`) via a domain-separated SHA-256. Stable across restarts
* and identical across horizontally-scaled pods (which share the session
* key), so an in-flight OIDC interaction survives a restart and can land on
* any pod — without ever committing key material to source.
* 3. As a last resort (no session key available), an ephemeral per-process
* random key. The integrity boundary holds, but interactions won't survive
* a restart or span multiple pods.
*/
export const resolveOidcCookieKeys = (
opts: {cookieKeys?: unknown, sessionKey?: string | null},
): string[] => {
const {cookieKeys, sessionKey} = opts;

if (Array.isArray(cookieKeys)) {
const usable = cookieKeys.filter((k): k is string => typeof k === 'string' && k.length > 0);
if (usable.length > 0) return usable;
}

if (typeof sessionKey === 'string' && sessionKey.length > 0) {
const derived = crypto.createHash('sha256')
.update(COOKIE_KEY_DERIVATION_LABEL)
.update(sessionKey)
.digest('hex');
return [derived];
}

return [crypto.randomBytes(32).toString('hex')];
};

/**
* Origin allow-list decision for the embedded OIDC provider's CORS-enabled
* endpoints (`/oidc/token`, `/oidc/me`, ...). oidc-provider invokes
* `clientBasedCORS(ctx, origin, client)` for every cross-origin request;
* returning `true` unconditionally (the historical behavior) reflected ANY
* `Origin` into `Access-Control-Allow-Origin`, letting unregistered origins read
* token/userinfo responses that use non-cookie credentials (Authorization
* headers, POST-body client credentials). Reported by `meifukun`.
*
* An origin is allowed only when it exactly matches the origin (scheme + host +
* port) of one of the client's registered redirect URIs.
*/
export const isOriginAllowedForOidcClient = (
origin: string | undefined | null,
client: {redirectUris?: unknown} | undefined | null,
): boolean => {
if (!origin || !client) return false;
const uris = (client as {redirectUris?: unknown}).redirectUris;
if (!Array.isArray(uris)) return false;
return uris.some((uri: unknown) => {
if (typeof uri !== 'string') return false;
try {
return new URL(uri).origin === origin;
} catch {
return false;
}
});
};
4 changes: 4 additions & 0 deletions src/node/utils/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ export type SettingsType = {
sso: {
issuer: string,
clients?: {client_id: string}[]
// Optional operator-supplied signing keys for the embedded OIDC provider's
// cookies. When unset, a secret key is derived from the session secret.
// Provide an ordered array `[newKey, ...oldKeys]` to rotate.
cookieKeys?: string[]
},
Comment on lines 304 to 311
showSettingsInAdminPage: boolean,
cleanup: {
Expand Down
98 changes: 98 additions & 0 deletions src/tests/backend/specs/OidcProviderSecurity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
'use strict';

/**
* Unit coverage for the embedded OIDC provider's cookie-signing key derivation
* and CORS origin allow-list. Both were reported by `meifukun`:
* - the provider historically signed its cookies with the hardcoded key
* `['oidc']`, so anyone with the public source could forge valid `.sig`
* cookies;
* - `clientBasedCORS` returned `true` for every origin, reflecting arbitrary
* `Origin` values into `Access-Control-Allow-Origin`.
*/

const assert = require('assert').strict;
import {
resolveOidcCookieKeys,
isOriginAllowedForOidcClient,
} from '../../../node/security/OidcProviderSecurity';

describe(__filename, function () {
describe('resolveOidcCookieKeys', function () {
it('never returns the historical hardcoded key', function () {
const keys = resolveOidcCookieKeys({sessionKey: 'a-persisted-session-secret'});
assert.ok(!keys.includes('oidc'));
});

it('uses operator-supplied cookieKeys when provided', function () {
const keys = resolveOidcCookieKeys({cookieKeys: ['k1', 'k2'], sessionKey: 'x'});
assert.deepEqual(keys, ['k1', 'k2']);
});

it('ignores empty/invalid entries in cookieKeys and falls through', function () {
const keys = resolveOidcCookieKeys({cookieKeys: ['', null as any], sessionKey: 'secret'});
assert.equal(keys.length, 1);
assert.notEqual(keys[0], 'oidc');
assert.ok(keys[0].length >= 32);
});

it('derives a stable key from the session secret (survives restart/multi-pod)', function () {
const a = resolveOidcCookieKeys({sessionKey: 'secret'});
const b = resolveOidcCookieKeys({sessionKey: 'secret'});
assert.deepEqual(a, b);
});

it('derives different keys for different session secrets', function () {
const a = resolveOidcCookieKeys({sessionKey: 'secret-a'});
const b = resolveOidcCookieKeys({sessionKey: 'secret-b'});
assert.notDeepEqual(a, b);
});

it('does not reuse the raw session secret as the cookie key', function () {
const keys = resolveOidcCookieKeys({sessionKey: 'secret'});
assert.ok(!keys.includes('secret'));
});

it('falls back to a fresh random key when no session secret exists', function () {
const a = resolveOidcCookieKeys({sessionKey: null});
const b = resolveOidcCookieKeys({sessionKey: null});
assert.equal(a.length, 1);
assert.notEqual(a[0], 'oidc');
assert.ok(a[0].length >= 32);
assert.notDeepEqual(a, b); // random => different each call
});
});

describe('isOriginAllowedForOidcClient', function () {
const client = {
redirectUris: ['https://app.example.com/admin/', 'https://app.example.com/'],
};

it('allows an origin matching a registered redirect URI', function () {
assert.equal(isOriginAllowedForOidcClient('https://app.example.com', client), true);
});

it('rejects an unregistered attacker origin', function () {
assert.equal(isOriginAllowedForOidcClient('https://evil.attacker.com', client), false);
});

it('rejects a look-alike suffix origin (no substring matching)', function () {
assert.equal(isOriginAllowedForOidcClient('https://app.example.com.evil.com', client), false);
});

it('rejects a scheme mismatch (http vs https)', function () {
assert.equal(isOriginAllowedForOidcClient('http://app.example.com', client), false);
});

it('rejects when origin is missing', function () {
assert.equal(isOriginAllowedForOidcClient(undefined, client), false);
});

it('rejects when client is missing', function () {
assert.equal(isOriginAllowedForOidcClient('https://app.example.com', null), false);
});

it('rejects when client has no redirect URIs', function () {
assert.equal(isOriginAllowedForOidcClient('https://app.example.com', {}), false);
});
});
});
Loading