Files
nixos/modules/www/global.nix
JakeHillion 6f47b32efe
commit-validation / validate-single-commit (pull_request) Successful in 10s
buildbot/nix-eval Build done. (636 warnings)
buildbot/nix-build Build done.
buildbot/nix-effects Build done.
gitea: challenge Lightpanda scraper and route tree-view through Anubis
A distributed scraper identifying as Lightpanda/1.0 was crawling every
commit across all repositories via the tree-view and commit endpoints,
pegging Gitea CPU and timing out real requests. Two gaps let it through:
the Caddy @expensive matcher did not cover tree-view, so those requests
reached Gitea directly, and Anubis was serving the rest because its
baked-in headless-browser rule anchors on "LightPanda" while the bot
sends "Lightpanda/1.0", so the rule never matched.

Add tree-view to the @expensive path regex so it is proxied through
Anubis, and supply a custom Anubis botPolicy that re-imports the upstream
default set (the module does not merge with the baked-in policy) plus a
case-insensitive Lightpanda rule that weighs the user-agent into the
proof-of-work challenge tier. The scraper arrives from thousands of
cookie-less IPs, so a challenge it cannot carry forward keeps it off the
backend without a hard block.

Test plan:
- CI
- Post-deploy: confirm Lightpanda requests receive an Anubis challenge
  and tree-view requests are proxied via port 8923
2026-08-12 20:24:41 +01:00

163 lines
5.7 KiB
Nix

{ pkgs, lib, config, nixpkgs-unstable, ... }:
let
cfg = config.custom.www.global;
locations = config.custom.locations.locations;
in
{
imports = [ "${nixpkgs-unstable}/nixos/modules/services/web-servers/caddy" ];
disabledModules = [ "services/web-servers/caddy/default.nix" ];
options.custom.www.global = {
enable = lib.mkEnableOption "global";
};
config = lib.mkIf cfg.enable {
age.secrets = (
let
mkSecret = domain: {
name = "caddy/${domain}.pem";
value = {
file = ../../secrets/certs/${domain}.pem.age;
owner = config.services.caddy.user;
group = config.services.caddy.group;
};
};
in
builtins.listToAttrs (builtins.map mkSecret [
"hillion.co.uk"
"blog.hillion.co.uk"
"gitea.hillion.co.uk"
"links.hillion.co.uk"
"pastes.hillion.co.uk"
"status.jakehillion.me"
])
) // {
"cloudflare/zone_keys.env" = {
file = ../../secrets/cloudflare/zone_keys.env.age;
owner = config.services.caddy.user;
group = config.services.caddy.group;
};
};
custom.www.www-repo.enable = true;
services.caddy = {
enable = true;
environmentFile = config.age.secrets."cloudflare/zone_keys.env".path;
globalConfig = ''
email acme@hillion.co.uk
'';
virtualHosts = {
## Cloudflare proxied sites
"hillion.co.uk".extraConfig = ''
tls ${./certs/hillion.co.uk.pem} ${config.age.secrets."caddy/hillion.co.uk.pem".path}
handle /.well-known/* {
header /.well-known/matrix/* Content-Type application/json
header /.well-known/matrix/* Access-Control-Allow-Origin *
respond /.well-known/matrix/server "{\"m.server\": \"matrix.hillion.co.uk:443\"}" 200
respond /.well-known/matrix/client `${builtins.toJSON {
"m.homeserver" = { "base_url" = "https://matrix.hillion.co.uk"; };
}}` 200
respond 404
}
handle {
redir https://blog.hillion.co.uk{uri}
}
'';
"blog.hillion.co.uk".extraConfig = ''
tls ${./certs/blog.hillion.co.uk.pem} ${config.age.secrets."caddy/blog.hillion.co.uk.pem".path}
root * /var/www/blog.hillion.co.uk
file_server
'';
"gitea.hillion.co.uk".extraConfig = ''
tls ${./certs/gitea.hillion.co.uk.pem} ${config.age.secrets."caddy/gitea.hillion.co.uk.pem".path}
# Anubis binds its proof-of-work clearance to the client IP (read from
# X-Real-IP). Cloudflare rotates its edge IPs per request, so without
# this every request looks like a new client and gets re-challenged.
# Forward the true visitor IP from Cf-Connecting-Ip on both Anubis hops
# so challenge issuance and the pass-challenge endpoint agree.
@anubis_assets path /.within.website/*
reverse_proxy @anubis_assets http://${locations.services.gitea}:8923 {
header_up X-Real-IP {http.request.header.Cf-Connecting-Ip}
}
@expensive {
not method POST
path_regexp ^/[^/]+/[^/]+/(blame|commits|commit|compare|graph|activity|search|src/commit|raw/commit|tree-view)(/.*)?$
}
reverse_proxy @expensive http://${locations.services.gitea}:8923 {
header_up X-Real-IP {http.request.header.Cf-Connecting-Ip}
}
reverse_proxy http://${locations.services.gitea}:3000
'';
"links.hillion.co.uk".extraConfig = ''
tls ${./certs/links.hillion.co.uk.pem} ${config.age.secrets."caddy/links.hillion.co.uk.pem".path}
redir https://matrix.to/#/@jake:hillion.co.uk
'';
"pastes.hillion.co.uk".extraConfig = ''
tls ${./certs/pastes.hillion.co.uk.pem} ${config.age.secrets."caddy/pastes.hillion.co.uk.pem".path}
reverse_proxy https://privatebin.${config.ogygia.domain} {
header_up Host {http.reverse_proxy.upstream.hostport}
}
'';
"radicale.hillion.co.uk".extraConfig = ''
tls {
dns cloudflare {
zone_token {env.CF_ZONE_TOKEN}
api_token {env.CF_API_TOKEN_HILLION_CO_UK}
}
}
reverse_proxy https://radicale.${config.ogygia.domain} {
header_up Host {http.reverse_proxy.upstream.hostport}
}
'';
"status.jakehillion.me".extraConfig = ''
tls ${./certs/status.jakehillion.me.pem} ${config.age.secrets."caddy/status.jakehillion.me.pem".path}
reverse_proxy https://status.${config.ogygia.domain} {
header_up Host {http.reverse_proxy.upstream.hostport}
}
'';
## ACME sites
"buildbot.hillion.co.uk".extraConfig = ''
tls {
dns cloudflare {
zone_token {env.CF_ZONE_TOKEN}
api_token {env.CF_API_TOKEN_HILLION_CO_UK}
}
}
reverse_proxy http://${locations.services.buildbot-nix-master}:8010
'';
"ntfy.hillion.co.uk".extraConfig = ''
tls {
dns cloudflare {
zone_token {env.CF_ZONE_TOKEN}
api_token {env.CF_API_TOKEN_HILLION_CO_UK}
}
}
reverse_proxy http://localhost:2586
'';
"matrix.hillion.co.uk".extraConfig = ''
tls {
dns cloudflare {
zone_token {env.CF_ZONE_TOKEN}
api_token {env.CF_API_TOKEN_HILLION_CO_UK}
}
}
reverse_proxy /_matrix/* http://${locations.services.matrix}:8008
reverse_proxy /_synapse/client/* http://${locations.services.matrix}:8008
'';
};
};
};
}