Skip to content

Commit e844384

Browse files
authored
Merge pull request #47 from buildkite/keithduncan/fix-credential-path-windows
Fix credential.helper path when it includes spaces
2 parents d498373 + 9d82456 commit e844384

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

s3secrets-helper/secrets/secrets.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,31 @@ func handleGitCredentials(conf Config, results <-chan getResult) error {
205205
continue
206206
}
207207
log.Printf("Adding git-credentials in %s/%s as a credential helper", r.bucket, r.key)
208-
helpers = append(helpers, fmt.Sprintf(
209-
"'credential.helper=%s %s %s'",
210-
conf.GitCredentialHelper, r.bucket, r.key,
211-
))
208+
209+
// Replace spaces ' ' in the helper path with an escaped space '\ '
210+
escapedCredentialHelper := strings.ReplaceAll(conf.GitCredentialHelper, " ", "\\ ")
211+
212+
helper := fmt.Sprintf("credential.helper=%s %s %s", escapedCredentialHelper, r.bucket, r.key)
213+
214+
helpers = append(helpers, helper)
212215
}
213216
if len(helpers) == 0 {
214217
return nil
215218
}
216-
env := "GIT_CONFIG_PARAMETERS=\"" + strings.Join(helpers, " ") + "\"\n"
219+
220+
// Build an environment variable for interpretation by a shell
221+
var singleQuotedHelpers []string
222+
for _, helper := range helpers {
223+
// Escape any escape sequences, the shell will interpret the first level
224+
// of escaping.
225+
226+
// Replace backslash '\' with double backslash '\\'
227+
helper = strings.ReplaceAll(helper, "\\", "\\\\")
228+
229+
singleQuotedHelpers = append(singleQuotedHelpers, "'" + helper + "'")
230+
}
231+
env := "GIT_CONFIG_PARAMETERS=\"" + strings.Join(singleQuotedHelpers, " ") + "\"\n"
232+
217233
if _, err := io.WriteString(conf.EnvSink, env); err != nil {
218234
return fmt.Errorf("writing GIT_CONFIG_PARAMETERS env: %w", err)
219235
}

0 commit comments

Comments
 (0)