Skip to content

Commit 3e7e1f0

Browse files
committed
Add a note on security
1 parent 84febd8 commit 3e7e1f0

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ class Article(models.Model):
5454
excerpt = RichTextField()
5555
```
5656

57+
Then you can display the article excerpt in your HTML templates by marking it as [`safe`](https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#safe)
58+
59+
```django
60+
<div class="article-excerpt">{{ article.excerpt | safe}}</div>
61+
```
62+
5763
### Large rich-text information
5864

5965
In case you want to store large rich-text information, like the content of an article, which can span to quite a few thousand characters, we suggest you use the `AbstractDocument` model. This will save large rich-text information in a separate database table, which is better for performance. Example:
@@ -71,6 +77,12 @@ class Article(models.Model):
7177
body = models.OneToOneField(ArticleContent, on_delete=models.CASCADE)
7278
```
7379

80+
Similarly here you can display the article's body by marking it as `safe`
81+
82+
```django
83+
<div class="article-body">{{ article.body.content | safe}}</div>
84+
```
85+
7486
### Attachments
7587

7688
Django Prose can also handle uploading attachments with drag and drop. To set this up, first you need to:
@@ -79,6 +91,15 @@ Django Prose can also handle uploading attachments with drag and drop. To set th
7991
- [x] Include the Django Prose URLs (example in [`prose_example/prose_example/urls.py`](https://github.com/withlogicco/django-prose/blob/9073d713f8d3febe5c50705976dbb31063270886/prose_example/prose_example/urls.py#L9-L10))
8092
- [x] (Optional) Set up a different Django storage to store your files (e.g. S3)
8193

94+
## 🔒 A note on security
95+
96+
As you can see in the examples above, what Django Prose does is provide you with a user friendly editor ([Trix](https://trix-editor.org/)) for your rich text content and then store it as HTML in your database. Since you will mark this HTML as safe in order to use it in your templates, it needs to be **sanitised**, before it gets stored in the database.
97+
98+
For this reason Django Prose is using [Bleach](https://bleach.readthedocs.io/en/latest/) to only allow the following tags and attributes:
99+
100+
- **Allowed tags**: `p`, `ul`, `ol`, `li`, `strong`, `em`, `div`, `span`, `a`, `blockquote`, `pre`, `figure`, `figcaption`, `br`, `code`, `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `picture`, `source`, `img`
101+
- **Allowed attributes**: `alt`, `class`, `id`, `src`, `srcset`, `href`, `media`
102+
82103
## Screenshots
83104

84105
### Django Prose Documents in Django Admin

prose/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"p", "ul", "ol", "li", "strong", "em", "div", "span", "a",
1010
"blockquote", "pre", "figure", "figcaption", "br", "code",
1111
"h1", "h2", "h3", "h4", "h5", "h6",
12-
"picture", "source", "img,"
12+
"picture", "source", "img",
1313
]
1414
ALLOWED_ATTRIBUTES = [
1515
"alt", "class", "id", "src", "srcset", "href", "media",

0 commit comments

Comments
 (0)