mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
#5174: an OIDC login against a provider that rejected the scopes failed with "Error in OAuth Server: Cannot read property 'ocs' of null", which says nothing about the refusal that caused it. getTokenContent() returns NULL for a token it cannot parse, and the ADFS/B2C branch assigns that straight into userinfo. The next line was the Nextcloud hack, `if (userinfo.ocs)`, so the first thing to touch the failed response was a property read on null. Every claim read after it had the same problem - that line was only first by accident of ordering, which is why the fix is not one null check but an order: the provider's answer is validated once, as a whole, before anything reads a field off it. Checked, in this order, each naming what failed: * the token response is an object at all; * it carries an access_token or an id_token - a 200 with neither is the provider answering with a complaint, so the error lists the fields that DID come back (keys only: the values are secrets, and JSON.stringify of a token response in a log is how refresh tokens leak); * userinfo is a non-null object, saying WHICH path produced nothing (claims expected in the access token, or the userinfo endpoint) and pointing at OAUTH2_REQUEST_PERMISSIONS, which is what was actually wrong in #5174; * the ocs and metadata hacks unwrap to something - `ocs` present with no `ocs.data` used to set userinfo to undefined and fail on the next line; * B2C's userinfo["emails"][0] is an array before it is indexed: a tenant that does not release the claim made that undefined[0]; * expires_in parses to a finite number - parseInt(undefined) is NaN, and NaN propagated into the account's expiry silently. Falls back to the OAuth2 default hour. tests/oidcNullUserinfo.test.cjs pins the order rather than the individual checks: it asserts each guard sits BEFORE the read it protects, since an edit that reads a claim earlier would restore the original bug without touching a check. It also pins that the no-token error names keys and never stringifies the response. All eight assertions fail on the previous commit. Not reproduced against a real provider - this sandbox has none. The null path is read from the source: getTokenContent returns null for a falsy token, and the ADFS/B2C branch passes its result to the line that threw. Thanks to Esther125 and xet7 ! Fixes #5174,
113 lines
5.5 KiB
JavaScript
113 lines
5.5 KiB
JavaScript
'use strict';
|
|
|
|
// #5174: an OIDC login against a provider that refused the request died with
|
|
//
|
|
// Error in OAuth Server: Cannot read property 'ocs' of null
|
|
//
|
|
// which tells the admin nothing about the refusal - the reporter's actual
|
|
// problem was a scope the provider did not allow.
|
|
//
|
|
// Where the null came from: getTokenContent() returns NULL for a token it cannot
|
|
// parse (`var content = null; if (token) {...} return content`), and the
|
|
// ADFS/B2C branch assigns its result straight into `userinfo`. The next line was
|
|
// the Nextcloud hack, `if (userinfo.ocs)`, so the first thing to touch the failed
|
|
// response was a property read on null. Every claim read after it had the same
|
|
// problem; that line just happened to be first.
|
|
//
|
|
// The fix is not one null check but the ORDER: the provider's answer is
|
|
// validated once, as a whole, before anything reads a field off it, and each
|
|
// failure says which step failed and what the provider sent. This pins that
|
|
// order, because a later edit that reads a claim earlier would restore the
|
|
// original bug without touching the checks.
|
|
//
|
|
// Run: node tests/oidcNullUserinfo.test.cjs
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const ROOT = path.join(__dirname, '..');
|
|
const src = fs.readFileSync(path.join(ROOT, 'packages/wekan-oidc/oidc_server.js'), 'utf8');
|
|
|
|
// Comments quote the old broken line on purpose; assertions are about code.
|
|
const code = src.replace(/\/\*[\s\S]*?\*\//g, '').replace(/^\s*\/\/.*$/gm, '');
|
|
|
|
let passed = 0;
|
|
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
|
|
|
|
console.log('oidcNullUserinfo:');
|
|
|
|
test('getTokenContent can still return null - that is the premise', () => {
|
|
// Not a bug in itself: an unparseable token has no content. The bug was
|
|
// handing that null to code that assumed an object. If this ever stops
|
|
// returning null the guards below are harmless, but the comment above would be
|
|
// describing history.
|
|
const at = code.indexOf('var getTokenContent = function');
|
|
assert.notStrictEqual(at, -1, 'getTokenContent must be there');
|
|
const fn = code.slice(at, code.indexOf('\n}', at));
|
|
assert.ok(/var content = null/.test(fn) && /return content/.test(fn),
|
|
'it returns null when the token cannot be parsed');
|
|
});
|
|
|
|
test('the token response is checked before any field is read off it', () => {
|
|
const tokenCheck = code.indexOf('the token endpoint returned no usable response');
|
|
const firstRead = code.indexOf('token.access_token');
|
|
assert.notStrictEqual(tokenCheck, -1, 'a missing/!object token response must be refused');
|
|
assert.ok(tokenCheck < firstRead,
|
|
'the check has to come BEFORE the first read, or the read throws first');
|
|
});
|
|
|
|
test('a token response with no token in it fails with what the provider sent', () => {
|
|
assert.ok(/neither access_token nor/.test(code),
|
|
'a 200 carrying neither token must be refused explicitly');
|
|
assert.ok(/Object\.keys\(token\)/.test(code),
|
|
'and name the fields that DID come back - keys only, since the values are secrets');
|
|
assert.ok(!/JSON\.stringify\(token\)/.test(code),
|
|
'never dump the whole token response into an error message');
|
|
});
|
|
|
|
test('userinfo is validated before any claim is read', () => {
|
|
const check = code.indexOf('no user information came back from the provider');
|
|
assert.notStrictEqual(check, -1, 'a null/non-object userinfo must be refused');
|
|
// The claim reads: the Nextcloud hack first, then the OAUTH2_*_MAP lookups.
|
|
const ocs = code.indexOf('userinfo.ocs');
|
|
const firstClaim = code.indexOf('userinfo[process.env.OAUTH2_ID_MAP]');
|
|
assert.ok(check < ocs && check < firstClaim,
|
|
'the check must precede BOTH the ocs hack and the claim lookups - the ocs '
|
|
+ 'line was only first by accident of ordering');
|
|
// And it must say which branch produced it: the claims-in-access-token path
|
|
// and the userinfo-endpoint path fail for different reasons.
|
|
assert.ok(/claimsInAccessToken/.test(code.slice(check, check + 600)),
|
|
'the message distinguishes the two paths, or it just moves the mystery');
|
|
assert.ok(/OAUTH2_REQUEST_PERMISSIONS/.test(code.slice(check, check + 700)),
|
|
'and points at the scopes, which is what was actually wrong in #5174');
|
|
});
|
|
|
|
test('the ocs and metadata hacks cannot produce an undefined userinfo', () => {
|
|
// Both re-point userinfo at a nested object. `ocs` present but `ocs.data`
|
|
// missing used to set userinfo to undefined and fail on the NEXT line.
|
|
assert.ok(/if \(userinfo\.ocs && userinfo\.ocs\.data\)/.test(code),
|
|
'the Nextcloud hack must check the nested object it unwraps to');
|
|
const hacks = code.indexOf('userinfo.ocs');
|
|
const after = code.slice(hacks, hacks + 800);
|
|
assert.ok(/unwrapped to nothing usable/.test(after),
|
|
'and an unwrap that lands on nothing must be refused right after');
|
|
});
|
|
|
|
test('the B2C email claim is checked before it is indexed', () => {
|
|
const at = code.indexOf('OAUTH2_B2C_ENABLED');
|
|
const branch = code.slice(code.indexOf('OAUTH2_B2C_ENABLED', at + 10));
|
|
assert.ok(/Array\.isArray\(userinfo\.emails\)/.test(branch),
|
|
'userinfo["emails"][0] on a token without the claim is undefined[0]');
|
|
assert.ok(/no "emails" claim/.test(branch),
|
|
'and the failure must say the tenant is not releasing it');
|
|
});
|
|
|
|
test('a missing expires_in does not become NaN', () => {
|
|
assert.ok(/Number\.isFinite\(expiresIn\)/.test(code),
|
|
'parseInt(undefined) is NaN, and NaN in expiresAt propagates silently');
|
|
assert.ok(/expiresIn = 3600/.test(code), 'with the OAuth2 default hour as the fallback');
|
|
});
|
|
|
|
console.log(`\n${passed} tests passed`);
|