Merge pull request '支持从加密聊天打开隔离浏览器链接' (#15) from xhgit/chat-browser-links into main

Reviewed-on: http://git.xhmcl.com/xhgit/XHChat/pulls/15
This commit was merged in pull request #15.
This commit is contained in:
xhgit committed 2026-11-14 00:12:35 +00:00
commit 759f28bae2
8 files changed
+54 -8

No files matched your search

+2 -2
View File
@@ -218,7 +218,7 @@ function configureSites(sites: Session, privateMode: boolean) {
})
}
export function openBrowser(trusted: Session, privateMode = false) {
export function openBrowser(trusted: Session, privateMode = false, initial?: URL) {
const sites = session.fromPartition(privateMode ? 'xhchat-private-' + randomUUID() : 'persist:xhchat-sites')
configureSites(sites, privateMode)
const window = new BrowserWindow({
@@ -244,7 +244,7 @@ export function openBrowser(trusted: Session, privateMode = false) {
for (const tab of browser.tabs) tab.view.webContents.close()
if (privateMode) void sites.clearData().catch(() => {})
})
window.webContents.on('did-finish-load', () => addTab(browser))
window.webContents.once('did-finish-load', () => addTab(browser, initial))
void window.loadURL(browserURL).catch(() => {
if (!window.isDestroyed()) window.close()
})
+5 -3
View File
@@ -2,7 +2,7 @@ import { app, BrowserWindow, ipcMain, protocol, safeStorage, session } from 'ele
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { signChallenge, signMls } from './identity'
import { openBrowser, registerBrowserHandlers } from './browser'
import { openBrowser, registerBrowserHandlers, website } from './browser'
const appURL = 'xhchat://app/'
app.enableSandbox()
@@ -102,9 +102,11 @@ async function start() {
return key
})
registerBrowserHandlers(trusted)
ipcMain.handle('browser:open', (event) => {
ipcMain.handle('browser:open', (event, url: unknown) => {
if (!trustedSender(event)) throw new Error('浏览器入口来源无效')
openBrowser(trusted)
const initial = typeof url === 'string' && url.length <= 4096 ? website(url) : undefined
if (url !== undefined && !initial) throw new Error('网页地址无效')
openBrowser(trusted, false, initial)
})
await win.loadURL(appURL)
}
+1 -1
View File
@@ -5,7 +5,7 @@ const apiArgument = process.argv.find((value) => value.startsWith('--xhchat-api=
contextBridge.exposeInMainWorld('xhchat', {
apiBase: apiArgument?.slice('--xhchat-api='.length),
signChallenge: (username: string, challenge: string) => ipcRenderer.invoke('device:sign', username, challenge),
openBrowser: () => ipcRenderer.invoke('browser:open'),
openBrowser: (url?: string) => ipcRenderer.invoke('browser:open', url),
signMls: (username: string, kind: string, user: string, device: string, value: Uint8Array) =>
ipcRenderer.invoke('device:sign-mls', username, kind, user, device, value),
protectMls: (key: string) => ipcRenderer.invoke('device:protect-mls', key),
+4
View File
@@ -72,3 +72,7 @@ Windows 浏览器新增可跨窗口保留的本地书签和历史,每类最多
## 浏览器逐站权限
普通窗口可按当前网站的协议、主机与端口单独授权或撤销摄像头、麦克风、位置、通知和剪贴板权限。默认拒绝,授权只用于可见的顶级网页。隐私窗口不继承授权。Electron 集成测试通过 Chromium 权限查询验证通知权限默认拒绝、显式授权和撤销。
## 聊天中的链接
加密文字消息中的 HTTPS 链接在端侧识别。Windows 从链接启动隔离 Chromium,Web 在独立标签页打开并隔离 opener。开发环境支持本机 HTTP。真实双端测试验证消息链接点击与 Chromium 地址显示。网页卡片与从浏览器分享给联系人仍待实现。
+11
View File
@@ -193,6 +193,17 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢
} finally { db.close() }
})
assert.deepEqual(stored, [{ wrapped: true, encrypted: true, fields: ['iv', 'key', 'sealed'] }])
const link = base + '/healthz'
await send(web, link)
const webLink = web.getByRole('link', { name: link, exact: true })
assert.equal(await webLink.getAttribute('href'), link)
assert.equal(await webLink.getAttribute('rel'), 'noopener noreferrer')
const windowOpened = desktop.waitForEvent('window')
await windows.getByRole('button', { name: link, exact: true }).click()
const toolbar = await windowOpened
const address = toolbar.getByRole('textbox', { name: '网址或搜索关键词' })
await toolbar.waitForFunction((expected) => document.querySelector('#address')?.value === expected, link)
assert.equal(await address.inputValue(), link)
} finally {
await desktop?.close()
await browser.close()
+1 -1
View File
@@ -6,7 +6,7 @@ interface DeviceProof {
interface DesktopBridge {
apiBase: string
signChallenge: (username: string, challenge: string) => Promise<DeviceProof>
openBrowser?: () => Promise<void>
openBrowser?: (url?: string) => Promise<void>
signMls: (username: string, kind: 'credential' | 'KeyPackage', user: string, device: string, value: Uint8Array<ArrayBuffer>) => Promise<Uint8Array<ArrayBuffer>>
protectMls: (key: string) => Promise<string>
unprotectMls: (key: string) => Promise<string>
+1
View File
@@ -92,6 +92,7 @@ input { width: 100%; color: var(--text); background: var(--base); border: 1px so
.chat-lines blockquote { border-left: 2px solid var(--line); margin: 0 0 4px; padding: 0 6px; color: var(--muted); font-size: .9em; }
.message-actions { margin-top: 5px; }
.message-actions .plain { font-size: .78em; padding: 2px 6px; }
.chat-link { border: 0; background: transparent; color: var(--green); text-decoration: underline; padding: 0; overflow-wrap: anywhere; font: inherit; }
.account-footer { display: grid; gap: 12px; margin-top: auto; }
.account-footer p { color: var(--muted); font-size: 13px; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); margin: 0 8px 1px 4px; }
+29 -1
View File
@@ -38,6 +38,31 @@ const lines = computed(() => {
(!favoritesOnly.value || line.favorite) &&
(!query || [line.text, line.attachment?.name, line.quote].some((part) => part?.toLowerCase().includes(query))))
})
const internalBrowser = window.xhchat?.openBrowser
function messageParts(text: string) {
const parts: { text: string; url?: string }[] = []
let offset = 0
for (const match of text.matchAll(/https?:\/\/[^\s<>"']+/giu)) {
const label = match[0].replace(/[.,!?;:,。!?;:、))\]】]+$/u, '')
try {
const url = new URL(label)
if (url.username || url.password || label.length > 4096 ||
(url.protocol !== 'https:' && !['127.0.0.1', 'localhost', '[::1]'].includes(url.hostname))) continue
if (match.index > offset) parts.push({ text: text.slice(offset, match.index) })
parts.push({ text: label, url: url.href })
offset = match.index + label.length
} catch {
continue
}
}
if (offset < text.length) parts.push({ text: text.slice(offset) })
return parts
}
function openLink(url: string) {
if (internalBrowser) void execute(() => internalBrowser(url))
}
let timer: ReturnType<typeof setInterval>
async function execute(action: () => Promise<void>) {
@@ -235,7 +260,10 @@ onBeforeUnmount(() => clearInterval(timer))
<span v-if="line.recalled" class="muted">消息已撤回</span>
<button v-else-if="line.attachment" type="button" class="plain" :disabled="busy"
@click="saveAttachment(line.attachment)">下载 {{ line.attachment.name }}</button>
<span v-else>{{ line.text }}</span><small v-if="view?.unsent.includes(line.id)">待发送</small>
<span v-else><template v-for="(part, index) in messageParts(line.text)" :key="index"><button v-if="part.url && internalBrowser"
type="button" class="chat-link" @click="openLink(part.url)">{{ part.text }}</button><a v-else-if="part.url"
class="chat-link" :href="part.url" target="_blank" rel="noopener noreferrer">{{ part.text }}</a><template v-else>{{ part.text }}</template></template></span>
<small v-if="view?.unsent.includes(line.id)">待发送</small>
<small v-if="line.edited && !line.recalled">已编辑</small>
<div class="message-actions">
<button v-if="!line.recalled" type="button" class="plain" :disabled="busy || Boolean(editing)" @click="reply = line">引用</button>