Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(auth): write only the patch on unguarded key mutations

Yudhi Armyndharise5e3edf20794
diff --git a/docs/29-engine-capability-matrix.md b/docs/29-engine-capability-matrix.md
index fd682fc0d..e0479e02e 100644
--- a/docs/29-engine-capability-matrix.md
+++ b/docs/29-engine-capability-matrix.md
@@ -87,7 +87,7 @@ Three automated gates keep the matrix honest:
```mermaid
flowchart TD
- M["engine-capability-matrix.ts<br/>hand-curated status + evidence"] --> SPEC["engine-parity.spec.ts<br/>matrix keys match interface methods<br/>throw-invariants scan"]
+ M["engine-capability-matrix.ts<br/>rows derived from the interface,<br/>status + evidence from curated exceptions"] --> SPEC["engine-parity.spec.ts<br/>matrix keys match interface methods<br/>throw-invariants scan"]
NM["node_modules<br/>installed engines"] --> SURF["check-upstream-surface.mjs<br/>npm run test:scripts"]
SNAP["upstream-surface.snapshot.json<br/>reviewed surface"] --> SURF
SURF -->|"new or removed symbol"| FAIL["fail until reviewed:<br/>expose / defer / record exclusion"]
diff --git a/src/engine/engine-capability-matrix.ts b/src/engine/engine-capability-matrix.ts
index 65aaa36fa..bdc19ad5b 100644
--- a/src/engine/engine-capability-matrix.ts
+++ b/src/engine/engine-capability-matrix.ts
@@ -476,12 +476,24 @@ function readInterfaceMethods(): string[] {
function deriveEngineCapabilityMatrix(): Record<string, MethodCapability> {
const matrix: Record<string, MethodCapability> = {};
for (const method of readInterfaceMethods()) {
- matrix[method] = CURATED_CAPABILITY_EXCEPTIONS[method] ?? {
- wwjs: { status: 'supported' },
- baileys: { status: 'supported' },
- };
+ // Copy, never alias: a consumer mutating its matrix row (or an adapter cell on it) must not
+ // write through into CURATED_CAPABILITY_EXCEPTIONS, which every future derivation reads.
+ matrix[method] = copyCapability(
+ CURATED_CAPABILITY_EXCEPTIONS[method] ?? {
+ wwjs: { status: 'supported' },
+ baileys: { status: 'supported' },
+ },
+ );
}
return matrix;
}
+/** A two-level copy — exactly the shape MethodCapability has, so the derived row shares no object
+ * with the curated table it came from. */
+function copyCapability(entry: MethodCapability): MethodCapability {
+ const copy: MethodCapability = { wwjs: { ...entry.wwjs }, baileys: { ...entry.baileys } };
+ if (entry.evidence !== undefined) copy.evidence = entry.evidence;
+ return copy;
+}
+
export const ENGINE_CAPABILITY_MATRIX: Record<string, MethodCapability> = deriveEngineCapabilityMatrix();
diff --git a/src/modules/auth/auth.service.spec.ts b/src/modules/auth/auth.service.spec.ts
index 3a92acce7..345a2872e 100644
--- a/src/modules/auth/auth.service.spec.ts
+++ b/src/modules/auth/auth.service.spec.ts
@@ -90,12 +90,12 @@ describe('bannerKeyLine (startup banner key masking)', () => {
describe('AuthService', () => {
let service: AuthService;
let repository: jest.Mocked<Partial<Repository<ApiKey>>>;
- /** In-memory stand-in for the api_keys table, shared by the findOne/remove/save mocks and the
- * guarded-statement double below. */
+ /** In-memory stand-in for the api_keys table, shared by the findOne/remove mocks and the
+ * statement double below. */
let keys: Map<string, ApiKey>;
- /** Guarded statements whose write committed (affected = 1), newest last — for assertions on what
- * actually landed. */
- let committedGuardedWrites: Array<{ mode: 'update' | 'delete'; patch?: Record<string, unknown> }>;
+ /** Statements whose write committed (affected = 1), newest last, with whether the last-admin
+ * guard clause was bound — for assertions on what actually landed, and how. */
+ let committedWrites: Array<{ mode: 'update' | 'delete'; patch?: Record<string, unknown>; guarded: boolean }>;
beforeEach(async () => {
repository = {
@@ -129,14 +129,16 @@ describe('AuthService', () => {
/**
* In-memory stand-in for the api_keys table: findOne()/save()/remove() and the query-builder
- * double below are all backed by the same Map. The last-admin guard now lives inside the guarded
+ * double below are all backed by the same Map. The last-admin guard lives inside the guarded
* SQL statement itself, so the double's execute() evaluates the guard and performs the write as
* ONE synchronous step — mirroring the single atomic statement, so concurrency scenarios stay
- * deterministic (statements interleave only before or after that step, never inside it).
+ * deterministic (statements interleave only before or after that step, never inside it). Only
+ * statements built through withLastAdminGuard carry the guard (the double mirrors the clause
+ * onto the andWhere call); an unguarded statement always touches an existing row.
*/
function setupKeys(seed: ApiKey[]): void {
keys = new Map(seed.map(k => [k.id, k]));
- committedGuardedWrites = [];
+ committedWrites = [];
(repository.findOne as jest.Mock).mockImplementation((options: { where: { id: string } }) =>
Promise.resolve(keys.get(options.where.id) ?? null),
);
@@ -153,6 +155,7 @@ describe('AuthService', () => {
mode: 'update' as 'update' | 'delete',
patch: undefined as Record<string, unknown> | undefined,
targetId: undefined as string | undefined,
+ guarded: false,
update() {
this.mode = 'update';
return this;
@@ -173,6 +176,7 @@ describe('AuthService', () => {
return this;
},
andWhere() {
+ this.guarded = true;
return this;
},
setParameters() {
@@ -182,11 +186,13 @@ describe('AuthService', () => {
const target = keys.get(this.targetId as string);
if (!target) return { affected: 0 };
const guardPasses =
- !isUsableAdminRow(target) || [...keys.values()].some(k => k.id !== target.id && isUsableAdminRow(k));
+ !this.guarded ||
+ !isUsableAdminRow(target) ||
+ [...keys.values()].some(k => k.id !== target.id && isUsableAdminRow(k));
if (!guardPasses) return { affected: 0 };
if (this.mode === 'delete') keys.delete(this.targetId as string);
else Object.assign(target, this.patch ?? {});
- committedGuardedWrites.push({ mode: this.mode, patch: this.patch });
+ committedWrites.push({ mode: this.mode, patch: this.patch, guarded: this.guarded });
return { affected: 1 };
},
};
@@ -285,9 +291,7 @@ describe('AuthService', () => {
describe('update', () => {
it('should update only the provided fields', async () => {
- const key = createMockApiKey();
- (repository.findOne as jest.Mock).mockResolvedValue(key);
- (repository.save as jest.Mock).mockImplementation(k => Promise.resolve(k));
+ setupKeys([createMockApiKey({ id: 'uuid-1' })]);
const result = await service.update('uuid-1', { name: 'Updated' });
@@ -300,9 +304,7 @@ describe('AuthService', () => {
jest
.spyOn((service as unknown as { moduleRef: { get: (...a: unknown[]) => unknown } }).moduleRef, 'get')
.mockReturnValue({ evictApiKey });
- const key = createMockApiKey({ allowedSessions: ['sess-A', 'sess-B'] });
- (repository.findOne as jest.Mock).mockResolvedValue(key);
- (repository.save as jest.Mock).mockImplementation(k => Promise.resolve(k));
+ setupKeys([createMockApiKey({ id: 'uuid-1', allowedSessions: ['sess-A', 'sess-B'] })]);
await service.update('uuid-1', { allowedSessions: ['sess-A'] });
@@ -314,9 +316,7 @@ describe('AuthService', () => {
jest
.spyOn((service as unknown as { moduleRef: { get: (...a: unknown[]) => unknown } }).moduleRef, 'get')
.mockReturnValue({ evictApiKey });
- const key = createMockApiKey({ role: ApiKeyRole.OPERATOR });
- (repository.findOne as jest.Mock).mockResolvedValue(key);
- (repository.save as jest.Mock).mockImplementation(k => Promise.resolve(k));
+ setupKeys([createMockApiKey({ id: 'uuid-1', role: ApiKeyRole.OPERATOR })]);
await service.update('uuid-1', { role: ApiKeyRole.ADMIN });
@@ -328,9 +328,7 @@ describe('AuthService', () => {
jest
.spyOn((service as unknown as { moduleRef: { get: (...a: unknown[]) => unknown } }).moduleRef, 'get')
.mockReturnValue({ evictApiKey });
- const key = createMockApiKey({ name: 'original' });
- (repository.findOne as jest.Mock).mockResolvedValue(key);
- (repository.save as jest.Mock).mockImplementation(k => Promise.resolve(k));
+ setupKeys([createMockApiKey({ id: 'uuid-1', name: 'original' })]);
await service.update('uuid-1', { name: 'renamed' });
@@ -338,14 +336,13 @@ describe('AuthService', () => {
});
it('rejects demoting or expiring the last usable admin', async () => {
- const key = createMockApiKey({ role: ApiKeyRole.ADMIN });
- (repository.findOne as jest.Mock).mockResolvedValue(key);
+ setupKeys([createMockApiKey({ id: 'uuid-1', role: ApiKeyRole.ADMIN })]);
await expect(service.update('uuid-1', { role: ApiKeyRole.OPERATOR })).rejects.toThrow(/last active admin/i);
await expect(
service.update('uuid-1', { expiresAt: new Date(Date.now() + 60_000).toISOString() }),
).rejects.toThrow(/last active admin/i);
- expect(repository.save).not.toHaveBeenCalled();
+ expect(committedWrites).toHaveLength(0); // neither write landed
});
});
@@ -402,9 +399,7 @@ describe('AuthService', () => {
describe('revoke', () => {
it('should set isActive to false', async () => {
- const key = createMockApiKey({ isActive: true });
- (repository.findOne as jest.Mock).mockResolvedValue(key);
- (repository.save as jest.Mock).mockImplementation(k => Promise.resolve(k));
+ setupKeys([createMockApiKey({ id: 'uuid-1', isActive: true })]);
const result = await service.revoke('uuid-1');
@@ -417,13 +412,11 @@ describe('AuthService', () => {
.spyOn((service as unknown as { moduleRef: { get: (...a: unknown[]) => unknown } }).moduleRef, 'get')
.mockReturnValue({ evictApiKey });
- const key = createMockApiKey({ isActive: true });
- (repository.findOne as jest.Mock).mockResolvedValue(key);
- (repository.save as jest.Mock).mockImplementation(k => Promise.resolve(k));
+ setupKeys([createMockApiKey({ id: 'uuid-1', isActive: true })]);
await service.revoke('uuid-1');
- expect(key.isActive).toBe(false);
+ expect((await service.findOne('uuid-1')).isActive).toBe(false);
expect(evictApiKey).toHaveBeenCalledWith('uuid-1', 'revoked');
});
@@ -434,9 +427,7 @@ describe('AuthService', () => {
throw new Error('gateway unavailable');
});
- const key = createMockApiKey({ isActive: true });
- (repository.findOne as jest.Mock).mockResolvedValue(key);
- (repository.save as jest.Mock).mockImplementation(k => Promise.resolve(k));
+ setupKeys([createMockApiKey({ id: 'uuid-1', isActive: true })]);
const result = await service.revoke('uuid-1');
@@ -444,12 +435,11 @@ describe('AuthService', () => {
});
it('rejects revoking the last usable admin', async () => {
- const key = createMockApiKey({ role: ApiKeyRole.ADMIN, isActive: true });
- (repository.findOne as jest.Mock).mockResolvedValue(key);
+ setupKeys([createMockApiKey({ id: 'uuid-1', role: ApiKeyRole.ADMIN, isActive: true })]);
await expect(service.revoke('uuid-1')).rejects.toThrow(/last active admin/i);
- expect(repository.save).not.toHaveBeenCalled();
- expect(key.isActive).toBe(true);
+ expect(committedWrites).toHaveLength(0); // the write never landed
+ expect((await service.findOne('uuid-1')).isActive).toBe(true);
});
});
@@ -487,7 +477,7 @@ describe('AuthService', () => {
expect(succeeded).toHaveLength(1);
expect(conflicts).toHaveLength(1);
// The loser's write never happened: exactly one capability-stripping write in total.
- const strippingWrites = committedGuardedWrites.filter(
+ const strippingWrites = committedWrites.filter(
w =>
w.mode === 'delete' ||
(w.patch?.role !== undefined && w.patch.role !== ApiKeyRole.ADMIN) ||
@@ -508,7 +498,7 @@ describe('AuthService', () => {
await expect(service.findOne('admin-b')).rejects.toThrow(NotFoundException);
});
- it('keeps non-admin mutations and benign admin updates off the guarded statement', async () => {
+ it('runs non-admin mutations and benign admin updates on unguarded statements', async () => {
setupKeys([
createMockApiKey({ id: 'op-del', role: ApiKeyRole.OPERATOR }),
createMockApiKey({ id: 'op-rev', role: ApiKeyRole.OPERATOR }),
@@ -521,7 +511,47 @@ describe('AuthService', () => {
await service.update('op-demote', { role: ApiKeyRole.VIEWER }); // demote of a non-admin
await service.update('adm-1', { name: 'renamed' }); // benign update of an admin
- expect(repository.createQueryBuilder).not.toHaveBeenCalled();
+ // The last-admin guard is bound via andWhere; none of these statements carries it — the
+ // benign admin rename runs unguarded even though the target IS a usable admin, because a
+ // non-stripping patch cannot strand the system.
+ expect(committedWrites).toHaveLength(3); // revoke + demote + rename (the delete removes the row)
+ expect(committedWrites.every(w => !w.guarded)).toBe(true);
+ await expect(service.findOne('op-del')).rejects.toThrow(NotFoundException);
+ expect((await service.findOne('op-rev')).isActive).toBe(false);
+ });
+ });
+
+ // ── racing mutations on the unguarded paths ───────────────────────
+
+ describe('racing mutations on unguarded targets', () => {
+ // Both scenarios model the same interleaving: the pre-read (findOne #1) returns a STALE
+ // snapshot, a concurrent mutation has already committed into the table by the time the
+ // write runs. The write must carry only its own patch — a full-entity save from the stale
+ // snapshot would resurrect the concurrent commit.
+ it('a rename does not resurrect a concurrent revoke: the write carries only name', async () => {
+ setupKeys([createMockApiKey({ id: 'op-1', role: ApiKeyRole.OPERATOR, isActive: false, name: 'original' })]);
+ (repository.findOne as jest.Mock).mockResolvedValueOnce(
+ createMockApiKey({ id: 'op-1', role: ApiKeyRole.OPERATOR, isActive: true, name: 'original' }), // stale pre-read
+ );
+
+ const result = await service.update('op-1', { name: 'renamed' });
+
+ expect(result.name).toBe('renamed');
+ expect(result.isActive).toBe(false); // the revoke survives the rename
+ expect((await service.findOne('op-1')).isActive).toBe(false); // in the table, not just the reply
+ });
+
+ it('a revoke does not clobber a concurrent rename: isActive is the only column written', async () => {
+ setupKeys([createMockApiKey({ id: 'op-1', role: ApiKeyRole.OPERATOR, isActive: true, name: 'renamed-by-peer' })]);
+ (repository.findOne as jest.Mock).mockResolvedValueOnce(
+ createMockApiKey({ id: 'op-1', role: ApiKeyRole.OPERATOR, isActive: true, name: 'original' }), // stale pre-read
+ );
+
+ const result = await service.revoke('op-1');
+
+ expect(result.isActive).toBe(false);
+ expect(result.name).toBe('renamed-by-peer'); // the concurrent rename survives the revoke
+ expect((await service.findOne('op-1')).name).toBe('renamed-by-peer');
});
});
diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts
index cf1e9b215..72eb282ab 100644
--- a/src/modules/auth/auth.service.ts
+++ b/src/modules/auth/auth.service.ts
@@ -253,8 +253,9 @@ export class AuthService implements OnModuleInit, OnModuleDestroy {
// The row's post-write state, for the eviction comparison below.
saved = await this.findOne(id);
} else {
- Object.assign(apiKey, patch);
- saved = await this.apiKeyRepository.save(apiKey);
+ await this.applyUnguardedUpdate(patch, id);
+ // The row's post-write state, for the eviction comparison below.
+ saved = await this.findOne(id);
}
// Compare membership, not order: a pure reorder of allowedIps/allowedSessions is a no-op for the
@@ -304,8 +305,9 @@ export class AuthService implements OnModuleInit, OnModuleDestroy {
await this.assertMutationApplied(id, result.affected);
saved = await this.findOne(id);
} else {
- apiKey.isActive = false;
- saved = await this.apiKeyRepository.save(apiKey);
+ // A non-admin target cannot strand the system — no guard needed.
+ await this.applyUnguardedUpdate({ isActive: false }, id);
+ saved = await this.findOne(id);
}
// A revoked key fails validation before its next flush, so its accumulator would orphan —
// drop it here.
@@ -379,6 +381,27 @@ export class AuthService implements OnModuleInit, OnModuleDestroy {
throw new ConflictException('Cannot remove the last active admin key');
}
+ /**
+ * Apply a single-row UPDATE with no last-admin guard — the caller has established the target
+ * cannot strand the system (non-admin target, or a non-stripping patch). The SET list is only
+ * the patch itself: saving the pre-read ENTITY instead would write every column from that
+ * snapshot, resurrecting a revoke or demote that committed between the read and the write (a
+ * rename writing isActive: true back over a concurrent false, for instance). There is no guard
+ * clause that can refuse, so zero affected rows can only mean a concurrent delete won the race;
+ * the re-read then raises the same NotFoundException the pre-read would have.
+ */
+ private async applyUnguardedUpdate(patch: QueryDeepPartialEntity<ApiKey>, id: string): Promise<void> {
+ const result = await this.apiKeyRepository
+ .createQueryBuilder()
+ .update(ApiKey)
+ .set(patch)
+ .where('"id" = :id', { id })
+ .execute();
+ if (!result.affected) {
+ await this.findOne(id);
+ }
+ }
+
/**
* Disconnect every WebSocket socket authenticated with the given key id. Resolved lazily via
* ModuleRef (not constructor injection) to avoid a static DI cycle between AuthModule and
diff --git a/src/modules/message/message-send.service.ts b/src/modules/message/message-send.service.ts
index f84b4aaa7..fc328f166 100644
--- a/src/modules/message/message-send.service.ts
+++ b/src/modules/message/message-send.service.ts
@@ -144,9 +144,11 @@ export class MessageSendService {
/**
* Run the pre-send `message:sending` plugin gate for one outbound message and return the
* (possibly plugin-modified) input, or throw BadRequestException if a plugin blocked the send.
- * Centralised so EVERY public sender — text, media, extended (location/contact/poll/sticker/
- * reply/forward) and edit — passes through the same moderation chokepoint, instead of only
- * `sendText`. The implementation is shared with StatusService via core/hooks/sending-gate.
+ * Centralised so EVERY public sender in this class — text, media, extended (location/contact/
+ * poll/sticker/reply/forward) — passes through the same moderation chokepoint, instead of only
+ * `sendText`. The edit path is gated by the twin method in MessageService (the query side of
+ * the send/query split), on the same core/hooks/sending-gate implementation shared with
+ * StatusService.
*/
private async applySendingGate<T extends object>(sessionId: string, type: string, input: T): Promise<T> {
// Pacing runs BEFORE the plugin gate, so a send that policy forbids never reaches a plugin at
diff --git a/src/modules/message/message.service.ts b/src/modules/message/message.service.ts
index fcb8b55c9..248668b01 100644
--- a/src/modules/message/message.service.ts
+++ b/src/modules/message/message.service.ts
@@ -499,9 +499,10 @@ export class MessageService {
/**
* Run the pre-send `message:sending` plugin gate for one outbound message and return the
* (possibly plugin-modified) input, or throw BadRequestException if a plugin blocked the send.
- * Centralised so EVERY public sender — text, media, extended (location/contact/poll/sticker/
- * reply/forward) and edit — passes through the same moderation chokepoint, instead of only
- * `sendText`. The implementation is shared with StatusService via core/hooks/sending-gate.
+ * On this query-side class the edit path is the one sender it gates; the text, media, and
+ * extended senders pass through the twin method in MessageSendService. Both funnel into the
+ * same core/hooks/sending-gate implementation (shared with StatusService too), so no outbound
+ * sender skips moderation.
*/
private async applySendingGate<T extends object>(sessionId: string, type: string, input: T): Promise<T> {
// Pacing runs BEFORE the plugin gate, so a send that policy forbids never reaches a plugin at