From 0691fd713f83fb03736bbcf1d64fdbe5eff2cbe3 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Mon, 26 Oct 2026 13:46:45 -0500 Subject: [PATCH 1/4] Carry encrypted attachments through Matrix send, poll, and download --- .github/workflows/ci.yaml | 7 +- DESCRIPTION | 4 +- R/matrix-attachments.R | 27 +++++++ R/matrix.R | 53 ++++++-------- inst/tinytest/test_matrix.R | 26 +++---- inst/tinytest/test_matrix_encrypted_media.R | 79 +++++++++++++++++++++ 6 files changed, 151 insertions(+), 45 deletions(-) create mode 100644 R/matrix-attachments.R create mode 100644 inst/tinytest/test_matrix_encrypted_media.R diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index d36f9cb..eb757c2 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -40,10 +40,15 @@ jobs: run: | Rscript -e ' if (isTRUE(getOption("rapt.enabled"))) rapt::disable() - remotes::install_cran(c("mx.api", "mx.crypto", "mx.client"), + remotes::install_cran("mx.api", repos = c("https://cornball-ai.github.io/drat", "https://cran.r-project.org"), type = "source", upgrade = "never") + # Pinned attachment support until these versions reach the drat/CRAN. + install.packages("https://github.com/cornball-ai/mx.crypto/archive/74980d6453b71bc6eac9639416e0ec96df971ba9.tar.gz", + repos = NULL, type = "source") + install.packages("https://github.com/cornball-ai/mx.client/archive/f3bece6891572eb120ca5f942ee7227df2dc2422.tar.gz", + repos = NULL, type = "source") remotes::install_deps(".", dependencies = TRUE, repos = "https://cran.r-project.org", upgrade = "never")' diff --git a/DESCRIPTION b/DESCRIPTION index 1090ff4..ae8e804 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,8 +23,8 @@ BugReports: https://github.com/cornball-ai/chat.api/issues Suggests: httr, mx.api, - mx.client (>= 0.2.1.1), - mx.crypto (>= 0.2.2), + mx.client (>= 0.2.1.4), + mx.crypto (>= 0.2.2.2), slackr, telegram, tinytest diff --git a/R/matrix-attachments.R b/R/matrix-attachments.R new file mode 100644 index 0000000..f2209b3 --- /dev/null +++ b/R/matrix-attachments.R @@ -0,0 +1,27 @@ +matrix_attachment_crypto_available <- function() { + requireNamespace("mx.client", quietly = TRUE) && + requireNamespace("mx.crypto", quietly = TRUE) && + all(c("mx_upload_encrypted", "mx_download_encrypted") %in% + getNamespaceExports("mx.client")) && + all(c("mxc_attachment_encrypt", "mxc_attachment_decrypt") %in% + getNamespaceExports("mx.crypto")) +} + +# Reuse the media extractor on the decrypted inner event. The outer sync +# contains only Megolm ciphertext and cannot supply attachment metadata. +matrix_decrypted_attachments <- function(event, self_id) { + if (!is.list(event$content) || !matrix_media_available()) { + return(NULL) + } + inner <- list(type = event$type %||% "m.room.message", + content = event$content, event_id = event$event_id, + sender = event$sender, origin_server_ts = event$ts) + sync <- list(rooms = list(join = setNames(list(list( + timeline = list(events = list(inner)))), event$room_id))) + records <- mx.client::mx_extract_media_events(sync, self_id) + if (!length(records)) return(NULL) + lapply(records, function(a) chat_attachment( + id = as.character(a$url), name = a$filename %||% a$body %||% NA_character_, + mime = a$mime %||% NA_character_, bytes = a$size %||% NA_integer_, + url = as.character(a$url), sha256 = a$sha256 %||% NA_character_, raw = a)) +} diff --git a/R/matrix.R b/R/matrix.R index 4964d76..c0dbace 100644 --- a/R/matrix.R +++ b/R/matrix.R @@ -608,6 +608,7 @@ chat_poll.chat_matrix <- function(client, since = NULL, timeout = NULL, ...) { mentions = unlist(d$mentions, use.names = FALSE), encrypted = TRUE, sender_verified = isTRUE(d$sender_verified), + attachments = matrix_decrypted_attachments(d, res$client$user_id), raw = d) } # Inbound media. A picture is its own m.room.message event on @@ -721,21 +722,22 @@ chat_send.chat_matrix <- function(client, channel, text, "so the reply would leave the thread. Check ", "chat_capabilities()$threads.", call. = FALSE) } - if (encrypted && !is.null(files)) { - # mx_send_media() posts an ordinary cleartext m.file event: the - # upload is not encrypted, and neither is the URL. There is no - # encrypted-attachment path to fall back to, so this refuses - # rather than leaking. chat_capabilities()$files reports FALSE on - # an e2ee client for the same reason. + if (encrypted && !is.null(files) && !matrix_attachment_crypto_available()) { stop("chat.api: cannot send attachments to the encrypted room ", - channel, ". Matrix media uploads are not encrypted by this ", - "adapter, and posting them would put the file on the ", - "homeserver in the clear.", call. = FALSE) + channel, "; install mx.client >= 0.2.1.4 and mx.crypto >= 0.2.2.2.", + call. = FALSE) } media_ids <- character() if (!is.null(files)) { for (f in files) { - event <- client$media_fn(client$env$mx, f, room = channel) + if (encrypted) { + content <- mx.client::mx_upload_encrypted(client$env$mx, f) + event <- client$crypto_ops$send(crypto, client$env$mx, channel, + content$body, msgtype = content$msgtype, + extra = content[c("file", "info")]) + } else { + event <- client$media_fn(client$env$mx, f, room = channel) + } media_ids <- c(media_ids, as.character(event)) } } @@ -884,16 +886,13 @@ chat_channel_invite.chat_matrix <- function(client, channel, user, ...) { #' @export chat_download.chat_matrix <- function(client, attachment, dest = NULL, ...) { dest <- attachment_dest(attachment, dest) - # An encrypted attachment is refused rather than fetched. The bytes - # behind the mxc URL are ciphertext, and this adapter has no - # decryption path for media -- downloading them would hand the - # caller a file that is not the picture, with nothing to say so. - # The same bargain chat_send() makes refusing to upload into an - # encrypted room. if (isTRUE(attachment$raw$encrypted)) { - stop("chat.api: attachment ", attachment$id, " is encrypted, and ", - "this adapter cannot decrypt media. Fetching it would ", - "write ciphertext to ", dest, ".", call. = FALSE) + if (!matrix_attachment_crypto_available()) { + stop("chat.api: cannot decrypt media; install mx.client >= 0.2.1.4 ", + "and mx.crypto >= 0.2.2.2", call. = FALSE) + } + return(mx.client::mx_download_encrypted(client$env$mx, + attachment$raw$file, dest)) } url <- attachment$url if (!is.character(url) || length(url) != 1L || is.na(url) || !nzchar(url)) { @@ -985,11 +984,8 @@ chat_capabilities.chat_matrix <- function(client, ...) { # because the context is built on first use, and a capability that # flipped after the first poll would be worse than either answer. # - # files is FALSE on an e2ee client. mx_send_media() posts a cleartext - # m.file event whatever the room's encryption state says, and this - # adapter has no encrypted-attachment path, so chat_send() refuses - # attachments to an encrypted room. TRUE would advertise something - # that fails in exactly the rooms such a client exists for. + # Encrypted files require both the attachment primitive and upload + # helper. Older optional packages retain the fail-closed refusal. list(threads = matrix_threads_available() && !isTRUE(client$e2ee), thread_replies = TRUE, # An edit works encrypted too: the Megolm path carries the @@ -1004,7 +1000,7 @@ chat_capabilities.chat_matrix <- function(client, ...) { set_identity = TRUE, avatar = TRUE, relogin = TRUE, channel_create = TRUE, leave = TRUE, channel_invite = TRUE, set_state = TRUE, - files = !isTRUE(client$e2ee), + files = !isTRUE(client$e2ee) || matrix_attachment_crypto_available(), # attachments tracks the installed mx.client, the way # reaction_events does: mx_extract_media_events() arrived in # 0.2.0.6, and on an older build the poll has no source for @@ -1012,11 +1008,8 @@ chat_capabilities.chat_matrix <- function(client, ...) { # media messages come back out of chat_poll(), and # chat_download() fetches their bytes. # - # TRUE on an e2ee client too, unlike outbound files. The - # events are reported either way; what an encrypted - # attachment cannot do is be fetched, and chat_download() - # refuses that one rather than the whole room's media going - # unmentioned. + # Events remain visible with older dependencies; fetching encrypted + # bytes requires matrix_attachment_crypto_available(). attachments = matrix_media_capable(client), typing = TRUE, e2ee = isTRUE(client$e2ee), # A call's media keys travel over Olm, so only an e2ee client diff --git a/inst/tinytest/test_matrix.R b/inst/tinytest/test_matrix.R index 24916b6..b0646d2 100644 --- a/inst/tinytest/test_matrix.R +++ b/inst/tinytest/test_matrix.R @@ -600,9 +600,9 @@ expect_null(enc$env$crypto) # The capability answers from the setting, not the context, so it does not # flip after the first poll. expect_true(chat_capabilities(enc)$e2ee) -# ... and files is FALSE, because mx_send_media posts a cleartext m.file -# event whatever the room's encryption state says. -expect_false(chat_capabilities(enc)$files) +# Files require the encrypted attachment helpers. +expect_identical(chat_capabilities(enc)$files, + chat.api:::matrix_attachment_crypto_available()) # ... and so is threads: the encrypted send path has nowhere to put an # m.relates_to. A threaded send is refused rather than posted to the # room's main timeline, which is files' bargain and not rich's -- a @@ -887,11 +887,11 @@ plain <- seam_client(.crypto = f$ops, expect_identical(chat_send(plain, "!unknown:ex", "hi"), "$clear1") expect_identical(cleartext$n, 1L) -# ---- Attachments are refused in encrypted rooms ---- -# mx_send_media() posts an ordinary cleartext m.file event. The check used -# to run after the upload loop, so the files reached the homeserver in the -# clear and only the text took the Megolm path -- and an attachment-only -# send never asked at all. +# ---- Older dependencies refuse attachments before any upload ---- +local({ +available <- chat.api:::matrix_attachment_crypto_available +assignInNamespace("matrix_attachment_crypto_available", function() FALSE, "chat.api") +on.exit(assignInNamespace("matrix_attachment_crypto_available", available, "chat.api")) f <- fake_crypto() uploads <- new.env(parent = emptyenv()); uploads$n <- 0L enc <- seam_client(e2ee = TRUE, .crypto = f$ops, @@ -912,6 +912,7 @@ expect_identical(uploads$n, 0L) ids <- chat_send(enc, "!plain:ex", "see these", files = c("/a.png", "/b.png")) expect_identical(ids, c("$ma.png", "$mb.png", "$id")) expect_identical(uploads$n, 2L) +}) # markup, kind, and mentions carry into the encrypted branch. Dropping # them would make an encrypted room silently lose formatting and pings @@ -1646,10 +1647,11 @@ local({ expect_true(file.exists(dest)) }) -# An encrypted attachment is refused rather than fetched: the bytes -# behind the URL are ciphertext and this adapter cannot decrypt media, -# so downloading would write a file that is not the picture. +# Older dependencies refuse encrypted media before fetching ciphertext. local({ + available <- chat.api:::matrix_attachment_crypto_available + assignInNamespace("matrix_attachment_crypto_available", function() FALSE, "chat.api") + on.exit(assignInNamespace("matrix_attachment_crypto_available", available, "chat.api")) cl <- seam_client(.download = function(...) stop("must not fetch")) enc_att <- chat_attachment(id = "mxc://ex/enc", name = "secret.png", url = "mxc://ex/enc", @@ -1680,7 +1682,7 @@ local({ e2ee = TRUE, .crypto = fake_crypto()$ops) caps <- chat_capabilities(cl) expect_true(caps$attachments) - expect_false(caps$files) + expect_identical(caps$files, chat.api:::matrix_attachment_crypto_available()) }) # ---- Threads ---- diff --git a/inst/tinytest/test_matrix_encrypted_media.R b/inst/tinytest/test_matrix_encrypted_media.R new file mode 100644 index 0000000..3aa2c95 --- /dev/null +++ b/inst/tinytest/test_matrix_encrypted_media.R @@ -0,0 +1,79 @@ +library(chat.api) +if (!chat.api:::matrix_attachment_crypto_available()) { + exit_file("encrypted attachments need current mx.client and mx.crypto") +} +local({ + root <- tempfile("chat-media-") + dir.create(root) + on.exit(unlink(root, recursive = TRUE), add = TRUE) + chat.api:::matrix_crypto_forget() + on.exit(chat.api:::matrix_crypto_forget(), add = TRUE) + cfg <- list(server = "https://example.invalid", token = "fixture-token", + user_id = "@bot:example.invalid", device_id = "FIXTURE") + path <- file.path(root, "plot.png") + bytes <- as.raw(0:255) + writeBin(bytes, path) + upload <- mx.api::mx_upload + download <- mx.api::mx_download + on.exit({ + assignInNamespace("mx_upload", upload, "mx.api") + assignInNamespace("mx_download", download, "mx.api") + }, add = TRUE) + payload <- NULL + sent <- list() + decrypted <- list() + assignInNamespace("mx_upload", function(session, path, content_type, filename) { + expect_identical(session$token, "rotated-fixture-token") + expect_identical(content_type, "application/octet-stream") + expect_identical(filename, "encrypted") + payload <<- readBin(path, "raw", file.size(path)) + "mxc://example.invalid/media" + }, "mx.api") + assignInNamespace("mx_download", function(session, mxc_url, dest) { + expect_identical(session$token, "rotated-fixture-token") + writeBin(payload, dest) + invisible(dest) + }, "mx.api") + ops <- list(init = function(...) new.env(), encrypted = function(...) TRUE, + decrypt = function(...) decrypted, + send = function(crypto, mx, room_id, text, msgtype, extra = NULL, ...) { + expect_identical(mx$token, "rotated-fixture-token") + sent[[length(sent) + 1L]] <<- c(list(body = text, msgtype = msgtype), extra) + paste0("$event", length(sent)) + }) + client <- chat_matrix(mx = cfg, e2ee = TRUE, .crypto = ops, + .sync = function(mx, ...) list(client = mx, sync = list(), first_run = FALSE), + .save = function(mx, ...) mx, .extract = function(...) list(), + .send = function(...) stop("plaintext send"), + .media = function(...) stop("plaintext upload")) + client$env$mx$token <- "rotated-fixture-token" + expect_true(chat_capabilities(client)$files) + expect_identical(chat_send(client, "!room:example.invalid", "", files = path), "$event1") + expect_length(sent, 1L) # no empty text bubble + expect_null(sent[[1]]$url) + expect_identical(sent[[1]]$msgtype, "m.image") + expect_false(identical(payload, bytes)) + expect_identical(mx.crypto::mxc_attachment_decrypt(payload, sent[[1]]$file), bytes) + # Incoming Megolm-decoded media must carry the encrypted-file object, + # rather than being flattened into a filename-only text message. + decrypted <- list(list(type = "m.room.message", content = sent[[1]], + body = "plot.png", msgtype = "m.image", room_id = "!room:example.invalid", + event_id = "$incoming", sender = "@alice:example.invalid", + sender_verified = TRUE)) + messages <- chat_poll(client)$messages + expect_length(messages, 1L) + expect_true(messages[[1]]$encrypted) + expect_true(messages[[1]]$sender_verified) + expect_length(messages[[1]]$attachments, 1L) + attachment <- messages[[1]]$attachments[[1]] + expect_identical(attachment$raw$file, sent[[1]]$file) + dest <- file.path(root, "received.png") + expect_identical(chat_download(client, attachment, dest), dest) + expect_identical(readBin(dest, "raw", 256), bytes) + payload[1] <- as.raw(bitwXor(as.integer(payload[1]), 1L)) + expect_error(chat_download(client, attachment, dest), "integrity") + expect_identical(readBin(dest, "raw", 256), bytes) + expect_error(chat_send(client, "!room:example.invalid", "", files = path, + thread = "$thread"), "leave the thread") + expect_length(sent, 1L) +}) From 34c62f1a88035173f96a71e9b4c3665de6f67ec5 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Mon, 26 Oct 2026 13:46:52 -0500 Subject: [PATCH 2/4] Bump version to 0.1.0.4 --- DESCRIPTION | 4 ++-- NEWS.md | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ae8e804..676e5bf 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: chat.api Type: Package Title: Transport-Agnostic Chat Contract -Version: 0.1.0.3 -Date: 2026-10-21 +Version: 0.1.0.4 +Date: 2026-10-26 Authors@R: c( person("Troy", "Hernandez", role = c("aut", "cre"), email = "troy@cornball.ai", diff --git a/NEWS.md b/NEWS.md index e1023fd..8e7f9d6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +# chat.api 0.1.0.4 + +- Send encrypted Matrix attachments using the adapter's existing crypto + context, with ciphertext-only uploads and encrypted file metadata. +- Preserve attachment metadata from decrypted room events and verify encrypted + downloads before returning plaintext. File capability reporting follows the + installed attachment helpers; older dependencies still refuse safely. + # chat.api 0.1.0.3 * `chat_set_identity()` takes `avatar`, a path to an image, alongside From 32a707a5547c7c8334415405ebb07dee801b14b6 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Mon, 26 Oct 2026 13:50:35 -0500 Subject: [PATCH 3/4] Use the source installer after disabling rapt in CI --- .github/workflows/ci.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index eb757c2..b9e8666 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -45,9 +45,9 @@ jobs: "https://cran.r-project.org"), type = "source", upgrade = "never") # Pinned attachment support until these versions reach the drat/CRAN. - install.packages("https://github.com/cornball-ai/mx.crypto/archive/74980d6453b71bc6eac9639416e0ec96df971ba9.tar.gz", + utils::install.packages("https://github.com/cornball-ai/mx.crypto/archive/74980d6453b71bc6eac9639416e0ec96df971ba9.tar.gz", repos = NULL, type = "source") - install.packages("https://github.com/cornball-ai/mx.client/archive/f3bece6891572eb120ca5f942ee7227df2dc2422.tar.gz", + utils::install.packages("https://github.com/cornball-ai/mx.client/archive/f3bece6891572eb120ca5f942ee7227df2dc2422.tar.gz", repos = NULL, type = "source") remotes::install_deps(".", dependencies = TRUE, repos = "https://cran.r-project.org", From 1c4746fd7f1ee1b0ca32b1ae83b32bf451beae8c Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Mon, 26 Oct 2026 13:56:47 -0500 Subject: [PATCH 4/4] Document encrypted attachment support and dependency fallback --- R/matrix.R | 9 +++++---- man/chat_matrix.Rd | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/R/matrix.R b/R/matrix.R index c0dbace..852459c 100644 --- a/R/matrix.R +++ b/R/matrix.R @@ -50,10 +50,11 @@ #' any direct send already carries, since \code{mx_with_relogin()} #' wraps only the sync. The sync cursor is held back until the crypto #' state is safe, on disk and on the client, so a sync carrying a room -#' key is not marked consumed until that key is stored. And attachments -#' are refused in encrypted rooms, because Matrix media uploads are not -#' encrypted by this adapter -- \code{\link{chat_capabilities}} reports -#' \code{files = FALSE} to match. +#' key is not marked consumed until that key is stored. Attachments use +#' ciphertext uploads and encrypted file metadata with mx.client 0.2.1.4 +#' and mx.crypto 0.2.2.2 or later. Older dependencies refuse encrypted +#' attachments before upload; \code{\link{chat_capabilities}} reports +#' \code{files} according to the installed helpers. #' @param crypto_store Character or NULL. Directory holding the pickled #' Olm account and sessions. NULL derives it from \code{app} via #' \code{mx.client::mx_crypto_store_dir()}, with a per-device diff --git a/man/chat_matrix.Rd b/man/chat_matrix.Rd index e4f1091..b8e6b04 100644 --- a/man/chat_matrix.Rd +++ b/man/chat_matrix.Rd @@ -87,10 +87,11 @@ before any poll publishes with the token it has -- the same exposure any direct send already carries, since \code{mx_with_relogin()} wraps only the sync. The sync cursor is held back until the crypto state is safe, on disk and on the client, so a sync carrying a room -key is not marked consumed until that key is stored. And attachments -are refused in encrypted rooms, because Matrix media uploads are not -encrypted by this adapter -- \code{\link{chat_capabilities}} reports -\code{files = FALSE} to match.} +key is not marked consumed until that key is stored. Attachments use +ciphertext uploads and encrypted file metadata with mx.client 0.2.1.4 +and mx.crypto 0.2.2.2 or later. Older dependencies refuse encrypted +attachments before upload; \code{\link{chat_capabilities}} reports +\code{files} according to the installed helpers.} \item{crypto_store}{Character or NULL. Directory holding the pickled Olm account and sessions. NULL derives it from \code{app} via