Skip to content

Commit 670e2ce

Browse files
fix: add input validation for offset/limit to prevent query injection
1 parent 1cee2cd commit 670e2ce

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

code/backend/api/chat_history.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ async def list_conversations():
5555
return jsonify({"error": "Chat history is not available"}), 400
5656

5757
try:
58-
offset = request.args.get("offset", 0)
58+
try:
59+
offset = int(request.args.get("offset", 0))
60+
except (ValueError, TypeError):
61+
return jsonify({"error": "offset must be a valid integer"}), 400
62+
if offset < 0:
63+
return jsonify({"error": "offset must be non-negative"}), 400
5964
authenticated_user = get_authenticated_user_details(
6065
request_headers=request.headers
6166
)

code/backend/batch/utilities/chat_history/cosmosdb.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,12 @@ async def delete_messages(self, conversation_id, user_id):
122122

123123
async def get_conversations(self, user_id, limit, sort_order="DESC", offset=0):
124124
parameters = [{"name": "@userId", "value": user_id}]
125+
if sort_order not in ("ASC", "DESC"):
126+
sort_order = "DESC"
125127
query = f"SELECT * FROM c where c.userId = @userId and c.type='conversation' order by c.updatedAt {sort_order}"
126128
if limit is not None:
129+
offset = int(offset)
130+
limit = int(limit)
127131
query += f" offset {offset} limit {limit}"
128132

129133
conversations = []

0 commit comments

Comments
 (0)