Skip to content

fix(shellBridge): validate URL before openExternal to prevent Invalid URL errors#1767

Merged
piorpua merged 1 commit intomainfrom
fix/sentry-ELECTRON-6Z
Mar 27, 2026
Merged

fix(shellBridge): validate URL before openExternal to prevent Invalid URL errors#1767
piorpua merged 1 commit intomainfrom
fix/sentry-ELECTRON-6Z

Conversation

@kaizhou-lab
Copy link
Copy Markdown
Collaborator

Summary

  • Validate URL with new URL(url) before calling shell.openExternal() in both shellBridge.ts and shellBridgeStandalone.ts
  • Invalid URLs log a warning and resolve gracefully instead of causing unhandled promise rejections
  • Add comprehensive unit tests for both shellBridge variants

Closes #1766

Sentry Issue

ELECTRON-6Z — 635 occurrences, last seen 3 hours ago

Error: Error: Invalid URL
Culprit: src/process/bridge/shellBridge.ts:21 (shell.openExternal(url))

Verification

  • Unit tests pass (14/14) covering valid URLs, invalid URLs, and empty strings
  • Lint: 0 errors
  • Format: clean
  • Type check: clean (tsc --noEmit)

Test plan

  • Verify openExternal works normally with valid URLs (https, http, mailto)
  • Verify invalid URLs are gracefully handled without crashing
  • Verify console warning is logged for invalid URLs

… Invalid URL errors

Add URL validation guard using `new URL()` in both shellBridge (Electron)
and shellBridgeStandalone (Node.js) before passing to openExternal.
Invalid URLs now log a warning and resolve gracefully instead of causing
unhandled promise rejections.

Fixes ELECTRON-6Z (635 occurrences)
@kaizhou-lab kaizhou-lab marked this pull request as ready for review March 27, 2026 02:22
@piorpua piorpua added the bot:reviewing Review in progress (mutex) label Mar 27, 2026
@piorpua
Copy link
Copy Markdown
Contributor

piorpua commented Mar 27, 2026

Code Review:fix(shellBridge): validate URL before openExternal to prevent Invalid URL errors (#1767)

变更概述

本 PR 修复 Sentry 高频错误 ELECTRON-6Z(635 次),原因是 shell.openExternal() 被传入无效 URL 导致 unhandled promise rejection。修改涉及 shellBridge.ts(Electron 模式)和 shellBridgeStandalone.ts(无 Electron 模式)两个文件,分别在调用 openExternal 前加入 new URL(url) 语法校验,并为两者新增单元测试。


方案评估

结论:✅ 方案合理

使用 new URL(url) 进行语法校验是 JS 标准做法,能精确复现 Sentry 报错路径。在两处调用点同时修复,保持了两个 bridge 实现的一致性。修复粒度适当,未引入额外抽象。需注意 new URL() 仅做语法校验(javascript:alert(1) 也能通过),但 URL scheme 安全性并非本次 PR 的目标,且原代码中已存在此前置条件。


问题清单

🔵 LOW — no-new lint 警告(生产代码)

文件src/process/bridge/shellBridge.ts,第 22 行;src/process/bridge/shellBridgeStandalone.ts,第 40 行

问题代码

new URL(url);

问题说明:oxlint 报 eslint(no-new) 警告——new 仅为副作用使用,未赋值给变量。虽然 CI Code Quality 以 0 errors 通过(警告不阻断 CI),但属于代码规范问题。

修复建议

// 方案 A(当前 Node.js 版本支持时推荐)
if (!URL.canParse(url)) {
  console.warn(`[shellBridge] Invalid URL passed to openExternal: ${url}`);
  return Promise.resolve();
}

🔵 LOW — 测试文件中的 no-explicit-any 警告

文件tests/unit/shellBridge.test.ts,第 12–36 行

问题说明:新测试文件中 any 共 12 处,此模式来自现有 shellBridgeStandalone.test.ts(历史技术债),新文件直接复制沿用。从本次 PR 角度这是已有模式,不作阻塞点,建议后续统一替换为具体类型或 unknown


汇总

# 严重级别 文件 问题
1 🔵 LOW shellBridge.ts:22shellBridgeStandalone.ts:40 no-new lint 警告,new URL() 未赋值
2 🔵 LOW shellBridge.test.ts:12-36 no-explicit-any 警告,复制自现有模式

结论

批准合并 — 修复逻辑正确,直接解决 Sentry 高频报错,两种 bridge 实现均已覆盖,测试完备(14 个用例全部通过,涵盖合法 URL、非法 URL、空字符串三种场景)。两处 LOW 级警告均不阻断 CI。


本报告由本地 pr-review skill 生成,包含完整项目上下文,无截断限制。

@piorpua
Copy link
Copy Markdown
Contributor

piorpua commented Mar 27, 2026

✅ 已自动 review,无阻塞性问题,正在触发自动合并。

@piorpua piorpua merged commit 7eabfac into main Mar 27, 2026
15 of 17 checks passed
@piorpua piorpua deleted the fix/sentry-ELECTRON-6Z branch March 27, 2026 02:34
@piorpua piorpua added bot:done Auto-merged by bot and removed bot:reviewing Review in progress (mutex) labels Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:done Auto-merged by bot

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(shellBridge): Invalid URL causes unhandled rejection in openExternal

2 participants