Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(codeartifact): split the pypiserver sidecar into a manager and a client (#5281)
Priamo Germosenea77da8a2af1
diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiController.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiController.javaindex 7c2263bb36..096562c6fc 100644--- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiController.java+++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiController.java@@ -69,16 +69,16 @@ public class CodeArtifactPypiController {private static final Pattern SIMPLE_INDEX_HREF = Pattern.compile("href=\"([^\"]*)\"");private final CodeArtifactService service;- private final PypiserverSidecarManager pypiserver;+ private final PypiserverSidecarClient pypiserver;private final HttpClient httpClient;private final ConcurrentHashMap<String, Object> uploadLocks = new ConcurrentHashMap<>();@Inject- public CodeArtifactPypiController(CodeArtifactService service, PypiserverSidecarManager pypiserver) {+ public CodeArtifactPypiController(CodeArtifactService service, PypiserverSidecarClient pypiserver) {this(service, pypiserver, HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build());}- CodeArtifactPypiController(CodeArtifactService service, PypiserverSidecarManager pypiserver,+ CodeArtifactPypiController(CodeArtifactService service, PypiserverSidecarClient pypiserver,HttpClient httpClient) {this.service = service;this.pypiserver = pypiserver;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 9503775009..f78e675b0d 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@@ -676,7 +676,7 @@ public class CodeArtifactService implements Resettable {String owner = effectiveOwner(domainOwner);// Maven, npm, and pypi are metadata-free passthroughs straight to their own sidecar- // (ReposiliteSidecarClient/VerdaccioSidecarClient/PypiserverSidecarManager): publish for+ // (ReposiliteSidecarClient/VerdaccioSidecarClient/PypiserverSidecarClient): publish for// these formats never goes through publishPackageVersion (it only ever accepts "generic"),// so packageVersions can never hold a record for them. Bridge straight to the sidecar// that actually has the asset instead of always 404ing here.diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarClient.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarClient.javanew file mode 100644index 0000000000..5f12f1165f--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarClient.java@@ -0,0 +1,252 @@+package io.github.hectorvent.floci.services.codeartifact;++import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;++import java.net.URI;+import java.net.http.HttpClient;+import java.net.http.HttpRequest;+import java.net.http.HttpResponse;+import java.net.http.HttpResponse.BodyHandlers;+import java.time.Duration;+import java.util.List;+import java.util.Locale;+import java.util.Optional;+import java.util.regex.Pattern;++/**+ * The HTTP wire protocol for a pypiserver sidecar: simple-index lookups and flat package-file+ * fetches. Container lifecycle (start, readiness, stop) is {@link PypiserverSidecarManager}'s job;+ * this class asks it for a ready base URL and speaks pypiserver's own protocol against it.+ */+@ApplicationScoped+public class PypiserverSidecarClient implements RepositorySidecarManager {++ private static final Pattern SIMPLE_INDEX_HREF = Pattern.compile("href=\"([^\"]*)\"");++ private final PypiserverSidecarManager manager;+ private final HttpClient httpClient;++ @Inject+ public PypiserverSidecarClient(PypiserverSidecarManager manager) {+ this(manager, HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build());+ }++ PypiserverSidecarClient(PypiserverSidecarManager manager, HttpClient httpClient) {+ this.manager = manager;+ this.httpClient = httpClient;+ }++ @Override+ public String format() {+ return "pypi";+ }++ @Override+ public String ensureReady(String repositoryContainerId, String publicUrl) {+ return manager.ensureReady(repositoryContainerId, publicUrl);+ }++ @Override+ public void release(String repositoryContainerId) {+ manager.release(repositoryContainerId);+ }++ /**+ * PyPI packages have no namespace (AWS: "Python ... package versions do not have a+ * corresponding component"). pypiserver serves every file it holds flatly at+ * {@code /packages/<filename>} regardless of which package or version it belongs to,+ * confirmed against a live container, so both are verified before serving anything: that+ * {@code packageName}'s own simple index genuinely lists {@code assetName}, and that+ * {@code assetName} itself embeds {@code version} the way every real PyPI filename does+ * (PEP 427/517 naming) rather than trusting the caller's {@code version} argument on its own.+ *+ * <p>{@code assetName} is percent-encoded via {@link SidecarUriUtils#encodeSegment(String)}+ * before joining it to the fixed {@code /packages/} prefix: without that, a {@code /} inside+ * it would be indistinguishable from a real path separator and could splice in extra path+ * segments.+ */+ @Override+ public Optional<byte[]> fetchPackageVersionAsset(String repositoryContainerId, String domain, String repository,+ String namespace, String packageName, String version, String assetName) {+ if (!filenameEmbedsVersion(assetName, packageName, version)) {+ return Optional.empty();+ }+ String baseUrl = ensureReady(repositoryContainerId, null);+ if (!packageIndexListsAsset(baseUrl, packageName, assetName)) {+ return Optional.empty();+ }+ URI uri = SidecarUriUtils.combine(URI.create(baseUrl), "/packages/" + SidecarUriUtils.encodeSegment(assetName));+ HttpRequest request = HttpRequest.newBuilder(uri).timeout(Duration.ofSeconds(10)).GET().build();+ HttpResponse<byte[]> response;+ try {+ response = httpClient.send(request, BodyHandlers.ofByteArray());+ } catch (Exception e) {+ throw new IllegalStateException("Could not reach the pypiserver sidecar to fetch " + assetName, e);+ }+ if (response.statusCode() == 404) {+ return Optional.empty();+ }+ if (response.statusCode() != 200) {+ throw new IllegalStateException("Could not fetch " + assetName + " from the pypiserver sidecar: "+ + "upstream returned " + response.statusCode());+ }+ return Optional.of(response.body());+ }++ /**+ * PEP 503 normalization: lowercase, runs of {@code -_.} collapsed to one {@code -}, same as+ * {@code CodeArtifactPypiController}'s own pre-upload index check. {@code -} and {@code _}+ * collapsing to the same character here is what lets this match a filename's own escaped+ * name portion (wheels, and modern sdists, replace a name's internal {@code -}/{@code .}+ * with {@code _}) against the caller-supplied, unescaped {@code packageName}.+ */+ private static String normalizePackageName(String packageName) {+ return packageName.toLowerCase(Locale.ROOT).replaceAll("[-_.]+", "-");+ }++ private boolean packageIndexListsAsset(String baseUrl, String packageName, String assetName) {+ String normalized = normalizePackageName(packageName);+ URI uri = SidecarUriUtils.combine(URI.create(baseUrl), "/simple/" + SidecarUriUtils.encodeSegment(normalized)+ + "/");+ HttpRequest request = HttpRequest.newBuilder(uri).timeout(Duration.ofSeconds(10)).GET().build();+ HttpResponse<String> response;+ try {+ response = httpClient.send(request, BodyHandlers.ofString());+ } catch (Exception e) {+ throw new IllegalStateException("Could not reach the pypiserver sidecar to look up " + packageName, e);+ }+ if (response.statusCode() == 404) {+ return false;+ }+ if (response.statusCode() != 200) {+ throw new IllegalStateException("Could not look up " + packageName + " on the pypiserver sidecar: "+ + "upstream returned " + response.statusCode());+ }+ // An exact entry match, not a substring search: a shorter real filename that happens to+ // be a substring of a different, longer real filename must not pass this check.+ return SIMPLE_INDEX_HREF.matcher(response.body()).results()+ .map(match -> {+ String href = match.group(1);+ String filename = href.substring(href.lastIndexOf('/') + 1);+ int fragment = filename.indexOf('#');+ return fragment < 0 ? filename : filename.substring(0, fragment);+ })+ .anyMatch(assetName::equals);+ }++ /**+ * PEP 440 multi-letter keywords that can introduce a continuation after a {@code -} or+ * {@code .}, per the {@code packaging} library's own {@code VERSION_PATTERN}: {@code+ * alpha}/{@code beta}/{@code preview}/{@code pre}/{@code rc} are all pre-release aliases,+ * {@code post}/{@code rev} are post-release aliases, {@code dev} is the dev-release keyword.+ * The matching single-letter aliases ({@code a}, {@code b}, {@code c}, {@code r}) are handled+ * separately in {@link #versionBoundaryEndsAt}, since a bare letter needs a following digit+ * to be treated as a continuation rather than an unrelated filename extension starting there.+ */+ private static final List<String> PEP440_CONTINUATION_PREFIXES =+ List.of("alpha", "beta", "preview", "pre", "post", "rev", "dev", "rc");++ /**+ * Real PyPI filenames always embed their own version right after a {@code -} (PEP 427/517+ * naming: {@code <name>-<version>.<ext>} for an sdist, {@code <name>-<version>-<tags>.whl}+ * for a wheel), so this is checked directly against the filename rather than trusted from the+ * caller's {@code version} argument alone, which a real file from a different version of the+ * same package would otherwise sail straight through.+ *+ * <p>Checks every {@code -<version>} occurrence and requires both that what follows it+ * actually ends the version field, handled by {@link #versionBoundaryEndsAt} (requesting+ * version {@code 1.0} against a real {@code pkg-1.0.1-py3-none-any.whl} contains the literal+ * text {@code -1.0.}, even though the file's actual version is {@code 1.0.1}, not {@code+ * 1.0}), and that what precedes it is exactly {@code packageName}, normalized the same way+ * the simple-index lookup normalizes it. Without that second check, a package name that+ * itself contains a run of digits could produce a false match inside the name portion of the+ * filename instead of the real version field: a package literally named {@code foo-1.0-bar}+ * at version {@code 2.0} produces {@code foo-1.0-bar-2.0.tar.gz}, and the text {@code -1.0-}+ * inside that name would otherwise look like a complete, valid version {@code 1.0} to a+ * caller who asked for the wrong one.+ */+ private static boolean filenameEmbedsVersion(String assetName, String packageName, String version) {+ String normalizedPackageName = normalizePackageName(packageName);+ String marker = "-" + version;+ for (int idx = assetName.indexOf(marker); idx >= 0; idx = assetName.indexOf(marker, idx + 1)) {+ String prefix = assetName.substring(0, idx);+ if (normalizePackageName(prefix).equals(normalizedPackageName)+ && versionBoundaryEndsAt(assetName, idx + marker.length())) {+ return true;+ }+ }+ return false;+ }++ /**+ * {@code after} is where a candidate version match ends; true when the version field genuinely+ * ends there rather than continuing as a longer PEP 440 version. A {@code +} never does: PEP+ * 440 gives it exactly one meaning, introducing a non-empty local-version segment, so whatever+ * immediately follows it is always part of a longer, different version (e.g. {@code 1.0+linux}+ * is not {@code 1.0}), never an unrelated filename extension starting there.+ *+ * <p>A {@code -} or a {@code .} might not end it either, and both need the same continuation+ * check: PEP 440 allows either character to separate a release segment from a following+ * {@code <release segment>} ({@code 1.0.1}), a pre/post/dev-release suffix (every keyword in+ * {@link #PEP440_CONTINUATION_PREFIXES}, plus the single-letter aliases checked below), or,+ * only after a {@code -} specifically, an implicit post-release with no keyword at all+ * ({@code 1.0-1} means {@code 1.0.post1}) <em>unless</em> {@link #isWheelBuildTag} says the+ * digits are actually a PEP 427 wheel build tag instead, which looks identical but does end+ * the version right there. All of those start right after the matched text the same way, so+ * both separators are checked for and rejected as real continuations.+ */+ private static boolean versionBoundaryEndsAt(String assetName, int after) {+ if (after == assetName.length()) {+ return true;+ }+ char next = assetName.charAt(after);+ if (next == '+' || (next != '-' && next != '.')) {+ return false;+ }+ String remainder = assetName.substring(after + 1);+ if (remainder.isEmpty()) {+ return true;+ }+ if (Character.isDigit(remainder.charAt(0))) {+ return next == '-' && isWheelBuildTag(assetName, remainder);+ }+ for (String continuation : PEP440_CONTINUATION_PREFIXES) {+ if (remainder.startsWith(continuation)) {+ return false;+ }+ }+ // Single-letter PEP 440 aliases (a/alpha, b/beta, c/rc, r/rev/post) need a following+ // digit to count as a continuation; without one, a bare letter is far more likely to be+ // the start of an unrelated filename extension than a pre/post-release marker.+ char first = remainder.charAt(0);+ boolean singleLetterAlias = first == 'a' || first == 'b' || first == 'c' || first == 'r';+ return !(singleLetterAlias && remainder.length() > 1 && Character.isDigit(remainder.charAt(1)));+ }++ /**+ * {@code remainder} is known to start with a digit, right after a {@code -}. PEP 440's+ * implicit, keyword-less post-release numeral ({@code 1.0-1} meaning {@code 1.0.post1}) and+ * PEP 427's wheel build tag ({@code digit+ letter*}, placed between the version and the+ * python/abi/platform tags, e.g. {@code 1.0-1-py3-none-any.whl}) look identical at this+ * point: both are a bare run of digits (optionally followed by letters) right after the+ * version. They're told apart by what comes after that run: a build tag is always followed+ * by another {@code -} introducing the next wheel tag, since wheels always have those three+ * trailing tags, whereas a post-release numeral is followed directly by the file extension.+ * Only a {@code .whl} filename can have a build tag at all; an sdist's {@code -<digits>} has+ * no other valid meaning than the post-release numeral.+ */+ private static boolean isWheelBuildTag(String assetName, String remainder) {+ if (!assetName.endsWith(".whl")) {+ return false;+ }+ int i = 0;+ while (i < remainder.length() && Character.isDigit(remainder.charAt(i))) {+ i++;+ }+ while (i < remainder.length() && Character.isLetter(remainder.charAt(i))) {+ i++;+ }+ return i < remainder.length() && remainder.charAt(i) == '-';+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarManager.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarManager.javaindex 2a420b9d4d..347e822ef6 100644--- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarManager.java@@ -13,56 +13,32 @@ import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.jboss.logging.Logger;-import java.net.URI;-import java.net.http.HttpClient;-import java.net.http.HttpRequest;-import java.net.http.HttpResponse;-import java.net.http.HttpResponse.BodyHandlers;-import java.time.Duration;import java.util.List;-import java.util.Locale;-import java.util.Optional;-import java.util.regex.Pattern;/**- * Lazily starts and manages one pypiserver container per CodeArtifact repository backing the- * {@code pypi} format. Like Verdaccio (npm), pypiserver has no native concept of multiple named- * indexes inside one instance, so pypi repositories each get their own container instead of- * sharing one the way Reposilite's Maven repositories do. The per-key container lifecycle (map of- * running containers, per-key start lock, health poll, restart-on-unhealthy, stop-all on- * shutdown/reset) is generic and lives in {@link PerKeyContainerPool}; this class only knows how- * to build and configure a pypiserver container specifically.+ * Starts and stops one pypiserver container per CodeArtifact repository backing the {@code pypi}+ * format, the same per-repository shape as Verdaccio (npm) rather than Reposilite's one shared+ * instance. The HTTP protocol lives in {@link PypiserverSidecarClient}. Implements+ * {@link ContainerTeardown} so reset, nuke, and shutdown stop every container this manager started.** <p>Each container is configured with authentication disabled (real CodeArtifact authorization* happens once at Floci's proxy layer, {@code CodeArtifactPypiController}, before a request ever* reaches this container) and {@code --disable-fallback}, matching the Maven and npm proxies' own* deliberate choice not to resolve upstream repositories or external connections: pypiserver* otherwise redirects a package missing from its local index to the real, public PyPI.- *- * <p>Unlike Verdaccio, this needs no public-URL environment variable: pypiserver's simple-index- * responses link to package files with a root-relative path ({@code /packages/<file>}), which pip- * and twine resolve against whatever host they actually connected to, not an address the- * container returns itself. There is nothing here for an internal, client-unreachable address to- * leak into. That root-relative path still needs rewriting to this repository's own- * {@code /codeartifact/pypi/<domain>/<repository>} prefix before a client sees it, since the- * download route lives there, not at the proxy's bare root; {@code CodeArtifactPypiController}- * does that rewrite, not this class.*/@ApplicationScoped-public class PypiserverSidecarManager implements RepositorySidecarManager, ContainerTeardown {+public class PypiserverSidecarManager implements ContainerTeardown {private static final Logger LOG = Logger.getLogger(PypiserverSidecarManager.class);- private static final String FORMAT = "pypi";private static final int PYPISERVER_PORT = 8080;private static final String HEALTH_PATH = "/health";private static final String PACKAGES_DIR = "/data/packages";- private static final Pattern SIMPLE_INDEX_HREF = Pattern.compile("href=\"([^\"]*)\"");private final ContainerBuilder containerBuilder;private final ContainerLifecycleManager lifecycleManager;private final EmulatorConfig config;private final PerKeyContainerPool pool;- private final HttpClient httpClient;@Injectpublic PypiserverSidecarManager(ContainerBuilder containerBuilder, ContainerLifecycleManager lifecycleManager,@@ -71,228 +47,24 @@ public class PypiserverSidecarManager implements RepositorySidecarManager, Contathis.lifecycleManager = lifecycleManager;this.config = config;this.pool = new PerKeyContainerPool(lifecycleManager, HEALTH_PATH);- this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build();- }-- @Override- public String format() {- return FORMAT;}/*** Base URL of a ready pypiserver instance for this pypi repository, starting its container if- * this is the first use. {@code publicUrl} is unused: see the class javadoc for why pypiserver- * needs no self-referential URL rewritten.+ * this is the first use. {@code publicUrl} is unused: pypiserver's simple-index responses link to+ * package files with a root-relative path, which pip and twine resolve against whatever host+ * they actually connected to, so there is nothing here for an internal, client-unreachable+ * address to leak into.*/- @Overridepublic String ensureReady(String pypiRepositoryId, String publicUrl) {return pool.ensureReady(pypiRepositoryId, () -> startContainer(pypiRepositoryId));}/** Stops and removes the container for one pypi repository, if one was ever started. */- @Overridepublic void release(String pypiRepositoryId) {pool.stopContainer(pypiRepositoryId);}- /**- * PyPI packages have no namespace (AWS: "Python ... package versions do not have a- * corresponding component"). pypiserver serves every file it holds flatly at- * {@code /packages/<filename>} regardless of which package or version it belongs to,- * confirmed against a live container, so both are verified before serving anything: that- * {@code packageName}'s own simple index genuinely lists {@code assetName}, and that- * {@code assetName} itself embeds {@code version} the way every real PyPI filename does- * (PEP 427/517 naming) rather than trusting the caller's {@code version} argument on its own.- *- * <p>{@code assetName} is percent-encoded via {@link SidecarUriUtils#encodeSegment(String)}- * before joining it to the fixed {@code /packages/} prefix: without that, a {@code /} inside- * it would be indistinguishable from a real path separator and could splice in extra path- * segments.- */- @Override- public Optional<byte[]> fetchPackageVersionAsset(String repositoryContainerId, String domain, String repository,- String namespace, String packageName, String version, String assetName) {- if (!filenameEmbedsVersion(assetName, packageName, version)) {- return Optional.empty();- }- String baseUrl = ensureReady(repositoryContainerId, null);- if (!packageIndexListsAsset(baseUrl, packageName, assetName)) {- return Optional.empty();- }- URI uri = SidecarUriUtils.combine(URI.create(baseUrl), "/packages/" + SidecarUriUtils.encodeSegment(assetName));- HttpRequest request = HttpRequest.newBuilder(uri).timeout(Duration.ofSeconds(10)).GET().build();- HttpResponse<byte[]> response;- try {- response = httpClient.send(request, BodyHandlers.ofByteArray());- } catch (Exception e) {- throw new IllegalStateException("Could not reach the pypiserver sidecar to fetch " + assetName, e);- }- if (response.statusCode() == 404) {- return Optional.empty();- }- if (response.statusCode() != 200) {- throw new IllegalStateException("Could not fetch " + assetName + " from the pypiserver sidecar: "- + "upstream returned " + response.statusCode());- }- return Optional.of(response.body());- }-- /**- * PEP 503 normalization: lowercase, runs of {@code -_.} collapsed to one {@code -}, same as- * {@code CodeArtifactPypiController}'s own pre-upload index check. {@code -} and {@code _}- * collapsing to the same character here is what lets this match a filename's own escaped- * name portion (wheels, and modern sdists, replace a name's internal {@code -}/{@code .}- * with {@code _}) against the caller-supplied, unescaped {@code packageName}.- */- private static String normalizePackageName(String packageName) {- return packageName.toLowerCase(Locale.ROOT).replaceAll("[-_.]+", "-");- }-- private boolean packageIndexListsAsset(String baseUrl, String packageName, String assetName) {- String normalized = normalizePackageName(packageName);- URI uri = SidecarUriUtils.combine(URI.create(baseUrl), "/simple/" + SidecarUriUtils.encodeSegment(normalized)- + "/");- HttpRequest request = HttpRequest.newBuilder(uri).timeout(Duration.ofSeconds(10)).GET().build();- HttpResponse<String> response;- try {- response = httpClient.send(request, BodyHandlers.ofString());- } catch (Exception e) {- throw new IllegalStateException("Could not reach the pypiserver sidecar to look up " + packageName, e);- }- if (response.statusCode() == 404) {- return false;- }- if (response.statusCode() != 200) {- throw new IllegalStateException("Could not look up " + packageName + " on the pypiserver sidecar: "- + "upstream returned " + response.statusCode());- }- // An exact entry match, not a substring search: a shorter real filename that happens to- // be a substring of a different, longer real filename must not pass this check.- return SIMPLE_INDEX_HREF.matcher(response.body()).results()- .map(match -> {- String href = match.group(1);- String filename = href.substring(href.lastIndexOf('/') + 1);- int fragment = filename.indexOf('#');- return fragment < 0 ? filename : filename.substring(0, fragment);- })- .anyMatch(assetName::equals);- }-- /**- * PEP 440 multi-letter keywords that can introduce a continuation after a {@code -} or- * {@code .}, per the {@code packaging} library's own {@code VERSION_PATTERN}: {@code- * alpha}/{@code beta}/{@code preview}/{@code pre}/{@code rc} are all pre-release aliases,- * {@code post}/{@code rev} are post-release aliases, {@code dev} is the dev-release keyword.- * The matching single-letter aliases ({@code a}, {@code b}, {@code c}, {@code r}) are handled- * separately in {@link #versionBoundaryEndsAt}, since a bare letter needs a following digit- * to be treated as a continuation rather than an unrelated filename extension starting there.- */- private static final List<String> PEP440_CONTINUATION_PREFIXES =- List.of("alpha", "beta", "preview", "pre", "post", "rev", "dev", "rc");-- /**- * Real PyPI filenames always embed their own version right after a {@code -} (PEP 427/517- * naming: {@code <name>-<version>.<ext>} for an sdist, {@code <name>-<version>-<tags>.whl}- * for a wheel), so this is checked directly against the filename rather than trusted from the- * caller's {@code version} argument alone, which a real file from a different version of the- * same package would otherwise sail straight through.- *- * <p>Checks every {@code -<version>} occurrence and requires both that what follows it- * actually ends the version field, handled by {@link #versionBoundaryEndsAt} (requesting- * version {@code 1.0} against a real {@code pkg-1.0.1-py3-none-any.whl} contains the literal- * text {@code -1.0.}, even though the file's actual version is {@code 1.0.1}, not {@code- * 1.0}), and that what precedes it is exactly {@code packageName}, normalized the same way- * the simple-index lookup normalizes it. Without that second check, a package name that- * itself contains a run of digits could produce a false match inside the name portion of the- * filename instead of the real version field: a package literally named {@code foo-1.0-bar}- * at version {@code 2.0} produces {@code foo-1.0-bar-2.0.tar.gz}, and the text {@code -1.0-}- * inside that name would otherwise look like a complete, valid version {@code 1.0} to a- * caller who asked for the wrong one.- */- private static boolean filenameEmbedsVersion(String assetName, String packageName, String version) {- String normalizedPackageName = normalizePackageName(packageName);- String marker = "-" + version;- for (int idx = assetName.indexOf(marker); idx >= 0; idx = assetName.indexOf(marker, idx + 1)) {- String prefix = assetName.substring(0, idx);- if (normalizePackageName(prefix).equals(normalizedPackageName)- && versionBoundaryEndsAt(assetName, idx + marker.length())) {- return true;- }- }- return false;- }-- /**- * {@code after} is where a candidate version match ends; true when the version field genuinely- * ends there rather than continuing as a longer PEP 440 version. A {@code +} never does: PEP- * 440 gives it exactly one meaning, introducing a non-empty local-version segment, so whatever- * immediately follows it is always part of a longer, different version (e.g. {@code 1.0+linux}- * is not {@code 1.0}), never an unrelated filename extension starting there.- *- * <p>A {@code -} or a {@code .} might not end it either, and both need the same continuation- * check: PEP 440 allows either character to separate a release segment from a following- * {@code <release segment>} ({@code 1.0.1}), a pre/post/dev-release suffix (every keyword in- * {@link #PEP440_CONTINUATION_PREFIXES}, plus the single-letter aliases checked below), or,- * only after a {@code -} specifically, an implicit post-release with no keyword at all- * ({@code 1.0-1} means {@code 1.0.post1}) <em>unless</em> {@link #isWheelBuildTag} says the- * digits are actually a PEP 427 wheel build tag instead, which looks identical but does end- * the version right there. All of those start right after the matched text the same way, so- * both separators are checked for and rejected as real continuations.- */- private static boolean versionBoundaryEndsAt(String assetName, int after) {- if (after == assetName.length()) {- return true;- }- char next = assetName.charAt(after);- if (next == '+' || (next != '-' && next != '.')) {- return false;- }- String remainder = assetName.substring(after + 1);- if (remainder.isEmpty()) {- return true;- }- if (Character.isDigit(remainder.charAt(0))) {- return next == '-' && isWheelBuildTag(assetName, remainder);- }- for (String continuation : PEP440_CONTINUATION_PREFIXES) {- if (remainder.startsWith(continuation)) {- return false;- }- }- // Single-letter PEP 440 aliases (a/alpha, b/beta, c/rc, r/rev/post) need a following- // digit to count as a continuation; without one, a bare letter is far more likely to be- // the start of an unrelated filename extension than a pre/post-release marker.- char first = remainder.charAt(0);- boolean singleLetterAlias = first == 'a' || first == 'b' || first == 'c' || first == 'r';- return !(singleLetterAlias && remainder.length() > 1 && Character.isDigit(remainder.charAt(1)));- }-- /**- * {@code remainder} is known to start with a digit, right after a {@code -}. PEP 440's- * implicit, keyword-less post-release numeral ({@code 1.0-1} meaning {@code 1.0.post1}) and- * PEP 427's wheel build tag ({@code digit+ letter*}, placed between the version and the- * python/abi/platform tags, e.g. {@code 1.0-1-py3-none-any.whl}) look identical at this- * point: both are a bare run of digits (optionally followed by letters) right after the- * version. They're told apart by what comes after that run: a build tag is always followed- * by another {@code -} introducing the next wheel tag, since wheels always have those three- * trailing tags, whereas a post-release numeral is followed directly by the file extension.- * Only a {@code .whl} filename can have a build tag at all; an sdist's {@code -<digits>} has- * no other valid meaning than the post-release numeral.- */- private static boolean isWheelBuildTag(String assetName, String remainder) {- if (!assetName.endsWith(".whl")) {- return false;- }- int i = 0;- while (i < remainder.length() && Character.isDigit(remainder.charAt(i))) {- i++;- }- while (i < remainder.length() && Character.isLetter(remainder.charAt(i))) {- i++;- }- return i < remainder.length() && remainder.charAt(i) == '-';- }-private PerKeyContainerPool.StartedContainer startContainer(String pypiRepositoryId) {String image = config.services().codeartifact().pypiImage();String containerName = ContainerStorageHelper.dockerName(config, "floci-pypiserver-" + pypiRepositoryId);diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiControllerTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiControllerTest.javaindex f33d942b3e..8d832c2bd5 100644--- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiControllerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiControllerTest.java@@ -40,7 +40,7 @@ import static org.mockito.Mockito.when;* {@link HttpServer}, not a mock of the JDK {@link HttpClient}, since that client's request/response* types are effectively unmockable) so the proxy-passthrough, auth-resolution, and pre-upload* overwrite-rejection logic all run for real. {@link CodeArtifactService} and- * {@link PypiserverSidecarManager} are mocked: what they do is already covered by+ * {@link PypiserverSidecarClient} are mocked: what they do is already covered by* {@code CodeArtifactServiceTest} and {@code PypiserverSidecarManagerTest}, what matters here is how* this controller uses their results.*/@@ -54,7 +54,7 @@ class CodeArtifactPypiControllerTest {private HttpServer backend;private String backendUrl;private CodeArtifactService service;- private PypiserverSidecarManager pypiserver;+ private PypiserverSidecarClient pypiserver;private CodeArtifactPypiController controller;@BeforeEach@@ -64,7 +64,7 @@ class CodeArtifactPypiControllerTest {backendUrl = "http://127.0.0.1:" + backend.getAddress().getPort();service = mock(CodeArtifactService.class);- pypiserver = mock(PypiserverSidecarManager.class);+ pypiserver = mock(PypiserverSidecarClient.class);when(service.resolveAuthorizationToken(anyString(), eq(DOMAIN))).thenReturn(Optional.of(new AuthorizationTokenScope(OWNER, REGION)));when(service.ensureFormatContainerId(eq("pypi"), eq(REGION), eq(DOMAIN), eq(OWNER), eq(REPOSITORY)))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 819d6efd17..562e89922b 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@@ -53,7 +53,7 @@ class CodeArtifactServiceTest {private AccountAwareStorageBackend<CodeArtifactRepository> repoStore;private VerdaccioSidecarClient verdaccioClient;private ReposiliteSidecarClient reposiliteClient;- private PypiserverSidecarManager pypiserverManager;+ private PypiserverSidecarClient pypiserverClient;@BeforeEachvoid setUp() {@@ -75,11 +75,11 @@ class CodeArtifactServiceTest {when(verdaccioClient.format()).thenReturn("npm");reposiliteClient = mock(ReposiliteSidecarClient.class);when(reposiliteClient.format()).thenReturn("maven");- pypiserverManager = mock(PypiserverSidecarManager.class);- when(pypiserverManager.format()).thenReturn("pypi");+ pypiserverClient = mock(PypiserverSidecarClient.class);+ when(pypiserverClient.format()).thenReturn("pypi");service = new CodeArtifactService(domainStore, repoStore, packageVersionStore, regionResolver, config,true, null, new CodeArtifactSidecarRegistry(- List.of(verdaccioClient, reposiliteClient, pypiserverManager)));+ List.of(verdaccioClient, reposiliteClient, pypiserverClient)));}// -------------------------------------------------------------- domains@@ -1122,7 +1122,7 @@ class CodeArtifactServiceTest {service.createDomain(REGION, "dom", null, Map.of());service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());byte[] content = "wheel bytes".getBytes(StandardCharsets.UTF_8);- when(pypiserverManager.fetchPackageVersionAsset(anyString(), eq("dom"), eq("repo"), isNull(),+ when(pypiserverClient.fetchPackageVersionAsset(anyString(), eq("dom"), eq("repo"), isNull(),eq("my-pkg"), eq("1.0.0"), eq("my_pkg-1.0.0-py3-none-any.whl"))).thenReturn(Optional.of(content));PackageVersionAssetResult result = service.getPackageVersionAsset(REGION, "dom", null, "repo", "pypi",@@ -1135,7 +1135,7 @@ class CodeArtifactServiceTest {void getPackageVersionAssetReturns404WhenTheSidecarHasNoSuchAssetForAContainerBackedFormat() {service.createDomain(REGION, "dom", null, Map.of());service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());- when(pypiserverManager.fetchPackageVersionAsset(anyString(), eq("dom"), eq("repo"), isNull(),+ when(pypiserverClient.fetchPackageVersionAsset(anyString(), eq("dom"), eq("repo"), isNull(),eq("my-pkg"), eq("1.0.0"), eq("missing.whl"))).thenReturn(Optional.empty());AwsException e = assertThrows(AwsException.class, () -> service.getPackageVersionAsset(REGION, "dom", null,@@ -1150,7 +1150,7 @@ class CodeArtifactServiceTest {service.createDomain(REGION, "dom", null, Map.of());service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());byte[] content = "wheel bytes".getBytes(StandardCharsets.UTF_8);- when(pypiserverManager.fetchPackageVersionAsset(anyString(), eq("dom"), eq("repo"), isNull(),+ when(pypiserverClient.fetchPackageVersionAsset(anyString(), eq("dom"), eq("repo"), isNull(),eq("my-pkg"), eq("1.0.0"), eq("my_pkg-1.0.0.whl"))).thenReturn(Optional.of(content));String realRevision = service.getPackageVersionAsset(REGION, "dom", null, "repo", "pypi", null, "my-pkg","1.0.0", "my_pkg-1.0.0.whl", null).packageVersionRevision();diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarClientTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarClientTest.javanew file mode 100644index 0000000000..69cfa66527--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarClientTest.java@@ -0,0 +1,409 @@+package io.github.hectorvent.floci.services.codeartifact;++import com.sun.net.httpserver.HttpServer;+import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;+import io.github.hectorvent.floci.core.common.docker.PerKeyContainerPool;+import org.junit.jupiter.api.AfterEach;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;++import java.lang.reflect.Field;+import java.net.InetSocketAddress;+import java.net.http.HttpClient;+import java.nio.charset.StandardCharsets;+import java.util.Optional;+import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.atomic.AtomicReference;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertThrows;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.Mockito.mock;++/**+ * Wire-contract tests for {@link PypiserverSidecarClient} against a JDK {@code HttpServer} standing+ * in for a running pypiserver container. The container is registered in the manager's pool+ * directly, so readiness resolves to the fake server without any Docker daemon.+ */+class PypiserverSidecarClientTest {++ private final ContainerBuilder containerBuilder = mock(ContainerBuilder.class);+ private final ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);+ private final EmulatorConfig config = mock(EmulatorConfig.class);++ private HttpServer backend;+ private String backendUrl;++ @BeforeEach+ void setUpBackend() throws Exception {+ backend = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);+ backend.createContext("/health", exchange -> {+ exchange.sendResponseHeaders(200, -1);+ exchange.close();+ });+ backend.start();+ backendUrl = "http://127.0.0.1:" + backend.getAddress().getPort();+ }++ @AfterEach+ void tearDownBackend() {+ backend.stop(0);+ }++ @Test+ void fetchPackageVersionAssetGetsTheFlatPackagesPath() throws Exception {+ PypiserverSidecarManager manager = manager();+ seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);+ respondWithSimpleIndex("demo-pkg", "demo_pkg-1.0.0.tar.gz");+ backend.createContext("/packages/demo_pkg-1.0.0.tar.gz", exchange -> {+ byte[] body = "sdist bytes".getBytes(StandardCharsets.UTF_8);+ exchange.sendResponseHeaders(200, body.length);+ exchange.getResponseBody().write(body);+ exchange.close();+ });++ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",+ "1.0.0", "demo_pkg-1.0.0.tar.gz");++ assertTrue(result.isPresent());+ assertEquals("sdist bytes", new String(result.get(), StandardCharsets.UTF_8));+ }++ /**+ * The fix for a real finding: the filename alone was trusted as proof of version, so a caller+ * could request a different (even nonexistent) version and still get back a real file that+ * actually belongs to a version it never named.+ */+ @Test+ void fetchPackageVersionAssetReturnsEmptyWhenTheRequestedVersionDoesNotMatchTheFilename() throws Exception {+ PypiserverSidecarManager manager = manager();+ seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);+ respondWithSimpleIndex("demo-pkg", "demo_pkg-1.0.0.tar.gz");+ backend.createContext("/packages/demo_pkg-1.0.0.tar.gz", exchange -> {+ byte[] body = "should never be served".getBytes(StandardCharsets.UTF_8);+ exchange.sendResponseHeaders(200, body.length);+ exchange.getResponseBody().write(body);+ exchange.close();+ });++ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",+ "9.9.9", "demo_pkg-1.0.0.tar.gz");++ assertTrue(result.isEmpty());+ }++ /**+ * The fix for a real finding: a plain substring check on {@code "-" + version} would wrongly+ * accept this, since {@code pkg-1.0.1-...} literally contains the text {@code -1.0.} even+ * though the file's real version is {@code 1.0.1}, not {@code 1.0}.+ */+ @Test+ void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfTheFilenamesRealVersion() throws Exception {+ PypiserverSidecarManager manager = manager();+ seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);+ respondWithSimpleIndex("demo-pkg", "demo_pkg-1.0.1-py3-none-any.whl");+ backend.createContext("/packages/demo_pkg-1.0.1-py3-none-any.whl", exchange -> {+ byte[] body = "should never be served".getBytes(StandardCharsets.UTF_8);+ exchange.sendResponseHeaders(200, body.length);+ exchange.getResponseBody().write(body);+ exchange.close();+ });++ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",+ "1.0", "demo_pkg-1.0.1-py3-none-any.whl");++ assertTrue(result.isEmpty());+ }++ /**+ * The fix for a real finding on top of the previous one: a PEP 440 pre/post/dev-release+ * suffix also starts right after the matched version text with a letter, not a digit, so the+ * first boundary fix (reject only when followed by another digit) still wrongly accepted+ * {@code 1.0.post1}, {@code 1.0.dev1}, and {@code 1.0.rc1} as if they were plain {@code 1.0}.+ */+ @Test+ void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAPostReleaseSuffix() throws Exception {+ assertRequestedVersionRejectedAgainstRealVersion("1.0.post1");+ }++ @Test+ void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfADevReleaseSuffix() throws Exception {+ assertRequestedVersionRejectedAgainstRealVersion("1.0.dev1");+ }++ @Test+ void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAReleaseCandidateSuffix() throws Exception {+ assertRequestedVersionRejectedAgainstRealVersion("1.0.rc1");+ }++ /**+ * The fix for a real finding: {@code +} introduces a PEP 440 local-version segment, so+ * whatever follows it is always part of a longer, different version. {@code 1.0+linux} is+ * not {@code 1.0}, but a {@code +} used to be treated the same as a wheel tag's {@code -} and+ * accepted as ending the version field right there.+ */+ @Test+ void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfALocalVersionSegment() throws Exception {+ assertRequestedVersionRejectedAgainstRealVersion("1.0+linux");+ }++ /**+ * The fix for a real finding: PEP 440 allows a bare {@code -<digits>} right after a release+ * segment, with no keyword at all, as a legacy, implicit way of writing a post-release+ * ({@code 1.0-1} means {@code 1.0.post1}). The old check treated every {@code -} right after+ * a matched version as an unconditional boundary, so this legacy form wasn't recognized as a+ * continuation.+ */+ @Test+ void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAnImplicitPostRelease() throws Exception {+ assertRequestedVersionRejectedAgainstRealVersion("1.0-1");+ }++ /**+ * Same finding, the explicit-keyword form of a legacy post-release separator: PEP 440 also+ * allows {@code -} (not just {@code .}) directly before {@code post}/{@code dev}/{@code rc}.+ */+ @Test+ void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAHyphenSeparatedPostRelease() throws Exception {+ assertRequestedVersionRejectedAgainstRealVersion("1.0-post1");+ }++ /**+ * The fix for a real finding: {@code pre}, {@code preview}, and {@code c} are all PEP 440+ * aliases for {@code rc}, and weren't in the continuation list, so a filename using one of+ * them could sail through as if it were the plain requested version.+ */+ @Test+ void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAPreReleaseAlias() throws Exception {+ assertRequestedVersionRejectedAgainstRealVersion("1.0.pre1");+ }++ @Test+ void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAPreviewReleaseAlias() throws Exception {+ assertRequestedVersionRejectedAgainstRealVersion("1.0.preview1");+ }++ @Test+ void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAShorthandReleaseCandidateAlias() throws Exception {+ assertRequestedVersionRejectedAgainstRealVersion("1.0.c1");+ }++ /**+ * Same finding, the post-release side: {@code r} is a PEP 440 alias for {@code post}/{@code+ * rev}, wasn't recognized either, and sits one character shorter than {@code rev} so it needs+ * its own coverage.+ */+ @Test+ void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAShorthandPostReleaseAlias() throws Exception {+ assertRequestedVersionRejectedAgainstRealVersion("1.0-r1");+ }++ /**+ * The fix for a real finding on the opposite side of the same area: a PEP 427 wheel build tag+ * ({@code digit+ letter*}, placed between the version and the python/abi/platform tags) looks+ * exactly like PEP 440's implicit post-release numeral at the point this check inspects, but+ * actually ends the version right there rather than continuing it. {@code demo_pkg-1.0-1-py3-+ * none-any.whl} is version {@code 1.0} with build tag {@code 1}, not version {@code 1.0.post1}.+ */+ @Test+ void fetchPackageVersionAssetAcceptsAWheelBuildTagRatherThanTreatingItAsAPostRelease() throws Exception {+ PypiserverSidecarManager manager = manager();+ seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);+ respondWithSimpleIndex("demo-pkg", "demo_pkg-1.0-1-py3-none-any.whl");+ backend.createContext("/packages/demo_pkg-1.0-1-py3-none-any.whl", exchange -> {+ byte[] body = "wheel bytes".getBytes(StandardCharsets.UTF_8);+ exchange.sendResponseHeaders(200, body.length);+ exchange.getResponseBody().write(body);+ exchange.close();+ });++ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",+ "1.0", "demo_pkg-1.0-1-py3-none-any.whl");++ assertTrue(result.isPresent());+ assertEquals("wheel bytes", new String(result.get(), StandardCharsets.UTF_8));+ }++ /**+ * The fix for a real finding: a package name can itself contain a run of digits that looks+ * like a version, and a legacy sdist filename (pre-dating the PEP 625 escaping modern+ * {@code demo_pkg-1.0.0.tar.gz}-style filenames in this test class use) keeps the project+ * name's own hyphens literal rather than underscoring them. A package literally named {@code+ * foo-1.0-bar} at version {@code 2.0} then produces the filename {@code+ * foo-1.0-bar-2.0.tar.gz}; the old check only looked at what followed a {@code -<version>}+ * match, so the {@code -1.0-} inside the name portion looked like a complete, valid version+ * {@code 1.0} on its own, serving the version-2.0 asset for a request asking for 1.0. Fixed+ * by also requiring that what precedes the match is exactly the requested package name,+ * normalized the same way the simple-index lookup normalizes it; this filenameEmbedsVersion+ * check now fails before any request is even sent.+ */+ @Test+ void fetchPackageVersionAssetRejectsAVersionThatOnlyMatchesInsideThePackageNamePortion() throws Exception {+ PypiserverSidecarManager manager = manager();+ seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);++ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "foo-1.0-bar",+ "1.0", "foo-1.0-bar-2.0.tar.gz");++ assertTrue(result.isEmpty());+ }++ private void assertRequestedVersionRejectedAgainstRealVersion(String realVersion) throws Exception {+ PypiserverSidecarManager manager = manager();+ seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);+ String filename = "demo_pkg-" + realVersion + ".tar.gz";+ respondWithSimpleIndex("demo-pkg", filename);+ backend.createContext("/packages/" + filename, exchange -> {+ byte[] body = "should never be served".getBytes(StandardCharsets.UTF_8);+ exchange.sendResponseHeaders(200, body.length);+ exchange.getResponseBody().write(body);+ exchange.close();+ });++ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",+ "1.0", filename);++ assertTrue(result.isEmpty(), "version 1.0 must not match " + filename);+ }++ /**+ * The fix for a real finding: the package-index check used to be a plain substring search,+ * which a shorter real filename that happens to be a substring of a different, longer real+ * filename could pass incorrectly.+ */+ @Test+ void fetchPackageVersionAssetRequiresAnExactIndexEntryNotASubstringMatch() throws Exception {+ PypiserverSidecarManager manager = manager();+ seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);+ // The index genuinely lists a longer filename that contains the requested, shorter one as+ // a substring; the shorter one was never actually published on its own.+ respondWithSimpleIndex("demo-pkg", "demo_pkg-1.0.0.tar.gz.asc");+ backend.createContext("/packages/demo_pkg-1.0.0.tar.gz", exchange -> {+ byte[] body = "should never be served".getBytes(StandardCharsets.UTF_8);+ exchange.sendResponseHeaders(200, body.length);+ exchange.getResponseBody().write(body);+ exchange.close();+ });++ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",+ "1.0.0", "demo_pkg-1.0.0.tar.gz");++ assertTrue(result.isEmpty());+ }++ @Test+ void fetchPackageVersionAssetReturnsEmptyWhenThePackageWasNeverUploaded() throws Exception {+ PypiserverSidecarManager manager = manager();+ seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);+ backend.createContext("/simple/demo-pkg/", exchange -> {+ exchange.sendResponseHeaders(404, -1);+ exchange.close();+ });++ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",+ "1.0.0", "missing.whl");++ assertTrue(result.isEmpty());+ }++ /**+ * The fix for a real finding: pypiserver serves every file it holds flatly, regardless of+ * which package it belongs to, so without checking the named package's own simple index first,+ * a caller could name a real file that belongs to a different package in the same repository+ * and get it back under the wrong package's name.+ */+ @Test+ void fetchPackageVersionAssetReturnsEmptyWhenTheFileBelongsToADifferentPackage() throws Exception {+ PypiserverSidecarManager manager = manager();+ seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);+ respondWithSimpleIndex("demo-pkg", "demo_pkg-1.0.0.tar.gz");+ backend.createContext("/packages/other_pkg-1.0.0.tar.gz", exchange -> {+ byte[] body = "should never be served".getBytes(StandardCharsets.UTF_8);+ exchange.sendResponseHeaders(200, body.length);+ exchange.getResponseBody().write(body);+ exchange.close();+ });++ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",+ "1.0.0", "other_pkg-1.0.0.tar.gz");++ assertTrue(result.isEmpty());+ }++ @Test+ void fetchPackageVersionAssetThrowsRatherThanReportingNotFoundOnASidecarServerError() throws Exception {+ PypiserverSidecarManager manager = manager();+ seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);+ backend.createContext("/simple/demo-pkg/", exchange -> {+ exchange.sendResponseHeaders(500, -1);+ exchange.close();+ });++ assertThrows(IllegalStateException.class, () -> client(manager).fetchPackageVersionAsset("pypi-repo-1", "dom",+ "repo", null, "demo-pkg", "1.0.0", "demo_pkg-1.0.0.tar.gz"));+ }++ /**+ * The fix for a real finding: the normalized package name used to be spliced straight into+ * {@code /simple/<normalized>/}. A {@code /} inside {@code packageName} would then be+ * indistinguishable from the real separators around it and could splice in extra path+ * segments. Fixed by percent-encoding the normalized name via {@code encodeSegment} before+ * it's placed in the path, so a {@code /} inside it survives only as a literal {@code %2F}+ * within that one segment. {@code packageName} is {@code a/../b}, not a cleaner {@code ../..}:+ * the PEP 503 normalization this method already applies collapses runs of {@code -_.} to a+ * single {@code -} first, which would otherwise quietly absorb a pure dot-segment attempt+ * before it ever reached the encoding this test is checking.+ */+ @Test+ void fetchPackageVersionAssetTreatsASlashInThePackageNameAsLiteralNotAStructuralSeparator() throws Exception {+ PypiserverSidecarManager manager = manager();+ seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);+ AtomicReference<String> observedRawPath = new AtomicReference<>();+ backend.createContext("/", exchange -> {+ observedRawPath.set(exchange.getRequestURI().getRawPath());+ exchange.sendResponseHeaders(404, -1);+ exchange.close();+ });++ Optional<byte[]> result = client(manager).fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "a/../b",+ "1.0.0", "a/-/b-1.0.0.tar.gz");++ assertTrue(result.isEmpty());+ assertEquals("/simple/a%2F-%2Fb/", observedRawPath.get());+ }++ private void respondWithSimpleIndex(String packageName, String filename) {+ String html = "<a href=\"/packages/" + filename + "\">" + filename + "</a>";+ backend.createContext("/simple/" + packageName + "/", exchange -> {+ byte[] bytes = html.getBytes(StandardCharsets.UTF_8);+ exchange.getResponseHeaders().set("Content-Type", "text/html");+ exchange.sendResponseHeaders(200, bytes.length);+ exchange.getResponseBody().write(bytes);+ exchange.close();+ });+ }++ private PypiserverSidecarManager manager() {+ return new PypiserverSidecarManager(containerBuilder, lifecycleManager, config);+ }++ private PypiserverSidecarClient client(PypiserverSidecarManager manager) {+ return new PypiserverSidecarClient(manager, HttpClient.newHttpClient());+ }++ @SuppressWarnings("unchecked")+ private static void seedPooledContainer(PypiserverSidecarManager manager, String key, String containerId,+ String url) throws Exception {+ Field poolField = PypiserverSidecarManager.class.getDeclaredField("pool");+ poolField.setAccessible(true);+ PerKeyContainerPool pool = (PerKeyContainerPool) poolField.get(manager);++ Field containersField = PerKeyContainerPool.class.getDeclaredField("containers");+ containersField.setAccessible(true);+ ConcurrentHashMap<String, PerKeyContainerPool.StartedContainer> containers =+ (ConcurrentHashMap<String, PerKeyContainerPool.StartedContainer>) containersField.get(pool);+ containers.put(key, new PerKeyContainerPool.StartedContainer(containerId, url));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarManagerTest.javaindex 80277a30bf..cec938ae8c 100644--- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/PypiserverSidecarManagerTest.java@@ -1,35 +1,27 @@package io.github.hectorvent.floci.services.codeartifact;-import com.sun.net.httpserver.HttpServer;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.ContainerTeardown;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.PerKeyContainerPool;-import org.junit.jupiter.api.AfterEach;-import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import java.lang.reflect.Field;-import java.net.InetSocketAddress;-import java.nio.charset.StandardCharsets;-import java.util.Optional;import java.util.concurrent.ConcurrentHashMap;-import java.util.concurrent.atomic.AtomicReference;-import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertInstanceOf;-import static org.junit.jupiter.api.Assertions.assertThrows;-import static org.junit.jupiter.api.Assertions.assertTrue;import static org.mockito.Mockito.mock;import static org.mockito.Mockito.verify;import static org.mockito.Mockito.verifyNoInteractions;/**- * Same reasoning as {@code VerdaccioSidecarManagerTest}: {@link PypiserverSidecarManager}'s own- * {@link PerKeyContainerPool#stopAll()} behavior is already covered by {@code PerKeyContainerPoolTest},- * so what matters here is the wiring, that the manager genuinely implements {@link ContainerTeardown}- * and hands the pool container ids it actually started.+ * {@link PypiserverSidecarManager#stopManagedContainers()} is a thin delegation to+ * {@link PerKeyContainerPool#stopAll()}, whose own behavior is covered by {@code PerKeyContainerPoolTest}.+ * What matters here is the wiring: that the manager implements {@link ContainerTeardown} (so+ * {@code ContainerTeardowns.stopAll} finds it on reset, nuke, and shutdown) and that it stops the+ * containers it actually started. A container is seeded straight into the pool by reflection, so this+ * needs no Docker daemon.*/class PypiserverSidecarManagerTest {@@ -37,363 +29,11 @@ class PypiserverSidecarManagerTest {private final ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);private final EmulatorConfig config = mock(EmulatorConfig.class);- private HttpServer backend;- private String backendUrl;-- @BeforeEach- void setUpBackend() throws Exception {- backend = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);- backend.createContext("/health", exchange -> {- exchange.sendResponseHeaders(200, -1);- exchange.close();- });- backend.start();- backendUrl = "http://127.0.0.1:" + backend.getAddress().getPort();- }-- @AfterEach- void tearDownBackend() {- backend.stop(0);- }-@Testvoid implementsContainerTeardownSoStateResetAndNukeCanFindIt() {assertInstanceOf(ContainerTeardown.class, manager());}- @Test- void fetchPackageVersionAssetGetsTheFlatPackagesPath() throws Exception {- PypiserverSidecarManager manager = manager();- seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);- respondWithSimpleIndex("demo-pkg", "demo_pkg-1.0.0.tar.gz");- backend.createContext("/packages/demo_pkg-1.0.0.tar.gz", exchange -> {- byte[] body = "sdist bytes".getBytes(StandardCharsets.UTF_8);- exchange.sendResponseHeaders(200, body.length);- exchange.getResponseBody().write(body);- exchange.close();- });-- Optional<byte[]> result = manager.fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",- "1.0.0", "demo_pkg-1.0.0.tar.gz");-- assertTrue(result.isPresent());- assertEquals("sdist bytes", new String(result.get(), StandardCharsets.UTF_8));- }-- /**- * The fix for a real finding: the filename alone was trusted as proof of version, so a caller- * could request a different (even nonexistent) version and still get back a real file that- * actually belongs to a version it never named.- */- @Test- void fetchPackageVersionAssetReturnsEmptyWhenTheRequestedVersionDoesNotMatchTheFilename() throws Exception {- PypiserverSidecarManager manager = manager();- seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);- respondWithSimpleIndex("demo-pkg", "demo_pkg-1.0.0.tar.gz");- backend.createContext("/packages/demo_pkg-1.0.0.tar.gz", exchange -> {- byte[] body = "should never be served".getBytes(StandardCharsets.UTF_8);- exchange.sendResponseHeaders(200, body.length);- exchange.getResponseBody().write(body);- exchange.close();- });-- Optional<byte[]> result = manager.fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",- "9.9.9", "demo_pkg-1.0.0.tar.gz");-- assertTrue(result.isEmpty());- }-- /**- * The fix for a real finding: a plain substring check on {@code "-" + version} would wrongly- * accept this, since {@code pkg-1.0.1-...} literally contains the text {@code -1.0.} even- * though the file's real version is {@code 1.0.1}, not {@code 1.0}.- */- @Test- void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfTheFilenamesRealVersion() throws Exception {- PypiserverSidecarManager manager = manager();- seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);- respondWithSimpleIndex("demo-pkg", "demo_pkg-1.0.1-py3-none-any.whl");- backend.createContext("/packages/demo_pkg-1.0.1-py3-none-any.whl", exchange -> {- byte[] body = "should never be served".getBytes(StandardCharsets.UTF_8);- exchange.sendResponseHeaders(200, body.length);- exchange.getResponseBody().write(body);- exchange.close();- });-- Optional<byte[]> result = manager.fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",- "1.0", "demo_pkg-1.0.1-py3-none-any.whl");-- assertTrue(result.isEmpty());- }-- /**- * The fix for a real finding on top of the previous one: a PEP 440 pre/post/dev-release- * suffix also starts right after the matched version text with a letter, not a digit, so the- * first boundary fix (reject only when followed by another digit) still wrongly accepted- * {@code 1.0.post1}, {@code 1.0.dev1}, and {@code 1.0.rc1} as if they were plain {@code 1.0}.- */- @Test- void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAPostReleaseSuffix() throws Exception {- assertRequestedVersionRejectedAgainstRealVersion("1.0.post1");- }-- @Test- void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfADevReleaseSuffix() throws Exception {- assertRequestedVersionRejectedAgainstRealVersion("1.0.dev1");- }-- @Test- void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAReleaseCandidateSuffix() throws Exception {- assertRequestedVersionRejectedAgainstRealVersion("1.0.rc1");- }-- /**- * The fix for a real finding: {@code +} introduces a PEP 440 local-version segment, so- * whatever follows it is always part of a longer, different version. {@code 1.0+linux} is- * not {@code 1.0}, but a {@code +} used to be treated the same as a wheel tag's {@code -} and- * accepted as ending the version field right there.- */- @Test- void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfALocalVersionSegment() throws Exception {- assertRequestedVersionRejectedAgainstRealVersion("1.0+linux");- }-- /**- * The fix for a real finding: PEP 440 allows a bare {@code -<digits>} right after a release- * segment, with no keyword at all, as a legacy, implicit way of writing a post-release- * ({@code 1.0-1} means {@code 1.0.post1}). The old check treated every {@code -} right after- * a matched version as an unconditional boundary, so this legacy form wasn't recognized as a- * continuation.- */- @Test- void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAnImplicitPostRelease() throws Exception {- assertRequestedVersionRejectedAgainstRealVersion("1.0-1");- }-- /**- * Same finding, the explicit-keyword form of a legacy post-release separator: PEP 440 also- * allows {@code -} (not just {@code .}) directly before {@code post}/{@code dev}/{@code rc}.- */- @Test- void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAHyphenSeparatedPostRelease() throws Exception {- assertRequestedVersionRejectedAgainstRealVersion("1.0-post1");- }-- /**- * The fix for a real finding: {@code pre}, {@code preview}, and {@code c} are all PEP 440- * aliases for {@code rc}, and weren't in the continuation list, so a filename using one of- * them could sail through as if it were the plain requested version.- */- @Test- void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAPreReleaseAlias() throws Exception {- assertRequestedVersionRejectedAgainstRealVersion("1.0.pre1");- }-- @Test- void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAPreviewReleaseAlias() throws Exception {- assertRequestedVersionRejectedAgainstRealVersion("1.0.preview1");- }-- @Test- void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAShorthandReleaseCandidateAlias() throws Exception {- assertRequestedVersionRejectedAgainstRealVersion("1.0.c1");- }-- /**- * Same finding, the post-release side: {@code r} is a PEP 440 alias for {@code post}/{@code- * rev}, wasn't recognized either, and sits one character shorter than {@code rev} so it needs- * its own coverage.- */- @Test- void fetchPackageVersionAssetRejectsAVersionThatIsOnlyAPrefixOfAShorthandPostReleaseAlias() throws Exception {- assertRequestedVersionRejectedAgainstRealVersion("1.0-r1");- }-- /**- * The fix for a real finding on the opposite side of the same area: a PEP 427 wheel build tag- * ({@code digit+ letter*}, placed between the version and the python/abi/platform tags) looks- * exactly like PEP 440's implicit post-release numeral at the point this check inspects, but- * actually ends the version right there rather than continuing it. {@code demo_pkg-1.0-1-py3-- * none-any.whl} is version {@code 1.0} with build tag {@code 1}, not version {@code 1.0.post1}.- */- @Test- void fetchPackageVersionAssetAcceptsAWheelBuildTagRatherThanTreatingItAsAPostRelease() throws Exception {- PypiserverSidecarManager manager = manager();- seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);- respondWithSimpleIndex("demo-pkg", "demo_pkg-1.0-1-py3-none-any.whl");- backend.createContext("/packages/demo_pkg-1.0-1-py3-none-any.whl", exchange -> {- byte[] body = "wheel bytes".getBytes(StandardCharsets.UTF_8);- exchange.sendResponseHeaders(200, body.length);- exchange.getResponseBody().write(body);- exchange.close();- });-- Optional<byte[]> result = manager.fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",- "1.0", "demo_pkg-1.0-1-py3-none-any.whl");-- assertTrue(result.isPresent());- assertEquals("wheel bytes", new String(result.get(), StandardCharsets.UTF_8));- }-- /**- * The fix for a real finding: a package name can itself contain a run of digits that looks- * like a version, and a legacy sdist filename (pre-dating the PEP 625 escaping modern- * {@code demo_pkg-1.0.0.tar.gz}-style filenames in this test class use) keeps the project- * name's own hyphens literal rather than underscoring them. A package literally named {@code- * foo-1.0-bar} at version {@code 2.0} then produces the filename {@code- * foo-1.0-bar-2.0.tar.gz}; the old check only looked at what followed a {@code -<version>}- * match, so the {@code -1.0-} inside the name portion looked like a complete, valid version- * {@code 1.0} on its own, serving the version-2.0 asset for a request asking for 1.0. Fixed- * by also requiring that what precedes the match is exactly the requested package name,- * normalized the same way the simple-index lookup normalizes it; this filenameEmbedsVersion- * check now fails before any request is even sent.- */- @Test- void fetchPackageVersionAssetRejectsAVersionThatOnlyMatchesInsideThePackageNamePortion() throws Exception {- PypiserverSidecarManager manager = manager();- seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);-- Optional<byte[]> result = manager.fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "foo-1.0-bar",- "1.0", "foo-1.0-bar-2.0.tar.gz");-- assertTrue(result.isEmpty());- }-- private void assertRequestedVersionRejectedAgainstRealVersion(String realVersion) throws Exception {- PypiserverSidecarManager manager = manager();- seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);- String filename = "demo_pkg-" + realVersion + ".tar.gz";- respondWithSimpleIndex("demo-pkg", filename);- backend.createContext("/packages/" + filename, exchange -> {- byte[] body = "should never be served".getBytes(StandardCharsets.UTF_8);- exchange.sendResponseHeaders(200, body.length);- exchange.getResponseBody().write(body);- exchange.close();- });-- Optional<byte[]> result = manager.fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",- "1.0", filename);-- assertTrue(result.isEmpty(), "version 1.0 must not match " + filename);- }-- /**- * The fix for a real finding: the package-index check used to be a plain substring search,- * which a shorter real filename that happens to be a substring of a different, longer real- * filename could pass incorrectly.- */- @Test- void fetchPackageVersionAssetRequiresAnExactIndexEntryNotASubstringMatch() throws Exception {- PypiserverSidecarManager manager = manager();- seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);- // The index genuinely lists a longer filename that contains the requested, shorter one as- // a substring; the shorter one was never actually published on its own.- respondWithSimpleIndex("demo-pkg", "demo_pkg-1.0.0.tar.gz.asc");- backend.createContext("/packages/demo_pkg-1.0.0.tar.gz", exchange -> {- byte[] body = "should never be served".getBytes(StandardCharsets.UTF_8);- exchange.sendResponseHeaders(200, body.length);- exchange.getResponseBody().write(body);- exchange.close();- });-- Optional<byte[]> result = manager.fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",- "1.0.0", "demo_pkg-1.0.0.tar.gz");-- assertTrue(result.isEmpty());- }-- @Test- void fetchPackageVersionAssetReturnsEmptyWhenThePackageWasNeverUploaded() throws Exception {- PypiserverSidecarManager manager = manager();- seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);- backend.createContext("/simple/demo-pkg/", exchange -> {- exchange.sendResponseHeaders(404, -1);- exchange.close();- });-- Optional<byte[]> result = manager.fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",- "1.0.0", "missing.whl");-- assertTrue(result.isEmpty());- }-- /**- * The fix for a real finding: pypiserver serves every file it holds flatly, regardless of- * which package it belongs to, so without checking the named package's own simple index first,- * a caller could name a real file that belongs to a different package in the same repository- * and get it back under the wrong package's name.- */- @Test- void fetchPackageVersionAssetReturnsEmptyWhenTheFileBelongsToADifferentPackage() throws Exception {- PypiserverSidecarManager manager = manager();- seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);- respondWithSimpleIndex("demo-pkg", "demo_pkg-1.0.0.tar.gz");- backend.createContext("/packages/other_pkg-1.0.0.tar.gz", exchange -> {- byte[] body = "should never be served".getBytes(StandardCharsets.UTF_8);- exchange.sendResponseHeaders(200, body.length);- exchange.getResponseBody().write(body);- exchange.close();- });-- Optional<byte[]> result = manager.fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "demo-pkg",- "1.0.0", "other_pkg-1.0.0.tar.gz");-- assertTrue(result.isEmpty());- }-- @Test- void fetchPackageVersionAssetThrowsRatherThanReportingNotFoundOnASidecarServerError() throws Exception {- PypiserverSidecarManager manager = manager();- seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);- backend.createContext("/simple/demo-pkg/", exchange -> {- exchange.sendResponseHeaders(500, -1);- exchange.close();- });-- assertThrows(IllegalStateException.class, () -> manager.fetchPackageVersionAsset("pypi-repo-1", "dom",- "repo", null, "demo-pkg", "1.0.0", "demo_pkg-1.0.0.tar.gz"));- }-- /**- * The fix for a real finding: the normalized package name used to be spliced straight into- * {@code /simple/<normalized>/}. A {@code /} inside {@code packageName} would then be- * indistinguishable from the real separators around it and could splice in extra path- * segments. Fixed by percent-encoding the normalized name via {@code encodeSegment} before- * it's placed in the path, so a {@code /} inside it survives only as a literal {@code %2F}- * within that one segment. {@code packageName} is {@code a/../b}, not a cleaner {@code ../..}:- * the PEP 503 normalization this method already applies collapses runs of {@code -_.} to a- * single {@code -} first, which would otherwise quietly absorb a pure dot-segment attempt- * before it ever reached the encoding this test is checking.- */- @Test- void fetchPackageVersionAssetTreatsASlashInThePackageNameAsLiteralNotAStructuralSeparator() throws Exception {- PypiserverSidecarManager manager = manager();- seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);- AtomicReference<String> observedRawPath = new AtomicReference<>();- backend.createContext("/", exchange -> {- observedRawPath.set(exchange.getRequestURI().getRawPath());- exchange.sendResponseHeaders(404, -1);- exchange.close();- });-- Optional<byte[]> result = manager.fetchPackageVersionAsset("pypi-repo-1", "dom", "repo", null, "a/../b",- "1.0.0", "a/-/b-1.0.0.tar.gz");-- assertTrue(result.isEmpty());- assertEquals("/simple/a%2F-%2Fb/", observedRawPath.get());- }-- private void respondWithSimpleIndex(String packageName, String filename) {- String html = "<a href=\"/packages/" + filename + "\">" + filename + "</a>";- backend.createContext("/simple/" + packageName + "/", exchange -> {- byte[] bytes = html.getBytes(StandardCharsets.UTF_8);- exchange.getResponseHeaders().set("Content-Type", "text/html");- exchange.sendResponseHeaders(200, bytes.length);- exchange.getResponseBody().write(bytes);- exchange.close();- });- }-@Testvoid stopManagedContainersStopsEveryContainerThePoolIsTracking() throws Exception {PypiserverSidecarManager manager = manager();