Visitar URL original
fix(tarball): normalize paths when matching files (#2334) · google/go-containerregistry@09fe1e5 · GitHub
Skip to content

Commit 09fe1e5

Browse files
authored
fix(tarball): normalize paths when matching files (#2334)
Some tools generate tarballs with ./ prefix in paths (e.g. ./manifest.json). Use path.Clean on both header name and target path to ensure they match correctly even if they have different formatting. This fixes loading images that have ./ prefixed paths, which was causing errors like "file manifest.json not found in tar".
1 parent 5baa399 commit 09fe1e5

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎pkg/v1/tarball/image.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func followLinks(opener Opener, filePath string, visited map[string]bool) (io.Re
248248
if err != nil {
249249
return nil, err
250250
}
251-
if hdr.Name == filePath {
251+
if path.Clean(hdr.Name) == path.Clean(filePath) {
252252
if hdr.Typeflag == tar.TypeSymlink || hdr.Typeflag == tar.TypeLink {
253253
currentDir := filepath.Dir(filePath)
254254
return followLinks(opener, path.Join(currentDir, path.Clean(hdr.Linkname)), visited)

‎pkg/v1/tarball/image_test.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,35 @@ func TestManifestEmptyDiffIDs(t *testing.T) {
241241
t.Errorf("img.Manifest(): expected error mentioning diff_ids, got: %v", err)
242242
}
243243
}
244+
245+
func TestLoadManifestPrefixPath(t *testing.T) {
246+
var buf bytes.Buffer
247+
tw := tar.NewWriter(&buf)
248+
manifestBytes := []byte(`[]`)
249+
if err := tw.WriteHeader(&tar.Header{
250+
Name: "./manifest.json",
251+
Mode: 0644,
252+
Size: int64(len(manifestBytes)),
253+
}); err != nil {
254+
t.Fatal(err)
255+
}
256+
if _, err := tw.Write(manifestBytes); err != nil {
257+
t.Fatal(err)
258+
}
259+
if err := tw.Close(); err != nil {
260+
t.Fatal(err)
261+
}
262+
263+
content := buf.Bytes()
264+
opener := func() (io.ReadCloser, error) {
265+
return io.NopCloser(bytes.NewReader(content)), nil
266+
}
267+
268+
manifest, err := LoadManifest(opener)
269+
if err != nil {
270+
t.Fatalf("LoadManifest() failed: %v", err)
271+
}
272+
if len(manifest) != 0 {
273+
t.Errorf("expected empty manifest, got %v", manifest)
274+
}
275+
}

0 commit comments

Comments
 (0)