-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreferer_policy_test.go
More file actions
230 lines (208 loc) · 5.68 KB
/
referer_policy_test.go
File metadata and controls
230 lines (208 loc) · 5.68 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package gatekeeper
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestRefererPolicyBlacklist(t *testing.T) {
config := Config{
RefererPolicy: &RefererPolicyConfig{
Mode: ModeBlacklist,
Exact: []string{
"http://malicious.com",
"https://spam.site",
},
Patterns: []string{
`(?i).*evil\.com.*`,
`(?i).*phishing\..*`,
},
},
DefaultBlockStatusCode: http.StatusForbidden,
DefaultBlockMessage: "Access denied",
}
gk, err := New(config)
if err != nil {
t.Fatalf("Failed to create gatekeeper: %v", err)
}
handler := gk.RefererPolicy(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}))
tests := []struct {
name string
referer string
expectedStatus int
expectedBody string
}{
{
name: "No referer - should allow",
referer: "",
expectedStatus: http.StatusOK,
expectedBody: "OK",
},
{
name: "Good referer - should allow",
referer: "https://google.com",
expectedStatus: http.StatusOK,
expectedBody: "OK",
},
{
name: "Exact blacklisted referer - should block",
referer: "http://malicious.com",
expectedStatus: http.StatusForbidden,
expectedBody: "Access denied\n",
},
{
name: "Case insensitive exact match - should block",
referer: "HTTP://MALICIOUS.COM",
expectedStatus: http.StatusForbidden,
expectedBody: "Access denied\n",
},
{
name: "Pattern match - should block",
referer: "https://sub.evil.com/path",
expectedStatus: http.StatusForbidden,
expectedBody: "Access denied\n",
},
{
name: "Phishing pattern - should block",
referer: "http://fake.phishing.site",
expectedStatus: http.StatusForbidden,
expectedBody: "Access denied\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", "/test", nil)
if tt.referer != "" {
req.Header.Set("Referer", tt.referer)
}
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != tt.expectedStatus {
t.Errorf("Expected status %d, got %d", tt.expectedStatus, w.Code)
}
if w.Body.String() != tt.expectedBody {
t.Errorf("Expected body %q, got %q", tt.expectedBody, w.Body.String())
}
})
}
}
func TestRefererPolicyWhitelist(t *testing.T) {
config := Config{
RefererPolicy: &RefererPolicyConfig{
Mode: ModeWhitelist,
Exact: []string{
"https://trusted.com",
"https://partner.site",
},
Patterns: []string{
`(?i).*\.mycompany\.com.*`,
`^https://[a-z]+\.safe\.org$`,
},
},
DefaultBlockStatusCode: http.StatusForbidden,
DefaultBlockMessage: "Access denied",
}
gk, err := New(config)
if err != nil {
t.Fatalf("Failed to create gatekeeper: %v", err)
}
handler := gk.RefererPolicy(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}))
tests := []struct {
name string
referer string
expectedStatus int
expectedBody string
}{
{
name: "No referer - should block in whitelist mode",
referer: "",
expectedStatus: http.StatusForbidden,
expectedBody: "Access denied\n",
},
{
name: "Non-whitelisted referer - should block",
referer: "https://google.com",
expectedStatus: http.StatusForbidden,
expectedBody: "Access denied\n",
},
{
name: "Exact whitelisted referer - should allow",
referer: "https://trusted.com",
expectedStatus: http.StatusOK,
expectedBody: "OK",
},
{
name: "Case insensitive exact match - should allow",
referer: "HTTPS://TRUSTED.COM",
expectedStatus: http.StatusOK,
expectedBody: "OK",
},
{
name: "Pattern match company domain - should allow",
referer: "https://app.mycompany.com/dashboard",
expectedStatus: http.StatusOK,
expectedBody: "OK",
},
{
name: "Pattern match safe.org - should allow",
referer: "https://docs.safe.org",
expectedStatus: http.StatusOK,
expectedBody: "OK",
},
{
name: "Invalid safe.org subdomain - should block",
referer: "https://123.safe.org",
expectedStatus: http.StatusForbidden,
expectedBody: "Access denied\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", "/test", nil)
if tt.referer != "" {
req.Header.Set("Referer", tt.referer)
}
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != tt.expectedStatus {
t.Errorf("Expected status %d, got %d", tt.expectedStatus, w.Code)
}
if w.Body.String() != tt.expectedBody {
t.Errorf("Expected body %q, got %q", tt.expectedBody, w.Body.String())
}
})
}
}
func TestRefererPolicyInvalidRegex(t *testing.T) {
config := Config{
RefererPolicy: &RefererPolicyConfig{
Mode: ModeBlacklist,
Patterns: []string{
`[invalid regex`,
},
},
}
_, err := New(config)
if err == nil {
t.Error("Expected error for invalid regex pattern, got nil")
}
if !contains(err.Error(), "invalid referer regex pattern") {
t.Errorf("Expected error message to contain 'invalid referer regex pattern', got: %v", err)
}
}
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(substr) == 0 || (len(s) > len(substr) && (s[:len(substr)] == substr || s[len(s)-len(substr):] == substr || containsSubstring(s, substr))))
}
func containsSubstring(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}