Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit 127692d

Browse files
Liangyx2XuehaoSunpre-commit-ci[bot]
authored
[NeuralChat] Refine path in AskDoc server (#1593)
* Update askdoc.yaml Signed-off-by: Liangyx2 <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: Liangyx2 <[email protected]> Co-authored-by: Sun, Xuehao <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 8ab25cf commit 127692d

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

intel_extension_for_transformers/neural_chat/server/restful/retrieval_api.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ def handle_retrieval_request(self, request: RetrievalRequest) -> RetrievalRespon
234234
RETRIEVAL_FILE_PATH = os.getenv("RETRIEVAL_FILE_PATH", default="./retrieval_docs")+'/'
235235
EXCEPT_PATTERNS = ["/xuhui_doc", "default/persist_dir"]
236236

237+
def safe_join(base_path, *paths):
238+
# Prevent path traversal by ensuring the final path is within the base path
239+
base_path = os.path.abspath(base_path)
240+
final_path = os.path.abspath(os.path.join(base_path, *paths))
241+
if not final_path.startswith(base_path):
242+
raise ValueError("Attempted Path Traversal Detected")
243+
return final_path
237244

238245
@router.post("/v1/askdoc/upload_link")
239246
async def retrieval_upload_link(request: Request):
@@ -316,7 +323,7 @@ async def retrieval_add_files(request: Request,
316323
path_prefix = get_path_prefix(kb_id, user_id)
317324
upload_path = path_prefix + '/upload_dir'
318325
persist_path = path_prefix + '/persist_dir'
319-
save_path = Path(upload_path) / file_path
326+
save_path = safe_join(Path(upload_path), file_path)
320327
save_path.parent.mkdir(parents=True, exist_ok=True)
321328

322329
# save file content to local disk
@@ -618,7 +625,7 @@ async def delete_single_file(request: Request):
618625
logger.info(f"[askdoc - delete_file] successfully delete kb {knowledge_base_id}")
619626
return {"status": True}
620627

621-
delete_path = Path(path_prefix) / "upload_dir" / del_path
628+
delete_path = safe_join(Path(path_prefix) / "upload_dir", del_path)
622629
logger.info(f'[askdoc - delete_file] delete_path: {delete_path}')
623630

624631
# partially delete files/folders from the kb

0 commit comments

Comments
 (0)