| 1 |
import pytest |
| 2 |
from flask import Blueprint |
| 3 |
from flask import g |
| 4 |
from flask import render_template_string |
| 5 |
from flask import request |
| 6 |
|
| 7 |
from flask_wtf import FlaskForm |
| 8 |
from flask_wtf.csrf import csrf_meta_tag |
| 9 |
from flask_wtf.csrf import CSRFError |
| 10 |
from flask_wtf.csrf import CSRFProtect |
| 11 |
from flask_wtf.csrf import generate_csrf |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture |
| 15 |
def app(app): |
| 16 |
CSRFProtect(app) |
| 17 |
|
| 18 |
@app.route("/", methods=["GET", "POST"]) |
| 19 |
def index(): |
| 20 |
pass |
| 21 |
|
| 22 |
@app.after_request |
| 23 |
def add_csrf_header(response): |
| 24 |
response.headers.set("X-CSRF-Token", generate_csrf()) |
| 25 |
return response |
| 26 |
|
| 27 |
return app |
| 28 |
|
| 29 |
|
| 30 |
@pytest.fixture |
| 31 |
def csrf(app): |
| 32 |
return app.extensions["csrf"] |
| 33 |
|
| 34 |
|
| 35 |
def test_render_token(req_ctx): |
| 36 |
token = generate_csrf() |
| 37 |
assert render_template_string("{{ csrf_token() }}") == token |
| 38 |
|
| 39 |
|
| 40 |
def test_csrf_meta_tag_default(req_ctx): |
| 41 |
token = generate_csrf() |
| 42 |
rendered = csrf_meta_tag() |
| 43 |
assert rendered == f'<meta name="csrf-token" content="{token}">' |
| 44 |
|
| 45 |
|
| 46 |
def test_csrf_meta_tag_custom_name_param(req_ctx): |
| 47 |
token = generate_csrf() |
| 48 |
rendered = csrf_meta_tag(name="x-csrf") |
| 49 |
assert rendered == f'<meta name="x-csrf" content="{token}">' |
| 50 |
|
| 51 |
|
| 52 |
def test_csrf_meta_tag_config(app, req_ctx): |
| 53 |
app.config["WTF_CSRF_META_NAME"] = "authenticity-token" |
| 54 |
token = generate_csrf() |
| 55 |
rendered = csrf_meta_tag() |
| 56 |
assert rendered == f'<meta name="authenticity-token" content="{token}">' |
| 57 |
|
| 58 |
|
| 59 |
def test_csrf_meta_tag_param_overrides_config(app, req_ctx): |
| 60 |
app.config["WTF_CSRF_META_NAME"] = "from-config" |
| 61 |
token = generate_csrf() |
| 62 |
rendered = csrf_meta_tag(name="from-param") |
| 63 |
assert rendered == f'<meta name="from-param" content="{token}">' |
| 64 |
|
| 65 |
|
| 66 |
def test_csrf_meta_tag_jinja(req_ctx): |
| 67 |
token = generate_csrf() |
| 68 |
assert ( |
| 69 |
render_template_string("{{ csrf_meta_tag() }}") |
| 70 |
== f'<meta name="csrf-token" content="{token}">' |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
def test_csrf_meta_tag_escapes_name(req_ctx): |
| 75 |
token = generate_csrf() |
| 76 |
rendered = csrf_meta_tag(name='"><script>alert(1)</script>') |
| 77 |
assert "<script>" not in rendered |
| 78 |
assert f'content="{token}"' in rendered |
| 79 |
|
| 80 |
|
| 81 |
def test_protect(app, client, app_ctx): |
| 82 |
response = client.post("/") |
| 83 |
assert response.status_code == 400 |
| 84 |
assert "The CSRF token is missing." in response.get_data(as_text=True) |
| 85 |
|
| 86 |
app.config["WTF_CSRF_ENABLED"] = False |
| 87 |
assert client.post("/").get_data() == b"" |
| 88 |
app.config["WTF_CSRF_ENABLED"] = True |
| 89 |
|
| 90 |
app.config["WTF_CSRF_CHECK_DEFAULT"] = False |
| 91 |
assert client.post("/").get_data() == b"" |
| 92 |
app.config["WTF_CSRF_CHECK_DEFAULT"] = True |
| 93 |
|
| 94 |
assert client.options("/").status_code == 200 |
| 95 |
assert client.post("/not-found").status_code == 404 |
| 96 |
|
| 97 |
response = client.get("/") |
| 98 |
assert response.status_code == 200 |
| 99 |
token = response.headers["X-CSRF-Token"] |
| 100 |
assert client.post("/", data={"csrf_token": token}).status_code == 200 |
| 101 |
assert client.post("/", data={"prefix-csrf_token": token}).status_code == 200 |
| 102 |
assert client.post("/", data={"prefix-csrf_token": ""}).status_code == 400 |
| 103 |
assert client.post("/", headers={"X-CSRF-Token": token}).status_code == 200 |
| 104 |
|
| 105 |
|
| 106 |
def test_same_origin(client): |
| 107 |
token = client.get("/").headers["X-CSRF-Token"] |
| 108 |
response = client.post( |
| 109 |
"/", base_url="https://localhost", headers={"X-CSRF-Token": token} |
| 110 |
) |
| 111 |
data = response.get_data(as_text=True) |
| 112 |
assert "The referrer header is missing." in data |
| 113 |
|
| 114 |
response = client.post( |
| 115 |
"/", |
| 116 |
base_url="https://localhost", |
| 117 |
headers={"X-CSRF-Token": token, "Referer": "http://localhost/"}, |
| 118 |
) |
| 119 |
data = response.get_data(as_text=True) |
| 120 |
assert "The referrer does not match the host." in data |
| 121 |
|
| 122 |
response = client.post( |
| 123 |
"/", |
| 124 |
base_url="https://localhost", |
| 125 |
headers={"X-CSRF-Token": token, "Referer": "https://other/"}, |
| 126 |
) |
| 127 |
data = response.get_data(as_text=True) |
| 128 |
assert "The referrer does not match the host." in data |
| 129 |
|
| 130 |
response = client.post( |
| 131 |
"/", |
| 132 |
base_url="https://localhost", |
| 133 |
headers={"X-CSRF-Token": token, "Referer": "https://localhost:8080/"}, |
| 134 |
) |
| 135 |
data = response.get_data(as_text=True) |
| 136 |
assert "The referrer does not match the host." in data |
| 137 |
|
| 138 |
response = client.post( |
| 139 |
"/", |
| 140 |
base_url="https://localhost", |
| 141 |
headers={"X-CSRF-Token": token, "Referer": "https://localhost/"}, |
| 142 |
) |
| 143 |
assert response.status_code == 200 |
| 144 |
|
| 145 |
|
| 146 |
def test_form_csrf_short_circuit(app, client): |
| 147 |
@app.route("/skip", methods=["POST"]) |
| 148 |
def skip(): |
| 149 |
assert g.get("csrf_valid") |
| 150 |
|
| 151 |
|
| 152 |
form = FlaskForm(None) |
| 153 |
assert form.validate() |
| 154 |
|
| 155 |
token = client.get("/").headers["X-CSRF-Token"] |
| 156 |
response = client.post("/skip", headers={"X-CSRF-Token": token}) |
| 157 |
assert response.status_code == 200 |
| 158 |
|
| 159 |
|
| 160 |
def test_exempt_view(app, csrf, client): |
| 161 |
@app.route("/exempt", methods=["POST"]) |
| 162 |
@csrf.exempt |
| 163 |
def exempt(): |
| 164 |
pass |
| 165 |
|
| 166 |
response = client.post("/exempt") |
| 167 |
assert response.status_code == 200 |
| 168 |
|
| 169 |
csrf.exempt("test_csrf_extension.index") |
| 170 |
response = client.post("/") |
| 171 |
assert response.status_code == 200 |
| 172 |
|
| 173 |
|
| 174 |
def test_manual_protect(app, csrf, client): |
| 175 |
@app.route("/manual", methods=["GET", "POST"]) |
| 176 |
@csrf.exempt |
| 177 |
def manual(): |
| 178 |
csrf.protect() |
| 179 |
|
| 180 |
response = client.get("/manual") |
| 181 |
assert response.status_code == 200 |
| 182 |
|
| 183 |
response = client.post("/manual") |
| 184 |
assert response.status_code == 400 |
| 185 |
|
| 186 |
|
| 187 |
def test_protect_apply_exemptions(app, csrf, client): |
| 188 |
app.config["WTF_CSRF_CHECK_DEFAULT"] = False |
| 189 |
|
| 190 |
@app.before_request |
| 191 |
def custom(): |
| 192 |
if request.headers.get("Authorization", "").startswith("Bearer "): |
| 193 |
return |
| 194 |
csrf.protect(apply_exemptions=True) |
| 195 |
|
| 196 |
@app.route("/api", methods=["POST"]) |
| 197 |
def api(): |
| 198 |
pass |
| 199 |
|
| 200 |
@app.route("/webhook", methods=["POST"]) |
| 201 |
@csrf.exempt |
| 202 |
def webhook(): |
| 203 |
pass |
| 204 |
|
| 205 |
bp = Blueprint("public", __name__, url_prefix="/public") |
| 206 |
csrf.exempt(bp) |
| 207 |
|
| 208 |
@bp.route("/", methods=["POST"]) |
| 209 |
def public(): |
| 210 |
pass |
| 211 |
|
| 212 |
app.register_blueprint(bp) |
| 213 |
|
| 214 |
assert client.post("/api").status_code == 400 |
| 215 |
assert client.post("/api", headers={"Authorization": "Bearer x"}).status_code == 200 |
| 216 |
assert client.post("/webhook").status_code == 200 |
| 217 |
assert client.post("/public/").status_code == 200 |
| 218 |
assert client.post("/missing").status_code == 404 |
| 219 |
|
| 220 |
|
| 221 |
def test_exempt_blueprint(app, csrf, client): |
| 222 |
bp = Blueprint("exempt", __name__, url_prefix="/exempt") |
| 223 |
csrf.exempt(bp) |
| 224 |
|
| 225 |
@bp.route("/", methods=["POST"]) |
| 226 |
def index(): |
| 227 |
pass |
| 228 |
|
| 229 |
app.register_blueprint(bp) |
| 230 |
response = client.post("/exempt/") |
| 231 |
assert response.status_code == 200 |
| 232 |
|
| 233 |
|
| 234 |
def test_exempt_nested_blueprint(app, csrf, client): |
| 235 |
bp1 = Blueprint("exempt1", __name__, url_prefix="/") |
| 236 |
bp2 = Blueprint("exempt2", __name__, url_prefix="/exempt") |
| 237 |
csrf.exempt(bp2) |
| 238 |
|
| 239 |
@bp2.route("/", methods=["POST"]) |
| 240 |
def index(): |
| 241 |
pass |
| 242 |
|
| 243 |
bp1.register_blueprint(bp2) |
| 244 |
app.register_blueprint(bp1) |
| 245 |
|
| 246 |
response = client.post("/exempt/") |
| 247 |
assert response.status_code == 200 |
| 248 |
|
| 249 |
|
| 250 |
def test_error_handler(app, client): |
| 251 |
@app.errorhandler(CSRFError) |
| 252 |
def handle_csrf_error(e): |
| 253 |
return e.description.lower() |
| 254 |
|
| 255 |
response = client.post("/") |
| 256 |
assert response.get_data(as_text=True) == "the csrf token is missing." |
| 257 |
|
| 258 |
|
| 259 |
def test_validate_error_logged(client, monkeypatch): |
| 260 |
from flask_wtf.csrf import logger |
| 261 |
|
| 262 |
messages = [] |
| 263 |
|
| 264 |
def assert_info(message): |
| 265 |
messages.append(message) |
| 266 |
|
| 267 |
monkeypatch.setattr(logger, "info", assert_info) |
| 268 |
|
| 269 |
client.post("/") |
| 270 |
assert len(messages) == 1 |
| 271 |
assert messages[0] == "The CSRF token is missing." |