# Encrypted attachments and object storage (https://docs.open-e2ee.dev/build/attachments-object-storage.md)

- Status: stable
- Applies to: 15.0.0
- Platforms: Expo · Browser · Node
- Prereqs: A working client and an object store you control
- Reading time: 12 min

Signal Protocol messages suit small encrypted payloads, not large binary objects. A ten-megabyte photo does not belong inside a ratchet step. So attachments use two encryption layers:

1. The client encrypts file bytes locally, and only ciphertext enters remote storage.
2. The end-to-end encrypted message carries the key, digest, sizes, and opaque object identifier.

The SDK states this consequence plainly:

> **The remote object store never receives the media key or plaintext.**

If you lose the message, you lose the ability to decrypt the object. The object
alone is inert.

`adapters.remoteObjectStore` is optional. Configure it only if you move files. Calling `downloadAttachment()` or `deleteRemoteAttachment()` without one throws an `EncryptionError`. Its code is `INITIALIZATION_FAILED`, and its message names the missing config. The call fails instead of silently doing nothing.

**Figure:** A device holding the media key beside an object store bracket containing three opaque ciphertext slabs of unequal size. The media key stays inside the device outline. Only opaque objects and the metadata needed to authorize access cross into the store.

## The `client.media` queue [#the-clientmedia-queue]

`client.media` is the only namespace-shaped property on the client, and it manages attachment work as durable jobs rather than fire-and-forget promises.

```ts
await client.media.upload(input, options);
await client.media.download(input, options);
await client.media.cleanup(input, options);

const result = await client.media.processPending({ limit: 5 });
// { attempted, completed, skipped, failed, expired, results }
```

Each of `upload`, `download`, and `cleanup` returns a `status` and `jobId`. The status is `'completed' | 'pending' | 'skipped' | 'failed'`. A `'pending'` result is not an error. It means the broker queued the job and `processPending()` will drive it. This design lets attachments survive a backgrounded app, a lost network, or a process kill mid-transfer.

Bytes enter the queue through an application-supplied callback rather than hidden package storage. Your app owns draft files, cache paths, and file permissions. The default queue limit is 200 jobs. The queue retries with backoff and stops after the configured attempt count.

Call `processPending()` from whatever your platform gives you for background work. The failure mode of not calling it is an attachment that never uploads and a message that references an object nobody can fetch.

## The file methods [#the-file-methods]

Flat client methods encrypt files directly. They pair a random symmetric key with Signal Protocol, which encrypts that key:

```ts
const { encryptedBlob, keyId, encryptedKey } = await client.encryptFile(
  remoteAddress,
  fileBlob,
  'image/jpeg',
);

const plaintextBlob = await client.decryptFile(remoteAddress, encryptedBlob, encryptedKey);
```

`encryptFiles()` and `decryptFiles()` take arrays and return results in the same order. Each file gets its own key. You can therefore revoke or grant access per file instead of per batch.

For the brokered upload path there are `uploadAttachment(data, { mimeType, ...sendOptions })`, `downloadAttachment(attachment, options?)`, and `deleteRemoteAttachment(attachment, options?)`. These operate on the object store and understand progress, checkpoints, abort signals, and retry policy.

## Object store adapters [#object-store-adapters]

Two concrete adapters ship, both implementing `SignalProtocolRemoteObjectStore`. They apply to `createSignalProtocolClient()` with an application-owned relay. `createHostedSignalProtocolClient()` takes no `remoteObjectStore` adapter, because the OpenE2EE Signal Protocol Relay transport supplies the object store.

### Convex R2 [#convex-r2]

```ts
import { convexR2ObjectStore } from '@open-e2ee/signal-protocol-sdk/remote/object-store/convex-r2';
import { api } from '../convex/_generated/api';

const client = await createSignalProtocolClient({
  identity: { userId },
  adapters: {
    storage,
    relay,
    remoteObjectStore: convexR2ObjectStore({ convex, api: api.signalObjectStore }),
  },
});
```

`ConvexR2ObjectStore` is a client adapter, not a Convex component. The application installs, mounts, and configures `@convex-dev/r2`, owns the R2 bucket and credentials, and exposes authenticated app-owned functions.

`ConvexR2ObjectStoreApi` types the four public functions that your Convex backend exports. A `satisfies` check fails to compile when the kind, arguments, or return type of a function is different from the contract:

```ts
// convex/signalObjectStore.ts exports the four functions. They are your code.
import type { ConvexR2ObjectStoreApi } from '@open-e2ee/signal-protocol-sdk/remote/object-store/convex-r2';
import { api } from './_generated/api';

api.signalObjectStore satisfies ConvexR2ObjectStoreApi;
```

| Function | Kind | Arguments | Returns |
|---|---|---|---|
| `createUpload` | mutation | `requestId`, `preparedAt`, `contentType`, `contentLength`, `digest` as an `ArrayBuffer` | `{ objectId, uploadUrl, expiresAt, headers?, protocol? }` |
| `createDownload` | action | `{ objectId }` | `{ downloadUrl, expiresAt, headers? }`, or `null` when the object is absent |
| `completeUpload` | action | `{ objectId }` | `null` |
| `deleteObject` | mutation | `{ objectId }` | `null` |

`createDownload` and `completeUpload` are actions because they produce time-sensitive credentials or await provider metadata. `createUpload` and `deleteObject` are mutations. Make `completeUpload` and `deleteObject` idempotent. `deleteObject` logically removes the object and schedules the R2 deletion. The SDK does not import `@convex-dev/r2`. Your backend mounts the component, so S3-only consumers do not load it.

