-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
90 lines (74 loc) · 2.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from flask import Flask
from flask import redirect
from flask import render_template
from flask import request
from flask import jsonify
import requests
from flask_wtf import CSRFProtect
from flask_csp.csp import csp_header
import logging
import userManagement as dbHandler
# Code snippet for logging a message
# app.logger.critical("message")
app_log = logging.getLogger(__name__)
logging.basicConfig(
filename="security_log.log",
encoding="utf-8",
level=logging.DEBUG,
format="%(asctime)s %(message)s",
)
# Generate a unique basic 16 key: https://acte.ltd/utils/randomkeygen
app = Flask(__name__)
app.secret_key = b"_53oi3uriq9pifpff;apl"
csrf = CSRFProtect(app)
# Redirect index.html to domain root for consistent UX
@app.route("/index", methods=["GET"])
@app.route("/index.htm", methods=["GET"])
@app.route("/index.asp", methods=["GET"])
@app.route("/index.php", methods=["GET"])
@app.route("/index.html", methods=["GET"])
def root():
return redirect("/", 302)
@app.route("/", methods=["POST", "GET"])
@csp_header(
{
# Server Side CSP is consistent with meta CSP in layout.html
"base-uri": "'self'",
"default-src": "'self'",
"style-src": "'self'",
"script-src": "'self'",
"img-src": "'self' data:",
"media-src": "'self'",
"font-src": "'self'",
"object-src": "'self'",
"child-src": "'self'",
"connect-src": "'self'",
"worker-src": "'self'",
"report-uri": "/csp_report",
"frame-ancestors": "'none'",
"form-action": "'self'",
"frame-src": "'none'",
}
)
def index():
return render_template("/index.html")
@app.route("/privacy.html", methods=["GET"])
def privacy():
return render_template("/privacy.html")
# example CSRF protected form
@app.route("/form.html", methods=["POST", "GET"])
def form():
if request.method == "POST":
email = request.form["email"]
text = request.form["text"]
return render_template("/form.html")
else:
return render_template("/form.html")
# Endpoint for logging CSP violations
@app.route("/csp_report", methods=["POST"])
@csrf.exempt
def csp_report():
app.logger.critical(request.data.decode())
return "done"
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=5000)