Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(auth): trim the raw API key before hashing it (#1047)
AlexC744db9539223
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e2b7fd762..208e11bba 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]+### Fixed++- **A key pasted with a stray space now authenticates on the WebSocket, not just over REST.**+ `validateApiKey` hashed the raw string as given, and HTTP strips surrounding whitespace from header+ values in transit — so a padded key was accepted on every REST call while the Socket.IO handshake,+ which carries the literal string in its `CONNECT` payload, hashed to something else and was rejected.+ The failure was silent in the worst way: the dashboard's commands all went through, but not a single+ event arrived, so every session rendered as disconnected and restarting one looked inert — the+ restart ran, the UI just never heard about it. The key is now trimmed once in `validateApiKey`, which+ REST, the WebSocket gateway and the MCP mount all route through, so every surface agrees on what the+ credential is. Whitespace is never part of a key, so no legitimate credential changes meaning.+## [0.12.5] - 2026-08-03The follow-up to 0.12.4, and the end of the decomposition work. **Nothing here is visible to a user ordiff --git a/src/modules/auth/auth.service.spec.ts b/src/modules/auth/auth.service.spec.tsindex 551c45251..c7dc7b809 100644--- a/src/modules/auth/auth.service.spec.ts+++ b/src/modules/auth/auth.service.spec.ts@@ -581,6 +581,18 @@ describe('AuthService', () => {expect(result.lastUsedAt).toBeDefined();});+ it('accepts a key padded with whitespace, as HTTP header parsing already does', async () => {+ // A key pasted with a stray space authenticates over REST (header values are trimmed in+ // transit) and must authenticate over the WebSocket handshake too, which carries the+ // literal string from the CONNECT payload.+ const rawKey = 'padded-key';+ const key = createMockApiKey({ keyHash: hashKey(rawKey) });+ (repository.findOne as jest.Mock).mockResolvedValue(key);+ (repository.save as jest.Mock).mockImplementation(k => Promise.resolve(k));++ await expect(service.validateApiKey(` ${rawKey}\n`)).resolves.toMatchObject({ id: key.id });+ });+it('coalesces the usage-stat write within the throttle window', async () => {const rawKey = 'recent-key';const key = createMockApiKey({ keyHash: hashKey(rawKey), lastUsedAt: new Date(), usageCount: 5 });diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.tsindex 403a42616..c66a14799 100644--- a/src/modules/auth/auth.service.ts+++ b/src/modules/auth/auth.service.ts@@ -413,7 +413,12 @@ export class AuthService implements OnModuleInit, OnModuleDestroy {}async validateApiKey(rawKey: string, clientIp?: string, sessionId?: string): Promise<ApiKey> {- const keyHash = this.hashKey(rawKey);+ // Trim before hashing so every surface agrees on what the credential is. HTTP already strips+ // surrounding whitespace from header values, so a pasted key with a stray space/newline+ // authenticates over REST but fails on the WebSocket handshake (the CONNECT payload carries the+ // literal string) — the dashboard then runs commands fine while never receiving events, and the+ // session looks permanently disconnected. Whitespace is never part of a key.+ const keyHash = this.hashKey(rawKey?.trim());const apiKey = await this.apiKeyRepository.findOne({ where: { keyHash } });if (!apiKey) {