fix(composer): stop three E2EE plaintext leaks (compress, schedule, GIF)
- Image compression in an encrypted room re-encoded the *plaintext* original, uploaded it unencrypted, and reused the original's encInfo, so the media sat on the server in the clear AND the attachment was undecryptable. The compressed bytes are now run through encryptFile and the synthetic upload item carries the new encInfo (buildCompressedUploadItem, unit-tested; it can never inherit the stale encInfo). - Scheduled messages (MSC4140) are PUT as raw m.room.message, bypassing the SDK encryption pipeline. The Schedule button is now hidden in encrypted rooms, handleScheduleClick no-ops there, and scheduleMessage() itself refuses with a clear error so no caller can regress this. README notes the limitation. - The GIF picker uploaded the Giphy blob unencrypted into E2EE rooms; it now mirrors the voice/attachment path (encryptFile -> upload ciphertext -> content.file). Fixes #6 Fixes #7 Fixes #11 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PPmy3tPq869XDW4njjVaKA
This commit is contained in:
@@ -105,6 +105,7 @@ import {
|
||||
settingsAtom,
|
||||
} from '../../state/settings';
|
||||
import {
|
||||
buildCompressedUploadItem,
|
||||
getAudioMsgContent,
|
||||
getFileMsgContent,
|
||||
getImageMsgContent,
|
||||
@@ -244,8 +245,11 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
const showLocation = composerToolbarButtons?.showLocation ?? true;
|
||||
const showPoll = composerToolbarButtons?.showPoll ?? true;
|
||||
const showVoice = composerToolbarButtons?.showVoice ?? true;
|
||||
// Schedule-send is hidden in thread mode (v1 reduction).
|
||||
const showSchedule = (composerToolbarButtons?.showSchedule ?? true) && !threadRootId;
|
||||
// Schedule-send is hidden in thread mode (v1 reduction) and in encrypted rooms:
|
||||
// MSC4140 delayed events are PUT as plaintext m.room.message, bypassing the
|
||||
// SDK's encryption pipeline, so scheduling in an E2EE room would leak the body.
|
||||
const showSchedule =
|
||||
(composerToolbarButtons?.showSchedule ?? true) && !threadRootId && !isEncrypted;
|
||||
const composerButtonOrder = useMemo(
|
||||
() => normalizeComposerToolbarOrder(composerToolbarButtons?.order),
|
||||
[composerToolbarButtons?.order],
|
||||
@@ -485,22 +489,29 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
const compressedFile = new File([compressionResult.blob], compressedName, {
|
||||
type: compressedType,
|
||||
});
|
||||
const uploadRes = await mx.uploadContent(compressedFile, {
|
||||
name: compressedName,
|
||||
type: compressedType,
|
||||
});
|
||||
// Compression re-encodes the image, so in an encrypted room the new
|
||||
// bytes must be encrypted before upload (and the event must carry the
|
||||
// *new* encInfo) — reusing the original's encInfo would publish the
|
||||
// image in the clear and yield an undecryptable attachment.
|
||||
const encrypted = fileItem.encInfo ? await encryptFile(compressedFile) : undefined;
|
||||
const uploadRes = encrypted
|
||||
? await mx.uploadContent(encrypted.file)
|
||||
: await mx.uploadContent(compressedFile, {
|
||||
name: compressedName,
|
||||
type: compressedType,
|
||||
});
|
||||
const compressedMxc = (uploadRes as { content_uri: string }).content_uri;
|
||||
if (compressedMxc) {
|
||||
// Delete the pre-uploaded original so only one copy lives on the server.
|
||||
tryDeleteMxcContent(mx, upload.mxc);
|
||||
mxc = compressedMxc;
|
||||
// Build a synthetic fileItem that refers to the compressed file so
|
||||
// getImageMsgContent picks up the correct dimensions and type.
|
||||
const compressedItem = {
|
||||
...fileItem,
|
||||
file: compressedFile,
|
||||
originalFile: compressedFile,
|
||||
};
|
||||
// Synthetic fileItem referring to the compressed file so
|
||||
// getImageMsgContent picks up the correct dimensions, type and encInfo.
|
||||
const compressedItem = buildCompressedUploadItem(
|
||||
fileItem,
|
||||
compressedFile,
|
||||
encrypted,
|
||||
);
|
||||
return getImageMsgContent(mx, compressedItem, mxc);
|
||||
}
|
||||
}
|
||||
@@ -697,11 +708,14 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
}, [editor, isMarkdown, mx, roomId, replyDraft]);
|
||||
|
||||
const handleScheduleClick = useCallback(() => {
|
||||
// Defense in depth: scheduling sends an unencrypted m.room.message, so never
|
||||
// open the modal for an encrypted room even if the button somehow renders.
|
||||
if (isEncrypted) return;
|
||||
// Pre-fill from editor if there's content; open blank if editor is empty.
|
||||
const content = buildCurrentTextContent();
|
||||
setScheduleContent(content);
|
||||
setScheduleOpen(true);
|
||||
}, [buildCurrentTextContent]);
|
||||
}, [buildCurrentTextContent, isEncrypted]);
|
||||
|
||||
const handleScheduled = useCallback(
|
||||
(delayId: string, sendAt: number, content: IContent) => {
|
||||
@@ -823,18 +837,38 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
return;
|
||||
}
|
||||
|
||||
const uploadRes = await mx.uploadContent(
|
||||
new File([blob], 'image.gif', { type: 'image/gif' }),
|
||||
{ type: 'image/gif', name: 'image.gif', includeFilename: false },
|
||||
);
|
||||
const mxcUrl = (uploadRes as { content_uri: string }).content_uri;
|
||||
if (!mxcUrl) return;
|
||||
mx.sendMessage(roomId, threadRootId ?? null, {
|
||||
const gifFile = new File([blob], 'image.gif', { type: 'image/gif' });
|
||||
const baseContent = {
|
||||
msgtype: MsgType.Image,
|
||||
body: 'image.gif',
|
||||
url: mxcUrl,
|
||||
info: { mimetype: 'image/gif', w, h, size: blob.size },
|
||||
});
|
||||
};
|
||||
|
||||
// Mirror the attachment/voice paths: in an encrypted room the media
|
||||
// itself must be encrypted, otherwise the homeserver (and anyone with
|
||||
// the mxc URI) can see the GIF even though the event body is encrypted.
|
||||
if (room.hasEncryptionStateEvent()) {
|
||||
const { encInfo, file: encBlob } = await encryptFile(gifFile);
|
||||
const uploadRes = await mx.uploadContent(encBlob);
|
||||
const mxcUrl = (uploadRes as { content_uri: string }).content_uri;
|
||||
if (!mxcUrl) return;
|
||||
mx.sendMessage(roomId, threadRootId ?? null, {
|
||||
...baseContent,
|
||||
file: { ...encInfo, url: mxcUrl },
|
||||
} as any);
|
||||
} else {
|
||||
const uploadRes = await mx.uploadContent(gifFile, {
|
||||
type: 'image/gif',
|
||||
name: 'image.gif',
|
||||
includeFilename: false,
|
||||
});
|
||||
const mxcUrl = (uploadRes as { content_uri: string }).content_uri;
|
||||
if (!mxcUrl) return;
|
||||
mx.sendMessage(roomId, threadRootId ?? null, {
|
||||
...baseContent,
|
||||
url: mxcUrl,
|
||||
} as any);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('GIF send failed:', e instanceof Error ? e.message : 'unknown error');
|
||||
if (!alive()) return;
|
||||
@@ -844,7 +878,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
if (alive()) setGifUploading(false);
|
||||
}
|
||||
},
|
||||
[mx, roomId, threadRootId, alive],
|
||||
[mx, room, roomId, threadRootId, alive],
|
||||
);
|
||||
|
||||
const handleStickerSelect = useCallback(
|
||||
|
||||
Reference in New Issue
Block a user