From dfeda0c7843b8536c9901e7b9cc90ffed3bd4b2a Mon Sep 17 00:00:00 2001 From: Pine Date: Mon, 7 Sep 2026 22:25:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20URL=20=E5=90=8D=E5=8D=95=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E9=80=9A=E9=85=8D=20*.gov.cn=20=E4=B8=8E=E6=AD=A3?= =?UTF-8?q?=E5=88=99=20/pattern/=20=E5=BD=A2=E6=80=81=EF=BC=88=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=E7=BC=96=E8=AF=91=E6=A0=A1=E9=AA=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/routers/rbac_operator.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/api/routers/rbac_operator.py b/app/api/routers/rbac_operator.py index e84fc83..005c176 100644 --- a/app/api/routers/rbac_operator.py +++ b/app/api/routers/rbac_operator.py @@ -6,6 +6,7 @@ from __future__ import annotations import json +import re from fastapi import APIRouter, Depends, File, HTTPException, Request, Response, UploadFile from pydantic import BaseModel @@ -635,8 +636,16 @@ def _split_host_list(raw: str | None) -> list[str]: def _normalize_host(host: str) -> str: h = host.strip().lower() + # 正则形态 /pattern/(如 /\.gov\.cn$/):编译校验后原样保留 + if h.startswith("/") and h.endswith("/") and len(h) > 2: + try: + re.compile(h[1:-1]) + except re.error: + raise HTTPException(status_code=422, detail="正则表达式格式不正确") + return h h = h.replace("https://", "").replace("http://", "") h = h.split("/")[0].split(":")[0].strip() + # 通配符形态 *.gov.cn 保留 *;其余为普通域名 if not h or any(ch.isspace() for ch in h): raise HTTPException(status_code=422, detail="域名格式不正确") return h @@ -651,9 +660,9 @@ async def _get_url_rules(db: Database) -> dict: async def _save_url_rules(db: Database, rules: dict) -> None: await db.config.set(URL_WHITELIST_KEY, ",".join(rules["whitelist"]), - description="应用内网页容器白名单(域名)") + description="应用内网页容器白名单(域名 / 通配 *.x / 正则 /pattern/)") await db.config.set(URL_BLACKLIST_KEY, ",".join(rules["blacklist"]), - description="应用内网页容器黑名单(域名,命中禁止打开)") + description="应用内网页容器黑名单(域名 / 通配 *.x / 正则 /pattern/,命中禁止打开)") @router.get("/site/url-rules", summary="站点 URL 白名单/黑名单列表")