Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(auth): fail closed on IP whitelist with unknown client IP, gate QR endpoint (#213)
Yudhi Armyndharisf1c7ed6fb33e
diff --git a/src/modules/auth/auth.service.spec.ts b/src/modules/auth/auth.service.spec.tsindex 44ce32b32..ca0eb82fc 100644--- a/src/modules/auth/auth.service.spec.ts+++ b/src/modules/auth/auth.service.spec.ts@@ -234,6 +234,16 @@ describe('AuthService', () => {expect(result.id).toBe(key.id);});+ it('should fail closed when an IP whitelist is set but the client IP is unknown', async () => {+ const key = createMockApiKey({+ allowedIps: ['10.0.0.1'],+ keyHash: hashKey('ip-no-client'),+ });+ (repository.findOne as jest.Mock).mockResolvedValue(key);++ await expect(service.validateApiKey('ip-no-client')).rejects.toThrow('Client IP could not be determined');+ });+it('should throw UnauthorizedException when session not in allowedSessions', async () => {const key = createMockApiKey({allowedSessions: ['session-A'],diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.tsindex 03f8888b9..b138f99a9 100644--- a/src/modules/auth/auth.service.ts+++ b/src/modules/auth/auth.service.ts@@ -174,8 +174,12 @@ export class AuthService implements OnModuleInit {throw new UnauthorizedException('API key has expired');}- // Check IP whitelist- if (apiKey.allowedIps && apiKey.allowedIps.length > 0 && clientIp) {+ // Check IP whitelist (fail closed: if a whitelist is configured but the client+ // IP could not be determined, reject rather than silently skipping the check)+ if (apiKey.allowedIps && apiKey.allowedIps.length > 0) {+ if (!clientIp) {+ throw new UnauthorizedException('Client IP could not be determined');+ }if (!this.isIpAllowed(clientIp, apiKey.allowedIps)) {this.logger.warn(`IP not allowed: ${clientIp}`, {keyId: apiKey.id,diff --git a/src/modules/session/session.controller.ts b/src/modules/session/session.controller.tsindex 5003c9aaa..0b0fe8628 100644--- a/src/modules/session/session.controller.ts+++ b/src/modules/session/session.controller.ts@@ -133,6 +133,7 @@ export class SessionController {}@Get(':id/qr')+ @RequireRole(ApiKeyRole.OPERATOR)@ApiOperation({ summary: 'Get QR code for session authentication' })@ApiParam({ name: 'id', description: 'Session ID' })@ApiResponse({