Skip to content

Commit

Permalink
[2.7] bpo-38243: Escape the server title of DocXMLRPCServer (GH-16447)
Browse files Browse the repository at this point in the history
Escape the server title of DocXMLRPCServer.DocXMLRPCServer
when rendering the document page as HTML.
  • Loading branch information
corona10 authored and vstinner committed Oct 1, 2019
1 parent 598f676 commit 8eb6415
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Lib/DocXMLRPCServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
CGIXMLRPCRequestHandler,
resolve_dotted_attribute)


def _html_escape_quote(s):
s = s.replace("&", "&") # 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"""

Expand Down Expand Up @@ -210,7 +220,8 @@ def generate_html_documentation(self):
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.
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_docxmlrpc.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -176,6 +177,25 @@ def test_autolink_dotted_methods(self):
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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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`.)

1 comment on commit 8eb6415

@Beyond-My
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,i have a question when i look this patch, I want to ask you what exactly this function does? Is it just for testing? " def test_server_title_escape(self):"
Thanks for your answer。

Please sign in to comment.