ECR Cross-Account Pull Policies Without Source Conditions
Overview
An ECR repository policy grants pull access to an entire trusted AWS account rather than one specific role, so any principal that account later trusts inherits the same rights, and standard IAM boundaries on the registry side never see the grant to intervene.
At a glance
- Symptom
- Any IAM principal within the trusted account, including later-added cross-account or third-party roles, calls BatchGetImage against the shared repository.
- Likely cause
- ECR repository policy grants pull permissions to an entire trusted account root ARN without aws:SourceArn or aws:PrincipalOrgID conditions.
- Impact
- Every role the trusted account later assumes inherits pull access to the full repository, exposing baked-in secrets in image layers to unintended principals.
- Verification signal
- Re-run the detection test and a controlled negative-path test.
- Safe correction
- Scope the repository policy Principal to a specific role ARN and enforce aws:SourceArn and aws:PrincipalOrgID conditions on the grant.
- Rollback or recovery
- Restore the exported configuration if the new control blocks required production traffic, then narrow the policy before redeployment.
The Trap
A cross-account ECR repository policy that grants ecr:GetDownloadUrlForLayer, ecr:BatchGetImage, and ecr:BatchCheckLayerAvailability to an external AWS account root ARN with no aws:PrincipalArn or aws:PrincipalOrgID condition attached.
The Default State
AWS’s own cross-account sharing examples, and most aws_ecr_repository_policy Terraform modules copied from them, set the Principal to {"AWS": "arn:aws:iam::<account-id>:root"} and stop there. Policies built in the console’s Permissions tab often end up the same way, so teams sharing a base image with a partner account or a separate CI account tick the box for “grant access” and never add a condition block restricting which role inside that account is doing the pulling.
The Blast Radius
Every IAM principal inside the trusted account inherits pull rights to the entire repository, not just the pipeline role you intended to authorise. That includes roles the account later assumes for third-party SaaS integrations, cross-account CI runners, or any Lambda execution role a developer spins up for testing.
ECR API calls, including the BatchGetImage and GetDownloadUrlForLayer calls behind a pull, are recorded in CloudTrail, but unless someone alerts on unexpected callers, a pull by an unintended principal is easy to miss.
Images built carelessly can carry secrets in their layers — a token passed as a Dockerfile ARG and kept in the image history, an .npmrc file copied in one layer and deleted in a later one, or credentials copied into the final stage — and these pull rights hand over the manifest and every layer, so any principal in the trusted account can extract them.
The Lead Mechanic Fix
Scope the resource policy to specific calling identities. set-repository-policy replaces the entire policy rather than merging, so first save the current one for rollback (aws ecr get-repository-policy --repository-name prod-api --query policyText --output text > policy-backup.json) and build policy.json from it, carrying over every statement you still need:
aws ecr set-repository-policy --repository-name prod-api --policy-text file://policy.json
Where policy.json names the pulling role itself as the Principal:
{"Version": "2012-10-17", "Statement": [{"Sid": "AllowCiPull", "Effect": "Allow", "Principal": {"AWS": "arn:aws:iam::<account>:role/ci-pull-role"}, "Action": ["ecr:BatchGetImage", "ecr:GetDownloadUrlForLayer", "ecr:BatchCheckLayerAvailability"]}]}
An aws:PrincipalOrgID condition only matches accounts inside your organisation, so add it only when the pulling account is a member; for a partner account the role ARN Principal does the scoping. To roll back, run the same set-repository-policy command with --policy-text file://policy-backup.json. Then confirm the intended role can pull and another role cannot; the pulling role also needs ecr:GetAuthorizationToken in its own IAM policy.
Alert on any BatchGetImage management event whose caller is not a pipeline role, matching the role name inside userIdentity.arn, which for a role is an assumed-role session ARN, not the IAM role ARN.
Related reading
- AWS Cross-Account AssumeRole Is Denied by a Mismatched Trust Policy
- Service Control Policies Never Restrict the AWS Organizations Management Account
- An AWS Principal Field Left as an Account ARN Silently Grants Every Role in That Account
- ECR Scan-on-Push Leaves Old Container Images Permanently Unscanned
Apply the safer control
Before you change production
Confirm the affected scope, export the current configuration, and test the replacement control in a non-production environment first.
Fix commands and configuration
set-repository-policyaws ecr get-repository-policy --repository-name prod-api --query policyText --output text > policy-backup.jsonpolicy.jsonVerify, roll back or escalate
Verify
Re-run the detection test and a controlled negative-path test. Confirm the unsafe behaviour is blocked while approved traffic still succeeds.
Rollback
Restore the exported configuration if the new control blocks required production traffic, then narrow the policy before redeployment.
Escalate
Escalate when the blast radius is uncertain, the control cannot be tested safely, or remediation requires an outage or security exception.
Fixed? Get the next one before it hits you: The KBY Brief
Practical fixes for the systems you run, by email about twice a week. Unsubscribe at any time.
About this page
Credited to
Dates
Published
Last updated
Corrections
- : Corrected during a review of published articles. Details
Was this useful?
Reports go to the site owner and are used to correct pages. Corrections are listed at /corrections.
