Merge pull request '支持向已核验设备加密转发文字和附件' (#13) from xhgit/encrypted-forwarding into main

Reviewed-on: http://git.xhmcl.com/xhgit/XHChat/pulls/13
This commit was merged in pull request #13.
This commit is contained in:
xhgit committed 2026-11-14 00:12:19 +00:00
commit e12eaaf979
6 files changed
+81 -6

No files matched your search

+1 -1
View File
@@ -18,7 +18,7 @@
| 联系人 | 按用户名查找、处理好友申请、管理备注与分组,以及拉黑。变更可在双端实时同步。 |
| 加密文字聊天 | 在两个已核验设备之间发送 MLS 加密消息,支持离线接收。 |
| 加密附件 | 两端可发送和下载最多 8 MiB 的文件,下载时验证完整性。 |
| 消息管理 | 支持引用回复、编辑、撤回、搜索本机历史、收藏和本机删除。 |
| 消息管理 | 支持引用回复、编辑、撤回、转发、搜索本机历史、收藏和本机删除。 |
| Chromium 浏览器 | Windows 端支持独立窗口、多标签、导航、搜索、书签、历史、下载和隐私窗口,网页运行在独立会话中。 |
当前为开发预览。大文件分片、群聊、音视频和多设备历史同步仍在开发,Windows 安装包尚未发布。 [开发进度与验收记录](docs/progress.md)
+1 -1
View File
@@ -59,7 +59,7 @@ Windows 与 Web 支持发送和下载最多 8 MiB 的附件。文件名、长度
Windows 与 Web 可发送带引用摘要的 MLS 文字消息。两端支持按正文、引用和文件名搜索本机历史,以及收藏和本机删除。收藏标记与历史保存在加密本地记录中。集成测试覆盖引用跨端显示、收藏筛选、搜索及删除后另一端仍可读取原消息。
原发送设备可编辑文字及撤回文字或附件消息。更新作为 MLS 应用消息传递,由客户端核对原发送设备与消息 ID。撤回会更新对方客户端显示,先前保存的附件、截图和其他独立副本仍可能存在。转发与跨设备收藏同步待后续交付。
原发送设备可编辑文字及撤回文字或附件消息。更新作为 MLS 应用消息传递,由客户端核对原发送设备与消息 ID。撤回会更新对方客户端显示,先前保存的附件、截图和其他独立副本仍可能存在。文字与附件可转发给已核验设备。转发附件会在本机校验并用新的密钥和附件 ID 加密。跨设备收藏同步待后续交付。
## 浏览器书签和历史
+8
View File
@@ -157,6 +157,14 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢
const file = await downloaded
assert.equal(file.suggestedFilename(), fileName)
assert.deepEqual(await readFile(await file.path()), contents)
const forwardedRequest = web.waitForRequest((req) => req.method() === 'POST' && req.url().includes('/api/v1/attachments/'))
await web.getByRole('listitem').filter({ hasText: fileName }).getByRole('button', { name: '转发', exact: true }).click()
await web.getByLabel('转发给').selectOption({ label: b })
await web.getByRole('button', { name: '确认转发' }).click()
const forwarded = await forwardedRequest
assert.notEqual(new URL(forwarded.url()).pathname, fileURL.pathname)
assert.notDeepEqual(forwarded.postDataBuffer(), uploaded)
await windows.getByRole('button', { name: '下载 ' + fileName, exact: true }).nth(1).waitFor()
await windows.getByRole('button', { name: '退出登录' }).click()
await windows.getByRole('heading', { name: '登录 XHChat' }).waitFor()
+6 -1
View File
@@ -102,7 +102,7 @@ function encodeContent(kind: number, value: object) {
return message
}
export async function downloadAttachment(attachment: Attachment, call: Call) {
export async function readAttachment(attachment: Attachment, call: Call) {
const ciphertext = await call<ArrayBuffer>(attachmentPath(attachment.id), 'GET', undefined, true)
if (ciphertext.byteLength !== attachment.size + 16) throw new Error('附件完整性校验失败')
const raw = decode(attachment.key)
@@ -115,6 +115,11 @@ export async function downloadAttachment(attachment: Attachment, call: Call) {
} finally {
raw.fill(0)
}
return bytes
}
export async function downloadAttachment(attachment: Attachment, call: Call) {
const bytes = await readAttachment(attachment, call)
const url = URL.createObjectURL(new Blob([bytes], { type: 'application/octet-stream' }))
const link = document.createElement('a')
link.download = attachment.name
+8 -1
View File
@@ -3,7 +3,7 @@ import { mlsBinding, signMls } from '../identity'
import { base64, decode, fingerprint, loadEngine } from './engine'
import type { DeviceMls } from './engine'
import { withVault } from './vault'
import { decodeMessage, encodeAttachment, encodeMutation, encodeReply, sealAttachment, uploadAttachment } from './attachments'
import { decodeMessage, encodeAttachment, encodeMutation, encodeReply, readAttachment, sealAttachment, uploadAttachment } from './attachments'
import type { Attachment, Mutation } from './attachments'
export interface Peer {
@@ -259,6 +259,13 @@ export function localMessageAction(identity: ChatIdentity, line: ChatLine, actio
})
}
export async function forwardMessage(identity: ChatIdentity, line: ChatLine, recipient: string, device: Peer, call: Call) {
if (line.recalled) throw new Error('撤回的消息无法转发')
const attachment = line.attachment
const content = attachment ? new File([await readAttachment(attachment, call)], attachment.name) : line.text
await send(identity, recipient, device, content, call)
}
export function synchronize(identity: ChatIdentity, user: string, call: Call) {
return update(identity, async (state, save, factory) => {
const peers = await directory(call, user)
+57 -2
View File
@@ -2,8 +2,9 @@
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useSession } from '../session'
import { enableChat, localMessageAction, mutateMessage, send, synchronize, trust } from '../chat/chat'
import type { ChatLine } from '../chat/chat'
import { directory, enableChat, forwardMessage, localMessageAction, mutateMessage, send, synchronize, trust } from '../chat/chat'
import type { ChatLine, Peer } from '../chat/chat'
import type { Contact } from '../contacts'
import { downloadAttachment } from '../chat/attachments'
import type { Attachment } from '../chat/attachments'
@@ -19,6 +20,11 @@ const reply = ref<ChatLine>()
const deleting = ref<ChatLine>()
const editing = ref<ChatLine>()
const recalling = ref<ChatLine>()
const forwarding = ref<ChatLine>()
const destinations = ref<Contact[]>([])
const destination = ref('')
const destinationDevice = ref('')
const forwardDevices = ref<Peer[]>([])
const error = ref('')
const busy = ref(false)
const view = ref<Awaited<ReturnType<typeof synchronize>>>()
@@ -130,6 +136,37 @@ function localAction(line: ChatLine, action: 'favorite' | 'remove') {
})
}
function beginForward(line: ChatLine) {
void execute(async () => {
destinations.value = (await session.contacts()).filter((contact) => contact.state === 'friend')
destination.value = ''
destinationDevice.value = ''
forwardDevices.value = []
forwarding.value = line
})
}
function loadForwardDevices() {
destinationDevice.value = ''
forwardDevices.value = []
if (!destination.value) return
void execute(async () => {
const devices = await directory(session.authorized, destination.value)
forwardDevices.value = devices.filter((device) => device.mls && view.value?.pinned[device.id] === device.publicKey)
destinationDevice.value = forwardDevices.value[0]?.id ?? ''
})
}
function submitForward() {
void execute(async () => {
const device = forwardDevices.value.find((candidate) => candidate.id === destinationDevice.value)
if (!session.identity || !forwarding.value || !device) return
await forwardMessage(session.identity, forwarding.value, destination.value, device, session.authorized)
forwarding.value = undefined
await refresh()
})
}
function sync() {
if (ownCode.value) void execute(refresh)
}
@@ -148,6 +185,7 @@ watch(selected, () => {
deleting.value = undefined
editing.value = undefined
recalling.value = undefined
forwarding.value = undefined
draft.value = ''
})
watch(() => [session.inboxChanges, session.contactChanges, session.connection], sync)
@@ -204,6 +242,7 @@ onBeforeUnmount(() => clearInterval(timer))
<button v-if="!line.recalled" type="button" class="plain" :disabled="busy" @click="localAction(line, 'favorite')">
{{ line.favorite ? '取消收藏' : '收藏' }}
</button>
<button v-if="!line.recalled" type="button" class="plain" :disabled="busy" @click="beginForward(line)">转发</button>
<button v-if="line.own && !line.recalled && !line.attachment" type="button" class="plain" :disabled="busy" @click="edit(line)">编辑</button>
<button v-if="line.own && !line.recalled" type="button" class="plain" :disabled="busy" @click="recalling = line">撤回</button>
<button type="button" class="plain" :disabled="busy || view?.unsent.includes(line.id)" @click="deleting = line">本机删除</button>
@@ -221,6 +260,22 @@ onBeforeUnmount(() => clearInterval(timer))
<button type="button" class="plain" :disabled="busy" @click="recall">确认撤回</button>
<button type="button" class="plain" @click="recalling = undefined">取消</button>
</div>
<form v-if="forwarding" class="contact-preferences confirmation" @submit.prevent="submitForward">
<label>转发给
<select v-model="destination" :disabled="busy" @change="loadForwardDevices">
<option value="">选择联系人</option>
<option v-for="contact in destinations" :key="contact.id" :value="contact.id">{{ contact.alias || contact.username }}</option>
</select>
</label>
<label v-if="forwardDevices.length">接收设备
<select v-model="destinationDevice" :disabled="busy">
<option v-for="device in forwardDevices" :key="device.id" :value="device.id">{{ device.name }}</option>
</select>
</label>
<p v-else-if="destination && !busy" class="muted">请先在对方聊天中核验接收设备。</p>
<button class="primary" :disabled="busy || !destinationDevice">确认转发</button>
<button type="button" class="plain" :disabled="busy" @click="forwarding = undefined">取消</button>
</form>
<form class="chat-compose" @submit.prevent="submit">
<div v-if="editing" class="chat-quote">
<span>正在编辑消息</span>