Skip to content

Commit 1a13f11

Browse files
authored
Consolidate shell escaping utilities into shell.go (#12074)
1 parent 7cf926b commit 1a13f11

7 files changed

Lines changed: 150 additions & 239 deletions

File tree

.changeset/patch-consolidate-shell-utils.md

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/workflows/security-guard.lock.yml

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/workflow/mcp_setup_generator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,20 +630,20 @@ func (c *Compiler) generateMCPSetup(yaml *strings.Builder, tools map[string]any,
630630

631631
// Add entrypoint override if specified
632632
if gatewayConfig.Entrypoint != "" {
633-
containerCmd += " --entrypoint " + shellQuote(gatewayConfig.Entrypoint)
633+
containerCmd += " --entrypoint " + shellEscapeArg(gatewayConfig.Entrypoint)
634634
}
635635

636636
containerCmd += " " + containerImage
637637

638638
if len(gatewayConfig.EntrypointArgs) > 0 {
639639
for _, arg := range gatewayConfig.EntrypointArgs {
640-
containerCmd += " " + shellQuote(arg)
640+
containerCmd += " " + shellEscapeArg(arg)
641641
}
642642
}
643643

644644
if len(gatewayConfig.Args) > 0 {
645645
for _, arg := range gatewayConfig.Args {
646-
containerCmd += " " + shellQuote(arg)
646+
containerCmd += " " + shellEscapeArg(arg)
647647
}
648648
}
649649

pkg/workflow/mcp_utilities.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

pkg/workflow/mcp_utilities_test.go

Lines changed: 0 additions & 189 deletions
This file was deleted.

pkg/workflow/shell.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,35 @@ func shellEscapeCommandString(cmd string) string {
6969
// Wrap in double quotes
7070
return "\"" + escaped + "\""
7171
}
72+
73+
// buildDockerCommandWithExpandableVars builds a properly quoted docker command
74+
// that allows ${GITHUB_WORKSPACE} and $GITHUB_WORKSPACE to be expanded at runtime
75+
func buildDockerCommandWithExpandableVars(cmd string) string {
76+
shellLog.Printf("Building docker command with expandable vars (length: %d)", len(cmd))
77+
// Replace ${GITHUB_WORKSPACE} with a placeholder that we'll handle specially
78+
// We want: 'docker run ... -v '"${GITHUB_WORKSPACE}"':'"${GITHUB_WORKSPACE}"':rw ...'
79+
// This closes the single quote, adds the variable in double quotes, then reopens single quote
80+
81+
// Split on ${GITHUB_WORKSPACE} to handle it specially
82+
if strings.Contains(cmd, "${GITHUB_WORKSPACE}") {
83+
parts := strings.Split(cmd, "${GITHUB_WORKSPACE}")
84+
var result strings.Builder
85+
result.WriteString("'")
86+
for i, part := range parts {
87+
if i > 0 {
88+
// Add the variable expansion outside of single quotes
89+
result.WriteString("'\"${GITHUB_WORKSPACE}\"'")
90+
}
91+
// Escape single quotes in the part
92+
escapedPart := strings.ReplaceAll(part, "'", "'\\''")
93+
result.WriteString(escapedPart)
94+
}
95+
result.WriteString("'")
96+
shellLog.Print("Docker command built with expandable GITHUB_WORKSPACE variables")
97+
return result.String()
98+
}
99+
100+
// No GITHUB_WORKSPACE variable, use normal quoting
101+
shellLog.Print("No GITHUB_WORKSPACE variable found, using normal escaping")
102+
return shellEscapeArg(cmd)
103+
}

0 commit comments

Comments
 (0)