2019-01-11 03:38:37 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from flask.testing import FlaskClient
|
2019-04-17 05:36:30 +00:00
|
|
|
from tests.helpers import create_ctfd, destroy_ctfd, login_as_user
|
2019-01-11 03:38:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_api_csrf_failure():
|
2019-04-17 05:36:30 +00:00
|
|
|
"""Test that API requests require the CSRF-Token header"""
|
2019-01-11 03:38:37 +00:00
|
|
|
app = create_ctfd()
|
|
|
|
app.test_client_class = FlaskClient
|
|
|
|
with app.app_context():
|
2019-05-12 01:09:37 +00:00
|
|
|
with login_as_user(app, "admin") as client:
|
2019-01-11 03:38:37 +00:00
|
|
|
r = client.post(
|
2019-05-12 01:09:37 +00:00
|
|
|
"/api/v1/challenges",
|
2019-01-11 03:38:37 +00:00
|
|
|
json={
|
|
|
|
"name": "chal",
|
|
|
|
"category": "cate",
|
|
|
|
"description": "desc",
|
|
|
|
"value": "100",
|
|
|
|
"state": "hidden",
|
2019-05-12 01:09:37 +00:00
|
|
|
"type": "standard",
|
|
|
|
},
|
2019-01-11 03:38:37 +00:00
|
|
|
)
|
|
|
|
assert r.status_code == 403
|
|
|
|
|
|
|
|
with client.session_transaction() as sess:
|
2019-05-12 01:09:37 +00:00
|
|
|
nonce = sess.get("nonce")
|
2019-01-11 03:38:37 +00:00
|
|
|
|
|
|
|
r = client.post(
|
2019-05-12 01:09:37 +00:00
|
|
|
"/api/v1/challenges",
|
|
|
|
headers={"CSRF-Token": nonce},
|
2019-01-11 03:38:37 +00:00
|
|
|
json={
|
|
|
|
"name": "chal",
|
|
|
|
"category": "cate",
|
|
|
|
"description": "desc",
|
|
|
|
"value": "100",
|
|
|
|
"state": "hidden",
|
2019-05-12 01:09:37 +00:00
|
|
|
"type": "standard",
|
|
|
|
},
|
2019-01-11 03:38:37 +00:00
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
destroy_ctfd(app)
|