From e0619edacfbc1a5ceecbbef6df6d1b55811ceb7b Mon Sep 17 00:00:00 2001 From: Praveen Kumar Date: Wed, 23 Aug 2023 11:24:55 +0530 Subject: [PATCH] proxy: Update trimTrailingEOL function to handle carriage return (\r) 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 --- pkg/crc/network/httpproxy/proxy.go | 2 +- pkg/crc/network/httpproxy/proxy_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/crc/network/httpproxy/proxy.go b/pkg/crc/network/httpproxy/proxy.go index f04cc99921..0ecca07948 100644 --- a/pkg/crc/network/httpproxy/proxy.go +++ b/pkg/crc/network/httpproxy/proxy.go @@ -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) { diff --git a/pkg/crc/network/httpproxy/proxy_test.go b/pkg/crc/network/httpproxy/proxy_test.go index 054dd1b5bc..ec048a10e5 100644 --- a/pkg/crc/network/httpproxy/proxy_test.go +++ b/pkg/crc/network/httpproxy/proxy_test.go @@ -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("")) +}