From 413bb884aa6e7558d3b02bee4703b43d493a3387 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Sun, 25 Oct 2026 15:51:49 -0400 Subject: [PATCH] fix(codeartifact): close two gaps left by #6435's status overrides (#6506) The npm republish guard's lenient-scan fallback (for a readme over the strict parser's string-length limit) only extracted version names for the Archived/Disposed check, leaving readmeByVersion empty; forwardSpooled's post-write bookkeeping (byte accounting, readme capture, and the redispose-protection call) keys off that map, so a version going through this fallback silently skipped all of it. The lenient envelope now populates the full guard input. A PyPI upload with no version form field skipped both the filename-vs-field check and the Archived/Disposed guard outright, since neither had a coordinate to check (hectorvent's follow-up on #6435). pypiserver trusts the uploaded filename over the form regardless, so a missing field now falls back to matching the filename against any version of the package that currently carries a status override before letting the upload through. --- .../CodeArtifactNpmDataPlane.java | 39 ++++++--- .../CodeArtifactPypiController.java | 55 ++++++++++++- .../codeartifact/CodeArtifactService.java | 80 +++++++++++++++++++ .../NpmPublishEnvelopeScanner.java | 25 ++++-- .../CodeArtifactNpmDataPlaneTest.java | 43 ++++++++++ .../CodeArtifactPypiControllerTest.java | 61 ++++++++++++++ .../codeartifact/CodeArtifactServiceTest.java | 23 ++++++ 7 files changed, 305 insertions(+), 21 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlane.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlane.java index 8591b2a4f9..69053f80ec 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlane.java +++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlane.java @@ -500,27 +500,42 @@ public class CodeArtifactNpmDataPlane { // NpmPublishEnvelopeScanner#scanLeniently's own javadoc), not because anything is // actually wrong with it; StreamConstraintsException is specifically that case, not // genuinely malformed JSON (a plain JsonParseException, handled below exactly as - // before). That second, far-more-tolerant pass recovers just the version names so the - // guard can still run; if even that fails, there is no safe way left to know what - // this publish names, so it is refused outright instead of silently forwarded with no - // guard having run. - Set versionsFromLenientScan; + // before). That second, far-more-tolerant pass recovers the full envelope (not just + // the version names) so this can populate readmeByVersion the normal way too: {@link + // #forwardSpooled}'s own post-write bookkeeping, byte accounting, readme capture, and + // the override-clearing call that undoes a concurrent dispose racing the write (see + // CodeArtifactService#forgetContainerBackedStatusOverrideIfUnchanged's own javadoc), + // all key off readmeByVersion alone; leaving it empty here, as an earlier version of + // this fix did, would have let the guard below run but then skipped every one of + // those for whichever version it let through. If even this more tolerant pass fails, + // there is no safe way left to know what this publish names, so it is refused + // outright instead of silently forwarded with no guard having run. + NpmPublishEnvelopeScanner.Envelope lenientEnvelope; try (InputStream in = new BufferedInputStream(Files.newInputStream(Path.of(spoolPath)))) { - versionsFromLenientScan = NpmPublishEnvelopeScanner.scanLeniently(in).versions().keySet(); + lenientEnvelope = NpmPublishEnvelopeScanner.scanLeniently(in); } catch (IOException lenientFailure) { return new RepublishCheck(false, 0L, identity, Map.of(), "Could not determine which version(s) this publish names.", Map.of()); } - for (String version : versionsFromLenientScan) { + long lenientTotalAttachmentBytes = lenientEnvelope.attachments().values().stream() + .mapToLong(NpmPublishEnvelopeScanner.Attachment::size) + .sum(); + Map lenientReadmeByVersion = new HashMap<>(); + lenientEnvelope.versions().forEach((version, v) -> lenientReadmeByVersion.put(version, v.readme())); + Map lenientBeforeRevisionByVersion = new HashMap<>(); + for (String version : lenientReadmeByVersion.keySet()) { try { - service.requireContainerBackedVersionNotArchivedOrDisposed(scope.region(), npmRequest.domain(), - scope.owner(), npmRequest.repository(), "npm", identity.namespace(), - identity.packageName(), version); + String beforeRevision = service.requireContainerBackedVersionNotArchivedOrDisposed( + scope.region(), npmRequest.domain(), scope.owner(), npmRequest.repository(), "npm", + identity.namespace(), identity.packageName(), version); + lenientBeforeRevisionByVersion.put(version, beforeRevision); } catch (AwsException blocked) { - return new RepublishCheck(false, 0L, identity, Map.of(), blocked.getMessage(), Map.of()); + return new RepublishCheck(false, lenientTotalAttachmentBytes, identity, lenientReadmeByVersion, + blocked.getMessage(), lenientBeforeRevisionByVersion); } } - return new RepublishCheck(false, 0L, identity, Map.of(), null, Map.of()); + return new RepublishCheck(false, lenientTotalAttachmentBytes, identity, lenientReadmeByVersion, null, + lenientBeforeRevisionByVersion); } catch (IOException e) { LOG.debugv(e, "Publish for {0}/{1} is not a confirmable republish: {2}", npmRequest.domain(), npmRequest.repository(), e.getMessage()); 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.java index 785d85135f..4d3fff686e 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 @@ -27,6 +27,7 @@ import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.Arrays; import java.util.Locale; +import java.util.Map; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; @@ -196,10 +197,26 @@ public class CodeArtifactPypiController { // missing/blank version is the same case the override-clearing call below already // skips: no coordinate to block either way. String beforeRevision = null; - if (version != null && !version.isBlank()) { + // Raised by hectorvent's follow-up on this same PR: a missing/blank version field + // must not let this guard be skipped just because there is no explicit coordinate to + // check directly. pypiserver trusts the filename over the form fields regardless (see + // PypiserverSidecarClient#filenameEmbedsVersion's own javadoc), so an Archived/Disposed + // version this upload's own filename names is found by checking it against every + // version of this package that currently carries a status override, the same guard + // below runs against once one is found. A real version field always wins when present: + // this fallback only matters for a client that omits it. + String guardPackageName = packageName; + String guardVersion = version; + if (guardVersion == null || guardVersion.isBlank()) { + ArchivedOrDisposedMatch match = archivedOrDisposedVersionFromFilename(handle, domain, repository, + file.get().filename()); + guardPackageName = match == null ? null : match.packageName(); + guardVersion = match == null ? null : match.version(); + } + if (guardVersion != null) { try { beforeRevision = service.requireContainerBackedVersionNotArchivedOrDisposed(handle.region(), - domain, handle.owner(), repository, "pypi", null, packageName, version); + domain, handle.owner(), repository, "pypi", null, guardPackageName, guardVersion); } catch (AwsException e) { return Response.status(409).entity(e.getMessage()).build(); } @@ -318,6 +335,40 @@ public class CodeArtifactPypiController { return name.toLowerCase(Locale.ROOT).replaceAll("[-_.]+", "-"); } + private record ArchivedOrDisposedMatch(String packageName, String version) { + } + + /** + * The exact (package, version) pair a status override currently marks Archived or Disposed + * that {@code filename} actually names, found by checking it against every override this + * repository has at all, never trusting the form's own {@code name} field for the lookup + * (Greptile's review: the form's {@code name} field can name an unrelated, un-overridden + * package while the uploaded filename itself matches a disposed one; searching by that field + * alone would miss it, since pypiserver indexes the real upload by the filename, never the + * form). Returns the override's own recorded package name, not the form's, so the caller's own + * guard call checks the coordinate the filename actually names. {@code null} when nothing + * matches, including the overwhelming common case, a repository with no overrides to check + * against at all. + */ + private ArchivedOrDisposedMatch archivedOrDisposedVersionFromFilename(RepositoryHandle handle, String domain, + String repository, String filename) { + Map> overriddenStatusesByPackage = + service.containerBackedOverriddenVersionStatusesByRepository(handle.region(), domain, handle.owner(), + repository, "pypi"); + for (Map.Entry> packageEntry : overriddenStatusesByPackage.entrySet()) { + String overriddenPackageName = packageEntry.getKey(); + for (Map.Entry versionEntry : packageEntry.getValue().entrySet()) { + String status = versionEntry.getValue(); + if (("Archived".equals(status) || "Disposed".equals(status)) + && PypiserverSidecarClient.filenameEmbedsVersion(filename, overriddenPackageName, + versionEntry.getKey())) { + return new ArchivedOrDisposedMatch(overriddenPackageName, versionEntry.getKey()); + } + } + } + return null; + } + /** * An exact entry match, not a substring search: a shorter real filename that happens to be a * substring of a different, longer real filename (or of surrounding markup) must not pass 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 bbc6c6fae7..406ebb49ee 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 @@ -237,6 +237,19 @@ public class CodeArtifactService implements Resettable { // ever parsing a stored key back into parts. private final ConcurrentHashMap> containerBackedOverriddenVersionsByPackage = new ConcurrentHashMap<>(); + // Raised by Greptile's review of this PR: a namespace-less format's own caller can have no + // reliable package-version coordinate at all to look an override up by directly (a PyPI twine + // upload's version form field is optional, and its name field is no more trustworthy without + // one, since pypiserver indexes by the uploaded filename, never the form). This answers "which + // package names in this one repository+format currently have any override at all", kept under + // a (version- and package-free) repository+format key, so a caller with only a filename can + // test it against every override that actually exists instead of trusting an unverified + // coordinate. Only ever written for a namespace-less format: Maven's GAV path always supplies a + // reliable version on its own, so it never needs this, and folding its own real namespace in + // here too would risk two different namespaces' same-spelled package name colliding into one + // entry for no caller that would ever need it. + private final ConcurrentHashMap> containerBackedOverriddenPackagesByRepository = + new ConcurrentHashMap<>(); /** * {@code publishedEpochSeconds} is carried forward from whichever value was already true for @@ -538,6 +551,10 @@ public class CodeArtifactService implements Resettable { .filter(k -> k.startsWith(ownerScopedOriginPrefix)) .toList() .forEach(containerBackedOverriddenVersionsByPackage::remove); + containerBackedOverriddenPackagesByRepository.keySet().stream() + .filter(k -> k.startsWith(ownerScopedOriginPrefix)) + .toList() + .forEach(containerBackedOverriddenPackagesByRepository::remove); return r; } @@ -1767,6 +1784,11 @@ public class CodeArtifactService implements Resettable { accountingName)); containerBackedOverriddenVersionsByPackage.computeIfAbsent(packageKey, k -> ConcurrentHashMap.newKeySet()) .add(normalizedVersion); + if (namespace == null) { + String repoFormatKey = ownerScopedKey(owner, repositoryFormatKey(region, domain, repository, format)); + containerBackedOverriddenPackagesByRepository.computeIfAbsent(repoFormatKey, + k -> ConcurrentHashMap.newKeySet()).add(accountingName); + } } /** @@ -1965,6 +1987,45 @@ public class CodeArtifactService implements Resettable { return result; } + /** + * Every package in this one repository+format that currently carries a status override on any + * version at all, each by its own version-to-status map, for a caller with no reliable + * package-version coordinate of its own to look an override up by directly. Raised by + * Greptile's follow-up review of this PR: a real twine upload's {@code version} form field is + * optional, and its {@code name} field is no more trustworthy without one, since pypiserver + * indexes a publish by the uploaded filename itself, never the form ({@code + * PypiserverSidecarClient#filenameEmbedsVersion}'s own javadoc); a wrong {@code name} field + * would otherwise have {@code CodeArtifactPypiController#upload} search the wrong package's + * overrides entirely and never find a real Archived/Disposed one the filename actually names. + * Only ever answers for a namespace-less format ({@link + * #containerBackedOverriddenPackagesByRepository}'s own javadoc explains why): the caller + * matches {@code filename} against whichever of these (package, version) pairs it actually + * names, trusting this store's own recorded package name over whatever the caller's own + * request happened to claim. Empty for the overwhelming common case, a repository no one has + * ever called {@code UpdatePackageVersionsStatus}/{@code DisposePackageVersions} against for + * this format. + */ + public synchronized Map> containerBackedOverriddenVersionStatusesByRepository( + String region, String domain, String domainOwner, String repository, String format) { + String owner = effectiveOwner(domainOwner); + Set packageNames = containerBackedOverriddenPackagesByRepository.get( + ownerScopedKey(owner, repositoryFormatKey(region, domain, repository, format))); + if (packageNames == null || packageNames.isEmpty()) { + return Map.of(); + } + Map> result = new LinkedHashMap<>(); + for (String packageName : packageNames) { + Map overrides = containerBackedOverridesForPackage(owner, region, + domain, repository, format, null, packageName); + if (!overrides.isEmpty()) { + Map statuses = new LinkedHashMap<>(); + overrides.forEach((version, override) -> statuses.put(version, override.status())); + result.put(packageName, statuses); + } + } + return result; + } + /** * Unlike {@code GetPackageVersionAsset}, real CodeArtifact's {@code DescribePackageVersion} * response has no asset listing at all (confirmed against the wire model: {@code @@ -2179,6 +2240,13 @@ public class CodeArtifactService implements Resettable { .toList() .forEach(containerBackedPackageVersionStatusOverrides::remove); containerBackedOverriddenVersionsByPackage.remove(ownerScopedKey(owner, accountingKey)); + if (namespace == null) { + Set packageNames = containerBackedOverriddenPackagesByRepository.get( + ownerScopedKey(owner, repositoryFormatKey(region, domain, repository, format))); + if (packageNames != null) { + packageNames.remove(accountingPackageName(format, packageName)); + } + } } else { List versions = genericPackageVersions(region, domain, owner, repository, format, namespace, packageName); @@ -3326,6 +3394,7 @@ public class CodeArtifactService implements Resettable { packageVersionReadmes.clear(); containerBackedPackageVersionStatusOverrides.clear(); containerBackedOverriddenVersionsByPackage.clear(); + containerBackedOverriddenPackagesByRepository.clear(); authorizationTokens.clear(); if (inMemory) { memoryAssetStore.clear(); @@ -3566,6 +3635,13 @@ public class CodeArtifactService implements Resettable { .toList() .forEach(containerBackedPackageVersionStatusOverrides::remove); containerBackedOverriddenVersionsByPackage.remove(ownerScopedKey); + if (namespace == null) { + Set packageNames = containerBackedOverriddenPackagesByRepository.get( + ownerScopedKey(owner, repositoryFormatKey(region, domain, repository, format))); + if (packageNames != null) { + packageNames.remove(accountingPackageName(format, packageName)); + } + } } /** @@ -3956,6 +4032,10 @@ public class CodeArtifactService implements Resettable { return region + "::" + domain + "::" + repository; } + private static String repositoryFormatKey(String region, String domain, String repository, String format) { + return repositoryKey(region, domain, repository) + "::" + format; + } + private static String packageVersionKey(String region, String domain, String repository, String format, String namespace, String packageName, String version) { return region + "::" + domain + "::" + repository + "::" + format + "::" diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/NpmPublishEnvelopeScanner.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/NpmPublishEnvelopeScanner.java index ee1653a80a..379e9e4e0d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/NpmPublishEnvelopeScanner.java +++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/NpmPublishEnvelopeScanner.java @@ -97,13 +97,24 @@ final class NpmPublishEnvelopeScanner { /** * {@link #LENIENT_FACTORY}'s own fallback parse, for a caller ({@link - * CodeArtifactNpmDataPlane#checkRepublish}) that still needs {@code versions()}' own key set - * once {@link #scan} itself has already thrown; see that factory's own javadoc for why a - * second, more tolerant pass is the only way to recover it at all. Its {@code readme} and - * {@code attachments} are not meant to be trusted the way {@link #scan}'s own result is (the - * whole reason this parse exists is that {@link #scan} itself refused to vouch for one of - * those same fields), so a caller must only ever read {@code versions().keySet()} from this, - * never anything else about the Envelope it returns. + * CodeArtifactNpmDataPlane#checkRepublish}) that still needs the full envelope once {@link + * #scan} itself has already thrown; see that factory's own javadoc for why a second, more + * tolerant pass is the only way to recover any of it at all. Both parses run the identical + * scanning logic over the identical bytes (see {@link #scan(InputStream, JsonFactory)}, which + * both this and {@link #scan} delegate to), only with a higher string-length ceiling, so every + * field this returns, {@code readme} and {@code attachments} included, is exactly as reliable + * as {@link #scan}'s own result (raised by Greptile's review, which this PR's own bookkeeping + * fix already relies on: {@link CodeArtifactNpmDataPlane#checkRepublish}'s own post-write + * byte accounting, readme capture, and redispose-protection call all key off this result's + * {@code readmeByVersion}, not just {@code versions().keySet()}). + * + *

