Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(auth): report whether a validated key is session-scoped
Yudhi Armyndharis0dce7e38c03f
diff --git a/src/modules/auth/auth-validate.controller.spec.ts b/src/modules/auth/auth-validate.controller.spec.tsindex 87744f78f..d411baf31 100644--- a/src/modules/auth/auth-validate.controller.spec.ts+++ b/src/modules/auth/auth-validate.controller.spec.ts@@ -14,9 +14,19 @@ describe('AuthValidateController', () => {valid: true,role: ApiKeyRole.ADMIN,engineType: 'baileys',+ scoped: false,});});+ it('reports a key restricted to selected sessions as scoped', () => {+ // The dashboard gates the unscoped-only stats reads on this, so a scoped admin never sends them.+ expect(controller.validate(makeKey({ role: ApiKeyRole.ADMIN, allowedSessions: ['s1'] }))).toMatchObject({+ valid: true,+ scoped: true,+ });+ expect(controller.validate(makeKey({ allowedSessions: [] }))).toMatchObject({ scoped: false });+ });+it('reports the engine to an operator key, which cannot read the admin-only infra route', () => {// The dashboard needs the engine to gate status compose and the Channels tab for every writer.expect(controller.validate(makeKey({ role: ApiKeyRole.OPERATOR }))).toMatchObject({ engineType: 'baileys' });@@ -27,7 +37,7 @@ describe('AuthValidateController', () => {// The handler must NOT re-validate without an IP, which previously fail-closed and wrongly// reported valid:false for any key carrying an allowedIps restriction.const key = makeKey({ allowedIps: ['10.0.0.0/24'] });- expect(controller.validate(key)).toEqual({ valid: true, role: key.role, engineType: 'baileys' });+ expect(controller.validate(key)).toEqual({ valid: true, role: key.role, engineType: 'baileys', scoped: false });});it('returns valid:false when no key is attached (defense-in-depth)', () => {diff --git a/src/modules/auth/auth-validate.controller.ts b/src/modules/auth/auth-validate.controller.tsindex 905505178..7aa762186 100644--- a/src/modules/auth/auth-validate.controller.ts+++ b/src/modules/auth/auth-validate.controller.ts@@ -21,7 +21,12 @@ export class AuthValidateController {description:'The key is valid but refused here: its allowedIps exclude this client, or it is restricted to selected chats',})- validate(@CurrentApiKey() apiKey?: ApiKey): { valid: boolean; role?: string; engineType?: string } {+ validate(@CurrentApiKey() apiKey?: ApiKey): {+ valid: boolean;+ role?: string;+ engineType?: string;+ scoped?: boolean;+ } {// This route is behind the global API-key guard, so only a validated key reaches this handler// (a missing/invalid key 401s first). The guard has already verified the key — including its// client-IP and session-scope restrictions — and attached it to the request. Re-validating here@@ -33,7 +38,13 @@ export class AuthValidateController {return { valid: false };}// The engine rides along because GET /infra/engines/current is admin-only, and the dashboard needs- // it for every role that can post a status or read channels.- return { valid: true, role: apiKey.role, engineType: this.engineFactory.getCurrentEngine() };+ // it for every role that can post a status or read channels. `scoped` lets it skip the routes that+ // refuse a session-scoped key (@RequireUnscopedKey) instead of sending reads the guard rejects.+ return {+ valid: true,+ role: apiKey.role,+ engineType: this.engineFactory.getCurrentEngine(),+ scoped: (apiKey.allowedSessions?.length ?? 0) > 0,+ };}}diff --git a/src/modules/auth/dto/api-key.dto.ts b/src/modules/auth/dto/api-key.dto.tsindex 163225a26..831dad41e 100644--- a/src/modules/auth/dto/api-key.dto.ts+++ b/src/modules/auth/dto/api-key.dto.ts@@ -136,6 +136,12 @@ export class ValidateApiKeyResponseDto {example: 'baileys',})engineType?: string;++ @ApiPropertyOptional({+ description: 'Whether the key is restricted to selected sessions; present only when valid.',+ example: false,+ })+ scoped?: boolean;}export class UpdateApiKeyDto {