Skip to content

feat(http): 让看板能在局域网地址与反向代理下打开 - #8

Closed
nohappiness wants to merge 9 commits into
ParticleLight:mainfrom
nohappiness:feat/trusted-remote-hosts
Closed

nohappiness wants to merge 9 commits into
ParticleLight:mainfrom
nohappiness:feat/trusted-remote-hosts

Conversation

@nohappiness

Copy link
Copy Markdown

问题

只要 DSH 不绑定 127.0.0.1,这个面板就打不开。所有 /api/all-usage/* 路由都走同一个守卫
(lib/http.js 的 isTrustedLocalApiRequest),它要求 TCP 对端 与 Host 头 同时是
loopback。dsh web --host <网卡地址> 启动、或在前面挂一层反向代理时,每一个请求都过不了第一道
检查,于是侧栏入口打开后就是一句「无法加载用量统计。请重试。」

重试也没用:它重放的是被同一条规则拒掉的请求。

复现与证据

把服务绑到网卡上,用该地址或经反代访问:

dsh web --host 192.168.168.226 --port 3080 --public-url https://chat.example.com/
请求 结果
不带会话 cookie(还没进插件) 401 unauthorized —— 是 DSH 的认证层,不是本问题
带合法 DSH 会话 cookie,Host: 192.168.168.226:3080 403 {"ok":false,"message":"forbidden"}
同一个 cookie,走一条上行源地址为 127.0.0.1 的桥接 200,真实数据(pluginVersion: "1.2.4")

即认证与数据链路都正常,只有 loopback 判定在拒绝这种部署。README 已明确写过当前契约
(「所有 API 仅接受本机 loopback 请求……HTTP 守卫还会检查真实 socket peer」),所以这是一条
设计边界而不是 bug —— 问题在于它是否应该覆盖这种部署形态,而共享主机或反代恰恰是默认形态。

本次改动

1. 边界改为按 Host 判定,集合来自配置

可信主机名 = loopback
           ∪ webApp.getAdvertisedUrl()      (--public-url,或监听 origin)
           ∪ webServer.getOrigin()          (DSH 实际绑定的地址)
           ∪ DSH_ALL_USAGE_TRUSTED_HOSTS    (额外名字,例如反代别名)
  • 不在集合里的名字照旧拒绝,所以 DNS rebinding 依旧无效:攻击者的域名永远不会进入一个
    来自配置的集合。
  • 非 loopback 的 socket 只能访问集合里的非 loopback 名字。「远端 socket + loopback Host」
    不是浏览器会产生的组合,仍然 403(有专门测试)。
  • 解析是惰性的,且在 listen 绑定之前不缓存,所以绑定地址在下一次请求就能生效,而不是等一个
    TTL;DSH 是先把插件 apply 完才去开端口。

2. Origin 改为按主机名与同一个集合比对

原来要求它逐字等于请求的 http://<host>。反代终结 TLS 后,页面的 https://name 到本进程变成了
http 跳,且代理可能改写 Host,于是写入类操作(保存价格、别名、立即同步、节假日抓取、归档)
从面板发出去全是 403。指向集合之外主机名的 Origin 仍然拒绝。

3. DSH_ALL_USAGE_STRICT_LOCAL=1 可完全退回旧边界,给不愿把 API 暴露给网络的部署。

4. 拒绝时说明是哪条规则没过

403 现在带 { ok: false, message: 'forbidden', reason },并回写同名响应头
x-all-usage-forbidden-reason。取值:missing-host、bad-host、host-not-trusted、
peer-loopback-host、cross-site、missing-origin、origin-not-trusted、
capability-invalid。原因码只说明规则,永远不含放行名单,被拒的一方能自查,但拿不到
部署拓扑。

5. 边界在面板里可见

status(以及展开它的完整快照)带
access: { strict, hosts, sources: { env, advertised, listener } }。空状态现在会解释拒绝原因
(host-not-trusted 会点出浏览器当前用的地址,以及该检查的 --public-url / --host / 环境变量),
「数据维护」底部新增只读的 访问控制 块,列出接受的主机名与各自来源。

新增模块:lib/trusted-hosts.js(集合)与 lib/net-address.js(loopback 与主机名字面量,
路由守卫与集合共用)。新增 UI 文案已补进六份非中文词典。

为什么这没有放松边界

关注点 依然成立
DNS rebinding 集合来自配置,重绑域名不在其中 → 403。
伪造请求头 远端 socket + 只属 loopback 的 Host → 403(peer-loopback-host)。
跨站读取 sec-fetch-site: cross-site → 403;不返回任何 CORS 头。
写入 仍要求每次 apply 生成的进程令牌(x-all-usage-request-token)。
拒绝时泄露 reason 只给规则名,不返回主机名、路径或拓扑。
想完全关掉 DSH_ALL_USAGE_STRICT_LOCAL=1 精确恢复 loopback-only。

另外要注意:这个插件的 API 位于 DSH 自己的会话 cookie 之后 —— 未认证的调用方在到达这些代码
之前就已经从宿主拿到 401。本次改动决定的是「本部署的哪些名字」可以访问已认证界面本来就在调的
路由,并没有新增未认证面。

兼容性

  • loopback 用户毫无变化:拿不到 webApp / webServer origin 时集合为空,行为与旧实现完全一致
    (既有安全测试除新增 reason 字段外全部原样通过)。
  • 纯 TUI 宿主(没有 webServer)不受影响;webApp 是惰性读取,且没有放进 inject。
  • 页面协议不变:它本来就用相对路径请求。

测试

npm test          496 项,491 通过,5 项既有 skip,0 失败
npm run build:client   重新生成 lib/client.js,重跑零 diff
node scripts/check-package.mjs   100 个文件

新增:test/trusted-hosts.test.js(集合解析、晚绑定、环境变量清单、strict 模式)、
test/client-access.test.js(原因码 → 文案,逐规则)。
扩展:test/security.test.js(局域网与反代访问、伪造 loopback Host、陌生名字、经反代写入、
环境变量放行、原因码、access 载荷)、test/archive-api.test.js(逐条拒绝的原因码)。

真实 socket、非 loopback 对端(192.168.168.226)下的端到端结果:
Host = 局域网地址 → 200,Host = 公布域名 → 200,Host = evil.example →
403 host-not-trusted,strict 模式 → 全部 403。

没有做的事

没有加设置页。名单本来就能从 --public-url / 监听地址自动推出,常见场景不需要配置;可编辑列表
会在一条安全边界上再开一个写入面,所以这次刻意让边界在 UI 里保持只读。

给维护者的说明

  • DSH_ALL_USAGE_TRUSTED_HOSTS 接受裸主机名、host:port、带方括号的 IPv6 与完整 URL;
    loopback 条目会被忽略,因为 loopback 始终可信。

Zhang Yueqian added 7 commits November 25, 2026 15:50
The API guard required both the socket peer and the Host header to be
loopback, so a DSH started with --host <interface> (or published behind a
reverse proxy) answered nothing: every /api/all-usage/* request was 403 and
the panel showed "无法加载用量统计".

The boundary is now decided from Host: loopback names, the URL DSH
advertises (--public-url or the listener origin), the listener's own
address, and any name in DSH_ALL_USAGE_TRUSTED_HOSTS. A socket that is not
loopback may only address a non-loopback name from that set, so a remote
peer pairing itself with a loopback Host is still refused, and a name
outside the set still fails closed (DNS rebinding stays dead).

An Origin is compared by host name against the same set instead of as a
scheme+authority pair: a TLS-terminating proxy legitimately turns the
page's https origin into an http hop and may rewrite Host.

DSH_ALL_USAGE_STRICT_LOCAL=1 restores the loopback-only boundary.
Extends the harness with an advertised URL and a listener origin, then
covers: LAN and proxied hosts answering, a proxy running on loopback,
foreign names and forged loopback Hosts staying 403, a proxied write whose
Origin and Host name different trusted hosts, the environment allowlist,
and strict mode. trusted-hosts.test.js pins the resolution itself,
including the listener origin that only exists after the port is bound.
README (zh/en) now says what the HTTP guard checks instead of claiming
loopback-only, and the configuration tables list
DSH_ALL_USAGE_TRUSTED_HOSTS and DSH_ALL_USAGE_STRICT_LOCAL. CHANGELOG gets
an unreleased entry.
ja/es/fr/de/ru/pt still described a loopback-only guard, which the branch
no longer enforces; each now names the trusted host set and carries the
two configuration rows.
A 403 used to be a bare "forbidden", so the panel could only say "please
retry" and a reader had no way to tell a missing Host name from a proxy
rewriting Origin. The guard now returns a code — missing-host, bad-host,
host-not-trusted, peer-loopback-host, cross-site, missing-origin,
origin-not-trusted, capability-invalid — sent in the body and in
`x-all-usage-forbidden-reason`.

The code names the rule that failed and nothing else: a refused caller
still learns no host name, which keeps the deployment's topology out of
an unauthenticated response.

The status and full payloads now also carry `access`
({ strict, hosts, sources }) so an open panel can show what the boundary
accepts without reading the host log; `trusted-hosts.js` grew a describe()
that reports every source separately.
The empty state reads the refusal code and says which rule failed,
including the address the browser used, instead of "please retry".
Maintenance gains a read-only Access control block listing the accepted
host names and where each came from (environment, advertised URL,
listener address), so the boundary is visible from the panel itself.

The six non-Chinese dictionaries carry the nine new strings.
README (zh/en) lists the refusal codes, notes that they never contain the
allowlist, and describes the read-only Access control block. CHANGELOG
records both under the unreleased entry.
@nohappiness
nohappiness marked this pull request as draft November 26, 2026 01:56
Zhang Yueqian added 2 commits November 26, 2026 09:58
A deployment started as `dsh web --host <iface> --public-url https://name/`
still answered 403 for that name: the flags land in DSH's `webStartup`
service (dsh-web-app/lib/startup.js), while `dsh-web-app` only reads its own
config, so `webApp.getAdvertisedUrl()` returned the listener origin instead.

Measured on a live host: the allowlist announced only the interface address,
and `status.access.sources.advertised` equalled it too.

The set now reads `webStartup` first (publicUrl, trustedHosts, host), falls
back to the launcher's `cmdlineArgs` snapshot (both `--flag value` and
`--flag=value`, repeatable `--trusted-host`), and only then to
`webApp.getAdvertisedUrl()`. `sources` grows a `trusted` list so the panel can
show where each name came from.

Loopback-only deployments are unchanged: with none of those services present
the set is still built from the environment allowlist alone.
The HTTP-boundary paragraphs and the configuration row now name the
webStartup service (and the cmdlineArgs fallback) instead of implying the
flags reach webApp.getAdvertisedUrl(). CHANGELOG records the fix.
@nohappiness

Copy link
Copy Markdown
Author

补充了一条提交:第一版在真机上仍然拒绝 --public-url。

原因是这些参数落在 DSH 的 webStartup 服务(dsh-web-app/lib/startup.js),
而 dsh-web-app 读取公布地址时只看自己的 config(lib/index.js),所以
webApp.getAdvertisedUrl() 返回的是监听地址 —— 可信集合里只出现了网卡地址,
域名访问照旧 403。

lib/trusted-hosts.js 现在按顺序取来源:先读 webStartup(publicUrl /
trustedHosts / host),再退回启动器给的 cmdlineArgs 快照(支持
--flag value 与 --flag=value,--trusted-host 可重复),最后才用
getAdvertisedUrl()。status.access.sources.trusted 会列出由
--trusted-host 带来的名字,便于诊断。

真机验证(Host 与 socket 均非 loopback):

请求 结果
Host: <网卡地址> 200
Host: <反代域名> 200
Host: evil.example 403 host-not-trusted

启动日志同时会打印放行清单:
[dsh-all-usage] API also accepts these non-loopback hosts: 192.168.168.226, chat.mis-zos.com

当前 head:36e3e91(53ad506 修复 + 36e3e91 文档)。

@nohappiness
nohappiness marked this pull request as ready for review November 26, 2026 02:03
@nohappiness

nohappiness commented Nov 27, 2026 •

Copy link
Copy Markdown
Author

superseded by v3.4.6 (b93f398)

EDITED:
错误!主分支并未supress当前提交。

@nohappiness

Copy link
Copy Markdown
Author

我的错。。。 消息混了,我把另一个项目的提交跟踪看成了这个项目,错误关闭了本PR。

重新打开了,以后我多注意 Orz

@nohappiness nohappiness reopened this Nov 27, 2026
@ParticleLight

Copy link
Copy Markdown
Owner

感谢反馈。这个 远程访问bug 确实存在。不过不会直接合你的 PR ,我让AI总结了下原因:它继续注册 exact 路由,而 exact 会先于宿主 /api prefix 命中,绕过 Connection 的 Cookie/admission;在 exact 上再补一套插件自己的 trust 判断,根因仍在。另外还有 publicUrl 被自动当成可信来源、Host 解析丢端口、测试未覆盖真实 Cookie 链等问题。修复会改为单 prefix,把认证交回宿主。目前修复在本地完成了,下个版本就会修复。
这个 PR 我会关闭,感谢提交。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants