Skip to content

Commit 2cadbbd

Browse files
committed
fix/batches: reject Git metadata in repository archives
1 parent c4030b5 commit 2cadbbd

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

internal/batches/workspace/bind_workspace.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,15 @@ func unzip(ctx context.Context, zipFile, dest string) error {
206206
return fmt.Errorf("%s: illegal file path", fpath)
207207
}
208208

209+
relativePath, err := filepath.Rel(dest, fpath)
210+
if err != nil {
211+
return err
212+
}
213+
firstPathElement, _, _ := strings.Cut(relativePath, string(os.PathSeparator))
214+
if strings.EqualFold(firstPathElement, ".git") {
215+
return fmt.Errorf("%s: repository archive contains Git metadata", f.Name)
216+
}
217+
209218
if f.FileInfo().IsDir() {
210219
if err := mkdirAll(dest, f.Name, 0777); err != nil {
211220
return err

internal/batches/workspace/bind_workspace_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,29 @@ func TestDockerBindWorkspaceCreator_Create(t *testing.T) {
143143
})
144144
}
145145

146+
func TestUnzipRejectsGitMetadata(t *testing.T) {
147+
for _, name := range []string{
148+
".git/config",
149+
".git/hooks/pre-commit",
150+
"dir/../.git/config",
151+
".GIT/config",
152+
} {
153+
t.Run(name, func(t *testing.T) {
154+
archivePath := zipUpFiles(t, t.TempDir(), map[string]string{name: "malicious"})
155+
dest := t.TempDir()
156+
157+
err := unzip(context.Background(), archivePath, dest)
158+
if err == nil || !strings.Contains(err.Error(), "repository archive contains Git metadata") {
159+
t.Fatalf("expected Git metadata error, got %v", err)
160+
}
161+
162+
if _, err := os.Stat(filepath.Join(dest, ".git")); !os.IsNotExist(err) {
163+
t.Fatalf("expected .git not to be extracted, got %v", err)
164+
}
165+
})
166+
}
167+
}
168+
146169
func TestDockerBindWorkspace_ApplyDiff(t *testing.T) {
147170
// Create a zip file for all the other tests to use.
148171
fakeFilesTmpDir := t.TempDir()

0 commit comments

Comments
 (0)