-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.go
141 lines (119 loc) · 2.68 KB
/
policy.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package csp
import (
"fmt"
"net/url"
"strings"
"sync"
)
type Policy struct {
m sync.Mutex
v map[string][]string
}
func NewPolicy() *Policy {
return &Policy{
v: make(map[string][]string),
}
}
func (p *Policy) Set(dir string, sources ...string) *Policy {
p.m.Lock()
defer p.m.Unlock()
if legalKey(dir) {
p.v[dir] = sources
}
return p
}
func (p *Policy) Add(dir string, sources ...string) *Policy {
p.m.Lock()
defer p.m.Unlock()
if legalKey(dir) {
p.v[dir] = append(p.v[dir], sources...)
}
return p
}
func (p Policy) String() string {
p.m.Lock()
defer p.m.Unlock()
polTokens := make([]string, 0, len(p.v)+1)
for directive, sourceList := range p.v {
polTokens = append(polTokens, directive+" "+strings.Join(sourceList, " "))
}
return strings.Join(polTokens, "; ")
}
func Parse(encPolicy string) (*Policy, error) {
rawDirectives := strings.Split(encPolicy, ";")
p := NewPolicy()
for _, rawDirective := range rawDirectives {
parts := strings.Split(strings.TrimSpace(rawDirective), " ")
name, err := parseName(parts[0])
if err != nil {
return nil, err
}
if _, there := p.v[name]; there {
break
}
if len(parts) == 1 {
p.v[name] = []string{}
break
}
sourceList, err := parseSourceList(parts[1])
if err != nil {
return nil, err
}
p.v[name] = sourceList
}
return p, nil
}
func parseName(dir string) (string, error) {
dir = strings.ToLower(dir)
switch dir {
case DirConnect, DirDefault, DirFont, DirFrame, DirImage, DirMedia,
DirObject, DirReport, DirSandbox, DirScript, DirStyle:
return dir, nil
}
return "", fmt.Errorf("csp: invalid directive name %s", dir)
}
func parseSourceList(val string) ([]string, error) {
sl := strings.Split(strings.TrimSpace(val), " ")
if len(sl) == 0 {
return []string{}, nil
}
sources := make([]string, 0, len(sl))
for _, sle := range sl {
val := strings.ToLower(strings.TrimSpace(sle))
switch {
case strings.HasPrefix(val, ValNoncePrfx):
fallthrough
case strings.HasPrefix(val, ValHashSHA256):
fallthrough
case strings.HasPrefix(val, ValHashSHA384):
fallthrough
case strings.HasPrefix(val, ValHashSHA512):
fallthrough
case ValNone == val:
fallthrough
case ValAny == val:
fallthrough
case ValSelf == val:
fallthrough
case ValUnsafeEval == val:
fallthrough
case ValUnsafeInline == val:
sources = append(sources, val)
default:
u, err := url.Parse(val)
if err != nil {
return nil, err
}
sources = append(sources, u.String())
}
}
return sources, nil
}
func legalKey(key string) bool {
switch key {
case DirConnect, DirDefault, DirFont, DirFrame, DirImage, DirMedia,
DirObject, DirSandbox, DirScript, DirReport:
return true
}
return false
}