Skip to content

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

Add alpha embedded authentication patch

hairyfc8077b09a78a
diff --git a/src-tauri/src/service/patch/alpha_auth.rs b/src-tauri/src/service/patch/alpha_auth.rs
new file mode 100644
index 00000000..394632cb
--- /dev/null
+++ b/src-tauri/src/service/patch/alpha_auth.rs
@@ -0,0 +1,109 @@
+//! alpha 核心的桌面嵌入鉴权兼容补丁。
+//!
+//! alpha 的 `dsh-client-connection` 默认要求浏览器先用启动 token 换取
+//! `SameSite=Strict` Cookie。桌面端通过跨上下文 iframe 承载 Web UI,无法稳定
+//! 完成该 Cookie 交换,因此仅在命中 alpha 专有锚点时跳过 browser-session 层。
+//! Host/Origin/loopback 信任边界仍由 `isTrustedApiRequest` 保留。
+
+use crate::utils::{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";
+
+// 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";
+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 */";
+
+/// 替换 alpha 的 index 与 API browser-session 鉴权,同时保留 Host/Origin fence。
+fn patch_source(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)
+ {
+ 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);
+ PatchOutcome::Patched(patched)
+}
+
+/// 对活动核心的 alpha `dsh-client-connection` 应用桌面嵌入鉴权补丁。
+/// 文件缺失或任一锚点变化时安全跳过,不阻断启动。
+pub fn apply(app_handle: &tauri::AppHandle) -> Result<(), String> {
+ patch_dsh(app_handle, CLIENT_CONNECTION_INDEX_JS, patch_source)
+}
+
+#[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"
+ )
+ }
+
+ #[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");
+ };
+ 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;")
+ );
+ 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"));
+ }
+
+ #[test]
+ fn patch_is_idempotent() {
+ let PatchOutcome::Patched(patched) = patch_source(&alpha_fixture()) else {
+ panic!("expected alpha patch");
+ };
+ assert_eq!(patch_source(&patched), PatchOutcome::AlreadyPatched);
+ }
+
+ #[test]
+ fn legacy_or_changed_layout_is_untouched() {
+ assert_eq!(
+ patch_source("authorizeIndex(req, res) {}"),
+ 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(
+ REJECTION_ANCHOR,
+ "requestRejection(request) { return 401; }",
+ );
+ assert_eq!(patch_source(&partial), PatchOutcome::AnchorMissing);
+ }
+}
diff --git a/src-tauri/src/service/patch/client_hmr.rs b/src-tauri/src/service/patch/client_hmr.rs
index 34dd2f45..81683760 100644
--- a/src-tauri/src/service/patch/client_hmr.rs
+++ b/src-tauri/src/service/patch/client_hmr.rs
@@ -19,6 +19,9 @@ const PATCHED: &str = r#"case "rebuilt":
/* dsh-tauri-desktop: debug client plugin reload fallback */
window.location.reload();
break;"#;
+// 上游已恢复完整的 invalidate → refresh 流程时,无需桌面端降级补丁。
+const UPSTREAM_FIXED: &str = r#"modLoader.invalidate(id, rev);
+ await modLoader.prefetch(id);"#;
/// 相对活动核心安装目录的 client-hmr `lib/client.js` 包内路径。
const CLIENT_HMR_CLIENT_JS: &str = "node_modules/@deepseek-ai/dsh-client-hmr/lib/client.js";
@@ -27,6 +30,9 @@ fn patch_source(source: &str) -> PatchOutcome {
if source.contains(PATCH_MARKER) {
return PatchOutcome::AlreadyPatched;
}
+ if source.contains(UPSTREAM_FIXED) {
+ return PatchOutcome::AlreadyPatched;
+ }
if !source.contains(ORIGINAL) {
return PatchOutcome::AnchorMissing;
}
@@ -74,4 +80,10 @@ mod tests {
PatchOutcome::AnchorMissing
);
}
+
+ #[test]
+ fn accepts_upstream_fixed_reload_flow() {
+ let source = format!("{UPSTREAM_FIXED}");
+ assert_eq!(patch_source(&source), PatchOutcome::AlreadyPatched);
+ }
}
diff --git a/src-tauri/src/service/patch/mod.rs b/src-tauri/src/service/patch/mod.rs
index e9552bc4..69962ca8 100644
--- a/src-tauri/src/service/patch/mod.rs
+++ b/src-tauri/src/service/patch/mod.rs
@@ -8,6 +8,7 @@
//! 命名约定:子模块名不带 `_patch` 后缀(`renderer` / `session` / `workspace` /
//! `client_hmr`),挂点统一为 `service::workflow::launch`,均为最佳努力、失败仅告警。
+pub(crate) mod alpha_auth;
pub(crate) mod client_hmr;
pub(crate) mod renderer;
pub(crate) mod session;
diff --git a/src-tauri/src/service/workflow/launch.rs b/src-tauri/src/service/workflow/launch.rs
index 9c9ff9f7..6afb13f5 100644
--- a/src-tauri/src/service/workflow/launch.rs
+++ b/src-tauri/src/service/workflow/launch.rs
@@ -116,11 +116,30 @@ fn version_supports_no_open(version: &str) -> bool {
///
/// 版本以活动核心为准:本地核心(用户 CLI 安装)与预打包核心各自读自己的
/// 包清单;读不到时保守处理:不追加标志。
-fn web_supports_no_open_flag(app_handle: &tauri::AppHandle) -> bool {
- match crate::service::core::active_version(app_handle) {
- Some(version) => version_supports_no_open(&version),
- None => false,
- }
+fn dsh_binary_version(binary: &std::path::Path) -> Option<String> {
+ let package_dir = binary.parent()?.parent()?;
+ let manifest = package_dir.join("package.json");
+ let content = fs::read_to_string(manifest).ok()?;
+ serde_json::from_str::<serde_json::Value>(&content)
+ .ok()?
+ .get("version")
+ .and_then(|value| value.as_str())
+ .map(str::to_owned)
+}
+
+/// 按实际将要执行的 dsh `bin.js` 判定 `--no-open` 能力。
+///
+/// 核心切换槽位的外层 package.json 可能没有 `dependencies` 清单(源码构建
+/// alpha 尤其如此),因此不能只读取活动核心登记版本;必须读取入口所属的
+/// `@deepseek-ai/dsh/package.json`,避免 alpha 因版本读空而漏传参数。
+fn web_supports_no_open_flag(
+ app_handle: &tauri::AppHandle,
+ dsh_binary_path: &std::path::Path,
+) -> bool {
+ dsh_binary_version(dsh_binary_path)
+ .or_else(|| crate::service::core::active_version(app_handle))
+ .map(|version| version_supports_no_open(&version))
+ .unwrap_or(false)
}
/// 检测并启动 Harness 服务
@@ -220,7 +239,6 @@ pub async fn launch(app_handle: tauri::AppHandle) -> Result<(), String> {
return Ok(());
}
let _launch_guard = LaunchGuard;
-
// 只有持有启动守卫的这条路径清扫残留:并发启动的其它调用已在守卫处返回,
// 不会误杀刚拉起的进程。崩溃/强杀残留的孤儿 Harness 实例(不在
// .harness.pid 标记中)持续占用配置端口与 dependencies/dsh 的文件句柄,
@@ -292,6 +310,12 @@ pub async fn launch(app_handle: tauri::AppHandle) -> Result<(), String> {
if let Err(e) = win_inspector::apply(&app_handle) {
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}");
+ }
// renderer 的 SlotOutlet 一行导出补丁(dsh-tauri-ui 设置侧边栏依赖):只补
// 活动核心的 dsh-client-ui-renderer lib/client.js,已含导出即跳过(幂等;核心
// 换版本后自动重打,上游官方导出后自动退休)。最佳努力:失败只告警,不阻断
@@ -431,7 +455,7 @@ pub async fn launch(app_handle: tauri::AppHandle) -> Result<(), String> {
// rc.8 起 `dsh web` 默认在系统浏览器打开 UI;桌面端内嵌 WebView,不需要
// 浏览器,追加 `--no-open` 关闭(老版本无此标志时按版本判定不传)。
- let no_open = web_supports_no_open_flag(&app_handle);
+ let no_open = web_supports_no_open_flag(&app_handle, &dsh_binary_path);
log::info!("Starting Harness process");