diff --git a/Dockerfile b/Dockerfile index 382bb00..bff1cbb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -59,17 +59,13 @@ COPY --from=frontend-builder /build/fronted-2023/dist ./themes/2023 # 安装系统安全更新 + Python 依赖 # 依赖从带哈希的锁定文件安装(--require-hashes),保证构建可复现、防供应链篡改。 -# gosu 用于入口脚本的数据卷属主修正后降权;清理 apt 缓存,降低镜像噪音与扫描面 +# 清理 apt 缓存,降低镜像噪音与扫描面 RUN apt-get update \ && apt-get upgrade -y --no-install-recommends \ - && apt-get install -y --no-install-recommends gosu \ && rm -rf /var/lib/apt/lists/* \ && pip install --no-cache-dir --require-hashes -r requirements.lock.txt \ && pip cache purge || true -# 非 root 运行用户;数据卷属主由 docker-entrypoint.sh 按需修正(兼容存量 root 卷) -RUN useradd --system --uid 10001 --home-dir /app app - # 环境变量配置 ENV HOST="0.0.0.0" \ PORT=12345 \ @@ -81,11 +77,8 @@ ENV HOST="0.0.0.0" \ EXPOSE 12345 -COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh -RUN chmod +x /usr/local/bin/docker-entrypoint.sh -ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] - # 生产环境启动命令 # FORWARDED_ALLOW_IPS 默认为空:仅信任直连 IP,避免任意客户端伪造 X-Forwarded-*。 # 若前面有反向代理,请显式设置为代理网段,例如 "10.0.0.0/8,172.16.0.0/12"。 +# 容器以 root 运行:兼容存量 root 属主的数据卷,以及 NAS/只读 bind mount。 CMD ["sh", "-c", "access_log_arg=--no-access-log; if [ \"${APP_ENV:-development}\" != \"production\" ] || [ \"${ACCESS_LOG:-false}\" = \"true\" ]; then access_log_arg=--access-log; fi; exec uvicorn main:app --host \"$HOST\" --port \"$PORT\" --workers \"$WORKERS\" --log-level \"$LOG_LEVEL\" \"$access_log_arg\" --proxy-headers --forwarded-allow-ips \"${FORWARDED_ALLOW_IPS:-}\""] diff --git a/apps/base/pages.py b/apps/base/pages.py index 09179b4..53c18e0 100644 --- a/apps/base/pages.py +++ b/apps/base/pages.py @@ -96,14 +96,18 @@ async def not_found_handler(request, exc=None): (负路径测试抓到的存量 bug)。浏览器(Accept 含 text/html)仍拿到 主题首页做 SPA 兜底;API 客户端拿到 JSON 404。 """ - if request is not None and request.method in {"GET", "HEAD"} and "text/html" in ( - request.headers.get("accept", "") - ): - return await index(request, exc) - return JSONResponse( + json_404 = JSONResponse( status_code=404, content={"code": 404, "message": "Not Found", "detail": "资源不存在"}, ) + if request is not None and request.method in {"GET", "HEAD"} and "text/html" in ( + request.headers.get("accept", "") + ): + try: + return await index(request, exc) + except HTTPException: + return json_404 + return json_404 @router.get("/") diff --git a/core/security.py b/core/security.py index 16b134e..3a708a4 100644 --- a/core/security.py +++ b/core/security.py @@ -155,8 +155,8 @@ def validate_outbound_hostname(value: Any) -> str: if os.environ.get("APP_ENV", "development") != "production": return hostname - if "://" in hostname or "/" in hostname: - raise ValueError(f"s3_hostname 应为裸主机名,不含协议或路径:{hostname}") + if "://" in hostname or "/" in hostname or "@" in hostname: + raise ValueError(f"s3_hostname 应为裸主机名,不含协议、路径或 userinfo:{hostname}") # 取主机部分:[v6] 形态取括号内;host:port 取冒号前;裸 IPv6(多个冒号)整体 if "[" in hostname: host = hostname.split("[", 1)[1].split("]", 1)[0] diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh deleted file mode 100755 index 3c1cd28..0000000 --- a/docker-entrypoint.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh -# FileCodeBox 容器入口:兼容式非 root 降权。 -# -# 默认以 root 进入(兼容存量 root 属主的数据卷),按需修正 data 目录属主后 -# 用 gosu 降权到 app 用户运行;若部署方显式指定了 user(非 root),则直接运行 -# 不做任何 chown,保证两种形态都可预期。 -set -e - -DATA_DIR="${DATA_DIR:-/app/data}" - -if [ "$(id -u)" = "0" ]; then - mkdir -p "$DATA_DIR" - # 仅当存在非 app 属主的文件时才 chown,避免大卷启动变慢 - if [ -n "$(find "$DATA_DIR" ! -uid "$(id -u app)" -print -quit 2>/dev/null)" ]; then - chown -R app:app "$DATA_DIR" - fi - exec gosu app "$@" -fi - -exec "$@" diff --git a/tests/test_negative_edge_paths.py b/tests/test_negative_edge_paths.py index e25f88d..4ce59f2 100644 --- a/tests/test_negative_edge_paths.py +++ b/tests/test_negative_edge_paths.py @@ -138,9 +138,23 @@ class TestNotFoundHandlerBranches: assert "text/html" not in response.headers["content-type"] async def test_browser_accept_gets_theme_page(self, initialized_client): - response = await initialized_client.get("/no-such-path", headers={"Accept": "text/html"}) - assert response.status_code == 200 - assert "text/html" in response.headers["content-type"] + from core.settings import BASE_DIR + + theme_dir = BASE_DIR / "themes" / "2024" + theme_dir.mkdir(parents=True, exist_ok=True) + index = theme_dir / "index.html" + created = not index.exists() + if created: + index.write_text("