Skip to main content
Fairyfly ships its own TLS stack (BearSSL) — no OpenSSL dependency, no system libraries. The same http.serve code serves plain HTTP and HTTPS. TLS is enabled per server, and WebSocket (wss://) rides on it for free. For HTTP/2 over the same port, see the HTTP/2 guide. TLS here means two things: encrypting your server so browsers see https://, and trusting other servers when you call fetch or new WebSocket against https:// URLs.

Permissions

A TLS server is still an http.serve listener, and its certificate files are still files:
A denied TLS path throws synchronously from http.serve:
The listener check runs first; if both the port and a cert path are denied, the net error wins. Trust and access are separate: --ca / FF_CA_FILE makes a private CA trusted but never grants network access, and --allow-net never makes an untrusted certificate valid. Full model: Permissions.

Enable TLS from JavaScript

Create tls-server.js:
Run it:
What happens:
  • tls.cert and tls.key accept a file path or inline PEM text. A string starting with -----BEGIN is treated as PEM content; anything else is a path.
  • Path inputs are permission-checked before reading. Grant --allow-read for each path (and any symlink target).
  • Cert files are capped at 512KB, key files at 256KB. Larger files fail to load.
  • A bad cert or key throws a TypeError and the server never starts half-configured — you see the error, never a silent plain-HTTP fallback. A denied path throws PermissionDenied instead.
Inline PEM needs no file grant:
Here the reads happen in JS, so --allow-read is still required for the fs.readFile calls — it just is not required again by http.serve.

Enable TLS from the CLI

If your entrypoint is ff.json, skip the tls object and pass paths at startup:
Environment fallback:
Rules:
  • --cert and --key must be given together. One without the other prints an error and exits.
  • When both CLI flags and the JS tls object are set, the JS object wins at listener start.
  • The CLI reads --cert / --key / FF_CERT / FF_KEY itself, so those paths do not need --allow-read. A tls object inside the app is a runtime read and does.
  • A binary built with -Dbearssl=false has no TLS: starting with cert/key prints built without TLS and exits.
  • The app’s listener still needs its port scope: --allow-net=0.0.0.0:<port> after start.

WebSocket over TLS

Nothing extra — same port, wss:// scheme, and the upgrade happens automatically exactly like plain WS (no { status: 101 } branch in the handler):
Create wss-client.js:
The server needs the cert/key read scopes; the client needs the wss host and port plus --ca for the self-signed certificate. Binary frames arrive as Uint8Array; send them with socket.sendBinary.

Trusting your own certificate

Browsers and fetch reject self-signed certs by default. Three cases: 1. Same process serves and fetches. Starting with --cert cert.pem (or FF_CERT) auto-trusts that cert for the process’s own fetch and WebSocket clients — no extra CA flag:
Both scopes are needed: 0.0.0.0:8443 for the listener and localhost:8443 for the outbound fetch. The certificate is trusted automatically, but trust does not replace --allow-net. 2. Separate client script. Pass the cert as a CA:
--ca and FF_CA_FILE are trust inputs, not app reads: they need no --allow-read and no --allow-env. 3. External tools. curl -k https://localhost:8443/ skips verification (-k = local testing only). For Node: NODE_EXTRA_CA_CERTS=cert.pem node client.mjs.

Generate a dev certificate

SubjectAltNames matter — the client verifies the hostname, so mint for localhost:
Note on IP literals: IP-literal TLS hosts (https://127.0.0.1:8443 or wss://127.0.0.1:8443) fail closed: std.crypto.tls.Client verifies DNS SANs, not IP SANs, and disabling host verification would accept any certificate. Always connect to TLS servers using a DNS hostname (e.g. localhost).
This creates cert.pem and key.pem in the current directory. Then:

TLS with PostgreSQL

Fairyfly’s BearSSL engine also secures database connections:
Only sslmode=verify-full and sslmode=disable are supported. Full details: Postgres API.

Limits you will hit

  • Server requires TLS 1.2 minimum (BearSSL profile restricts server versions to TLS 1.2; TLS 1.0 and 1.1 are rejected).
  • IP-literal TLS hosts (https://127.0.0.1:... / wss://127.0.0.1:...) fail closed: std.crypto.tls.Client matches DNS SANs, not IP SANs. Always dial TLS servers using a DNS hostname (e.g. localhost).
  • Custom CA bundles loaded via --ca or FF_CA_FILE are retained across client root bundle rescans and safe to configure before or after server initialization.
  • One listener is TLS-or-plain per tls block. No client-certificate auth.
  • Cert ≤ 512KB, key ≤ 256KB.
  • RSA or EC keys; RSA is the well-trodden path.
  • Outbound fetch stays HTTP/1.1 even against HTTPS hosts.
  • Server bind is always 0.0.0.0; the net scope must name 0.0.0.0:<port>.
  • tls.cert / tls.key paths need --allow-read; inline PEM and CLI --cert / FF_CERT do not.

Try it

  • Generate a cert with the openssl command above.
  • Start tls-server.js with the net and read flags, then curl -k https://localhost:8443/.
  • Connect wss-client.js with --allow-net=localhost:8443 --ca cert.pem and confirm echo works over TLS.
  • Start a second script with --ca cert.pem and fetch your own server without -k.

See also