Skip to content

Commit

Permalink
proxy: Update trimTrailingEOL function to handle carriage return (\r)
Browse files Browse the repository at this point in the history
It will fix the proxy parse issue in case user update the cert on
windows and add blank line at the end of certificate. Unit test is also
added for same.

When the cert file ends with \r\n, trimTrailingEOL would only remove \n
and the file will end with \r. Then this causes issues in regexp which
only replace `\r\n` => `\n` but doesn't perform any action for `\r` and
eventualy turn up this bug.

fixes: #3785
  • Loading branch information
praveenkumar committed Aug 23, 2023
1 parent 97705a0 commit e0619ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/crc/network/httpproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func readProxyCAData(proxyCAFile string) (string, error) {
}

func trimTrailingEOL(s string) string {
return strings.TrimRight(s, "\n")
return strings.TrimRight(s, "\r\n")
}

func NewProxyDefaults(httpProxy, httpsProxy, noProxy, proxyCAFile string) (*ProxyConfig, error) {
Expand Down
12 changes: 12 additions & 0 deletions pkg/crc/network/httpproxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ func TestValidateProxyURL(t *testing.T) {
assert.EqualError(t, ValidateProxyURL("company.com:8080", true), "HTTPS proxy URL 'company.com:8080' is not valid: url should start with http:// or https://")
assert.EqualError(t, ValidateProxyURL("https://company.com", false), "HTTP proxy URL 'https://company.com' is not valid: url should start with http://")
}
func TestTrimTrailingEOL(t *testing.T) {
assert.Equal(t, "foo\nbar", trimTrailingEOL("foo\nbar\n"))
assert.Equal(t, "foo", trimTrailingEOL("foo\n"))
assert.Equal(t, "foo", trimTrailingEOL("foo\r\n"))
assert.Equal(t, "foo\r\nbar", trimTrailingEOL("foo\r\nbar\r\n"))
assert.Equal(t, "foo\r\nbar", trimTrailingEOL("foo\r\nbar\r\n\r\n"))
assert.Equal(t, "foo\nbar", trimTrailingEOL("foo\nbar\n\n"))
assert.Equal(t, "foo\nbar", trimTrailingEOL("foo\nbar\n\n\n"))
assert.Equal(t, "", trimTrailingEOL("\r\n"))
assert.Equal(t, "", trimTrailingEOL("\n"))
assert.Equal(t, "", trimTrailingEOL(""))
}

0 comments on commit e0619ed

Please sign in to comment.