为双端群聊增加端到端加密附件
This commit is contained in:
1 parent
5988ad6ed6
commit
a95a007768
21 files changed
+405
-84
No files matched your search
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user