Skip to content

Commit

Permalink
lib/config: Improve parsing of gui-address overrides (syncthing#9144)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
calmh and digtail committed Oct 2, 2023
1 parent a8486b0 commit 296db31
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions lib/config/guiconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down

0 comments on commit 296db31

Please sign in to comment.