Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(auth): state the deleted-key case and pin the usage write's target

Yudhi Armyndharisaa67646eb3cd
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 006515adf..471ac278a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,9 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `docs/29-engine-capability-matrix.md` (`docs/29`) now covers all 152 Baileys socket methods, all 81 whatsapp-web.js Client methods, all 34 + 31 library events and all five install-time patches, each mapped to the interface method that uses it or marked unexposed.
- An onboarding modal the "What's new" probe does not recognise is now logged once with its heading and confirm-button labels (`onboarding_dialog_unrecognized`), so it can be covered via `WWEBJS_ONBOARDING_CONTINUE_LABELS` before WhatsApp unlinks the companion. Refs #1072.
-### Fixed
+### Security
-- An advisory usage-statistics write no longer persists the whole API-key row, so a revocation or a narrowing of role, allowlist or expiry cannot be reverted by the values an in-flight request loaded.
+- An advisory usage-statistics write no longer persists the whole API-key row. A key deleted while a request that had already loaded it was in flight was re-inserted with its original hash and authenticated again; a revocation or a narrowing of role, allowlist, IP allowlist or expiry was likewise reverted to the values that request loaded. The write is now scoped to the two usage columns and affects nothing if the row is gone.
## [0.15.0] - 2026-08-09
diff --git a/src/modules/auth/api-key-usage-tracker.service.ts b/src/modules/auth/api-key-usage-tracker.service.ts
index 6712a4483..24b8ad372 100644
--- a/src/modules/auth/api-key-usage-tracker.service.ts
+++ b/src/modules/auth/api-key-usage-tracker.service.ts
@@ -58,9 +58,11 @@ export class ApiKeyUsageTracker {
// time, so persisting it whole writes every column back as it was then — including isActive,
// role, allowedSessions, allowedIps and expiresAt. An administrator change committed between
// that load and this windowed write would be reverted by an advisory statistics update.
- // `forget()` closes the window for a key holding only pending counters, but cannot reach an
- // entity a request handler is already holding. Same reasoning as flushPending() below, which
- // writes one column for the same reason.
+ // Worse for a DELETED key: revocation is a hard `remove()`, so `save()` finds no row for the
+ // primary key and INSERTs it back, hash included — the credential authenticates again. An
+ // `update()` by id affects zero rows instead. `forget()` closes the window for a key holding
+ // only pending counters, but cannot reach an entity a request handler is already holding.
+ // Same reasoning as flushPending() below, which writes one column for the same reason.
await this.apiKeyRepository.update(
{ id: apiKey.id },
{ lastUsedAt: apiKey.lastUsedAt, usageCount: apiKey.usageCount },
diff --git a/src/modules/auth/api-key-usage-write-scope.spec.ts b/src/modules/auth/api-key-usage-write-scope.spec.ts
index 137e0af91..9b3660d9f 100644
--- a/src/modules/auth/api-key-usage-write-scope.spec.ts
+++ b/src/modules/auth/api-key-usage-write-scope.spec.ts
@@ -10,7 +10,9 @@ import { ApiKey, ApiKeyRole } from './entities/api-key.entity';
* `lastUsedAt`/`usageCount` on it and persists on a 60s window. Persisting that entity WHOLE writes
* every column back — including `isActive`, `role`, `allowedSessions`, `allowedIps` and `expiresAt`
* as they were when the request began. An administrator change committed between the load and the
- * windowed write is silently reverted by an advisory statistics update.
+ * windowed write is silently reverted by an advisory statistics update. Revocation is a hard delete,
+ * so the same write on a removed key is an INSERT rather than an UPDATE: the credential comes back
+ * with its original hash and authenticates again.
*
* `forget()` is called by revoke() before its deactivating save and closes the window for a key with
* only PENDING counters. It cannot reach an entity a request handler is already holding, which is the
@@ -115,11 +117,27 @@ describe('API-key usage write scope', () => {
const written: Partial<ApiKey> = updateCall ? updateCall[1] : saveCall[0];
expect(Object.keys(written).sort()).toEqual([...USAGE_COLUMNS].sort());
- // Neither column may arrive undefined: TypeORM's update() writes NULL for an undefined value.
+ // Neither column may arrive undefined. TypeORM strips an undefined value from the SET clause
+ // rather than writing NULL, so an undefined here is not a wipe — it is a write that silently
+ // updates nothing but `updatedAt`, and the statistics stop advancing with no error to notice.
expect(written.lastUsedAt).toBeInstanceOf(Date);
expect(typeof written.usageCount).toBe('number');
});
+ it('aims the write at the row it loaded and no other', async () => {
+ const row = makeRow();
+ const repository = makeRepository(row);
+ const tracker = new ApiKeyUsageTracker(repository as never);
+
+ await tracker.record({ ...row });
+
+ // Blast radius, not column scope. The double writes whatever patch it is handed to the single row
+ // it models, so every assertion about WHAT is written holds just as well for a write aimed at the
+ // wrong rows: `update({ isActive: true }, ...)` — which TypeORM accepts — would stamp one key's
+ // statistics onto every active key, and passes all of the tests above.
+ expect(repository.update.mock.calls[0][0]).toEqual({ id: row.id });
+ });
+
it('still returns the incremented counters on the entity the caller holds', async () => {
const row = makeRow({ usageCount: 5 });
const repository = makeRepository(row);