Skip to content

Dev#19

Merged
qwasd7680 merged 5 commits intomainfrom
dev
Nov 4, 2025
Merged

Dev#19
qwasd7680 merged 5 commits intomainfrom
dev

Conversation

@qwasd7680
Copy link
Copy Markdown
Owner

No description provided.

main.py Outdated
Comment on lines 212 to 218
客户端收到通知后,通过此路由下载文件。
"""
zip_file_name = f"{file_name}.zip"
file_path = FILE_PATH / zip_file_name # 使用统一的 Path 对象和变量
file_path = FILE_PATH / zip_file_name

if file_path.exists():
return responses.FileResponse(file_path, filename=zip_file_name, media_type="application/zip")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security Issue: Path Traversal

The function constructs a file path from user input without sanitizing, which could lead to a path traversal attack. An attacker could potentially access arbitrary files on the server.

Recommendation:
Sanitize file_name to ensure it does not contain path traversal characters like .. or /. Use secure methods to combine paths and validate inputs to prevent unauthorized file access.

Comment on lines 336 to 337
if __name__ == '__main__':
# 确保 uvicorn 运行时引用的是当前文件的 app 实例
uvicorn.run("main:app", host="0.0.0.0", log_level="info")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security Concern: Server Exposure

The application is configured to listen on all network interfaces (0.0.0.0), which could expose the server to unwanted external access if not properly secured.

Recommendation:
Consider binding the server to 127.0.0.1 if it is only meant for local access. If remote access is required, ensure that appropriate security measures such as firewalls and authentication are in place to protect the server.

Comment on lines 467 to 472
return cached_result

client = get_jm_client()
pages: jmcomic.JmCategoryPage = client.categories_filter(
page=1,
time=jmcomic.JmMagicConstants.TIME_ALL,
category=jmcomic.JmMagicConstants.CATEGORY_ALL,
order_by=jmcomic.JmMagicConstants.ORDER_BY_LATEST,
)
if searchTime == "month":
pages: jmcomic.JmCategoryPage = client.month_ranking(1)
elif searchTime == "week":
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lack of Error Handling and Validation

The function rank retrieves ranking data based on the searchTime parameter but does not handle potential errors from the client method calls nor validates the searchTime input. This could lead to unhandled exceptions or incorrect data retrieval if searchTime is not one of the expected values ('month', 'week', 'day').

Recommendation:

  1. Validate searchTime to ensure it matches one of the expected values before proceeding with the method calls.
  2. Implement error handling for the client method calls to manage exceptions and provide appropriate feedback or fallbacks.

Added path sanitization and validation to prevent path traversal attacks. Ensured the resolved file path remains within the allowed directory.
Comment on lines 338 to 347
status_code=400,
content={"status": "error", "msg": "Invalid file name."}
)

zip_file_name = f"{safe_file_name}.zip"
file_path = FILE_PATH / zip_file_name

# 确保解析后的路径仍在 FILE_PATH 目录内(双重防护)
try:
resolved_path = file_path.resolve()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential Security Issue: Inconsistent Path Validation

The code checks if the sanitized file_name is different from the original, which is good for preventing path traversal. However, the check if not safe_file_name or safe_file_name != file_name might be redundant since safe_file_name is derived by stripping potentially harmful characters from file_name. If safe_file_name is empty, it should be handled as an invalid input earlier in the logic.

Recommendation:
Refactor the condition to first check if safe_file_name is empty and handle it as an invalid input before proceeding with other operations. This makes the intent clearer and the code more maintainable.

Comment on lines 361 to 372
content={"status": "error", "msg": "Invalid file path."}
)

return JSONResponse(
return JSONResponse(
status_code=404,
content={"status": "error", "msg": "File not found or has expired."}
)


# --- 其他原有路由 (保持不变) ---

@app.get("/v1/{timestamp}")
async def read_root(timestamp: float):
"""
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error Handling Improvement: Specific Error Messages

The error handling in lines [361-372] could be improved by providing more specific error messages based on the type of error encountered (e.g., file not found, access denied, etc.). Currently, the generic message 'Invalid file path.' does not give enough information about what went wrong, which can hinder troubleshooting and user experience.

Recommendation:
Enhance the error handling by catching specific exceptions and returning more descriptive error messages. This will help in diagnosing issues more effectively and improve the clarity of communication to the end-user.

Comment on lines 367 to 372
)


# --- 其他原有路由 (保持不变) ---

@app.get("/v1/{timestamp}")
async def read_root(timestamp: float):
"""
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improvement in Error Messaging

The error handling in lines [367-372] could be enhanced by providing more specific error messages based on the type of error encountered (e.g., file not found, access denied, etc.). Currently, the generic message 'File not found or has expired.' does not give enough information about what went wrong, which can hinder troubleshooting and user experience.

Recommendation:
Enhance the error handling by catching specific exceptions and returning more descriptive error messages. This will help in diagnosing issues more effectively and improve the clarity of communication to the end-user.

@qwasd7680 qwasd7680 merged commit c0293ab into main Nov 4, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant