File tree Expand file tree Collapse file tree 3 files changed +35
-1
lines changed
Misc/NEWS.d/next/Security Expand file tree Collapse file tree 3 files changed +35
-1
lines changed Original file line number Diff line number Diff line change 2020 CGIXMLRPCRequestHandler ,
2121 resolve_dotted_attribute )
2222
23+
24+ def _html_escape_quote (s ):
25+ s = s .replace ("&" , "&" ) # Must be done first!
26+ s = s .replace ("<" , "<" )
27+ s = s .replace (">" , ">" )
28+ s = s .replace ('"' , """ )
29+ s = s .replace ('\' ' , "'" )
30+ return s
31+
32+
2333class ServerHTMLDoc (pydoc .HTMLDoc ):
2434 """Class used to generate pydoc HTML document for a server"""
2535
@@ -210,7 +220,8 @@ def generate_html_documentation(self):
210220 methods
211221 )
212222
213- return documenter .page (self .server_title , documentation )
223+ title = _html_escape_quote (self .server_title )
224+ return documenter .page (title , documentation )
214225
215226class DocXMLRPCRequestHandler (SimpleXMLRPCRequestHandler ):
216227 """XML-RPC and documentation request handler class.
Original file line number Diff line number Diff line change 11from DocXMLRPCServer import DocXMLRPCServer
22import httplib
3+ import re
34import sys
45from test import test_support
56threading = test_support .import_module ('threading' )
@@ -176,6 +177,25 @@ def test_autolink_dotted_methods(self):
176177 self .assertIn ("""Try self.<strong>add</strong>, too.""" ,
177178 response .read ())
178179
180+ def test_server_title_escape (self ):
181+ """Test that the server title and documentation
182+ are escaped for HTML.
183+ """
184+ self .serv .set_server_title ('test_title<script>' )
185+ self .serv .set_server_documentation ('test_documentation<script>' )
186+ self .assertEqual ('test_title<script>' , self .serv .server_title )
187+ self .assertEqual ('test_documentation<script>' ,
188+ self .serv .server_documentation )
189+
190+ generated = self .serv .generate_html_documentation ()
191+ title = re .search (r'<title>(.+?)</title>' , generated ).group ()
192+ documentation = re .search (r'<p><tt>(.+?)</tt></p>' , generated ).group ()
193+ self .assertEqual ('<title>Python: test_title<script></title>' ,
194+ title )
195+ self .assertEqual ('<p><tt>test_documentation<script></tt></p>' ,
196+ documentation )
197+
198+
179199def test_main ():
180200 test_support .run_unittest (DocXMLRPCHTTPGETServer )
181201
Original file line number Diff line number Diff line change 1+ Escape the server title of :class: `DocXMLRPCServer.DocXMLRPCServer `
2+ when rendering the document page as HTML.
3+ (Contributed by Dong-hee Na in :issue: `38243 `.)
You can’t perform that action at this time.
0 commit comments