-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(openapi): add GET file endpoint and update file route to support multiple methods #6874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,10 @@ def __init__( | |
| "/v1/chat": ("POST", self.chat_send), | ||
| "/v1/chat/sessions": ("GET", self.get_chat_sessions), | ||
| "/v1/configs": ("GET", self.get_chat_configs), | ||
| "/v1/file": ("POST", self.upload_file), | ||
| "/v1/file": [ | ||
| ("POST", self.upload_file), | ||
| ("GET", self.get_file), | ||
| ], | ||
| "/v1/im/message": ("POST", self.send_message), | ||
| "/v1/im/bots": ("GET", self.get_bots), | ||
| } | ||
|
|
@@ -455,10 +458,7 @@ async def _handle_chat_ws_send(self, post_data: dict) -> None: | |
| if msg_type == "end": | ||
| break | ||
| if (streaming and msg_type == "complete") or not streaming: | ||
| if chain_type in ( | ||
| "tool_call", | ||
| "tool_call_result", | ||
| ): | ||
| if chain_type in ("tool_call", "tool_call_result"): | ||
| continue | ||
| try: | ||
| refs = self.chat_route._extract_web_search_refs( | ||
|
|
@@ -540,6 +540,9 @@ async def chat_ws(self) -> None: | |
| async def upload_file(self): | ||
| return await self.chat_route.post_file() | ||
|
|
||
| async def get_file(self): | ||
| return await self.chat_route.get_attachment() | ||
|
Comment on lines
+543
to
+544
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method This is a critical security issue. To fix this, you should validate the path before serving the file. It's best to fix this in Here is a suggestion for a secure implementation of this method: async def get_file(self):
attachment_id = request.args.get("attachment_id")
if not attachment_id:
return Response().error("Missing key: attachment_id").__dict__
try:
attachment = await self.db.get_attachment_by_id(attachment_id)
if not attachment:
return Response().error("Attachment not found").__dict__
file_path = attachment.path
real_file_path = os.path.realpath(file_path)
# Security check for path traversal
attachments_dir = os.path.realpath(self.chat_route.attachments_dir)
legacy_img_dir = os.path.realpath(self.chat_route.legacy_img_dir)
is_safe = real_file_path.startswith(attachments_dir) or real_file_path.startswith(legacy_img_dir)
if not is_safe:
return Response().error("Invalid file path").__dict__
return await send_file(real_file_path, mimetype=attachment.mime_type)
except (FileNotFoundError, OSError):
return Response().error("File access error").__dict__Please also consider fixing the |
||
|
|
||
| async def get_chat_sessions(self): | ||
| username, username_err = self._resolve_open_username( | ||
| request.args.get("username") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
self.chat_route.post_file()method called byupload_filehas a path traversal vulnerability. It uses the user-provided filename directly when saving the uploaded file. An attacker could provide a malicious filename like../../../../etc/passwdto write files outside of the intended directory.This is a critical security issue.
To fix this, you should sanitize the filename using
werkzeug.utils.secure_filenamebefore saving it. The fix should be applied in thepost_filemethod inastrbot/dashboard/routes/chat.py.Example fix in
chat.py: