mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 13:00:55 +00:00
🔐 feat: Enforce Required Two-Factor Authentication (#14731)
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
* feat: enforce required two-factor authentication Require local and LDAP accounts to enroll in TOTP before they can use the app when ENFORCE_TWO_FACTOR_AUTHENTICATION is set. Sign-in, refresh, bearer verification, optional share auth and image authorization all route an unenrolled account into a staged setup flow, and every bearer credential minted before enrollment or a password reset is retired. Password reset retirement uses the existing credentialsChangedAt stamp, and commitPasswordReset clears any staged enrollment in the same write. Passkey sign-in follows the same enrollment gate as password sign-in. * test: cover passkey sign-in handing off to required enrollment * refactor: keep Mongoose user types out of two-factor auth contracts * fix: withdraw a minted session when its retirement recheck fails * fix: hold Agent API keys and the reset CLI to the required two-factor policy * fix: adopt a setup token handed off after the setup screen mounted * fix: move every tab that receives an enrollment hand-off into setup * fix: read the enrollment gate's retirement cutoff in system scope * fix: clear minted session cookies before the fallible session deletion * fix: adopt a setup token persisted before the setup screen subscribed
This commit is contained in:
parent
557038bd6b
commit
92432e95d5
119 changed files with 10630 additions and 826 deletions
|
|
@ -1,88 +1,22 @@
|
|||
const cookie = require('cookie');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const { isEnabled } = require('@librechat/api');
|
||||
const { logger, runAsSystem } = require('@librechat/data-schemas');
|
||||
const { SystemRoles } = require('librechat-data-provider');
|
||||
const {
|
||||
createOptionalShareFileAuth,
|
||||
clearCloudFrontCookies,
|
||||
isTwoFactorEnrollmentRequired,
|
||||
isTokenRetired,
|
||||
isEnabled,
|
||||
} = require('@librechat/api');
|
||||
const { getUserById, findSession } = require('~/models');
|
||||
|
||||
const verifySignedUserId = (token) => {
|
||||
try {
|
||||
const payload = jwt.verify(token, process.env.JWT_REFRESH_SECRET);
|
||||
return typeof payload?.id === 'string' ? payload.id : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const getRefreshTokenUserId = async (token) => {
|
||||
const userId = verifySignedUserId(token);
|
||||
if (!userId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const session = await runAsSystem(() => findSession({ userId, refreshToken: token }));
|
||||
return session ? userId : null;
|
||||
};
|
||||
|
||||
const getOpenIdUserId = (parsed, req) => {
|
||||
if (parsed.token_provider !== 'openid' || !isEnabled(process.env.OPENID_REUSE_TOKENS)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const sessionRefreshToken = req.session?.openidTokens?.refreshToken;
|
||||
if (!parsed.refreshToken || parsed.refreshToken !== sessionRefreshToken) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return verifySignedUserId(parsed.openid_user_id);
|
||||
};
|
||||
|
||||
/**
|
||||
* Fallback auth for share file routes that are hit by `<img>`/anchor requests,
|
||||
* which can't carry the bearer access token. Resolves the viewer from the
|
||||
* `refreshToken` cookie (or an active OpenID session plus signed `openid_user_id`
|
||||
* cookie) so non-public shared links can authorize the viewer's ACL. Never
|
||||
* blocks: on any failure it leaves `req.user` unset and lets
|
||||
* `canAccessSharedLink` decide (public access, 401, or 403).
|
||||
*/
|
||||
const optionalShareFileAuth = async (req, res, next) => {
|
||||
if (req.user) {
|
||||
return next();
|
||||
}
|
||||
|
||||
try {
|
||||
const cookieHeader = req.headers.cookie;
|
||||
if (!cookieHeader) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const parsed = cookie.parse(cookieHeader);
|
||||
const userId =
|
||||
getOpenIdUserId(parsed, req) ||
|
||||
(parsed.refreshToken ? await getRefreshTokenUserId(parsed.refreshToken) : null);
|
||||
if (!userId) {
|
||||
return next();
|
||||
}
|
||||
|
||||
// Resolve in system context: this runs before canAccessSharedLink establishes
|
||||
// the share tenant, so under strict tenant isolation a tenant-scoped User
|
||||
// query would otherwise throw. The viewer's id comes from verified, active
|
||||
// cookie auth; the share's tenant-scoped ACL check still gates access.
|
||||
const user = await runAsSystem(() =>
|
||||
getUserById(userId, '-password -__v -totpSecret -backupCodes'),
|
||||
);
|
||||
if (user) {
|
||||
user.id = user._id.toString();
|
||||
if (!user.role) {
|
||||
user.role = SystemRoles.USER;
|
||||
}
|
||||
req.user = user;
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn('[optionalShareFileAuth] cookie auth failed:', error?.message);
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
module.exports = optionalShareFileAuth;
|
||||
module.exports = createOptionalShareFileAuth({
|
||||
parseCookie: cookie.parse,
|
||||
getUserById,
|
||||
findSession,
|
||||
runAsSystem,
|
||||
clearCloudFrontCookies,
|
||||
enrollmentRequired: isTwoFactorEnrollmentRequired,
|
||||
tokenRetired: isTokenRetired,
|
||||
enabled: isEnabled,
|
||||
warn: (...args) => logger.warn(...args),
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue