Skip to content

Go: Explicitly check whether proxy env vars are empty #19598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025

Conversation

mbg
Copy link
Member

@mbg mbg commented May 27, 2025

This should resolve github/codeql-action#2909 after we didn't catch this in #19248. If the environment variables are set, but contain the empty string, then we treat those empty strings as meaningful values, even though they are not.

@Copilot Copilot AI review requested due to automatic review settings May 27, 2025 14:00
@mbg mbg requested a review from a team as a code owner May 27, 2025 14:00
@github-actions github-actions bot added the Go label May 27, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refines proxy environment variable handling to explicitly ignore empty string values, ensuring that unintentionally set empty vars are treated as unset.

  • Added && var != "" checks to skip empty proxy env vars for host, port, CA certificate, and URLs.
  • Maintains existing formatting of proxy address and logging only when both host and port are non-empty.

Comment on lines +53 to +54
if proxy_host, proxy_host_set := os.LookupEnv(PROXY_HOST); proxy_host_set && proxy_host != "" {
if proxy_port, proxy_port_set := os.LookupEnv(PROXY_PORT); proxy_port_set && proxy_port != "" {
Copy link
Preview

Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

[nitpick] The pattern of checking for both existence and non-empty value is repeated across multiple env vars. Consider creating a helper such as lookupNonEmptyEnv(key string) (string, bool) to encapsulate this logic and reduce duplication.

Suggested change
if proxy_host, proxy_host_set := os.LookupEnv(PROXY_HOST); proxy_host_set && proxy_host != "" {
if proxy_port, proxy_port_set := os.LookupEnv(PROXY_PORT); proxy_port_set && proxy_port != "" {
if proxy_host, _ := lookupNonEmptyEnv(PROXY_HOST); proxy_host != "" {
if proxy_port, _ := lookupNonEmptyEnv(PROXY_PORT); proxy_port != "" {

Copilot uses AI. Check for mistakes.

@@ -82,7 +82,7 @@ func getEnvVars() []string {
}
}

if proxy_urls, proxy_urls_set := os.LookupEnv(PROXY_URLS); proxy_urls_set {
if proxy_urls, proxy_urls_set := os.LookupEnv(PROXY_URLS); proxy_urls_set && proxy_urls != "" {
Copy link
Preview

Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

Values containing only whitespace will pass the != "" check and may cause parsing errors. Consider using strings.TrimSpace(proxy_urls) before testing for emptiness to treat whitespace-only inputs as empty.

Suggested change
if proxy_urls, proxy_urls_set := os.LookupEnv(PROXY_URLS); proxy_urls_set && proxy_urls != "" {
if proxy_urls, proxy_urls_set := os.LookupEnv(PROXY_URLS); proxy_urls_set && strings.TrimSpace(proxy_urls) != "" {

Copilot uses AI. Check for mistakes.

Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

Looks plausible

@mbg mbg merged commit 8c39f61 into main May 27, 2025
13 checks passed
@mbg mbg deleted the mbg/go/handle-empty-strings-in-proxy-vars branch May 27, 2025 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Building a go library fails due to proxy errors (Default setup)
2 participants