fix: end API client tokens when the client secret is rotated [T-2513]
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 replacesclient.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 theshclaim.JwtPayload(api/src/domain/http.ts) getssh?: string, with a comment.jwt.strategy.ts, client case: selects['id', 'encryptedSecret']and throwsUnauthorizedExceptionwhen the token'sshis missing or does not match. It then deletesencryptedSecretfrom the returnedProjectClient, so the secret never reaches handlers (same pattern assessionVersionfor users).docs/changelog.md: two lines undernext, 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:
rotate-secret (POST) should end the tokens issued with the old secret: creates a client and gets token A withclient_credentials.GET /api/v1/projects/:id/termswith 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.a project client token without the hash of its secret should be refused: a client token signed withoutsh, 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-fmtat the root (prettier --check "**/*.ts", as CircleCIcheck_lintruns 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 issrc/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.