Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

feat(alpha_auth): refactoring alpha authentication patch to control authentication skipping through startup parameters

hairyf9bfffd7da620
diff --git a/src-tauri/src/service/patch/alpha_auth.rs b/src-tauri/src/service/patch/alpha_auth.rs
index 394632cb..74b76798 100644
--- a/src-tauri/src/service/patch/alpha_auth.rs
+++ b/src-tauri/src/service/patch/alpha_auth.rs
@@ -1,109 +1,185 @@
-//! alpha 核心的桌面嵌入鉴权兼容补丁。
+//! alpha 核心的桌面嵌入鉴权补丁:为 `dsh --profile web` 补上可选的
+//! `--skip-auth` 参数,而不是像旧实现那样无条件下发绕过。
//!
//! alpha 的 `dsh-client-connection` 默认要求浏览器先用启动 token 换取
-//! `SameSite=Strict` Cookie。桌面端通过跨上下文 iframe 承载 Web UI,无法稳定
-//! 完成该 Cookie 交换,因此仅在命中 alpha 专有锚点时跳过 browser-session 层。
-//! Host/Origin/loopback 信任边界仍由 `isTrustedApiRequest` 保留。
+//! `SameSite=Strict` Cookie。桌面端通过沙箱跨源 iframe 承载 Web UI,无法稳定完成
+//! 该 Cookie 交换。旧补丁直接改写鉴权方法体、对所有请求放行;本补丁改为:只有
+//! 显式传入 `--skip-auth` 时跳过 browser-session 层,其余场景保持上游完整鉴权:
+//!
+//! - `dsh-web-app/lib/startup.js`:给 web 命令补上 `--skip-auth` 选项;命中时设置
+//! `DSH_SKIP_AUTH=1`(进程级开关,桌面端在启动参数里显式传该标志)。
+//! - `dsh-client-connection/lib/index.js`:仅当 `DSH_SKIP_AUTH=1` 时跳过
+//! browser-session 层(`authorizeIndex` 直接放行 index、`requestRejection` 不再
+//! 校验 cookie),Host/Origin 信任边界仍由 `isTrustedApiRequest` 保留。
+//!
+//! 普通用户运行 `dsh web`(不带 `--skip-auth`)时鉴权行为与上游一致;旧核心无
+//! 上述锚点时 `patch_dsh` 安全跳过,不改变旧版行为。
-use crate::utils::{patch_dsh, PatchOutcome};
+use crate::utils::{dsh_rel_contains, patch_dsh, PatchOutcome};
-const PATCH_MARKER: &str = "dsh-tauri-desktop: alpha embedded auth bypass";
-const CLIENT_CONNECTION_INDEX_JS: &str =
- "node_modules/@deepseek-ai/dsh-client-connection/lib/index.js";
+const PATCH_MARKER: &str = "dsh-tauri-desktop: alpha embedded auth --skip-auth flag";
+
+// ── dsh-web-app/lib/startup.js ────────────────────────────────────────────────
+const WEB_STARTUP_REL: &str = "node_modules/@deepseek-ai/dsh-web-app/lib/startup.js";
+const STARTUP_OPTION_ANCHOR: &str =
+ ".option(\"--no-open\", \"do not open the Web UI in the default browser\")";
+const STARTUP_OPTION_REPLACEMENT: &str = ".option(\"--no-open\", \"do not open the Web UI in the default browser\")\n\t\t.option(\"--skip-auth\", \"skip the browser-session token/cookie exchange; keeps the Host/Origin trust fence (for embedded UIs)\")";
+const STARTUP_ACTION_ANCHOR: &str =
+ "\t\tconst options = program.opts();\n\t\tconst allowLan = process.env.DSH_PKG_ALLOW_LAN === \"1\";";
+const STARTUP_ACTION_REPLACEMENT: &str = "\t\tconst options = program.opts();\n\t\t/* dsh-tauri-desktop: alpha embedded auth --skip-auth flag */\n\t\tif (options.skipAuth) process.env.DSH_SKIP_AUTH = \"1\";\n\t\tconst allowLan = process.env.DSH_PKG_ALLOW_LAN === \"1\";";
-// alpha 专有错误文案:旧版没有该鉴权实现,不允许仅凭通用方法名误打补丁。
-const ALPHA_AUTH_ANCHOR: &str =
- "dsh web authentication required; reopen the URL printed by dsh web.";
-const AUTHORIZE_ANCHOR: &str = "\tauthorizeIndex(req, res) {";
-const AUTHORIZE_END: &str = "\n\t}\n\t/**\n\t* Verify the authority-bound browser cookie";
+// ── dsh-client-connection/lib/index.js ────────────────────────────────────────
+const CONNECTION_INDEX_JS: &str =
+ "node_modules/@deepseek-ai/dsh-client-connection/lib/index.js";
const REJECTION_ANCHOR: &str = "\trequestRejection(request) {\n\t\tif (!isTrustedApiRequest(request, this.trustedHosts)) return 403;\n\t\treturn this.browserAuth.isAuthenticated(request) ? void 0 : 401;\n\t}";
-const REJECTION_PATCHED: &str = "\trequestRejection(request) {\n\t\tif (!isTrustedApiRequest(request, this.trustedHosts)) return 403;\n\t\treturn void 0;\n\t} /* dsh-tauri-desktop: alpha embedded auth bypass */";
+const REJECTION_PATCHED: &str = "\trequestRejection(request) {\n\t\tif (!isTrustedApiRequest(request, this.trustedHosts)) return 403;\n\t\tif (process.env.DSH_SKIP_AUTH === \"1\") return void 0;\n\t\treturn this.browserAuth.isAuthenticated(request) ? void 0 : 401;\n\t} /* dsh-tauri-desktop: alpha embedded auth --skip-auth flag */";
+const AUTHORIZE_ANCHOR: &str = "\tauthorizeIndex(request, response) {\n\t\treturn this.browserAuth.authorizeIndex(request, response);\n\t}";
+const AUTHORIZE_PATCHED: &str = "\tauthorizeIndex(request, response) {\n\t\tif (process.env.DSH_SKIP_AUTH === \"1\") return true;\n\t\treturn this.browserAuth.authorizeIndex(request, response);\n\t} /* dsh-tauri-desktop: alpha embedded auth --skip-auth flag */";
-/// 替换 alpha 的 index 与 API browser-session 鉴权,同时保留 Host/Origin fence。
-fn patch_source(source: &str) -> PatchOutcome {
+/// 替换 web 启动命令:接受 `--skip-auth`;命中时写入 `DSH_SKIP_AUTH=1` 作为与
+/// connection 层之间的进程级开关。
+fn patch_startup(source: &str) -> PatchOutcome {
if source.contains(PATCH_MARKER) {
return PatchOutcome::AlreadyPatched;
}
- if !source.contains(ALPHA_AUTH_ANCHOR)
- || !source.contains(AUTHORIZE_ANCHOR)
- || !source.contains(AUTHORIZE_END)
- || !source.contains(REJECTION_ANCHOR)
- {
+ if !source.contains(STARTUP_OPTION_ANCHOR) || !source.contains(STARTUP_ACTION_ANCHOR) {
return PatchOutcome::AnchorMissing;
}
- let authorize_start = source
- .find(AUTHORIZE_ANCHOR)
- .expect("checked authorize anchor");
- let authorize_body_start = authorize_start + AUTHORIZE_ANCHOR.len();
- let authorize_end = source[authorize_body_start..]
- .find(AUTHORIZE_END)
- .map(|offset| authorize_body_start + offset)
- .expect("checked authorize end anchor");
- // 结束锚点从原方法的闭合 `}` 开始,因此这里只替换方法体,不重复插入闭合括号。
- let authorize_replacement = "\n\t\treturn true;";
-
- let mut patched = source.to_string();
- patched.replace_range(
- authorize_start + AUTHORIZE_ANCHOR.len()..authorize_end,
- authorize_replacement,
- );
- patched = patched.replacen(REJECTION_ANCHOR, REJECTION_PATCHED, 1);
+ let patched = source
+ .replacen(STARTUP_OPTION_ANCHOR, STARTUP_OPTION_REPLACEMENT, 1)
+ .replacen(STARTUP_ACTION_ANCHOR, STARTUP_ACTION_REPLACEMENT, 1);
PatchOutcome::Patched(patched)
}
-/// 对活动核心的 alpha `dsh-client-connection` 应用桌面嵌入鉴权补丁。
-/// 文件缺失或任一锚点变化时安全跳过,不阻断启动。
+/// 替换 alpha 的 connection 鉴权:仅在 `DSH_SKIP_AUTH=1` 时跳过 browser-session,
+/// 始终保留 Host/Origin trust fence;未设置时行为与上游一致。
+fn patch_connection(source: &str) -> PatchOutcome {
+ if source.contains(PATCH_MARKER) {
+ return PatchOutcome::AlreadyPatched;
+ }
+ if !source.contains(REJECTION_ANCHOR) || !source.contains(AUTHORIZE_ANCHOR) {
+ return PatchOutcome::AnchorMissing;
+ }
+
+ let patched = source
+ .replacen(REJECTION_ANCHOR, REJECTION_PATCHED, 1)
+ .replacen(AUTHORIZE_ANCHOR, AUTHORIZE_PATCHED, 1);
+ PatchOutcome::Patched(patched)
+}
+
+/// 对活动核心应用「`--skip-auth` 可选鉴权」补丁。目标缺失或任一锚点变化时安全
+/// 跳过,不阻断启动。
pub fn apply(app_handle: &tauri::AppHandle) -> Result<(), String> {
- patch_dsh(app_handle, CLIENT_CONNECTION_INDEX_JS, patch_source)
+ patch_dsh(app_handle, WEB_STARTUP_REL, patch_startup)?;
+ patch_dsh(app_handle, CONNECTION_INDEX_JS, patch_connection)?;
+ Ok(())
+}
+
+/// 活动核心是否已具备完整的 `--skip-auth` 支持(选项层与 connection 层都已命中,
+/// 或上游官方合并)。
+///
+/// 供 launch 判定是否往服务参数追加 `--skip-auth`:必须两层都生效才传,否则只传
+/// 了选项、connection 层不认,会造成「看似跳过、实则仍拦」的半吊子状态;同时避免
+/// 旧核心把未知选项当作错误而直接退出。
+pub fn web_startup_supports_skip_auth(app_handle: &tauri::AppHandle) -> bool {
+ dsh_rel_contains(app_handle, WEB_STARTUP_REL, "--skip-auth")
+ && dsh_rel_contains(app_handle, CONNECTION_INDEX_JS, "DSH_SKIP_AUTH")
}
#[cfg(test)]
mod tests {
use super::*;
- fn alpha_fixture() -> String {
- format!(
- "{AUTHORIZE_ANCHOR}\n\t\tconst url = new URL(req.url ?? \"/\", \"http://dsh.invalid\");\n\t\tthis.writeUnauthorized(req, res);\n\t}}\n\t/**\n\t* Verify the authority-bound browser cookie\n{REJECTION_ANCHOR}\n{ALPHA_AUTH_ANCHOR}\n"
- )
+ fn connection_fixture() -> String {
+ let mut source = String::new();
+ source.push_str("\trequestRejection(request) {\n");
+ source.push_str("\t\tif (!isTrustedApiRequest(request, this.trustedHosts)) return 403;\n");
+ source.push_str("\t\treturn this.browserAuth.isAuthenticated(request) ? void 0 : 401;\n");
+ source.push_str("\t}\n");
+ source.push_str("\t/** Authenticate an index request through the process-token exchange or cookie. */\n");
+ source.push_str(AUTHORIZE_ANCHOR);
+ source.push_str("\n\tdsh web authentication required; reopen the URL printed by dsh web.\n");
+ source
+ }
+
+ fn startup_fixture() -> String {
+ let mut source = String::new();
+ source.push_str("function webCommand() {\n");
+ source.push_str(
+ "\treturn new Command().name(\"dsh --profile web\").description(\"Serve the DeepSeek Harness browser UI.\").helpOption(\"-h, --help\", \"show this help\").option(\"--host <host>\", \"bind host\").option(\"--no-open\", \"do not open the Web UI in the default browser\").option(\"--port <port>\", \"listen port; pass 0 to let the OS pick a free one\").option(\"--trusted-host <authority...>\", \"extra authority the /api browser-trust fence accepts (host or host:port; repeatable)\").addHelpText(\"after\", `\nExamples:\n`);\n",
+ );
+ source.push_str("}\n\n");
+ source.push_str("function apply(ctx) {\n");
+ source.push_str("\tconst program = webCommand();\n");
+ source.push_str("\tprogram.action(() => {\n");
+ source.push_str(STARTUP_ACTION_ANCHOR);
+ source.push_str("\n\t});\n");
+ source.push_str("}\n");
+ source
}
#[test]
- fn patches_alpha_index_and_api_auth_but_keeps_trust_fence() {
- let PatchOutcome::Patched(patched) = patch_source(&alpha_fixture()) else {
- panic!("expected alpha patch");
+ fn connection_patch_keeps_trust_fence_and_gates_browser_session() {
+ let PatchOutcome::Patched(patched) = patch_connection(&connection_fixture()) else {
+ panic!("expected connection patch");
};
assert!(patched.contains(PATCH_MARKER));
- assert!(patched.contains("authorizeIndex(req, res) {\n\t\treturn true;"));
+ // 信任边界保留。
assert!(
patched.contains("if (!isTrustedApiRequest(request, this.trustedHosts)) return 403;")
);
+ // 命中开关时不返回 401。
+ assert!(patched.contains("if (process.env.DSH_SKIP_AUTH === \"1\") return void 0;"));
+ // 未命中时仍走原鉴权。
+ assert!(patched.contains("this.browserAuth.isAuthenticated(request) ? void 0 : 401"));
+ // 未命中 index 仍交给上游。
assert!(patched
- .contains("return void 0;\n\t} /* dsh-tauri-desktop: alpha embedded auth bypass */"));
- assert!(!patched.contains("this.browserAuth.isAuthenticated(request) ? void 0 : 401"));
+ .contains("if (process.env.DSH_SKIP_AUTH === \"1\") return true;\n\t\treturn this.browserAuth.authorizeIndex(request, response);"));
}
#[test]
- fn patch_is_idempotent() {
- let PatchOutcome::Patched(patched) = patch_source(&alpha_fixture()) else {
- panic!("expected alpha patch");
+ fn startup_patch_adds_option_and_env_handoff() {
+ let PatchOutcome::Patched(patched) = patch_startup(&startup_fixture()) else {
+ panic!("expected startup patch");
};
- assert_eq!(patch_source(&patched), PatchOutcome::AlreadyPatched);
+ assert!(patched.contains(PATCH_MARKER));
+ assert!(patched.contains(".option(\"--skip-auth\", \"skip the browser-session token/cookie exchange; keeps the Host/Origin trust fence (for embedded UIs)\")"));
+ assert!(patched
+ .contains("\t\tif (options.skipAuth) process.env.DSH_SKIP_AUTH = \"1\";"));
+ }
+
+ #[test]
+ fn patches_are_idempotent() {
+ let PatchOutcome::Patched(startup) = patch_startup(&startup_fixture()) else {
+ panic!("expected startup patch");
+ };
+ assert_eq!(patch_startup(&startup), PatchOutcome::AlreadyPatched);
+
+ let PatchOutcome::Patched(connection) = patch_connection(&connection_fixture()) else {
+ panic!("expected connection patch");
+ };
+ assert_eq!(patch_connection(&connection), PatchOutcome::AlreadyPatched);
}
#[test]
fn legacy_or_changed_layout_is_untouched() {
assert_eq!(
- patch_source("authorizeIndex(req, res) {}"),
+ patch_connection("requestRejection(request) { return 401; }"),
PatchOutcome::AnchorMissing
);
- let mut partial = alpha_fixture();
- partial = partial.replace(ALPHA_AUTH_ANCHOR, "legacy web page");
- assert_eq!(patch_source(&partial), PatchOutcome::AnchorMissing);
- let partial = alpha_fixture().replace(
+ assert_eq!(patch_startup("no web command here"), PatchOutcome::AnchorMissing);
+
+ let partial = startup_fixture().replace(
+ STARTUP_ACTION_ANCHOR,
+ "\t\tconst options = program.opts();\n\t\tconst allowLan = 0;",
+ );
+ assert_eq!(patch_startup(&partial), PatchOutcome::AnchorMissing);
+
+ let partial = connection_fixture().replace(
REJECTION_ANCHOR,
"requestRejection(request) { return 401; }",
);
- assert_eq!(patch_source(&partial), PatchOutcome::AnchorMissing);
+ assert_eq!(patch_connection(&partial), PatchOutcome::AnchorMissing);
}
}
diff --git a/src-tauri/src/service/workflow/launch.rs b/src-tauri/src/service/workflow/launch.rs
index 997415df..2446a71a 100644
--- a/src-tauri/src/service/workflow/launch.rs
+++ b/src-tauri/src/service/workflow/launch.rs
@@ -350,11 +350,12 @@ pub async fn launch(app_handle: tauri::AppHandle) -> Result<(), String> {
log::warn!("win32 terminal support apply failed: {e}");
}
// alpha 的 iframe 无法稳定完成 SameSite=Strict browser-session Cookie 交换:
- // 仅命中 alpha 鉴权锚点时跳过 browser-session 层,但保留 Host/Origin fence;
- // 旧核心无 alpha 锚点,patch_dsh 会安全跳过,不改变旧版行为。
- // if let Err(e) = crate::service::patch::alpha_auth::apply(&app_handle) {
- // log::warn!("alpha embedded auth patch failed: {e}");
- // }
+ // 补丁让 dsh 接受 `--skip-auth`,仅在桌面端显式传该标志时跳过 browser-session
+ // 层(保留 Host/Origin fence);普通 `dsh web` 不受影响。旧核心无锚点时
+ // patch_dsh 安全跳过,不改变旧版行为。
+ if let Err(e) = crate::service::patch::alpha_auth::apply(&app_handle) {
+ log::warn!("alpha --skip-auth patch failed: {e}");
+ }
// renderer 的 SlotOutlet 一行导出补丁(dsh-tauri-ui 设置侧边栏依赖):只补
// 活动核心的 dsh-client-ui-renderer lib/client.js,已含导出即跳过(幂等;核心
// 换版本后自动重打,上游官方导出后自动退休)。最佳努力:失败只告警,不阻断
@@ -496,6 +497,14 @@ pub async fn launch(app_handle: tauri::AppHandle) -> Result<(), String> {
// 浏览器,追加 `--no-open` 关闭(老版本无此标志时按版本判定不传)。
let no_open = web_supports_no_open_flag(&app_handle, &dsh_binary_path);
+ // 版本判定打不到 alpha 的 web-startup 选项表(见 web_supports_no_open_flag)。
+ // alpha 的浏览器会话 Cookie 在沙箱跨源 iframe 上下文无法完成交换,因此桌面端
+ // 显式追加 `--skip-auth`:只有核心(经上面的 alpha_auth 补丁,或上游官方合并)
+ // 确实支持该标志才传,避免旧核心把未知选项当成错误退出。
+ let skip_auth = crate::service::patch::alpha_auth::web_startup_supports_skip_auth(
+ &app_handle,
+ );
+
log::info!("Starting Harness process");
// dsh 的 Loader 在插件 dispose 时会把组合后的整棵 entry 树回写进
@@ -533,6 +542,9 @@ pub async fn launch(app_handle: tauri::AppHandle) -> Result<(), String> {
if no_open {
args.push(OsString::from("--no-open"));
}
+ if skip_auth {
+ args.push(OsString::from("--skip-auth"));
+ }
// 只负责 spawn 并返回管道/PID/句柄:探测与重试期间不登记、不挂
// 监视线程——只有最终采用的那个进程才登记,否则旧监视线程会通过
@@ -645,6 +657,9 @@ pub async fn launch(app_handle: tauri::AppHandle) -> Result<(), String> {
if no_open {
cmd.arg("--no-open");
}
+ if skip_auth {
+ cmd.arg("--skip-auth");
+ }
cmd.envs(&envs)
.current_dir(config::get_dsh_install_path(&app_handle))
// 核心修正:提供一个空的 stdin 防止 setRawMode 报错
diff --git a/src-tauri/src/utils/mod.rs b/src-tauri/src/utils/mod.rs
index ab66eb44..f9a84622 100644
--- a/src-tauri/src/utils/mod.rs
+++ b/src-tauri/src/utils/mod.rs
@@ -66,6 +66,19 @@ pub fn patch_dsh(
Ok(())
}
+/// 判定活动核心安装目录下的某个 dsh 包文件是否包含给定子串。
+///
+/// 用于「按能力追加启动参数」:例如 web 启动命令已具备 `--skip-auth`(本工具已打
+/// 过补丁或上游官方合并)才向服务参数追加该标志。目标不存在或读取失败一律视为
+/// 不包含,调用方据此保守不传标志。
+pub fn dsh_rel_contains(app_handle: &tauri::AppHandle, rel_path: &str, needle: &str) -> bool {
+ let target = active_core_install_dir(app_handle).join(rel_path);
+ match std::fs::read_to_string(&target) {
+ Ok(content) => content.contains(needle),
+ Err(_) => false,
+ }
+}
+
pub fn show_window<R: Runtime>(window: &WebviewWindow<R>) {
let _ = window.unminimize();
let _ = window.show();