comparison tests/test-hgweb-csp.t @ 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
children a6d95a8b7243
comparison
equal deleted inserted replaced
30765:eb7de21b15be 30766:d7bf7d2bd5ab
1 #require serve
2
3 $ cat > web.conf << EOF
4 > [paths]
5 > / = $TESTTMP/*
6 > EOF
7
8 $ hg init repo1
9 $ cd repo1
10 $ touch foo
11 $ hg -q commit -A -m initial
12 $ cd ..
13
14 $ hg serve -p $HGPORT -d --pid-file=hg.pid --web-conf web.conf
15 $ cat hg.pid >> $DAEMON_PIDS
16
17 repo index should not send Content-Security-Policy header by default
18
19 $ get-with-headers.py --headeronly localhost:$HGPORT '' content-security-policy etag
20 200 Script output follows
21
22 static page should not send CSP by default
23
24 $ get-with-headers.py --headeronly localhost:$HGPORT static/mercurial.js content-security-policy etag
25 200 Script output follows
26
27 repo page should not send CSP by default, should send ETag
28
29 $ get-with-headers.py --headeronly localhost:$HGPORT repo1 content-security-policy etag
30 200 Script output follows
31 etag: W/"*" (glob)
32
33 $ killdaemons.py
34
35 Configure CSP without nonce
36
37 $ cat >> web.conf << EOF
38 > [web]
39 > csp = script-src https://example.com/ 'unsafe-inline'
40 > EOF
41
42 $ hg serve -p $HGPORT -d --pid-file=hg.pid --web-conf web.conf
43 $ cat hg.pid > $DAEMON_PIDS
44
45 repo index should send Content-Security-Policy header when enabled
46
47 $ get-with-headers.py --headeronly localhost:$HGPORT '' content-security-policy etag
48 200 Script output follows
49 content-security-policy: script-src https://example.com/ 'unsafe-inline'
50
51 static page should send CSP when enabled
52
53 $ get-with-headers.py --headeronly localhost:$HGPORT static/mercurial.js content-security-policy etag
54 200 Script output follows
55 content-security-policy: script-src https://example.com/ 'unsafe-inline'
56
57 repo page should send CSP by default, include etag w/o nonce
58
59 $ get-with-headers.py --headeronly localhost:$HGPORT repo1 content-security-policy etag
60 200 Script output follows
61 content-security-policy: script-src https://example.com/ 'unsafe-inline'
62 etag: W/"*" (glob)
63
64 nonce should not be added to html if CSP doesn't use it
65
66 $ get-with-headers.py localhost:$HGPORT repo1/graph/tip | egrep 'content-security-policy|<script'
67 <script type="text/javascript" src="/repo1/static/mercurial.js"></script>
68 <!--[if IE]><script type="text/javascript" src="/repo1/static/excanvas.js"></script><![endif]-->
69 <script type="text/javascript">
70 <script type="text/javascript">
71
72 Configure CSP with nonce
73
74 $ killdaemons.py
75 $ cat >> web.conf << EOF
76 > csp = image-src 'self'; script-src https://example.com/ 'nonce-%nonce%'
77 > EOF
78
79 $ hg serve -p $HGPORT -d --pid-file=hg.pid --web-conf web.conf
80 $ cat hg.pid > $DAEMON_PIDS
81
82 nonce should be substituted in CSP header
83
84 $ get-with-headers.py --headeronly localhost:$HGPORT '' content-security-policy etag
85 200 Script output follows
86 content-security-policy: image-src 'self'; script-src https://example.com/ 'nonce-*' (glob)
87
88 nonce should be included in CSP for static pages
89
90 $ get-with-headers.py --headeronly localhost:$HGPORT static/mercurial.js content-security-policy etag
91 200 Script output follows
92 content-security-policy: image-src 'self'; script-src https://example.com/ 'nonce-*' (glob)
93
94 repo page should have nonce, no ETag
95
96 $ get-with-headers.py --headeronly localhost:$HGPORT repo1 content-security-policy etag
97 200 Script output follows
98 content-security-policy: image-src 'self'; script-src https://example.com/ 'nonce-*' (glob)
99
100 nonce should be added to html when used
101
102 $ get-with-headers.py localhost:$HGPORT repo1/graph/tip content-security-policy | egrep 'content-security-policy|<script'
103 content-security-policy: image-src 'self'; script-src https://example.com/ 'nonce-*' (glob)
104 <script type="text/javascript" src="/repo1/static/mercurial.js"></script>
105 <!--[if IE]><script type="text/javascript" src="/repo1/static/excanvas.js"></script><![endif]-->
106 <script type="text/javascript" nonce="*"> (glob)
107 <script type="text/javascript" nonce="*"> (glob)
108
109 hgweb_mod w/o hgwebdir works as expected
110
111 $ killdaemons.py
112
113 $ hg -R repo1 serve -p $HGPORT -d --pid-file=hg.pid --config "web.csp=image-src 'self'; script-src https://example.com/ 'nonce-%nonce%'"
114 $ cat hg.pid > $DAEMON_PIDS
115
116 static page sends CSP
117
118 $ get-with-headers.py --headeronly localhost:$HGPORT static/mercurial.js content-security-policy etag
119 200 Script output follows
120 content-security-policy: image-src 'self'; script-src https://example.com/ 'nonce-*' (glob)
121
122 nonce included in <script> and headers
123
124 $ get-with-headers.py localhost:$HGPORT graph/tip content-security-policy | egrep 'content-security-policy|<script'
125 content-security-policy: image-src 'self'; script-src https://example.com/ 'nonce-*' (glob)
126 <script type="text/javascript" src="/static/mercurial.js"></script>
127 <!--[if IE]><script type="text/javascript" src="/static/excanvas.js"></script><![endif]-->
128 <script type="text/javascript" nonce="*"> (glob)
129 <script type="text/javascript" nonce="*"> (glob)