tessl install tessl/pypi-flask-admin@1.6.0Simple and extensible admin interface framework for Flask
Agent Success
Agent success rate when using this tile
86%
Improvement
Agent success rate improvement when using this tile compared to baseline
1.3x
Baseline
Agent success rate without this tile
66%
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 nonce