Repository navigation
feat(platform): 平台管理里配置外部访问地址,并让授权类报错说清下一步 - #1307
Closed
newexploreor wants to merge 1 commit into
Closed
newexploreor wants to merge 1 commit into
newexploreor wants to merge 1 commit into
Conversation
The platform's external address was only readable from
AGENTFLOW_PUBLIC_URL at startup, so a deployment that did not set it had
no way to recover: chat Bots answered "平台外部访问地址尚未配置,请联系
管理员" and could build neither an account binding link nor a write
confirmation link, and notifications lost their run detail link. Fixing it
meant editing .env and restarting.
Add 平台管理 → 外部访问地址, where a platform administrator saves the
address once. A saved value wins over the environment, so the page can
always correct a wrong or missing deployment configuration; the page names
the effective source and shows the shadowed environment value, so a
configured AGENTFLOW_PUBLIC_URL is never silently mistaken for the one in
use.
Reading it on use required a resolver. Service.PublicURL was a string read
from the environment once and copied again into botIdentityHandlers, so
nothing could change it at runtime. Service.PublicAddress now resolves the
stored value first and the environment second, and every link builder goes
through it; botIdentityHandlers takes a func instead of a string. The row is
a single primary-key lookup and the callers run at chat-event rate, so there
is no cache to invalidate.
The suggested value comes from the administrator's own browser
(location.origin), never from a Host header the platform received: this
address is pasted into binding links that carry a one-time token, so a
forgeable source would be a phishing vector. Accepted shapes mirror
scripts/install-address.py so the installer and the page agree; a loopback
address is saved with a warning rather than refused, because it is what a
local trial uses but cannot work in a link sent to a phone.
Also make the authorization failures say what to do about them, which is
what sent people looking at the wrong layer:
- WeCom 850002 ("no authorization") fell through to the generic "请检查机器
人授权或稍后重试" and was not counted as awaiting authorization, so the
configuration showed a plain error and retrying looked reasonable. It now
names the capability grant, where to grant it, and that retrying will not
help; 851009 (pending approval) and 853006 (tenant capability blocked) get
their own text, and awaiting_authorization is decided by one predicate
shared by the health and delivery paths. 853006 stays a plain error because
no authorization resolves it.
- The Feishu/DingTalk enterprise-confirmation notice now names the step that
unblocks it instead of asking the sender to retry, which cannot succeed
until an administrator confirms the enterprise.
New table only, filled by AutoMigrate; no data migration.
Collaborator
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
平台的对外访问地址只能在启动时从
AGENTFLOW_PUBLIC_URL读一次。部署时没设的话,没有任何补救办法:要修只能改
.env再重启。改动
1. 平台管理 → 外部访问地址
新增分区,管理员填一次存库。页面设置优先于环境变量,这样页面永远能纠正部署时的错配或漏配;同时页面标注当前生效来源,并显示被盖掉的环境变量值,避免「我改了 env 怎么没生效」。
2. 为此需要一个 resolver
Service.PublicURL原本是启动时读一次 env 的字符串,还被再拷一份进botIdentityHandlers,所以运行时改不动。Service.PublicAddress():先查库里保存的值,再回落到环境变量;notification/service.go、push.go、httpapi/notifications.go、httpapi/bot_drafts.go);botIdentityHandlers.publicURL从string改成func() string。单行主键查询,调用方都是 IM 事件级频率,所以不加缓存,省掉缓存失效的复杂度。
3. 「自动识别」怎么做的
页面上的「填入当前访问地址」取的是管理员浏览器自己的
location.origin,不读也不信任服务端收到的Host头。这点是刻意的:这个地址会被拼进带一次性 token 的绑定链接,并引导用户去那里登录平台。来源可伪造就等于一条钓鱼链路。所以没有做「运行时从请求头自动学习」。
校验规则照搬
scripts/install-address.py:86-95已有的那套,保证安装器和页面接受完全相同的值:拒绝 userinfo / query / fragment / 路径、拒绝0.0.0.0和链路本地地址。回环地址保存但附警告而不是拒绝——它是本机联调会用的,但发到手机上点不开。另外校验会把 scheme 和 host 规范化成小写(
HTTPS://X→https://x),因为这个值要被拼进链接。这是写测试时发现的。4. 顺带:让授权类报错说清下一步
这几条是实际踩坑时把人引向错误方向的提示:
no authorization)原先掉进兜底文案「请检查机器人授权或稍后重试」,而且没被计入awaiting_authorization,所以 Bot 在管理页显示成笼统的error,「重试一下」看起来很合理——但这个码重试一万次也一样。现在明确说是机器人的「消息」能力授权没给、该由谁去哪里授权、以及重试不解决问题。awaiting_authorization的判定收敛成一个awaitingAuthorization()谓词,健康状态和发送失败两条路径共用。853006 故意不算在内——没有任何授权能解决它,算进去会让人一直等一个等不到的审批。迁移
只新增一张表,由
repo.Open()的 gorm AutoMigrate 补齐,不需要迁移脚本。测试
新增 4 个测试文件:
internal/domain/platform_settings_test.goUsablePublicURL、长度上限internal/httpapi/platform_public_url_test.goPublicAddress()和绑定链接立刻生效(resolver 的真正验收点)internal/repo/platform_settings_test.gointernal/notification/wecom_authorization_test.goawaitingAuthorization();853006 不算待授权;未知码保持兜底反向验证做了两处,确认测试在测链路不是空跑:
PublicAddress()的查库分支 →TestSavedPublicURLAppliesWithoutRestart报address after saving = "";TestPublicURLRequiresPlatformAdmin报GET as a plain member = 200。回归对比:
go test ./internal/httpapi在改动前后的失败用例名完全一致(12 个,均为本环境缺bwrap、以及web/native-chat是 gitignore 的构建产物所致),用git stash在干净 main 上复跑确认。./internal/domain、./internal/repo、./internal/notification、./internal/botinteraction全绿。gofmt、go vet干净。