# Images and the network

A surface can load assets sent in-band by your program, and can fetch from the
network only when both the host and the document explicitly allow it
([§7](https://hotty.neuroplast.io/docs/protocol#7-resources)). Images, fonts, and stylesheets travel
alongside your documents over stdout as **resources**. Network access stays
disabled unless the terminal user (or embedding web page) grants permission.

## Resources

A resource is a named binary payload with a MIME type stored by the host for
the duration of the session ([§7.1](https://hotty.neuroplast.io/docs/protocol#71-in-band-resources)). Send
a resource with `a=res`, then reference it in HTML or CSS using `cid:<id>`
(similar to MIME attachments in HTML email):

```text
program → ESC ] 7279 ; a=res:id=logo:type=image/png ; <the PNG's bytes> ST
program → ESC ] 7279 ; a=doc:s=about ; <img src="cid:logo"> <h1>Deploys</h1> ST
```

(Payloads are shown decoded here; on the wire they are base64-encoded.)

Resources are scoped to the session (clarified in 0.8): another program's
document cannot resolve, overwrite, delete, or detect your `cid:kit.css`, even
if it defines a resource with the same name. Multiplexers isolate resources
across panes in the same way.

`cid:` URLs work anywhere a document references an external asset:

- HTML attributes: `src`, `srcset`, `poster`, SVG `href`, and `<link rel=stylesheet href=...>`;
- CSS `url()` and `@import`, whether inside a `style` attribute, a `<style>`
  block, or another `cid:` stylesheet.

A single resource can be shared across multiple surfaces. Send a shared
stylesheet or font once, and reference it from every surface your program
creates.

- **Order-independent:** referencing a `cid:` resource before it arrives is
  valid; the host resolves it once `a=res` arrives. Placements with `f=1` emit
  `fit` if loading the resource changes the document height
  ([Surfaces](https://hotty.neuroplast.io/docs/concepts/surfaces#when-the-content-changes-height)).
- **Live replacement:** sending `a=res` again with the same `id` replaces the
  resource and repaints every surface referencing it—useful for updating
  rendered charts or plots in place.
- **Deletion:** `a=del:id=<id>` removes the resource; subsequent references
  fail as if it never existed.

Hosts enforce a total resource quota reported as `limits.resources` and reject
excess uploads with `EQUOTA`. Because each resource is transmitted in a single
message, `limits.message` also caps the maximum size of an individual resource.
Hosts that enforce message limits should accept at least 1 MiB
([§13](https://hotty.neuroplast.io/docs/protocol#13-limits)).

For small assets, you do not need `a=res`: hosts also support `data:` URLs
([§12](https://hotty.neuroplast.io/docs/protocol#12-security)) and inline `<svg>`.

**Original attributes are preserved:** even if a host internally rewrites
`cid:` references (for example, to `blob:` URLs) to render them, morphs and
events always see the original attribute value (`src="cid:logo"`).

## Why in-band

Sending assets in-band over the terminal stream guarantees that programs work
identically locally, over SSH, and inside containers
([§1](https://hotty.neuroplast.io/docs/protocol#1-introduction)). The host never accesses the program's
filesystem, which may live on a remote machine.

In-band delivery is also a security boundary. Terminal output can originate
from untrusted sources—including a file someone `cat`s—and any outbound network
request leaks viewer identity and IP address to a remote server
([Appendix B](https://hotty.neuroplast.io/docs/protocol#appendix-b-rationale)). Requiring user consent for
network access prevents untrusted markup from phoning home.

## The network

A two-sided **network policy** governs outbound fetches from a surface
([§7.2](https://hotty.neuroplast.io/docs/protocol#72-the-network)). A URL is fetched only if both halves
permit it:

- **Host policy:** configured by the user or the embedding web page. Terminals
  should default to an empty policy (no network access).
- **Document policy:** declared by the document itself. A document that
  declares no policy cannot fetch anything, even if the host permits it.

Because the effective policy is the intersection of both halves, a document can
only narrow what the host allows, and a host grant never triggers fetches for
documents that did not request them.

Both halves list allowed **sources** per **directive**, following Content
Security Policy (CSP) syntax:

| directive | covers |
| --- | --- |
| `img-src` | images: `<img>`, `srcset`, `<picture>`, `poster`, SVG images, and CSS images |
| `media-src` | `<audio>` and `<video>` |
| `font-src` | `@font-face` |
| `style-src` | stylesheets: `<link rel=stylesheet>` and `@import` |

A source is either a specific origin (such as `https://example.com` or
`http://localhost:8080`) or `https:` to match any HTTPS origin. Documents
declare their half using a `<meta>` tag:

```html
<meta name="hotty-network" content="img-src https://example.com; font-src https:">
```

The host advertises its half under `net` in its capability response
([§4](https://hotty.neuroplast.io/docs/protocol#4-detection-and-capabilities)). Check `net` before relying
on remote URLs, and fall back to in-band `a=res` uploads when the host does not
grant the required origin:

```text
host → ESC ] 7279 ; a=ok:n=1:re=q ; {"v": "0.10", …, "net": {"img-src": ["https://example.com"]}} ST
```

Hosts should fetch without sending a `Referer` header or credentials (such as
cookies). Disallowed URLs fail silently like missing resources. Because remote
fetches complete asynchronously after initial layout, content-sized surfaces
should be placed with `f=1` to receive `fit` updates once assets load.

Certain requests are blocked unconditionally regardless of policy: prefetch and
preconnect hints, and nested documents such as `<iframe>`, `<object>`, and
`<embed>` ([Security](https://hotty.neuroplast.io/docs/concepts/security)).

## The base URL

Relative URLs in a document resolve against its base URL
([§7.3](https://hotty.neuroplast.io/docs/protocol#73-the-base-url)): the `href` of the first `<base>`
element if it is an absolute `http` or `https` URL, or `https://hotty.invalid/`
otherwise.

Setting `<base>` does not grant any permissions. Relative image fetches still
require network policy approval, and relative links never navigate. In this
example, a blog post fetches its cover image from an allowed origin and handles
a link click ([Appendix A](https://hotty.neuroplast.io/docs/protocol#appendix-a-examples)):

```text
program → ESC ] 7279 ; a=doc:s=post ; <base href="https://example.com/blog/"> <meta name="hotty-network" content="img-src https://example.com"> <img src="cover.png"> <a href="../about">About</a> ST
          (the host fetches https://example.com/blog/cover.png)
host    → ESC ] 7279 ; a=ev:s=post:e=click ; {"area": …, "href": "../about", "url": "https://example.com/about"} ST
```

A link `click` event includes both the raw `href` and the resolved `url`. If
the resolved URL falls under `https://hotty.invalid/` (because no valid
`<base>` was provided), `url` is omitted and only `href` is sent. Your program
decides how to handle the click—for example, navigating internally or opening
the URL via the OS ([Input](https://hotty.neuroplast.io/docs/concepts/input#links)).

- **`target="_blank"` links** are terminal hyperlinks: the host opens `url`
  using the same gesture and rules as an OSC 8 hyperlink. Links without a
  resolved `url` are not treated as hyperlinks.
- **Form `action` attributes** also resolve against the base URL, but have no
  network effect—submitting a form only emits a `submit` event.
