CI / Secrets and trust
Use a secret in a job
Your job must send a token to a service, but no script in the job container can be allowed to read or print the token. The job gets a placeholder, and the forge swaps it for the value only as a request leaves for the host of the secret.
7 min read
You need a secret with a value in the environment of the job, bound to a host and a header. See Add a CI secret.
1. Declare the secret on the job
A job declares its environment, the names of the secrets that it needs, and the hosts that it can reach (egress). The workflow syntax is in Your first workflow.
import { defineWorkflow, pullRequest } from '@tanstack-source/ci'
export default defineWorkflow('pr', { on: [pullRequest({ branches: ['main'] })] })
.job('test', {
environment: 'preview',
secrets: { NX_CLOUD_ACCESS_TOKEN: {} },
egress: ['registry.npmjs.org', 'cloud.nx.app'],
steps: [
{ name: 'Install', run: 'pnpm install --frozen-lockfile' },
{ name: 'Test', run: 'pnpm nx run-many -t test' },
],
})
- A host that a secret is bound to must also be in the
egresslist of the job. - Each secret takes its value in the environment. If a secret has no value there, the job is skipped, with the reason. To run the job without the secret, declare it as
{ optional: true }. - A value that is not secret, like a deploy URL, can be a CI variable instead.
2. Put the workflow on the base branch
A pull request job takes its environment and its secrets from the workflow of the base branch. So the job must be on main before a pull request into main gets the secret.
Push the workflow to main, or merge it through a pull request. Then open a pull request into main.
3. Read the run
Open the run of the pull request. The run page shows the environment of each job next to its state: preview for test.
Done
The test job of a pull request run gets the read-only Nx Cloud token of preview. The container held only a placeholder, and the token went only to cloud.nx.app, in the Authorization header.
Which environment a job gets
The job runs only when its run passes the two rules of the environment:
- The ref of the run is one of the refs of the environment.
- The trust level of the run is at least the lowest trust level of the environment.
If not, the job is skipped, and the reason says which rule failed. For example:
productiontakes only protected runs of branches whose branch rule requires a pull request.previewtakes only pull request runs that are at leasttrusted.- A tag push is only
trusted, because a person with write access can push that code alone. A push to a default branch with no such rule is onlytrustedtoo. Soproductionrefuses both.
A job that declares a secret and names no environment uses the development environment: its secrets take their values there. Some runs cannot use development: by default, a pull request run and a tag run. In such a run, a job whose secrets are all optional runs without them. A job with a required secret is skipped.
See CI environments for the rules of each environment.
When a job sends its first request with a placeholder, the forge checks the rules of the environment again. It uses the settings that the environment has then, and reads the values into memory. If an admin changed the environment so that the run no longer passes, the request is refused with 403, and the job error says why. The values are never stored in the record of the job.
What the job sees
A job never holds a secret value. Each declared secret is an environment variable that holds a placeholder: tss_ph_ and 40 random letters and digits, new for each run. In a do step, ctx.secrets holds the same placeholders.
The forge swaps a placeholder for the value on the way out, only when both of these are true:
- The request goes to a host that the secret is bound to.
- The placeholder is in the header of the secret. When the header is
Authorization, this includes the user name or the password of HTTP Basic credentials.
The forge refuses the request with 403 in these cases:
- A placeholder goes to any other host. The forge records the request as a leak attempt. The job error names the secret, never the value.
- A placeholder is in the URL path or query, also for a bound host. A host can send its URL back in a response.
- A placeholder is in any other header than the header of its secret, also for a bound host.
By default, the forge swaps no placeholder in a request body. A service that reads its token from the body gets the placeholder. An admin can let one secret go in bodies: see Send a secret in a request body.
What comes back from the host
The answer of a bound host comes back with every secret value masked as its placeholder again, in the headers and in the body. The forge masks:
- The value as it is, percent-encoded, JSON-escaped, and as base64.
- The exact Basic credentials that it sent.
- Any piece of those of 12 bytes or more.
So a service that echoes its request, for example an error page that quotes the Authorization header, never hands the value to the job.
- A request that carries a value also loses its
Range,If-RangeandAccept-Encodingheaders. So nobody can ask the host for a part of its answer, or for a compressed one, which the masking cannot read. - When the forge cannot read the values at that moment, the request is refused with
403. It is never sent with the placeholder.
Warning
The masking only adds safety. A host that holds the value can send it back in a form that the forge does not know. Bind a secret only to hosts that you trust with it.
Send a secret in a request body
Some services read a secret from the request body: an OAuth token endpoint that takes client_secret in a form, or an API that takes a key in JSON. An admin turns this on for one secret.
- Bind the secret to the host, and turn on request bodies:
src secret set CLIENT_SECRET --env production --host auth.example.com --in-body < secret.txt. On the web, tick Also send it in JSON and form request bodies to these hosts in the dialog of the secret. Over REST and MCP, send"inBody": true. - Declare the secret on the job, with the host in
egress, and put$CLIENT_SECRETin the body of the request.
What the forge does:
- It swaps the placeholder in the body of a request to a bound host, when the body is JSON (
application/json,+json) or a form (application/x-www-form-urlencoded). It writes the value in the form of the body: JSON-escaped, or percent-encoded. - It reads only a body with a
Content-Lengthof at most 1 MiB that is not compressed. Any other body goes as it is, with the placeholder. - A secret can have a header too, or go in bodies only: leave
--headerout.--in-body=falseturns bodies off again. - Turning it on deletes the values of the secret in write-only environments, as a new host does. Enter them again.
- The answer of the host is masked as for a header.
Caution
Turn this on only for a host that does not store or show what a body holds, like a token endpoint. Some hosts keep the body, like an API that makes a comment or a file from it. Such a host keeps the real value where people read it. This is why it is off by default.
The run token and the OIDC token
- Each job also gets
TSS_TOKEN, the placeholder of its run token. The forge swaps it only for its own git host and REST API. Sogit pushand theforge()client work, within the permissions of the run. - A job with
permissions: { idToken: true }, in a trusted or protected run, can also ask for an OIDC token to trade with a cloud for keys. It is the one real credential that a job holds. It is good for 5 minutes and for one audience, and the forge does not mask it in logs. Do not print it.