88 "fmt"
99 "os"
1010 "sort"
11- "strings"
1211
1312 "github.com/sourcegraph/sourcegraph/lib/errors"
1413
@@ -112,6 +111,10 @@ git commit --quiet --all --allow-empty -m src-action-exec
112111func (wc * dockerVolumeWorkspaceCreator ) unzipRepoIntoVolume (ctx context.Context , w * dockerVolumeWorkspace , zip string ) error {
113112 // We want to mount that temporary file into a Docker container that has the
114113 // workspace volume attached, and unzip it into the volume.
114+ zipMount , err := docker .BindMount (zip , "/tmp/zip" , true )
115+ if err != nil {
116+ return errors .Wrap (err , "creating archive mount" )
117+ }
115118
116119 // We need to keep a temporary file in the volume before unzipping for the
117120 // permissions to persist because... reasons. Rather than reading the
@@ -157,7 +160,7 @@ func (wc *dockerVolumeWorkspaceCreator) unzipRepoIntoVolume(ctx context.Context,
157160 "--rm" ,
158161 "--init" ,
159162 "--workdir" , "/work" ,
160- "--mount" , "type=bind,source=" + zip + ",target=/tmp/zip,ro" ,
163+ "--mount" , zipMount ,
161164 }, w .dockerRunOptsWithUser (w .uidGid , "/work" )... )
162165 opts = append (
163166 opts ,
@@ -177,6 +180,7 @@ func (wc *dockerVolumeWorkspaceCreator) copyFilesIntoVolumes(ctx context.Context
177180 if len (files ) == 0 {
178181 return nil
179182 }
183+ const copyScript = `while test "$#" -gt 0; do cp "$1" "$2" || exit; shift 2; done`
180184
181185 opts := append ([]string {
182186 "run" ,
@@ -192,22 +196,34 @@ func (wc *dockerVolumeWorkspaceCreator) copyFilesIntoVolumes(ctx context.Context
192196 }
193197 sort .Strings (names )
194198
195- var copyCmds []string
196- for _ , name := range names {
199+ var copyArgs []string
200+ for i , name := range names {
201+ if err := validateWorkspaceFileName (name ); err != nil {
202+ return err
203+ }
197204 localPath := files [name ]
205+ // Names originate from the Sourcegraph instance. Keep them out of both
206+ // Docker's comma-delimited mount grammar and the shell program.
207+ mountTarget := fmt .Sprintf ("/tmp/src-additional-file-%d" , i )
208+ mount , err := docker .BindMount (localPath , mountTarget , true )
209+ if err != nil {
210+ return errors .Wrap (err , "creating additional file mount" )
211+ }
198212 opts = append (opts , []string {
199- "--mount" , "type=bind,source=" + localPath + ",target=/tmp/" + name + ",ro" ,
213+ "--mount" , mount ,
200214 }... )
201215
202- copyCmds = append (copyCmds , "cp /tmp/" + name + " /work/"+ name )
216+ copyArgs = append (copyArgs , mountTarget , " /work/"+ name )
203217 }
204218
205219 opts = append (
206220 opts ,
207221 DockerVolumeWorkspaceImage ,
208222 "sh" , "-c" ,
209- strings .Join (copyCmds , " && " )+ ";" ,
223+ copyScript ,
224+ "copy-additional-files" ,
210225 )
226+ opts = append (opts , copyArgs ... )
211227
212228 if out , err := exec .CommandContext (ctx , "docker" , opts ... ).CombinedOutput (); err != nil {
213229 return errors .Wrapf (err , "unzip output:\n \n %s\n \n " , string (out ))
@@ -327,12 +343,17 @@ func (w *dockerVolumeWorkspace) runScript(ctx context.Context, target, script st
327343 return nil , errors .Wrap (err , "generating run options" )
328344 }
329345
346+ scriptMount , err := docker .BindMount (name , "/run.sh" , true )
347+ if err != nil {
348+ return nil , errors .Wrap (err , "creating run script mount" )
349+ }
350+
330351 opts := append ([]string {
331352 "run" ,
332353 "--rm" ,
333354 "--init" ,
334355 "--workdir" , target ,
335- "--mount" , "type=bind,source=" + name + ",target=/run.sh,ro" ,
356+ "--mount" , scriptMount ,
336357 }, common ... )
337358 opts = append (opts , DockerVolumeWorkspaceImage , "sh" , "/run.sh" )
338359
0 commit comments