| 1 |
""" |
| 2 |
test_templates |
| 3 |
~~~~~~~~~~ |
| 4 |
|
| 5 |
Test templates to be W3C valid |
| 6 |
|
| 7 |
:copyright: (c) 2025-2025 by J. Christopher Wagner (jwag). |
| 8 |
:license: MIT, see LICENSE for more details. |
| 9 |
|
| 10 |
Note that the validator definitely rate-limits us - so we don't just run |
| 11 |
them all. |
| 12 |
|
| 13 |
To run a test pass templates="RESET,LOGIN" on the command line. |
| 14 |
""" |
| 15 |
|
| 16 |
import re |
| 17 |
from time import sleep |
| 18 |
|
| 19 |
import pytest |
| 20 |
import requests |
| 21 |
from flask import g |
| 22 |
|
| 23 |
from tests.test_utils import ( |
| 24 |
authenticate, |
| 25 |
logout, |
| 26 |
capture_reset_password_requests, |
| 27 |
get_form_input_value, |
| 28 |
reset_fresh, |
| 29 |
setup_tf_sms, |
| 30 |
) |
| 31 |
from tests.test_webauthn import HackWebauthnUtil, reg_first_key |
| 32 |
|
| 33 |
|
| 34 |
def check_message(msgs, mtype="error"): |
| 35 |
# list of JSON messages from validator |
| 36 |
errors = [] |
| 37 |
for msg in msgs: |
| 38 |
if msg["type"] == mtype: |
| 39 |
errors.append(msg["message"]) |
| 40 |
return errors |
| 41 |
|
| 42 |
|
| 43 |
def check_template(name, client, r): |
| 44 |
# returns list of validation errors |
| 45 |
t = client.get(name) |
| 46 |
if t.status_code != 200: |
| 47 |
return [f"{name} error {t.status_code}"] |
| 48 |
return check_template_rdata(name, r, t.data) |
| 49 |
|
| 50 |
|
| 51 |
def check_template_rdata(name, r, rdata): |
| 52 |
vout = r.post("https://validator.w3.org/nu/?out=json", rdata) |
| 53 |
if vout.status_code == 429: |
| 54 |
# we got rate limited try again |
| 55 |
sleep(2.0) |
| 56 |
vout = r.post("https://validator.w3.org/nu/?out=json", rdata) |
| 57 |
if vout.status_code != 200: |
| 58 |
return [f"{name} API error {vout.status_code}"] |
| 59 |
if vout.status_code != 200: |
| 60 |
return [f"{name} API error {vout.status_code}"] |
| 61 |
return check_message(vout.json()["messages"]) |
| 62 |
|
| 63 |
|
| 64 |
def get_script_tags(html): |
| 65 |
"""Return list of script tags in the HTML""" |
| 66 |
return re.findall(r"<script\b[^>]*>", html, re.DOTALL | re.IGNORECASE) |
| 67 |
|
| 68 |
|
| 69 |
@pytest.mark.registerable() |
| 70 |
@pytest.mark.recoverable() |
| 71 |
@pytest.mark.changeable() |
| 72 |
@pytest.mark.change_email() |
| 73 |
@pytest.mark.change_username() |
| 74 |
@pytest.mark.oauth() |
| 75 |
@pytest.mark.username_recovery() |
| 76 |
@pytest.mark.unified_signin() |
| 77 |
@pytest.mark.webauthn(webauthn_util_cls=HackWebauthnUtil) |
| 78 |
@pytest.mark.two_factor() |
| 79 |
@pytest.mark.settings( |
| 80 |
multi_factor_recovery_codes=True, |
| 81 |
oauth_enable=True, |
| 82 |
) |
| 83 |
@pytest.mark.csrf() |
| 84 |
def test_valid_html(app, client): |
| 85 |
# since we get rate limited - use external pytest option to specify |
| 86 |
totry = app.config.get("TEMPLATES", "").split(",") |
| 87 |
rsession = requests.session() |
| 88 |
rsession.headers.update({"Content-Type": "text/html; charset=utf-8"}) |
| 89 |
|
| 90 |
unauth_urls = [ |
| 91 |
"LOGIN", |
| 92 |
"REGISTER", |
| 93 |
"RESET", |
| 94 |
"US_SIGNIN", |
| 95 |
"USERNAME_RECOVERY", |
| 96 |
"WAN_SIGNIN", |
| 97 |
] |
| 98 |
auth_urls = [ |
| 99 |
"CHANGE", |
| 100 |
"CHANGE_USERNAME", |
| 101 |
"CHANGE_EMAIL", |
| 102 |
"MULTI_FACTOR_RECOVERY_CODES", |
| 103 |
"TWO_FACTOR_SETUP", |
| 104 |
"US_SETUP", |
| 105 |
"US_VERIFY", |
| 106 |
"WAN_REGISTER", |
| 107 |
"WAN_VERIFY", |
| 108 |
] |
| 109 |
|
| 110 |
# MULTI_FACTOR_RECOVERY requires tf-setup and login/password |
| 111 |
# TWO_FACTOR_RESCUE has an issue with the RadioField |
| 112 |
# TWO_FACTOR_SELECT needs special setup |
| 113 |
|
| 114 |
authenticate(client, csrf=True) |
| 115 |
response = client.get("/change") |
| 116 |
csrf_token = get_form_input_value(response, "csrf_token") |
| 117 |
reg_first_key(client, csrf_token=csrf_token) |
| 118 |
|
| 119 |
logout(client) |
| 120 |
|
| 121 |
terrors = dict() |
| 122 |
for t in [u for u in unauth_urls if u in totry]: |
| 123 |
terrors[t] = check_template(app.config[f"SECURITY_{t}_URL"], client, rsession) |
| 124 |
|
| 125 |
authenticate(client, csrf=True) |
| 126 |
for t in [u for u in auth_urls if u in totry]: |
| 127 |
if t == "US_SETUP": |
| 128 |
response = client.get("/us-setup") |
| 129 |
csrf_token = get_form_input_value(response, "csrf_token") |
| 130 |
response = client.post( |
| 131 |
"us-setup", |
| 132 |
data=dict(chosen_method="authenticator", csrf_token=csrf_token), |
| 133 |
) |
| 134 |
terrors[t] = check_template_rdata("US_SETUP", rsession, response.data) |
| 135 |
continue |
| 136 |
if t == "TWO_FACTOR_SETUP": |
| 137 |
response = client.get("/tf-setup") |
| 138 |
csrf_token = get_form_input_value(response, "csrf_token") |
| 139 |
response = client.post( |
| 140 |
"tf-setup", |
| 141 |
data=dict(setup="authenticator", csrf_token=csrf_token), |
| 142 |
) |
| 143 |
terrors[t] = check_template_rdata( |
| 144 |
"TWO_FACTOR_SETUP", rsession, response.data |
| 145 |
) |
| 146 |
continue |
| 147 |
elif t == "US_VERIFY" or t == "VERIFY": |
| 148 |
reset_fresh(client, app.config["SECURITY_FRESHNESS"]) |
| 149 |
|
| 150 |
terrors[t] = check_template(app.config[f"SECURITY_{t}_URL"], client, rsession) |
| 151 |
|
| 152 |
print(f"Validated: {totry}") |
| 153 |
errors = {k: v for k, v in terrors.items() if v} |
| 154 |
assert not any(errors), errors |
| 155 |
|
| 156 |
|
| 157 |
@pytest.mark.confirmable() |
| 158 |
@pytest.mark.csrf() |
| 159 |
def test_valid_html_confirm(app, client): |
| 160 |
rsession = requests.session() |
| 161 |
rsession.headers.update({"Content-Type": "text/html; charset=utf-8"}) |
| 162 |
# since we get rate limited - use external pytest option to specify |
| 163 |
totry = app.config.get("TEMPLATES", "").split(",") |
| 164 |
if "CONFIRM" in totry: |
| 165 |
print(f"Validated: {totry}") |
| 166 |
terrors = check_template(app.config["SECURITY_CONFIRM_URL"], client, rsession) |
| 167 |
assert not terrors |
| 168 |
|
| 169 |
|
| 170 |
@pytest.mark.recoverable() |
| 171 |
@pytest.mark.csrf() |
| 172 |
def test_valid_html_recover(app, client): |
| 173 |
rsession = requests.session() |
| 174 |
rsession.headers.update({"Content-Type": "text/html; charset=utf-8"}) |
| 175 |
# since we get rate limited - use external pytest option to specify |
| 176 |
totry = app.config.get("TEMPLATES", "").split(",") |
| 177 |
if "RESET" in totry: |
| 178 |
print(f"Validated: {totry}") |
| 179 |
with capture_reset_password_requests() as resets: |
| 180 |
response = client.get("/reset") |
| 181 |
csrf_token = get_form_input_value(response, "csrf_token") |
| 182 |
client.post("/reset", data=dict(email="[email protected]", csrf_token=csrf_token)) |
| 183 |
token = resets[0]["token"] |
| 184 |
terrors = check_template( |
| 185 |
f'{app.config[f"SECURITY_RESET_URL"]}/{token}', client, rsession |
| 186 |
) |
| 187 |
assert not terrors |
| 188 |
|
| 189 |
|
| 190 |
@pytest.mark.two_factor() |
| 191 |
@pytest.mark.csrf() |
| 192 |
def test_valid_html_rescue(app, client): |
| 193 |
rsession = requests.session() |
| 194 |
rsession.headers.update({"Content-Type": "text/html; charset=utf-8"}) |
| 195 |
# since we get rate limited - use external pytest option to specify |
| 196 |
totry = app.config.get("TEMPLATES", "").split(",") |
| 197 |
if "TWO_FACTOR_RESCUE" in totry: |
| 198 |
authenticate(client, csrf=True) |
| 199 |
response = client.get("/tf-setup") |
| 200 |
csrf_token = get_form_input_value(response, "csrf_token") |
| 201 |
setup_tf_sms(client, csrf_token=csrf_token) |
| 202 |
logout(client) |
| 203 |
authenticate(client, csrf=True) |
| 204 |
print(f"Validated: {totry}") |
| 205 |
terrors = check_template( |
| 206 |
app.config["SECURITY_TWO_FACTOR_RESCUE_URL"], client, rsession |
| 207 |
) |
| 208 |
assert not terrors |
| 209 |
|
| 210 |
|
| 211 |
@pytest.mark.webauthn(webauthn_util_cls=HackWebauthnUtil) |
| 212 |
def test_script_nonce(app, client): |
| 213 |
"""Test that script nonces appear in script tags when configured.""" |
| 214 |
|
| 215 |
@app.before_request |
| 216 |
def set_nonce(): |
| 217 |
g.csp_nonce = "12345" |
| 218 |
|
| 219 |
# default config -> no nonces in script tags |
| 220 |
res = client.get("/wan-signin") |
| 221 |
tags = get_script_tags(res.text) |
| 222 |
assert tags |
| 223 |
assert all("nonce" not in t for t in tags) |
| 224 |
|
| 225 |
# invalid nonce key given -> no nonces in script tags |
| 226 |
app.config["SECURITY_SCRIPT_NONCE_KEY"] = "wrong_key" |
| 227 |
res = client.get("/wan-signin") |
| 228 |
tags = get_script_tags(res.text) |
| 229 |
assert tags |
| 230 |
assert all("nonce" not in t for t in tags) |
| 231 |
|
| 232 |
# nonces set up correctly -> nonce in script tags |
| 233 |
app.config["SECURITY_SCRIPT_NONCE_KEY"] = "csp_nonce" |
| 234 |
res = client.get("/wan-signin") |
| 235 |
tags = get_script_tags(res.text) |
| 236 |
assert tags |
| 237 |
assert all('nonce="12345"' in t for t in tags) |