Sanitize a url against xss attacks in "safe_mode". Rather than specifically blacklisting `javascript:alert("XSS")` and all its aliases (see ), we whitelist known safe url formats. Most urls contain a network location, however some
(self, url)
| 241 | return el |
| 242 | |
| 243 | def sanitize_url(self, url): |
| 244 | """ |
| 245 | Sanitize a url against xss attacks in "safe_mode". |
| 246 | |
| 247 | Rather than specifically blacklisting `javascript:alert("XSS")` and all |
| 248 | its aliases (see <http://ha.ckers.org/xss.html>), we whitelist known |
| 249 | safe url formats. Most urls contain a network location, however some |
| 250 | are known not to (i.e.: mailto links). Script urls do not contain a |
| 251 | location. Additionally, for `javascript:...`, the scheme would be |
| 252 | "javascript" but some aliases will appear to `urlparse()` to have no |
| 253 | scheme. On top of that relative links (i.e.: "foo/bar.html") have no |
| 254 | scheme. Therefore we must check "path", "parameters", "query" and |
| 255 | "fragment" for any literal colons. We don't check "scheme" for colons |
| 256 | because it *should* never have any and "netloc" must allow the form: |
| 257 | `username:password@host:port`. |
| 258 | |
| 259 | """ |
| 260 | locless_schemes = ['', 'mailto', 'news'] |
| 261 | scheme, netloc, path, params, query, fragment = url = urlparse(url) |
| 262 | safe_url = False |
| 263 | if netloc != '' or scheme in locless_schemes: |
| 264 | safe_url = True |
| 265 | |
| 266 | for part in url[2:]: |
| 267 | if ":" in part: |
| 268 | safe_url = False |
| 269 | |
| 270 | if self.markdown.safeMode and not safe_url: |
| 271 | return '' |
| 272 | else: |
| 273 | return urlunparse(url) |
| 274 | |
| 275 | class ImagePattern(LinkPattern): |
| 276 | """ Return a img element from the given match. """ |
no outgoing calls
no test coverage detected