CSP (Content Security Policy) is a good tool for enhancing security of web applications. Mozilla Observatory is a good place to check for CSP implementation across web applications. How easy it is to enforce strict CSP settings depends, to a large degree, on the frameworks used in the stack.
Dash out of the box is quite CSP settings friendly, e.g. you can do pip install dash flask-talisman (alternatively set the CSP headers directly instead of using flask-talisman) and then run e.g.
import dash
import dash_html_components as html
from flask_talisman import Talisman
app = dash.Dash(__name__)
CSP = {
"default-src": "'self'",
"script-src": [
"'self'",
# Due to https://github.com/plotly/dash/issues/630:
"'sha256-jZlsGVOhUAIcH+4PVs7QuGZkthRMgvT2n0ilH6/zTM0='",
]
}
Talisman(app.server, content_security_policy=CSP, force_https=False)
app.layout = html.Div(children=["Hello Dash!"])
if __name__ == "__main__":
app.run_server()
This will work with no CSP errors on localhost in the browser console - despite quite strict CSP settings.
If however import dash_core_components as dcc is added, you will need to either add
"style-src": ["'self'", "'unsafe-inline'"],
or
"style-src": [
"'sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='",
"'sha256-cJ5ObsneXZnEJ3xXl8hpmaM9RbOwaU6UIPslFf0/OOE='",
"'sha256-joa3+JBk4cwm5iLfizxl9fClObT5tKuZMFQ9B2FZlMg='",
"'sha256-Jtp4i67c7nADbaBCKBYgqI86LQOVcoZ/khTKsuEkugc='",
"'sha256-MoQFjm6Ko0VvKSJqNGTl4e5H3guejX+CG/LxytSdnYg='",
"'sha256-kkTbqhXgCW2olAc5oz4XYZtzEwh5ojnaxt9iOTAZcZo='",
],
to the CSP dictionary in order to allow the CSS that dcc is adding inline to the app.
The first is not optimal as you then again open for inline CSS "XSS" (as when not using CSP at all). The second is not optimal either as the hashes will need to update each time a new version of dcc changes its inline style content.
If dcc could output CSS as a separate file during build, instead of inline style, we could with Dash also enforce strict CSS CSP. I.e. use mini-css-extract-plugin instead of style-load in webpack.config.js during (production?) build of dcc.
CSP (Content Security Policy) is a good tool for enhancing security of web applications. Mozilla Observatory is a good place to check for CSP implementation across web applications. How easy it is to enforce strict CSP settings depends, to a large degree, on the frameworks used in the stack.
Dash out of the box is quite CSP settings friendly, e.g. you can do
pip install dash flask-talisman(alternatively set the CSP headers directly instead of using flask-talisman) and then run e.g.This will work with no CSP errors on localhost in the browser console - despite quite strict CSP settings.
If however
import dash_core_components as dccis added, you will need to either addor
to the
CSPdictionary in order to allow the CSS thatdccis adding inline to the app.The first is not optimal as you then again open for inline CSS "XSS" (as when not using CSP at all). The second is not optimal either as the hashes will need to update each time a new version of
dccchanges its inline style content.If
dcccould output CSS as a separate file during build, instead of inline style, we could with Dash also enforce strict CSS CSP. I.e. usemini-css-extract-plugininstead ofstyle-loadinwebpack.config.jsduring (production?) build ofdcc.