The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
fix(api): preserve suspension deadlines in organization cache (#2210)
Ricky Chinfdd470c9dcb6
diff --git a/apps/api/README.md b/apps/api/README.mdindex 2d9888ae9..33d709a37 100644--- a/apps/api/README.md+++ b/apps/api/README.md@@ -111,6 +111,12 @@ and drops a unique schema, requires a loopback database host, and skips without`DB_HOST`. It exercises the production authorization guard and repository queries;JWT identity setup, rate limiting, and Redis transport use test doubles.+## Organization suspension++Organization access caches organization records as JSON for 10 seconds. On cache reads,+`suspendedUntil` is restored to a `Date` so temporary suspensions are enforced until their+deadline on both cached and database reads. A null deadline keeps a suspension indefinite.+## Registration configurationTurnstile registration protection is opt-in. Leave `TURNSTILE_SITEKEY` unset ordiff --git a/apps/api/src/organization/guards/organization-access.guard.ts b/apps/api/src/organization/guards/organization-access.guard.tsindex 01d58b0b1..eedfc9c29 100644--- a/apps/api/src/organization/guards/organization-access.guard.ts+++ b/apps/api/src/organization/guards/organization-access.guard.ts@@ -125,7 +125,11 @@ export class OrganizationAccessGuard implements CanActivate {try {const cachedOrganization = await this.redis.get(`organization:${organizationId}`)if (cachedOrganization) {- return JSON.parse(cachedOrganization)+ const organization = JSON.parse(cachedOrganization)+ if (organization.suspendedUntil != null) {+ organization.suspendedUntil = new Date(organization.suspendedUntil)+ }+ return organization}const organization = await this.organizationService.findOne(organizationId)if (organization) {diff --git a/apps/api/src/organization/services/organization.service.spec.ts b/apps/api/src/organization/services/organization.service.spec.tsindex af0d8e37a..fe37b9519 100644--- a/apps/api/src/organization/services/organization.service.spec.ts+++ b/apps/api/src/organization/services/organization.service.spec.ts@@ -3,8 +3,11 @@* SPDX-License-Identifier: AGPL-3.0*/-import { ConflictException, NotFoundException } from '@nestjs/common'+import { ConflictException, ExecutionContext, NotFoundException } from '@nestjs/common'+import { OrganizationAuthContext } from '../../common/interfaces/auth-context.interface'+import { SystemRole } from '../../user/enums/system-role.enum'import { Organization } from '../entities/organization.entity'+import { OrganizationAccessGuard } from '../guards/organization-access.guard'import { OrganizationService } from './organization.service'const organization = (overrides: Partial<Organization> = {}): Organization =>@@ -243,16 +246,42 @@ describe('OrganizationService.assertOrganizationIsNotSuspended', () => {// suspensionReason is free text an admin or support agent wrote and may describe a case// (e.g. a fraud investigation) in more detail than "you are suspended" should disclose.- it('does not leak the stored suspension reason to the suspended organization itself', () => {- const { service } = makeService(organization())- const suspended = organization({ suspended: true, suspensionReason: 'flagged for suspected abuse by support' })-- expect(() => service.assertOrganizationIsNotSuspended(suspended)).toThrow(- expect.objectContaining({- message: expect.not.stringContaining('flagged for suspected abuse by support'),- }),- )- })+ it.each(['temporary', 'indefinite'])(+ 'rejects a %s suspension without leaking its reason on cache hits',+ async (kind) => {+ const suspendedUntil = kind === 'temporary' ? new Date(Date.now() + 60_000) : null+ const { service, organizationRepository } = makeService(+ organization({ suspended: true, suspendedUntil, suspensionReason: 'flagged for suspected abuse by support' }),+ )+ const guard = new OrganizationAccessGuard(service, {+ findOne: jest.fn().mockResolvedValue({ organizationId: 'org-1', userId: 'user-1' }),+ } as any)+ const cache = new Map<string, string>()+ ;(guard as any).redis = {+ get: async (key: string) => cache.get(key) ?? null,+ set: async (key: string, value: string) => cache.set(key, value),+ }++ for (let read = 0; read < 2; read++) {+ const request = {+ params: { organizationId: 'org-1' },+ user: { userId: 'user-1', role: SystemRole.USER },+ }+ const context = { switchToHttp: () => ({ getRequest: () => request }) } as ExecutionContext+ await expect(guard.canActivate(context)).resolves.toBe(true)+ const found = (request.user as OrganizationAuthContext).organization++ expect(() => service.assertOrganizationIsNotSuspended(found)).toThrow(+ expect.objectContaining({+ status: 403,+ message: expect.not.stringContaining('flagged for suspected abuse by support'),+ }),+ )+ expect(found.suspendedUntil).toEqual(suspendedUntil)+ }+ expect(organizationRepository.findOne).toHaveBeenCalledTimes(1)+ },+ )it('does not throw once a temporary suspension has expired', () => {const { service } = makeService(organization())