Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions backend/internal/crowdsec/hub_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,10 +930,11 @@ func (s *HubService) extractTarGz(ctx context.Context, archive []byte, targetDir
if hdr.FileInfo().Mode()&os.ModeType != 0 && !hdr.FileInfo().Mode().IsRegular() && !hdr.FileInfo().IsDir() {
continue
}
cleanName := filepath.Clean(hdr.Name)
if hasParentComponent(cleanName) || filepath.IsAbs(cleanName) {
// IsLocal rejects absolute, empty and ".." component names (but accepts "..foo").
if !filepath.IsLocal(hdr.Name) {
return fmt.Errorf("unsafe path %s", hdr.Name)
}
cleanName := filepath.Clean(hdr.Name)
if IsEngineOwnedPath(cleanName) {
continue
}
Expand Down Expand Up @@ -1025,14 +1026,3 @@ func isGzip(data []byte) bool {
}
return data[0] == 0x1f && data[1] == 0x8b
}

// hasParentComponent reports whether any path component equals "..".
// Names that merely start with two dots (e.g. "..foo") are legitimate.
func hasParentComponent(name string) bool {
for _, part := range strings.Split(filepath.ToSlash(name), "/") {
if part == ".." {
return true
}
}
return false
}
29 changes: 29 additions & 0 deletions backend/internal/crowdsec/hub_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2299,3 +2299,32 @@ func TestHubBaseCandidates_DistinctRolesKeepOrder(t *testing.T) {
defaultHubBaseURL,
}, svc.hubBaseCandidates())
}

func TestExtractTarGzLocalPathEdgeCases(t *testing.T) {
t.Parallel()
svc := NewHubService(nil, nil, t.TempDir())

t.Run("accepts dot directory entry", func(t *testing.T) {
t.Parallel()
targetDir := t.TempDir()
buf := &bytes.Buffer{}
gw := gzip.NewWriter(buf)
tw := tar.NewWriter(gw)
require.NoError(t, tw.WriteHeader(&tar.Header{Name: "./", Typeflag: tar.TypeDir, Mode: 0o755}))
require.NoError(t, tw.WriteHeader(&tar.Header{Name: "./a.txt", Mode: 0o644, Size: 2}))
_, err := tw.Write([]byte("ok"))
require.NoError(t, err)
require.NoError(t, tw.Close())
require.NoError(t, gw.Close())
require.NoError(t, svc.extractTarGz(context.Background(), buf.Bytes(), targetDir))
require.FileExists(t, filepath.Join(targetDir, "a.txt"))
})

t.Run("rejects empty name", func(t *testing.T) {
t.Parallel()
archive := makeTarGz(t, map[string]string{"": "bad"})
err := svc.extractTarGz(context.Background(), archive, t.TempDir())
require.Error(t, err)
require.Contains(t, err.Error(), "unsafe path")
})
}
Loading