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

Code before:
// Copyright (C) 2014 The Syncthing Authors.
//
// 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"
	"strconv"
	"strings"
)

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

func (c GUIConfiguration) IsAuthEnabled() bool {
	return c.AuthMode == AuthModeLDAP || (len(c.User) > 0 && len(c.Password) > 0)
}

func (c 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

Review comment (security):
The usual pattern for fields that shouldn't be accessed directly is prepending `Raw`, d.h. field is `RawUnixSocketPermissions` and the method below `UnixSocketPermissions`.

Code after:
// Copyright (C) 2014 The Syncthing Authors.
//
// 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"
	"strconv"
	"strings"
)

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

func (c GUIConfiguration) IsAuthEnabled() bool {
	return c.AuthMode == AuthModeLDAP || (len(c.User) > 0 && len(c.Password) > 0)
}

func (c 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