# Headless Chrome

Render PDFs and screenshots, or scrape, with Chrome running in your app's containers

Apps that drive headless Chrome with Grover, ferrum\_pdf, puppeteer-ruby or Ferrum run it inside the app's own containers.

## With the generated Dockerfile

Hatchbox installs Debian's Chromium, the Liberation and Noto Color Emoji fonts, and tini when `Gemfile.lock` has `grover`, `ferrum_pdf` or `puppeteer-ruby`, or has `ferrum` without `cuprite`. With `cuprite`, Ferrum is there for system tests and nothing is installed.

- **Ferrum and puppeteer-ruby** find Chromium on the `PATH`.
- **Grover** runs Puppeteer with Node, so `puppeteer` must be in `package.json`'s `dependencies`. Node and `node_modules` stay in the image, and `PUPPETEER_EXECUTABLE_PATH` points Puppeteer at `/usr/bin/chromium` instead of a Chrome of its own.
- **Updates.** Chromium is installed again on the first deploy of each week, so it gets Debian's security updates. The deploy log shows the generated Dockerfile's `CHROMIUM_WEEK` changing when that happens. An app that isn't deployed keeps the Chromium it has.
- **Ruby 3.1 or newer.** Older Ruby images are on Debian releases whose Chromium no longer gets security updates, so those apps need their own Dockerfile.
- **Fonts.** Text in Chinese, Japanese, Korean or Arabic needs fonts the image doesn't have, and renders blank without them. Load them as web fonts, or commit the Dockerfile and add a package like `fonts-noto-cjk`.

Something else that needs Chrome (Selenium, Puppeteer from a Node script) needs its own Dockerfile. Start from the generated one, under **Dockerfile** in the app's settings, and add the lines below.

## With your own Dockerfile

Install Chromium in the stage that runs the app:

```
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y chromium fonts-liberation fonts-noto-color-emoji tini && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives && \
    echo 'export CHROMIUM_FLAGS="$CHROMIUM_FLAGS --no-sandbox"' > /etc/chromium.d/no-sandbox
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium PUPPETEER_SKIP_DOWNLOAD=true
ENTRYPOINT ["tini", "--"]
```

- **tini.** Chrome leaves processes behind that only the container's first process can clean up, and your app never does: five Grover renders leave 20 of them, and they add up until the container can't start more. tini as the entrypoint cleans them up and passes SIGTERM on to your command. Skip it if your image already has an init of its own.
- **Puppeteer's own Chrome.** Don't rely on the Chrome Puppeteer downloads when `npm install` runs. It lands in the home directory of the user that ran it, `/root/.cache/puppeteer` in a build, which the non-root user the image runs as can't read.
- **Updates.** Docker reuses the layer that installed Chromium until that line changes, so Chromium stays at the version of its first build. **Rebuild and deploy** installs the current one.

## The sandbox

Chrome's sandbox needs user namespaces, which Docker's default seccomp profile refuses, so Chrome in a container stops with "No usable sandbox!" unless it starts with `--no-sandbox`. The `/etc/chromium.d` line adds it whichever gem starts Chrome, and the generated Dockerfile does the same. Setting `CHROMIUM_FLAGS` in the environment does nothing, since Debian's launcher resets it. Each gem also has its own switch: `GROVER_NO_SANDBOX=true`, `FERRUM_CHROME_DOCKERIZE=true` and `PUPPETEER_RUBY_NO_SANDBOX=true`.

Without the sandbox, the container is all that stands between a page Chrome loads and your app's env vars, files and network. Your own templates are fine, remote images and fonts included. See *Pages you didn't write* before loading anyone else's.

## Things to know

- **Memory.** Chrome's memory counts toward the process's memory limit, so a limit sized for Puma alone can get the container killed mid-render. Leave room for Chrome.
- **Grover's display URL.** Grover serves the HTML you give it as `http://example.com` unless you pass `display_url:`. Current Chromium upgrades that to HTTPS and renders the real site instead of your HTML, so pass your app's URL. Grover's middleware does this for you.
- **puppeteer-ruby and SIGTERM.** Launching a browser replaces your process's SIGTERM handler with one that closes the browser, so pass `handle_SIGTERM: false` (and `handle_SIGINT: false`, `handle_SIGHUP: false`) to `Puppeteer.launch` in a web or job process that should shut down on its own terms.
- **Shared memory.** Containers keep Docker's 64 MB `/dev/shm`, which Chrome doesn't need here: Grover, ferrum\_pdf and puppeteer-ruby start it with `--disable-dev-shm-usage`. If another way of starting Chrome crashes large pages with "Target closed" or "Browser is dead", pass it that flag.

## Pages you didn't write

Scraping other sites, or rendering HTML that people outside your business wrote (an invoice template a customer edits), carries two risks:

- **Requests to internal addresses.** A page can load `<img>`, `<iframe>`, CSS or `fetch()` from any address the container can reach: the cloud provider's metadata service (`169.254.169.254`), your database, Redis or other containers on the cluster, and the PDF can show what came back. The sandbox wouldn't stop this either.
- **A Chrome exploit.** Code a crafted page runs in Chrome runs as your app's user, with its env vars, files and network, since there's no sandbox.

Before loading someone else's page:

- **Sanitize HTML you render.** Allow only the tags and attributes your templates need, and drop `script`, `iframe`, `object` and `embed`.
- **Turn off JavaScript** unless the page needs it: Grover's `javascript_enabled: false`, Ferrum's `page.disable_javascript`, puppeteer-ruby's `page.javascript_enabled = false`.
- **Refuse requests to internal addresses** and allow the public ones you need. Check the IP address the host resolves to, not the host name, since a public name can point at `10.0.0.5`. Ferrum intercepts requests with `page.network.intercept` and `page.on(:request)`, calling `request.abort` or `request.continue`, and has `page.network.allowlist=` for a fixed list of hosts. puppeteer-ruby does the same with `page.request_interception = true` and `page.on('request')`. Grover intercepts requests itself to load your HTML and has no way to add a filter, so render untrusted HTML that loads remote resources with Ferrum or puppeteer-ruby.
- **Deploy regularly**, so Chromium stays current.

Hatchbox doesn't block internal addresses for you: apps on AWS get their credentials from the same metadata address through instance roles.