# seamless

## Identity

- **Path:** `packages/seamless`. **Published as:** `seamless` on npm — **unscoped**, MIT, © Travis Tidwell. `main` is `build/seamless.parent.min.js`.
- **OSS sync:** has an `ossRepo` block (`github.com/travist/seamless.js`, `srcPath: "."`) — the **whole directory mirrors to a public repo**, which has external users beyond this monorepo. Keep in-repo-only detail in `/docs/`, not source comments.
- **Module system:** none. Every file is a **bare ES5 IIFE** loaded as a plain `<script>`. **Era:** 2014, unchanged. No transpile step — `var`/`function` only, no `const`/arrow/template literals.
- **Purpose:** cross-origin iframe embedding — the child reports its height over `postMessage` and the parent resizes the iframe so the embed looks inline. This is what the Form.io embed snippet loads on customer pages.

## Floor — immutable musts

- **Cross-file communication is `window` only.** `vendor/postmessage.js` sets `window.pm`; the `src/seamless.*.js` files set `window.SeamlessBase` / `SeamlessConnection` / `seamless`. No file has a top-level declaration another file reads (the lone exception, `NO_JQUERY`, is private to `vendor/postmessage.js`). Don't introduce one — the build only concatenates, so a shared top-level `var` is invisible coupling.
- **`pm.bind()` does NOT validate the sender.** `vendor/postmessage.js` skips its origin check whenever a listener registered without an explicit origin (`origin: origin || $.pm.origin`, default `null`), and every handler here registers that way. **Any handler you add must check `event.source` and `event.origin` itself.** FIO-12090 was this: `seamless_noiframe` piped `data.href` from any window into `window.location.replace()`. `seamless_error` (parent) and `seamless_connect` / `seamless_styles` (child) are still unguarded — treat them as such.
- **Validate the url you navigate to, not the one you were handed.** The handler strips the `noiframe` flag before checking the origin, because `[^&?#]+` also matches `@` and `/` — `https://x:noiframe=y@forms.example/` has origin `forms.example` but strips down to `https://x/`. Check-then-mutate silently moves the host.
- **`vendor/postmessage.js` must stay out of `lib/`.** Upstream keeps it at `lib/postmessage/postmessage.js`; the root `.gitignore` ignores `lib` globally, so that path is **silently untracked** here. The `vendor/` location and the two `gulpfile.js` references to it are load-bearing.
- **Keep `gulp-uglify` at `^2` (uglify-js 2.8.29).** That exact version reproduces every checked-in artifact byte-for-byte; bumping it rewrites all eight bundles and makes any real diff unreviewable.
- **`build/` is a turbo output and gitignored** — but the npm tarball needs it, and the package has no `files` allowlist, so publish only from a built tree.
- **Four bundle variants, all required:** `parent`/`child` × with/without the bundled `postmessage` (`.nopm`), each plus a `.min`. Consumers pick different ones; don't drop a variant.

## Blast radius

**6 workspace dependents** — `formio-app`, `@formio/viewer`, `uswds-viewer`, `pro.formview.io`, `formmanager`, `@formio/pdf` — **understated on two axes**:

1. `formio-app` bundles `seamless.parent` into `scripts.js`, which is the portal (`portal.form.io`).
2. The viewers' gulp tasks copy `node_modules/seamless/build/**` to `dist/lib/seamless`, which is the copy **customer pages** load via the embed snippet (`<appBase>/lib/seamless/seamless.parent.min.js`). A change here reaches third-party sites, not just this repo.

## Test & build

```sh
pnpm -F seamless test    # mocha + jsdom — test/seamless.parent.test.js
pnpm -F seamless lint    # gulp lint (gulp-eslint, browser env, strict)
pnpm -F seamless build   # gulp build → 8 files in build/
```

"Green" = `test` + `lint` pass. The whole toolchain (gulp 4, uglify-js 2.8.29, jsdom 24) runs on the repo's Node 22 — there is no Node-10 requirement despite the 2014 dependency tree.

**Testing navigation is indirect.** `window.location.replace` is non-configurable in jsdom, so it can't be spied on; the suite counts jsdom's "navigation not implemented" errors instead. That channel stays silent for `javascript:` URLs, so a `javascript:` assertion passes against vulnerable code — a false pass. Assert non-http(s) schemes with `vbscript:`/`data:`, which take the identical branch.

## Cross-cutting triggers

- **Adding or editing any `pm.bind` handler** → re-read the sender-validation bullet above. The parent's `seamless_noiframe` guard (`isFromRegisteredFrame` + `httpOrigin` vs `event.origin`) is the reference shape.
- **Changing `build/` output paths or variant names** → `formio-app/src/modules.js` imports `seamless/build/seamless.parent` directly, and four gulpfiles copy `node_modules/seamless/build/**`. Both break silently.
- **Touching the parent's navigation path** → it is one half of the portal XSS surface; the other half is the `script-src * 'unsafe-inline'` CSP in [`apps/formio-server/src/middleware/cspSettings.js`](../../apps/formio-server/src/middleware/cspSettings.js) and `helmet.js`, which is what makes a `javascript:` navigation executable.
- **Publishing** → this is the source of truth now; sync to `travist/seamless.js` rather than patching that repo directly.

## References

- Repo-wide: [`/CLAUDE.md`](../../CLAUDE.md), [`/STANDARDS.md`](../../STANDARDS.md)
- Upstream: [`github.com/travist/seamless.js`](https://github.com/travist/seamless.js)
- Consumer side: [`apps/formio-app`](../../apps/formio-app), [`packages/formio-pdf/CLAUDE.md`](../formio-pdf/CLAUDE.md)
