Debian Codegraph @

src/cherrypy_foundation/tools/tests/test_secure_headers.py

raw
1 # Cherrypy-foundation
2 # Copyright (C) 2026 IKUS Software
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 import cherrypy
18 from cherrypy.test import helper
19
20 from cherrypy_foundation.tools import secure_headers # noqa
21
22
23 @cherrypy.tools.secure_headers(
24 csp={
25 "default-src": "'self'",
26 "style-src": ("'self'", "'unsafe-inline'"),
27 "script-src": ("'self'", "'unsafe-inline'"),
28 "img-src": ("'self'", "data:"),
29 }
30 )
31 @cherrypy.tools.proxy(local=None, remote='X-Real-IP')
32 class Root:
33
34 @cherrypy.expose
35 @cherrypy.tools.sessions(locking='explicit')
36 def index(self):
37 return {'var1': 'test-jinja2'}
38
39 @cherrypy.expose
40 @cherrypy.tools.sessions(locking='explicit')
41 def logout(self, **kwargs):
42 raise cherrypy.HTTPRedirect('/')
43
44 @cherrypy.expose
45 @cherrypy.tools.secure_headers(on=False)
46 @cherrypy.tools.sessions(on=False)
47 def static(self, value=None):
48 return 'OK'
49
50
51 class SecureHeaderTest(helper.CPWebCase):
52 interactive = False
53
54 @classmethod
55 def setup_server(cls):
56 cherrypy.tree.mount(Root(), '/')
57
58 def test_cookie_samesite_lax(self):
59 # Given a request made to rdiffweb
60 # When receiving the response
61 self.getPage('/')
62 # Then the header contains Set-Cookie with SameSite=Lax
63 cookie = self.assertHeader('Set-Cookie')
64 self.assertIn('SameSite=Lax', cookie)
65
66 def test_cookie_samesite_lax_without_session(self):
67 # Given not a client sending no cookie
68 self.cookies = None
69 # When a query is made to a static path (without session)
70 self.getPage('/static/blue.css')
71 # Then Set-Cookie is not defined.
72 self.assertNoHeader('Set-Cookie')
73
74 def test_cookie_with_https(self):
75 # Given an https request made to rdiffweb
76 self.getPage('/', headers=[('X-Forwarded-Proto', 'https')])
77 # When receiving the response
78 self.assertStatus(200)
79 # Then the header contains Set-Cookie with Secure
80 cookie = self.assertHeader('Set-Cookie')
81 self.assertIn('Secure', cookie)
82
83 def test_cookie_with_http(self):
84 # Given an https request made to rdiffweb
85 self.getPage('/')
86 # When receiving the response
87 # Then the header contains Set-Cookie with Secure
88 cookie = self.assertHeader('Set-Cookie')
89 self.assertNotIn('Secure', cookie)
90
91 def test_get_with_wrong_origin(self):
92 # Given a GET request made to rdiffweb
93 # When the request is made using a different origin
94 self.getPage('/', headers=[('Origin', 'http://www.examples.com')])
95 # Then the response status it 200 OK.
96 self.assertStatus(200)
97
98 def test_post_with_wrong_origin(self):
99 # Given a POST request made to rdiffweb
100 # When the request is made using a different origin
101 self.getPage('/logout', headers=[('Origin', 'http://www.examples.com')], method='POST')
102 # Then the request is refused with 403 Forbiden
103 self.assertStatus(403)
104 self.assertInBody('Unexpected Origin header')
105
106 def test_post_with_prefixed_origin(self):
107 # Given a POST request made to rdiffweb
108 # When the request is made using a different origin
109 base = 'http://%s:%s' % (self.HOST + 'anything.com', self.PORT)
110 self.getPage('/logout', headers=[('Origin', base)], method='POST')
111 # Then the request is accepted with 200 OK
112 self.assertStatus(403)
113 self.assertInBody('Unexpected Origin header')
114
115 def test_post_with_valid_origin(self):
116 # Given a POST request made to rdiffweb
117 # When the request is made using a different origin
118 base = 'http://%s:%s' % (self.HOST, self.PORT)
119 self.getPage('/logout', headers=[('Origin', base)], method='POST')
120 # Then the request is accepted with 200 OK
121 self.assertStatus(303)
122
123 def test_post_without_origin(self):
124 # Given a POST request made to rdiffweb
125 # When the request is made without an origin
126 self.getPage('/logout', method='POST')
127 # Then the request is accepted with 200 OK
128 self.assertStatus(303)
129
130 def test_clickjacking_defense(self):
131 # Given a POST request made to rdiffweb
132 # When the request is made without an origin
133 self.getPage('/')
134 # Then the request is accepted with 200 OK
135 self.assertStatus(200)
136 self.assertHeaderItemValue('X-Frame-Options', 'DENY')
137
138 def test_no_cache(self):
139 # Given a POST request made to rdiffweb
140 # When the request is made without an origin
141 self.getPage('/')
142 # Then the request is accepted with 200 OK
143 self.assertStatus(200)
144 self.assertHeaderItemValue('Cache-control', 'no-cache')
145 self.assertHeaderItemValue('Cache-control', 'no-store')
146 self.assertHeaderItemValue('Cache-control', 'must-revalidate')
147 self.assertHeaderItemValue('Cache-control', 'max-age=0')
148 self.assertHeaderItemValue('Pragma', 'no-cache')
149 self.assertHeaderItemValue('Expires', '0')
150
151 def test_no_cache_with_static(self):
152 self.getPage('/static')
153 # Then the request is accepted with 200 OK
154 self.assertStatus(200)
155 self.assertNoHeader('Cache-control')
156 self.assertNoHeader('Pragma')
157 self.assertNoHeader('Expires')
158
159 def test_referrer_policy(self):
160 # Given a POST request made to rdiffweb
161 # When the request is made without an origin
162 self.getPage('/')
163 # Then the request is accepted with 200 OK
164 self.assertStatus(200)
165 self.assertHeaderItemValue('Referrer-Policy', 'same-origin')
166
167 def test_nosniff(self):
168 # Given a POST request made to rdiffweb
169 # When the request is made without an origin
170 self.getPage('/')
171 # Then the request is accepted with 200 OK
172 self.assertStatus(200)
173 self.assertHeaderItemValue('X-Content-Type-Options', 'nosniff')
174
175 def test_xss_protection(self):
176 # Given a POST request made to rdiffweb
177 # When the request is made without an origin
178 self.getPage('/')
179 # Then the request is accepted with 200 OK
180 self.assertStatus(200)
181 self.assertHeaderItemValue('X-XSS-Protection', '1; mode=block')
182
183 def test_content_security_policy(self):
184 # Given a POST request made to rdiffweb
185 # When the request is made without an origin
186 self.getPage('/')
187 # Then the request is accepted with 200 OK
188 self.assertStatus(200)
189 self.assertHeaderItemValue(
190 'Content-Security-Policy',
191 "default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; img-src 'self' data:",
192 )
193
194 def test_strict_transport_security(self):
195 # Given a POST request made to rdiffweb
196 # When the request is made without an origin
197 self.getPage('/', headers=[('X-Forwarded-Proto', 'https')])
198 # Then the request is accepted with 200 OK
199 self.assertStatus(200)
200 self.assertHeaderItemValue('Strict-Transport-Security', 'max-age=31536000; includeSubDomains')