Skip to content
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

Added suport of allowed_protocols bleach config #152

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ WAGTAILMARKDOWN = {
"allowed_tags": [], # optional. a list of HTML tags. e.g. ['div', 'p', 'a']
"allowed_styles": [], # optional. a list of styles
"allowed_attributes": {}, # optional. a dict with HTML tag as key and a list of attributes as value
"allowed_protocols": [], # optional. a list of protocols e.g. ['http', 'https', 'mailto', 'tel']
"allowed_settings_mode": "extend", # optional. Possible values: "extend" or "override". Defaults to "extend".
"extensions": [], # optional. a list of python-markdown supported extensions
"extension_configs": {}, # optional. a dictionary with the extension name as key, and its configuration as value
Expand All @@ -62,7 +63,7 @@ WAGTAILMARKDOWN = {
}
```

Note: `allowed_tags`, `allowed_styles`, `allowed_attributes`, `extensions` and `extension_configs` are added to the
Note: `allowed_tags`, `allowed_styles`, `allowed_attributes`, `allowed_protocols`, `extensions` and `extension_configs` are added to the
[default wagtail-markdown settings](https://github.com/torchbox/wagtail-markdown/blob/main/src/wagtailmarkdown/constants.py).


Expand Down Expand Up @@ -156,17 +157,18 @@ WAGTAILMARKDOWN = {
}
```

#### Allowed HTML - `allowed_styles` / `allowed_attributes` / `allowed_tags`
#### Allowed HTML - `allowed_styles` / `allowed_attributes` / `allowed_tags` / `allowed_protocols`

wagtail-markdown uses [bleach](https://github.com/mozilla/bleach) to sanitise the input. To extend the default
bleach configurations, you can add your own allowed tags, styles or attributes:
bleach configurations, you can add your own allowed tags, styles, attributes or protocols:

```python
WAGTAILMARKDOWN = {
# ...
"allowed_tags": ["i"],
"allowed_styles": ["some_style"],
"allowed_attributes": {"i": ["aria-hidden"]},
"allowed_protocols": ["tel"],
}
```

Expand Down
7 changes: 7 additions & 0 deletions src/wagtailmarkdown/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,17 @@
"margin-right",
]

DEFAULT_ALLOWED_PROTOCOLS = [
"http",
"https",
"mailto",
]

DEFAULT_BLEACH_KWARGS = {
"tags": DEFAULT_ALLOWED_TAGS,
"attributes": DEFAULT_ALLOWED_ATTRIBUTES,
"styles": DEFAULT_ALLOWED_STYLES,
"protocols": DEFAULT_ALLOWED_PROTOCOLS,
}


Expand Down
11 changes: 11 additions & 0 deletions src/wagtailmarkdown/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ def _get_bleach_kwargs():
key: list(value) for key, value in merged.items()
}

if "allowed_protocols" in settings.WAGTAILMARKDOWN:
if override:
bleach_kwargs["protocols"] = settings.WAGTAILMARKDOWN["allowed_protocols"]
else:
bleach_kwargs["protocols"] = list(
set(
settings.WAGTAILMARKDOWN["allowed_protocols"]
+ bleach_kwargs["protocols"]
)
)

return bleach_kwargs


Expand Down
19 changes: 19 additions & 0 deletions tests/testapp/tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"allowed_tags": ["i"],
"allowed_styles": ["some_style"],
"allowed_attributes": {"i": ["aria-hidden"], "a": ["data-test"]},
"allowed_protocols": ["tel"],
"extensions": ["toc", "sane_lists"],
"extension_configs": {
"codehilite": [("guess_lang", True)],
Expand Down Expand Up @@ -163,3 +164,21 @@ def test_get_bleach_kwargs_with_attributes(self):
}
):
self.assertDictEqual(_get_bleach_kwargs()["attributes"], allowed)

def test_get_bleach_kwargs_with_protocols(self):
with override_settings(WAGTAILMARKDOWN={"allowed_protocols": ["tel"]}):
self.assertListEqual(
sorted(_get_bleach_kwargs()["protocols"]),
sorted(set(DEFAULT_BLEACH_KWARGS["protocols"] + ["tel"])),
)

with override_settings(
WAGTAILMARKDOWN={
"allowed_protocols": ["tel"],
"allowed_settings_mode": SETTINGS_MODE_OVERRIDE,
}
):
self.assertListEqual(
sorted(_get_bleach_kwargs()["protocols"]),
["tel"],
)