# Add-ons

kurama handles add-ons in three layers:

1. **uBlock Origin is bundled.** A signed XPI ships inside the executable and is
   copied into the profile's `extensions/` folder. This always works offline and
   needs no browser driver.
2. **Selected catalog add-ons are installed automatically when possible.**
   kurama downloads each signed XPI from addons.mozilla.org, validates it, and
   asks Firefox through WebDriver to install it.
3. **Anything that fails opens its store page.** The user finishes with one
   click. A failed add-on never fails the configuration run.

## Bundled uBlock Origin

Asset: `internal/addons/data/uBlock0@raymondhill.net.xpi`

The XPI is embedded at build time. `addons.InstallUBlock` copies it to
`<profile>/extensions/uBlock0@raymondhill.net.xpi`. The generated `user.js`
includes:

```
user_pref("extensions.autoDisableScopes", 0);
```

Firefox otherwise disables extensions that were not installed through its own
interaction flow on first start. The bundled XPI is signed by Mozilla, so it
loads as a normal extension.

`kurama doctor` prints the bundled extension's ID, name, and version, which
quickly proves the embedded asset is intact.

## The add-on catalog

The catalog lives in `internal/catalog/addons.go`. Every entry has a stable ID,
display name, description, Mozilla Add-ons URL, category, and optional interest
tags. The URL is the source of truth: the AMO slug for downloading is parsed
from it, so there is no second list to keep in sync.

| ID | Name | Category |
| --- | --- | --- |
| `ublock-origin` | uBlock Origin | privacy (bundled) |
| `clearurls` | ClearURLs | privacy |
| `multi-account-containers` | Firefox Multi-Account Containers | privacy |
| `privacy-badger` | Privacy Badger | privacy |
| `localcdn` | LocalCDN | privacy |
| `canvasblocker` | CanvasBlocker | privacy |
| `snowflake` | Snowflake | privacy |
| `bitwarden` | Bitwarden | security |
| `proton-pass` | Proton Pass | security |
| `keepassxc-browser` | KeePassXC-Browser | security |
| `dark-reader` | Dark Reader | appearance |
| `stylus` | Stylus | appearance |
| `sponsorblock` | SponsorBlock | media |
| `enhancer-for-youtube` | Enhancer for YouTube | media |
| `wayback-machine` | Wayback Machine | research |
| `raindrop` | Raindrop.io | productivity |
| `sidebery` | Sidebery | productivity |
| `tree-style-tab` | Tree Style Tab | productivity |
| `simple-tab-groups` | Simple Tab Groups | productivity |
| `feedbro` | Feedbro | news |
| `violentmonkey` | Violentmonkey | customization |

Some entries carry notes or warnings. Snowflake needs WebRTC; CanvasBlocker and
Ghost Mode should not be combined.

### Adding a catalog entry

Add a `model.Addon` literal with a unique ID, an `https://addons.mozilla.org/...`
URL that contains `/addon/<slug>/`, and interest tags. Add a test case for its
slug in `internal/addons/amo_test.go`. No schema or migration is needed.

## Automatic installation

### Why WebDriver, not clicking the page

Firefox places `addons.mozilla.org` in a **privileged browsing context**.
WebDriver content commands cannot read that page, and the install doorhanger is
browser chrome, not page DOM. Scripted clicks therefore cannot work reliably.
The supported route is WebDriver's extension-install command, which Firefox
executes through its AddonManager.

Selenium is only a client library around geckodriver. kurama therefore skips it
in the product entirely: `internal/webdriver` speaks the W3C WebDriver protocol
to a bundled geckodriver over loopback HTTP. There is no Python, no `pip
install`, and no Selenium Manager download at runtime.

### Bundled geckodriver

Mozilla's `geckodriver` `0.37.1` is embedded in the executable for every
released platform:

| Platform | Archive |
| --- | --- |
| Windows amd64 | `geckodriver-v0.37.1-win64.zip` |
| Windows arm64 | `geckodriver-v0.37.1-win-aarch64.zip` |
| Linux amd64 | `geckodriver-v0.37.1-linux64.tar.gz` |
| Linux arm64 | `geckodriver-v0.37.1-linux-aarch64.tar.gz` |
| macOS amd64/arm64 | `geckodriver-v0.37.1-macos.tar.gz` (universal) |

Build tags embed only the archive for the target platform. On first use, kurama
extracts it to:

```
<cache>/kurama/drivers/geckodriver-v0.37.1/<os>-<arch>/geckodriver[.exe]
```

`<cache>` is the OS user cache directory (`%LOCALAPPDATA%` on Windows,
`~/.cache` on Linux, `~/Library/Caches` on macOS). The extracted file is reused
on later runs. Driver discovery order is:

1. `KURAMA_GECKODRIVER` (explicit file),
2. a `geckodriver[.exe]` next to `kurama`,
3. the bundled archive,
4. `geckodriver` on `PATH`.

The bundled driver comes before `PATH` so a stale system driver cannot break a
working installation. Archive hashes are recorded in
`internal/webdriver/data/checksums.txt`. geckodriver is distributed under the
Mozilla Public License 2.0; see `THIRD_PARTY_NOTICES.md`. Run `kurama doctor`
to see the driver version, source, and extracted path, or the reason it is
unavailable.

### Flow

