Repository: syncthing/syncthing
File: lib/config/guiconfiguration.go (Go)

Code before:
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

package config

import (
	"net/url"
	"os"
	"regexp"
	"strconv"
	"strings"

	"golang.org/x/crypto/bcrypt"

	"github.com/syncthing/syncthing/lib/rand"
)

type GUIConfiguration struct {
	Enabled                   bool     `json:"enabled" xml:"enabled,attr" default:"true"`
	RawAddress                string   `json:"address" xml:"address" default:"127.0.0.1:8384"`
	RawUnixSocketPermissions  string   `json:"unixSocketPermissions" xml:"unixSocketPermissions,omitempty"`
	User                      string   `json:"user" xml:"user,omitempty"`
	Password                  string   `json:"password" xml:"password,omitempty"`
	AuthMode                  AuthMode `json:"authMode" xml:"authMode,omitempty"`
	PromMetricsNoAuth         bool     `json:"promMetricsNoAuth" xml:"promMetricsNoAuth" default:"false"`
	RawUseTLS                 bool     `json:"useTLS" xml:"tls,attr"`
	APIKey                    string   `json:"apiKey" xml:"apikey,omitempty"`
	InsecureAdminAccess       bool     `json:"insecureAdminAccess" xml:"insecureAdminAccess,omitempty"`
	Theme                     string   `json:"theme" xml:"theme" default:"default"`
	Debugging                 bool     `json:"debugging" xml:"debugging,attr"`
	InsecureSkipHostCheck     bool     `json:"insecureSkipHostcheck" xml:"insecureSkipHostcheck,omitempty"`
	InsecureAllowFrameLoading bool     `json:"insecureAllowFrameLoading" xml:"insecureAllowFrameLoading,omitempty"`
	SendBasicAuthPrompt       bool     `json:"sendBasicAuthPrompt" xml:"sendBasicAuthPrompt,attr"`
}

func (c GUIConfiguration) IsAuthEnabled() bool {
	// This function should match isAuthEnabled() in syncthingController.js
	return c.AuthMode == AuthModeLDAP || (len(c.User) > 0 && len(c.Password) > 0)
}

func (GUIConfiguration) IsOverridden() bool {
	return os.Getenv("STGUIADDRESS") != ""
}

func (c GUIConfiguration) Address() string {
	if override := os.Getenv("STGUIADDRESS"); override != "" {
		// This value may be of the form "scheme://address:port" or just
		// "address:port". We need to chop off the scheme. We try to parse it as
		// an URL if it contains a slash. If that fails, return it as is and let
		// some other error handling handle it.

Review comment (security):
```suggestion
	MetricsWithoutAuth         bool     `json:"metricsWithoutAuth" xml:"metricsWithoutAuth" default:"false"`
```

Code after:
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

package config

import (
	"net/url"
	"os"
	"regexp"
	"strconv"
	"strings"

	"golang.org/x/crypto/bcrypt"

	"github.com/syncthing/syncthing/lib/rand"
)

type GUIConfiguration struct {
	Enabled                   bool     `json:"enabled" xml:"enabled,attr" default:"true"`
	RawAddress                string   `json:"address" xml:"address" default:"127.0.0.1:8384"`
	RawUnixSocketPermissions  string   `json:"unixSocketPermissions" xml:"unixSocketPermissions,omitempty"`
	User                      string   `json:"user" xml:"user,omitempty"`
	Password                  string   `json:"password" xml:"password,omitempty"`
	AuthMode                  AuthMode `json:"authMode" xml:"authMode,omitempty"`
	MetricsWithoutAuth        bool     `json:"metricsWithoutAuth" xml:"metricsWithoutAuth" default:"false"`
	RawUseTLS                 bool     `json:"useTLS" xml:"tls,attr"`
	APIKey                    string   `json:"apiKey" xml:"apikey,omitempty"`
	InsecureAdminAccess       bool     `json:"insecureAdminAccess" xml:"insecureAdminAccess,omitempty"`
	Theme                     string   `json:"theme" xml:"theme" default:"default"`
	Debugging                 bool     `json:"debugging" xml:"debugging,attr"`
	InsecureSkipHostCheck     bool     `json:"insecureSkipHostcheck" xml:"insecureSkipHostcheck,omitempty"`
	InsecureAllowFrameLoading bool     `json:"insecureAllowFrameLoading" xml:"insecureAllowFrameLoading,omitempty"`
	SendBasicAuthPrompt       bool     `json:"sendBasicAuthPrompt" xml:"sendBasicAuthPrompt,attr"`
}

func (c GUIConfiguration) IsAuthEnabled() bool {
	// This function should match isAuthEnabled() in syncthingController.js
	return c.AuthMode == AuthModeLDAP || (len(c.User) > 0 && len(c.Password) > 0)
}

func (GUIConfiguration) IsOverridden() bool {
	return os.Getenv("STGUIADDRESS") != ""
}

func (c GUIConfiguration) Address() string {
	if override := os.Getenv("STGUIADDRESS"); override != "" {
		// This value may be of the form "scheme://address:port" or just
		// "address:port". We need to chop off the scheme. We try to parse it as
		// an URL if it contains a slash. If that fails, return it as is and let
		// some other error handling handle it.