Skip to content

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

fix(pet): address CodeRabbit review — cmd allowlist, focus/geometry hardening, settings a11y + rejection handling

hairyf0bb1bd7f9dbc
diff --git a/packages/dsh-tauri-pet/src/client/components/pet-settings.tsx b/packages/dsh-tauri-pet/src/client/components/pet-settings.tsx
index b70db9f4..79a7ef68 100644
--- a/packages/dsh-tauri-pet/src/client/components/pet-settings.tsx
+++ b/packages/dsh-tauri-pet/src/client/components/pet-settings.tsx
@@ -44,6 +44,9 @@ export function PetSettings(_props: PetSettingsProps) {
try {
setPetStatus(await setPetEnabled(!enabled))
}
+ catch (error) {
+ console.error('[dsh-tauri-pet] setPetEnabled failed:', error)
+ }
finally {
setBusy(false)
}
@@ -56,6 +59,9 @@ export function PetSettings(_props: PetSettingsProps) {
try {
setPetStatus(await setActivePet(id))
}
+ catch (error) {
+ console.error('[dsh-tauri-pet] setActivePet failed:', error)
+ }
finally {
setBusy(false)
}
@@ -65,7 +71,7 @@ export function PetSettings(_props: PetSettingsProps) {
<div className="dshpet-settings" role="dialog" aria-modal="false" aria-label={text('name')}>
<header className="dshpet-settingsHead">
<h2 className="dshpet-settingsTitle">{text('name')}</h2>
- <button type="button" className="dshpet-settingsClose" aria-label={text('disable')} onClick={() => closePetSettings()}>×</button>
+ <button type="button" className="dshpet-settingsClose" aria-label={text('close')} onClick={() => closePetSettings()}>×</button>
</header>
<div className="dshpet-settingsBody">
<section className="dshpet-card">
diff --git a/packages/dsh-tauri-pet/src/client/locales/index.ts b/packages/dsh-tauri-pet/src/client/locales/index.ts
index 1a2d2244..4fdd7626 100644
--- a/packages/dsh-tauri-pet/src/client/locales/index.ts
+++ b/packages/dsh-tauri-pet/src/client/locales/index.ts
@@ -16,6 +16,7 @@ const DICT_ZH: Record<LocaleKey, string> = {
disable: '停用',
enabled: '已启用',
disabled: '已停用',
+ close: '关闭',
selectPet: '选择宠物',
show: '显示',
hide: '隐藏',
@@ -37,6 +38,7 @@ const DICT_EN: Record<LocaleKey, string> = {
disable: 'Disable',
enabled: 'Enabled',
disabled: 'Disabled',
+ close: 'Close',
selectPet: 'Select pet',
show: 'Show',
hide: 'Hide',
diff --git a/packages/dsh-tauri-pet/src/client/types/index.ts b/packages/dsh-tauri-pet/src/client/types/index.ts
index 9fb5a964..d8aa06f6 100644
--- a/packages/dsh-tauri-pet/src/client/types/index.ts
+++ b/packages/dsh-tauri-pet/src/client/types/index.ts
@@ -13,6 +13,7 @@ export type LocaleKey
| 'disable'
| 'enabled'
| 'disabled'
+ | 'close'
| 'selectPet'
| 'show'
| 'hide'
diff --git a/src-tauri/src/desktop/pet.rs b/src-tauri/src/desktop/pet.rs
index 1a2a4bbe..e1b13bdc 100644
--- a/src-tauri/src/desktop/pet.rs
+++ b/src-tauri/src/desktop/pet.rs
@@ -139,8 +139,14 @@ pub fn ensure_pet_window<R: Runtime>(app: &AppHandle<R>) -> tauri::Result<Webvie
let app_for_pos = app_handle.clone();
let saved = get_pet_window_position(&app_for_pos);
if let (Some(x), Some(y)) = (saved.x, saved.y) {
- // 恢复的位置必须落在某个可见屏幕内,否则回退默认定位。
- let _ = window.set_position(tauri::Position::Physical(PhysicalPosition::new(x, y)));
+ // 恢复的位置必须落在某个可见屏幕内,否则回退默认定位(防止显示器
+ // 拓扑变化后窗口被放到屏幕外不可见)。
+ if position_on_any_monitor(&window, x, y) {
+ let _ = window.set_position(tauri::Position::Physical(PhysicalPosition::new(x, y)));
+ }
+ else {
+ place_pet_at_default(&window);
+ }
}
else {
place_pet_at_default(&window);
@@ -148,6 +154,32 @@ pub fn ensure_pet_window<R: Runtime>(app: &AppHandle<R>) -> tauri::Result<Webvie
Ok(window)
}
+/// 判断给定物理坐标(窗口左上角)是否落在任一可见屏幕内(含边缘相交)。
+///
+/// 显示器拓扑可能在保存位置后变化(拔掉外接屏 / 改变排布),恢复到一个不
+/// 属于任何屏幕的位置会让桌宠「消失」在屏幕外;这里只在原位置仍有效时恢复。
+fn position_on_any_monitor<R: Runtime>(window: &WebviewWindow<R>, x: i32, y: i32) -> bool {
+ let app = window.app_handle();
+ let Ok(monitors) = app.available_monitors() else {
+ return false;
+};
+ // 以窗口自身的物理尺寸近似命中矩形,允许窗口底部/右侧探出一点点也不误判。
+ let w = (PET_WINDOW_WIDTH * window.scale_factor().unwrap_or(1.0)) as i32;
+ let h = (PET_WINDOW_HEIGHT * window.scale_factor().unwrap_or(1.0)) as i32;
+ let hit_left = x;
+ let hit_top = y;
+ let hit_right = x + w;
+ let hit_bottom = y + h;
+ monitors.iter().any(|m| {
+ let pos = m.position();
+ let size = m.size();
+ let (ml, mt) = (pos.x, pos.y);
+ let (mr, mb) = (pos.x + size.width as i32, pos.y + size.height as i32);
+ // 两个矩形至少有一个点的交集:窗口左上角在屏内,或屏被窗口覆盖。
+ hit_left < mr && hit_right > ml && hit_top < mb && hit_bottom > mt
+ })
+}
+
/// 把桌宠窗口放到主工作区右下角略偏上(主屏内、避开底部任务栏高度)。
fn place_pet_at_default<R: Runtime>(window: &WebviewWindow<R>) {
let app = window.app_handle();
@@ -179,7 +211,7 @@ pub fn sync_pet_window<R: Runtime>(app: &AppHandle<R>) {
if ensure_pet_window(app).is_ok() {
if let Some(window) = app.get_webview_window(PET_WINDOW_LABEL) {
let _ = window.show();
- let _ = window.set_focus();
+ // 刻意不 set_focus():桌宠不应抢占用户当前应用的焦点(非打断性浮现)。
}
}
}
diff --git a/src/hooks/use-iframe-invoke.ts b/src/hooks/use-iframe-invoke.ts
index 5b7c55f6..45aab396 100644
--- a/src/hooks/use-iframe-invoke.ts
+++ b/src/hooks/use-iframe-invoke.ts
@@ -29,6 +29,19 @@ interface InvokeBridgeRequest {
nonce?: string
}
+/**
+ * 允许 iframe 桥调用的 Tauri command 白名单(与 dsh-tauri-pet 的
+ * service/pet.ts 一一对应)。凡新增可经桥调用的 command 必须在此登记,
+ * 防止 iframe 内其他插件借道桥执行任意 Tauri command(越权)。
+ */
+const ALLOWED_INVOKE_CMDS = new Set([
+ 'get_pet_status',
+ 'set_pet_enabled',
+ 'set_active_pet',
+ 'show_pet',
+ 'hide_pet',
+])
+
export function useIframeInvoke(iframeRef: RefObject<HTMLIFrameElement | null>): void {
function handleMessage(event: MessageEvent<InvokeBridgeRequest>) {
const data = event.data
@@ -46,6 +59,11 @@ export function useIframeInvoke(iframeRef: RefObject<HTMLIFrameElement | null>):
if (data.type !== 'dsh://tauri:invoke' || !data.cmd) {
return
}
+ // 仅在白名单内的 command 允许调用,其余静默忽略(防越权)。
+ if (!ALLOWED_INVOKE_CMDS.has(data.cmd)) {
+ console.warn(`[iframe-invoke] ignored non-allowlisted cmd: ${data.cmd}`)
+ return
+ }
const cmd = data.cmd
const args = data.args
const nonce = data.nonce ?? ''