uWSGI Router Header Injection for Django/Flask Accessibility
0 reputation · 10 Jul 2020, 15:19 UTC
0 reputation · 10 Jul 2020, 15:19 UTC
Goal: Configure uWSGI so that a single server‑level rule set injects accessibility‑related HTTP headers (e.g., CSP, X‑Content‑Type‑Options, ARIA‑role hints) into responses from multiple Django or Flask applications without modifying the WSGI code.
35275 reputation · 10 Jul 2020, 20:38 UTC
uWSGI can inject static response headers globally with --add-header and conditionally with internal routing actions such as route-uri paired with addheader. It cannot reliably prevent duplicate headers when the Django or Flask application also emits the same header, because uWSGI evaluates routes before the WSGI app produces its response. ARIA attributes cannot be exposed as HTTP headers at all; they are DOM properties. In an Emperor deployment, header policy must be explicitly propagated to vassals or defined per vassal.
Confirmed behavior: --add-header appends a fixed header to every response. Path- or host-based injection uses routing rules (route-uri, route-host, route-if) with an addheader action. Values should be static and quoted, especially CSP directives containing spaces and semicolons.
Duplicate prevention is the hard constraint. uWSGI routing runs before the WSGI application, so uWSGI does not know whether Django or Flask already set Content-Security-Policy or X-Content-Type-Options. If both sides set them, clients may receive repeated headers. uWSGI does not inspect or rewrite the application's response headers in this flow.
ARIA compliance attributes (role, aria-label, aria-live, etc.) are HTML attributes consumed by browsers and assistive technology. HTTP headers do not enter the accessibility tree. Adding X-ARIA-* headers would create a false sense of compliance without changing rendered markup.
--add-header "Content-Security-Policy: default-src 'self'" and --add-header "X-Content-Type-Options: nosniff".route-uri with an addheader action, keeping values static and allowlisted.curl -sI that matching and non-matching paths return exactly one CSP and one nosniff header.Do not assume emperor-level options automatically apply to vassals. Use the vassal propagation mechanism supported by your uWSGI build, or set the header policy in each vassal configuration. A single broad CSP across tenants can weaken a strict application or break another that requires different script, style, or asset origins.
uwsgi --help on the target binary and grep for add-header, route-uri, and addheader to confirm exact tokens.Because exact option spelling and Emperor inheritance behavior are version- and build-sensitive, confirm them on your deployed binary. If your uWSGI build does not expose addheader in routing help, or if your applications already emit CSP/nosniff, share the relevant uwsgi --help output and the current header ownership model; that determines whether uWSGI alone is safe or a proxy is required.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.