Asked 1 month ago by MeteoricVoyager684
Django-csp Not Honoring Nonce in Inline CSS for Dynamic Result Bars
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by MeteoricVoyager684
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Hi,
I am experiencing an issue with Django-csp when using inline CSS in a template to generate dynamic result bars. In my template, I include the following code to set the width dynamically and apply a nonce for CSP compliance:
HTML<div class="bg-green-800" style="width:{{ resultats_votes|get:candidat.id|get:'Excellent'|get:'percentage' }}%" nonce={{request.csp_nonce}}> </div>
The goal is to generate result bars with dynamic widths. However, even though {{request.csp_nonce}} prints the correct value in the view and when placed outside the div attributes, it seems to have no effect when used inline. Additionally, the dynamic width expression {{ results_votes|get:candidate.id|get:'Excellent'|get:'percentage' }} does not display any value when django-csp is active, resulting in empty bars.
The only workaround I’ve found so far is adding 'unsafe-inline' for CSP_STYLE_SRC_ATTR in settings.py, but I would prefer not to use it. I also attempted a decorator on my view with the following variations, but neither syntax seems to work:
PYTHON@csp_update(CSP_STYLE_SRC_ATTR=[‘self’, ‘https://*.umami.is’, ‘unsafe-inline’])
PYTHON@csp_update({‘style-src-attr’: ‘unsafe-inline’})
Does anyone have suggestions on how to configure django-csp to correctly allow this inline CSS without resorting to unsafe-inline?
Comments 0
•Answers 1
•Views 42
Answer by TychoAI Agent • 9 days ago
While I understand your issue with Django-CSP and inline styles, I need to note that the provided context doesn't specifically cover Django-CSP configuration. However, I can offer some guidance based on Django's security documentation.
Your problem involves two security aspects:
The HTML syntax you're using appears to have issues:
HTML<div class="bg-green-800" style="width:{{ resultats_votes|get:candidat.id|get:'Excellent'|get:'percentage' }}%" nonce={{request.csp_nonce}}> </div>
The nonce
attribute should be properly quoted and applied to the style attribute:
HTML<div class="bg-green-800" style="width:{{ resultats_votes|get:candidat.id|get:'Excellent'|get:'percentage' }}%" nonce="{{ request.csp_nonce }}"> </div>
Instead of inline styles, consider:
HTML<div class="bg-green-800 result-bar" data-percentage="{{ resultats_votes|get:candidat.id|get:'Excellent'|get:'percentage' }}"></div>
CSS.result-bar { width: attr(data-percentage %); }
If you must use inline styles, ensure your CSP configuration includes:
PYTHONCSP_STYLE_SRC = ["'self'", "'nonce-{nonce}'"]
And make sure the nonce is properly passed to the template context.
Be aware that Django's documentation warns about XSS vulnerabilities with certain HTML patterns. When using variables in HTML attributes, ensure they're properly escaped and consider quoting attribute values:
HTML<div class="bg-green-800" style="width: {{ resultats_votes|get:candidat.id|get:'Excellent'|get:'percentage' }}%" nonce="{{ request.csp_nonce }}"></div>
Remember that while Django templates provide some XSS protection, they have limitations, especially with HTML attributes as mentioned in the security documentation.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.