Skip to content

Instantly share code, notes, and snippets.

@pirate
Last active October 22, 2020 10:49
Show Gist options
  • Save pirate/c18bfe4fd96008ffa0aef25001a2e88f to your computer and use it in GitHub Desktop.
Save pirate/c18bfe4fd96008ffa0aef25001a2e88f to your computer and use it in GitHub Desktop.
import bleach
import json as jsonlib
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@register.filter
def json(value):
"""safe jsonify filter, bleaches the json string using the bleach html tag remover"""
uncleaned = jsonlib.dumps(value)
clean = bleach.clean(uncleaned)
try:
jsonlib.loads(clean)
except:
# should never happen, but this is a last-line-of-defense check
# to make sure this blob wont get eval'ed by the JS engine as
# anything other than a JSON object
raise ValueError('JSON contains a quote or escape sequence that was unable to be stripped')
return mark_safe(clean)
@GitCMDR
Copy link

GitCMDR commented Sep 11, 2017

Remember to add 'from django.utils.safestring import mark_safe'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment