fix(serve): validate ECR registry host before docker login - #6160
fix(serve): validate ECR registry host before docker login #6160mohamedzeidan2021 wants to merge 2 commits into
Conversation
…ntainerMode
_is_ecr_image() classified an image as ECR by substring-matching
".dkr.ecr." and ".amazonaws.com" anywhere in the URI, while _pull_image()
took the docker login target from image.split("/")[0]. The two used
inconsistent values, so a crafted URI such as
attacker.com/x.dkr.ecr.<region>.amazonaws.com/repo passed the classifier
yet caused "docker login -u AWS -p <ECR_TOKEN> attacker.com", leaking a
valid, replayable ECR authorization token to an attacker-controlled host.
Parse the registry host first and validate it against a strict ECR
endpoint pattern, and reuse that same validated host for docker login, so
the classifier and the login target can never disagree. Add unit tests
covering valid ECR (incl. China partition), attacker-crafted, and public
image URIs.
Add a consumption-time guard for image URIs sourced from a (potentially untrusted) hub document, complementing the sink-side validation in LocalContainerMode._pull_image. An attacker-writable hub could publish an EcrAddress such as attacker.com/x.dkr.ecr.<region>.amazonaws.com/repo that looks like ECR under the historical substring classifier while its real registry host is attacker-controlled. validate_hub_ecr_address() rejects exactly that parser-confusion signature -- a URI carrying the ECR-like substrings whose registry host is not a valid ECR endpoint -- at the point EcrAddress / init_kwargs.image_uri is read from the hub document, before it can propagate to docker login. It fails closed ONLY on the attack signature: legitimate non-ECR images (public/DockerHub) and ECR hosts in partitions the strict pattern does not enumerate (ISO c2s.ic.gov, FIPS ecr-fips) pass through untouched rather than raising, so a narrow regex cannot turn into an outage. The strict ECR host pattern (ECR_HOST_RE) is promoted to check_image_uri as the single source of truth and reused by LocalContainerMode, so the classifier, the docker-login target extractor, and the hub-consumption guard all operate on the same value. Add unit tests covering valid ECR (standard + China), attacker-spoofed host, ECR substrings in the repo path, public images, ISO/FIPS pass-through, and the empty-URI no-op.
|
Claude finished @mohamedzeidan2021's task in 3m 49s —— View job Code Review — PR #6160 (ECR host validation / parser-confusion hardening)
Overall this is a well-scoped, well-documented security fix. The core idea — deriving both the "is this ECR?" classifier and the 1. Regex anchor
|
Tightens how LocalContainerMode identifies and handles ECR image URIs.
Previously the ECR classifier and the registry-host extraction used different views of the image string,
which could disagree for unusual/malformed URIs. This parses the registry host once, validates it against
a strict ECR endpoint pattern, and reuses that same value consistently.
Also adds a lightweight validation of hub-sourced image URIs at consumption time so malformed addresses
are caught early with a clear error, rather than surfacing as a confusing failure later in the flow.
Legitimate non-ECR images and other-partition ECR hosts (e.g. ISO/FIPS endpoints) pass through unchanged.
ECR_HOST_RE is consolidated into check_image_uri as a single source of truth. Adds unit tests covering
standard/China ECR URIs, malformed URIs, ISO/FIPS pass-through, and public images.
Issue #, if available:
Description of changes:
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.