-
Notifications
You must be signed in to change notification settings - Fork 9
Add system check warning for unsafe 'style' attribute #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add system check warning for unsafe 'style' attribute #73
Conversation
for more information, see https://pre-commit.ci
|
@shayanTaki excellent - thanks! Could you just take a look at the mypy issues please? Looks like it needs some type hints on the check. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a Django system check (W001) to warn developers when the style attribute is included in NH3_ALLOWED_ATTRIBUTES, as nh3 does not sanitize CSS content, which poses an XSS security risk.
Key changes:
- New system check function that inspects
NH3_ALLOWED_ATTRIBUTESfor unsafestyleattribute usage - Registration of the check in the app's
ready()method - Comprehensive test coverage for both warning and safe configuration scenarios
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/django_nh3/checks.py | Implements the security check function that detects style attribute in allowed attributes configuration |
| src/django_nh3/apps.py | Imports the checks module in the app's ready() method to register the system check |
| tests/test_checks.py | Adds test cases validating the check triggers warnings for unsafe configs and remains silent for safe ones |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
tests/test_checks.py
Outdated
| assert errors[0].id == "django_nh3.W001" | ||
|
|
||
|
|
||
| @override_settings(NH3_ALLOWED_ATTRIBUTES={"div": {"class", "href"}}) |
Copilot
AI
Dec 6, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The href attribute is not valid for div elements. Consider using a valid attribute for div (e.g., id, data-*) or testing with an a tag if href is needed. While this doesn't affect the test's purpose, using semantically correct HTML attributes in tests improves clarity.
| @override_settings(NH3_ALLOWED_ATTRIBUTES={"div": {"class", "href"}}) | |
| @override_settings(NH3_ALLOWED_ATTRIBUTES={"div": {"class", "id"}}) |
for more information, see https://pre-commit.ci
…m/shayanTaki/django-nh2 into feature/security-check-style-attr
|
Thanks for the review! I have applied the fixes: Added full type hints to checks.py to satisfy mypy. |
for more information, see https://pre-commit.ci
|
Currently Django-Nh3 does not allow I am working in a PR for the |
|
Thank you for the comprehensive explanation, @Pantzan. I agree with your overall assessment, and introducing an allowlist-based mechanism for handling That said, a secure implementation should not be limited to filtering CSS properties alone. Proper validation and sanitization at the value level is equally important, as constructs such as It is also important that the configuration and exposure of this functionality remain aligned with the existing project structure and public API, avoiding breaking changes and preserving predictable default behavior. Regarding warnings, replacing environment-limited warnings with a logging-based approach (e.g., I am looking forward to reviewing your Pull Request, and I would be glad to assist with review or implementation if that would be helpful. |
| allowed_attributes = getattr(settings, "NH3_ALLOWED_ATTRIBUTES", {}) | ||
|
|
||
| found_style = False | ||
| if isinstance(allowed_attributes, dict): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't look like this block has test coverage - at least according to the coverage report.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I have updated the tests to explicitly cover scenarios where NH3_ALLOWED_ATTRIBUTES is None or an empty dictionary. This ensures the isinstance check is fully exercised and should satisfy the coverage report.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent - thank you!
…m/shayanTaki/django-nh3 into feature/security-check-style-attr
for more information, see https://pre-commit.ci
This PR adds a Django System Check (
W001) to warn developers ifstyleis included inNH3_ALLOWED_ATTRIBUTES.Rationale
By default,
nh3does not parse or sanitize the content of thestyleattribute. Enabling it allows potential XSS vectors via CSS (e.g.,background-image: url('javascript:...')).While some use cases might require inline styles, users should be explicitly warned about the security implications if they override the defaults.
Changes
check_nh3_settingsinsrc/django_nh3/checks.py.src/django_nh3/apps.py.tests/test_checks.py.