Skip to content

Commit b0a67fe

Browse files
committed
add flask-wtf pkg for csrf protection
1 parent cd843da commit b0a67fe

File tree

6 files changed

+12
-1
lines changed

6 files changed

+12
-1
lines changed

.github/workflows/deploy.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ jobs:
3434
- name: Deploy
3535
uses: DefangLabs/[email protected]
3636
with:
37-
config-env-vars: OPENAI_API_KEY
37+
config-env-vars: OPENAI_API_KEY SECRET_KEY
3838
mode: production
3939
provider: aws
4040

4141
env:
4242
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
43+
SECRET_KEY: ${{ secrets.SECRET_KEY }}

app/app.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
from flask import Flask, request, jsonify, render_template, Response, stream_with_context
2+
from flask_wtf.csrf import CSRFProtect
23
from rag_system import rag_system
34
import subprocess
45
app = Flask(__name__, static_folder='templates/images')
56

7+
import os
8+
9+
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
10+
csrf = CSRFProtect(app)
11+
612
@app.route('/', methods=['GET', 'POST'])
713
def index():
814
return render_template('index.html')

app/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Flask==2.0.1
2+
Flask-WTF==1.2.2
23
Werkzeug==2.0.3
34
scikit-learn==0.24.2
45
numpy==1.22.0

app/templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ <h2>Ask Defang</h2>
232232
method: 'POST',
233233
headers: {
234234
'Content-Type': 'application/json',
235+
'X-CSRFToken': '{{ csrf_token() }}'
235236
},
236237
body: JSON.stringify({ query: query }),
237238
})

compose.dev.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ services:
1010
mode: ingress
1111
environment:
1212
FLASK_APP: app.py
13+
SECRET_KEY: supersecret
1314
OPENAI_API_KEY: ${OPENAI_API_KEY} # Set your OpenAI API key here or in the .env file
1415
command: flask run --host=0.0.0.0
1516
deploy:

compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ services:
1313
mode: ingress
1414
environment:
1515
FLASK_APP: app.py
16+
SECRET_KEY:
1617
OPENAI_API_KEY: ${OPENAI_API_KEY} # Set your OpenAI API key here or in the .env file
1718
command: uwsgi --http 0.0.0.0:5000 --wsgi-file app.py --callable app --processes 4 --threads 2
1819
deploy:

0 commit comments

Comments
 (0)