What a caller must still never do with this result on its own: treat it as grounds for + * {@code CodeArtifactNpmDataPlane.RepublishCheck#confirmed} (a byte-identical idempotent + * retry safe to skip forwarding entirely). That determination compares the envelope against + * the sidecar's own stored packument content, which {@link #scan} itself threw before this + * fallback ever ran for, and {@code checkRepublish} always forwards on this path rather than + * attempting it; this parse's own tolerance has no bearing on that separate guarantee either + * way. */ static Envelope scanLeniently(InputStream in) throws IOException { return scan(in, LENIENT_FACTORY); diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlaneTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlaneTest.java index 203416acf2..1ec26732cf 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlaneTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactNpmDataPlaneTest.java @@ -460,6 +460,49 @@ class CodeArtifactNpmDataPlaneTest { assertEquals(false, upstreamReceivedPut.get()); } + /** + * Raised by Greptile's review of the fix above: the guard passing is not the whole story. + * {@link CodeArtifactNpmDataPlane#forwardSpooled}'s own post-write bookkeeping, including the + * override-clearing call that undoes a concurrent dispose racing this exact write (see + * {@code CodeArtifactService#forgetContainerBackedStatusOverrideIfUnchanged}'s own javadoc), + * keys entirely off {@code readmeByVersion}. An earlier version of this fix left that map + * empty for the oversized-readme fallback, which let the guard run but then skipped every one + * of those for whichever version it let through, the exact gap a concurrent dispose could + * still exploit afterward. This version's own forward must still reach that bookkeeping. + */ + @Test + void aPublishWhoseReadmeOverflowsTheStrictScanStillRunsPostWriteBookkeeping() throws Exception { + upstream = vertx.createHttpServer() + .requestHandler(request -> request.response().setStatusCode(201).end()) + .listen(0, "127.0.0.1") + .toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS); + + CodeArtifactService service = mock(CodeArtifactService.class); + when(service.resolveAuthorizationToken("good-token", DOMAIN)) + .thenReturn(Optional.of(new AuthorizationTokenScope("000000000000", "us-east-1"))); + when(service.ensureFormatContainerId("npm", "us-east-1", DOMAIN, "000000000000", REPOSITORY)) + .thenReturn(NPM_REPOSITORY_ID); + VerdaccioSidecarClient verdaccioClient = mock(VerdaccioSidecarClient.class); + when(verdaccioClient.ensureReady(eq(NPM_REPOSITORY_ID), anyString())) + .thenReturn("http://127.0.0.1:" + upstream.actualPort()); + startDataPlane(service, verdaccioClient); + + byte[] content = "fresh bytes".getBytes(StandardCharsets.UTF_8); + String oversizedReadme = "x".repeat(1_000_001); + String envelope = "{\"name\":\"my-pkg\",\"versions\":{\"1.0.0\":{\"name\":\"my-pkg\",\"version\":\"1.0.0\"," + + "\"readme\":\"" + oversizedReadme + "\",\"dist\":{\"tarball\":\"http://ignored/my-pkg-1.0.0.tgz\"," + + "\"integrity\":\"" + integrity(content) + "\"}}},\"_attachments\":{\"my-pkg-1.0.0.tgz\":{" + + "\"content_type\":\"application/octet-stream\",\"data\":\"" + + Base64.getEncoder().encodeToString(content) + "\",\"length\":" + content.length + "}}}"; + + HttpResponse response = put("/codeartifact/npm/" + DOMAIN + "/" + REPOSITORY + "/my-pkg", "good-token", + envelope); + + assertEquals(201, response.statusCode()); + verify(service).forgetContainerBackedStatusOverrideIfUnchanged(eq("us-east-1"), eq(DOMAIN), + eq("000000000000"), eq(REPOSITORY), eq("npm"), isNull(), eq("my-pkg"), eq("1.0.0"), isNull()); + } + /** * The identity used to look up an existing match comes from the URL the publish was sent to, * not the envelope's own {@code name} field. A mismatched body must not be checked against 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.java index c551deae52..5ca8ca1db8 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 @@ -15,6 +15,7 @@ import java.net.InetSocketAddress; import java.net.http.HttpClient; import java.nio.charset.StandardCharsets; import java.time.Duration; +import java.util.Map; import java.util.Optional; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; @@ -435,6 +436,66 @@ class CodeArtifactPypiControllerTest { anyLong(), anyLong()); } + /** + * hectorvent's follow-up on this PR: a missing version field must not let an upload of an + * Archived/Disposed version through just because there is no explicit coordinate to check. + * The uploaded filename itself names version 1.0.0, which this package's only override marks + * Disposed, so the guard must still fire even though the form carries no version field at all. + */ + @Test + void uploadWithNoVersionFieldStillRejectsAFilenameNamingAnArchivedOrDisposedVersion() { + AtomicBoolean uploadReceived = new AtomicBoolean(); + backend.createContext("/simple/my-pkg/", exchange -> respond(exchange, 404, "text/plain", "")); + backend.createContext("/", exchange -> { + uploadReceived.set(true); + respond(exchange, 200, "text/plain", ""); + }); + when(service.containerBackedOverriddenVersionStatusesByRepository(any(), eq(DOMAIN), any(), eq(REPOSITORY), + eq("pypi"))).thenReturn(Map.of("my-pkg", Map.of("1.0.0", "Disposed"))); + doThrow(new AwsException("ConflictException", "Package version '1.0.0' of package 'my-pkg' is Disposed " + + "and cannot be republished.", 409)) + .when(service).requireContainerBackedVersionNotArchivedOrDisposed(any(), eq(DOMAIN), any(), + eq(REPOSITORY), eq("pypi"), isNull(), eq("my-pkg"), eq("1.0.0")); + byte[] body = multipartWithFile("boundary123", "my-pkg", "my_pkg-1.0.0.tar.gz", "new content"); + + Response response = controller.upload( + headers("Bearer good", "multipart/form-data; boundary=boundary123"), DOMAIN, REPOSITORY, body); + + assertEquals(409, response.getStatus()); + assertEquals(false, uploadReceived.get()); + } + + /** + * Greptile's follow-up review of this PR: the form's own {@code name} field is no more + * trustworthy than its missing {@code version} field, since pypiserver indexes a real upload + * by the filename, never the form. {@code other-pkg}'s own overrides (there are none) must not + * be the only ones checked: the filename actually names {@code my-pkg} version {@code 1.0.0}, + * which is Disposed, so the guard must still fire even though the form's {@code name} claims + * an unrelated package. + */ + @Test + void uploadWithNoVersionFieldStillRejectsAFilenameNamingADifferentArchivedOrDisposedPackage() { + AtomicBoolean uploadReceived = new AtomicBoolean(); + backend.createContext("/simple/other-pkg/", exchange -> respond(exchange, 404, "text/plain", "")); + backend.createContext("/", exchange -> { + uploadReceived.set(true); + respond(exchange, 200, "text/plain", ""); + }); + when(service.containerBackedOverriddenVersionStatusesByRepository(any(), eq(DOMAIN), any(), eq(REPOSITORY), + eq("pypi"))).thenReturn(Map.of("my-pkg", Map.of("1.0.0", "Disposed"))); + doThrow(new AwsException("ConflictException", "Package version '1.0.0' of package 'my-pkg' is Disposed " + + "and cannot be republished.", 409)) + .when(service).requireContainerBackedVersionNotArchivedOrDisposed(any(), eq(DOMAIN), any(), + eq(REPOSITORY), eq("pypi"), isNull(), eq("my-pkg"), eq("1.0.0")); + byte[] body = multipartWithFile("boundary123", "other-pkg", "my_pkg-1.0.0.tar.gz", "new content"); + + Response response = controller.upload( + headers("Bearer good", "multipart/form-data; boundary=boundary123"), DOMAIN, REPOSITORY, body); + + assertEquals(409, response.getStatus()); + assertEquals(false, uploadReceived.get()); + } + /** A missing version field, same as the readme capture right below it, has no coordinate to clear. */ @Test void uploadWithNoVersionFieldNeverClearsAnyStatusOverride() { 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 9086dd1cab..e2518f34f4 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 @@ -3967,6 +3967,29 @@ class CodeArtifactServiceTest { verify(reposiliteClient, never()).fetchPackageVersionAsset(any(), any(), any(), any(), any(), any(), any()); } + @Test + void containerBackedOverriddenVersionStatusesByRepositoryReturnsEveryOverriddenPackageInOneFormat() { + service.createDomain(REGION, "dom", null, Map.of()); + CodeArtifactRepository created = service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); + String repoId = created.getSidecarContainerIds().get("pypi"); + when(pypiserverClient.listPackageVersions(eq(repoId), eq("dom"), eq("repo"), isNull(), eq("my-pkg"))) + .thenReturn(List.of( + new RepositorySidecarManager.PackageVersionTimestamp("1.0.0", Instant.ofEpochSecond(1000)))); + when(pypiserverClient.listPackageVersions(eq(repoId), eq("dom"), eq("repo"), isNull(), eq("other-pkg"))) + .thenReturn(List.of( + new RepositorySidecarManager.PackageVersionTimestamp("2.0.0", Instant.ofEpochSecond(2000)))); + + service.updatePackageVersionsStatus(REGION, "dom", null, "repo", "pypi", null, "my-pkg", List.of("1.0.0"), + null, null, "Archived"); + service.updatePackageVersionsStatus(REGION, "dom", null, "repo", "pypi", null, "other-pkg", List.of("2.0.0"), + null, null, "Unlisted"); + + assertEquals(Map.of("my-pkg", Map.of("1.0.0", "Archived"), "other-pkg", Map.of("2.0.0", "Unlisted")), + service.containerBackedOverriddenVersionStatusesByRepository(REGION, "dom", null, "repo", "pypi")); + assertEquals(Map.of(), + service.containerBackedOverriddenVersionStatusesByRepository(REGION, "dom", null, "repo", "npm")); + } + @Test void disposePackageVersionsDeletesEveryRealAssetAndKeepsListingTheVersionAsDisposed() { service.createDomain(REGION, "dom", null, Map.of());