为双端群聊增加端到端加密附件

This commit is contained in:
xhgit committed 2026-11-14 09:05:40 +08:00
1 parent 5988ad6ed6
commit a95a007768
21 files changed
+405 -84

No files matched your search

+34
View File
@@ -157,11 +157,43 @@ test('群密文只投递到当前名单中的设备,离开或撤销后阻断
assert.deepEqual(await json(path + '/messages', 'POST', member.token, message), sequence)
const received = await json(path + '/messages', 'GET', member.token)
assert.deepEqual(received.map((item) => item.wire), [message.wire])
const attachmentId = randomUUID()
const attachment = path + '/attachments/' + attachmentId
const bytes = randomBytes(16_384)
const upload = (token, epoch, body) => fetch(base + '/api/v1' + attachment + '?epoch=' + epoch, {
method: 'POST', headers: { Authorization: 'Bearer ' + token, 'Content-Type': 'application/octet-stream' },
body, signal: AbortSignal.timeout(10_000),
})
const linked = { id: randomUUID(), epoch: second.id, wire: frame(), attachmentId }
await json(path + '/messages', 'POST', owner.token, linked, 409)
assert.equal((await upload(outsider.token, second.id, bytes)).status, 404)
assert.equal((await upload(owner.token, first.id, bytes)).status, 409)
assert.equal((await upload(owner.token, second.id, Buffer.alloc(8 * 1024 * 1024 + 17))).status, 413)
assert.equal((await upload(owner.token, second.id, bytes)).status, 204)
assert.equal((await upload(owner.token, second.id, bytes)).status, 204)
assert.equal((await upload(owner.token, second.id, randomBytes(64))).status, 409)
await json(attachment, 'GET', owner.token, undefined, 404)
await json(attachment, 'GET', member.token, undefined, 404)
await json(path + '/messages', 'POST', member.token, linked, 409)
const posted = await json(path + '/messages', 'POST', owner.token, linked)
assert.deepEqual(await json(path + '/messages', 'POST', owner.token, linked), posted)
await json(path + '/messages', 'POST', owner.token, { ...linked, id: randomUUID() }, 409)
await json(path + '/messages', 'POST', owner.token, { ...linked, attachmentId: undefined }, 409)
assert.equal((await json(path + '/messages?after=' + sequence.sequence, 'GET', member.token))[0].attachmentId, attachmentId)
const download = await call(attachment, 'GET', member.token)
assert.equal(download.status, 200)
assert.equal(download.headers.get('cache-control'), 'no-store')
assert.deepEqual(Buffer.from(await download.arrayBuffer()), bytes)
await json(attachment, 'GET', outsider.token, undefined, 404)
await change(owner, 'remove', member.user.id)
await json(path + '/messages', 'GET', member.token, undefined, 404)
await json(attachment, 'GET', member.token, undefined, 404)
await json(path + '/messages', 'POST', owner.token, { ...message, id: randomUUID() }, 409)
await change(owner, 'invite', member.user.id)
await change(member, 'accept')
await json(attachment, 'GET', member.token, undefined, 404)
assert.equal((await call(attachment, 'GET', owner.token)).status, 200)
assert.deepEqual(await json(path + '/messages', 'GET', member.token), [])
assert.deepEqual(await json(path + '/epochs', 'GET', member.token), [])
const next = { ...second, id: randomUUID(), membershipVersion: '4' }
@@ -169,6 +201,8 @@ test('群密文只投递到当前名单中的设备,离开或撤销后阻断
assert.equal((await call('/devices/' + member.device.id, 'DELETE', member.token)).status, 204)
await json(path + '/messages', 'POST', owner.token, { id: randomUUID(), epoch: next.id, wire: frame() }, 409)
assert.equal((await json(path + '/epochs', 'GET', owner.token)).every((epoch) => !epoch.current), true)
await change(owner, 'disband')
await json(attachment, 'GET', owner.token, undefined, 404)
})
test('设备密文投递、幂等、离线补拉、实时提示和撤销', { timeout: 25000 }, async () => {
+49 -2
View File
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict'
import { randomBytes } from 'node:crypto'
import { mkdtemp, rm } from 'node:fs/promises'
import { mkdtemp, readFile, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import test from 'node:test'
@@ -67,7 +67,21 @@ async function send(sender, text) {
return packet.epoch
}
test('群聊跨 Windows 与 Web 加密投递,移除成员后切换新会话', { timeout: 120_000 }, async () => {
async function sendFile(sender, name, mimeType, contents) {
const upload = sender.waitForResponse((item) => /\/groups\/[^/]+\/attachments\/[^/]+\?epoch=/.test(item.url()) && item.request().method() === 'POST')
const message = sender.waitForResponse((item) => /\/groups\/[^/]+\/messages$/.test(item.url()) && item.request().method() === 'POST')
await chat(sender).getByLabel('选择群附件').setInputFiles({ name, mimeType, buffer: contents })
await chat(sender).getByRole('button', { name: '发送群附件' }).click()
const uploaded = await upload
assert.equal(uploaded.status(), 204)
const sent = await message
assert.equal(sent.status(), 200, await sent.text())
const packet = sent.request().postDataJSON()
assert.match(new URL(uploaded.url()).pathname, new RegExp('/' + packet.attachmentId + '$'))
assert.ok(!Buffer.from(packet.wire, 'base64').includes(Buffer.from(name)))
}
test('群聊跨 Windows 与 Web 加密投递文字和附件,移除成员后切换会话', { timeout: 120_000 }, async () => {
const browser = await chromium.launch({ channel: process.env.XHCHAT_TEST_BROWSER ?? 'msedge' })
const profile = await mkdtemp(join(tmpdir(), 'xhchat-group-test-'))
let desktop
@@ -119,6 +133,39 @@ test('群聊跨 Windows 与 Web 加密投递,移除成员后切换新会话',
await chat(removed).getByRole('button', { name: '启用本机群聊' }).click()
await chat(removed).getByText(first, { exact: true }).waitFor()
const contents = Buffer.from('Windows 群文件内容 ' + id() + '\n'.repeat(9000))
const filename = '群附件-' + id() + '.txt'
await sendFile(windows, filename, 'text/plain', contents)
const fileDownload = owner.waitForEvent('download')
const fileResponse = owner.waitForResponse((item) => item.url().includes('/attachments/') && item.request().method() === 'GET')
await chat(owner).getByRole('button', { name: '下载 ' + filename }).click()
const response = await fileResponse
assert.equal(response.status(), 200)
const ciphertext = await response.body()
assert.equal(ciphertext.length, contents.length + 16)
assert.ok(!ciphertext.includes(contents.subarray(0, 24)))
const downloaded = await fileDownload
assert.equal(downloaded.suggestedFilename(), filename)
assert.deepEqual(await readFile(await downloaded.path()), contents)
await owner.route(response.url(), async (route) => {
const tampered = Buffer.from(ciphertext)
tampered[0] ^= 1
await route.fulfill({ status: 200, contentType: 'application/octet-stream', body: tampered })
}, { times: 1 })
await chat(owner).getByRole('button', { name: '下载 ' + filename }).click()
await chat(owner).getByRole('alert').getByText('附件完整性校验失败').waitFor()
const photo = '群图片-' + id() + '.png'
const pixel = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aX2sAAAAASUVORK5CYII=', 'base64')
await sendFile(owner, photo, 'image/png', pixel)
await chat(windows).getByRole('button', { name: '预览 ' + photo }).click()
const image = chat(windows).getByRole('img', { name: photo })
await image.evaluate((element) => element.decode())
assert.equal(await image.evaluate((element) => element.naturalWidth), 1)
await chat(windows).getByRole('button', { name: '关闭预览' }).click()
await image.waitFor({ state: 'detached' })
const member = owner.getByRole('list', { name: '群成员' }).getByRole('listitem').filter({ hasText: names[2] })
await member.getByRole('button', { name: '移除成员' }).click()
await owner.getByRole('button', { name: '确认操作' }).click()