-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathtest_csrf.py
311 lines (259 loc) · 9.85 KB
/
test_csrf.py
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from __future__ import with_statement
import re
from flask import Blueprint
from flask import render_template
from flask_wtf.csrf import CsrfProtect
from flask_wtf.csrf import validate_csrf, generate_csrf
from .base import TestCase, MyForm, to_unicode
csrf_token_input = re.compile(
r'name="csrf_token" type="hidden" value="([0-9a-z#A-Z-\.]*)"'
)
def get_csrf_token(data):
match = csrf_token_input.search(to_unicode(data))
assert match
return match.groups()[0]
class TestCSRF(TestCase):
def setUp(self):
app = self.create_app()
app.config['WTF_CSRF_SECRET_KEY'] = "a poorly kept secret."
csrf = CsrfProtect(app)
self.csrf = csrf
@csrf.exempt
@app.route('/csrf-exempt', methods=['GET', 'POST'])
def csrf_exempt():
form = MyForm()
if form.validate_on_submit():
name = form.name.data.upper()
else:
name = ''
return render_template(
"index.html", form=form, name=name
)
@csrf.exempt
@app.route('/csrf-protect-method', methods=['GET', 'POST'])
def csrf_protect_method():
csrf.protect()
return 'protected'
bp = Blueprint('csrf', __name__)
@bp.route('/foo', methods=['GET', 'POST'])
def foo():
return 'foo'
app.register_blueprint(bp, url_prefix='/bar')
self.bp = bp
self.app = app
self.client = self.app.test_client()
def test_invalid_csrf(self):
response = self.client.post("/", data={"name": "danny"})
assert response.status_code == 400
@self.csrf.error_handler
def invalid(reason):
return reason
response = self.client.post("/", data={"name": "danny"})
assert response.status_code == 200
assert b'token missing' in response.data
def test_invalid_csrf2(self):
# tests with bad token
response = self.client.post("/", data={
"name": "danny",
"csrf_token": "9999999999999##test"
# will work only if greater than time.time()
})
assert response.status_code == 400
def test_invalid_secure_csrf3(self):
# test with multiple separators
response = self.client.post("/", data={
"name": "danny",
"csrf_token": "1378915137.722##foo##bar##and"
# will work only if greater than time.time()
})
assert response.status_code == 400
def test_valid_csrf(self):
response = self.client.get("/")
csrf_token = get_csrf_token(response.data)
response = self.client.post("/", data={
"name": "danny",
"csrf_token": csrf_token
})
assert b'DANNY' in response.data
def test_prefixed_csrf(self):
response = self.client.get('/')
csrf_token = get_csrf_token(response.data)
response = self.client.post('/', data={
'prefix-name': 'David',
'prefix-csrf_token': csrf_token,
})
assert response.status_code == 200
def test_invalid_secure_csrf(self):
response = self.client.get("/", base_url='https://localhost/')
csrf_token = get_csrf_token(response.data)
response = self.client.post(
"/",
data={"name": "danny"},
headers={'X-CSRFToken': csrf_token},
base_url='https://localhost/',
)
assert response.status_code == 400
assert b'failed' in response.data
response = self.client.post(
"/",
data={"name": "danny"},
headers={
'X-CSRFToken': csrf_token,
},
environ_base={
'HTTP_REFERER': 'https://example.com/',
},
base_url='https://localhost/',
)
assert response.status_code == 400
assert b'not match' in response.data
response = self.client.post(
"/",
data={"name": "danny"},
headers={
'X-CSRFToken': csrf_token,
},
environ_base={
'HTTP_REFERER': 'http://localhost/',
},
base_url='https://localhost/',
)
assert response.status_code == 400
assert b'not match' in response.data
response = self.client.post(
"/",
data={"name": "danny"},
headers={
'X-CSRFToken': csrf_token,
},
environ_base={
'HTTP_REFERER': 'https://localhost:3000/',
},
base_url='https://localhost/',
)
assert response.status_code == 400
assert b'not match' in response.data
def test_valid_secure_csrf(self):
response = self.client.get("/", base_url='https://localhost/')
csrf_token = get_csrf_token(response.data)
response = self.client.post(
"/",
data={"name": "danny"},
headers={
'X-CSRFToken': csrf_token,
},
environ_base={
'HTTP_REFERER': 'https://localhost/',
},
base_url='https://localhost/',
)
assert response.status_code == 200
def test_valid_csrf_method(self):
response = self.client.get("/")
csrf_token = get_csrf_token(response.data)
response = self.client.post("/csrf-protect-method", data={
"csrf_token": csrf_token
})
assert response.status_code == 200
def test_invalid_csrf_method(self):
response = self.client.post("/csrf-protect-method", data={"name": "danny"})
assert response.status_code == 400
@self.csrf.error_handler
def invalid(reason):
return reason
response = self.client.post("/", data={"name": "danny"})
assert response.status_code == 200
assert b'token missing' in response.data
def test_empty_csrf_headers(self):
response = self.client.get("/", base_url='https://localhost/')
csrf_token = get_csrf_token(response.data)
self.app.config['WTF_CSRF_HEADERS'] = list()
response = self.client.post(
"/",
data={"name": "danny"},
headers={
'X-CSRFToken': csrf_token,
},
environ_base={
'HTTP_REFERER': 'https://localhost/',
},
base_url='https://localhost/',
)
assert response.status_code == 400
def test_custom_csrf_headers(self):
response = self.client.get("/", base_url='https://localhost/')
csrf_token = get_csrf_token(response.data)
self.app.config['WTF_CSRF_HEADERS'] = ['X-XSRF-TOKEN']
response = self.client.post(
"/",
data={"name": "danny"},
headers={
'X-XSRF-TOKEN': csrf_token,
},
environ_base={
'HTTP_REFERER': 'https://localhost/',
},
base_url='https://localhost/',
)
assert response.status_code == 200
def test_not_endpoint(self):
response = self.client.post('/not-endpoint')
assert response.status_code == 404
def test_testing(self):
self.app.testing = True
self.client.post("/", data={"name": "danny"})
def test_csrf_exempt(self):
response = self.client.get("/csrf-exempt")
csrf_token = get_csrf_token(response.data)
response = self.client.post("/csrf-exempt", data={
"name": "danny",
"csrf_token": csrf_token
})
assert b'DANNY' in response.data
def test_validate_csrf(self):
with self.app.test_request_context():
assert not validate_csrf('ff##dd')
csrf_token = generate_csrf()
assert validate_csrf(csrf_token)
def test_validate_not_expiring_csrf(self):
with self.app.test_request_context():
csrf_token = generate_csrf(time_limit=False)
assert validate_csrf(csrf_token, time_limit=False)
def test_csrf_token_helper(self):
@self.app.route("/token")
def withtoken():
return render_template("csrf.html")
response = self.client.get('/token')
assert b'#' in response.data
def test_csrf_blueprint(self):
response = self.client.post('/bar/foo')
assert response.status_code == 400
self.csrf.exempt(self.bp)
response = self.client.post('/bar/foo')
assert response.status_code == 200
def test_csrf_token_macro(self):
@self.app.route("/token")
def withtoken():
return render_template("import_csrf.html")
response = self.client.get('/token')
assert b'#' in response.data
def test_csrf_custom_token_key(self):
with self.app.test_request_context():
# Generate a normal and a custom CSRF token
default_csrf_token = generate_csrf()
custom_csrf_token = generate_csrf(token_key='oauth_state')
# Verify they are different due to using different session keys
assert default_csrf_token != custom_csrf_token
# However, the custom key can validate as well
assert validate_csrf(custom_csrf_token, token_key='oauth_state')
def test_csrf_url_safe(self):
with self.app.test_request_context():
# Generate a normal and URL safe CSRF token
default_csrf_token = generate_csrf()
url_safe_csrf_token = generate_csrf(url_safe=True)
# Verify they are not the same and the URL one is truly URL safe
assert default_csrf_token != url_safe_csrf_token
assert '#' not in url_safe_csrf_token
assert re.match(r'^[a-f0-9]+--[a-f0-9]+$', url_safe_csrf_token)
# Verify we can validate our URL safe key
assert validate_csrf(url_safe_csrf_token, url_safe=True)