File size: 4,381 Bytes
6a7089a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | package report
import (
"reflect"
"strings"
"github.com/pinchtab/pinchtab/internal/config"
)
func ApplyRecommendedSecurityDefaults(fc *config.FileConfig) {
defaults := config.DefaultFileConfig()
if fc == nil {
return
}
fc.Server.Bind = defaults.Server.Bind
fc.Security = defaults.Security
if strings.TrimSpace(fc.Server.Token) == "" {
token, err := config.GenerateAuthToken()
if err == nil {
fc.Server.Token = token
}
}
}
func applyRecommendedSecurityDefaults(fc *config.FileConfig) {
ApplyRecommendedSecurityDefaults(fc)
}
func RestoreSecurityDefaults() (string, bool, error) {
fc, configPath, err := config.LoadFileConfig()
if err != nil {
return "", false, err
}
before := securityDefaultsSnapshot(fc)
ApplyRecommendedSecurityDefaults(fc)
after := securityDefaultsSnapshot(fc)
if reflect.DeepEqual(before, after) {
return configPath, false, nil
}
if err := config.SaveFileConfig(fc, configPath); err != nil {
return "", false, err
}
return configPath, true, nil
}
func restoreSecurityDefaults() (string, bool, error) {
return RestoreSecurityDefaults()
}
func RecommendedSecurityDefaultLines(cfg *config.RuntimeConfig) []string {
posture := AssessSecurityPosture(cfg)
ordered := []string{
"server.bind = 127.0.0.1",
"security.allowEvaluate = false",
"security.allowMacro = false",
"security.allowScreencast = false",
"security.allowDownload = false",
"security.allowUpload = false",
"security.attach.enabled = false",
"security.attach.allowHosts = 127.0.0.1,localhost,::1",
"security.attach.allowSchemes = ws,wss",
"security.idpi.enabled = true",
"security.idpi.allowedDomains = 127.0.0.1,localhost,::1",
"security.idpi.strictMode = true",
"security.idpi.scanContent = true",
"security.idpi.wrapContent = true",
}
needed := make(map[string]bool, len(ordered))
for _, check := range posture.Checks {
if check.Passed {
continue
}
switch check.ID {
case "bind_loopback":
needed["server.bind = 127.0.0.1"] = true
case "sensitive_endpoints_disabled":
for _, line := range []string{
"security.allowEvaluate = false",
"security.allowMacro = false",
"security.allowScreencast = false",
"security.allowDownload = false",
"security.allowUpload = false",
} {
needed[line] = true
}
case "attach_disabled", "attach_local_only":
for _, line := range []string{
"security.attach.enabled = false",
"security.attach.allowHosts = 127.0.0.1,localhost,::1",
"security.attach.allowSchemes = ws,wss",
} {
needed[line] = true
}
case "idpi_whitelist_scoped", "idpi_strict_mode", "idpi_content_protection":
for _, line := range []string{
"security.idpi.enabled = true",
"security.idpi.allowedDomains = 127.0.0.1,localhost,::1",
"security.idpi.strictMode = true",
"security.idpi.scanContent = true",
"security.idpi.wrapContent = true",
} {
needed[line] = true
}
}
}
lines := make([]string, 0, len(needed))
for _, line := range ordered {
if needed[line] {
lines = append(lines, line)
}
}
return lines
}
type securityDefaultsState struct {
Bind string
Token string
Security securityConfigValues
}
type securityConfigValues struct {
AllowEvaluate bool
AllowMacro bool
AllowScreencast bool
AllowDownload bool
AllowUpload bool
MaxRedirects int
AttachEnabled bool
IDPI config.IDPIConfig
}
func securityDefaultsSnapshot(fc *config.FileConfig) securityDefaultsState {
if fc == nil {
return securityDefaultsState{}
}
s := securityDefaultsState{
Bind: fc.Server.Bind,
Token: fc.Server.Token,
Security: securityConfigValues{
IDPI: fc.Security.IDPI,
},
}
if fc.Security.AllowEvaluate != nil {
s.Security.AllowEvaluate = *fc.Security.AllowEvaluate
}
if fc.Security.AllowMacro != nil {
s.Security.AllowMacro = *fc.Security.AllowMacro
}
if fc.Security.AllowScreencast != nil {
s.Security.AllowScreencast = *fc.Security.AllowScreencast
}
if fc.Security.AllowDownload != nil {
s.Security.AllowDownload = *fc.Security.AllowDownload
}
if fc.Security.AllowUpload != nil {
s.Security.AllowUpload = *fc.Security.AllowUpload
}
if fc.Security.MaxRedirects != nil {
s.Security.MaxRedirects = *fc.Security.MaxRedirects
}
if fc.Security.Attach.Enabled != nil {
s.Security.AttachEnabled = *fc.Security.Attach.Enabled
}
return s
}
|