mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(codeartifact): block Archived version downloads on native per-format routes (#6782)
GetPackageVersionAsset already 404s an Archived or Disposed version's bytes, but the native per-format passthroughs (Maven's GAV path, npm's tarball route, pypi's filename route, nuget's content route) proxied straight to their own sidecar with no awareness of that override, so a plain mvn/npm/ pip/nuget client could still download an Archived version's real bytes. Disposed already self-resolves: DisposePackageVersions genuinely deletes the sidecar's own bytes, so those routes already 404 for the real reason, nothing to add there. Archived is the real gap, closed the same way for all four formats via one new CodeArtifactService#requireContainerBackedVersionDownloadable check, called right before each controller's own proxy: - Maven and nuget already carry namespace/package/version as path params, so this is a direct call. - npm's tarball GET has no version as its own path segment, only embedded in the filename; detected with the same `/-/` marker-splitting technique nativeUnpublishTarget already uses for its own `/-rev/` marker. - pypi's download route is a bare filename with no package name or version anywhere in the URL at all, so the coordinate is resolved by matching the filename against this repository's own Archived/Disposed status overrides, the same way the upload-side guard already does. Whichever path put the file there (a native upload or CopyPackageVersions), a later status override is found the same way. - npm resolves the requested tarball's real version against Verdaccio's own packument (dist.tarball), and only when the package has a status override at all. Verified live against all four real sidecar containers (Reposilite, Verdaccio, pypiserver, BaGetter): archive a version, confirm the download 404s while the real file is still present, then un-archive and confirm the exact same bytes come back unchanged.
This commit is contained in:
1 parent
4f9dcf049c
commit
74a313a587
14 files changed
+715
-35
No files matched your search
+29
-4
@@ -159,8 +159,11 @@ public class CodeArtifactMavenController {
|
||||
@Produces(MediaType.WILDCARD)
|
||||
public Response fetch(@Context HttpHeaders headers, @PathParam("domain") String domain,
|
||||
@PathParam("repository") String repository, @PathParam("gav") String gav) {
|
||||
String repoId = requireScope(headers, domain, repository).repoId();
|
||||
return reposilite.fetchArtifact(repoId, gav)
|
||||
MavenRequestScope scope = requireScope(headers, domain, repository);
|
||||
if (!isDownloadable(scope, domain, repository, gav)) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
return reposilite.fetchArtifact(scope.repoId(), gav)
|
||||
.map(this::toResponse)
|
||||
.orElseGet(() -> Response.status(Response.Status.NOT_FOUND).build());
|
||||
}
|
||||
@@ -168,12 +171,34 @@ public class CodeArtifactMavenController {
|
||||
@HEAD
|
||||
public Response head(@Context HttpHeaders headers, @PathParam("domain") String domain,
|
||||
@PathParam("repository") String repository, @PathParam("gav") String gav) {
|
||||
String repoId = requireScope(headers, domain, repository).repoId();
|
||||
return reposilite.artifactExists(repoId, gav)
|
||||
MavenRequestScope scope = requireScope(headers, domain, repository);
|
||||
if (!isDownloadable(scope, domain, repository, gav)) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
return reposilite.artifactExists(scope.repoId(), gav)
|
||||
? Response.ok().build()
|
||||
: Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code true} unless {@code gav} names an exact version this service knows is {@code
|
||||
* Archived}: {@link #parseNamespaceAndPackageName}'s own version element is {@code null} for a
|
||||
* bare {@code maven-metadata.xml} path (no specific version to block), which this treats the
|
||||
* same as "not a well-formed, versioned GAV at all": both fall through to Reposilite
|
||||
* unchecked, exactly as {@code deploy} above already does for the identical case.
|
||||
*/
|
||||
private boolean isDownloadable(MavenRequestScope scope, String domain, String repository, String gav) {
|
||||
return parseNamespaceAndPackageName(gav).filter(c -> c[2] != null).map(c -> {
|
||||
try {
|
||||
service.requireContainerBackedVersionDownloadable(scope.owner(), scope.region(), domain, repository,
|
||||
"maven", c[0], c[1], c[2], scope.repoId());
|
||||
return true;
|
||||
} catch (AwsException e) {
|
||||
return false;
|
||||
}
|
||||
}).orElse(true);
|
||||
}
|
||||
|
||||
private Response toResponse(FetchedArtifact artifact) {
|
||||
return Response.ok(artifact.content(), artifact.contentType()).build();
|
||||
}
|
||||
|
||||
+83
-16
@@ -149,24 +149,28 @@ public class CodeArtifactNpmDataPlane {
|
||||
context.request().pause();
|
||||
String repoId = npmRepositoryId;
|
||||
AuthorizationTokenScope tokenScope = scope.get();
|
||||
Optional<TarballDownloadRequest> tarballTarget = nativeTarballDownloadTarget(context.request().method(),
|
||||
npmRequest.rest());
|
||||
Optional<PackageIdentity> unpublishTarget = nativeUnpublishTarget(context.request().method(),
|
||||
npmRequest.rest());
|
||||
vertx.<String>executeBlocking(promise -> promise.complete(ensureBackendReady(repoId, npmRequest)))
|
||||
.onComplete(result -> {
|
||||
if (result.failed()) {
|
||||
context.request().resume();
|
||||
context.response().setStatusCode(503).end();
|
||||
return;
|
||||
}
|
||||
proxy(context, result.result(), npmRequest.rest() + querySuffix(context), status ->
|
||||
unpublishTarget.ifPresent(identity -> {
|
||||
if (status == 200 || status == 201) {
|
||||
service.forgetContainerBackedPackage(tokenScope.region(), npmRequest.domain(),
|
||||
tokenScope.owner(), npmRequest.repository(), "npm", identity.namespace(),
|
||||
identity.packageName());
|
||||
}
|
||||
}));
|
||||
});
|
||||
vertx.<String>executeBlocking(promise -> {
|
||||
tarballTarget.ifPresent(target -> blockIfArchived(tokenScope, npmRequest, repoId, target));
|
||||
promise.complete(ensureBackendReady(repoId, npmRequest));
|
||||
}).onComplete(result -> {
|
||||
if (result.failed()) {
|
||||
context.request().resume();
|
||||
context.response().setStatusCode(result.cause() instanceof AwsException ? 404 : 503).end();
|
||||
return;
|
||||
}
|
||||
proxy(context, result.result(), npmRequest.rest() + querySuffix(context), status ->
|
||||
unpublishTarget.ifPresent(identity -> {
|
||||
if (status == 200 || status == 201) {
|
||||
service.forgetContainerBackedPackage(tokenScope.region(), npmRequest.domain(),
|
||||
tokenScope.owner(), npmRequest.repository(), "npm", identity.namespace(),
|
||||
identity.packageName());
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -223,6 +227,69 @@ public class CodeArtifactNpmDataPlane {
|
||||
return Optional.of(packageIdentityFromPath(rest.substring(0, revIndex)));
|
||||
}
|
||||
|
||||
/**
|
||||
* A real tarball download ({@code npm install}, or a direct link from Verdaccio's own web UI)
|
||||
* is a {@code GET} to {@code <packagePath>/-/<filename>}, the exact shape {@code
|
||||
* VerdaccioSidecarClient}'s own tarball requests already use: detected here the same way
|
||||
* {@link #nativeUnpublishTarget} detects its own {@code /-rev/} marker. The version is not a
|
||||
* separate path segment the way Maven's GAV or nuget's content route carry it, only embedded
|
||||
* in {@code filename} itself, so {@link #blockIfArchived} resolves it for real against
|
||||
* Verdaccio's own packument rather than this method guessing it from the filename's own text.
|
||||
*
|
||||
* <p>Raised by Greptile's review: an earlier version of this method assumed {@code filename}
|
||||
* always has the {@code <packageName>-<version>.tgz} shape this emulator's own publish path
|
||||
* happens to write, and stripped that known prefix/suffix to read the version straight out of
|
||||
* it. Nothing in the real npm publish protocol requires a client-chosen {@code dist.tarball}
|
||||
* name to follow that convention at all (a version {@code 1.0.0} can legitimately name its
|
||||
* tarball {@code bundle.tgz}), so a filename that didn't fit the assumed shape skipped the
|
||||
* Archived check entirely, and one that happened to fit it by coincidence (a real tarball
|
||||
* literally named {@code <packageName>-release.tgz}) could resolve to the wrong, unrelated
|
||||
* version {@code release}.
|
||||
*/
|
||||
private static Optional<TarballDownloadRequest> nativeTarballDownloadTarget(HttpMethod method, String rest) {
|
||||
if (method != HttpMethod.GET) {
|
||||
return Optional.empty();
|
||||
}
|
||||
int marker = rest.indexOf("/-/");
|
||||
if (marker < 1) {
|
||||
return Optional.empty();
|
||||
}
|
||||
PackageIdentity identity = packageIdentityFromPath(rest.substring(0, marker));
|
||||
String filename = rest.substring(marker + 3);
|
||||
return Optional.of(new TarballDownloadRequest(identity, filename));
|
||||
}
|
||||
|
||||
private record TarballDownloadRequest(PackageIdentity identity, String filename) {}
|
||||
|
||||
/**
|
||||
* Blocks (by throwing {@link AwsException}) only when {@code target}'s own filename resolves
|
||||
* to a version this repository currently has marked {@code Archived}. Checks whether this
|
||||
* package has any status override at all first, a plain in-memory map read, before ever
|
||||
* paying for a live Verdaccio packument fetch: the overwhelming common case, a package
|
||||
* nothing has ever called {@code UpdatePackageVersionsStatus}/{@code DisposePackageVersions}
|
||||
* on, costs nothing beyond that one check. {@link VerdaccioSidecarClient#versionForTarballFilename}
|
||||
* resolves the real version, against the packument itself rather than the filename's own
|
||||
* text, only once there is actually something in this repository worth checking against.
|
||||
*/
|
||||
private void blockIfArchived(AuthorizationTokenScope tokenScope, NpmRequest npmRequest, String repoId,
|
||||
TarballDownloadRequest target) {
|
||||
Map<String, ?> overrides = service.containerBackedOverridesForPackage(tokenScope.owner(), tokenScope.region(),
|
||||
npmRequest.domain(), npmRequest.repository(), "npm", target.identity().namespace(),
|
||||
target.identity().packageName());
|
||||
if (overrides.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Optional<String> version = verdaccioClient.versionForTarballFilename(repoId, npmRequest.domain(),
|
||||
npmRequest.repository(), target.identity().namespace(), target.identity().packageName(),
|
||||
target.filename());
|
||||
if (version.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
service.requireContainerBackedVersionDownloadable(tokenScope.owner(), tokenScope.region(),
|
||||
npmRequest.domain(), npmRequest.repository(), "npm", target.identity().namespace(),
|
||||
target.identity().packageName(), version.get(), repoId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Real npm registries and the pinned Verdaccio image both reject a republish of an existing
|
||||
* version with 409, even for byte-identical content. The AWS CodeArtifact User Guide states the
|
||||
|
||||
+8
-1
@@ -130,7 +130,14 @@ public class CodeArtifactNuGetController {
|
||||
public Response packageContent(@Context HttpHeaders headers, @PathParam("domain") String domain,
|
||||
@PathParam("repository") String repository, @PathParam("id") String id,
|
||||
@PathParam("version") String version, @PathParam("filename") String filename) {
|
||||
String baseUrl = requireRepository(headers, domain, repository).baseUrl();
|
||||
RepositoryHandle handle = requireRepository(headers, domain, repository);
|
||||
try {
|
||||
service.requireContainerBackedVersionDownloadable(handle.owner(), handle.region(), domain, repository,
|
||||
"nuget", null, id, version, handle.repoId());
|
||||
} catch (AwsException e) {
|
||||
return Response.status(404).build();
|
||||
}
|
||||
String baseUrl = handle.baseUrl();
|
||||
String lowerId = SidecarUriUtils.encodeSegment(id.toLowerCase(Locale.ROOT));
|
||||
String encodedVersion = SidecarUriUtils.encodeSegment(version);
|
||||
String lowerFilename = filename.toLowerCase(Locale.ROOT);
|
||||
|
||||
+25
-2
@@ -120,14 +120,37 @@ public class CodeArtifactPypiController {
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* pypi's real download route is a bare filename with no package name or version anywhere in
|
||||
* the URL, unlike Maven's GAV path or nuget's content route, so there is nothing in a download
|
||||
* request itself to resolve the coordinate {@link #archivedOrDisposedVersionFromFilename}
|
||||
* needs. Resolving it against this repository's own status overrides, the same way the upload
|
||||
* guard above already does, rather than a separate map recorded only at upload time: raised by
|
||||
* Greptile's review, an earlier version of this check used exactly that kind of map, recorded
|
||||
* only by a native twine upload, so a version {@code CopyPackageVersions} wrote straight to the
|
||||
* destination sidecar (never touching this controller's own upload handler at all) had no
|
||||
* recorded identity, and archiving it afterward never blocked the download. Resolving against
|
||||
* the override records instead needs no write-time bookkeeping at all: whichever path put the
|
||||
* real file there, an override written later by {@code UpdatePackageVersionsStatus}/{@code
|
||||
* DisposePackageVersions} is found the same way regardless.
|
||||
*/
|
||||
@GET
|
||||
@Path("packages/{filename}")
|
||||
@Produces(MediaType.WILDCARD)
|
||||
public Response downloadFile(@Context HttpHeaders headers, @PathParam("domain") String domain,
|
||||
@PathParam("repository") String repository,
|
||||
@PathParam("filename") String filename) {
|
||||
String baseUrl = requireRepository(headers, domain, repository).baseUrl();
|
||||
return proxyGet(baseUrl, "/packages/" + SidecarUriUtils.encodeSegment(filename));
|
||||
RepositoryHandle handle = requireRepository(headers, domain, repository);
|
||||
ArchivedOrDisposedMatch match = archivedOrDisposedVersionFromFilename(handle, domain, repository, filename);
|
||||
if (match != null) {
|
||||
try {
|
||||
service.requireContainerBackedVersionDownloadable(handle.owner(), handle.region(), domain,
|
||||
repository, "pypi", null, match.packageName(), match.version(), handle.repoId());
|
||||
} catch (AwsException e) {
|
||||
return Response.status(404).build();
|
||||
}
|
||||
}
|
||||
return proxyGet(handle.baseUrl(), "/packages/" + SidecarUriUtils.encodeSegment(filename));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+46
-12
@@ -1995,17 +1995,13 @@ public class CodeArtifactService implements Resettable {
|
||||
* one status that still allows a normal download ("if the Unlisted package version is already
|
||||
* referenced in an npm package-lock.json file, it can still be downloaded and installed").
|
||||
*
|
||||
* <p><b>Known gap, not fixed here (flagged by Qodo review on the PR that added {@code
|
||||
* DisposePackageVersions}):</b> this only guards {@code GetPackageVersionAsset} itself. The
|
||||
* native per-format data planes ({@code CodeArtifactMavenController}'s GAV path, {@code
|
||||
* CodeArtifactNpmDataPlane}'s tarball route, {@code CodeArtifactPypiController}'s raw
|
||||
* filename-only download route, {@code CodeArtifactNuGetController}'s content route) proxy
|
||||
* straight to each sidecar and never consult this override at all, for any purpose, so a
|
||||
* plain {@code pip install}/{@code npm install}/{@code mvn} fetch against an Archived version
|
||||
* can still download it. Fixing this means threading status awareness into four independently
|
||||
* shaped native protocols (pypi's own download route does not even carry a version in its
|
||||
* URL, only a filename, unlike the other three), a feature-family-sized gap closer to Package
|
||||
* Groups than a quick patch, tracked in the private design memo, not silently dropped.
|
||||
* <p>The native per-format data planes ({@code CodeArtifactMavenController}'s GAV path,
|
||||
* {@code CodeArtifactNpmDataPlane}'s tarball route, {@code CodeArtifactPypiController}'s raw
|
||||
* filename-only download route, {@code CodeArtifactNuGetController}'s content route) each
|
||||
* call this same check too, via {@link #requireContainerBackedVersionDownloadable}, before
|
||||
* proxying a GET straight to their own sidecar: see that method's own javadoc for why only
|
||||
* {@code Archived} needs an active block there (a {@code Disposed} version's native download
|
||||
* already 404s against the sidecar directly, since its bytes are genuinely gone).
|
||||
*/
|
||||
private static void requireAssetDownloadable(String status, String version) {
|
||||
if ("Archived".equals(status) || "Disposed".equals(status)) {
|
||||
@@ -2173,6 +2169,37 @@ public class CodeArtifactService implements Resettable {
|
||||
return override == null ? null : override.revision();
|
||||
}
|
||||
|
||||
/**
|
||||
* The download-side counterpart to {@link #requireContainerBackedVersionNotArchivedOrDisposed}
|
||||
* above: blocks a native per-format download route (Maven's GAV path, npm's tarball route,
|
||||
* nuget's content route, pypi's filename route) from serving an {@code Archived} version's
|
||||
* bytes, the same way {@link #getContainerBackedPackageVersionAsset} already blocks it for
|
||||
* {@code GetPackageVersionAsset} itself, via this exact same {@link #requireAssetDownloadable}.
|
||||
* {@code Disposed} needs no real check here: {@link #deleteContainerBackedVersionAssets}
|
||||
* already deletes the sidecar's own bytes, so a disposed version's native download already
|
||||
* 404s against the sidecar directly, with nothing left here to block, but this still covers
|
||||
* it anyway, so a caller never has to reason about which of the two statuses this does or
|
||||
* doesn't close.
|
||||
*
|
||||
* <p>Unlike {@link #requireContainerBackedVersionNotArchivedOrDisposed}, this takes an
|
||||
* already-resolved {@code owner} and {@code repoId} rather than a raw {@code domainOwner}:
|
||||
* every native controller has already resolved both, {@code owner} from its own token scope,
|
||||
* {@code repoId} from its own {@link #ensureFormatContainerId} call, before it ever reaches
|
||||
* this check, so re-resolving either here would only repeat work the caller already paid for.
|
||||
*
|
||||
* <p>Not {@code synchronized}, unlike that counterpart: nothing here writes, so there is no
|
||||
* race to close, the same reasoning that already leaves {@link
|
||||
* #getContainerBackedPackageVersionAsset}'s own call to {@link
|
||||
* #currentContainerBackedVersionStatus} unsynchronized.
|
||||
*/
|
||||
public void requireContainerBackedVersionDownloadable(String owner, String region, String domain,
|
||||
String repository, String format, String namespace, String packageName, String version,
|
||||
String repoId) {
|
||||
PackageVersionStatusOverride state = currentContainerBackedVersionStatus(owner, region, domain, repository,
|
||||
format, namespace, packageName, version, repoId);
|
||||
requireAssetDownloadable(state.status(), version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Raised by Greptile's review of this same PR: a native publish's own check, real sidecar
|
||||
* write, and override clear are three separate calls (necessarily, since the real write sits
|
||||
@@ -2263,8 +2290,15 @@ public class CodeArtifactService implements Resettable {
|
||||
* then looked up individually in {@link #containerBackedPackageVersionStatusOverrides} itself.
|
||||
* Empty for a package nothing has ever called {@link #updatePackageVersionsStatus} or {@link
|
||||
* #disposePackageVersions} on, which is every container-backed package before this existed.
|
||||
*
|
||||
* <p>Public (not just this class's own internal callers) for the same reason {@link
|
||||
* #containerBackedOverriddenVersionStatusesByRepository} already is: raised by Greptile's
|
||||
* review, a real npm tarball GET names a package directly (unlike pypi's bare filename), so
|
||||
* {@code CodeArtifactNpmDataPlane}'s own download-side guard can check this cheaply, in
|
||||
* memory, before ever paying for a live Verdaccio packument fetch to resolve which version
|
||||
* (if any) a requested filename actually belongs to.
|
||||
*/
|
||||
private Map<String, PackageVersionStatusOverride> containerBackedOverridesForPackage(String owner, String region,
|
||||
public Map<String, PackageVersionStatusOverride> containerBackedOverridesForPackage(String owner, String region,
|
||||
String domain, String repository, String format, String namespace, String packageName) {
|
||||
String accountingName = accountingPackageName(format, packageName);
|
||||
Set<String> versions = containerBackedOverriddenVersionsByPackage.get(ownerScopedKey(owner,
|
||||
|
||||
+28
@@ -23,6 +23,7 @@ import java.time.Instant;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -363,6 +364,33 @@ public class VerdaccioSidecarClient implements RepositorySidecarManager {
|
||||
return tarballFilename.isEmpty() ? null : tarballFilename;
|
||||
}
|
||||
|
||||
/**
|
||||
* The exact version whose tarball Verdaccio's own packument records {@code filename} under,
|
||||
* found by checking every version's own {@link #tarballFilenameFor} rather than assuming
|
||||
* {@code filename} embeds its version in the {@code <packageName>-<version>.tgz} shape this
|
||||
* emulator's own publish path always writes: nothing in the real npm publish protocol
|
||||
* requires a client-chosen {@code dist.tarball} name to follow that convention, so {@code
|
||||
* CodeArtifactNpmDataPlane}'s own download-side Archived guard resolves the real coordinate
|
||||
* here instead of guessing from the filename's own text. Empty when the package doesn't
|
||||
* exist, or no version's own tarball matches.
|
||||
*/
|
||||
public Optional<String> versionForTarballFilename(String repositoryContainerId, String domain,
|
||||
String repository, String namespace, String packageName, String filename) {
|
||||
String baseUrl = ensureReady(repositoryContainerId, publicUrl(domain, repository));
|
||||
JsonNode document = fetchPackageDocument(baseUrl, namespace, packageName).orElse(null);
|
||||
if (document == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
Iterator<String> versions = document.path("versions").fieldNames();
|
||||
while (versions.hasNext()) {
|
||||
String version = versions.next();
|
||||
if (filename.equals(tarballFilenameFor(document, version))) {
|
||||
return Optional.of(version);
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* npm has exactly one asset per version, the tarball: unlike Maven's version directory, there
|
||||
* is no separate checksum sidecar file or secondary artifact to enumerate. Empty when the
|
||||
|
||||
Reference in new issue
Block a user