mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(partition): hold escaped host regexes to the DNS suffix gate (#5232)
The partition literal gate's dns-suffix pattern matched amazonaws.com but not a Java regex that escapes its dots (amazonaws\\.com), so hand-written host patterns escaped it. With the dots allowed to be escaped, the gate found two on main: - ECR's pull-through cache recognised an ecr upstream only on the commercial and China suffixes, while Floci's own registry recognises an ECR image URI in every partition. The upstream host pattern is now built from AwsPartitions.dnsSuffixRegex() the same way. - The console sign-in host pattern in SigninService has no published non-commercial form, so it carries a partition-literal escape like the console literals beside it.
This commit is contained in:
1 parent
a0a2a5f0d6
commit
1b61945f28
5 files changed
+40
-3
No files matched your search
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.ecr;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsPartitions;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
@@ -40,6 +41,13 @@ import java.util.regex.Pattern;
|
||||
@ApplicationScoped
|
||||
public class EcrService implements ResourceProvider {
|
||||
|
||||
/**
|
||||
* A private ECR registry host in any partition, {@code <account>.dkr.ecr.<region>.<dnsSuffix>},
|
||||
* matched the same way {@code EcrRegistryManager} recognises an ECR image URI.
|
||||
*/
|
||||
private static final Pattern ECR_UPSTREAM_HOST = Pattern.compile(
|
||||
"[0-9]{12}\\.dkr\\.ecr\\.[a-z0-9-]+\\." + AwsPartitions.dnsSuffixRegex());
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(EcrService.class);
|
||||
private static final Pattern REPO_NAME = Pattern.compile(
|
||||
"(?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)*[a-z0-9]+(?:[._-][a-z0-9]+)*");
|
||||
@@ -1023,7 +1031,7 @@ public class EcrService implements ResourceProvider {
|
||||
if (host.equals("cgr.dev")) {
|
||||
return "chainguard";
|
||||
}
|
||||
if (host.matches("[0-9]{12}\\.dkr\\.ecr\\.[a-z0-9-]+\\.amazonaws\\.com(?:\\.cn)?")) {
|
||||
if (ECR_UPSTREAM_HOST.matcher(host).matches()) {
|
||||
return "ecr";
|
||||
}
|
||||
throw unsupportedUpstreamRegistry(upstreamRegistryUrl);
|
||||
|
||||
@@ -50,7 +50,7 @@ public class SigninService {
|
||||
private static final int MAX_STATE_LENGTH = 128;
|
||||
private static final Pattern PKCE_VALUE_PATTERN = Pattern.compile("[A-Za-z0-9._~-]{43,128}");
|
||||
private static final Pattern AWS_SIGNIN_HOST_PATTERN =
|
||||
Pattern.compile("[a-z]{2}-[a-z-]+-\\d+\\.signin\\.aws\\.amazon\\.com");
|
||||
Pattern.compile("[a-z]{2}-[a-z-]+-\\d+\\.signin\\.aws\\.amazon\\.com"); // partition-literal: console sign-in host, non-commercial host has no published source
|
||||
|
||||
private final IamService iamService;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@@ -227,6 +227,29 @@ class EcrServiceTest {
|
||||
"missing/cache", null, null, null, REGION)).getErrorCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* An ECR upstream is recognised in every partition, as Floci's own registry recognises an ECR
|
||||
* image URI: the host pattern once accepted only the commercial and China suffixes.
|
||||
*/
|
||||
@Test
|
||||
void pullThroughCacheRule_acceptsAnEcrUpstreamInEveryPartition() {
|
||||
String[] hosts = {
|
||||
"123456789012.dkr.ecr.us-east-1.amazonaws.com",
|
||||
"123456789012.dkr.ecr.cn-north-1.amazonaws.com.cn",
|
||||
"123456789012.dkr.ecr.us-gov-west-1.amazonaws.com",
|
||||
"123456789012.dkr.ecr.us-iso-east-1.c2s.ic.gov",
|
||||
"123456789012.dkr.ecr.eusc-de-east-1.amazonaws.eu",
|
||||
};
|
||||
for (int i = 0; i < hosts.length; i++) {
|
||||
PullThroughCacheRule rule = service.createPullThroughCacheRule("ecr-up-" + i, hosts[i], null,
|
||||
null, null, null, null, REGION);
|
||||
assertEquals("ecr", rule.getUpstreamRegistry(), hosts[i]);
|
||||
}
|
||||
assertEquals("UnsupportedUpstreamRegistryException", assertThrows(AwsException.class,
|
||||
() -> service.createPullThroughCacheRule("ecr-up-bad", "123456789012.dkr.ecr.us-east-1.example.com",
|
||||
null, null, null, null, null, REGION)).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void pullThroughCacheRule_rejectsInvalidInputs() {
|
||||
assertEquals("InvalidParameterException", assertThrows(AwsException.class,
|
||||
|
||||
@@ -81,7 +81,9 @@ CATEGORIES: tuple[Category, ...] = (
|
||||
),
|
||||
Category(
|
||||
"dns-suffix",
|
||||
re.compile(r"amazonaws\.com(?!\.cn)|cloudfront\.net|signin\.aws|public\.ecr\.aws"),
|
||||
# A dot may be escaped (amazonaws\\.com in a Java regex), so a hand-written host pattern
|
||||
# is held to the same rule as a host.
|
||||
re.compile(r"amazonaws\\*\.com(?!\\*\.cn)|cloudfront\\*\.net|signin\\*\.aws|public\\*\.ecr\\*\.aws"),
|
||||
True,
|
||||
"a commercial DNS suffix or host; derive from the partition's dnsSuffix",
|
||||
),
|
||||
|
||||
@@ -116,6 +116,10 @@ def test_arn_regex_dialect_does_not_match_a_plain_literal():
|
||||
("public.ecr.aws/lambda/java", True),
|
||||
("ec2.cn-north-1.amazonaws.com.cn", False),
|
||||
("c2s.ic.gov", False),
|
||||
# A host written as a Java regex, its dots escaped, is held to the same rule.
|
||||
("[0-9]{12}\\\\.dkr\\\\.ecr\\\\.[a-z0-9-]+\\\\.amazonaws\\\\.com", True),
|
||||
("[a-z]+\\\\.cloudfront\\\\.net", True),
|
||||
("ecr\\\\.[a-z0-9-]+\\\\.amazonaws\\\\.com\\\\.cn", False),
|
||||
])
|
||||
def test_dns_suffix_matches_commercial_hosts_only(text, expected):
|
||||
assert ("dns-suffix" in categories_of(text)) is expected
|
||||
|
||||
Reference in new issue
Block a user