# File-preview postMessage contract (CFA-1848)

canvas-rce's rendered content (an iframe or the top-level document, via
`enhance-user-content`) opens a file preview in the parent Canvas page by
posting a message rather than calling a shared function. This is a
security-relevant, origin-checked cross-window contract, not just a
convenience seam — it must stay compatible if canvas-rce is extracted into
its own package/repo.

## Sender

`packages/canvas-rce/src/enhance-user-content/instructure_helper.js`,
`showFilePreviewInOverlay()`. Posts to `window.top` with an explicit
`canvasOrigin` target (never `'*'`):

```js
window.top.postMessage(
  {
    subject: 'preview_file', // version 1 (implicit, undeclared)
    file_id,
    verifier, // optional
    access_token, // optional, requires instfs_id
    instfs_id, // optional, requires access_token
    location, // optional
  },
  canvasOrigin,
)
```

## Receiver

`ui/shared/enhanced-user-content/jquery/index.js`, `wireUpFilePreview()`.
Listens on `window` for `message` events and only acts when:

1. `event.data.subject === 'preview_file'` (message shape check), and
2. `event.origin === (ENV.DEEP_LINKING_POST_MESSAGE_ORIGIN || location.origin)`,
   **or** `event.source` is a same-page iframe that looks like an LTI tool
   launch (`isFromToolLaunchIframe`, see QUIZ-18084).

The origin check exists because any cross-origin frame — a malicious LTI
tool, an injected iframe, a pop-under — could otherwise trigger the file
preview overlay with attacker-supplied `file_id`/`verifier`/`access_token`
in the victim's session.

## Versioning going forward

There is no explicit version field today; `subject: 'preview_file'` is
implicitly "v1". Before canvas-rce moves out of this repo (so the sender
and receiver can ship on independent release cadences), any change to
this payload shape must:

- Add a `version` field to the message rather than repurposing `subject`,
  so an old sender and a new receiver (or vice versa) fail closed instead
  of misparsing fields.
- Keep the receiver's origin check as the sole trust boundary — do not
  loosen it to accommodate a new sender location.
- Update both this doc and the inline contract comments in
  `instructure_helper.js` / `enhanced-user-content/jquery/index.js`
  together, since they're the only place the two sides of the contract
  are described.
