Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
surykatka
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Romain Courteaud
surykatka
Commits
91cdbde6
Commit
91cdbde6
authored
Aug 04, 2022
by
Vincent Pelletier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
HTTP: Strip nonce values from Content-Security-Policy headers
Thus stabilising the value of these headers.
parent
e335e8fb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
1 deletion
+42
-1
src/surykatka/http.py
src/surykatka/http.py
+12
-1
tests/test_http.py
tests/test_http.py
+30
-0
No files found.
src/surykatka/http.py
View file @
91cdbde6
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
# See COPYING file for full licensing terms.
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
# See https://www.nexedi.com/licensing for rationale and options.
import
re
import
requests
import
requests
import
sys
import
sys
import
traceback
import
traceback
...
@@ -29,6 +30,8 @@ PREFERRED_TYPE = "text/html"
...
@@ -29,6 +30,8 @@ PREFERRED_TYPE = "text/html"
TIMEOUT
=
2
TIMEOUT
=
2
ELAPSED_FAST
=
0.2
ELAPSED_FAST
=
0.2
ELAPSED_MODERATE
=
0.5
ELAPSED_MODERATE
=
0.5
SUB_CSP_NONCE_REPL
=
"'nonce-'"
SUB_CSP_NONCE
=
re
.
compile
(
r"'nonce-[^']*'"
).
sub
def
getUrlHostname
(
url
):
def
getUrlHostname
(
url
):
...
@@ -244,7 +247,6 @@ def checkHttpStatus(
...
@@ -244,7 +247,6 @@ def checkHttpStatus(
"Content-Encoding"
,
"Content-Encoding"
,
"Content-Disposition"
,
"Content-Disposition"
,
# Security
# Security
"Content-Security-Policy"
,
"Referrer-Policy"
,
"Referrer-Policy"
,
"Strict-Transport-Security"
,
"Strict-Transport-Security"
,
"Feature-Policy"
,
"Feature-Policy"
,
...
@@ -263,6 +265,15 @@ def checkHttpStatus(
...
@@ -263,6 +265,15 @@ def checkHttpStatus(
if
header_value
is
not
None
:
if
header_value
is
not
None
:
header_dict
[
header_key
]
=
header_value
header_dict
[
header_key
]
=
header_value
# Strip nonces from Content-Security-Policy
header_key
=
"Content-Security-Policy"
content_security_policy
=
response
.
headers
.
get
(
header_key
,
None
)
if
content_security_policy
is
not
None
:
header_dict
[
header_key
]
=
SUB_CSP_NONCE
(
repl
=
SUB_CSP_NONCE_REPL
,
string
=
content_security_policy
,
)
# Store key only, because of non stability
# Store key only, because of non stability
# 'Etag', 'Last-Modified', 'Set-Cookie', 'Date', 'Age'
# 'Etag', 'Last-Modified', 'Set-Cookie', 'Date', 'Age'
key_only_header_list
=
[
key_only_header_list
=
[
...
...
tests/test_http.py
View file @
91cdbde6
...
@@ -1001,6 +1001,36 @@ class SurykatkaHttpTestCase(unittest.TestCase):
...
@@ -1001,6 +1001,36 @@ class SurykatkaHttpTestCase(unittest.TestCase):
result_dict
[
header
]
=
header
+
" bar"
result_dict
[
header
]
=
header
+
" bar"
return
result_dict
return
result_dict
@
httpretty
.
activate
def
test_checkContentSecurityPolicyNonceStripper
(
self
):
header_key
=
"Content-Security-Policy"
httpretty
.
register_uri
(
httpretty
.
GET
,
"http://127.0.0.1/foo?bar=1"
,
status
=
418
,
adding_headers
=
{
header_key
:
(
"script-src 'self' 'nonce-dHkctJcn0BOAQxGH1YNXbg==' 'strict-dynamic';"
"style-src 'self' 'nonce-dHkctJcn0BOAQxGH1YNXbg==';"
"object-src 'none';"
),
},
)
checkHttpStatus
(
self
.
db
,
logStatus
(
self
.
db
,
"foo"
),
"http://example.org/foo?bar=1"
,
"127.0.0.1"
,
1
,
"http://example.org/contact"
,
)
assert
self
.
db
.
HttpCodeChange
.
select
().
count
()
==
1
assert
self
.
db
.
HttpCodeChange
.
get
().
http_header_dict
[
header_key
]
==
(
"script-src 'self' 'nonce-' 'strict-dynamic';"
"style-src 'self' 'nonce-';"
"object-src 'none';"
)
@
httpretty
.
activate
@
httpretty
.
activate
def
test_checkHttpStatus_whitelistHttpHeader
(
self
):
def
test_checkHttpStatus_whitelistHttpHeader
(
self
):
ip
=
"127.0.0.1"
ip
=
"127.0.0.1"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment