-
Notifications
You must be signed in to change notification settings - Fork 140
/
builder.go
121 lines (106 loc) · 2.9 KB
/
builder.go
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
package cspbuilder
import (
"sort"
"strings"
)
const (
// Fetch Directives.
ChildSrc = "child-src"
ConnectSrc = "connect-src"
DefaultSrc = "default-src"
FontSrc = "font-src"
FrameSrc = "frame-src"
ImgSrc = "img-src"
ManifestSrc = "manifest-src"
MediaSrc = "media-src"
ObjectSrc = "object-src"
PrefetchSrc = "prefetch-src"
ScriptSrc = "script-src"
ScriptSrcAttr = "script-src-attr"
ScriptSrcElem = "script-src-elem"
StyleSrc = "style-src"
StyleSrcAttr = "style-src-attr"
StyleSrcElem = "style-src-elem"
WorkerSrc = "worker-src"
// Document Directives.
BaseURI = "base-uri"
Sandbox = "sandbox"
// Navigation directives.
FormAction = "form-action"
FrameAncestors = "frame-ancestors"
NavigateTo = "navigate-to"
// Reporting directives.
ReportURI = "report-uri"
ReportTo = "report-to"
// Other directives.
RequireTrustedTypesFor = "require-trusted-types-for"
TrustedTypes = "trusted-types"
UpgradeInsecureRequests = "upgrade-insecure-requests"
)
type Builder struct {
Directives map[string]([]string)
}
// MustBuild is like Build but panics if an error occurs.
func (builder *Builder) MustBuild() string {
policy, err := builder.Build()
if err != nil {
panic(err)
}
return policy
}
// Build creates a content security policy string from the specified directives.
// If any directive contains invalid values, an error is returned instead.
func (builder *Builder) Build() (string, error) {
var sb strings.Builder
// Pull the directive keys out.
directiveKeys := []string{}
for key := range builder.Directives {
directiveKeys = append(directiveKeys, key)
}
// Sort the policies: https://www.w3.org/TR/CSP3/#framework-policy
sort.Strings(directiveKeys)
for _, directive := range directiveKeys {
if sb.Len() > 0 {
sb.WriteString("; ")
}
switch directive {
case Sandbox:
err := buildDirectiveSandbox(&sb, builder.Directives[directive])
if err != nil {
return "", err
}
case FrameAncestors:
err := buildDirectiveFrameAncestors(&sb, builder.Directives[directive])
if err != nil {
return "", err
}
case ReportTo:
err := buildDirectiveReportTo(&sb, builder.Directives[directive])
if err != nil {
return "", err
}
case RequireTrustedTypesFor:
err := buildDirectiveRequireTrustedTypesFor(&sb, builder.Directives[directive])
if err != nil {
return "", err
}
case TrustedTypes:
err := buildDirectiveTrustedTypes(&sb, builder.Directives[directive])
if err != nil {
return "", err
}
case UpgradeInsecureRequests:
err := buildDirectiveUpgradeInsecureRequests(&sb, builder.Directives[directive])
if err != nil {
return "", err
}
default:
// no special handling of directive values needed
err := buildDirectiveDefault(&sb, directive, builder.Directives[directive])
if err != nil {
return "", err
}
}
}
return sb.String(), nil
}