-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_security.go
More file actions
61 lines (55 loc) · 1.85 KB
/
Copy pathapp_security.go
File metadata and controls
61 lines (55 loc) · 1.85 KB
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
package nexus
import (
"github.com/paulmanoni/nexus/middleware"
"github.com/paulmanoni/nexus/middleware/secure"
)
// securityStatusKey is the extValues key under which installSecurity
// stashes the resolved security posture. The extension/security
// dashboard tab reads it via App.Value so it can show what's actually
// active without re-deriving config.
const securityStatusKey = "nexus.security.status"
// installSecurity wires the built-in security middleware from
// Config.Middleware.Security. Headers are on unless explicitly disabled
// (so a nil config still hardens the app); CSRF is opt-in. It records a
// status map for the dashboard either way.
func (a *App) installSecurity(sc *SecurityConfig) {
headersOn := sc == nil || !sc.DisableHeaders
csrfOn := sc != nil && sc.EnableCSRF
if headersOn {
hc := secure.HeadersConfig{}
if sc != nil {
hc.FrameOptions = sc.FrameOptions
hc.ReferrerPolicy = sc.ReferrerPolicy
hc.ContentSecurityPolicy = sc.CSP
if sc.HSTSMaxAge > 0 {
hc.HSTS = &secure.HSTSConfig{MaxAge: sc.HSTSMaxAge}
}
}
secure.ApplyHeaderDefaults(&hc)
a.engine.Use(secure.HeadersHandler(&hc))
a.registry.RegisterMiddleware(middleware.Info{
Name: "security-headers",
Kind: middleware.KindBuiltin,
Description: "Security response headers (built-in)",
})
a.registry.RegisterGlobalMiddleware("security-headers")
}
if csrfOn {
cc := secure.CSRFConfig{}
if sc != nil {
cc.CookieSecure = sc.CSRFCookieSecure
}
secure.ApplyCSRFDefaults(&cc)
a.engine.Use(secure.CSRFHandler(&cc))
a.registry.RegisterMiddleware(middleware.Info{
Name: "csrf",
Kind: middleware.KindBuiltin,
Description: "CSRF double-submit check (built-in)",
})
a.registry.RegisterGlobalMiddleware("csrf")
}
a.SetValue(securityStatusKey, map[string]any{
"headers": headersOn,
"csrf": csrfOn,
})
}