diff --git a/changes/547-ui-turnstile.minor.md b/changes/547-ui-turnstile.minor.md new file mode 100644 index 00000000..c2499ae3 --- /dev/null +++ b/changes/547-ui-turnstile.minor.md @@ -0,0 +1 @@ +- **ui:** with `MORCA_UI_TURNSTILE_*` set, a visitor who is not logged in passes Cloudflare Turnstile once a day before a data read; Hetzner deploy tools 0.37.0 set it from `TURNSTILE_SITEKEY` and `secrets/orca/turnstile-secret`. diff --git a/deploy/hetzner/VERSION b/deploy/hetzner/VERSION index 93d4c1ef..0f1a7dfc 100644 --- a/deploy/hetzner/VERSION +++ b/deploy/hetzner/VERSION @@ -1 +1 @@ -0.36.0 +0.37.0 diff --git a/deploy/hetzner/hetzner.env.example b/deploy/hetzner/hetzner.env.example index 79e26960..259c451e 100644 --- a/deploy/hetzner/hetzner.env.example +++ b/deploy/hetzner/hetzner.env.example @@ -120,6 +120,15 @@ UI_URL= #HCLOUD_POOL_TOKEN_FILE= #POOL_FIREWALL= #POOL_SSH_KEYS= +# Cloudflare Turnstile before the UI's data reads (morca ui): a visitor who +# isn't logged in passes it once a day. Its site key (public) and its secret +# key: the key secrets/orca/turnstile-secret in SECRETS_FILE, or else +# TURNSTILE_SECRET_FILE, a file with it (or a .sops one). The hostnames a +# pass may come from, comma separated, are UI_URL's unless set. Off without +# both the site key and the secret. +#TURNSTILE_SITEKEY= +#TURNSTILE_SECRET_FILE= +#TURNSTILE_HOSTNAMES= # Each run gets a cgroup with a memory limit at its grant (docs/hetzner.md, # A cgroup per run). That's the server's setting, not this file's: on by diff --git a/deploy/hetzner/lib.sh b/deploy/hetzner/lib.sh index f4fc29f0..f721b376 100755 --- a/deploy/hetzner/lib.sh +++ b/deploy/hetzner/lib.sh @@ -31,7 +31,7 @@ die() { # The settings that name a file: a relative one is read from the folder of # the settings file that sets it (resolve_paths). PATH_SETTINGS="SECRETS_FILE S3_CREDENTIALS_FILE TUNNEL_TOKEN_FILE GITHUB_TOKEN_FILE -NTFY_TOPIC_FILE NTFY_TOKEN_FILE GHCR_TOKEN_FILE EXTRA_ENV_FILE CONFIG_OVERLAY_FILE SSH_PUBLIC_KEY_FILE SSH_PRIVATE_KEY_FILE" +NTFY_TOPIC_FILE NTFY_TOKEN_FILE GHCR_TOKEN_FILE TURNSTILE_SECRET_FILE EXTRA_ENV_FILE CONFIG_OVERLAY_FILE SSH_PUBLIC_KEY_FILE SSH_PRIVATE_KEY_FILE" # resolve_paths DIR: each PATH_SETTINGS that is set and relative becomes # DIR/. Run after each settings file, so a shared hetzner.env (in a @@ -445,7 +445,7 @@ need_settings() { done [ -z "${GHCR_USER:-}" ] || has_secret ghcr-token \ || die "GHCR_USER needs GHCR_TOKEN_FILE or a ghcr-token key in SECRETS_FILE" - for v in S3_CREDENTIALS_FILE TUNNEL_TOKEN_FILE GITHUB_TOKEN_FILE NTFY_TOPIC_FILE NTFY_TOKEN_FILE GHCR_TOKEN_FILE CONFIG_OVERLAY_FILE; do + for v in S3_CREDENTIALS_FILE TUNNEL_TOKEN_FILE GITHUB_TOKEN_FILE NTFY_TOPIC_FILE NTFY_TOKEN_FILE GHCR_TOKEN_FILE TURNSTILE_SECRET_FILE CONFIG_OVERLAY_FILE; do eval "val=\${$v:-}" if is_sops "$val"; then need_tool sops @@ -485,7 +485,7 @@ need_handover_wait() { # value is the file's content (a string). It may have more secrets/orca/ # keys, the secrets the apps' [apps.] secrets name (#331), sent the same way. One decrypt per run: a Secure # Enclave age key (age-plugin-se) asks for Touch ID once, not per file. -SECRET_NAMES="s3.env tunnel.env secrets/orca/github-token secrets/orca/ntfy-topic secrets/orca/ntfy-token ghcr-token secrets/orca/hcloud-token" +SECRET_NAMES="s3.env tunnel.env secrets/orca/github-token secrets/orca/ntfy-topic secrets/orca/ntfy-token ghcr-token secrets/orca/hcloud-token secrets/orca/turnstile-secret" SECRETS_JSON= SECRETS_KEYS= SECRETS_LOADED=0 @@ -500,6 +500,7 @@ secret_var() { secrets/orca/ntfy-token) echo NTFY_TOKEN_FILE ;; ghcr-token) echo GHCR_TOKEN_FILE ;; secrets/orca/hcloud-token) echo HCLOUD_POOL_TOKEN_FILE ;; + secrets/orca/turnstile-secret) echo TURNSTILE_SECRET_FILE ;; esac } @@ -603,6 +604,20 @@ write_settings() { fi # Where links point, and where pool machines reach the UI. [ -z "${UI_URL:-}" ] || echo "MORCA_UI_URL=$UI_URL" + # Cloudflare Turnstile before the UI's data reads: the site key is + # public, the secret a secret file whose id is here; the hostnames a + # pass may come from are UI_URL's unless TURNSTILE_HOSTNAMES says. + if [ -n "${TURNSTILE_SITEKEY:-}" ] && has_secret secrets/orca/turnstile-secret; then + echo "MORCA_UI_TURNSTILE_SITEKEY=$TURNSTILE_SITEKEY" + echo "MORCA_UI_TURNSTILE_SECRET=orca/turnstile-secret" + ts_hosts=${TURNSTILE_HOSTNAMES:-} + if [ -z "$ts_hosts" ] && [ -n "${UI_URL:-}" ]; then + ts_hosts=${UI_URL#*://} + ts_hosts=${ts_hosts%%/*} + ts_hosts=${ts_hosts%%:*} + fi + [ -z "$ts_hosts" ] || echo "MORCA_UI_TURNSTILE_HOSTNAMES=$ts_hosts" + fi # Worker pools morca grows itself ([pools.], morca #267): the # Hetzner token is a secret file, only its id is here; the machines # get the stack's firewall and SSH key unless POOL_FIREWALL and diff --git a/deploy/hetzner/server/morca-stack b/deploy/hetzner/server/morca-stack index 6834bc54..3e74c699 100755 --- a/deploy/hetzner/server/morca-stack +++ b/deploy/hetzner/server/morca-stack @@ -339,7 +339,7 @@ cmd_configure() { # The files sync may write (in $CONF): settings.env and the secret files # up.sh and deploy.sh send. -SYNC_FILES="settings.env overlay/morca.overlay.toml s3.env tunnel.env ghcr-token secrets/orca/github-token secrets/orca/ntfy-topic secrets/orca/ntfy-token secrets/orca/hcloud-token" +SYNC_FILES="settings.env overlay/morca.overlay.toml s3.env tunnel.env ghcr-token secrets/orca/github-token secrets/orca/ntfy-topic secrets/orca/ntfy-token secrets/orca/hcloud-token secrets/orca/turnstile-secret" # key_changes OLD NEW: "added A, B; removed C; changed D" between two # KEY=VALUE files (the last line of a key counts, as docker's --env-file diff --git a/docs/hetzner.md b/docs/hetzner.md index e658b891..5608ab76 100644 --- a/docs/hetzner.md +++ b/docs/hetzner.md @@ -189,6 +189,7 @@ secrets/orca/github-token: ... # GITHUB_TOKEN_FILE's secrets/orca/ntfy-topic: ... # NTFY_TOPIC_FILE's secrets/orca/ntfy-token: ... # NTFY_TOKEN_FILE's ghcr-token: ... # GHCR_TOKEN_FILE's +secrets/orca/turnstile-secret: ... # TURNSTILE_SECRET_FILE's ``` Write it with `sops edit prod/secrets.sops.yaml`, or, from the repository's diff --git a/docs/ui.md b/docs/ui.md index 0bbe62b7..a8877069 100644 --- a/docs/ui.md +++ b/docs/ui.md @@ -750,6 +750,30 @@ are `/healthz`, the stylesheet, `/robots.txt` and the worker API. `X-Robots-Tag: noindex, nofollow`: run, snapshot and data pages are endless and the data views are costly. +## Turnstile + +Crawlers that come from a new address each time get past the limits per +client. With Cloudflare Turnstile on, a visitor who isn't logged in passes +a check once a day before a data read (a Data tab, `data.csv`, +`data.json`): the page shows the widget, which posts its token to +`POST /turnstile`; the UI asks Cloudflare's siteverify, needs `success`, +the action `data` and one of the hostnames, then sets the `morca_human` +cookie (a day; signed with a key the UI makes at start, so a restart asks +again) and sends the browser back. Without the cookie, `data.csv`, +`data.json` and JSON get a 403 in text. People logged in, API tokens and +the worker API never see it; nothing else is checked. + +| Variable | | +|---|---| +| `MORCA_UI_TURNSTILE_SITEKEY` | the widget's site key (public) | +| `MORCA_UI_TURNSTILE_SECRET` | the id of its secret key, read as the other secrets are | +| `MORCA_UI_TURNSTILE_HOSTNAMES` | the hostnames a pass may come from, comma separated | + +It's off unless the site key and the secret are both set. On Hetzner, +`TURNSTILE_SITEKEY`, `secrets/orca/turnstile-secret` in `SECRETS_FILE` (or +`TURNSTILE_SECRET_FILE`) and `TURNSTILE_HOSTNAMES` (UI_URL's host unless +set) in `hetzner.env` set them. + ## Stopping SIGTERM (what `docker stop` sends) or SIGINT (Ctrl-C) drains `morca ui` diff --git a/pixi.toml b/pixi.toml index 6971769f..0b9bdcc9 100644 --- a/pixi.toml +++ b/pixi.toml @@ -216,6 +216,7 @@ test-weekly = { cmd = "scripts/with-postgres.sh tests/weekly.sh" } # the file and records, config show and export, and the env on each runner. test-toml = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-toml" } test-limit = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-limit" } +test-turnstile = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-turnstile" } test-upgrade = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-upgrade" } test-config = { cmd = "scripts/unit-tests.sh build scheduler-ui && scripts/unit-tests.sh run test-config && scripts/with-postgres.sh tests/config.sh" } # Fills (morca.fill): a pipeline filling its range by itself, within limits. @@ -300,7 +301,7 @@ test-executor-core = { depends-on = ["build-shared", "test-sample", "test-steps" test-executor-workers = { depends-on = ["build-shared", "test-ec2", "test-autoscale", "test-no-workers", "test-pool", "test-hcloud", "test-pool-scale", "test-pool-scale-ec2", "test-pool-image", "test-run-cluster", "test-secrets", "test-placement", "test-grants", "test-pressure", "test-lost-workers"] } test-executor = { depends-on = ["test-executor-core", "test-executor-workers"] } # The scheduler, the UI, and what a registration and its code control: -test-scheduler-ui = { depends-on = ["build-shared", "test-toml", "test-upgrade", "test-config", "test-fill", "test-fill-version", "test-fill-requests", "test-priority", "test-pool-fills", "test-run-locks", "test-metrics", "test-terms", "test-scheduler", "test-ui", "test-fleet", "test-system", "test-actions", "test-calendar", "test-ui-login", "test-ui-drain", "test-remote", "test-worker-api", "test-pause", "test-older-binary", "test-watchdog", "test-limit", "test-notify", "test-versions", "test-manual-run", "test-new-code", "test-defer", "test-dead-runs", "test-adopt", "test-overlap", "test-outdated", "test-concurrency", "test-one-run-per-key", "test-hourly", "test-weekly", "test-sense-errors", "test-stop", "test-binary-timeouts"] } +test-scheduler-ui = { depends-on = ["build-shared", "test-toml", "test-upgrade", "test-config", "test-fill", "test-fill-version", "test-fill-requests", "test-priority", "test-pool-fills", "test-run-locks", "test-metrics", "test-terms", "test-scheduler", "test-ui", "test-fleet", "test-system", "test-actions", "test-calendar", "test-ui-login", "test-ui-drain", "test-remote", "test-worker-api", "test-pause", "test-older-binary", "test-watchdog", "test-limit", "test-turnstile", "test-notify", "test-versions", "test-manual-run", "test-new-code", "test-defer", "test-dead-runs", "test-adopt", "test-overlap", "test-outdated", "test-concurrency", "test-one-run-per-key", "test-hourly", "test-weekly", "test-sense-errors", "test-stop", "test-binary-timeouts"] } test = { depends-on = ["test-executor", "test-scheduler-ui"] } toy = { cmd = "scripts/mojo-build.sh -I src $DEP_INC examples/toy.mojo -o build/toy" } fanout = { cmd = "scripts/mojo-build.sh -I src $DEP_INC examples/fanout.mojo -o build/fanout" } diff --git a/scripts/unit-tests.sh b/scripts/unit-tests.sh index 46d7b273..1c18e924 100755 --- a/scripts/unit-tests.sh +++ b/scripts/unit-tests.sh @@ -86,6 +86,7 @@ scheduler-ui:tests/test_remote.mojo:test-remote scheduler-ui:tests/test_worker.mojo:test-worker-api scheduler-ui:tests/test_watchdog.mojo:test-watchdog scheduler-ui:tests/test_limit.mojo:test-limit +scheduler-ui:tests/test_turnstile.mojo:test-turnstile data-tables:tests/test_tables.mojo:test-tables data-tables:tests/test_lake_batch.mojo:test-lake-batch data-tables:tests/test_replace.mojo:test-replace diff --git a/src/morca/_internal/web/server.mojo b/src/morca/_internal/web/server.mojo index c115f318..8d306f59 100644 --- a/src/morca/_internal/web/server.mojo +++ b/src/morca/_internal/web/server.mojo @@ -41,7 +41,10 @@ A client may send `MORCA_UI_RATE` requests a minute (off unless set; the Hetzner stack sets 120), and the data reads, which take a second or more, run `MORCA_UI_DATA_READS` at once (half the workers), one per client, so the other pages don't wait behind them; people logged in and scripts aren't -limited (`morca._internal.web.limit`). +limited (`morca._internal.web.limit`). With `MORCA_UI_TURNSTILE_SITEKEY` and +`MORCA_UI_TURNSTILE_SECRET` set, a visitor who isn't logged in passes +Cloudflare Turnstile once a day before a data read +(`morca._internal.web.turnstile`, #547). Each request but `/healthz` and the stylesheet is logged as one line: the time (UTC, to the second), method, path, status, milliseconds, the client (`client`), Cloudflare's `CF-Connecting-IP` when there is one, and the User-Agent. @@ -89,6 +92,11 @@ from morca._internal.web.auth import ( ) from morca._internal.web.drain import Drain from morca._internal.web.limit import Gate, Limits +from morca._internal.web.turnstile import ( + VERIFY_PATH, + Turnstile, + challenge_page, +) from morca._internal.web.page import Page, respond from morca._internal.web.router import ( ACTION, @@ -252,12 +260,21 @@ struct Ui(Copyable, Handler): """Set once a drain has started (`morca._internal.web.drain.Drain.draining`).""" var gate: Gate """The limits per client (`morca._internal.web.limit`).""" - - def __init__(out self, url: String, draining: AtomicFlag, gate: Gate): + var turnstile: Turnstile + """The check before a data read (`morca._internal.web.turnstile`).""" + + def __init__( + out self, + url: String, + draining: AtomicFlag, + gate: Gate, + var turnstile: Turnstile, + ): self.url = url self.table = routes() self.draining = draining self.gate = gate + self.turnstile = turnstile^ def serve(self, req: Request) raises -> Response: var path = _path_of(req.url) @@ -282,6 +299,21 @@ struct Ui(Copyable, Handler): return _healthz(self.url, self.draining.is_set()) if post and path.startswith("/api/worker/"): return worker_api.worker_response(self.url, req) + if self.turnstile.on(): + if post and path == VERIFY_PATH: + if not same_origin(req): + return respond(cross_origin(), False) + return self.turnstile.verify(req) + if self.turnstile.needs_page(req, path): + return _with_user( + challenge_page(self.turnstile.sitekey, req.url), + False, + "", + False, + ) + var refused = self.turnstile.check(req, path) + if refused: + return refused.take() var found = find(self.table, req.method, path) var reading = req.method == "GET" or req.method == "HEAD" if not found.route and not (reading or (post and found.miss)): @@ -479,6 +511,7 @@ def serve_ui(url: String, addr: String, worker_addr: String = "") raises: # that's busy with a slow page. _ = setenv("FLARE_REUSEPORT_WORKERS", "0") var limits = Limits(workers) + var turnstile = Turnstile() print( "morca ui on http://", addr, @@ -490,11 +523,14 @@ def serve_ui(url: String, addr: String, worker_addr: String = "") raises: String( ", ", limits.rate, " requests a minute a client" ) if limits.rate else String(), + ", Turnstile before data reads" if turnstile.on() else "", ")", sep="", ) server.serve( - Logged(drain.counting(Ui(url, drain.draining(), limits.gate()))), + Logged( + drain.counting(Ui(url, drain.draining(), limits.gate(), turnstile^)) + ), workers, ) _ = drain^ diff --git a/src/morca/_internal/web/turnstile.mojo b/src/morca/_internal/web/turnstile.mojo new file mode 100644 index 00000000..5efb810f --- /dev/null +++ b/src/morca/_internal/web/turnstile.mojo @@ -0,0 +1,296 @@ +"""Cloudflare Turnstile in front of the data reads (a Data tab, `data.csv`, +`data.json`): a visitor who isn't logged in passes a check once a day +before the UI runs one for them. Crawlers that come from a new address +each time get past the per-client limits (`morca._internal.web.limit`); +they don't get past this. + +| Variable | | +|---|---| +| `MORCA_UI_TURNSTILE_SITEKEY` | the widget's site key (public) | +| `MORCA_UI_TURNSTILE_SECRET` | the id of its secret key (`morca._internal.control.fetch_secret`) | +| `MORCA_UI_TURNSTILE_HOSTNAMES` | the hostnames a pass may come from, comma separated | + +It's off unless the site key and the secret are both set. On, a data read +without a valid `morca_human` cookie is answered 403: a page with the +widget, or for `data.csv`, `data.json` and JSON a line of text. The widget +posts its token to `POST /turnstile`, which asks Cloudflare's siteverify +and needs `success`, the action `data` and one of the hostnames; it then +sets `morca_human` (a day, signed with a key the UI makes when it starts, +so a restart asks again) and sends the browser back. The same requests as +the limits' (`exempt`: sessions, API tokens, the worker API) never see it. +""" + +from std.ffi import external_call +from std.os import getenv + +from flare.crypto.random import random_bytes +from flare.http import Request, Response +from json import loads +from objectstore.crypto import hmac_sha256_hex +from objectstore.http import Header, HttpClient, RetryPolicy + +from morca._internal.control import fetch_secret +from morca._internal.html import Html, escape, query_param, url_encode +from morca._internal.ui.layout import page_html +from morca._internal.web.auth import client, cookie +from morca._internal.web.limit import exempt, is_data_read +from morca._internal.web.page import Page + +comptime HUMAN_COOKIE = "morca_human" +comptime VERIFY_PATH = "/turnstile" +comptime ACTION = "data" +comptime COOKIE_SECONDS = 86400 +comptime MAX_TOKEN = 2048 +comptime SITEVERIFY = ( + "https://challenges.cloudflare.com/turnstile/v0/siteverify" +) +comptime SCRIPT = "https://challenges.cloudflare.com/turnstile/v0/api.js" + + +def _now() -> Int: + return Int(external_call["time", Int64](Int(0))) + + +def sign(key: Span[UInt8, _], expiry: Int) -> String: + """The cookie's value: when it expires (seconds since the epoch) and its + HMAC-SHA256 under `key`.""" + var e = String(expiry) + return String(e, ".", hmac_sha256_hex(key, e)) + + +def _same(a: String, b: String) -> Bool: + """`a == b`, in time that doesn't depend on where they differ.""" + var x = a.as_bytes() + var y = b.as_bytes() + if len(x) != len(y): + return False + var d: UInt8 = 0 + for i in range(len(x)): + d |= x[i] ^ y[i] + return d == 0 + + +def valid(key: Span[UInt8, _], value: String, now: Int) -> Bool: + """Whether `value` is a cookie `sign` made with `key` that hasn't + expired by `now`.""" + var dot = value.find(".") + if dot <= 0: + return False + var expiry: Int + try: + expiry = Int(value[byte=:dot]) + except: + return False + if expiry <= now: + return False + return _same(value, sign(key, expiry)) + + +def safe_next(next: String) -> String: + """Where to send the browser after a pass: `next` if it's a path on + this site (starts with one `/`, no backslash), else `/`.""" + if ( + not next.startswith("/") + or next.startswith("//") + or "\\" in next + or "\n" in next + or "\r" in next + ): + return "/" + return next + + +def _wants_text(req: Request, path: String) -> Bool: + return ( + path.endswith("/data.csv") + or path.endswith("/data.json") + or query_param(req.url, "format") == "json" + or "application/json" in req.headers.get("Accept") + ) + + +def challenge_page(sitekey: String, next: String) -> Page: + """The 403 page with the widget, which posts its token on to `next`.""" + var h = Html() + h.raw( + ( + "

