mirror of
https://github.com/vastsa/FileCodeBox.git
synced 2026-11-22 23:17:09 +08:00
fix: keep Docker as root, close s3_hostname SSRF, unbreak 404 tests
Revert gosu/non-root entrypoint so NAS and root-owned volumes keep working. Reject @/userinfo in s3_hostname. Theme-less checkouts no longer crash the HTML 404 handler.
This commit is contained in:
1 parent
57c75a7141
commit
031d300cae
6 files changed
+36
-39
No files matched your search
+2
-9
@@ -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:-}\""]
|
||||
+9
-5
@@ -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("/")
|
||||
|
||||
+2
-2
@@ -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]
|
||||
|
||||
@@ -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 "$@"
|
||||
@@ -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("<!doctype html><title>{{title}}</title>", encoding="utf-8")
|
||||
try:
|
||||
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"]
|
||||
finally:
|
||||
if created:
|
||||
index.unlink(missing_ok=True)
|
||||
|
||||
async def test_default_star_accept_gets_json_404(self, initialized_client):
|
||||
"""curl 默认 */* 不含 text/html——必须走 JSON 分支(防误伤脚本调用方)。"""
|
||||
|
||||
@@ -90,6 +90,12 @@ def test_hostname_tier_rejects_url_forms(production_env):
|
||||
validate_outbound_hostname("http://127.0.0.1:9000")
|
||||
|
||||
|
||||
def test_hostname_tier_rejects_userinfo(production_env):
|
||||
"""存储层拼 https://{s3_hostname},@ 会被当成 userinfo,实际连 @ 后的地址。"""
|
||||
for hostname in ("evil.com@127.0.0.1", "attacker.example@127.0.0.1:8443"):
|
||||
with pytest.raises(ValueError):
|
||||
validate_outbound_hostname(hostname)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestChangedOnlyEnforcement:
|
||||
"""changed-only 集成回归:存量内网 endpoint 不得挡死无关设置保存。
|
||||
|
||||
Reference in new issue
Block a user