# Security

Hosts treat every hotty document as untrusted. Because documents arrive over
the pty output stream—which may come from a remote server, an untrusted tool,
or a file someone runs `cat` on—the host enforces strict sandboxing on all
markup regardless of source ([§12](https://hotty.neuroplast.io/docs/protocol#12-security)). A surface can
only display what your program sends and report what the user explicitly does.

## What a surface cannot do

Surfaces have no scripting, no ambient network access, and no navigation. Every
host must enforce that:

- **No scripts execute:** `<script>` elements, inline event-handler attributes,
  `javascript:` URLs, and scripts inside SVG are all blocked.
- **No unauthorized fetches occur:** both HTML attributes and CSS can load only
  in-band resources (`cid:`), `data:` URLs, and URLs permitted by the active
  network policy.
- **No navigation occurs:** links, form submissions, and
  `<meta http-equiv=refresh>` never navigate; links and forms only emit events.
- **No popups or dialogs open:** surfaces cannot open windows, tabs, alerts, or
  native dialogs.
- **Focus cannot be stolen:** `autofocus` is ignored; only user interaction or
  an explicit `a=focus` command gives a surface keyboard focus.
- **Rendering stays clipped:** nothing can paint outside the surface's placed
  rectangle, including `position: fixed` elements.
- **No host state is readable:** surfaces cannot inspect terminal cells, the
  clipboard, other surfaces, or any terminal state beyond the host stylesheet
  ([§8](https://hotty.neuroplast.io/docs/protocol#8-the-host-stylesheet)).

Prefetch hints and nested documents (`<iframe>`, `<object>`, `<embed>`) are
never fetched. Surfaces have no cookies or web storage
([§1](https://hotty.neuroplast.io/docs/protocol#non-goals)). Events carry only your program's own element
IDs and explicit user input.

## Links and forms

Clicking a link emits a `click` event containing `href` and the resolved `url`,
and does not navigate ([Input](https://hotty.neuroplast.io/docs/concepts/input#links)). Submitting a form emits a
`submit` event containing its field values to your program and makes no network
request.

The only link handled directly by the host is a hyperlink with
`target="_blank"` ([§9](https://hotty.neuroplast.io/docs/protocol#9-events)). The host treats it identically
to an OSC 8 hyperlink in terminal cells, applying the same activation gesture,
allowed URL schemes, confirmation prompts, and user preferences.

As a result, a surface cannot open any link that the program could not already
emit via OSC 8 in plain terminal output. `<base href>` only controls how
relative URLs resolve ([§7.3](https://hotty.neuroplast.io/docs/protocol#73-the-base-url)); it grants no
permissions.

## The network is the user's to grant

A surface cannot make network requests unless both the host and the document
explicitly permit them ([§7.2](https://hotty.neuroplast.io/docs/protocol#72-the-network)). Because any
outbound request leaks that a document was viewed (and from where), terminals
should start with an empty network policy by default.

A URL is fetched only when allowed by both halves of the policy:

- **Host policy:** configured by the user or embedding page, listing allowed
  sources per directive (`img-src`, `media-src`, `font-src`, `style-src`). Each
  source is an origin or `https:` for any HTTPS origin.
- **Document policy:** declared by the document using CSP syntax in a `<meta>`
  tag. Without it, the document requests no network access.

```text
host    → ESC ] 7279 ; a=ok:n=1:re=q ; {"v": "0.10", …, "net": {"img-src": ["https://example.com"]}} ST
program → ESC ] 7279 ; a=doc:s=post ; <meta name="hotty-network" content="img-src https://example.com"> <img src="https://example.com/cover.png"> ST
```

The host advertises its policy under `net` in its capability response so
programs know which origins are permitted. A document can never exceed the
host's policy, and blocked URLs fail like missing resources.

Hosts should omit `Referer` headers and credentials (such as cookies) on all
fetches. See [Images and the network](https://hotty.neuroplast.io/docs/concepts/resources) for details on requesting
and using network assets.

## What a host checks

Hosts should enforce these restrictions with two independent layers of defense
in depth ([§12](https://hotty.neuroplast.io/docs/protocol#12-security)):

- **Sanitization:** strip executable, navigational, and disallowed elements and
  attributes before markup enters a live DOM.
- **Engine/network enforcement:** block disallowed fetches at the rendering
  engine or network layer regardless of what passes sanitization.

The second layer is essential because HTML attribute sanitizers alone can miss
CSS-initiated fetches (`url()`, `@import`, `@font-face`).

Browser-based hosts also inherit the CSP and sandboxing rules of their
embedding page; consult the host's documentation for required host-page
headers. For example, `xterm-addon-hotty` renders each surface inside a
sandboxed iframe ([Choose a host](https://hotty.neuroplast.io/docs/hosts#a-browser)).

Hosts also protect terminal stability with resource and time limits
([§13](https://hotty.neuroplast.io/docs/protocol#13-limits)): rejecting operations that exceed quotas with
`EQUOTA`, dropping oversized messages, and aborting slow renders with `EBUDGET`
so a single document cannot freeze the terminal.

## What a program still does

Host sandboxing prevents malicious markup from escaping the surface, but it
cannot validate your application logic. Programs remain responsible for:

- **Escaping untrusted text:** the host cannot distinguish your template markup
  from interpolated user or external data. Always HTML-escape untrusted strings
  to prevent injected links, forms, or network requests.
- **Validating URLs before opening them:** if your program opens `url` from a
  link `click` event via the OS, you take on the terminal's security role—filter
  allowed URL schemes and prompt the user when appropriate.
- **Detaching persistent surfaces:** any active surface left on screen after
  your program exits will write events into the next foreground process (such
  as the user's shell). Create persistent surfaces detached, or detach them
  before exiting ([§5.5](https://hotty.neuroplast.io/docs/protocol#55-ownership-adetach)).
- **Draining pending replies:** read responses to your commands before exiting,
  and cancel any pending deferred capability query
  ([Degrading](https://hotty.neuroplast.io/docs/concepts/degrading#a-late-answer)).
- **Handling sensitive text:** surface content is part of terminal output. It
  persists in scrollback alongside its anchor line and is searchable and
  copyable like terminal cells ([§11](https://hotty.neuroplast.io/docs/protocol#11-text)). Only display
  secrets on a surface where you would also be comfortable printing them.

Detached surfaces emit no events and disable their form controls, though
`target="_blank"` hyperlinks remain clickable because they are handled directly
by the terminal.