One moment

Check that you're not a crawler to see the" + " data." + ), + " It lasts a day; logging in skips it.

", + ) + h.raw('
') + h.raw('') + h.raw('
') + h.raw("
") + h.raw('') + h.raw( + "", + ) + return Page( + 403, + String('{"error": "check that you are not a crawler first"}'), + page_html("One moment", "", h), + ) + + +def _text(status: Int, reason: String, body: String) -> Response: + var resp = Response( + status=status, reason=reason, body=List[UInt8](body.as_bytes()) + ) + try: + resp.headers.set("Content-Type", "text/plain; charset=utf-8") + resp.headers.set("Cache-Control", "no-store") + except: + pass + return resp^ + + +def _forbidden(why: String) -> Response: + return _text(403, "Forbidden", String(why, "\n")) + + +struct Turnstile(Copyable, Movable): + """The check's settings and the key its cookies are signed with.""" + + var sitekey: String + var secret: String + var hostnames: List[String] + var key: List[UInt8] + var verify_url: String + """Cloudflare's siteverify; `MORCA_UI_TURNSTILE_VERIFY_URL` (tests only) + points it at a stand-in.""" + + def __init__(out self) raises: + """From the environment (the module doc); off if either the site key + or the secret is missing, or the secret can't be read.""" + self.sitekey = getenv("MORCA_UI_TURNSTILE_SITEKEY") + self.secret = String() + var id = getenv("MORCA_UI_TURNSTILE_SECRET") + if self.sitekey and id: + try: + self.secret = fetch_secret(id) + except e: + print("morca ui: Turnstile is off: its secret:", e) + self.hostnames = List[String]() + for h in getenv("MORCA_UI_TURNSTILE_HOSTNAMES").split(","): + var s = String(h.strip()) + if s: + self.hostnames.append(s) + self.key = random_bytes(32) + var url = getenv("MORCA_UI_TURNSTILE_VERIFY_URL") + self.verify_url = url if url else String(SITEVERIFY) + if self.on() and not self.hostnames: + print( + "morca ui: MORCA_UI_TURNSTILE_HOSTNAMES is empty: no check", + " will pass", + sep="", + ) + + def on(self) -> Bool: + return Bool(self.sitekey) and Bool(self.secret) + + def _asked(self, req: Request, path: String) -> Bool: + """Whether `req` is a data read that must pass the check first.""" + if not self.on() or not is_data_read(req.url) or exempt(req, path): + return False + return not valid(self.key, cookie(req, HUMAN_COOKIE), _now()) + + def needs_page(self, req: Request, path: String) -> Bool: + """Whether `req` gets the widget's page (`challenge_page`).""" + return self._asked(req, path) and not _wants_text(req, path) + + def check(self, req: Request, path: String) -> Optional[Response]: + """The 403 in text for a data read of a file or JSON that hasn't + passed the check, or None (`needs_page` covers the pages).""" + if not self._asked(req, path) or not _wants_text(req, path): + return None + return _forbidden( + String( + "check that you're not a crawler first: open the dataset's", + " Data tab in a browser, or log in", + ) + ) + + def _siteverify(self, token: String, ip: String) raises -> Bool: + var form = String( + "secret=", + url_encode(self.secret), + "&response=", + url_encode(token), + "&remoteip=", + url_encode(ip), + ) + var http = HttpClient() + http.follow_redirects = False + http.verify_tls = True + http.timeout_ms = 10_000 + http.retry = RetryPolicy.none() + var h: List[Header] = [ + Header("Content-Type", "application/x-www-form-urlencoded"), + Header("User-Agent", "morca"), + ] + var r = http.request("POST", self.verify_url, h, form.as_bytes()) + if r.status != 200: + print("morca ui: Turnstile siteverify answered", r.status) + return False + var v = loads(String(from_utf8_lossy=Span(r.body))) + if not v.is_object(): + return False + if "success" not in v or not v["success"].bool_value(): + return False + if "action" not in v or not v["action"].is_string(): + return False + if v["action"].string_value() != ACTION: + return False + if "hostname" not in v or not v["hostname"].is_string(): + return False + var host = v["hostname"].string_value() + for allowed in self.hostnames: + if host == allowed: + return True + return False + + def verify(self, req: Request) -> Response: + """`POST /turnstile`: the cookie and a 303 to `next` if Cloudflare + says the token passed, else 403. Fails closed: no answer is no + pass.""" + var form = String("?", String(from_utf8_lossy=Span(req.body))) + var token = query_param(form, "cf-turnstile-response") + var next = safe_next(query_param(form, "next")) + if not self.on(): + return _forbidden("there's no check here") + var tl = token.byte_length() + if tl == 0 or tl > MAX_TOKEN: + return _forbidden( + "the check didn't send a token: go back and try again" + ) + var ok = False + try: + ok = self._siteverify(token, client(req)) + except e: + print("morca ui: Turnstile siteverify:", e) + if not ok: + return _forbidden("the check didn't pass: go back and try again") + var resp = _text(303, "See Other", "") + try: + resp.headers.set("Location", next) + resp.headers.set( + "Set-Cookie", + String( + HUMAN_COOKIE, + "=", + sign(self.key, _now() + COOKIE_SECONDS), + "; Path=/; HttpOnly; SameSite=Lax; Max-Age=", + COOKIE_SECONDS, + "; Secure" if getenv("MORCA_UI_SECURE_COOKIES") + == "1" else "", + ), + ) + except: + return _forbidden( + "the check passed, but its cookie couldn't be set" + ) + return resp^ diff --git a/tests/fake_siteverify.py b/tests/fake_siteverify.py new file mode 100644 index 00000000..8baf8002 --- /dev/null +++ b/tests/fake_siteverify.py @@ -0,0 +1,41 @@ +"""A stand-in for Cloudflare Turnstile's siteverify, for tests/ui_drain.sh. + + python3 tests/fake_siteverify.py + +Every POST's form is appended to as a line of JSON. The token `pass` +passes (action `data`, hostname 127.0.0.1); any other fails. Nothing is +ever sent anywhere else. +""" + +import http.server +import json +import sys +import urllib.parse + +PORT = int(sys.argv[1]) +FILE = sys.argv[2] + + +class Handler(http.server.BaseHTTPRequestHandler): + def do_POST(self): + n = int(self.headers.get("Content-Length", "0")) + form = urllib.parse.parse_qs(self.rfile.read(n).decode("utf-8", "replace")) + form = {k: v[0] for k, v in form.items()} + with open(FILE, "a") as f: + f.write(json.dumps(form) + "\n") + if form.get("response") == "pass": + out = {"success": True, "action": "data", "hostname": "127.0.0.1"} + else: + out = {"success": False, "error-codes": ["invalid-input-response"]} + b = json.dumps(out).encode() + self.send_response(200) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(b))) + self.end_headers() + self.wfile.write(b) + + def log_message(self, *args): + pass + + +http.server.ThreadingHTTPServer(("127.0.0.1", PORT), Handler).serve_forever() diff --git a/tests/hetzner.sh b/tests/hetzner.sh index 170eeef5..249928e1 100755 --- a/tests/hetzner.sh +++ b/tests/hetzner.sh @@ -566,18 +566,20 @@ all_secrets='{"s3.env": "AWS_ACCESS_KEY_ID=x4\nAWS_SECRET_ACCESS_KEY=onefile55\n "secrets/orca/ntfy-token": "tk_ntfy99", "ghcr-token": "ghcr11", "secrets/orca/hcloud-token": "hc_pool12", + "secrets/orca/turnstile-secret": "ts_sec14", "secrets/orca/posthog-key": "phc_app13"}' secrets_file "$all_secrets" sed -i.bak '/^S3_CREDENTIALS_FILE=/d; /^TUNNEL_TOKEN_FILE=/d' "$FAKE/hetzner.env" echo "SECRETS_FILE=$FAKE/secrets.sops.yaml" >> "$FAKE/hetzner.env" +printf 'TURNSTILE_SITEKEY=0xSITE\nTURNSTILE_HOSTNAMES=orca.example\n' >> "$FAKE/hetzner.env" secret_files() { cat "$MORCA_CONF/settings.env" "$MORCA_CONF/s3.env" "$MORCA_CONF/tunnel.env" 2> /dev/null | cksum; } -values='onefile55\|tunnel66\|ghp_gh77\|topic88\|tk_ntfy99\|ghcr11\|hc_pool12\|phc_app13' +values='onefile55\|tunnel66\|ghp_gh77\|topic88\|tk_ntfy99\|ghcr11\|hc_pool12\|ts_sec14\|phc_app13' : > "$FAKE/sops.calls" before=$(secret_files) sh deploy/hetzner/deploy.sh --dry-run eee5555 > "$FAKE/out" 2>&1 || fail "a dry run with SECRETS_FILE" -has "# a dry run doesn't decrypt $FAKE/secrets.sops.yaml: it assumes it has s3.env tunnel.env secrets/orca/github-token secrets/orca/ntfy-topic secrets/orca/ntfy-token ghcr-token secrets/orca/hcloud-token$" +has "# a dry run doesn't decrypt $FAKE/secrets.sops.yaml: it assumes it has s3.env tunnel.env secrets/orca/github-token secrets/orca/ntfy-topic secrets/orca/ntfy-token ghcr-token secrets/orca/hcloud-token secrets/orca/turnstile-secret$" has "^ + sops -d $FAKE/secrets.sops.yaml (once)$" -for n in s3.env tunnel.env secrets/orca/github-token secrets/orca/ntfy-topic secrets/orca/ntfy-token ghcr-token secrets/orca/hcloud-token; do +for n in s3.env tunnel.env secrets/orca/github-token secrets/orca/ntfy-topic secrets/orca/ntfy-token ghcr-token secrets/orca/hcloud-token secrets/orca/turnstile-secret; do has "^ | ssh root@ morca-stack sync $n$" done has "# settings.env from .*MORCA_NOTIFY_SECRET MORCA_NOTIFY_TOKEN_SECRET" @@ -591,6 +593,12 @@ has " morca-stack: s3.env: changed$" has " morca-stack: secrets/orca/ntfy-topic: new$" has " morca-stack: ghcr-token: new$" has " morca-stack: secrets/orca/hcloud-token: new$" +# Turnstile before the UI's data reads: the site key, the secret's id and the +# hostnames in settings.env, the secret a file of its own. +has " morca-stack: secrets/orca/turnstile-secret: new$" +grep -qx "MORCA_UI_TURNSTILE_SITEKEY=0xSITE" "$MORCA_CONF/settings.env" || fail "no MORCA_UI_TURNSTILE_SITEKEY" +grep -qx "MORCA_UI_TURNSTILE_SECRET=orca/turnstile-secret" "$MORCA_CONF/settings.env" || fail "no MORCA_UI_TURNSTILE_SECRET" +grep -qx "MORCA_UI_TURNSTILE_HOSTNAMES=orca.example" "$MORCA_CONF/settings.env" || fail "no MORCA_UI_TURNSTILE_HOSTNAMES" # An app's secret (#331): any secrets/orca/ key, sent the same way. has " morca-stack: secrets/orca/posthog-key: new$" # Worker pools (morca #267): the Hetzner token's id, the stack's firewall and @@ -609,6 +617,7 @@ same secrets/orca/github-token ghp_gh77 same secrets/orca/ntfy-topic topic88 same secrets/orca/ntfy-token tk_ntfy99 same ghcr-token ghcr11 +same secrets/orca/turnstile-secret ts_sec14 same secrets/orca/posthog-key phc_app13 grep -qx 'MORCA_NOTIFY_SECRET=orca/ntfy-topic' "$MORCA_CONF/settings.env" || fail "no MORCA_NOTIFY_SECRET from SECRETS_FILE" grep -qx 'MORCA_NOTIFY_TOKEN_SECRET=orca/ntfy-token' "$MORCA_CONF/settings.env" || fail "no MORCA_NOTIFY_TOKEN_SECRET from SECRETS_FILE" diff --git a/tests/test_turnstile.mojo b/tests/test_turnstile.mojo new file mode 100644 index 00000000..964cda8b --- /dev/null +++ b/tests/test_turnstile.mojo @@ -0,0 +1,51 @@ +"""The Turnstile cookie and where a pass sends the browser +(`morca._internal.web.turnstile`).""" + +from std.testing import TestSuite, assert_equal, assert_false, assert_true + +from morca._internal.web.turnstile import safe_next, sign, valid + + +def _key() -> List[UInt8]: + var k = List[UInt8]() + for i in range(32): + k.append(UInt8(i)) + return k^ + + +def test_good_cookie() raises: + var k = _key() + assert_true(valid(k, sign(k, 2000), 1000)) + + +def test_expired_cookie() raises: + var k = _key() + assert_false(valid(k, sign(k, 1000), 1000)) + assert_false(valid(k, sign(k, 999), 1000)) + + +def test_tampered_cookie() raises: + var k = _key() + var c = sign(k, 2000) + # A later expiry with the old signature. + assert_false(valid(k, String("3000", c[byte=4:]), 1000)) + var other = _key() + other[0] = 99 + assert_false(valid(other, c, 1000)) + assert_false(valid(k, "", 1000)) + assert_false(valid(k, "2000", 1000)) + assert_false(valid(k, "x.y", 1000)) + assert_false(valid(k, String(c, "0"), 1000)) + + +def test_safe_next() raises: + assert_equal(safe_next("/datasets/a?tab=data"), "/datasets/a?tab=data") + assert_equal(safe_next(""), "/") + assert_equal(safe_next("//evil.example/"), "/") + assert_equal(safe_next("https://evil.example/"), "/") + assert_equal(safe_next("/\\evil.example"), "/") + assert_equal(safe_next("/a\r\nSet-Cookie: x"), "/") + + +def main() raises: + TestSuite.discover_tests[__functions_in_module()]().run() diff --git a/tests/ui_drain.sh b/tests/ui_drain.sh index acd9d60b..288705f2 100755 --- a/tests/ui_drain.sh +++ b/tests/ui_drain.sh @@ -12,7 +12,11 @@ # (1, the request cut) within the bound plus half a second; # - on 2 workers (MORCA_UI_WORKERS), a new request is answered while a slow # one waits, both are logged, and SIGTERM still lets the slow one finish; -# - a client over MORCA_UI_RATE gets 429, unless it has a session cookie. +# - a client over MORCA_UI_RATE gets 429, unless it has a session cookie; +# - with Turnstile on (siteverify a stand-in, tests/fake_siteverify.py), a +# data read without the cookie gets the widget's 403 page (data.json a +# 403 in text), a token that fails is 403, one that passes sets +# morca_human and sends the browser back, and the read with it goes on. # # The first three run on one worker: with several, each worker's connections # keep being accepted until the drain stops the loop. @@ -193,4 +197,41 @@ got=$(curl -s -o /dev/null -w '%{http_code}' -H 'Cookie: morca_session=x' "$base grep -Eq '^morca ui: [0-9TZ:-]+ GET /runs 429 ' "$log" || fail "the 429 wasn't logged" kill -TERM "$ui" stopped 30 +# Turnstile (morca.web.turnstile): the secret's id is its value here, and no +# rate (a prefix to a function stays set in some shells). +free_port vport +verified=build/ui-drain-siteverify.jsonl +: > "$verified" +python3 tests/fake_siteverify.py "$vport" "$verified" >> "$log" 2>&1 & +stub=$! +MORCA_UI_RATE=0 MORCA_SECRET_FETCH=echo MORCA_UI_TURNSTILE_SITEKEY=1x00000000000000000000AA \ + MORCA_UI_TURNSTILE_SECRET=sec-ret MORCA_UI_TURNSTILE_HOSTNAMES=127.0.0.1 \ + MORCA_UI_TURNSTILE_VERIFY_URL="http://127.0.0.1:$vport/siteverify" start 1 +grep -q "Turnstile before data reads)" "$log" || { kill "$stub"; fail "no Turnstile in the start message"; } +read_url="/datasets/toy.events?tab=data" +got=$(curl -s -o build/ui-drain-ts.html -w '%{http_code}' "$base$read_url") +[ "$got" = 403 ] || { kill "$stub"; fail "a data read without the cookie got $got"; } +grep -q 'class="cf-turnstile" data-sitekey="1x00000000000000000000AA"' build/ui-drain-ts.html \ + || { kill "$stub"; fail "no widget on the 403 page"; } +got=$(curl -s -o /dev/null -w '%{http_code}' "$base/datasets/toy.events/data.json") +[ "$got" = 403 ] || { kill "$stub"; fail "data.json without the cookie got $got"; } +got=$(curl -s -o /dev/null -w '%{http_code}' "$base/runs") +[ "$got" = 200 ] || { kill "$stub"; fail "a page that isn't a data read got $got"; } +got=$(curl -s -o /dev/null -w '%{http_code}' -H "Origin: $base" --data 'cf-turnstile-response=nope&next=/' "$base/turnstile") +[ "$got" = 403 ] || { kill "$stub"; fail "a token that fails got $got"; } +got=$(curl -s -D build/ui-drain-ts.h -o /dev/null -w '%{http_code}' -H "Origin: $base" \ + --data-urlencode 'cf-turnstile-response=pass' --data-urlencode "next=$read_url" "$base/turnstile") +[ "$got" = 303 ] || { kill "$stub"; fail "a token that passes got $got"; } +grep -qi "^location: /datasets/toy.events?tab=data" build/ui-drain-ts.h || { kill "$stub"; fail "a pass isn't sent back"; } +human=$(sed -n 's/^[Ss]et-[Cc]ookie: \(morca_human=[^;]*\);.*HttpOnly.*/\1/p' build/ui-drain-ts.h) +[ -n "$human" ] || { kill "$stub"; fail "no morca_human cookie"; } +grep -q '"secret": "sec-ret"' "$verified" || { kill "$stub"; fail "siteverify wasn't sent the secret"; } +got=$(curl -s -o /dev/null -w '%{http_code}' -H "Cookie: $human" "$base$read_url") +[ "$got" != 403 ] || { kill "$stub"; fail "a data read with the cookie got 403"; } +got=$(curl -s -o /dev/null -w '%{http_code}' -H "Cookie: morca_human=9999999999.00" "$base$read_url") +[ "$got" = 403 ] || { kill "$stub"; fail "a forged cookie got $got"; } +kill "$stub" 2> /dev/null || true +wait "$stub" 2> /dev/null || true +kill -TERM "$ui" +stopped 30 echo "ui drain: ok"