Hi Florian,

On Fri, May 01, 2020 at 02:33:21PM +0200, Florian Weimer wrote:
> * Salvatore Bonaccorso:
> 
> > Hi Florian,
> >
> > On Fri, May 01, 2020 at 02:11:50PM +0200, Florian Weimer wrote:
> >> * 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.
> >
> > Do you want to deploy that or rather investigate an alternative?
> 
> I'd appreciate if you could spot-check the changes (e.g., do we still
> do HTML escaping properly?) and deploy it.  It looks like I have
> forgotten how to do it.

Looks good to me, and yes can deploy it if you want me to. Please have
a look at at attache git format-patch'ed version if you agree with the
slight rewrite, since I do not want to commit something in your name
you would not agree with).

Salvatore
>From 88268c60d683edcda8aa2ae9b339bd46c59e58ec Mon Sep 17 00:00:00 2001
From: Florian Weimer <f...@deneb.enyo.de>
Date: Fri, 1 May 2020 14:11:50 +0200
Subject: [PATCH] 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: Salvatore Bonaccorso <car...@debian.org>
---
 lib/python/web_support.py | 42 ++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 20 deletions(-)

diff --git a/lib/python/web_support.py b/lib/python/web_support.py
index 5752f34b5f77..116cbec2bea3 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):
-- 
2.26.2

Reply via email to