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 <didev@dinid.net>
This commit is contained in:
Jakob Borg 2023-10-02 08:40:03 +02:00 committed by GitHub
parent a8486b0468
commit 296db314f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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{