diff --git a/README.md b/README.md index e004b34..36c459f 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,10 @@ | 账号与设备 | Windows 和 Web 共用账号,支持用户名注册、登录、查看设备和远程撤销。 | | 联系人 | 按用户名查找、处理好友申请、管理备注与分组,以及拉黑。变更可在双端实时同步。 | | 加密文字聊天 | 在两个已核验设备之间发送 MLS 加密消息,支持离线接收。 | +| 加密附件 | 两端可发送和下载最多 8 MiB 的文件,下载时验证完整性。 | | Chromium 浏览器 | Windows 端支持独立窗口、多标签、导航、搜索和隐私窗口,网页运行在独立会话中。 | -当前为开发预览。附件、群聊、音视频和多设备历史同步仍在开发,Windows 安装包尚未发布。 [开发进度与验收记录](docs/progress.md) +当前为开发预览。大文件分片、群聊、音视频和多设备历史同步仍在开发,Windows 安装包尚未发布。 [开发进度与验收记录](docs/progress.md) ## 本地预览 diff --git a/deploy/nginx.conf b/deploy/nginx.conf index 3b18825..1cceea4 100644 --- a/deploy/nginx.conf +++ b/deploy/nginx.conf @@ -21,6 +21,11 @@ server { proxy_pass http://api:8080; proxy_set_header Host $http_host; } + location /api/v1/attachments/ { + client_max_body_size 9m; + proxy_pass http://api:8080; + proxy_set_header Host $http_host; + } add_header Content-Security-Policy "default-src 'none'; script-src 'self' 'wasm-unsafe-eval'; style-src 'self'; connect-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'" always; add_header Referrer-Policy no-referrer always; diff --git a/docs/api.md b/docs/api.md index a8a2d1b..311ca21 100644 --- a/docs/api.md +++ b/docs/api.md @@ -23,6 +23,8 @@ | POST | /api/v1/key-packages | 当前设备发布带设备签名的 MLS KeyPackage,成功返回 204 | | GET | /api/v1/mls-local-key | 读取当前设备的公开认证密钥,供本机安全码计算 | | POST | /api/v1/mls-credential | 当前设备登记带认证密钥签名的 MLS 签名公钥 | +| POST | /api/v1/attachments/{id}?recipient= | 已接受好友设备上传附件密文,使用 application/octet-stream,成功返回 204 | +| GET | /api/v1/attachments/{id} | 原发送或接收设备获取附件密文 | | POST | /api/v1/contacts/{id}/devices/{device}/claim | 为当前设备原子领取一个联系人的未使用 KeyPackage | 用户名为 3–32 位 ASCII 字母、数字或下划线,忽略大小写。密码最少 12 个 Unicode 字符,最多 256 字节。platform 为 web 或 windows。公钥、签名和 challenge 使用无填充 Base64URL。设备使用 Ed25519 签名 UTF-8 字符串 `XHChat device authentication v1\n` 后紧接 challenge。该签名仅用于证明基础设备密钥持有权。 @@ -35,4 +37,6 @@ MLS 凭据由标准 Base64 的 publicKey 和 signature 组成,使用 UTF-8 前缀 `XHChat MLS credential v1\n{userId}\n{deviceId}\n` 后接 32 字节 MLS Ed25519 公钥。设备目录返回可选的 mls 对象。已登记公钥仅允许原值重试。 -下一阶段将增加 /conversations 和 /attachments。后续加入 /groups、/calls、/posts、/backups、/push 与独立的管理接口。 +附件密文上限为 8 MiB 加 16 字节认证标签。重复提交相同 ID 和密文为幂等操作。附件按投递保留期清理,文件名和密钥只通过 MLS 消息传递。 + +下一阶段将增加 /conversations。后续加入 /groups、/calls、/posts、/backups、/push 与独立的管理接口。 diff --git a/docs/database.md b/docs/database.md index 9f7300b..4e4ccff 100644 --- a/docs/database.md +++ b/docs/database.md @@ -9,6 +9,7 @@ | conversations、memberships | 会话与成员关系,记录角色和生效世代 | | devices、device_key_packages | 公开设备凭据及签名的一次性密钥包,事务领取,撤销后停止发放 | | device_envelopes | 二进制协议封包、投递游标和目标设备,按发送设备、接收设备和客户端消息 ID 幂等 | +| device_attachments | 预览版 8 MiB 附件密文与设备访问范围,按附件 ID 幂等,跟随消息保留期 | | attachments、backups | 对象存储标识、密文尺寸、配额和版本,密钥由端侧传递 | | calls、call_participants | 通话状态、参与设备和时间,不存媒体 | | posts、post_audiences、reactions | 私人动态正文为密文,受众变更触发重新分发或换钥 | diff --git a/docs/encryption.md b/docs/encryption.md index 9b6b982..837dda4 100644 --- a/docs/encryption.md +++ b/docs/encryption.md @@ -9,3 +9,5 @@ OpenMLS 0.9.2 兼容性用例已在 Linux 原生和 Windows Chrome WebAssembly 中通过,覆盖三设备 Welcome、密文往返、重放拒绝和移除后无法读取新世代消息。OpenMLS 发布过 SRLabs 安全审计,应用层的身份绑定、WASM 封装、存储和投递仍需独立验证。 [OpenMLS 审计公告](https://blog.openmls.tech/) 附件采用公开的分块认证加密格式,评估带明确结束认证与大文件恢复语义的实现。每份附件生成独立随机密钥,密文通过对象存储分发,文件密钥及完整性元数据放在 E2EE 消息中。备份使用独立随机恢复秘密,显式确认恢复权限和完整性,服务端只存密文及版本。私人动态按受众分发密钥。Web 应用以同源、受控发布和严格 CSP 管理脚本信任,当前页面的恶意同源脚本属于独立威胁。 + +预览版附件使用 Web Crypto 的整文件 AES-256-GCM,限制 8 MiB。每份文件独立随机密钥和 96 位 IV,附件 ID、文件名和字节数纳入认证数据。下载在完整验证后才生成本地文件,服务端只持有密文及投递元数据。生产版仍需分块认证、断点续传和对象存储。 diff --git a/docs/progress.md b/docs/progress.md index f5e4aae..86676ee 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -19,7 +19,7 @@ Web 和 Electron 共用 Vue 客户端,独立接入同一 Go API。真实 Postg ## 尚待实现 - 阶段一还需邮箱和手机号注册、验证码服务、找回密码和双重验证。真实联调需要 SMTP 与短信供应商配置。 -- 阶段二与三还需附件、消息操作、群聊、多设备可信授权、加密备份与跨设备历史同步。两设备文字私聊已进入开发预览。 +- 阶段二与三还需分片附件、消息操作、群聊、多设备可信授权、加密备份与跨设备历史同步。两设备文字和小附件私聊已进入开发预览。 - 阶段四还需 WebRTC 音视频、TURN、SFU、屏幕共享及媒体 E2EE。 - 阶段五还需持久书签和历史、下载记录、逐站授权、反钓鱼及聊天联动。 - 阶段六到八还需社交、扩展生态、独立管理后台、推送、生产部署与备份演练、压力测试、安全评估、签名安装包和发布流程。 @@ -50,3 +50,7 @@ OpenMLS WASM 提供两设备会话创建、加入、消息加解密和端侧状 互为好友后,双方在聊天页分别启用本机加密,并通过其他可信渠道核验对方设备安全码。设备认证密钥签署 MLS 公钥和密钥包,OpenMLS 处理会话与加解密。服务端投递 MLS 封包。浏览器使用不可导出的 Web Crypto AES-GCM 密钥,Windows 使用 DPAPI 封装本地加密密钥。协议状态、历史、待发送封包和收件游标持久化在端侧加密记录中。 Edge 与 Electron 集成测试覆盖错误安全码、双向文字、网络封包无测试正文、重新登录后离线接收和 Windows 本地密文存储。当前预览按设备建立会话,支持最多 1000 条本机消息。账号级密钥认证、跨设备同步、定期 MLS 更新、可信设备授权与安全评估待后续交付。 + +## 端到端加密附件预览 + +Windows 与 Web 支持发送和下载最多 8 MiB 的附件。文件名、长度、密钥和 IV 位于 MLS 消息中,附件服务只存密文。下载先验证认证标签,再提供本地保存。跨端用例覆盖上传密文、篡改拒绝和下载字节一致性。对象存储、分片断点续传和媒体专用界面仍待完成。 diff --git a/server/internal/database/006_attachments.sql b/server/internal/database/006_attachments.sql new file mode 100644 index 0000000..9fcada7 --- /dev/null +++ b/server/internal/database/006_attachments.sql @@ -0,0 +1,10 @@ +CREATE TABLE device_attachments ( + id uuid PRIMARY KEY, + sender_device uuid NOT NULL REFERENCES devices(id) ON DELETE CASCADE, + recipient_device uuid NOT NULL REFERENCES devices(id) ON DELETE CASCADE, + ciphertext bytea NOT NULL CHECK (octet_length(ciphertext) BETWEEN 16 AND 8388624), + created_at timestamptz NOT NULL DEFAULT now(), + CHECK (sender_device <> recipient_device) +); + +CREATE INDEX device_attachments_expiry ON device_attachments(created_at); diff --git a/server/internal/database/database.go b/server/internal/database/database.go index 4ac18c9..2933ae7 100644 --- a/server/internal/database/database.go +++ b/server/internal/database/database.go @@ -22,6 +22,9 @@ var keyPackagesMigration string //go:embed 005_mls_credentials.sql var mlsCredentialsMigration string +//go:embed 006_attachments.sql +var attachmentsMigration string + func Migrate(ctx context.Context, pool *pgxpool.Pool) error { tx, err := pool.Begin(ctx) if err != nil { @@ -34,7 +37,7 @@ func Migrate(ctx context.Context, pool *pgxpool.Pool) error { if _, err = tx.Exec(ctx, "CREATE TABLE IF NOT EXISTS schema_migrations (version integer PRIMARY KEY)"); err != nil { return err } - for index, migration := range []string{identityMigration, contactsMigration, deliveryMigration, keyPackagesMigration, mlsCredentialsMigration} { + for index, migration := range []string{identityMigration, contactsMigration, deliveryMigration, keyPackagesMigration, mlsCredentialsMigration, attachmentsMigration} { version := index + 1 var applied bool if err = tx.QueryRow(ctx, "SELECT EXISTS (SELECT 1 FROM schema_migrations WHERE version=$1)", version).Scan(&applied); err != nil { diff --git a/server/internal/delivery/attachments.go b/server/internal/delivery/attachments.go new file mode 100644 index 0000000..b0c1f20 --- /dev/null +++ b/server/internal/delivery/attachments.go @@ -0,0 +1,55 @@ +package delivery + +import ( + "bytes" + "context" + "errors" + + "github.com/jackc/pgx/v5" +) + +const AttachmentLimit = 8<<20 + 16 + +func (s *Store) UploadAttachment(ctx context.Context, sender, recipient, id string, ciphertext []byte) error { + tx, err := s.db.Begin(ctx) + if err != nil { + return err + } + defer tx.Rollback(ctx) + if err = authorizeDelivery(ctx, tx, sender, recipient); err != nil { + return err + } + result, err := tx.Exec(ctx, ` + INSERT INTO device_attachments(id,sender_device,recipient_device,ciphertext) + VALUES($1,$2,$3,$4) ON CONFLICT(id) DO NOTHING + `, id, sender, recipient, ciphertext) + if err != nil { + return err + } + if result.RowsAffected() == 0 { + var stored []byte + err = tx.QueryRow(ctx, ` + SELECT ciphertext FROM device_attachments WHERE id=$1 AND sender_device=$2 AND recipient_device=$3 + `, id, sender, recipient).Scan(&stored) + if errors.Is(err, pgx.ErrNoRows) || err == nil && !bytes.Equal(stored, ciphertext) { + return ErrConflict + } + if err != nil { + return err + } + } + return tx.Commit(ctx) +} + +func (s *Store) DownloadAttachment(ctx context.Context, device, id string) ([]byte, error) { + var ciphertext []byte + err := s.db.QueryRow(ctx, ` + SELECT attachment.ciphertext FROM device_attachments attachment JOIN devices d ON d.id=$2 + WHERE attachment.id=$1 AND d.revoked_at IS NULL + AND d.id IN (attachment.sender_device,attachment.recipient_device) + `, id, device).Scan(&ciphertext) + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrUnavailable + } + return ciphertext, err +} diff --git a/server/internal/delivery/store.go b/server/internal/delivery/store.go index 5216763..b80c108 100644 --- a/server/internal/delivery/store.go +++ b/server/internal/delivery/store.go @@ -34,47 +34,10 @@ func (s *Store) Send(ctx context.Context, sender, recipient, message string, cip } defer tx.Rollback(ctx) - var senderUser, recipientUser string - err = tx.QueryRow(ctx, ` - SELECT sender.user_id, recipient.user_id FROM devices sender CROSS JOIN devices recipient - WHERE sender.id=$1 AND recipient.id=$2 AND sender.id<>recipient.id - `, sender, recipient).Scan(&senderUser, &recipientUser) - if errors.Is(err, pgx.ErrNoRows) { - return "", ErrUnavailable - } - if err != nil { + if err = authorizeDelivery(ctx, tx, sender, recipient); err != nil { return "", err } - // Use the same account lock order as contact changes. Locking both devices also - // serializes writes for each recipient and prevents a cursor passing an uncommitted write. - if _, err = tx.Exec(ctx, "SELECT id FROM users WHERE id IN ($1,$2) ORDER BY id FOR UPDATE", senderUser, recipientUser); err != nil { - return "", err - } - var active int - err = tx.QueryRow(ctx, ` - SELECT count(*) FROM ( - SELECT id FROM devices WHERE id IN ($1,$2) AND revoked_at IS NULL ORDER BY id FOR UPDATE - ) AS active_devices - `, sender, recipient).Scan(&active) - if err != nil { - return "", err - } - if active != 2 { - return "", ErrUnavailable - } - - // This direct-message endpoint only connects accepted contacts. - // A separate authorization flow is needed for same-account device transfers. - var friends bool - err = tx.QueryRow(ctx, "SELECT "+acceptedContact, senderUser, recipientUser).Scan(&friends) - if err != nil { - return "", err - } - if !friends { - return "", ErrUnavailable - } - var seq string err = tx.QueryRow(ctx, ` INSERT INTO device_envelopes(message_id, sender_device, recipient_device, ciphertext) @@ -122,10 +85,59 @@ func (s *Store) Inbox(ctx context.Context, recipient string, after int64) ([]Env } func (s *Store) Cleanup(ctx context.Context, retention time.Duration) error { - if _, err := s.db.Exec(ctx, "DELETE FROM device_envelopes WHERE created_at < $1", time.Now().Add(-retention)); err != nil { + cutoff := time.Now().Add(-retention) + if _, err := s.db.Exec(ctx, "DELETE FROM device_envelopes WHERE created_at < $1", cutoff); err != nil { + return err + } + if _, err := s.db.Exec(ctx, "DELETE FROM device_attachments WHERE created_at < $1", cutoff); err != nil { return err } // Keep issued package hashes past the validity window of an individual upload. _, err := s.db.Exec(ctx, "DELETE FROM device_key_packages WHERE created_at < now() - interval '90 days'") return err } + +func authorizeDelivery(ctx context.Context, tx pgx.Tx, sender, recipient string) error { + var senderUser, recipientUser string + err := tx.QueryRow(ctx, ` + SELECT sender.user_id, recipient.user_id FROM devices sender CROSS JOIN devices recipient + WHERE sender.id=$1 AND recipient.id=$2 AND sender.id<>recipient.id + `, sender, recipient).Scan(&senderUser, &recipientUser) + if errors.Is(err, pgx.ErrNoRows) { + return ErrUnavailable + } + if err != nil { + return err + } + + // Use the same account lock order as contact changes. Locking both devices also + // serializes writes for each recipient and prevents a cursor passing an uncommitted write. + if _, err = tx.Exec(ctx, "SELECT id FROM users WHERE id IN ($1,$2) ORDER BY id FOR UPDATE", senderUser, recipientUser); err != nil { + return err + } + var active int + err = tx.QueryRow(ctx, ` + SELECT count(*) FROM ( + SELECT id FROM devices WHERE id IN ($1,$2) AND revoked_at IS NULL ORDER BY id FOR UPDATE + ) AS active_devices + `, sender, recipient).Scan(&active) + if err != nil { + return err + } + if active != 2 { + return ErrUnavailable + } + + // This direct-message endpoint only connects accepted contacts. + // A separate authorization flow is needed for same-account device transfers. + var friends bool + err = tx.QueryRow(ctx, "SELECT "+acceptedContact, senderUser, recipientUser).Scan(&friends) + if err != nil { + return err + } + if !friends { + return ErrUnavailable + } + + return nil +} diff --git a/server/internal/httpapi/api.go b/server/internal/httpapi/api.go index cfde332..b0c739f 100644 --- a/server/internal/httpapi/api.go +++ b/server/internal/httpapi/api.go @@ -54,6 +54,8 @@ func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *d mux.HandleFunc("POST /api/v1/contacts/{id}/devices/{device}/claim", a.authorized(a.claimPackage)) mux.HandleFunc("POST /api/v1/key-packages", a.authorized(a.publishPackage)) mux.HandleFunc("POST /api/v1/mls-credential", a.authorized(a.publishCredential)) + mux.HandleFunc("POST /api/v1/attachments/{id}", a.authorized(a.uploadAttachment)) + mux.HandleFunc("GET /api/v1/attachments/{id}", a.authorized(a.downloadAttachment)) mux.HandleFunc("GET /api/v1/mls-local-key", a.authorized(a.localMLSKey)) return a.boundary(mux) } diff --git a/server/internal/httpapi/attachments.go b/server/internal/httpapi/attachments.go new file mode 100644 index 0000000..092d8d9 --- /dev/null +++ b/server/internal/httpapi/attachments.go @@ -0,0 +1,65 @@ +package httpapi + +import ( + "errors" + "io" + "net/http" + + "xhchat/server/internal/account" + "xhchat/server/internal/delivery" +) + +func (a *API) uploadAttachment(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) { + id, recipient := r.PathValue("id"), r.URL.Query().Get("recipient") + if !uuidPattern.MatchString(id) || !uuidPattern.MatchString(recipient) || + r.Header.Get("Content-Type") != "application/octet-stream" { + problem(w, 400, "invalid_attachment", "附件请求格式无效") + return + } + if !a.limit(w, r, "attachment-upload:"+identity.Device.ID, 12) { + return + } + body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, delivery.AttachmentLimit)) + var tooLarge *http.MaxBytesError + switch { + case errors.As(err, &tooLarge): + problem(w, 413, "attachment_too_large", "附件超过 8 MiB") + return + case err != nil || len(body) < 16: + problem(w, 400, "invalid_attachment", "附件请求格式无效") + return + } + err = a.delivery.UploadAttachment(r.Context(), identity.Device.ID, recipient, id, body) + switch { + case errors.Is(err, delivery.ErrUnavailable): + problem(w, 404, "attachment_unavailable", "无法向指定设备发送附件") + case errors.Is(err, delivery.ErrConflict): + problem(w, 409, "attachment_conflict", "附件 ID 已使用") + case err != nil: + unavailable(w, err) + default: + w.WriteHeader(204) + } +} + +func (a *API) downloadAttachment(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) { + id := r.PathValue("id") + if !uuidPattern.MatchString(id) { + problem(w, 404, "attachment_unavailable", "附件不可用") + return + } + if !a.limit(w, r, "attachment-download:"+identity.Device.ID, 30) { + return + } + body, err := a.delivery.DownloadAttachment(r.Context(), identity.Device.ID, id) + switch { + case errors.Is(err, delivery.ErrUnavailable): + problem(w, 404, "attachment_unavailable", "附件不可用或已过期") + case err != nil: + unavailable(w, err) + default: + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Content-Disposition", "attachment") + _, _ = w.Write(body) + } +} diff --git a/tests/delivery.test.mjs b/tests/delivery.test.mjs index db8a2f0..cf23302 100644 --- a/tests/delivery.test.mjs +++ b/tests/delivery.test.mjs @@ -116,3 +116,33 @@ test('设备密钥包验证签名并只领取一次', { timeout: 25000 }, async assert.equal((await call('/devices/' + owner.device.id, 'DELETE', owner.token)).status, 204) assert.deepEqual(await json(directory, 'GET', contactUser.token), []) }) + +test('附件访问按设备授权并支持相同密文重试', { timeout: 15000 }, async () => { + const sender = await register() + const recipient = await register() + const stranger = await register() + const path = '/api/v1/attachments/' + randomUUID() + const bytes = randomBytes(256) + const upload = (body) => fetch(base + path + '?recipient=' + recipient.device.id, { + method: 'POST', + headers: { Authorization: 'Bearer ' + sender.token, 'Content-Type': 'application/octet-stream' }, + body, + signal: AbortSignal.timeout(10_000), + }) + const download = (token) => fetch(base + path, { + headers: { Authorization: 'Bearer ' + token }, + signal: AbortSignal.timeout(10_000), + }) + assert.equal((await upload(bytes)).status, 404) + await contact(sender, recipient, 'request') + await contact(recipient, sender, 'accept') + assert.equal((await upload(bytes)).status, 204) + assert.equal((await upload(bytes)).status, 204) + assert.equal((await upload(randomBytes(256))).status, 409) + assert.equal((await download(stranger.token)).status, 404) + const response = await download(recipient.token) + assert.equal(response.status, 200) + assert.deepEqual(Buffer.from(await response.arrayBuffer()), bytes) + assert.equal((await call('/devices/' + recipient.device.id, 'DELETE', recipient.token)).status, 204) + assert.equal((await download(recipient.token)).status, 401) +}) diff --git a/tests/encrypted-chat.test.mjs b/tests/encrypted-chat.test.mjs index 0d26f1e..5abfb19 100644 --- a/tests/encrypted-chat.test.mjs +++ b/tests/encrypted-chat.test.mjs @@ -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' @@ -46,7 +46,7 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢 const profile = await mkdtemp(join(tmpdir(), 'xhchat-mls-test-')) let desktop try { - const web = await browser.newPage() + const web = await browser.newPage({ acceptDownloads: true }) await web.goto(base) desktop = await _electron.launch({ executablePath: electron, @@ -106,6 +106,33 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢 await send(windows, reply) await web.getByText(reply, { exact: true }).waitFor() + const contents = Buffer.from('端到端文件 ' + randomBytes(32).toString('hex')) + const fileName = '跨端验收.txt' + const upload = windows.waitForRequest((req) => req.method() === 'POST' && req.url().includes('/api/v1/attachments/')) + await windows.locator('input[type=file]').setInputFiles({ name: fileName, mimeType: 'text/plain', buffer: contents }) + const fileRequest = await upload + const uploaded = fileRequest.postDataBuffer() + assert.ok(uploaded) + assert.equal(uploaded.length, contents.length + 16) + assert.ok(!uploaded.includes(contents)) + assert.ok(!fileRequest.url().includes(encodeURIComponent(fileName))) + const fileButton = web.getByRole('button', { name: '下载 ' + fileName, exact: true }) + await fileButton.waitFor() + const fileURL = new URL(fileRequest.url()) + await web.route(fileURL.origin + fileURL.pathname, async (route) => { + const response = await route.fetch() + const corrupted = await response.body() + corrupted[0] ^= 1 + await route.fulfill({ response, body: corrupted }) + }, { times: 1 }) + await fileButton.click() + await web.getByText('附件完整性校验失败').waitFor() + const downloaded = web.waitForEvent('download') + await fileButton.click() + const file = await downloaded + assert.equal(file.suggestedFilename(), fileName) + assert.deepEqual(await readFile(await file.path()), contents) + await windows.getByRole('button', { name: '退出登录' }).click() await windows.getByRole('heading', { name: '登录 XHChat' }).waitFor() const offline = '离线补拉 ' + randomBytes(8).toString('hex') diff --git a/web/src/api.ts b/web/src/api.ts index 8df5adc..1827c87 100644 --- a/web/src/api.ts +++ b/web/src/api.ts @@ -6,20 +6,39 @@ export class APIError extends Error { } } -export async function request(path: string, method = 'GET', token = '', body?: object): Promise { +export async function request(path: string, method = 'GET', token = '', body?: object, binary = false): Promise { const headers: Record = { Accept: 'application/json' } if (token) headers.Authorization = 'Bearer ' + token - if (body) headers['Content-Type'] = 'application/json' + const bytes = body instanceof ArrayBuffer + if (body) headers['Content-Type'] = bytes ? 'application/octet-stream' : 'application/json' let response: Response try { response = await fetch(base + '/api/v1' + path, { - method, headers, body: body ? JSON.stringify(body) : undefined, - credentials: 'omit', redirect: 'error', signal: AbortSignal.timeout(12000), + method, headers, body: bytes ? body : body ? JSON.stringify(body) : undefined, + credentials: 'omit', redirect: 'error', signal: AbortSignal.timeout(binary || bytes ? 60_000 : 12_000), }) } catch { throw new Error('无法连接 XHChat 服务,请检查网络后重试') } if (response.status === 204) return undefined as T + if (binary && response.ok) { + if (Number(response.headers.get('Content-Length')) > 8 * 1024 * 1024 + 16) throw new Error('附件超过大小限制') + const reader = response.body?.getReader() + if (!reader) throw new Error('附件下载失败') + const chunks: Uint8Array[] = [] + let length = 0 + for (;;) { + const { value, done } = await reader.read() + if (done) break + length += value.byteLength + if (length > 8 * 1024 * 1024 + 16) { + await reader.cancel() + throw new Error('附件超过大小限制') + } + chunks.push(value) + } + return await new Blob(chunks).arrayBuffer() as T + } const data = await response.json().catch(() => null) if (!response.ok) throw new APIError(response.status, data?.error?.message ?? '请求失败,请稍后重试') return data as T diff --git a/web/src/chat/attachments.ts b/web/src/chat/attachments.ts new file mode 100644 index 0000000..eb12f2d --- /dev/null +++ b/web/src/chat/attachments.ts @@ -0,0 +1,89 @@ +import { base64, decode } from './engine' +import type { Call } from './chat' + +const maxSize = 8 * 1024 * 1024 +const encoder = new TextEncoder() +const decoder = new TextDecoder('utf-8', { fatal: true }) + +export interface Attachment { + id: string + name: string + size: number + key: string + iv: string +} + +function attachmentPath(id: string) { + return '/attachments/' + encodeURIComponent(id) +} + +function authenticatedData(file: Pick) { + return encoder.encode(JSON.stringify(['XHChat attachment v1', file.id, file.name, file.size])) +} + +function safeName(name: string) { + return name.replace(/[\u0000-\u001f\u007f\u202a-\u202e\u2066-\u2069<>:"/\\|?*]/g, '_').trim().slice(0, 180) || 'attachment' +} + +export async function sealAttachment(file: File) { + if (file.size > maxSize) throw new Error('附件超过 8 MiB') + const key = crypto.getRandomValues(new Uint8Array(32)) + const iv = crypto.getRandomValues(new Uint8Array(12)) + const attachment = { id: crypto.randomUUID(), name: safeName(file.name), size: file.size, key: base64(key), iv: base64(iv) } + try { + const imported = await crypto.subtle.importKey('raw', key, 'AES-GCM', false, ['encrypt']) + const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv, additionalData: authenticatedData(attachment) }, imported, await file.arrayBuffer()) + return { attachment, ciphertext } + } finally { + key.fill(0) + } +} + +export function uploadAttachment(attachment: Attachment, ciphertext: ArrayBuffer, peer: string, call: Call) { + return call(attachmentPath(attachment.id) + '?recipient=' + encodeURIComponent(peer), 'POST', ciphertext) +} + +export function encodeAttachment(attachment: Attachment) { + const body = encoder.encode(JSON.stringify(attachment)) + const message = new Uint8Array(body.length + 2) + // An invalid UTF-8 prefix keeps attachment messages distinct from older text payloads. + message[0] = 255 + message[1] = 1 + message.set(body, 2) + return message +} + +export function decodeMessage(message: Uint8Array): { text: string; attachment?: Attachment } { + if (message[0] !== 255) return { text: decoder.decode(message) } + if (message[1] !== 1 || message.length > 4096) throw new Error('附件描述格式无效') + const value = JSON.parse(decoder.decode(message.subarray(2))) as Attachment | null + if (!value || typeof value.id !== 'string' || !/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(value.id) || + typeof value.name !== 'string' || value.name !== safeName(value.name) || + !Number.isInteger(value.size) || value.size < 0 || value.size > maxSize || + typeof value.key !== 'string' || decode(value.key).length !== 32 || + typeof value.iv !== 'string' || decode(value.iv).length !== 12) { + throw new Error('附件描述格式无效') + } + return { text: '', attachment: value } +} + +export async function downloadAttachment(attachment: Attachment, call: Call) { + const ciphertext = await call(attachmentPath(attachment.id), 'GET', undefined, true) + if (ciphertext.byteLength !== attachment.size + 16) throw new Error('附件完整性校验失败') + const raw = decode(attachment.key) + let bytes: ArrayBuffer + try { + const key = await crypto.subtle.importKey('raw', raw, 'AES-GCM', false, ['decrypt']) + bytes = await crypto.subtle.decrypt({ name: 'AES-GCM', iv: decode(attachment.iv), additionalData: authenticatedData(attachment) }, key, ciphertext) + } catch { + throw new Error('附件完整性校验失败') + } finally { + raw.fill(0) + } + const url = URL.createObjectURL(new Blob([bytes], { type: 'application/octet-stream' })) + const link = document.createElement('a') + link.download = attachment.name + link.href = url + link.click() + setTimeout(() => URL.revokeObjectURL(url), 30_000) +} diff --git a/web/src/chat/chat.ts b/web/src/chat/chat.ts index 2595ba1..caba034 100644 --- a/web/src/chat/chat.ts +++ b/web/src/chat/chat.ts @@ -3,6 +3,8 @@ import { mlsBinding, signMls } from '../identity' import { base64, decode, fingerprint, loadEngine } from './engine' import type { DeviceMls } from './engine' import { withVault } from './vault' +import { decodeMessage, encodeAttachment, sealAttachment, uploadAttachment } from './attachments' +import type { Attachment } from './attachments' export interface Peer { id: string @@ -24,6 +26,7 @@ export interface ChatLine { id: string peer: string text: string + attachment?: Attachment time: string own: boolean } @@ -40,7 +43,7 @@ interface ChatState { rejected: number } -export type Call = (path: string, method?: string, body?: object) => Promise +export type Call = (path: string, method?: string, body?: object, binary?: boolean) => Promise const encoder = new TextEncoder() function aad(id: string, sender: string, recipient: string) { @@ -146,19 +149,29 @@ async function flush(state: ChatState, save: (state: ChatState) => Promise return unavailable } -export function send(identity: ChatIdentity, user: string, peer: Peer, text: string, call: Call) { +export function send(identity: ChatIdentity, user: string, peer: Peer, content: string | File, call: Call) { return update(identity, async (state, save, factory) => { const current = (await directory(call, user)).find((item) => item.id === peer.id) if (!current || state.pinned[current.id] !== current.publicKey) throw new Error('发送前请核验对方设备安全码') const key = await verifiedPeer(current, user) const unavailable = await flush(state, save, call) if (unavailable.has(current.id)) throw new Error('该设备暂不可接收消息') - const plaintext = encoder.encode(text.trim()) - if (!plaintext.length || plaintext.length > 8000) throw new Error('消息须为 1 至 8000 字节') + const text = typeof content === 'string' ? content.trim() : '' + let plaintext = encoder.encode(text) + if (typeof content === 'string' && (!plaintext.length || plaintext.length > 8000)) { + throw new Error('消息须为 1 至 8000 字节') + } if (state.lines.length >= 1000) throw new Error('本机预览版消息容量已达上限') const engine = factory.DeviceMls.restore(decode(state.engine)) try { let group = state.groups[current.id] ? decode(state.groups[current.id]) : undefined + let attachment: Attachment | undefined + if (typeof content !== 'string') { + const sealed = await sealAttachment(content) + attachment = sealed.attachment + await uploadAttachment(attachment, sealed.ciphertext, current.id, call) + plaintext = encodeAttachment(attachment) + } if (!group) { const pack = await call('/contacts/' + user + '/devices/' + current.id + '/claim', 'POST') const wire = decode(pack.package) @@ -171,7 +184,7 @@ export function send(identity: ChatIdentity, user: string, peer: Peer, text: str const id = crypto.randomUUID() const ciphertext = engine.encrypt(group, aad(id, identity.device.id, current.id), plaintext) state.outbox.push({ messageId: id, recipientDevice: current.id, ciphertext: base64(ciphertext) }) - state.lines.push({ id, peer: current.id, text: text.trim(), time: new Date().toISOString(), own: true }) + state.lines.push({ id, peer: current.id, text, attachment, time: new Date().toISOString(), own: true }) state.engine = base64(engine.snapshot()) await save(state) await flush(state, save, call) @@ -207,7 +220,7 @@ export function synchronize(identity: ChatIdentity, user: string, call: Call) { } else { const plaintext = engine.decrypt(peer.id, key, aad(item.messageId, peer.id, identity.device.id), wire) if (state.lines.length < 1000) state.lines.push({ - id: item.messageId, peer: peer.id, text: new TextDecoder('utf-8', { fatal: true }).decode(plaintext), + id: item.messageId, peer: peer.id, ...decodeMessage(plaintext), time: item.createdAt, own: false, }) } diff --git a/web/src/session.ts b/web/src/session.ts index b1aa2d6..737a4b9 100644 --- a/web/src/session.ts +++ b/web/src/session.ts @@ -91,10 +91,10 @@ export const useSession = defineStore('session', () => { void connect(token) } - async function authorized(path: string, method = 'GET', body?: object) { + async function authorized(path: string, method = 'GET', body?: object, binary = false) { const current = token try { - const response = await request(path, method, current, body) + const response = await request(path, method, current, body, binary) if (token !== current) throw new Error('登录会话已变更') return response } catch (error) { diff --git a/web/src/views/ChatView.vue b/web/src/views/ChatView.vue index f137db5..057847b 100644 --- a/web/src/views/ChatView.vue +++ b/web/src/views/ChatView.vue @@ -3,6 +3,8 @@ import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue' import { useRoute } from 'vue-router' import { useSession } from '../session' import { enableChat, send, synchronize, trust } from '../chat/chat' +import { downloadAttachment } from '../chat/attachments' +import type { Attachment } from '../chat/attachments' const session = useSession() const route = useRoute() @@ -69,6 +71,22 @@ function submit() { }) } +function attach(event: Event) { + const input = event.target as HTMLInputElement + const file = input.files?.[0] + input.value = '' + if (!file) return + void execute(async () => { + if (!session.identity || !peer.value) return + await send(session.identity, target.value, peer.value, file, session.authorized) + await refresh() + }) +} + +function saveAttachment(attachment: Attachment) { + void execute(() => downloadAttachment(attachment, session.authorized)) +} + function sync() { if (ownCode.value) void execute(refresh) } @@ -119,10 +137,13 @@ onBeforeUnmount(() => clearInterval(timer))

已拒绝 {{ view.rejected }} 个无法验证的封包。

  1. - {{ line.text }}待发送 + + {{ line.text }}待发送
+