Fix: OIDC users losing credentials after re-login due to non-persistent encryption keys #363
@@ -70,13 +70,11 @@ class UserCrypto {
|
||||
}
|
||||
|
|
||||
|
||||
async setupOIDCUserEncryption(userId: string): Promise<void> {
|
||||
// Check if DEK already exists for this OIDC user
|
||||
const existingKEKSalt = await this.getKEKSalt(userId);
|
||||
const existingEncryptedDEK = await this.getEncryptedDEK(userId);
|
||||
|
||||
let DEK: Buffer;
|
||||
|
||||
if (existingKEKSalt && existingEncryptedDEK) {
|
||||
if (existingEncryptedDEK) {
|
||||
// User already has a persisted DEK, retrieve it
|
||||
const systemKey = this.deriveOIDCSystemKey(userId);
|
||||
DEK = this.decryptDEK(existingEncryptedDEK, systemKey);
|
||||
@@ -84,19 +82,30 @@ class UserCrypto {
|
||||
} else {
|
||||
// First time setup - create and persist new DEK
|
||||
DEK = crypto.randomBytes(UserCrypto.DEK_LENGTH);
|
||||
|
||||
// Generate a KEK salt for OIDC user (using a deterministic approach)
|
||||
const kekSalt = await this.generateKEKSalt();
|
||||
await this.storeKEKSalt(userId, kekSalt);
|
||||
|
||||
// Derive system key for OIDC user
|
||||
const systemKey = this.deriveOIDCSystemKey(userId);
|
||||
|
||||
// Encrypt and store the DEK
|
||||
const encryptedDEK = this.encryptDEK(DEK, systemKey);
|
||||
await this.storeEncryptedDEK(userId, encryptedDEK);
|
||||
try {
|
||||
const encryptedDEK = this.encryptDEK(DEK, systemKey);
|
||||
await this.storeEncryptedDEK(userId, encryptedDEK);
|
||||
|
||||
systemKey.fill(0);
|
||||
// MITIGATION: Read back the stored DEK to ensure we use the one that won the race.
|
||||
const storedEncryptedDEK = await this.getEncryptedDEK(userId);
|
||||
if (
|
||||
storedEncryptedDEK &&
|
||||
storedEncryptedDEK.data !== encryptedDEK.data
|
||||
) {
|
||||
// We lost the race. Use the DEK from the database.
|
||||
DEK.fill(0); // Discard our generated DEK.
|
||||
DEK = this.decryptDEK(storedEncryptedDEK, systemKey);
|
||||
} else if (!storedEncryptedDEK) {
|
||||
// This is an unexpected state; the store operation should have worked.
|
||||
throw new Error(
|
||||
"Failed to store and retrieve user encryption key.",
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
systemKey.fill(0);
|
||||
}
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
@@ -161,10 +170,9 @@ class UserCrypto {
|
||||
|
||||
async authenticateOIDCUser(userId: string): Promise<boolean> {
|
||||
try {
|
||||
const kekSalt = await this.getKEKSalt(userId);
|
||||
const encryptedDEK = await this.getEncryptedDEK(userId);
|
||||
|
||||
if (!kekSalt || !encryptedDEK) {
|
||||
if (!encryptedDEK) {
|
||||
// First time login or missing encryption data - set up encryption
|
||||
await this.setupOIDCUserEncryption(userId);
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user
This new implementation introduces a critical race condition and contains redundant logic.
1. Critical Race Condition (Data Loss):
If two concurrent login requests for the same new OIDC user arrive, both can enter the
elseblock to create a new DEK. ThestoreEncryptedDEKfunction performs a non-atomicselect-then-update/insert, which can cause one request's DEK to overwrite the other's in the database. If a user's session uses a DEK that is different from the one stored, any data they save will be unrecoverable on their next login. This is a critical data loss scenario.2. Redundant
kekSaltLogic:The
kekSaltis generated, stored, and checked, but it's not used for OIDC key derivation (deriveOIDCSystemKeyuses theuserIdas the salt). This adds unnecessary complexity and database writes.Suggested Fix:
The following suggestion addresses both issues:
kekSalthandling.✅ Changes Made
1. Race Condition Mitigation (Critical Fix)
Problem Identified
You're right - the original implementation had a classic check-then-act race condition. Two concurrent login requests could both generate different DEKs, and whichever stored last would win, leaving the other session with a mismatched key.
Race Condition Scenario:
Solution Implemented
Added a "write-then-read-and-verify" pattern in
setupOIDCUserEncryption():How This Prevents Data Loss: