comparison mercurial/hgweb/hgwebdir_mod.py @ 30766:d7bf7d2bd5ab

hgweb: support Content Security Policy Content-Security-Policy (CSP) is a web security feature that allows servers to declare what loaded content is allowed to do. For example, a policy can prevent loading of images, JavaScript, CSS, etc unless the source of that content is whitelisted (by hostname, URI scheme, hashes of content, etc). It's a nifty security feature that provides extra mitigation against some attacks, notably XSS. Mitigation against these attacks is important for Mercurial because hgweb renders repository data, which is commonly untrusted. While we make attempts to escape things, etc, there's the possibility that malicious data could be injected into the site content. If this happens today, the full power of the web browser is available to that malicious content. A restrictive CSP policy (defined by the server operator and sent in an HTTP header which is outside the control of malicious content), could restrict browser capabilities and mitigate security problems posed by malicious data. CSP works by emitting an HTTP header declaring the policy that browsers should apply. Ideally, this header would be emitted by a layer above Mercurial (likely the HTTP server doing the WSGI "proxying"). This works for some CSP policies, but not all. For example, policies to allow inline JavaScript may require setting a "nonce" attribute on <script>. This attribute value must be unique and non-guessable. And, the value must be present in the HTTP header and the HTML body. This means that coordinating the value between Mercurial and another HTTP server could be difficult: it is much easier to generate and emit the nonce in a central location. This commit introduces support for emitting a Content-Security-Policy header from hgweb. A config option defines the header value. If present, the header is emitted. A special "%nonce%" syntax in the value triggers generation of a nonce and inclusion in <script> elements in templates. The inclusion of a nonce does not occur unless "%nonce%" is present. This makes this commit completely backwards compatible and the feature opt-in. The nonce is a type 4 UUID, which is the flavor that is randomly generated. It has 122 random bits, which should be plenty to satisfy the guarantees of a nonce.
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 10 Jan 2017 23:37:08 -0800
parents e38e7ea21987
children da7d19324b1e
comparison
equal deleted inserted replaced
30765:eb7de21b15be 30766:d7bf7d2bd5ab
17 from .common import ( 17 from .common import (
18 ErrorResponse, 18 ErrorResponse,
19 HTTP_NOT_FOUND, 19 HTTP_NOT_FOUND,
20 HTTP_OK, 20 HTTP_OK,
21 HTTP_SERVER_ERROR, 21 HTTP_SERVER_ERROR,
22 cspvalues,
22 get_contact, 23 get_contact,
23 get_mtime, 24 get_mtime,
24 ismember, 25 ismember,
25 paritygen, 26 paritygen,
26 staticfile, 27 staticfile,
225 226
226 def _runwsgi(self, req): 227 def _runwsgi(self, req):
227 try: 228 try:
228 self.refresh() 229 self.refresh()
229 230
231 csp, nonce = cspvalues(self.ui)
232 if csp:
233 req.headers.append(('Content-Security-Policy', csp))
234
230 virtual = req.env.get("PATH_INFO", "").strip('/') 235 virtual = req.env.get("PATH_INFO", "").strip('/')
231 tmpl = self.templater(req) 236 tmpl = self.templater(req, nonce)
232 ctype = tmpl('mimetype', encoding=encoding.encoding) 237 ctype = tmpl('mimetype', encoding=encoding.encoding)
233 ctype = templater.stringify(ctype) 238 ctype = templater.stringify(ctype)
234 239
235 # a static file 240 # a static file
236 if virtual.startswith('static/') or 'static' in req.form: 241 if virtual.startswith('static/') or 'static' in req.form:
464 return tmpl("index", entries=entries, subdir=subdir, 469 return tmpl("index", entries=entries, subdir=subdir,
465 pathdef=hgweb_mod.makebreadcrumb('/' + subdir, self.prefix), 470 pathdef=hgweb_mod.makebreadcrumb('/' + subdir, self.prefix),
466 sortcolumn=sortcolumn, descending=descending, 471 sortcolumn=sortcolumn, descending=descending,
467 **dict(sort)) 472 **dict(sort))
468 473
469 def templater(self, req): 474 def templater(self, req, nonce):
470 475
471 def motd(**map): 476 def motd(**map):
472 if self.motd is not None: 477 if self.motd is not None:
473 yield self.motd 478 yield self.motd
474 else: 479 else:
508 "logourl": logourl, 513 "logourl": logourl,
509 "logoimg": logoimg, 514 "logoimg": logoimg,
510 "staticurl": staticurl, 515 "staticurl": staticurl,
511 "sessionvars": sessionvars, 516 "sessionvars": sessionvars,
512 "style": style, 517 "style": style,
518 "nonce": nonce,
513 } 519 }
514 tmpl = templater.templater.frommapfile(mapfile, defaults=defaults) 520 tmpl = templater.templater.frommapfile(mapfile, defaults=defaults)
515 return tmpl 521 return tmpl
516 522
517 def updatereqenv(self, env): 523 def updatereqenv(self, env):