[CVE-2019-16935] bpo-38243: Escape the server title of DocXMLRPCServer (GH-16447) 99/215299/1 accepted/tizen_5.5_base accepted/tizen_5.5_base_mobile_hotfix accepted/tizen_5.5_base_wearable_hotfix sandbox/backup/tizen_5.5_base/python_2.7.15_20191209 tizen_5.5_base_mobile_hotfix tizen_5.5_base_wearable_hotfix tizen_5.5_tv accepted/tizen/5.5/base/20191030.083110 accepted/tizen/5.5/base/mobile/hotfix/20201023.084940 accepted/tizen/5.5/base/wearable/hotfix/20201023.081305 accepted/tizen/base/20191008.101327 submit/tizen_5.5_base/20191030.000001 submit/tizen_5.5_base_mobile_hotfix/20201023.171501 submit/tizen_5.5_base_wearable_hotfix/20201023.155601 submit/tizen_base/20191007.022254 tizen_5.5.m2_release
authorDong-hee Na <donghee.na92@gmail.com>
Tue, 1 Oct 2019 10:58:01 +0000 (19:58 +0900)
committerDongHun Kwak <dh0128.kwak@samsung.com>
Mon, 7 Oct 2019 01:02:35 +0000 (10:02 +0900)
Escape the server title of DocXMLRPCServer.DocXMLRPCServer
when rendering the document page as HTML.

Change-Id: Id7e5a2c440b9a2e9bc832bd321740ce0c1581edf
Signed-off-by: DongHun Kwak <dh0128.kwak@samsung.com>
Lib/DocXMLRPCServer.py
Lib/test/test_docxmlrpc.py
Misc/NEWS.d/next/Security/2019-09-25-13-21-09.bpo-38243.1pfz24.rst [new file with mode: 0644]

index 4064ec2..90b037d 100644 (file)
@@ -20,6 +20,16 @@ from SimpleXMLRPCServer import (SimpleXMLRPCServer,
             CGIXMLRPCRequestHandler,
             resolve_dotted_attribute)
 
+
+def _html_escape_quote(s):
+    s = s.replace("&", "&amp;") # Must be done first!
+    s = s.replace("<", "&lt;")
+    s = s.replace(">", "&gt;")
+    s = s.replace('"', "&quot;")
+    s = s.replace('\'', "&#x27;")
+    return s
+
+
 class ServerHTMLDoc(pydoc.HTMLDoc):
     """Class used to generate pydoc HTML document for a server"""
 
@@ -210,7 +220,8 @@ class XMLRPCDocGenerator:
                                 methods
                             )
 
-        return documenter.page(self.server_title, documentation)
+        title = _html_escape_quote(self.server_title)
+        return documenter.page(title, documentation)
 
 class DocXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
     """XML-RPC and documentation request handler class.
index 4dff415..c45b892 100644 (file)
@@ -1,5 +1,6 @@
 from DocXMLRPCServer import DocXMLRPCServer
 import httplib
+import re
 import sys
 from test import test_support
 threading = test_support.import_module('threading')
@@ -176,6 +177,25 @@ class DocXMLRPCHTTPGETServer(unittest.TestCase):
         self.assertIn("""Try&nbsp;self.<strong>add</strong>,&nbsp;too.""",
                       response.read())
 
+    def test_server_title_escape(self):
+        """Test that the server title and documentation
+        are escaped for HTML.
+        """
+        self.serv.set_server_title('test_title<script>')
+        self.serv.set_server_documentation('test_documentation<script>')
+        self.assertEqual('test_title<script>', self.serv.server_title)
+        self.assertEqual('test_documentation<script>',
+                self.serv.server_documentation)
+
+        generated = self.serv.generate_html_documentation()
+        title = re.search(r'<title>(.+?)</title>', generated).group()
+        documentation = re.search(r'<p><tt>(.+?)</tt></p>', generated).group()
+        self.assertEqual('<title>Python: test_title&lt;script&gt;</title>',
+                title)
+        self.assertEqual('<p><tt>test_documentation&lt;script&gt;</tt></p>',
+                documentation)
+
+
 def test_main():
     test_support.run_unittest(DocXMLRPCHTTPGETServer)
 
diff --git a/Misc/NEWS.d/next/Security/2019-09-25-13-21-09.bpo-38243.1pfz24.rst b/Misc/NEWS.d/next/Security/2019-09-25-13-21-09.bpo-38243.1pfz24.rst
new file mode 100644 (file)
index 0000000..8f02bae
--- /dev/null
@@ -0,0 +1,3 @@
+Escape the server title of :class:`DocXMLRPCServer.DocXMLRPCServer`
+when rendering the document page as HTML.
+(Contributed by Dong-hee Na in :issue:`38243`.)