Simple and extensible admin interface framework for Flask
86
Quality
Pending
Does it follow best practices?
Impact
86%
1.30xAverage score across 10 eval scenarios
Build a Flask web application with an admin interface that implements Content Security Policy (CSP) headers with dynamic nonce generation. The admin interface should be secure against XSS attacks by using CSP nonces for inline scripts and styles.
Create a Flask application with the following:
Implement CSP headers that:
script-src and style-src directivesCreate an admin interface with:
Web framework for building the application.
Admin interface framework that supports CSP nonce generation.
File: test_csp.py { .test }
Test: Verify that CSP headers are present in admin responses
def test_csp_header_present(client):
"""Test that CSP header is present in admin page response"""
response = client.get('/admin/')
assert 'Content-Security-Policy' in response.headers
csp_header = response.headers['Content-Security-Policy']
assert 'script-src' in csp_header
assert 'nonce-' in csp_headerFile: test_csp.py { .test }
Test: Verify that each request generates a unique nonce
def test_nonce_uniqueness(client):
"""Test that each request generates a unique nonce"""
response1 = client.get('/admin/')
response2 = client.get('/admin/')
csp_header1 = response1.headers.get('Content-Security-Policy', '')
csp_header2 = response2.headers.get('Content-Security-Policy', '')
# Extract nonce values from CSP headers
import re
nonce1 = re.search(r"nonce-([A-Za-z0-9+/=]+)", csp_header1)
nonce2 = re.search(r"nonce-([A-Za-z0-9+/=]+)", csp_header2)
assert nonce1 is not None
assert nonce2 is not None
assert nonce1.group(1) != nonce2.group(1)File: test_csp.py { .test }
Test: Verify that the nonce is available in templates
def test_nonce_in_template(client):
"""Test that nonce is accessible in template context"""
response = client.get('/admin/')
assert response.status_code == 200
# Extract nonce from CSP header
import re
csp_header = response.headers.get('Content-Security-Policy', '')
nonce_match = re.search(r"nonce-([A-Za-z0-9+/=]+)", csp_header)
assert nonce_match is not None
nonce_value = nonce_match.group(1)
# Verify nonce is used in the HTML response
assert f'nonce="{nonce_value}"' in response.get_data(as_text=True) or \
f"nonce='{nonce_value}'" in response.get_data(as_text=True)When running the application and accessing /admin/, the response should:
Content-Security-Policy header with a unique nonceInstall with Tessl CLI
npx tessl i tessl/pypi-flask-admindocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10