-
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
Open
shayanTaki
wants to merge
11
commits into
marksweb:main
Choose a base branch
from
shayanTaki:feature/security-check-style-attr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e6e4e79
feat: add system check for unsafe style attribute
shayanTaki 9ff0968
ci: auto fixes from pre-commit hooks
pre-commit-ci[bot] 7649a41
fix: add type hints for mypy compliance
shayanTaki 4aefad0
ci: auto fixes from pre-commit hooks
pre-commit-ci[bot] 5b740e4
fix: add type hints and fix html semantics in tests
shayanTaki 8b498d5
Merge branch 'feature/security-check-style-attr' of https://github.co…
shayanTaki a298df5
fix: add return type hint to AppConfig.ready
shayanTaki 0f26bab
ci: auto fixes from pre-commit hooks
pre-commit-ci[bot] b5e08e2
test: add full coverage for empty dict and None settings
shayanTaki 73f8a7d
Merge branch 'feature/security-check-style-attr' of https://github.co…
shayanTaki d97da3b
ci: auto fixes from pre-commit hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| from typing import Any | ||
|
|
||
| from django.apps import AppConfig | ||
| from django.conf import settings | ||
| from django.core.checks import CheckMessage, Tags, Warning, register | ||
|
|
||
|
|
||
| @register(Tags.security) | ||
| def check_nh3_settings( | ||
| app_configs: list[AppConfig] | None, **kwargs: Any | ||
| ) -> list[CheckMessage]: | ||
| """ | ||
| Inspects the NH3_ALLOWED_ATTRIBUTES setting to ensure that the 'style' | ||
| attribute is not allowed, as nh3 does not sanitize CSS content. | ||
| """ | ||
| errors: list[CheckMessage] = [] | ||
| allowed_attributes = getattr(settings, "NH3_ALLOWED_ATTRIBUTES", {}) | ||
|
|
||
| found_style = False | ||
| if isinstance(allowed_attributes, dict): | ||
| for _tag, attrs in allowed_attributes.items(): | ||
| if "style" in attrs: | ||
| found_style = True | ||
| break | ||
|
|
||
| if found_style: | ||
| errors.append( | ||
| Warning( | ||
| "The 'style' attribute is allowed in NH3_ALLOWED_ATTRIBUTES.", | ||
| hint=( | ||
| "Allowing 'style' poses a security risk (XSS) because nh3 " | ||
| "does not sanitize CSS content. Ensure you strictly trust " | ||
| "the input if this attribute is required." | ||
| ), | ||
| id="django_nh3.W001", | ||
| ) | ||
| ) | ||
| return errors | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from django.test import override_settings | ||
|
|
||
| from django_nh3.checks import check_nh3_settings | ||
|
|
||
|
|
||
| @override_settings(NH3_ALLOWED_ATTRIBUTES={"div": {"style"}}) | ||
| def test_check_warns_about_style(): | ||
| errors = check_nh3_settings(None) | ||
| assert len(errors) == 1 | ||
| assert errors[0].id == "django_nh3.W001" | ||
|
|
||
|
|
||
| @override_settings(NH3_ALLOWED_ATTRIBUTES={"div": {"class", "id"}}) | ||
| def test_check_is_silent_when_safe(): | ||
| errors = check_nh3_settings(None) | ||
| assert len(errors) == 0 | ||
|
|
||
|
|
||
| @override_settings(NH3_ALLOWED_ATTRIBUTES={}) | ||
| def test_check_handles_empty_dict(): | ||
| errors = check_nh3_settings(None) | ||
| assert len(errors) == 0 | ||
|
|
||
|
|
||
| @override_settings(NH3_ALLOWED_ATTRIBUTES=None) | ||
| def test_check_handles_non_dict_attributes(): | ||
| errors = check_nh3_settings(None) | ||
| assert len(errors) == 0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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_ATTRIBUTESisNoneor an empty dictionary. This ensures theisinstancecheck 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!