diff --git a/server/src/nest/service-identity/service-identity.service.ts b/server/src/nest/service-identity/service-identity.service.ts index 91e002b18..9ca782320 100644 --- a/server/src/nest/service-identity/service-identity.service.ts +++ b/server/src/nest/service-identity/service-identity.service.ts @@ -1,9 +1,13 @@ +import { servicesConfig } from '../app-config/tokens'; import { logDebug } from '../audit/audit-log.logger'; import { IssuerClient } from './issuer.client'; import { QuotaTracker } from './quota-tracker'; import { ServiceClock } from './service-clock'; +import { SERVICE_ISSUER_ORIGIN } from './service-identity.constants'; +import { isAllowlisted } from './service-identity.helpers'; import { ServiceKeyService, type KeySnapshot } from './service-key.service'; -import { Injectable } from '@nestjs/common'; +import { Inject, Injectable } from '@nestjs/common'; +import type { ConfigType } from '@nestjs/config'; import { serviceTierSchema, type ServiceIdentityStatus, @@ -23,6 +27,7 @@ export class ServiceIdentityService { private cached: { bearer: string | null; at: number; quota: ServiceQuota | null } | null = null; constructor( + @Inject(servicesConfig.KEY) private readonly config: ConfigType, private readonly keys: ServiceKeyService, private readonly issuer: IssuerClient, private readonly tracker: QuotaTracker, @@ -49,8 +54,9 @@ export class ServiceIdentityService { return this.status(); } - /** GET /v1/quota, which costs no allowance; null when the issuer cannot be asked right now. */ + /** GET /v1/quota, which costs no allowance; null when the issuer cannot or may not be asked right now. */ private async freshQuota(bearer: string | null): Promise { + if (bearer && !this.issuerMaySeeKey()) return null; if (this.cached && this.cached.bearer === bearer && Date.now() - this.cached.at < QUOTA_CACHE_MS) { return this.cached.quota; } @@ -65,6 +71,11 @@ export class ServiceIdentityService { this.cached = { bearer, at: Date.now(), quota }; return quota; } + + /** Even the issuer sees the key only while TREK_SERVICES_HOSTS lists it. */ + private issuerMaySeeKey(): boolean { + return isAllowlisted(new URL(SERVICE_ISSUER_ORIGIN), this.config.hosts); + } } function keyTier(key: KeySnapshot): ServiceTier | null { diff --git a/server/tests/unit/nest/service-identity/service-identity.service.test.ts b/server/tests/unit/nest/service-identity/service-identity.service.test.ts index 5923722ae..c355c9f9f 100644 --- a/server/tests/unit/nest/service-identity/service-identity.service.test.ts +++ b/server/tests/unit/nest/service-identity/service-identity.service.test.ts @@ -3,6 +3,7 @@ * the issuer when it answers, the last one a liketrek answer stated when it * does not, nothing at all while the services are off. */ +import { servicesConfig } from '../../../../src/nest/app-config/tokens'; import { IssuerClient } from '../../../../src/nest/service-identity/issuer.client'; import { QuotaTracker } from '../../../../src/nest/service-identity/quota-tracker'; import { ServiceClock } from '../../../../src/nest/service-identity/service-clock'; @@ -21,6 +22,7 @@ const QUOTA = { tier: 'std', limit: 10_000, used: 250, remaining: 9_750, reset_a const keys = { snapshot: vi.fn(async (): Promise => CONNECTED), regenerate: vi.fn(async () => undefined) }; const issuer = { quota: vi.fn() }; +const config = { mode: 'auto' as const, key: null, hosts: ['places.liketrek.com'] }; const testDb = createSnapshotTestDb(); let t: TestModule; @@ -28,6 +30,7 @@ let identity: ServiceIdentityService; beforeEach(async () => { vi.clearAllMocks(); + config.hosts = ['places.liketrek.com']; issuer.quota.mockResolvedValue({ body: QUOTA, serverMs: null }); t = await createTestModule({ db: testDb, @@ -38,6 +41,7 @@ beforeEach(async () => { ServiceClock, { provide: ServiceKeyService, useValue: keys }, { provide: IssuerClient, useValue: issuer }, + { provide: servicesConfig.KEY, useValue: config }, ], }); identity = t.get(ServiceIdentityService); @@ -105,4 +109,10 @@ describe('ServiceIdentityService.status', () => { expect(after.state).toBe('registering'); expect(issuer.quota).toHaveBeenLastCalledWith(null); }); + + it('SISVC-006: never shows the key to the issuer when TREK_SERVICES_HOSTS leaves it out', async () => { + config.hosts = ['gate.example.org']; + expect(await identity.status()).toMatchObject({ state: 'connected', tier: 'hosted', quota: null }); + expect(issuer.quota).not.toHaveBeenCalled(); + }); });