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 anhttp.serve listener, and its certificate files are
still files:
http.serve:
--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
Createtls-server.js:
tls.certandtls.keyaccept a file path or inline PEM text. A string starting with-----BEGINis treated as PEM content; anything else is a path.- Path inputs are permission-checked before reading. Grant
--allow-readfor 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
TypeErrorand the server never starts half-configured — you see the error, never a silent plain-HTTP fallback. A denied path throwsPermissionDeniedinstead.
--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 isff.json, skip the tls object and pass paths at startup:
--certand--keymust be given together. One without the other prints an error and exits.- When both CLI flags and the JS
tlsobject are set, the JS object wins at listener start. - The CLI reads
--cert/--key/FF_CERT/FF_KEYitself, so those paths do not need--allow-read. Atlsobject inside the app is a runtime read and does. - A binary built with
-Dbearssl=falsehas no TLS: starting with cert/key printsbuilt without TLSand exits. - The app’s listener still needs its port scope:
--allow-net=0.0.0.0:<port>afterstart.
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):
wss-client.js:
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 andfetch 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:
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 forlocalhost:
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).cert.pem and key.pem in the current directory. Then:
TLS with PostgreSQL
Fairyfly’s BearSSL engine also secures database connections: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.Clientmatches DNS SANs, not IP SANs. Always dial TLS servers using a DNS hostname (e.g.localhost). - Custom CA bundles loaded via
--caorFF_CA_FILEare retained across client root bundle rescans and safe to configure before or after server initialization. - One listener is TLS-or-plain per
tlsblock. No client-certificate auth. - Cert ≤ 512KB, key ≤ 256KB.
- RSA or EC keys; RSA is the well-trodden path.
- Outbound
fetchstays HTTP/1.1 even against HTTPS hosts. - Server bind is always
0.0.0.0; the net scope must name0.0.0.0:<port>. tls.cert/tls.keypaths need--allow-read; inline PEM and CLI--cert/FF_CERTdo not.
Try it
- Generate a cert with the openssl command above.
- Start
tls-server.jswith the net and read flags, thencurl -k https://localhost:8443/. - Connect
wss-client.jswith--allow-net=localhost:8443 --ca cert.pemand confirm echo works over TLS. - Start a second script with
--ca cert.pemand fetch your own server without-k.
See also
- Permissions — flags, scopes, denial contract
- HTTP Server guide — listener basics
- HTTP/2 guide — ALPN over TLS
- Limitations — every hard cap