|
Flask 防止 sql injection 注入
以下是如何在 Flask 應用程式中使用我提到的一些安全措施的範例:
使用 HTTPS:要在 Flask 應用程式中使用 HTTPS,可以使用 NGINX 或 Apache 等反向代理伺服器來處理 SSL/TLS 加密。以下是 NGINX 的範例配置:
- server {
- listen 443 ssl;
- server_name example.com;
- ssl_certificate /path/to/ssl/certificate.pem;
- ssl_certificate_key /path/to/ssl/private_key.pem;
- location / {
- proxy_pass http://localhost:5000;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto $scheme;
- }
- }
複製代碼
2. 驗證輸入:若要在 Flask 應用程式中驗證輸入,您可以使用 WTForms 函式庫建立具有定義欄位和驗證規則的表單物件。以下是一個範例表單:
- from flask_wtf import FlaskForm
- from wtforms import StringField, PasswordField
- from wtforms.validators import InputRequired, Length
- class LoginForm(FlaskForm):
- username = StringField('Username', validators=[InputRequired(), Length(min=3, max=30)])
- password = PasswordField('Password', validators=[InputRequired(), Length(min=8, max=30)])
複製代碼
要使用表單,您可以在範本中呈現它並在視圖函數中處理提交:
- @app.route('/login', methods=['GET', 'POST'])
- def login():
- form = LoginForm()
- if form.validate_on_submit():
- # Form is valid, process the submission
- ...
- return render_template('login.html', form=form)pyo
複製代碼
3. 使用 CSRF 保護:若要在 Flask 應用中使用 CSRF 保護,可以使用 Flask-WTF 擴充及其 csrf_protect 裝飾器。以下是一個使用 CSRF 保護的範例視圖函數:
- from flask_wtf.csrf import CSRFProtect
- csrf = CSRFProtect(app)
- @app.route('/submit', methods=['POST'])
- @csrf.exempt
- def submit():
- # Process the form submission
- ...
複製代碼
4. 使用已淨化的 HTML 輸出:若要淨化 Flask 應用程式中的 HTML 輸出,您可以使用 Bleach 函式庫。以下是如何淨化使用者提交的 HTML 的範例:
您可以使用 Bleach 函式庫來清理 Flask 應用程式中的 HTML 輸出:
4.1 首先,使用 pip 安裝 Bleach 函式庫:
4.2 接下來,定義一個函數來淨化 HTML。函數應該將 HTML 作為輸入,並傳回淨化後的 HTML 作為輸出。以下是一個範例函數,它只允許使用有限的一組標籤和屬性:
- import bleach
- def sanitize_html(html):
- # Allow only a limited set of tags and attributes
- allowed_tags = ['a', 'b', 'i', 'em', 'strong']
- allowed_attributes = {'a': ['href']}
- return bleach.clean(html, tags=allowed_tags, attributes=allowed_attributes)
複製代碼
4.3 若要使用 sanitize_html 函數,您可以將使用者提交的 HTML 傳入該函數,然後再渲染至範本中。以下是一個執行此操作的範例視圖函數:
- @app.route('/submit', methods=['POST'])
- def submit():
- # Get the user-submitted HTML
- html = request.form['html']
-
- # Sanitize the HTML
- sanitized_html = sanitize_html(html)
-
- # Render the sanitized HTML in a template
- return render_template('view.html', html=sanitized_html)
複製代碼
5. 使用 Web 應用程式防火牆:若要在 Flask 應用程式中使用 Web 應用程式防火牆 (WAF),您可以使用啟用了 WAF 模組的反向代理伺服器,例如 NGINX 或 Apache。以下是啟用了 modsecurity WAF 模組的 NGINX 的範例配置:
- server {
- listen 80;
- server_name example.com;
- location / {
- modsecurity on;
- modsecurity_rules_file /path/to/modsecurity/rules.conf;
- proxy_pass http://localhost:5000;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto $scheme;
- }
- }
複製代碼
6. 保持應用程式及其相依性的更新:為了保持應用程式及其相依性的更新,您可以使用 pip 之類的工具來管理 Python 軟體包。若要更新專案中的所有軟體包,您可以執行以下命令:
- pip install -U -r requirements.txt
複製代碼
這會將 requirements.txt 檔案中列出的所有套件更新為最新版本。
https://python.plainenglish.io/the-best-security-practice-of-your-flask-application-api-691a21e37c27
|
|