```
Plan.InstallAddons
  └─ apply.resolveAddons
       ├─ bundled uBlock Origin → copy XPI (no WebDriver)
       └─ other add-ons → addons.InstallSelected
            ├─ probe the bundled driver (`geckodriver --version`)
            ├─ download XPIs in parallel (max 4)
            ├─ validate each manifest (ID, name, version)
            ├─ one WebDriver session
            │    ├─ POST /session
            │    ├─ POST /session/<id>/moz/addon/install (permanent)
            │    └─ DELETE /session/<id>
            └─ per-add-on fallback → store page URL + warning
```

Downloads use `https://addons.mozilla.org/firefox/downloads/latest/<slug>/latest.xpi`.
kurama requires a successful HTTP status, an `application/x-xpinstall` content
type, a size under 64 MiB, and a ZIP containing `manifest.json` with a Firefox
extension ID. The file is written with mode `0600` to a temporary folder that is
removed after the run.

kurama starts geckodriver on a random loopback port, then opens a session with
`-headless -no-remote -profile <target>`. Using `-profile` directly means the
install works even when Firefox has not imported the newly written
`profiles.ini` yet. `-no-remote` prevents a running Firefox from receiving the
request and installing the extension into the wrong profile. First-run prompts
are suppressed with normal Firefox preferences.

For each XPI, kurama sends the file as base64 to
`POST /session/<id>/moz/addon/install` with `temporary: false`. Firefox writes
the add-on into the profile and records it in `extensions.json` with
`active: true`, so the install survives a restart. The session is deleted
afterwards, which quits Firefox, and the process tree is cleaned up if Firefox
or the driver hangs.

### Requirements

- Firefox or a Firefox-based browser installed locally.
- Network access to addons.mozilla.org for non-bundled add-ons.
- No Python, Selenium, Java, Docker, or driver download at runtime.

The first run extracts the driver once. The total install timeout is six
minutes, which covers slow first starts on large profiles.

### Disabling automation

Automatic installation is opt-in-friendly but never forced:

| Control | Effect |
| --- | --- |
| GUI toggle on the Add-ons step | Off means every selected add-on opens its store page. |
| `kurama apply --no-addon-install` | Same behaviour from the CLI. |
| `KURAMA_NO_WEBDRIVER=1` | Disables automation for this process. |
| `KURAMA_NO_SELENIUM=1` | Legacy alias, still accepted. |
| `InstallAddons: false` in a plan | Same behaviour through the API. |

When disabled or unavailable, `AddonPages` is populated and the browser opens
those pages on launch. The review screen and result screen explain the
difference, so a user is never left wondering why a tab opened.

### Environment variables for diagnosis

| Variable | Purpose |
| --- | --- |
| `KURAMA_GECKODRIVER` | Use this geckodriver binary instead of the bundled copy. |
| `KURAMA_DRIVER_CACHE` | Replace the driver cache root (tests use this). |
| `KURAMA_WEBDRIVER_HEADLESS=0` | Show the Firefox window used for installation. |
| `KURAMA_WEBDRIVER_VERBOSE=1` | Include geckodriver and Firefox diagnostics in the log. |
| `KURAMA_NO_WEBDRIVER=1` | Force the store-page fallback. |
| `KURAMA_SELENIUM_HEADLESS`, `KURAMA_SELENIUM_VERBOSE`, `KURAMA_NO_SELENIUM` | Legacy aliases kept for existing scripts. |

Example:

```bash
KURAMA_WEBDRIVER_VERBOSE=1 KURAMA_WEBDRIVER_HEADLESS=0 \
  kurama apply --profile test --addons clearurls --yes
```

On Windows PowerShell:

```powershell
$env:KURAMA_WEBDRIVER_VERBOSE = "1"
$env:KURAMA_WEBDRIVER_HEADLESS = "0"
kurama apply --profile test --addons clearurls --yes
```

## Selenium Grid

Grid belongs to the **test harness**, not the product. `tests/selenium` can
point at a Selenium Grid with `SELENIUM_REMOTE_URL` for GUI tests on another
machine. Grid cannot reach the test machine's profile folders, so profile tests
skip there. Add-on installation is covered by the Go integration suite, which
always uses the local bundled driver.

See [tests/selenium/README.md](../tests/selenium/README.md) and
`tests/selenium/docker-compose.yml`.

## Failure modes and their fallback

| Failure | User-visible result |
| --- | --- |
| No bundled driver for this platform | Probe error; every selected add-on's store page opens. |
| Driver archive cannot be extracted | Warning; store pages open. |
| geckodriver cannot start or crashes | Session error; store pages open. |
| AMO download fails or returns HTML | That add-on's store page opens; others continue. |
| XPI manifest invalid | That add-on is skipped with a warning. |
| Firefox rejects one XPI | That add-on's page opens; the rest of the batch stays installed. |
| User cancels the job | The pipeline stops at the next step; installed add-ons remain installed. |

uBlock Origin is handled before WebDriver, so it is present even in every
fallback path.

## Permissions and trust

kurama installs exactly the add-ons the user selects from its catalog. It does
not install arbitrary URLs, does not sideload unsigned extensions, and does not
change Mozilla's signature requirements. Add-ons are downloaded over HTTPS from
Mozilla Add-ons, and their manifest is checked before Firefox sees them. Firefox
still performs its own signature and permission validation; a rejected add-on is
reported, not bypassed.
