-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bc28b4
commit 7eab0fa
Showing
2 changed files
with
49 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Content Security Policy | ||
|
||
Content Security Policy (CSP) can significantly reduce the risk and impact of cross-site scripting attacks in modern browsers. A CSP has been a W3C recomendation since 2016 and is now the industry standard in securing web applications. | ||
|
||
More information on CSP: [w3c documentation](http://www.w3.org/TR/CSP2/) | ||
|
||
## To add a Content Security Policy header to your Flask application. | ||
|
||
### Installation | ||
Install the extension with using pip, or easy_install. [Pypi Link](https://pypi.python.org/pypi/flask-csp) | ||
|
||
```bash | ||
$ pip install flask-csp | ||
``` | ||
|
||
## Usage | ||
Add the csp_header(...) decorator after the app.route(...) decorator to create a csp header on each route. The decorator can either be passed no value (Add default policies) or custom values by a dict (Add custom policies). For more information on the default policies see "Change Default Policies" below. | ||
|
||
### Add default header | ||
```python | ||
@app.route('/', methods=['POST', 'GET']) | ||
@app.route('/index.html', methods=['GET']) | ||
@csp_header({ | ||
"default-src": "'self'", | ||
"script-src": "'self'", | ||
"img-src": "http: https: data:", | ||
"object-src": "'self'", | ||
"style-src": "'self'", | ||
"media-src": "'self'", | ||
"child-src": "'self'", | ||
"connect-src": "'self'", | ||
"base-uri": "", | ||
"report-uri": "/csp_report", | ||
"frame-ancestors": 'none' | ||
}) | ||
def index(): | ||
#index implementation | ||
``` | ||
|
||
### Create an app route for CSP reports | ||
|
||
```python | ||
@app.route('/csp_report',methods=['POST']) | ||
def csp_report(): | ||
with open('csp_reports', "a") as fh: | ||
fh.write(request.data.decode()+"\n") | ||
return 'done' | ||
``` |
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