Sub topic 7
Exact matching, wildcards, and why the choice matters
This chapter covers how IAM compares the token's sub claim to the value in your trust policy. It assumes you know what a JSON Web Token is, can read GitHub Actions workflow YAML, and have written a basic IAM role trust policy. The earlier chapters introduced the sub claim and the role trust policy. Here the focus is on the condition operator you choose, because that choice decides who can assume the role.
Why the operator is the security decision
A trust policy for OIDC federation has two parts that matter. The Principal names the identity provider, and the Condition block decides which tokens from that provider are accepted. GitHub signs tokens for every repository on GitHub. The provider is shared by all of them. If your conditions are loose, a token from someone else's repository can satisfy them.
The operator controls how the condition compares strings. Two operators cover almost every case:
StringEqualsrequires the claim to match the policy value exactly, character for character, and case-sensitively.StringLiketreats*as "zero or more of any character" and?as "exactly one character".
Both are case-sensitive. A * in a StringLike value can match /, : and any other character, so it is not limited to one path segment.
Exact matching with StringEquals
Exact matching is the safest default. This is the fragment from the branch-restricted policy:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/main"
}
}
}Only a token whose sub is exactly repo:example-org/example-repo:ref:refs/heads/main passes. A push to main in example-org/example-repo produces that value. Each of the following fails:
| Token `sub` value | Passes? | Reason |
|---|---|---|
| `repo:example-org/example-repo:ref:refs/heads/main` | Yes | Identical string |
| `repo:example-org/example-repo:ref:refs/heads/develop` | No | Different branch |
| `repo:example-org/example-repo:pull_request` | No | Pull request subject |
| `repo:example-org/other-repo:ref:refs/heads/main` | No | Different repository |
| `repo:example-org/example-repo:environment:production` | No | Environment subject |
When several conditions sit under one operator, all of them must pass. Here aud and sub must both match.
If you need to allow more than one exact value, give the key a list. IAM treats the list as "any of these":
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": [
"repo:example-org/example-repo:ref:refs/heads/main",
"repo:example-org/example-repo:ref:refs/heads/hotfix"
]
}
}
}A list keeps matching exact while allowing a small, named set. It is usually better than a wildcard when you can enumerate the values.
Wildcards with StringLike
Sometimes the set of values is open-ended. Release branches such as release/1.0 and release/2.0 are created over time. StringLike lets you accept all of them:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/release/*"
}
}
}The wildcard begins after release/. Everything before it is still exact, so the repository and the release/ prefix are fixed. Anyone who can push a branch named release/anything in that repository can now get the role. That is the cost of the wildcard: the trust boundary moves from "this branch" to "whoever can create a branch with this prefix".
Note that aud stays under StringEquals. Use the strictest operator that fits each key. Mixing operators in one Condition block is normal, and all operators must pass.
What a wildcard really widens
A wildcard widens trust along the dimension where you put it. The position tells you the risk.
| Pattern | What it accepts | Risk |
|---|---|---|
| `repo:example-org/example-repo:ref:refs/heads/main` | One branch in one repository | Lowest |
| `repo:example-org/example-repo:ref:refs/heads/release/*` | Any `release/` branch in one repository | Depends on who can create branches |
| `repo:example-org/example-repo:*` | Any branch, tag, pull request, or environment in one repository | High. Includes pull request jobs |
| `repo:example-org/*` | Every repository in the organization | Very high |
| `repo:example-org/example-repo*` | Repositories whose names begin with `example-repo` | Easy to overlook |
| `*` | Any token from the provider | Complete exposure |
The last two rows deserve attention. A pattern such as repo:example-org/example-repo* has no : or / after the repository name. It matches example-org/example-repo-fork-test and example-org/example-repo-sandbox too. If someone creates a repository with a name that starts with yours, its workflows satisfy the condition. End the repository part with a colon, as in repo:example-org/example-repo:*, so the name has to end where you expect.
The pattern repo:example-org/example-repo:* is especially tempting because it "just works" for every workflow. It also accepts the pull_request subject. Any contributor who can open a pull request that runs a workflow with id-token: write can then request credentials for that role. Whether a pull request from a fork can do this depends on your repository's settings for fork workflows, but you should not rely on that setting alone for the role boundary.
The same value under two operators
The choice of operator changes the meaning of identical-looking text. Suppose the policy value is repo:example-org/example-repo:environment:production.
- Under
StringEquals, only that exact subject matches. - Under
StringLike, it still matches only that subject, because the value contains no*or?. AStringLikewithout wildcards behaves like an exact match.
The reverse is the dangerous case. If a value contains a literal * and you use StringEquals, IAM looks for an actual asterisk in the claim and nothing matches. The role then stays closed, and the workflow fails with Not authorized to perform sts:AssumeRoleWithWebIdentity. This is a safe failure, but it often leads people to switch to StringLike to make the error go away. Before switching, ask whether you want a pattern at all, or whether an exact value or a list would work.
Choosing between them
Use these criteria in order:
- Can you name every allowed value? Use
StringEquals, with a list if there is more than one. - Is the set open-ended but under a naming convention you control, such as
release/*? UseStringLike, keep the prefix as long as possible, and confirm who can create branches with that prefix. Branch protection or rulesets that restrict creation of matching branch names reduce the risk. - Would the pattern cross a repository boundary or a subject type (branch, pull request, environment)? Do not do it. Create a separate role instead.
Prefer several narrow roles over one broad role. A role used only for deployments to production and a role used only for read-only checks on pull requests each get a short, exact policy. Compromise of the weaker workflow then does not reach the stronger role.
Also consider what the role can do once assumed. A wildcard in the trust policy is more tolerable on a role with read-only permissions to a test bucket than on an administrator role. The trust policy and the permissions policy together define the real exposure.
Checking a pattern before you ship it
You can test how a pattern behaves by comparing sample subjects against it. This short script mimics IAM's StringLike semantics, where * matches any run of characters and ? matches one character. It is a local aid, not a replacement for testing against AWS.
from fnmatch import fnmatchcase
pattern = "repo:example-org/example-repo*"
subjects = [
"repo:example-org/example-repo:ref:refs/heads/main",
"repo:example-org/example-repo-sandbox:ref:refs/heads/main",
"repo:example-org/other-repo:ref:refs/heads/main",
]
for subject in subjects:
print(fnmatchcase(subject, pattern), subject)Running it prints True for the first two subjects and False for the third. The second result shows the unintended match on a similarly named repository. Change the pattern to repo:example-org/example-repo:* and run it again: the sandbox subject now returns False.
fnmatchcase also treats [ as a special character, which IAM does not, so avoid square brackets in test patterns.
Exercise
A trust policy uses StringLike on sub with the value repo:example-org/payments*. The team intends to trust only the payments repository. Name one risk, and rewrite the condition so that it trusts only pushes to main in payments.
Show answer
The pattern has no separator after payments, so it also matches repositories such as payments-sandbox or payments-demo in the same organization, and it matches any branch, pull request, tag, or environment subject. The fix is to use StringEquals with the exact value repo:example-org/payments:ref:refs/heads/main, keeping the aud check of sts.amazonaws.com alongside it.
Exercise
A role must be assumable from main and from any branch under release/ in example-org/example-repo. Write the sub conditions, using the narrowest operators that work.
Show answer
Use StringEquals for aud and for the exact main subject, and StringLike for the release pattern. Because IAM requires every operator block to pass, the two sub values cannot sit in separate operator blocks. Put both values in one StringLike list instead: ["repo:example-org/example-repo:ref:refs/heads/main", "repo:example-org/example-repo:ref:refs/heads/release/*"], with aud under StringEquals. The main entry has no wildcard, so StringLike treats it as an exact match.