更新
This commit is contained in:
@@ -7,9 +7,10 @@ import vm from 'node:vm';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const ts = require(process.env.KEFU_SSO_TYPESCRIPT || 'typescript');
|
||||
const hashWasm = require(process.env.KEFU_SSO_HASH_WASM || 'hash-wasm');
|
||||
|
||||
function loadTs(relative, dependencies = {}) {
|
||||
dependencies = { '#/locales/t': { t: key => key }, ...dependencies };
|
||||
dependencies = { '#/locales/t': { t: key => key }, 'hash-wasm': hashWasm, ...dependencies };
|
||||
const source = fs.readFileSync(new URL(relative, import.meta.url), 'utf8');
|
||||
const result = ts.transpileModule(source, { compilerOptions: { target: ts.ScriptTarget.ES2022,
|
||||
module: ts.ModuleKind.CommonJS, esModuleInterop: true }, reportDiagnostics: true });
|
||||
@@ -26,6 +27,10 @@ const config = { enabled: true, client_id: 'kefu', entry: 'http://127.0.0.1:5173
|
||||
authorize_url: 'http://127.0.0.1:5173/sso/authorize',
|
||||
redirect_uri: 'http://127.0.0.1:5666/auth/group-login' };
|
||||
const origin = 'http://127.0.0.1:5666';
|
||||
const publicOrigin = 'http://im.zhenyangtang.cn:8369';
|
||||
const publicConfig = { enabled: true, client_id: 'kefu', entry: 'https://x.zhenyangtang.com.cn',
|
||||
authorize_url: 'https://x.zhenyangtang.com.cn/sso/authorize',
|
||||
redirect_uri: `${publicOrigin}/auth/group-login`, http_callback_allowed: true };
|
||||
const now = 1_000_000;
|
||||
const code = 'C'.repeat(43);
|
||||
|
||||
@@ -60,7 +65,9 @@ test('authorization uses random state, S256 PKCE and a fixed client/callback', a
|
||||
assert.match(pending.state, /^[A-Za-z0-9_-]{43}$/);
|
||||
assert.match(pending.verifier, /^[A-Za-z0-9_-]{43}$/);
|
||||
assert.notEqual(pending.state, pending.verifier);
|
||||
assert.deepEqual(Object.keys(pending).sort(), ['created', 'state', 'verifier']);
|
||||
assert.deepEqual(Object.keys(pending).sort(), ['callback', 'created', 'httpCallbackAllowed', 'state', 'verifier']);
|
||||
assert.equal(pending.callback, config.redirect_uri);
|
||||
assert.equal(pending.httpCallbackAllowed, false);
|
||||
assert.equal(target.origin, 'http://127.0.0.1:5173');
|
||||
assert.equal(target.pathname, '/sso/authorize');
|
||||
assert.deepEqual([...target.searchParams.keys()].sort(),
|
||||
@@ -106,6 +113,79 @@ test('entry/callback validation rejects insecure and attacker controlled configu
|
||||
}
|
||||
});
|
||||
|
||||
test('approved HTTP callback keeps its exact port and cannot approve HTTP Group URLs', async () => {
|
||||
assert.equal(sso.groupLoginEntry(publicConfig, publicOrigin), '/auth/group-login');
|
||||
assert.equal(sso.groupLoginEntry(publicConfig, 'https://other.example'), publicConfig.redirect_uri);
|
||||
assert.equal(sso.validateGroupConfig(publicConfig, publicOrigin).redirect_uri, publicConfig.redirect_uri);
|
||||
for (const changedOrigin of ['http://im.zhenyangtang.cn', 'http://im.zhenyangtang.cn:8370',
|
||||
'http://im.zhenyangtang.cn.evil:8369']) {
|
||||
assert.throws(() => sso.validateGroupConfig(publicConfig, changedOrigin));
|
||||
assert.throws(() => sso.groupLoginEntry(publicConfig, changedOrigin));
|
||||
await assert.rejects(sso.beginGroupLogin(publicConfig, changedOrigin, storage(), webcrypto, now));
|
||||
}
|
||||
for (const http_callback_allowed of [false, undefined, 'true', 1]) {
|
||||
assert.throws(() => sso.validateGroupConfig({ ...publicConfig, http_callback_allowed }, publicOrigin));
|
||||
assert.throws(() => sso.groupLoginEntry({ ...publicConfig, http_callback_allowed }, publicOrigin));
|
||||
}
|
||||
for (const changed of [
|
||||
{ redirect_uri: `http://user@im.zhenyangtang.cn:8369/auth/group-login` },
|
||||
{ redirect_uri: `http://@im.zhenyangtang.cn:8369/auth/group-login` },
|
||||
{ redirect_uri: `${publicOrigin}/other` },
|
||||
{ entry: 'http://x.zhenyangtang.com.cn', authorize_url: 'http://x.zhenyangtang.com.cn/sso/authorize' },
|
||||
{ authorize_url: 'http://x.zhenyangtang.com.cn/sso/authorize' },
|
||||
]) {
|
||||
assert.throws(() => sso.validateGroupConfig({ ...publicConfig, ...changed }, publicOrigin));
|
||||
assert.throws(() => sso.groupLoginEntry({ ...publicConfig, ...changed }, publicOrigin));
|
||||
}
|
||||
});
|
||||
|
||||
test('HTTP without subtle uses real hash-wasm for the known S256 vector and a port-bound callback', async () => {
|
||||
const verifier = 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk';
|
||||
let randomCalls = 0;
|
||||
const httpCrypto = { getRandomValues(bytes) {
|
||||
assert.equal(bytes.length, 32);
|
||||
bytes.set(++randomCalls === 2 ? Buffer.from(verifier, 'base64url') : new Uint8Array(32).fill(17));
|
||||
return bytes;
|
||||
} };
|
||||
const state = storage();
|
||||
const target = new URL(await sso.beginGroupLogin(publicConfig, publicOrigin, state, httpCrypto, now));
|
||||
assert.equal(randomCalls, 2);
|
||||
assert.equal(target.origin, publicConfig.entry);
|
||||
assert.equal(target.searchParams.get('redirect_uri'), `${publicOrigin}/auth/group-login`);
|
||||
assert.equal(target.searchParams.get('code_challenge_method'), 'S256');
|
||||
assert.equal(target.searchParams.get('code_challenge'), 'E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM');
|
||||
assert.notEqual(target.searchParams.get('code_challenge'), verifier);
|
||||
const pending = JSON.parse(state.getItem(sso.GROUP_TRANSACTION_KEY));
|
||||
assert.equal(pending.verifier, verifier);
|
||||
assert.equal(pending.callback, publicConfig.redirect_uri);
|
||||
assert.equal(pending.httpCallbackAllowed, true);
|
||||
const href = `${publicConfig.redirect_uri}?code=${code}&state=${pending.state}`;
|
||||
assert.equal(sso.consumeGroupCallback(href, state, now).code_verifier, verifier);
|
||||
assert.equal(state.values.size, 0);
|
||||
});
|
||||
|
||||
test('HTTP crypto keeps secure randomness and rejects callbacks from another origin or path', async () => {
|
||||
const httpCrypto = { getRandomValues: webcrypto.getRandomValues.bind(webcrypto) };
|
||||
for (const prefix of ['http://im.zhenyangtang.cn/auth/group-login',
|
||||
'http://im.zhenyangtang.cn:8370/auth/group-login', 'https://im.zhenyangtang.cn:8369/auth/group-login',
|
||||
'http://im.zhenyangtang.cn.evil:8369/auth/group-login', `${publicOrigin}/other`,
|
||||
'http://user@im.zhenyangtang.cn:8369/auth/group-login',
|
||||
'http://@im.zhenyangtang.cn:8369/auth/group-login']) {
|
||||
const state = storage();
|
||||
const target = new URL(await sso.beginGroupLogin(publicConfig, publicOrigin, state, httpCrypto, now));
|
||||
const pending = JSON.parse(state.getItem(sso.GROUP_TRANSACTION_KEY));
|
||||
assert.notEqual(pending.state, pending.verifier);
|
||||
const digest = Buffer.from(await webcrypto.subtle.digest('SHA-256', new TextEncoder().encode(pending.verifier))).toString('base64url');
|
||||
assert.equal(target.searchParams.get('code_challenge'), digest);
|
||||
assert.throws(() => sso.consumeGroupCallback(`${prefix}?code=${code}&state=${pending.state}`, state, now));
|
||||
assert.equal(state.values.size, 0);
|
||||
}
|
||||
const state = storage();
|
||||
state.setItem(sso.GROUP_TRANSACTION_KEY, 'previous');
|
||||
await assert.rejects(sso.beginGroupLogin(publicConfig, publicOrigin, state, { subtle: webcrypto.subtle }, now));
|
||||
assert.equal(state.values.size, 0);
|
||||
});
|
||||
|
||||
test('starting another login clears the old transaction', async () => {
|
||||
const { state, pending } = await transaction();
|
||||
await sso.beginGroupLogin(config, origin, state, webcrypto, now);
|
||||
|
||||
Reference in New Issue
Block a user