### Amazon S3 and S3-compatible storage [#amazon-s3-and-s3-compatible-storage]

```ts
import { s3ObjectStore } from '@open-e2ee/signal-protocol-sdk/remote/object-store/s3';

const remoteObjectStore = s3ObjectStore({
  broker: {
    createUpload: (input) => storageApi.createS3Upload(input),
    createDownload: (input) => storageApi.createS3Download(input),
    completeUpload: (input) => storageApi.completeS3Upload(input),
    deleteObject: (input) => storageApi.deleteS3Object(input),
  },
});
```

`S3ObjectStore` is framework-neutral. AWS SDK clients and AWS credentials remain on the backend that implements the broker.

## The brokered model is the security point [#the-brokered-model-is-the-security-point]

Both adapters are deliberately brokered:

> An authenticated application backend maps a retry-stable `requestId` to a canonical `objectId` and a private provider key, then issues short-lived upload and download operations. **Cloud credentials and unrestricted provider clients do not belong in the app runtime.**

Read that as three separate rules.

**The client never holds provider credentials.** Not a scoped token, not a signing key. It receives short-lived presigned operations for one object, and nothing else. An attacker with full control of a client device gets what that device could already reach.

**The backend names the object. The client does not name it.** The client supplies
`requestId`, a retry-stable key for one logical upload. The backend uses it to
enforce idempotency. The backend maps it to `objectId` and a private provider key.
It never returns that private key. `storageId` is the opaque object identifier
that the backend issues.

**The application must never treat `requestId` or `storageId` as a provider key.**
A client that selects its own storage path could read or overwrite another user's
objects.

**Security requires idempotency.** Because `requestId` is retry-stable, a resumed upload reserves the same object after a crash. It does not orphan one object and create another. The reserve step must be idempotent. The completion step must authorize the caller again. In the Convex backend, `completeUpload` is an action, and its queries and mutations run in separate transactions. Authorize the caller again in the mutation that marks the upload complete.

Every remote object store should receive only ciphertext plus the metadata needed to authorize and construct short-lived operations.

> **Failure mode — WebCrypto has no streaming API, and it never has**
>
> `SubtleCrypto` offers `encrypt()` and `decrypt()` over complete buffers. There is no incremental interface. Issue w3c/webcrypto#73 opened in May 2016 and remains unresolved.
>
> Encrypted attachments therefore need hand-written chunked AEAD framing instead of a pipe. The SDK's `/files` module exposes `streamingEncrypt` and `streamingDecrypt` over segments. The decrypt side must receive the `segmentSize`.
>
> The operational consequence is memory. In a browser, a large attachment keeps the plaintext, ciphertext, and DOM buffers resident at once. Test `maxContentLength` on the weakest device you support. The `50 * 1024 * 1024` value above is a placeholder, not a recommendation.
>
> Expect a considerably lower limit on mobile web than on desktop. Failures appear as tab crashes and out-of-memory kills, not as `EncryptionError`. Your error tracking will not see them.

> **Tradeoff — Chunk size: memory footprint against per-chunk overhead**
>
> Smaller segments cap peak memory and let a transfer resume closer to its stopping point. They require more AEAD tags, framing bytes, and per-chunk work. Larger segments amortize that overhead and run faster on capable hardware. They require a higher memory floor and give coarser resume points. The SDK does not pick for you because the correct value depends on your file-size distribution and weakest supported device. A value that works on a development laptop can crash a four-year-old Android browser.

> **Not covered — Content policy, thumbnails, and cache lifetime**
>
> The application owns cache retention, view-once enforcement, file-system cleanup, and background execution policy. It must also validate content policy before it renders decrypted bytes. Deleting an object from your bucket does not delete remote copies. See [Deletion and revocation](https://docs.open-e2ee.dev/operate/deletion-revocation.md).

**Opacity ledger**

| Artifact | Stays on device | Sent to relay | In object store | Visible as metadata |
|---|---|---|---|---|
| Attachment plaintext bytes | yes | no | no | no |
| Attachment ciphertext (`encryptedBlob`) | staged | no | yes | object size |
| Media key | yes | no | no | no |
| `encryptedKey` (media key sealed to the peer) | yes | yes | no | no |
| `keyId` | yes | yes | no | no |
| Digest and plaintext size | yes | yes | no | no |
| `requestId` (retry-stable idempotency key) | yes | no | yes | yes |
| `objectId` (backend-canonical) | no | no | yes | yes |
| Private provider key | no | no | backend only | no |
| `storageId` (opaque, backend-issued) | yes | yes | yes | yes |
| `contentType` / `allowedContentTypes` | yes | no | yes | yes |
| `maxContentLength` enforcement result | no | no | yes | yes |
| Presigned URL and `downloadExpiresInSeconds` | transient | no | yes | yes |
| `MediaAttachmentPointer` in the message | yes | yes | no | no |
| `identityKeyPair` private half | yes | no | no | no |

The `encryptedKey` row summarizes the design. The key that opens the object travels inside the end-to-end encrypted message. It never enters the object store.

## Next [#next]

- [Encrypted attachments with R2 and S3](https://docs.open-e2ee.dev/guides/encrypted-attachments-r2-s3.md): the full broker walkthrough
- [Relay and prekeys](https://docs.open-e2ee.dev/build/relay-and-prekeys.md): the relay contract that carries the pointer message
- [Deletion and revocation](https://docs.open-e2ee.dev/operate/deletion-revocation.md): what deleting an object does and does not achieve
- [Runtime support](https://docs.open-e2ee.dev/reference/runtime-support.md): WebCrypto and platform crypto availability
