fix sanitization; allow markdown in tag descriptions also

This commit is contained in:
Tomáš Mládek 2020-12-19 17:01:20 +01:00
parent dff853e443
commit 79abc84905
2 changed files with 12 additions and 3 deletions

View file

@ -1,6 +1,5 @@
import bleach import bleach
import markdown2 import markdown2
from django.core.exceptions import ValidationError
from django.core.files.storage import FileSystemStorage from django.core.files.storage import FileSystemStorage
from django.db import models from django.db import models
from django.db.models import Count, Q from django.db.models import Count, Q
@ -8,11 +7,21 @@ from model_utils.managers import SoftDeletableManager, SoftDeletableQuerySet
from model_utils.models import SoftDeletableModel from model_utils.models import SoftDeletableModel
from ordered_model.models import OrderedModel from ordered_model.models import OrderedModel
BLEACH_ALLOWED_TAGS = bleach.sanitizer.ALLOWED_TAGS + ['p', 'br', 'h1', 'h2', 'h3']
class Tag(SoftDeletableModel): class Tag(SoftDeletableModel):
name = models.CharField(max_length=128, null=False, blank=False) name = models.CharField(max_length=128, null=False, blank=False)
description = models.TextField(null=False, blank=True) description = models.TextField(null=False, blank=True)
@property
def html_description(self):
return bleach.clean(bleach.linkify(markdown2.markdown(self.description)), tags=BLEACH_ALLOWED_TAGS)
@property
def plain_description(self):
return bleach.clean(self.html_description, tags=[], strip=True)
def __str__(self): def __str__(self):
return self.name return self.name
@ -63,7 +72,7 @@ class Document(SoftDeletableModel):
@property @property
def html_description(self): def html_description(self):
return bleach.linkify(markdown2.markdown(self.description)) return bleach.clean(bleach.linkify(markdown2.markdown(self.description)), tags=BLEACH_ALLOWED_TAGS)
@property @property
def plain_description(self): def plain_description(self):

View file

@ -14,7 +14,7 @@
{% if tag %} {% if tag %}
<div class="text tag-text"> <div class="text tag-text">
<h1>{{ tag.name }}</h1> <h1>{{ tag.name }}</h1>
<p class="tag-desc">{{ tag.description }}</p> <div class="tag-desc">{{ tag.html_description|safe }}</div>
</div> </div>
{% endif %} {% endif %}