fix(codeartifact): escape the namespace/package boundary in storage keys (#5408)

packageKey and packageVersionKey joined namespace and package name with a
plain "::", but namespace/package tokens only reject '#', '/', and
whitespace, so ':' is legal in either. Namespace "a::b" + package "c" and
namespace "a" + package "b::c" are two different, independently valid
packages that produced the identical storage key, letting one's origin
configuration or published version answer for the other's.

Escapes ':' and '\' in the namespace/package/version segments before they
go into the key, using '#' as the escape introducer rather than '\'
itself. '#' is one of the three characters namespace/package/version can
never legally contain (AWS's own PackageName/PackageNamespace wire
pattern forbids it, same as Floci's own validation), so an escaped
segment can never equal a pre-existing raw key that happened to contain a
literal backslash, closing the same cross-contamination risk at the
old/new boundary that escaping with '\' as the introducer would have
reopened there. For any package whose namespace, name, or version never
contains a backslash or colon, the overwhelming majority, the escaped
segment is identical to the raw one, so this produces the exact same key
as before: no migration of already-persisted data is needed for that
common case.

Follow-up from the PutPackageOriginConfiguration review on #5381.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-11 09:12:41 -05:00
1 parent d884d55c26
commit 7fa1a506c7
2 files changed
+169 -4

No files matched your search

@@ -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) {