Sub topic 3
Pinning the audience
This chapter assumes you can read a GitHub Actions workflow file and an AWS IAM trust policy, and that you know a JWT is three base64url parts (header, payload, signature) joined by dots. It also assumes you have read the previous chapter on what is inside the OIDC token. Here the focus is one claim, aud, and how it protects a role.
What the audience claim means
The aud (audience) claim names the party the token is meant for. A token issued for one audience should be rejected by any other. This stops a token that was minted for service A from being replayed against service B.
GitHub's OIDC provider issues tokens for many consumers: AWS, Google Cloud, Azure, HashiCorp Vault, and any custom service. The same workflow run can request several tokens, each with a different audience. The audience is how the issuer says which consumer the token is for.
In a GitHub Actions token, aud is set in one of two ways:
- If the workflow does not ask for a specific audience, GitHub uses the URL of the repository owner, for example
https://github.com/example-org. - If the workflow requests an audience, GitHub puts exactly that string in
aud.
For AWS, the conventional audience is sts.amazonaws.com. The aws-actions/configure-aws-credentials action requests that value by default. You can change it with the action's audience input, but you rarely need to.
Why the audience check matters
The sub claim tells AWS who is asking. The aud claim tells AWS who the token was addressed to. Without an audience check, a role accepts any token that carries a matching sub, whatever service the token was originally minted for.
Consider a workflow in example-org/example-repo on main. That workflow can legitimately request tokens for many audiences, including ones for third-party services. If one of those services is compromised or malicious, it receives a valid GitHub-signed token whose sub is repo:example-org/example-repo:ref:refs/heads/main. If your AWS role checks only sub, that service could present the token to AWS STS and assume your role. A role that also requires aud to equal sts.amazonaws.com rejects any token issued for a different audience.
The check does not stop a malicious step inside your own workflow, because that step can request a token with whatever audience it likes. The audience check limits the damage from tokens that leave the workflow. It does not replace the sub conditions.
The sequence below shows where each check happens.
Two places AWS checks the audience
AWS checks the audience in two separate places, and they do different jobs.
- The IAM OIDC provider's client ID list. When you create the IAM identity provider for
https://token.actions.githubusercontent.com, you give it a list of client IDs. For AWS STS, that list should containsts.amazonaws.com. AWS compares the token'saudwith this list when it validates the token. This is a check at the account level. It applies to every role that trusts this provider. - A condition in each role's trust policy. The condition key
token.actions.githubusercontent.com:audlets each role state which audience it accepts. This is a check at the role level.
The provider-level list is a coarse gate. The role-level condition is the one you control per role, and it is the one you should always write. Treat the two as defense in depth. Do not rely on only one of them, because the client ID list is shared by every role and may later be extended for another purpose.
Writing the audience condition
Use StringEquals for aud. The audience is a fixed string, so there is no reason for a wildcard. This is the same shape used in the trust policies from earlier in the series:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::123456789012:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/main"
}
}
}
]
}Both keys sit in the same StringEquals block. IAM requires every key in a block to match, so the token must have the right audience and the right subject. If you split them across two statements, either statement alone would be enough to grant access, which is almost never what you want.
Seeing the audience in a real token
You can inspect the claims from inside a workflow. The job must have id-token: write. GitHub exposes two environment variables to such jobs: ACTIONS_ID_TOKEN_REQUEST_URL and ACTIONS_ID_TOKEN_REQUEST_TOKEN. Append &audience=... to the URL to choose the audience.
This workflow requests a token and prints only three claims. It does not print the token itself.
name: inspect-audience
on:
workflow_dispatch:
permissions:
id-token: write
contents: read
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- name: Show claims for the default AWS audience
run: |
TOKEN=$(curl -sS \
-H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=sts.amazonaws.com" | jq -r .value)
echo "$TOKEN" | python3 -c '
import sys, base64, json
payload = sys.stdin.read().strip().split(".")[1]
payload += "=" * (-len(payload) % 4)
claims = json.loads(base64.urlsafe_b64decode(payload))
print(json.dumps({k: claims[k] for k in ("iss", "aud", "sub")}, indent=2))
'Run it from the main branch and the output looks like this:
{
"iss": "https://token.actions.githubusercontent.com",
"aud": "sts.amazonaws.com",
"sub": "repo:example-org/example-repo:ref:refs/heads/main"
}Remove &audience=sts.amazonaws.com from the URL and run it again. The aud value becomes the owner URL, https://github.com/example-org. A role whose trust policy requires sts.amazonaws.com rejects that token. This is the behavior you want, and it is a quick way to confirm that your condition actually does something.
Assuming the role with the default audience
In the normal case you do not touch the audience at all. The configure-aws-credentials action requests a token with the right audience, calls STS, and exports temporary credentials:
permissions:
id-token: write
contents: read
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/example-deploy-role
aws-region: us-east-1If your trust policy requires a different audience, for example because your organization uses a custom string, set the action's audience input to match. The workflow value and the trust policy value must be identical, including case.
Common audience mistakes
| Mistake | Effect | Fix |
|---|---|---|
| No `aud` condition in the trust policy | The role accepts a token minted for any audience if `sub` matches | Add `StringEquals` on `token.actions.githubusercontent.com:aud` |
| `StringLike` with `*` on `aud` | Any audience matches, same as having no check | Use `StringEquals` with the exact value |
| Audience in the policy and the workflow differ | `AssumeRoleWithWebIdentity` fails with an access denied error | Make both strings identical |
| `sts.amazonaws.com` missing from the provider's client ID list | STS rejects the token before it reads the trust policy | Add the client ID to the IAM OIDC provider |
| Reusing a custom audience across unrelated services | A token meant for one service works on another | Use one audience per consuming service |
Debugging a rejected token
When AssumeRoleWithWebIdentity fails, the error says the identity is not authorized, and it does not say which condition failed. Work through the claims in order:
- Print
iss,aud, andsubwith the workflow above. - Compare
audwith the audience your trust policy and the provider's client ID list expect. - Compare
subwith the policy character by character. - Check the AWS CloudTrail event for
AssumeRoleWithWebIdentity. It records the failure and the request context.
Most failures at this layer come from a mismatched audience string or a missing id-token: write permission, so check those first.
Summary of the rules
- Always set
token.actions.githubusercontent.com:audwithStringEquals. - Use the value
sts.amazonaws.comunless you have a documented reason to differ. - Keep
sts.amazonaws.comin the IAM OIDC provider's client ID list. - Put
audandsubin the same condition block so both must match. - Verify behavior by decoding a real token and by trying a token with the wrong audience.
Exercise
A teammate writes a trust policy for example-deploy-role with a StringEquals condition on token.actions.githubusercontent.com:sub set to repo:example-org/example-repo:ref:refs/heads/main, and no aud condition. The IAM OIDC provider lists sts.amazonaws.com as its client ID. They argue the policy is safe because the provider already checks the audience. Explain what is wrong with that argument, and write the condition block you would use instead.
Show answer
The provider's client ID list is an account-wide check. It applies to every role that trusts the provider and says nothing about what this particular role expects, and the list can change later for other uses. A role-level aud condition keeps the role safe even if the list grows, and it documents the intended audience in the policy itself. The condition block should be: "Condition": { "StringEquals": { "token.actions.githubusercontent.com:aud": "sts.amazonaws.com", "token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/main" } }. Both keys sit in the same StringEquals block so that both must match.
Exercise
You run the inspection workflow once with &audience=sts.amazonaws.com in the token request URL and once without it. What aud value do you expect in each run for a repository owned by example-org, and which run can assume a role whose trust policy requires sts.amazonaws.com?
Show answer
With the parameter, aud is sts.amazonaws.com, and that run can assume the role. Without it, aud is the owner URL https://github.com/example-org, so the aud condition fails and STS denies the request.