-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for calculating CSP hashes of inline scripts
- Loading branch information
1 parent
d9a8d9c
commit e32c56a
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ cryptography==3.0 | |
requests[security]==2.21.0 | ||
beautifulsoup4==4.8.2 | ||
waitress==1.4.3 | ||
flask-talisman==0.7.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import contextlib | ||
|
||
import pytest | ||
import flask_talisman | ||
from selenium.common.exceptions import NoSuchElementException | ||
|
||
import dash | ||
import dash_core_components as dcc | ||
import dash_html_components as html | ||
from dash.dependencies import Input, Output | ||
|
||
|
||
@contextlib.contextmanager | ||
def does_not_raise(): | ||
yield | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"add_hashes, hash_algorithm, expectation", | ||
[ | ||
(False, None, pytest.raises(NoSuchElementException)), | ||
(True, "sha256", does_not_raise()), | ||
(True, "sha384", does_not_raise()), | ||
(True, "sha512", does_not_raise()), | ||
(True, "sha999", pytest.raises(ValueError)), | ||
], | ||
) | ||
def test_csp_hashes_inline_scripts(dash_duo, add_hashes, hash_algorithm, expectation): | ||
app = dash.Dash(__name__) | ||
|
||
app.layout = html.Div( | ||
[dcc.Input(id="input_element", type="text"), html.Div(id="output_element")] | ||
) | ||
|
||
app.clientside_callback( | ||
""" | ||
function(input) { | ||
return input; | ||
} | ||
""", | ||
Output("output_element", "children"), | ||
[Input("input_element", "value")], | ||
) | ||
|
||
with expectation: | ||
csp = { | ||
"default-src": "'self'", | ||
"script-src": ["'self'"] | ||
+ (app.csp_hashes(hash_algorithm) if add_hashes else []), | ||
} | ||
|
||
flask_talisman.Talisman( | ||
app.server, content_security_policy=csp, force_https=False | ||
) | ||
|
||
dash_duo.start_server(app) | ||
|
||
dash_duo.find_element("#input_element").send_keys("xyz") | ||
assert dash_duo.wait_for_element("#output_element").text == "xyz" |