Light, fluffy, and always free - The AWS Local Emulator alternative
fix(codeartifact): escape the namespace/package boundary in storage keys (#5408)
Priamo Germosen7fa1a506c766
diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.javaindex 89bbadd986..07f2d0761c 100644--- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java+++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java@@ -685,6 +685,11 @@ public class CodeArtifactService implements Resettable {requireNonBlank(repository, "repository");requireNonBlank(packageName, "package");requireNonBlank(version, "packageVersion");+ validatePackageToken("package", packageName);+ validatePackageToken("packageVersion", version);+ if (namespace != null) {+ validatePackageToken("namespace", namespace);+ }String owner = effectiveOwner(domainOwner);String key = packageVersionKey(region, domain, repository, format, namespace, packageName, version);return packageVersions.getForAccount(owner, key)@@ -934,6 +939,11 @@ public class CodeArtifactService implements Resettable {requireNonBlank(packageName, "package");requireNonBlank(version, "packageVersion");requireNonBlank(assetName, "asset");+ validatePackageToken("package", packageName);+ validatePackageToken("packageVersion", version);+ if (namespace != null) {+ validatePackageToken("namespace", namespace);+ }String owner = effectiveOwner(domainOwner);// Maven, npm, and pypi are metadata-free passthroughs straight to their own sidecar@@ -1509,13 +1519,57 @@ public class CodeArtifactService implements Resettable {private static String packageVersionKey(String region, String domain, String repository, String format,String namespace, String packageName, String version) {return region + "::" + domain + "::" + repository + "::" + format + "::"- + (namespace == null ? "" : namespace) + "::" + packageName + "::" + version;+ + escapeKeySegment(namespace) + "::" + escapeKeySegment(packageName) + "::"+ + escapeKeySegment(version);}private static String packageKey(String region, String domain, String repository, String format,String namespace, String packageName) {return region + "::" + domain + "::" + repository + "::" + format + "::"- + (namespace == null ? "" : namespace) + "::" + packageName;+ + escapeKeySegment(namespace) + "::" + escapeKeySegment(packageName);+ }++ /**+ * Escapes a namespace/package/version segment before it goes into a {@code ::}-joined storage+ * key. {@code region}/{@code domain}/{@code repository}/{@code format} never need this: they're+ * each validated against a pattern that already excludes {@code :}. Namespace, package name, and+ * version are different, {@link #PACKAGE_TOKEN} only excludes {@code #}, {@code /}, and+ * whitespace, so {@code ::} itself is a legal character sequence in any of them. Without+ * escaping, namespace {@code "a::b"} + package {@code "c"} and namespace {@code "a"} + package+ * {@code "b::c"} join to the identical key, so one package's stored origin configuration (or+ * published version) silently answers for the other.+ *+ * <p>{@code #} is the escape introducer, not {@code \}: every {@code \} becomes {@code #b} and+ * every {@code :} becomes {@code #c}. {@code #} is itself one of the three characters+ * {@link #PACKAGE_TOKEN} (and AWS's own {@code PackageName}/{@code PackageNamespace} wire+ * pattern) forbids, so no namespace/package/version ever validated through this service, before+ * or after this method existed, can contain one. An escaped segment therefore never collides+ * with a pre-existing stored key built from a segment that happened to contain a literal+ * backslash: that old raw key cannot contain {@code #}, and this method's output only does when+ * it actually escaped something. For the overwhelming majority of packages, whose+ * namespace/name/version never contain a backslash or colon, this returns the input unchanged,+ * so the key built from those segments is byte-identical to the one built before this method+ * existed.+ */+ private static String escapeKeySegment(String value) {+ if (value == null) {+ return "";+ }+ if (value.indexOf(':') < 0 && value.indexOf('\\') < 0) {+ return value;+ }+ StringBuilder escaped = new StringBuilder(value.length() + 4);+ for (int i = 0; i < value.length(); i++) {+ char c = value.charAt(i);+ if (c == '\\') {+ escaped.append("#b");+ } else if (c == ':') {+ escaped.append("#c");+ } else {+ escaped.append(c);+ }+ }+ return escaped.toString();}private static AwsException validation(String message) {diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactServiceTest.javaindex a821c8a9c0..de66ffc4f5 100644--- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactServiceTest.java@@ -54,6 +54,7 @@ class CodeArtifactServiceTest {private CodeArtifactService service;private RegionResolver regionResolver;private AccountAwareStorageBackend<CodeArtifactRepository> repoStore;+ private AccountAwareStorageBackend<PackageOriginConfig> packageOriginConfigStore;private VerdaccioSidecarClient verdaccioClient;private ReposiliteSidecarClient reposiliteClient;private PypiserverSidecarClient pypiserverClient;@@ -64,8 +65,7 @@ class CodeArtifactServiceTest {repoStore = AccountAwareStorageBackend.inMemory(ACCOUNT_ID);AccountAwareStorageBackend<CodeArtifactPackageVersion> packageVersionStore =AccountAwareStorageBackend.inMemory(ACCOUNT_ID);- AccountAwareStorageBackend<PackageOriginConfig> packageOriginConfigStore =- AccountAwareStorageBackend.inMemory(ACCOUNT_ID);+ packageOriginConfigStore = AccountAwareStorageBackend.inMemory(ACCOUNT_ID);regionResolver = mock(RegionResolver.class);when(regionResolver.getAccountId()).thenReturn(ACCOUNT_ID);@@ -1296,6 +1296,117 @@ class CodeArtifactServiceTest {assertEquals("Published", published.packageVersion().getStatus());}+ /**+ * Namespace {@code "a::b"} + package {@code "c"} and namespace {@code "a"} + package+ * {@code "b::c"} are two different, independently valid package coordinates, {@code ::} is not+ * one of the characters namespace/package names reject, so a storage key that joined them with+ * a plain {@code ::} delimiter without escaping would see the two as the same package and let+ * one's origin configuration answer for the other's.+ */+ @Test+ void putPackageOriginConfigurationDoesNotCollideAcrossTheNamespacePackageBoundary() {+ service.createDomain(REGION, "dom", null, Map.of());+ service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());++ service.putPackageOriginConfiguration(REGION, "dom", null, "repo", "generic", "a::b", "c", "BLOCK", "ALLOW");+ service.putPackageOriginConfiguration(REGION, "dom", null, "repo", "generic", "a", "b::c", "ALLOW", "BLOCK");++ CodeArtifactService.PackageDescription first = service.describePackage(REGION, "dom", null, "repo",+ "generic", "a::b", "c");+ CodeArtifactService.PackageDescription second = service.describePackage(REGION, "dom", null, "repo",+ "generic", "a", "b::c");+ assertEquals("BLOCK", first.publishRestriction());+ assertEquals("ALLOW", first.upstreamRestriction());+ assertEquals("ALLOW", second.publishRestriction());+ assertEquals("BLOCK", second.upstreamRestriction());+ }++ /**+ * Same coordinate-boundary collision as above, but for published version content rather than+ * origin configuration: without escaping, the second publish's bytes would be stored under the+ * same key as the first's, so one package's asset would overwrite or answer for the other's.+ */+ @Test+ void publishPackageVersionDoesNotCollideAcrossTheNamespacePackageBoundary() {+ service.createDomain(REGION, "dom", null, Map.of());+ service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());+ byte[] firstContent = "first-package".getBytes(StandardCharsets.UTF_8);+ byte[] secondContent = "second-package".getBytes(StandardCharsets.UTF_8);++ service.publishPackageVersion(REGION, "dom", null, "repo", "generic", "a::b", "c", "1.0.0", "a.txt",+ sha256Hex(firstContent), "false", firstContent);+ service.publishPackageVersion(REGION, "dom", null, "repo", "generic", "a", "b::c", "1.0.0", "a.txt",+ sha256Hex(secondContent), "false", secondContent);++ PackageVersionAssetResult first = service.getPackageVersionAsset(REGION, "dom", null, "repo", "generic",+ "a::b", "c", "1.0.0", "a.txt", null);+ PackageVersionAssetResult second = service.getPackageVersionAsset(REGION, "dom", null, "repo", "generic",+ "a", "b::c", "1.0.0", "a.txt", null);+ assertEquals("first-package", new String(first.asset().getContent(), StandardCharsets.UTF_8));+ assertEquals("second-package", new String(second.asset().getContent(), StandardCharsets.UTF_8));+ }++ /**+ * A build before escaping existed stored namespace/package segments raw, so one could already+ * contain a literal backslash followed by {@code c}, the exact two characters escaping a colon+ * now produces. If escaping used {@code \} as its own introducer, that old raw record would+ * become indistinguishable from the new escaped key of a completely different namespace+ * ("a:b" instead of "a\cb"). Escaping uses {@code #} instead, a character namespace/package+ * tokens can never contain (AWS's own wire pattern forbids it the same as Floci's), so the new+ * coordinate's escaped key can never equal that old raw one.+ *+ * <p>Asserting only the new coordinate's own read-back is not enough: that passes whether or+ * not the two keys actually collided, since the put and the read-back always agree with+ * whatever was last written under the key they both compute. The legacy record has to be read+ * back too, straight from the store under its own raw key, to prove the put never touched it.+ */+ @Test+ void newEscapedKeysNeverCollideWithAPreExistingRawKeyThatHappenedToContainABackslash() {+ service.createDomain(REGION, "dom", null, Map.of());+ service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());+ String legacyRawKey = REGION + "::dom::repo::generic::a\\cb::x";+ packageOriginConfigStore.putForAccount(ACCOUNT_ID, legacyRawKey, new PackageOriginConfig("BLOCK", "BLOCK"));++ service.putPackageOriginConfiguration(REGION, "dom", null, "repo", "generic", "a:b", "x", "ALLOW", "ALLOW");++ CodeArtifactService.PackageDescription described = service.describePackage(REGION, "dom", null, "repo",+ "generic", "a:b", "x");+ assertEquals("ALLOW", described.publishRestriction());+ assertEquals("ALLOW", described.upstreamRestriction());++ PackageOriginConfig legacy = packageOriginConfigStore.getForAccount(ACCOUNT_ID, legacyRawKey).orElseThrow();+ assertEquals("BLOCK", legacy.getPublishRestriction());+ assertEquals("BLOCK", legacy.getUpstreamRestriction());+ }++ /**+ * {@code #} is the escape introducer, so a namespace that already contains it reaches+ * {@link CodeArtifactService#escapeKeySegment} unchanged (there's no {@code :} or {@code \} in+ * it to escape) and can spell out another coordinate's already-escaped key directly: namespace+ * {@code "a#cb"} builds the identical key as the escaped form of namespace {@code "a:b"}.+ * {@code describePackage}/{@code putPackageOriginConfiguration} already reject {@code #} via+ * {@code validatePackageCoordinates}, but {@code describePackageVersion} and+ * {@code getPackageVersionAsset} used to only check for blank, reaching the shared key-building+ * code with an unvalidated namespace. Both now validate the same token rule before building a+ * key, so this is rejected rather than silently answering for a different package.+ */+ @Test+ void readPathsRejectANamespaceThatSpellsOutAnotherCoordinatesEscapedKey() {+ service.createDomain(REGION, "dom", null, Map.of());+ service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());+ byte[] content = "victim".getBytes(StandardCharsets.UTF_8);+ service.publishPackageVersion(REGION, "dom", null, "repo", "generic", "a:b", "x", "1.0.0", "a.txt",+ sha256Hex(content), "false", content);++ AwsException describeVersion = assertThrows(AwsException.class, () -> service.describePackageVersion(REGION,+ "dom", null, "repo", "generic", "a#cb", "x", "1.0.0"));+ assertEquals("ValidationException", describeVersion.getErrorCode());++ AwsException getAsset = assertThrows(AwsException.class, () -> service.getPackageVersionAsset(REGION, "dom",+ null, "repo", "generic", "a#cb", "x", "1.0.0", "a.txt", null));+ assertEquals("ValidationException", getAsset.getErrorCode());+ }+@Testvoid deletePackageRemovesEveryVersionAndAssetButLeavesOtherPackagesAlone() {service.createDomain(REGION, "dom", null, Map.of());