From 296db314f5c89057b92e617dd1b5459aa4effb3f Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 2 Oct 2023 08:40:03 +0200 Subject: [PATCH] lib/config: Improve parsing of gui-address overrides (#9144) improve parsing of gui-address overrides make checks for whether the gui-address is overridden consistent by checking whether the environment variable is set and not an empty string. the `Network()` function however checked for the inclusion of a slash instead of the presence of any characters. If the config file's gui address was set to a unix socket and the gui override to a tcp address, then the function would have wrongly returned "unix". the `URL()` function always returned the config file's gui address if a unix socket was configured, even if an override was specified. the `URL()` function wrongly formatted unix addresses. the http(s) protocol was used as the sheme and the path was percent escaped. because of the previous bug, this could only be triggered if the config file's gui address was tcp and an unix socket override was given. simplify the `useTLS()` function's codepath for overrides. Co-authored-by: digital --- lib/config/guiconfiguration.go | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/config/guiconfiguration.go b/lib/config/guiconfiguration.go index 9f5df227f..77832a349 100644 --- a/lib/config/guiconfiguration.go +++ b/lib/config/guiconfiguration.go @@ -60,14 +60,12 @@ func (c GUIConfiguration) UnixSocketPermissions() os.FileMode { } func (c GUIConfiguration) Network() string { - if override := os.Getenv("STGUIADDRESS"); strings.Contains(override, "/") { + if override := os.Getenv("STGUIADDRESS"); override != "" { url, err := url.Parse(override) - if err != nil { - return "tcp" - } - if strings.HasPrefix(url.Scheme, "unix") { + if err == nil && strings.HasPrefix(url.Scheme, "unix") { return "unix" } + return "tcp" } if strings.HasPrefix(c.RawAddress, "/") { return "unix" @@ -77,19 +75,17 @@ func (c GUIConfiguration) Network() string { func (c GUIConfiguration) UseTLS() bool { if override := os.Getenv("STGUIADDRESS"); override != "" { - if strings.HasPrefix(override, "http") { - return strings.HasPrefix(override, "https:") - } - if strings.HasPrefix(override, "unix") { - return strings.HasPrefix(override, "unixs:") - } + return strings.HasPrefix(override, "https:") || strings.HasPrefix(override, "unixs:") } return c.RawUseTLS } func (c GUIConfiguration) URL() string { - if strings.HasPrefix(c.RawAddress, "/") { - return "unix://" + c.RawAddress + if c.Network() == "unix" { + if c.UseTLS() { + return "unixs://" + c.Address() + } + return "unix://" + c.Address() } u := url.URL{