W3-d 复核 L2:使用者列通知渠道时拿不到全局、应用邮件渠道的收件地址

复核发现:GET /api/notify/channels 对使用者照样列全局、应用渠道(应用的通知里要挑渠道,只用名字和 id),而邮件渠道的
mail_to 给的是全部收件地址原文;channel_out 的说明却写着「搭建者以上才看得到」。Webhook 渠道只给主机名,邮件渠道多露了
一层:任何使用者都能拿到内部的收件名单。

做法:channel_out 加 recipients 参数,list_channels 按 _can_edit 的同一条规矩判(关登录时谁都能改,开了登录要搭建者
以上):改得了的照原样给 mail_to(改地址要整份重填),改不了的只给条数 mail_count。个人渠道照旧写发到自己的账号邮箱。
建、改渠道的返回本来就只给能改的人,照旧。说明同步改;前端 typesApp 的 NotifyChannelRow 加 mail_count(设置页的
通知只给使用者列个人渠道,界面上用不上,不加入口)。

测试(tests/test_notify_mail.py):开登录,使用者列渠道时全局、应用的邮件渠道照样在、没有 mail_to、只有条数,自己的个人
渠道照常;搭建者看得到地址。去掉修复回跑,这一项失败。
This commit is contained in:
ThearchyHelios
2026-10-16 18:20:52 +08:00
parent 36c8f581cc
commit e5003a1d46
4 changed files with 50 additions and 6 deletions
+6 -2
View File
@@ -120,7 +120,10 @@ async def _owner_emails(rows: list[NotifyChannel]) -> dict[str, str | None]:
@router.get("/channels")
async def list_channels() -> dict[str, Any]:
"""{items, mail}:形状同前端的接口约定(frontend/src/api/capabilities.ts)。mail 是邮件服务启没启用、本人的账号邮箱
(关登录时没有账号,为 null):设置页据此列不列「邮件」格式、个人邮件渠道发到哪。"""
(关登录时没有账号,为 null):设置页据此列不列「邮件」格式、个人邮件渠道发到哪。
全局、应用渠道对谁都列(应用的通知里要挑渠道),但邮件渠道的收件地址只给改得了它的人(同 _can_edit:关登录时谁都能改,
开了登录要搭建者以上),别人只拿到条数(channel_out 的 recipients)。"""
from app.db.models import User
principal = current_principal()
@@ -133,7 +136,8 @@ async def list_channels() -> dict[str, Any]:
cfg = await mail.load_config(session)
mine = await session.get(User, principal.user_id) if principal.mode != "off" and principal.user_id else None
emails = await _owner_emails(rows)
return {"items": [channel_out(r, emails.get(r.owner_id or "")) for r in rows],
editor = principal.mode == "off" or principal.at_least("builder")
return {"items": [channel_out(r, emails.get(r.owner_id or ""), recipients=editor) for r in rows],
"mail": {"enabled": cfg.enabled, "account_email": (mine.email or None) if mine is not None else None}}
+9 -3
View File
@@ -134,11 +134,13 @@ async def personal_url_problem(url: str) -> str | None:
return None
def channel_out(row: NotifyChannel, owner_email: str | None = None) -> dict[str, Any]:
def channel_out(row: NotifyChannel, owner_email: str | None = None, *, recipients: bool = True) -> dict[str, Any]:
"""渠道的接口形状(typesApp.ts 的 NotifyChannelRow):地址只回主机名,密钥只说配没配。
邮件渠道(notify/mail.py)没有主机名:mail_to 是发给谁——个人渠道是拥有者的账号邮箱(owner_email,没有邮箱是空的),
全局、应用的是填的那几个收件地址(搭建者以上才看得到这些渠道,改地址时要整份重填,所以照原样给)。"""
全局、应用的是填的那几个收件地址:改地址时要整份重填,所以给能改它的人照原样给。recipients 为假(调用方判定看的人改不了
全局、应用的渠道,api/notify.list_channels)时不给地址、只给条数 mail_count:渠道列表对使用者照样列全局、应用渠道(应用
的通知里要挑一个,只用名字和 id),Webhook 只给主机名,邮件的收件地址不能比它多露一层。"""
from app.core.crypto import decrypt
url = decrypt(row.url) or ""
@@ -149,7 +151,11 @@ def channel_out(row: NotifyChannel, owner_email: str | None = None) -> dict[str,
if row.scope == "user":
out["mail_to"] = [owner_email] if owner_email else []
else:
out["mail_to"] = [p.strip() for p in url.split(",") if p.strip()]
to = [p.strip() for p in url.split(",") if p.strip()]
if recipients:
out["mail_to"] = to
else:
out["mail_count"] = len(to)
return out
+32
View File
@@ -334,6 +334,38 @@ async def test_personal_mail_channel_takes_no_address(smtp):
await auth_env.reset()
async def test_users_listing_channels_do_not_get_global_recipients(smtp):
"""使用者列渠道照样看得到全局、应用渠道(应用的通知里要挑一个,只用名字和 id),但邮件渠道的收件地址只给能改它的人
(搭建者以上):只给条数,不给地址。Webhook 只给主机名,邮件地址不能比它多露(复核 L2)。"""
from tests.fakes import auth_env
await configure()
await auth_env.reset()
try:
await auth_env.turn_on_local()
await auth_env.make_user("mail-uma", "user", email="uma@example.com")
await auth_env.make_user("mail-bo", "builder")
everyone = "finance-lead@example.com, ops@example.com"
made = await channel("global", to=everyone)
await channel("app", to="team@example.com")
uma = await auth_env.login("mail-uma")
mine = await uma.post("/api/notify/channels", json={"name": "我的邮件", "scope": "user", "format": "email",
"events": ["task.opened"]})
assert mine.status_code == 201, mine.text
listed = (await uma.get("/api/notify/channels")).json()["items"]
by = {row["scope"]: row for row in listed}
assert set(by) == {"global", "app", "user"}, "全局、应用渠道照样列(应用的通知里要挑)"
assert "finance-lead" not in json.dumps(listed) and "team@" not in json.dumps(listed), "收件地址不给使用者"
assert "mail_to" not in by["global"] and by["global"]["mail_count"] == 2
assert "mail_to" not in by["app"] and by["app"]["mail_count"] == 1
assert by["user"]["mail_to"] == ["uma@example.com"], "自己的个人渠道照常写发到哪"
bo = await auth_env.login("mail-bo")
shown = {row["id"]: row for row in (await bo.get("/api/notify/channels")).json()["items"]}
assert shown[made.id]["mail_to"] == ["finance-lead@example.com", "ops@example.com"], "能改的人看得到,改地址要整份重填"
finally:
await auth_env.reset()
# --------------------------------------------------------------------------
# 设置
# --------------------------------------------------------------------------
+3 -1
View File
@@ -734,9 +734,11 @@ export interface NotifyChannelRow {
enabled: boolean
/**
* 邮件渠道发给谁:个人渠道是拥有者的账号邮箱(没有邮箱是空的,发不出去),全局、应用的是填的那几个收件地址。
* Webhook 渠道没有这一项
* Webhook 渠道没有这一项。全局、应用的邮件渠道只给改得了它的人(开了登录要搭建者以上),别人拿到的是 mail_count
*/
mail_to?: string[]
/** 全局、应用的邮件渠道给改不了它的人列出时:只有收件地址的个数,没有地址(设置页的通知只给使用者列个人渠道,用不上) */
mail_count?: number
}
/**