网友修改版
This commit is contained in:
1 parent
290332c70c
commit
fbccc5d909
14 files changed
+9042
-73
No files matched your search
@@ -9,6 +9,7 @@ from logging.handlers import RotatingFileHandler
|
||||
from pathlib import Path
|
||||
import configparser
|
||||
import sys
|
||||
from security_middleware import apply_security_headers
|
||||
|
||||
# 基准目录:编译后取可执行文件所在目录,开发时取脚本所在目录
|
||||
# 解决 Nuitka onefile 解压到 /tmp 后相对路径失效的问题
|
||||
@@ -33,6 +34,7 @@ parser.add_argument('--config', type=str, default='config.ini', help='Configurat
|
||||
args = parser.parse_args()
|
||||
|
||||
app = Flask(__name__)
|
||||
apply_security_headers(app)
|
||||
CORS(app)
|
||||
|
||||
# ==================== 配置文件加载 ====================
|
||||
|
||||
@@ -20,7 +20,8 @@
|
||||
<button id="delete-note">删除当前节点</button>
|
||||
</div>
|
||||
</div>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/markdown-it/12.0.0/markdown-it.min.js"></script>
|
||||
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/markdown-it/12.0.0/markdown-it.min.js"></script>-->
|
||||
<script src="js/markdown-it-12_0_0.js"></script>
|
||||
<script src="js/validation.js"></script>
|
||||
<script src="js/dnfs.js"></script>
|
||||
</div>
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,42 @@
|
||||
"""
|
||||
Security headers middleware for Flask applications.
|
||||
Usage:
|
||||
from security_middleware import apply_security_headers
|
||||
apply_security_headers(app)
|
||||
"""
|
||||
import secrets
|
||||
|
||||
def apply_security_headers(app, csp_policy=None):
|
||||
"""Apply security response headers to a Flask app."""
|
||||
|
||||
@app.after_request
|
||||
def set_security_headers(response):
|
||||
# Prevent clickjacking
|
||||
response.headers['X-Frame-Options'] = 'DENY'
|
||||
# XSS protection (legacy browsers)
|
||||
response.headers['X-XSS-Protection'] = '1; mode=block'
|
||||
# Prevent MIME type sniffing
|
||||
response.headers['X-Content-Type-Options'] = 'nosniff'
|
||||
# Referrer policy
|
||||
response.headers['Referrer-Policy'] = 'strict-origin-when-cross-origin'
|
||||
# Permissions policy (disable camera, mic, geolocation)
|
||||
response.headers['Permissions-Policy'] = 'camera=(), microphone=(), geolocation=()'
|
||||
|
||||
# Content Security Policy
|
||||
if csp_policy:
|
||||
response.headers['Content-Security-Policy'] = csp_policy
|
||||
else:
|
||||
response.headers['Content-Security-Policy'] = (
|
||||
"default-src 'self'; "
|
||||
"script-src 'self' 'unsafe-inline'; "
|
||||
"style-src 'self' 'unsafe-inline'; "
|
||||
"img-src 'self' data:; "
|
||||
"font-src 'self'; "
|
||||
"connect-src 'self'; "
|
||||
"frame-ancestors 'none'"
|
||||
)
|
||||
|
||||
# Remove server header info
|
||||
response.headers.pop('Server', None)
|
||||
|
||||
return response
|
||||
Reference in new issue
Block a user