Files
kami_apple_exchage/DRAFT_SECURITY_REPORT.md
danial 5c486e34d3 docs(项目): 添加项目文档并进行代码调整
- 新增 CODEBUDDY.md、GEMINI.md、GEMINI_CN.md 等项目文档
- 更新 Dockerfile 和其他配置文件
- 优化部分代码结构,如 orders.py、tasks.py 等
- 新增 .dockerignore 文件
2025-09-12 19:38:24 +08:00

100 lines
8.7 KiB
Markdown

## Vulnerability Report
### 1. Container Runs as Root
* **Vulnerability:** The Docker container runs as the `root` user.
* **Severity:** High
* **Location:** `backend/Dockerfile`
* **Line Content:** Not applicable (absence of `USER` instruction).
* **Description:** Running a container as `root` is a security risk. If an attacker gains control of the application, they will have root privileges inside the container, which can be used to escalate privileges further.
* **Recommendation:** Create a non-root user and switch to it using the `USER` instruction in the Dockerfile.
### 2. Hardcoded Database Credentials
* **Vulnerability:** The `DATABASE_URL` in the `.env.production` file contains a hardcoded password.
* **Severity:** Critical
* **Location:** `backend/.env.production`
* **Line Content:** `DATABASE_URL="postgresql+asyncpg://postgres:password@db:5432/apple_exchange"`
* **Description:** The production database credentials are hardcoded in the `.env.production` file, which is then copied into the Docker image. This makes it easy for an attacker to gain access to the database if they can read the contents of the image.
* **Recommendation:** Use a secrets management solution to inject the database password into the container at runtime. The `DATABASE_URL` should be constructed using an environment variable for the password, similar to how `JWT_SECRET_KEY` and `ENCRYPTION_KEY` are handled.
### 3. Test Container Runs as Root
* **Vulnerability:** The test Docker container runs as the `root` user.
* **Severity:** High
* **Location:** `backend/Dockerfile.test`
* **Line Content:** Not applicable (absence of `USER` instruction).
* **Description:** Running a container as `root` is a security risk, even in a test environment. If an attacker gains control of the application, they will have root privileges inside the container, which can be used to escalate privileges further.
* **Recommendation:** Create a non-root user and switch to it using the `USER` instruction in the Dockerfile.
### 4. Worker Container Runs as Root
* **Vulnerability:** The worker Docker container runs as the `root` user.
* **Severity:** High
* **Location:** `backend/Dockerfile.worker`
* **Line Content:** Not applicable (absence of `USER` instruction).
* **Description:** Running a container as `root` is a security risk. If an attacker gains control of the application, they will have root privileges inside the container, which can be used to escalate privileges further.
* **Recommendation:** Create a non-root user and switch to it using the `USER` instruction in the Dockerfile.
### 5. Potential IDOR in `get_order` endpoint
* **Vulnerability:** Potential Insecure Direct Object Reference (IDOR) in the `get_order` endpoint.
* **Severity:** High
* **Location:** `backend/app/api/v1/orders.py`
* **Line Content:** `async def get_order(order_id: str, db: AsyncSession = Depends(get_async_db)) -> OrderDetailResponse:`
* **Description:** The `get_order` endpoint takes an `order_id` from the URL and retrieves the order details. There are no checks to verify that the authenticated user has permission to access the requested order. An attacker could potentially enumerate order IDs to view orders belonging to other users.
* **Recommendation:** Implement an access control check to ensure that the current user is authorized to view the requested order. This typically involves checking if the `order.user_id` matches the `current_user.id`.
### 6. Missing Access Control on Task Management Endpoints
* **Vulnerability:** Missing access control on task management endpoints.
* **Severity:** High
* **Location:** `backend/app/api/v1/tasks.py`
* **Line Content:** `async def toggle_task_state(request: TaskControlRequest) -> TaskControlResponse:`
* **Description:** The endpoints in `tasks.py` for controlling the task queue (`/toggle`, `/status`, etc.) do not have any authorization checks. This means that any user, including unauthenticated users, could potentially pause, resume, or otherwise interfere with the task queue, leading to a denial of service.
* **Recommendation:** Implement a robust access control mechanism for all task management endpoints. Only authorized users (e.g., administrators) should be able to perform these actions. This can be achieved using FastAPI's dependency injection system to require an authenticated user with the necessary permissions.
### 7. Potential IDOR in `user_data` endpoints
* **Vulnerability:** Potential Insecure Direct Object Reference (IDOR) in the `user_data` endpoints.
* **Severity:** High
* **Location:** `backend/app/api/v1/user_data.py`
* **Line Content:** `async def get_user_data(user_id: str, service: UserDataService = Depends(get_user_data_service)):` and `async def delete_user_data(user_id: str, service: UserDataService = Depends(get_user_data_service)):`
* **Description:** The `get_user_data` and `delete_user_data` endpoints take a `user_id` from the URL and retrieve or delete the user's data. There are no checks to verify that the authenticated user has permission to access or delete the requested user's data. An attacker could potentially enumerate user IDs to view or delete user data belonging to other users.
* **Recommendation:** Implement an access control check to ensure that the current user is authorized to view or delete the requested user data. This typically involves checking if the `user_id` matches the `current_user.id` or if the current user has administrative privileges.
### 8. Potential IDOR in `get_task_status` endpoint
* **Vulnerability:** Potential Insecure Direct Object Reference (IDOR) in the `get_task_status` endpoint.
* **Severity:** Medium
* **Location:** `backend/app/api/v1/user_data.py`
* **Line Content:** `async def get_task_status(task_id: str, service: UserDataService = Depends(get_user_data_service)):`
* **Description:** The `get_task_status` endpoint takes a `task_id` from the URL and retrieves the task status. There are no checks to verify that the authenticated user has permission to access the requested task status. An attacker could potentially enumerate task IDs to view the status of tasks belonging to other users.
* **Recommendation:** Implement an access control check to ensure that the current user is authorized to view the requested task status. This could involve associating tasks with users and checking for ownership.
### 9. Permissive CORS and `ALLOWED_HOSTS` Configuration
* **Vulnerability:** The `ALLOWED_HOSTS` and `CORS_ORIGINS` are set to `["*"]` by default.
* **Severity:** Medium
* **Location:** `backend/app/core/config.py`
* **Line Content:** `ALLOWED_HOSTS: list[str] = Field(default=["*"], description="允许的主机列表")` and `CORS_ORIGINS: list[str] = Field(default=["*"], description="CORS允许的源列表")`
* **Description:** The `ALLOWED_HOSTS` and `CORS_ORIGINS` settings are configured to allow any host. This is a permissive setting that could be a security risk in a production environment. A permissive CORS policy can make the application vulnerable to cross-site request forgery (CSRF) attacks, and a permissive `ALLOWED_HOSTS` setting can make the application vulnerable to host header attacks.
* **Recommendation:** In a production environment, `ALLOWED_HOSTS` and `CORS_ORIGINS` should be set to a specific list of trusted domains.
### 10. Use of `KEYS` command in Redis
* **Vulnerability:** The `list_states` method uses the `KEYS` command to get all keys matching a pattern.
* **Severity:** Low
* **Location:** `backend/app/core/state_manager.py`
* **Line Content:** `keys = await redis_client.keys(search_pattern)`
* **Description:** The `KEYS` command can block the Redis server while it is executing, and can cause performance issues on a Redis instance with a large number of keys. This could be a potential denial of service vector.
* **Recommendation:** Use the `SCAN` command instead of `KEYS` to iterate over the keys in a non-blocking way.
### 11. Storage of PII in Plain Text
* **Vulnerability:** Personally Identifiable Information (PII) is stored in plain text in the database.
* **Severity:** Medium
* **Location:** `backend/app/models/user_data.py`
* **Line Content:** Not applicable (model definition).
* **Description:** The `UserData` model contains sensitive user information such as name, address, email, and phone number. This data is stored in the database without any encryption at the application level. If the database is compromised, all of this sensitive user data will be exposed in plain text.
* **Recommendation:** Encrypt sensitive PII in the database. This can be done at the application level using a library like `sqlalchemy-utils` with the `EncryptedType` or by using database-level encryption features.