Skip to content

Commit

Permalink
fix: 处理安全问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Aug 1, 2024
1 parent a8fefc6 commit cf01039
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion xiaomusic/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ async def file_iterator(file_path, start, end):
@app.get("/music/{file_path:path}")
async def music_file(request: Request, file_path: str):
absolute_path = os.path.abspath(config.music_path)
absolute_file_path = os.path.join(absolute_path, file_path)
absolute_file_path = os.path.normpath(os.path.join(absolute_path, file_path))
if not absolute_file_path.startswith(absolute_path):
raise HTTPException(status_code=404, detail="File not found")
if not os.path.exists(absolute_file_path):
raise HTTPException(status_code=404, detail="File not found")

Expand Down
4 changes: 3 additions & 1 deletion xiaomusic/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@ async def file_iterator(file_path, start, end):
@app.get("/music/{file_path:path}")
async def music_file(request: Request, file_path: str):
absolute_path = os.path.abspath(config.music_path)
absolute_file_path = os.path.join(absolute_path, file_path)
absolute_file_path = os.path.normpath(os.path.join(absolute_path, file_path))
if not absolute_file_path.startswith(absolute_path):
raise HTTPException(status_code=404, detail="File not found")
if not os.path.exists(absolute_file_path):
raise HTTPException(status_code=404, detail="File not found")

Expand Down

0 comments on commit cf01039

Please sign in to comment.