-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsp_test.go
168 lines (149 loc) · 4.52 KB
/
csp_test.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
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 csp
import (
"testing"
)
func TestParse(t *testing.T) {
tests := []struct {
in string
expectDirectives []Directive
expectErr error
}{
// test some basic stuff
{
in: " default-src 'self' ; script-src 'self' https://; connect-src ; object-src 'self';base-uri 'none';report-uri https://logs.templarbit.com/csp/foobar/reports?foo=bar; ",
expectDirectives: []Directive{
{
Name: "default-src",
Value: []string{"'self'"},
},
{
Name: "script-src",
Value: []string{"'self'", "https://"},
},
{
Name: "connect-src",
Value: []string{},
},
{
Name: "object-src",
Value: []string{"'self'"},
},
{
Name: "base-uri",
Value: []string{"'none'"},
},
{
Name: "report-uri",
Value: []string{"https://logs.templarbit.com/csp/foobar/reports?foo=bar"},
},
},
expectErr: nil,
},
// test duplicate directive
{
in: "object-src 'self'; object-src 'none'",
expectDirectives: nil,
expectErr: ErrDuplicateDirective,
},
{
in: "object-src 'self'; Object-src 'none'",
expectDirectives: nil,
expectErr: ErrDuplicateDirective,
},
// test unknown directive name
{
in: "bogus 'self'",
expectDirectives: nil,
expectErr: ErrDirectiveNameUnknown,
},
// test comma in directive value
{
in: "style-src 'self', 'unsafe-inline'",
expectDirectives: nil,
expectErr: ErrCommaInDirectiveValue,
},
{
in: "style-src 'self' 'unsafe-inline' http://example.com,",
expectDirectives: nil,
expectErr: ErrCommaInDirectiveValue,
},
// test ; in diretive value, although this should never happen anyway,
// because the whole directive is split by ; at the very beginning
{
in: "style-src 'self'; 'unsafe-inline' http://example.com,",
expectDirectives: nil,
expectErr: ErrDirectiveNameUnknown, // because it will treat unsafe-inline as directive name
},
// test invalid chars in value
{
in: "object-src 'se\x00lf'",
expectDirectives: nil,
expectErr: ErrInvalidValueChars,
},
}
for i, tt := range tests {
out, err := ParseDirectives(tt.in)
if err != tt.expectErr {
if xerr, ok := err.(*ParseError); ok {
if xerr.Err != tt.expectErr {
t.Fatalf("expect %v, got %v, in %v", tt.expectErr, xerr, i)
}
} else {
t.Fatalf("expect %v, got %v, in %v", tt.expectErr, err, i)
}
}
if len(out) != len(tt.expectDirectives) {
t.Fatalf("expect len(%v), got len(%v), in %v", len(tt.expectDirectives), len(out), i)
}
for j := 0; j < len(out); j++ {
if out[j].Name != tt.expectDirectives[j].Name {
t.Errorf("expect %v, got %v, in %v", tt.expectDirectives[j].Name, out[j].Name, i)
}
if len(out[j].Value) != len(tt.expectDirectives[j].Value) {
t.Fatalf("expect len(%v), got len(%v), in %v", len(tt.expectDirectives[j].Value), len(out[j].Value), i)
}
for k := 0; k < len(out[j].Value); k++ {
if out[j].Value[k] != tt.expectDirectives[j].Value[k] {
t.Errorf("expect %v, got %v, in %v", tt.expectDirectives[j].Value[k], out[j].Value[k], i)
}
}
}
}
}
func TestDirectivesToString(t *testing.T) {
e := "default-src 'self'; script-src 'self'; object-src 'self' http://; base-uri 'none'; report-uri https://logs.templarbit.com/csp/xxkey/reports"
directives, err := ParseDirectives(e)
if err != nil {
t.Fatal(err)
}
o := directives.String()
if o != e {
t.Errorf("\nexpected: %v\n got: %v", e, o)
}
}
func TestAddDirective(t *testing.T) {
d := make(Directives, 0)
d.AddDirective(Directive{"script-src", []string{"b", "a", "r"}})
d.AddDirective(Directive{"style-src", []string{"r", "a", "b"}})
d.AddDirective(Directive{"script-src", []string{"1", "2", "a"}})
result := d.String()
expect := "script-src 1 2 a b r; style-src a b r"
if result != expect {
t.Errorf("expected %v, got %v", expect, result)
}
}
func TestRemoveDirective(t *testing.T) {
e := "default-src 'self'; script-src 'self'; object-src 'self' http://; base-uri 'none'; report-uri https://logs.templarbit.com/csp/xxkey/reports"
directives, err := ParseDirectives(e)
if err != nil {
t.Fatal(err)
}
directives.RemoveDirectiveByName("script-src")
directives.RemoveDirectiveByName("report-uri")
directives.RemoveDirectiveByName("object-src")
result := directives.String()
expect := "default-src 'self'; base-uri 'none'"
if result != expect {
t.Errorf("expected %v, got %v", expect, result)
}
}