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.java index 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. + * + *
{@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.java
index 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 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());
+ }
+
@Test
void deletePackageRemovesEveryVersionAndAssetButLeavesOtherPackagesAlone() {
service.createDomain(REGION, "dom", null, Map.of());