mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-08 18:28:12 +00:00
Merge pull request #4453 from Infisical/lockout-lock-fix
Lockout lock fix
This commit is contained in:
@@ -37,7 +37,7 @@
|
|||||||
"build": "tsup --sourcemap",
|
"build": "tsup --sourcemap",
|
||||||
"build:frontend": "npm run build --prefix ../frontend",
|
"build:frontend": "npm run build --prefix ../frontend",
|
||||||
"start": "node --enable-source-maps dist/main.mjs",
|
"start": "node --enable-source-maps dist/main.mjs",
|
||||||
"type:check": "tsc --noEmit",
|
"type:check": "node --max-old-space-size=8192 ./node_modules/.bin/tsc --noEmit",
|
||||||
"lint:fix": "node --max-old-space-size=8192 ./node_modules/.bin/eslint --fix --ext js,ts ./src",
|
"lint:fix": "node --max-old-space-size=8192 ./node_modules/.bin/eslint --fix --ext js,ts ./src",
|
||||||
"lint": "node --max-old-space-size=8192 ./node_modules/.bin/eslint 'src/**/*.ts'",
|
"lint": "node --max-old-space-size=8192 ./node_modules/.bin/eslint 'src/**/*.ts'",
|
||||||
"test:unit": "vitest run -c vitest.unit.config.ts",
|
"test:unit": "vitest run -c vitest.unit.config.ts",
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ export const KeyStorePrefixes = {
|
|||||||
SecretRotationLock: (rotationId: string) => `secret-rotation-v2-mutex-${rotationId}` as const,
|
SecretRotationLock: (rotationId: string) => `secret-rotation-v2-mutex-${rotationId}` as const,
|
||||||
SecretScanningLock: (dataSourceId: string, resourceExternalId: string) =>
|
SecretScanningLock: (dataSourceId: string, resourceExternalId: string) =>
|
||||||
`secret-scanning-v2-mutex-${dataSourceId}-${resourceExternalId}` as const,
|
`secret-scanning-v2-mutex-${dataSourceId}-${resourceExternalId}` as const,
|
||||||
|
IdentityLockoutLock: (lockoutKey: string) => `identity-lockout-lock-${lockoutKey}` as const,
|
||||||
CaOrderCertificateForSubscriberLock: (subscriberId: string) =>
|
CaOrderCertificateForSubscriberLock: (subscriberId: string) =>
|
||||||
`ca-order-certificate-for-subscriber-lock-${subscriberId}` as const,
|
`ca-order-certificate-for-subscriber-lock-${subscriberId}` as const,
|
||||||
SecretSyncLastRunTimestamp: (syncId: string) => `secret-sync-last-run-${syncId}` as const,
|
SecretSyncLastRunTimestamp: (syncId: string) => `secret-sync-last-run-${syncId}` as const,
|
||||||
|
|||||||
@@ -8,11 +8,18 @@ import {
|
|||||||
validatePrivilegeChangeOperation
|
validatePrivilegeChangeOperation
|
||||||
} from "@app/ee/services/permission/permission-fns";
|
} from "@app/ee/services/permission/permission-fns";
|
||||||
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
|
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
|
||||||
import { TKeyStoreFactory } from "@app/keystore/keystore";
|
import { KeyStorePrefixes, TKeyStoreFactory } from "@app/keystore/keystore";
|
||||||
import { getConfig } from "@app/lib/config/env";
|
import { getConfig } from "@app/lib/config/env";
|
||||||
import { crypto } from "@app/lib/crypto/cryptography";
|
import { crypto } from "@app/lib/crypto/cryptography";
|
||||||
import { BadRequestError, NotFoundError, PermissionBoundaryError, UnauthorizedError } from "@app/lib/errors";
|
import {
|
||||||
|
BadRequestError,
|
||||||
|
NotFoundError,
|
||||||
|
PermissionBoundaryError,
|
||||||
|
RateLimitError,
|
||||||
|
UnauthorizedError
|
||||||
|
} from "@app/lib/errors";
|
||||||
import { checkIPAgainstBlocklist, extractIPDetails, isValidIpOrCidr, TIp } from "@app/lib/ip";
|
import { checkIPAgainstBlocklist, extractIPDetails, isValidIpOrCidr, TIp } from "@app/lib/ip";
|
||||||
|
import { logger } from "@app/lib/logger";
|
||||||
|
|
||||||
import { ActorType, AuthTokenType } from "../auth/auth-type";
|
import { ActorType, AuthTokenType } from "../auth/auth-type";
|
||||||
import { TIdentityOrgDALFactory } from "../identity/identity-org-dal";
|
import { TIdentityOrgDALFactory } from "../identity/identity-org-dal";
|
||||||
@@ -40,15 +47,18 @@ type TIdentityUaServiceFactoryDep = {
|
|||||||
identityOrgMembershipDAL: TIdentityOrgDALFactory;
|
identityOrgMembershipDAL: TIdentityOrgDALFactory;
|
||||||
permissionService: Pick<TPermissionServiceFactory, "getOrgPermission">;
|
permissionService: Pick<TPermissionServiceFactory, "getOrgPermission">;
|
||||||
licenseService: Pick<TLicenseServiceFactory, "getPlan">;
|
licenseService: Pick<TLicenseServiceFactory, "getPlan">;
|
||||||
keyStore: Pick<TKeyStoreFactory, "setItemWithExpiry" | "getItem" | "deleteItem" | "getKeysByPattern" | "deleteItems">;
|
keyStore: Pick<
|
||||||
|
TKeyStoreFactory,
|
||||||
|
"setItemWithExpiry" | "getItem" | "deleteItem" | "getKeysByPattern" | "deleteItems" | "acquireLock"
|
||||||
|
>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TIdentityUaServiceFactory = ReturnType<typeof identityUaServiceFactory>;
|
export type TIdentityUaServiceFactory = ReturnType<typeof identityUaServiceFactory>;
|
||||||
|
|
||||||
// type LockoutObject = {
|
type LockoutObject = {
|
||||||
// lockedOut: boolean;
|
lockedOut: boolean;
|
||||||
// failedAttempts: number;
|
failedAttempts: number;
|
||||||
// };
|
};
|
||||||
|
|
||||||
export const identityUaServiceFactory = ({
|
export const identityUaServiceFactory = ({
|
||||||
identityUaDAL,
|
identityUaDAL,
|
||||||
@@ -62,15 +72,8 @@ export const identityUaServiceFactory = ({
|
|||||||
const login = async (clientId: string, clientSecret: string, ip: string) => {
|
const login = async (clientId: string, clientSecret: string, ip: string) => {
|
||||||
const identityUa = await identityUaDAL.findOne({ clientId });
|
const identityUa = await identityUaDAL.findOne({ clientId });
|
||||||
if (!identityUa) {
|
if (!identityUa) {
|
||||||
throw new NotFoundError({
|
throw new UnauthorizedError({
|
||||||
message: "No identity with specified client ID was found"
|
message: "Invalid credentials"
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const identityMembershipOrg = await identityOrgMembershipDAL.findOne({ identityId: identityUa.identityId });
|
|
||||||
if (!identityMembershipOrg) {
|
|
||||||
throw new NotFoundError({
|
|
||||||
message: "No identity with the org membership was found"
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,119 +81,184 @@ export const identityUaServiceFactory = ({
|
|||||||
ipAddress: ip,
|
ipAddress: ip,
|
||||||
trustedIps: identityUa.clientSecretTrustedIps as TIp[]
|
trustedIps: identityUa.clientSecretTrustedIps as TIp[]
|
||||||
});
|
});
|
||||||
const clientSecretPrefix = clientSecret.slice(0, 4);
|
|
||||||
const clientSecrtInfo = await identityUaClientSecretDAL.find({
|
|
||||||
identityUAId: identityUa.id,
|
|
||||||
isClientSecretRevoked: false,
|
|
||||||
clientSecretPrefix
|
|
||||||
});
|
|
||||||
|
|
||||||
let validClientSecretInfo: (typeof clientSecrtInfo)[0] | null = null;
|
const LOCKOUT_KEY = `lockout:identity:${identityUa.identityId}:${IdentityAuthMethod.UNIVERSAL_AUTH}:${clientId}`;
|
||||||
for await (const info of clientSecrtInfo) {
|
|
||||||
const isMatch = await crypto.hashing().compareHash(clientSecret, info.clientSecretHash);
|
|
||||||
|
|
||||||
if (isMatch) {
|
let lock: Awaited<ReturnType<typeof keyStore.acquireLock>>;
|
||||||
validClientSecretInfo = info;
|
try {
|
||||||
break;
|
lock = await keyStore.acquireLock([KeyStorePrefixes.IdentityLockoutLock(LOCKOUT_KEY)], 500, {
|
||||||
}
|
retryCount: 3,
|
||||||
|
retryDelay: 300,
|
||||||
|
retryJitter: 100
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
logger.info(
|
||||||
|
`identity login failed to acquire lock [identityId=${identityUa.identityId}] [authMethod=${IdentityAuthMethod.UNIVERSAL_AUTH}]`
|
||||||
|
);
|
||||||
|
throw new RateLimitError({ message: "Rate limit exceeded" });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!validClientSecretInfo) throw new UnauthorizedError({ message: "Invalid credentials" });
|
try {
|
||||||
|
const lockoutRaw = await keyStore.getItem(LOCKOUT_KEY);
|
||||||
|
|
||||||
const { clientSecretTTL, clientSecretNumUses, clientSecretNumUsesLimit } = validClientSecretInfo;
|
let lockout: LockoutObject | undefined;
|
||||||
if (Number(clientSecretTTL) > 0) {
|
if (lockoutRaw) {
|
||||||
const clientSecretCreated = new Date(validClientSecretInfo.createdAt);
|
lockout = JSON.parse(lockoutRaw) as LockoutObject;
|
||||||
const ttlInMilliseconds = Number(clientSecretTTL) * 1000;
|
}
|
||||||
const currentDate = new Date();
|
|
||||||
const expirationTime = new Date(clientSecretCreated.getTime() + ttlInMilliseconds);
|
|
||||||
|
|
||||||
if (currentDate > expirationTime) {
|
if (lockout && lockout.lockedOut) {
|
||||||
|
throw new UnauthorizedError({
|
||||||
|
message: "This identity auth method is temporarily locked, please try again later"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const identityMembershipOrg = await identityOrgMembershipDAL.findOne({ identityId: identityUa.identityId });
|
||||||
|
if (!identityMembershipOrg) {
|
||||||
|
throw new UnauthorizedError({
|
||||||
|
message: "Invalid credentials"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const clientSecretPrefix = clientSecret.slice(0, 4);
|
||||||
|
const clientSecretInfo = await identityUaClientSecretDAL.find({
|
||||||
|
identityUAId: identityUa.id,
|
||||||
|
isClientSecretRevoked: false,
|
||||||
|
clientSecretPrefix
|
||||||
|
});
|
||||||
|
|
||||||
|
let validClientSecretInfo: (typeof clientSecretInfo)[0] | null = null;
|
||||||
|
for await (const info of clientSecretInfo) {
|
||||||
|
const isMatch = await crypto.hashing().compareHash(clientSecret, info.clientSecretHash);
|
||||||
|
|
||||||
|
if (isMatch) {
|
||||||
|
validClientSecretInfo = info;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!validClientSecretInfo) {
|
||||||
|
if (identityUa.lockoutEnabled) {
|
||||||
|
if (!lockout) {
|
||||||
|
lockout = {
|
||||||
|
lockedOut: false,
|
||||||
|
failedAttempts: 0
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
lockout.failedAttempts += 1;
|
||||||
|
if (lockout.failedAttempts >= identityUa.lockoutThreshold) {
|
||||||
|
lockout.lockedOut = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
await keyStore.setItemWithExpiry(
|
||||||
|
LOCKOUT_KEY,
|
||||||
|
lockout.lockedOut ? identityUa.lockoutDurationSeconds : identityUa.lockoutCounterResetSeconds,
|
||||||
|
JSON.stringify(lockout)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new UnauthorizedError({ message: "Invalid credentials" });
|
||||||
|
} else if (lockout) {
|
||||||
|
await keyStore.deleteItem(LOCKOUT_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { clientSecretTTL, clientSecretNumUses, clientSecretNumUsesLimit } = validClientSecretInfo;
|
||||||
|
if (Number(clientSecretTTL) > 0) {
|
||||||
|
const clientSecretCreated = new Date(validClientSecretInfo.createdAt);
|
||||||
|
const ttlInMilliseconds = Number(clientSecretTTL) * 1000;
|
||||||
|
const currentDate = new Date();
|
||||||
|
const expirationTime = new Date(clientSecretCreated.getTime() + ttlInMilliseconds);
|
||||||
|
|
||||||
|
if (currentDate > expirationTime) {
|
||||||
|
await identityUaClientSecretDAL.updateById(validClientSecretInfo.id, {
|
||||||
|
isClientSecretRevoked: true
|
||||||
|
});
|
||||||
|
|
||||||
|
throw new UnauthorizedError({
|
||||||
|
message: "Access denied due to expired client secret"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (clientSecretNumUsesLimit > 0 && clientSecretNumUses >= clientSecretNumUsesLimit) {
|
||||||
|
// number of times client secret can be used for
|
||||||
|
// a login operation reached
|
||||||
await identityUaClientSecretDAL.updateById(validClientSecretInfo.id, {
|
await identityUaClientSecretDAL.updateById(validClientSecretInfo.id, {
|
||||||
isClientSecretRevoked: true
|
isClientSecretRevoked: true
|
||||||
});
|
});
|
||||||
|
|
||||||
throw new UnauthorizedError({
|
throw new UnauthorizedError({
|
||||||
message: "Access denied due to expired client secret"
|
message: "Access denied due to client secret usage limit reached"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (clientSecretNumUsesLimit > 0 && clientSecretNumUses === clientSecretNumUsesLimit) {
|
const accessTokenTTLParams =
|
||||||
// number of times client secret can be used for
|
Number(identityUa.accessTokenPeriod) === 0
|
||||||
// a login operation reached
|
? {
|
||||||
await identityUaClientSecretDAL.updateById(validClientSecretInfo.id, {
|
accessTokenTTL: identityUa.accessTokenTTL,
|
||||||
isClientSecretRevoked: true
|
accessTokenMaxTTL: identityUa.accessTokenMaxTTL
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
accessTokenTTL: identityUa.accessTokenPeriod,
|
||||||
|
// We set a very large Max TTL for periodic tokens to ensure that clients (even outdated ones) can always renew their token
|
||||||
|
// without them having to update their SDKs, CLIs, etc. This workaround sets it to 30 years to emulate "forever"
|
||||||
|
accessTokenMaxTTL: 1000000000
|
||||||
|
};
|
||||||
|
|
||||||
|
const identityAccessToken = await identityUaDAL.transaction(async (tx) => {
|
||||||
|
const uaClientSecretDoc = await identityUaClientSecretDAL.incrementUsage(validClientSecretInfo!.id, tx);
|
||||||
|
await identityOrgMembershipDAL.updateById(
|
||||||
|
identityMembershipOrg.id,
|
||||||
|
{
|
||||||
|
lastLoginAuthMethod: IdentityAuthMethod.UNIVERSAL_AUTH,
|
||||||
|
lastLoginTime: new Date()
|
||||||
|
},
|
||||||
|
tx
|
||||||
|
);
|
||||||
|
const newToken = await identityAccessTokenDAL.create(
|
||||||
|
{
|
||||||
|
identityId: identityUa.identityId,
|
||||||
|
isAccessTokenRevoked: false,
|
||||||
|
identityUAClientSecretId: uaClientSecretDoc.id,
|
||||||
|
accessTokenNumUses: 0,
|
||||||
|
accessTokenNumUsesLimit: identityUa.accessTokenNumUsesLimit,
|
||||||
|
accessTokenPeriod: identityUa.accessTokenPeriod,
|
||||||
|
authMethod: IdentityAuthMethod.UNIVERSAL_AUTH,
|
||||||
|
...accessTokenTTLParams
|
||||||
|
},
|
||||||
|
tx
|
||||||
|
);
|
||||||
|
|
||||||
|
return newToken;
|
||||||
});
|
});
|
||||||
throw new UnauthorizedError({
|
|
||||||
message: "Access denied due to client secret usage limit reached"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const accessTokenTTLParams =
|
const appCfg = getConfig();
|
||||||
Number(identityUa.accessTokenPeriod) === 0
|
const accessToken = crypto.jwt().sign(
|
||||||
? {
|
|
||||||
accessTokenTTL: identityUa.accessTokenTTL,
|
|
||||||
accessTokenMaxTTL: identityUa.accessTokenMaxTTL
|
|
||||||
}
|
|
||||||
: {
|
|
||||||
accessTokenTTL: identityUa.accessTokenPeriod,
|
|
||||||
// We set a very large Max TTL for periodic tokens to ensure that clients (even outdated ones) can always renew their token
|
|
||||||
// without them having to update their SDKs, CLIs, etc. This workaround sets it to 30 years to emulate "forever"
|
|
||||||
accessTokenMaxTTL: 1000000000
|
|
||||||
};
|
|
||||||
|
|
||||||
const identityAccessToken = await identityUaDAL.transaction(async (tx) => {
|
|
||||||
const uaClientSecretDoc = await identityUaClientSecretDAL.incrementUsage(validClientSecretInfo!.id, tx);
|
|
||||||
await identityOrgMembershipDAL.updateById(
|
|
||||||
identityMembershipOrg.id,
|
|
||||||
{
|
|
||||||
lastLoginAuthMethod: IdentityAuthMethod.UNIVERSAL_AUTH,
|
|
||||||
lastLoginTime: new Date()
|
|
||||||
},
|
|
||||||
tx
|
|
||||||
);
|
|
||||||
const newToken = await identityAccessTokenDAL.create(
|
|
||||||
{
|
{
|
||||||
identityId: identityUa.identityId,
|
identityId: identityUa.identityId,
|
||||||
isAccessTokenRevoked: false,
|
clientSecretId: validClientSecretInfo.id,
|
||||||
identityUAClientSecretId: uaClientSecretDoc.id,
|
identityAccessTokenId: identityAccessToken.id,
|
||||||
accessTokenNumUses: 0,
|
authTokenType: AuthTokenType.IDENTITY_ACCESS_TOKEN
|
||||||
accessTokenNumUsesLimit: identityUa.accessTokenNumUsesLimit,
|
} as TIdentityAccessTokenJwtPayload,
|
||||||
accessTokenPeriod: identityUa.accessTokenPeriod,
|
appCfg.AUTH_SECRET,
|
||||||
authMethod: IdentityAuthMethod.UNIVERSAL_AUTH,
|
// akhilmhdh: for non-expiry tokens you should not even set the value, including undefined. Even for undefined jsonwebtoken throws error
|
||||||
...accessTokenTTLParams
|
Number(identityAccessToken.accessTokenTTL) === 0
|
||||||
},
|
? undefined
|
||||||
tx
|
: {
|
||||||
|
expiresIn: Number(identityAccessToken.accessTokenTTL)
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return newToken;
|
return {
|
||||||
});
|
accessToken,
|
||||||
|
identityUa,
|
||||||
const appCfg = getConfig();
|
validClientSecretInfo,
|
||||||
const accessToken = crypto.jwt().sign(
|
identityAccessToken,
|
||||||
{
|
identityMembershipOrg,
|
||||||
identityId: identityUa.identityId,
|
...accessTokenTTLParams
|
||||||
clientSecretId: validClientSecretInfo.id,
|
};
|
||||||
identityAccessTokenId: identityAccessToken.id,
|
} finally {
|
||||||
authTokenType: AuthTokenType.IDENTITY_ACCESS_TOKEN
|
await lock.release();
|
||||||
} as TIdentityAccessTokenJwtPayload,
|
}
|
||||||
appCfg.AUTH_SECRET,
|
|
||||||
// akhilmhdh: for non-expiry tokens you should not even set the value, including undefined. Even for undefined jsonwebtoken throws error
|
|
||||||
Number(identityAccessToken.accessTokenTTL) === 0
|
|
||||||
? undefined
|
|
||||||
: {
|
|
||||||
expiresIn: Number(identityAccessToken.accessTokenTTL)
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
accessToken,
|
|
||||||
identityUa,
|
|
||||||
validClientSecretInfo,
|
|
||||||
identityAccessToken,
|
|
||||||
identityMembershipOrg,
|
|
||||||
...accessTokenTTLParams
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const attachUniversalAuth = async ({
|
const attachUniversalAuth = async ({
|
||||||
|
|||||||
Reference in New Issue
Block a user