Debian Codegraph @

tests/test-hgweb-csp.t

raw
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 $ get-with-headers.py --twice --headeronly localhost:$HGPORT repo1/static/style.css content-security-policy
58 200 Script output follows
59 content-security-policy: script-src https://example.com/ 'unsafe-inline'
60 304 Not Modified
61 content-security-policy: script-src https://example.com/ 'unsafe-inline'
62
63 repo page should send CSP by default, include etag w/o nonce
64
65 $ get-with-headers.py --headeronly localhost:$HGPORT repo1 content-security-policy etag
66 200 Script output follows
67 content-security-policy: script-src https://example.com/ 'unsafe-inline'
68 etag: W/"*" (glob)
69
70 nonce should not be added to html if CSP doesn't use it
71
72 $ get-with-headers.py localhost:$HGPORT repo1/graph/tip | grep -E 'content-security-policy|<script'
73 <script type="text/javascript" src="/repo1/static/mercurial.js"></script>
74 <script type="text/javascript">
75 <script type="text/javascript">
76
77 Configure CSP with nonce
78
79 $ killdaemons.py
80 $ cat >> web.conf << EOF
81 > csp = image-src 'self'; script-src https://example.com/ 'nonce-%nonce%'
82 > EOF
83
84 $ hg serve -p $HGPORT -d --pid-file=hg.pid --web-conf web.conf
85 $ cat hg.pid > $DAEMON_PIDS
86
87 nonce should be substituted in CSP header
88
89 $ get-with-headers.py --headeronly localhost:$HGPORT '' content-security-policy etag
90 200 Script output follows
91 content-security-policy: image-src 'self'; script-src https://example.com/ 'nonce-*' (glob)
92
93 nonce should be included in CSP for static pages
94
95 $ get-with-headers.py --headeronly localhost:$HGPORT static/mercurial.js content-security-policy etag
96 200 Script output follows
97 content-security-policy: image-src 'self'; script-src https://example.com/ 'nonce-*' (glob)
98
99 repo page should have nonce, no ETag
100
101 $ get-with-headers.py --headeronly localhost:$HGPORT repo1 content-security-policy etag
102 200 Script output follows
103 content-security-policy: image-src 'self'; script-src https://example.com/ 'nonce-*' (glob)
104
105 nonce should be added to html when used
106
107 $ get-with-headers.py localhost:$HGPORT repo1/graph/tip content-security-policy | grep -E 'content-security-policy|<script'
108 content-security-policy: image-src 'self'; script-src https://example.com/ 'nonce-*' (glob)
109 <script type="text/javascript" src="/repo1/static/mercurial.js"></script>
110 <script type="text/javascript" nonce="*"> (glob)
111 <script type="text/javascript" nonce="*"> (glob)
112
113 hgweb_mod w/o hgwebdir works as expected
114
115 $ killdaemons.py
116
117 $ hg serve -R repo1 -p $HGPORT -d --pid-file=hg.pid --config "web.csp=image-src 'self'; script-src https://example.com/ 'nonce-%nonce%'"
118 $ cat hg.pid > $DAEMON_PIDS
119
120 static page sends CSP
121
122 $ get-with-headers.py --headeronly localhost:$HGPORT static/mercurial.js content-security-policy etag
123 200 Script output follows
124 content-security-policy: image-src 'self'; script-src https://example.com/ 'nonce-*' (glob)
125
126 nonce included in <script> and headers
127
128 $ get-with-headers.py localhost:$HGPORT graph/tip content-security-policy | grep -E 'content-security-policy|<script'
129 content-security-policy: image-src 'self'; script-src https://example.com/ 'nonce-*' (glob)
130 <script type="text/javascript" src="/static/mercurial.js"></script>
131 <script type="text/javascript" nonce="*"> (glob)
132 <script type="text/javascript" nonce="*"> (glob)