summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorFlorian Weimer <fw@deneb.enyo.de>2020-05-01 14:11:50 +0200
committerSalvatore Bonaccorso <carnil@debian.org>2020-05-01 16:34:37 +0200
commita1f8d448da19a59a502d90ce40901f50e5249e38 (patch)
treee34079ad24611f2c268e21e6d9d171523a9aa907 /lib
parent095a5904c54ea17e27cff1e2c48eb68f6c0e42ee (diff)
Add basic Unicode support to the web framework
As mentioned in Debian bug #959231 ("security-tracker: Proxy Error on CVE-2020-11565 tracker page"): * Florian Weimer: > * Francesco Poli: > >> Please note that the CVE is mentioned in [DSA-4667-1]. >> >> [DSA-4667-1]: <https://lists.debian.org/debian-security-announce/2020/msg00071.html> >> >> What's wrong with that tracker page? > > It's something in the NVD data that breaks the HTML escaping. This patch adds basic Unicode support to the web framework. I'm not sure if it is the right direction to move in, but it fixes the issue. An alternative fix would be to change the NVD importer not to put Unicode strings into the database, by encoding them as byte strings first. [carnil: Slightly rewrite the commit message] BugLink: https://bugs.debian.org/929228 BugLink: https://bugs.debian.org/959231 Signed-off-by: Florian Weimer <fw@deneb.enyo.de> Signed-off-by: Salvatore Bonaccorso <carnil@debian.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/python/web_support.py42
1 files changed, 22 insertions, 20 deletions
diff --git a/lib/python/web_support.py b/lib/python/web_support.py
index 5752f34b5f..116cbec2be 100644
--- a/lib/python/web_support.py
+++ b/lib/python/web_support.py
@@ -220,27 +220,25 @@ class URLFactory:
def updateParams(self, **args):
self.updateParamsDict(args)
-charToHTML = map(chr, range(256))
-charToHTMLattr = map(chr, range(256))
-def _initStringToHTML(s):
- for (ch, repl) in (('<', '&lt;'),
- ('>', '&gt;'),
- ('&', '&amp;')):
- s[ord(ch)] = repl
-_initStringToHTML(charToHTML)
-_initStringToHTML(charToHTMLattr)
-charToHTMLattr[ord('"')] = '&34;'
-del _initStringToHTML
+charToHTML = {
+ '<' : '&lt;',
+ '>' : '&gt;',
+ '&' : '&amp;',
+}
+charToHTMLattr = {
+ '&' : '&amp;',
+ '"' : '&34;',
+}
def escapeHTML(str):
- '''Replaces the characters <>&" in the passed strings with their
+ '''Replaces the characters <>& in the passed strings with their
HTML entities.'''
+ return ''.join([charToHTML.get(ch, ch) for ch in str])
- result = []
- append = result.append
- for ch in str:
- append(charToHTML[ord(ch)])
- return ''.join(result)
+def escapeHTMLattr(str):
+ '''Replaces the characters &" in the passed strings with their
+ HTML entities.'''
+ return ''.join([charToHTMLattr.get(ch, ch) for ch in str])
class HTMLBase:
def flatten(self, write):
@@ -310,8 +308,7 @@ class Tag(HTMLBase):
else:
append(key)
append('="')
- for ch in str(value):
- append(charToHTMLattr[ord(ch)])
+ append(escapeHTMLattr(str(value)))
append('"')
self.__attribs = ''.join(attrs)
self.contents = contents
@@ -659,7 +656,12 @@ class HTMLResult(Result):
buf = cStringIO.StringIO()
buf.write(self.doctype)
buf.write('\n')
- self.contents.flatten(buf.write)
+ def write_both(s):
+ if type(s) == types.UnicodeType:
+ buf.write(s.encode('UTF-8'))
+ else:
+ buf.write(s)
+ self.contents.flatten(write_both)
buf = buf.getvalue()
self.headers['Content-Length'] = str(len(buf))
def later(req):

© 2014-2024 Faster IT GmbH | imprint | privacy policy