Security / JS Security (npm audit) (push) Failing after 15s
Lint / JS (eslint) (push) Successful in 16s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Lint / JS (eslint) (pull_request) Successful in 13s
Lint / Notify on failure (pull_request) Skipped
Test / JS Tests (jest) (push) Successful in 15s
Lint / Deploy (pull_request) Skipped
Test / JS Tests (jest) (pull_request) Successful in 17s
Security / JS Security (npm audit) (pull_request) Failing after 20s
Replace every em dash and en dash outside user-visible UI text with a colon, comma, hyphen or "to". The UI strings and the vendored web_template are handled separately. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
67 lines
2.9 KiB
JavaScript
67 lines
2.9 KiB
JavaScript
// WebSocket upgrade authentication (issue #38).
|
|
//
|
|
// Every connection is classified once, at the HTTP upgrade:
|
|
// browser: came through the trusted proxy with an Authelia identity in an allowed group;
|
|
// receives the live event feed.
|
|
// worker: presented the worker key at the handshake (Authorization: Bearer <key>).
|
|
// pending: neither, accepted only while WORKER_AUTH_ENFORCE is off so that workers
|
|
// which predate handshake auth can still identify with worker_connect.
|
|
// Gets no events and may send nothing but worker_connect.
|
|
// reject: refused with 401 before the upgrade completes.
|
|
|
|
const crypto = require('crypto');
|
|
const { ssoIdentity, ALLOWED_GROUPS } = require('./pageauth');
|
|
|
|
// Constant-time string comparison. Hashing first makes both sides the same
|
|
// length, so neither the content nor the length of the secret leaks via timing.
|
|
// An unset or empty secret never matches.
|
|
function safeEqual(given, secret) {
|
|
if (typeof given !== 'string' || typeof secret !== 'string' || !given || !secret) return false;
|
|
const a = crypto.createHash('sha256').update(given).digest();
|
|
const b = crypto.createHash('sha256').update(secret).digest();
|
|
return crypto.timingSafeEqual(a, b);
|
|
}
|
|
|
|
function checkWorkerKey(key, workerKey = process.env.WORKER_API_KEY) {
|
|
return safeEqual(key, workerKey);
|
|
}
|
|
|
|
function workerAuthEnforced(env = process.env) {
|
|
return env.WORKER_AUTH_ENFORCE === '1';
|
|
}
|
|
|
|
function bearerToken(req) {
|
|
const h = (req.headers && req.headers.authorization) || '';
|
|
const m = /^Bearer\s+(.+)$/i.exec(h);
|
|
return m ? m[1].trim() : null;
|
|
}
|
|
|
|
function classifyUpgrade(req, opts = {}) {
|
|
const workerKey = 'workerKey' in opts ? opts.workerKey : process.env.WORKER_API_KEY;
|
|
const enforce = 'enforce' in opts ? opts.enforce : workerAuthEnforced();
|
|
|
|
const token = bearerToken(req);
|
|
if (token !== null) {
|
|
// A wrong key is never downgraded to pending: it is either a misconfigured
|
|
// worker or an attacker, and both should see the refusal.
|
|
return checkWorkerKey(token, workerKey) ? 'worker' : 'reject';
|
|
}
|
|
|
|
const identity = opts.trusted ? ssoIdentity(req, opts.trusted) : ssoIdentity(req);
|
|
if (identity && identity.groups.some(g => ALLOWED_GROUPS.includes(g))) return 'browser';
|
|
|
|
return enforce ? 'reject' : 'pending';
|
|
}
|
|
|
|
// May this connection register as a worker with this worker_connect message?
|
|
function allowWorkerConnect(kind, apiKey, opts = {}) {
|
|
const workerKey = 'workerKey' in opts ? opts.workerKey : process.env.WORKER_API_KEY;
|
|
const enforce = 'enforce' in opts ? opts.enforce : workerAuthEnforced();
|
|
if (kind === 'worker') return { ok: true };
|
|
if (checkWorkerKey(apiKey, workerKey)) return { ok: true };
|
|
if (kind === 'pending' && !enforce && !apiKey) return { ok: true, legacy: true };
|
|
return { ok: false };
|
|
}
|
|
|
|
module.exports = { safeEqual, checkWorkerKey, workerAuthEnforced, bearerToken, classifyUpgrade, allowWorkerConnect };
|