Skip to content

Commit 062efe0

Browse files
authored
Merge pull request #41 from DefangLabs/linda-ask-token
Added Ask-Token header to bypass CSRF and PoW when needed
2 parents 52ba787 + 0b37406 commit 062efe0

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

.github/workflows/deploy.yaml

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

4141
env:
42+
ASK_TOKEN: ${{ secrets.ASK_TOKEN }}
4243
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
4344
SECRET_KEY: ${{ secrets.SECRET_KEY }}
4445
SEGMENT_WRITE_KEY: ${{ secrets.SEGMENT_WRITE_KEY }}

app/app.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,7 @@ def validate_pow(nonce, data, difficulty):
2626
first_uint32 = int.from_bytes(calculated_hash[:4], byteorder='big')
2727
return first_uint32 <= difficulty
2828

29-
30-
@app.route('/', methods=['GET', 'POST'])
31-
def index():
32-
return render_template('index.html', debug=os.getenv('DEBUG'))
33-
34-
@app.route('/ask', methods=['POST'])
35-
def ask():
36-
if not validate_pow(request.headers.get('X-Nonce'), request.get_data(), 0x50000):
37-
return jsonify({"error": "Invalid proof of work"}), 400
38-
29+
def handle_ask_request(request, session):
3930
data = request.get_json()
4031
query = data.get('query')
4132

@@ -70,6 +61,30 @@ def generate():
7061

7162
return Response(stream_with_context(generate()), content_type='text/markdown')
7263

64+
@app.route('/', methods=['GET', 'POST'])
65+
def index():
66+
return render_template('index.html', debug=os.getenv('DEBUG'))
67+
68+
@app.route('/ask', methods=['POST'])
69+
def ask():
70+
if not validate_pow(request.headers.get('X-Nonce'), request.get_data(), 0x50000):
71+
return jsonify({"error": "Invalid Proof of Work"}), 400
72+
73+
response = handle_ask_request(request, session)
74+
return response
75+
76+
# /v1/ask allows bypassing of CSRF and PoW for clients with a valid Ask Token
77+
@app.route('/v1/ask', methods=['POST'])
78+
@csrf.exempt
79+
def v1_ask():
80+
auth_header = request.headers.get('Authorization')
81+
ask_token = auth_header.split("Bearer ")[1] if auth_header and auth_header.startswith("Bearer ") else None
82+
if ask_token and ask_token == os.getenv('ASK_TOKEN'):
83+
response = handle_ask_request(request, session)
84+
return response
85+
else:
86+
jsonify({"error": "Invalid or missing Ask Token"}), 401
87+
7388
@app.route('/trigger-rebuild', methods=['POST'])
7489
@csrf.exempt
7590
def trigger_rebuild():

compose.dev.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ services:
99
protocol: tcp
1010
mode: ingress
1111
environment:
12+
ASK_TOKEN: asktoken
1213
FLASK_APP: app.py
1314
SECRET_KEY: supersecret
1415
SEGMENT_WRITE_KEY: ${SEGMENT_WRITE_KEY} # Set your Segment write key here or in the .env file

compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ services:
1212
protocol: tcp
1313
mode: ingress
1414
environment:
15+
ASK_TOKEN:
1516
FLASK_APP: app.py
1617
DEBUG: 0
1718
SECRET_KEY:

0 commit comments

Comments
 (0)