Skip to content

fix: unquote environment variable passed to hook commands#410

Draft
zimeg wants to merge 2 commits intomainfrom
zimeg-fix-env-quote
Draft

fix: unquote environment variable passed to hook commands#410
zimeg wants to merge 2 commits intomainfrom
zimeg-fix-env-quote

Conversation

@zimeg
Copy link
Member

@zimeg zimeg commented Mar 18, 2026

Changelog

Environment variable values set for hook commands are no longer surrounded with double quotes.

Summary

This PR fixes an issue where environment variables were set with double quotes surrounding values:

// MapToStringSlice converts a map[string]string to a slice of strings with
// elements key="value" from the map with an optional prefix
func MapToStringSlice(args map[string]string, prefix string) []string {
var res = []string{}
for name, value := range args {
if len(value) > 0 {
var escapedValue string
if runtime.GOOS == "windows" {
escapedValue = strings.ReplaceAll(value, `"`, "`\"")
} else {
escapedValue = strings.ReplaceAll(value, `"`, `\"`)
}
res = append(res, fmt.Sprintf(`%s%s="%s"`, prefix, name, escapedValue))
}
}
return res
}

	// Env specifies the environment of the process.
	// Each entry is of the form "key=value".

📚 Reference: https://pkg.go.dev/os/exec#Cmd

Requirements

@zimeg zimeg added this to the Next Release milestone Mar 18, 2026
@zimeg zimeg self-assigned this Mar 18, 2026
@zimeg zimeg added bug M-T: confirmed bug report. Issues are confirmed when the reproduction steps are documented semver:patch Use on pull requests to describe the release version increment labels Mar 18, 2026
@codecov
Copy link

codecov bot commented Mar 18, 2026

Codecov Report

❌ Patch coverage is 50.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.21%. Comparing base (8056444) to head (5b2cf70).

Files with missing lines Patch % Lines
internal/pkg/platform/localserver.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #410      +/-   ##
==========================================
- Coverage   68.24%   68.21%   -0.03%     
==========================================
  Files         218      218              
  Lines       18049    18051       +2     
==========================================
- Hits        12318    12314       -4     
- Misses       4578     4585       +7     
+ Partials     1153     1152       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member Author

@zimeg zimeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💌 Notes on code for the amazing reviewers! I am also planning to follow up with testing notes before asking for particular review 🧪 ✨

var cmdEnvVars = os.Environ()
cmdEnvVars = append(cmdEnvVars, goutils.MapToStringSlice(opts.Env, "")...)
for name, value := range opts.Env {
cmdEnvVars = append(cmdEnvVars, name+"="+value)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔭 note: These values are set for both the default and message-boundaries protocols:

cmd := opts.Exec.Command(cmdEnvVars, stdout, stderr, opts.Stdin, cmdArgs[0], cmdArgVars...)

cmd := opts.Exec.Command(cmdEnvVars, &stdout, stderr, opts.Stdin, cmdArgs[0], cmdArgVars...)

// Command creates a command ready to be run with the current processes shell
func (sh ShellExec) Command(env []string, stdout io.Writer, stderr io.Writer, stdin io.Reader, name string, arg ...string) ShellCommand {
cmd := sh.command(name, arg...)
cmd.Env = env

// To avoid removing any environment variables that are set in the current environment, we first set the cmd.Env to the current environment.
// before adding any new environment variables.
var cmdEnvVars = os.Environ()
cmdEnvVars = append(cmdEnvVars, goutils.MapToStringSlice(sdkManagedConnectionStartHookOpts.Env, "")...)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏁 note: I understand the MapToStringSlice utilities to be useful in escaping command line arguments but we might not need this with the direct environment variable value setting in commands.

🏆 ramble: This implementation in localserver is the other find of a direct call to the hooks command executor so shares the change!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug M-T: confirmed bug report. Issues are confirmed when the reproduction steps are documented semver:patch Use on pull requests to describe the release version increment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant