11package handler
22
33import (
4+ "context"
45 "io"
56 "log/slog"
67 "net/http"
@@ -9,6 +10,8 @@ import (
910 "sync/atomic"
1011 "testing"
1112 "time"
13+
14+ "github.com/git-pkgs/registries/fetch"
1215)
1316
1417const testProxyURL = "http://localhost:8080"
@@ -302,13 +305,13 @@ func TestConanV2FileRoutesCacheArchives(t *testing.T) {
302305 filename string
303306 }{
304307 {"recipe" , "/v2/conans/zlib/1.3.1/_/_/revisions/rrev1/files/conan_sources.tgz" ,
305- "rrev1_conan_sources .tgz" },
308+ "_/_/rrev1/conan_sources .tgz" },
306309 {"package" , "/v2/conans/zlib/1.3.1/_/_/revisions/rrev1/packages/pkgid1/revisions/prev1/files/conan_package.tgz" ,
307- "rrev1_pkgid1_prev1_conan_package .tgz" },
310+ "_/_/rrev1/packages/pkgid1/prev1/conan_package .tgz" },
308311 {"recipe xz" , "/v2/conans/zlib/1.3.1/_/_/revisions/rrev1/files/conan_export.txz" ,
309- "rrev1_conan_export .txz" },
312+ "_/_/rrev1/conan_export .txz" },
310313 {"package zstd" , "/v2/conans/zlib/1.3.1/_/_/revisions/rrev1/packages/pkgid1/revisions/prev1/files/conan_package.tzst" ,
311- "rrev1_pkgid1_prev1_conan_package .tzst" },
314+ "_/_/rrev1/packages/pkgid1/prev1/conan_package .tzst" },
312315 }
313316
314317 for _ , tt := range tests {
@@ -362,9 +365,9 @@ func TestConanUserChannelKeptApart(t *testing.T) {
362365
363366 for _ , ref := range []struct { path , body , filename string }{
364367 {"/v2/conans/zlib/1.3.1/_/_/revisions/rrev1/files/conan_sources.tgz" ,
365- "center sources" , "rrev1_conan_sources .tgz" },
368+ "center sources" , "_/_/rrev1/conan_sources .tgz" },
366369 {"/v2/conans/zlib/1.3.1/acme/stable/revisions/rrev1/files/conan_sources.tgz" ,
367- "acme sources" , "acme@stable_rrev1_conan_sources .tgz" },
370+ "acme sources" , "acme/stable/rrev1/conan_sources .tgz" },
368371 } {
369372 fetcher .artifact = artifactBody (ref .body )
370373 fetcher .fetchCalled = false
@@ -651,3 +654,102 @@ func TestConanMetadataPreservesStatusCodes(t *testing.T) {
651654 upstream .Close ()
652655 }
653656}
657+
658+ const (
659+ conanRecipeLookalike = "/v2/conans/zlib/1.3/_/_/revisions/R_P_Q/files/conan_package.tgz"
660+ conanPackageFile = "/v2/conans/zlib/1.3/_/_/revisions/R/packages/P/revisions/Q/files/conan_package.tgz"
661+ )
662+
663+ // conanPathEchoServer answers every file with its own path, so each URL has
664+ // different bytes.
665+ func conanPathEchoServer (t * testing.T , available * atomic.Bool ) * httptest.Server {
666+ t .Helper ()
667+ upstream := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
668+ if ! available .Load () {
669+ http .Error (w , "unavailable" , http .StatusServiceUnavailable )
670+ return
671+ }
672+ _ , _ = w .Write ([]byte (r .URL .Path ))
673+ }))
674+ t .Cleanup (upstream .Close )
675+ return upstream
676+ }
677+
678+ func serveConanRequest (h * ConanHandler , target string ) * httptest.ResponseRecorder {
679+ w := httptest .NewRecorder ()
680+ h .Routes ().ServeHTTP (w , httptest .NewRequest (http .MethodGet , target , nil ))
681+ return w
682+ }
683+
684+ // A recipe revision with underscores must not share a cache entry with a
685+ // package whose revision, reference and package revision spell it.
686+ func TestConanFileKeysKeepPathFieldsApart (t * testing.T ) {
687+ var available atomic.Bool
688+ available .Store (true )
689+ upstream := conanPathEchoServer (t , & available )
690+ proxy , _ , _ , _ := setupTestProxy (t )
691+ proxy .Fetcher = fetch .NewFetcher (fetch .WithHTTPClient (upstream .Client ()), fetch .WithMaxRetries (0 ))
692+ h := NewConanHandlerWithUpstream (proxy , testProxyURL , upstream .URL )
693+
694+ for _ , target := range []string {conanRecipeLookalike , conanPackageFile } {
695+ if w := serveConanRequest (h , target ); w .Code != http .StatusOK || w .Body .String () != target {
696+ t .Errorf ("GET %s = %d %q, want its own file" , target , w .Code , w .Body .String ())
697+ }
698+ }
699+ }
700+
701+ // A file cached under the name used before path fields were kept apart is
702+ // still served for the URL it was fetched from, and only for that URL.
703+ func TestConanLegacyFileEntryServedForItsURL (t * testing.T ) {
704+ var available atomic.Bool
705+ available .Store (true )
706+ upstream := conanPathEchoServer (t , & available )
707+ proxy , _ , _ , _ := setupTestProxy (t )
708+ proxy .Fetcher = fetch .NewFetcher (fetch .WithHTTPClient (upstream .Client ()), fetch .WithMaxRetries (0 ))
709+ h := NewConanHandlerWithUpstream (proxy , testProxyURL , upstream .URL )
710+
711+ legacy , err := proxy .GetOrFetchArtifactFromURL (context .Background (), "conan" , "zlib" , "1.3" ,
712+ "R_P_Q_conan_package.tgz" , upstream .URL + conanRecipeLookalike )
713+ if err != nil {
714+ t .Fatalf ("seed legacy entry: %v" , err )
715+ }
716+ _ = legacy .Reader .Close ()
717+
718+ if w := serveConanRequest (h , conanPackageFile ); w .Body .String () != conanPackageFile {
719+ t .Errorf ("package file = %d %q, want its own file, not the legacy entry" , w .Code , w .Body .String ())
720+ }
721+ available .Store (false )
722+ if w := serveConanRequest (h , conanRecipeLookalike ); w .Code != http .StatusOK || w .Body .String () != conanRecipeLookalike {
723+ t .Errorf ("recipe file with the upstream down = %d %q, want the legacy entry" , w .Code , w .Body .String ())
724+ }
725+ }
726+
727+ // A path value is decoded, so one holding an encoded slash could spell the
728+ // cache key of another reference. Those requests are refused.
729+ func TestConanFileRoutesRefuseEncodedSlash (t * testing.T ) {
730+ var available atomic.Bool
731+ available .Store (true )
732+ upstream := conanPathEchoServer (t , & available )
733+ proxy , _ , _ , _ := setupTestProxy (t )
734+ proxy .Fetcher = fetch .NewFetcher (fetch .WithHTTPClient (upstream .Client ()), fetch .WithMaxRetries (0 ))
735+ h := NewConanHandlerWithUpstream (proxy , testProxyURL , upstream .URL )
736+
737+ if w := serveConanRequest (h , conanPackageFile ); w .Code != http .StatusOK || w .Body .String () != conanPackageFile {
738+ t .Fatalf ("GET %s = %d %q, want its own file" , conanPackageFile , w .Code , w .Body .String ())
739+ }
740+
741+ const recipe , pkg = "/v2/conans/zlib/1.3/_/_/revisions/" , "/v2/conans/zlib/1.3/_/_/revisions/R/packages/"
742+ for field , target := range map [string ]string {
743+ "revision spelling a package file" : recipe + "R%2Fpackages%2FP%2FQ/files/conan_package.tgz" ,
744+ "user" : "/v2/conans/zlib/1.3/a%2Fb/_/revisions/R/files/conan_package.tgz" ,
745+ "channel" : "/v2/conans/zlib/1.3/_/a%2Fb/revisions/R/files/conan_package.tgz" ,
746+ "recipe filename" : recipe + "R/files/a%2Fconan_package.tgz" ,
747+ "package reference" : pkg + "P%2FX/revisions/Q/files/conan_package.tgz" ,
748+ "package revision" : pkg + "P/revisions/Q%2FX/files/conan_package.tgz" ,
749+ "package filename" : pkg + "P/revisions/Q/files/a%2Fconan_package.tgz" ,
750+ } {
751+ if w := serveConanRequest (h , target ); w .Code != http .StatusBadRequest {
752+ t .Errorf ("%s: GET %s = %d %q, want 400" , field , target , w .Code , w .Body .String ())
753+ }
754+ }
755+ }
0 commit comments