fix: end API client tokens when the client secret is rotated [T-2513]

Merged
EV
7d ago
+84-5

What was wrong

An admin rotates an API client's secret when it leaks. Before this change, the tokens issued with the leaked secret kept working until they expired: 24 hours by default, longer when TR_AUTH_TOKEN_EXPIRES is set higher. That token reads the project, and writes to it for editor or admin clients. Only deleting the client cut off access.

  • api/src/controllers/project-client.controller.ts:130-134: rotate() only replaces client.encryptedSecret.
  • api/src/controllers/auth.controller.ts:336: client tokens were signed as { sub: client.id, type: 'client' }. Nothing in the token was tied to the secret.
  • api/src/services/jwt.strategy.ts:61-66: the strategy accepted any client token whose client id still existed (select: ['id']).
  • User tokens are already revoked through sessionVersion (jwt.strategy.ts:49-52). Clients had nothing like it.

What changed

  • New api/src/utils/client-secret-hash.ts: clientSecretHash(encryptedSecret) returns the first 16 hex characters of the SHA-256 of the stored bcrypt secret. Each rotation stores a new bcrypt hash, so the value changes. The bcrypt hash is salted and never leaves the server, so the claim does not help anyone guess the secret.
  • authenticateClient (auth.controller.ts) adds it to client tokens as the sh claim.
  • JwtPayload (api/src/domain/http.ts) gets sh?: string, with a comment.
  • jwt.strategy.ts, client case: selects ['id', 'encryptedSecret'] and throws UnauthorizedException when the token's sh is missing or does not match. It then deletes encryptedSecret from the returned ProjectClient, so the secret never reaches handlers (same pattern as sessionVersion for users).
  • docs/changelog.md: two lines under next, the fix and the upgrade note.

Upgrade note: client tokens issued before this deploy have no sh, so they stop working once. Clients must request a new token with their id and secret (grant type client_credentials), as they already do when a token expires. User tokens are not affected.

Tests

Two new e2e tests in api/test/project-client.e2e-spec.ts:

  1. rotate-secret (POST) should end the tokens issued with the old secret: creates a client and gets token A with client_credentials. GET /api/v1/projects/:id/terms with A returns 200. It rotates the secret. The same GET with A returns 401 (200 on develop). A token from the new secret returns 200, and a token request with the old secret returns 401.
  2. a project client token without the hash of its secret should be refused: a client token signed without sh, as before this change, returns 401 (200 on develop).

Before the fix (develop cad26d6 plus the new tests): project-client.e2e-spec.ts had 2 failed and 10 passed, both with expected 401 "Unauthorized", got 200 "OK".
After the fix: 12 passed out of 12.

Checks run (locally, Node 24, SQLite as yarn test:e2e uses)

  • yarn check-fmt at the root (prettier --check "**/*.ts", as CircleCI check_lint runs it): all files pass.
  • yarn --cwd api lint (tslint): clean.
  • yarn --cwd api test (jest, --maxWorkers=2): 164 of 165 passed, in 25 of 26 suites. The one failure is src/formatters/properties.spec.ts › should export properties files. It fails only on Windows: the exporter writes CRLF line endings and the fixture is LF. This PR does not touch the formatters.
  • yarn --cwd api test:e2e (full suite, in band): 21 suites, 187 of 187 passed.

Fleet task T-2513; authored by the lead lane while the fleet is paused by its daily cost ceiling.

🤖 Generated with Claude Code


Summary by cubic

Rotating an API client's secret now immediately ends tokens issued with that secret, instead of letting them work until they expire (24 hours by default, or longer with TR_AUTH_TOKEN_EXPIRES).

Client tokens now carry an sh claim: a short hash of the client's stored bcrypt secret. The JWT strategy loads the stored secret, refuses tokens whose sh is missing or no longer matches, and deletes the secret from the object it hands to handlers.

Migration

  • Client tokens issued before this change stop working once; clients request a new one with client_credentials, as they already do when a token expires.
  • User tokens are unaffected.