@@ -143,6 +143,47 @@ func TestDockerBindWorkspaceCreator_Create(t *testing.T) {
143143 })
144144}
145145
146+ func TestPrepareGitRepoRemovesUntrustedGitMetadata (t * testing.T ) {
147+ dir := t .TempDir ()
148+ dotGit := filepath .Join (dir , ".git" )
149+ if err := os .Mkdir (dotGit , 0755 ); err != nil {
150+ t .Fatal (err )
151+ }
152+ maliciousConfig := "[core]\n \t repositoryformatversion = 0\n \t bare = false\n \t fsmonitor = ./fsmonitor\n "
153+ if err := os .WriteFile (filepath .Join (dotGit , "config" ), []byte (maliciousConfig ), 0644 ); err != nil {
154+ t .Fatal (err )
155+ }
156+ if err := os .WriteFile (filepath .Join (dotGit , "attacker-controlled" ), nil , 0644 ); err != nil {
157+ t .Fatal (err )
158+ }
159+ if err := os .WriteFile (filepath .Join (dir , "fsmonitor" ), []byte ("#!/bin/sh\n touch fsmonitor-ran\n " ), 0755 ); err != nil {
160+ t .Fatal (err )
161+ }
162+ if err := os .WriteFile (filepath .Join (dir , "tracked.txt" ), []byte ("tracked\n " ), 0644 ); err != nil {
163+ t .Fatal (err )
164+ }
165+
166+ creator := & dockerBindWorkspaceCreator {}
167+ workspace := & dockerBindWorkspace {dir : dir }
168+ if err := creator .prepareGitRepo (context .Background (), workspace ); err != nil {
169+ t .Fatal (err )
170+ }
171+
172+ if _ , err := os .Stat (filepath .Join (dir , "fsmonitor-ran" )); ! os .IsNotExist (err ) {
173+ t .Fatalf ("fsmonitor command ran on the host: %v" , err )
174+ }
175+ if _ , err := os .Stat (filepath .Join (dotGit , "attacker-controlled" )); ! os .IsNotExist (err ) {
176+ t .Fatalf ("untrusted Git metadata was preserved: %v" , err )
177+ }
178+ config , err := os .ReadFile (filepath .Join (dotGit , "config" ))
179+ if err != nil {
180+ t .Fatal (err )
181+ }
182+ if strings .Contains (string (config ), "fsmonitor" ) {
183+ t .Fatalf ("untrusted Git config was preserved:\n %s" , config )
184+ }
185+ }
186+
146187func TestDockerBindWorkspace_DiffRestoresTrustedGitConfig (t * testing.T ) {
147188 archivePath := zipUpFiles (t , t .TempDir (), map [string ]string {
148189 "tracked.txt" : "before\n " ,
0 commit comments