From: Mark Wooding Date: Wed, 13 Mar 2013 14:41:51 +0000 (+0000) Subject: cgi.py: Escape quote signs in `htmlescape' and `html_quotify'. X-Git-Tag: 1.0.0~17 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/chopwood/commitdiff_plain/b40d16b214d9164e6255857a63c729c5f65b017f?ds=sidebyside cgi.py: Escape quote signs in `htmlescape' and `html_quotify'. This helps protect against XSS attacks. --- diff --git a/cgi.py b/cgi.py index 0bd66cc..531a49c 100644 --- a/cgi.py +++ b/cgi.py @@ -59,7 +59,7 @@ CONF.DEFAULTS.update( ## Some handy regular expressions. R_URLESC = RX.compile('%([0-9a-fA-F]{2})') R_URLBAD = RX.compile('[^-\\w,.!]') -R_HTMLBAD = RX.compile('[&<>]') +R_HTMLBAD = RX.compile('[&<>\'"]') def urldecode(s): """Decode a single form-url-encoded string S.""" @@ -77,17 +77,18 @@ def htmlescape(s): ## Some standard character sequences, and HTML entity names for prettier ## versions. -_quotify = U.StringSubst({ +html_quotify = U.StringSubst({ + "<": '<', + ">": '>', + "&": '&', "`": '‘', "'": '’', + '"': '"', "``": '“', "''": '”', "--": '–', "---": '—' }) -def html_quotify(s): - """Return a pretty HTML version of S.""" - return _quotify(htmlescape(s)) ###-------------------------------------------------------------------------- ### Output machinery.