|
38 | 38 | from __future__ import print_function
|
39 | 39 | from __future__ import absolute_import
|
40 | 40 |
|
41 |
| -import cgi |
42 | 41 | import re
|
| 42 | +import sys |
43 | 43 | from email.utils import parseaddr, formataddr
|
44 | 44 | from string import ascii_letters, digits, printable
|
45 | 45 | from unicodedata import category, normalize, name as unicode_name
|
|
49 | 49 | except ImportError:
|
50 | 50 | nbformat = None
|
51 | 51 |
|
| 52 | +PY2 = sys.version_info[0] < 3 |
| 53 | + |
| 54 | +escape_html = None |
| 55 | +if PY2: |
| 56 | + from cgi import escape as escape_html |
| 57 | +else: |
| 58 | + from html import escape as escape_html |
| 59 | +assert escape_html is not None |
| 60 | + |
52 | 61 | from mig.shared.base import force_unicode, force_utf8
|
53 | 62 | from mig.shared.defaults import src_dst_sep, username_charset, \
|
54 | 63 | username_max_length, session_id_charset, session_id_length, \
|
@@ -319,16 +328,18 @@ def __wrap_unicode_val(char):
|
319 | 328 |
|
320 | 329 | # Public functions
|
321 | 330 |
|
322 |
| -def html_escape(contents): |
323 |
| - """Uses cgi.escape() to encode contents in a html safe way. In that |
| 331 | +def html_escape(contents, quote=None): |
| 332 | + """Use an stdlib escape to encode contents in a html safe way. In that |
324 | 333 | way the resulting data can be included in a html page without risk
|
325 | 334 | of XSS vulnerabilities.
|
| 335 | + The optional quote argument is passed as-is to enable additional escaping |
| 336 | + of single and double quotes. |
326 | 337 | """
|
327 | 338 |
|
328 | 339 | # We use html_escape as a general protection even though it is
|
329 |
| - # mostly html (cgi) related |
| 340 | + # mostly html request related |
330 | 341 |
|
331 |
| - return cgi.escape(contents) |
| 342 | + return escape_html(contents, quote) |
332 | 343 |
|
333 | 344 |
|
334 | 345 | def valid_printable(contents, min_length=0, max_length=-1):
|
@@ -2270,7 +2281,9 @@ def __str__(self):
|
2270 | 2281 | return force_utf8(force_unicode(self.value))
|
2271 | 2282 |
|
2272 | 2283 |
|
2273 |
| -if __name__ == '__main__': |
| 2284 | +def main(_print=print): |
| 2285 | + print = _print # workaround print as reserved word on PY2 |
| 2286 | + |
2274 | 2287 | for test_cn in ('Firstname Lastname', 'Test Æøå', 'Test Überh4x0r',
|
2275 | 2288 | 'Harry S. Truman', u'Unicode æøå', "Invalid D'Angelo",
|
2276 | 2289 | 'Test Maybe Invalid Źacãŕ', 'Test Invalid ?',
|
@@ -2468,3 +2481,6 @@ def __str__(self):
|
2468 | 2481 | print("Rejected:")
|
2469 | 2482 | for (key, val) in rejected.items():
|
2470 | 2483 | print("\t%s: %s" % (key, val))
|
| 2484 | + |
| 2485 | +if __name__ == '__main__': |
| 2486 | + main() |
0 commit comments