From dbaf181a507b12b4b7cdac4c698ac0369c5d8ea3 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Thu, 7 Jan 2021 20:22:53 -0600 Subject: [PATCH] Escape illegal XML in Feed --- judge/feed.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/judge/feed.py b/judge/feed.py index c436745..ebb2f5f 100644 --- a/judge/feed.py +++ b/judge/feed.py @@ -8,6 +8,13 @@ from django.utils.feedgenerator import Atom1Feed from judge.jinja2.markdown import markdown from judge.models import BlogPost, Comment, Problem +import re + + +# https://lsimons.wordpress.com/2011/03/17/stripping-illegal-characters-out-of-xml-in-python/ +def escape_xml_illegal_chars(val, replacement='?'): + _illegal_xml_chars_RE = re.compile(u'[\x00-\x08\x0b\x0c\x0e-\x1F\uD800-\uDFFF\uFFFE\uFFFF]') + return _illegal_xml_chars_RE.sub(replacement, val) class ProblemFeed(Feed): title = 'Recently Added %s Problems' % settings.SITE_NAME @@ -86,6 +93,7 @@ class BlogFeed(Feed): summary = cache.get(key) if summary is None: summary = str(markdown(post.summary or post.content, 'blog')) + summary = escape_xml_illegal_chars(summary) cache.set(key, summary, 86400) return summary