-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notifications: small fixes found after reviewer (#10996)
* Notifications: small fixes found after reviewer Use single `{}` since we are using `.format()` to format these strings. * Handle missing `.format()` key Avoid internal error and log the exception so we can figure it out how to solve it. This happens when the `Notification` does not have _all_ the required `format_values`. * Protection agasint XSS when rendering notifications See #10922 (comment) * Test for missing key in format values and XSS protection * Update common/ * Lint * Document we only support `str` and `int` for now in `format_values` We don't support nested dictionaries in `format_values` or random objects. Only `str` and `int`. That should be enough for now. Skip all the values that are not `str` or `int` from the format values to render the messages. * Typo
- Loading branch information
Showing
4 changed files
with
92 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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,51 @@ | ||
from readthedocs.notifications.constants import INFO | ||
from readthedocs.notifications.messages import Message | ||
|
||
|
||
class TestMessage: | ||
def test_xss_protection(self): | ||
message = Message( | ||
id="test", | ||
header="XSS: {header}", | ||
body="XSS: {body}", | ||
type=INFO, | ||
) | ||
message.set_format_values( | ||
{ | ||
"header": "<p>xss</p>", | ||
"body": "<span>xss</span>", | ||
} | ||
) | ||
|
||
assert message.get_rendered_header() == "XSS: <p>xss</p>" | ||
assert message.get_rendered_body() == "XSS: <span>xss</span>" | ||
|
||
def test_invalid_format_values_type(self): | ||
message = Message( | ||
id="test", | ||
header="Header: {dict}", | ||
body="Body: {dict}", | ||
type=INFO, | ||
) | ||
message.set_format_values( | ||
{ | ||
"dict": { | ||
"key": "value", | ||
}, | ||
} | ||
) | ||
|
||
# The rendered version skips the ``dict`` because it's not supported | ||
assert message.get_rendered_header() == "Header: " | ||
assert message.get_rendered_body() == "Body: " | ||
|
||
def test_missing_key_format_values(self): | ||
message = Message( | ||
id="test", | ||
header="Missing format value: {header}", | ||
body="Missing format value: {body}", | ||
type=INFO, | ||
) | ||
|
||
assert message.get_rendered_header() == "Missing format value: " | ||
assert message.get_rendered_body() == "Missing format value: " |
This file contains 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 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