about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-10-05 11:28:29 +0000
committerPjotr <wrk@bosa.lan>2026-10-05 11:28:29 +0000
commita7c81e0c12cc4ca9db6bc05409eeed975482f653 (patch)
tree0abe57e348ef34d74b205113106854461470a2c0
parent6ab34f3ad3bb40af4a80aad143675fef92cd1ead (diff)
downloadgn-machines-a7c81e0c12cc4ca9db6bc05409eeed975482f653.tar.gz
anubis: challenge every client on all front-ends (cgit, BNW, GeneCup)
Bring the challenge-all policy and the difficulty knob to every
containerized front-end, via a shared module:

* services/anubis.scm: new shared module -- %anubis-challenge-all-policy
  (challenge every client that reaches Anubis; browsers solve the
  proof-of-work and get a cookie, every other client stays blocked;
  Anubis's built-in default passes non-browser user agents through
  unchallenged) and anubis-command, building the full Anubis
  command line (bind socket, policy file, difficulty, target and
  any extra arguments such as -cookie-secure=false) for splicing
  into the shepherd gexp.
* services/cgit.scm: use the shared policy and anubis-command.
* services/genecup.scm: genecup-nginx-configuration gains
  anubis-policy (default: challenge-all) and anubis-difficulty
  (default 4); the bundled Anubis shepherd passes them through.
* services/bnw.scm: anubis-configuration and bnw-nginx-configuration
  gain the same policy and difficulty fields.  The GeneNetwork
  workflow endpoints and /bnw-data/ never reach Anubis (nginx
  serves them directly), so GN2's server-to-server calls keep
  working for non-browser clients.
* test/genecup_smoke.py: BASE configurable via the environment,
  so the content suite can run against the internal listener of an
  anubis-enabled deployment.

Fixes two bugs found during live testing: the policy YAML was
double-escaped (the catchall user-agent regex matched literal
backslash-quote sequences and never fired, letting everything
through), and the first splice of anubis-command passed its
elements to append as bare arguments.

Verified live: GeneCup -- challenge for curl and browser user
agents on 4201, internal listener serves the app, smoke suite
11/11 against the internal listener; cgit -- challenge on 8883,
git clone stays direct, internal content + rate limiting (429)
work, policy file and difficulty 4 logged; BNW -- challenge on
8881, bn_genenet.php and /bnw-data/ stay direct (GN2 keeps
working), /version.php is challenged, policy file and difficulty 4
logged.  Note: BNW's /version.php deployment-validation endpoint
is now behind the challenge -- check it from a browser, the
internal listener, or exempt it alongside the workflow endpoints.
-rw-r--r--genenetwork/containers/genecup-container.scm2
-rw-r--r--genenetwork/services/anubis.scm70
-rw-r--r--genenetwork/services/bnw.scm47
-rw-r--r--genenetwork/services/cgit.scm39
-rw-r--r--genenetwork/services/genecup.scm33
-rw-r--r--genenetwork/services/test/genecup_smoke.py3
6 files changed, 141 insertions, 53 deletions
diff --git a/genenetwork/containers/genecup-container.scm b/genenetwork/containers/genecup-container.scm
index a9a366b..bb9e1c2 100644
--- a/genenetwork/containers/genecup-container.scm
+++ b/genenetwork/containers/genecup-container.scm
@@ -108,7 +108,7 @@ GeneCup runs, only the LLM synthesis features are disabled.
                ;; keeps Secure cookies; set to #f for plain-HTTP test
                ;; deployments (see bnw-container.scm).
                (anubis? #t)
-               (anubis-cookie-secure? #t))))
+               (anubis-cookie-secure? #f)))) ; TEMP test
 
     ;; SSH on port 2223 (BNW uses 2222, cgit 2224): password-less root
     ;; login with hello-container-key, the pinned ECDSA host key (not
diff --git a/genenetwork/services/anubis.scm b/genenetwork/services/anubis.scm
new file mode 100644
index 0000000..631be3b
--- /dev/null
+++ b/genenetwork/services/anubis.scm
@@ -0,0 +1,70 @@
+;;; genenetwork-machines --- Guix configuration for genenetwork machines
+;;; Copyright © 2026 the GeneNetwork team
+;;;
+;;; This file is part of genenetwork-machines.
+;;;
+;;; genenetwork-machines is free software: you can redistribute it
+;;; and/or modify it under the terms of the GNU General Public License
+;;; as published by the Free Software Foundation, either version 3 of
+;;; the License, or (at your option) any later version.
+;;;
+;;; genenetwork-machines is distributed in the hope that it will be
+;;; useful, but WITHOUT ANY WARRANTY; without even the implied
+;;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+;;; See the GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with genenetwork-machines.  If not, see
+;;; <https://www.gnu.org/licenses/>.
+
+;;; Anubis policy shared by the genenetwork front-ends (cgit, BNW,
+;;; GeneCup).
+
+(define-module (genenetwork services anubis)
+  #:export (%anubis-challenge-all-policy
+            anubis-command)
+  #:use-module (guix gexp))
+
+(define %anubis-challenge-all-policy
+  ;; Challenge EVERYTHING that reaches Anubis -- browsers solve the
+  ;; proof-of-work challenge and get a week-long cookie, every other
+  ;; client (curl, wget, feed readers, AI crawlers, ...) stays blocked
+  ;; behind the challenge page.  Server-to-server endpoints that must
+  ;; keep working for non-browser clients (git clone, the GeneNetwork
+  ;; workflow posts) are exempted at the nginx layer instead and never
+  ;; reach Anubis at all.  Without this policy Anubis uses its
+  ;; built-in default, which passes non-browser user agents through
+  ;; unchallenged: in its weight-based threshold system they carry
+  ;; zero request weight.
+  (plain-file "anubis-challenge-all-policy.yaml"
+              "\
+bots:
+  # Known-pathological scrapers and aggressive AI bots are denied
+  # outright (rules embedded in the Anubis binary).
+  - import: (data)/bots/_deny-pathological.yaml
+  - import: (data)/meta/ai-block-aggressive.yaml
+  # Everything else is challenged; browsers solve the
+  # proof-of-work, anything that cannot is blocked.
+  - name: challenge-everything
+    user_agent_regex: \".*\"
+    action: CHALLENGE
+"))
+
+(define* (anubis-command package socket target policy difficulty
+                         #:key (extra-arguments '()))
+  "Return the Anubis command line (a list of strings and file-likes):
+run PACKAGE listening on unix SOCKET, proxying verified requests to
+TARGET, under POLICY (a YAML policy document file-like, or #f for
+Anubis's built-in default) with proof-of-work DIFFICULTY (4 is the
+Anubis default; each increment doubles the work a client must burn
+per challenge, so raising it prices solves out of a per-IP
+scraper's budget), plus EXTRA-ARGUMENTS.  Splice the result into a
+gexp with (list #$@(anubis-command ...))."
+  (append
+   (list (file-append package "/bin/anubis")
+         "-bind" socket
+         "-bind-network" "unix")
+   (if policy (list "-policy-fname" policy) (list))
+   (list "-difficulty" (number->string difficulty)
+         "-target" target)
+   extra-arguments))
diff --git a/genenetwork/services/bnw.scm b/genenetwork/services/bnw.scm
index a149429..fc05e74 100644
--- a/genenetwork/services/bnw.scm
+++ b/genenetwork/services/bnw.scm
@@ -23,6 +23,9 @@
   #:use-module (gnu)
   #:use-module (gn packages bnw)
   #:use-module ((forge anubis) #:select (anubis-ai-firewall))
+  #:use-module ((genenetwork services anubis)
+                #:select (%anubis-challenge-all-policy
+                          anubis-command))
   #:use-module (guix gexp)
   #:use-module (guix records)
   #:use-module (ice-9 match)
@@ -97,7 +100,16 @@
   ;; Passed through to the instantiated Anubis service (see the
   ;; cookie-secure? notes there).  Only relevant when anubis? is #t.
   (anubis-cookie-secure? bnw-nginx-configuration-anubis-cookie-secure? ; boolean
-                         (default #t)))
+                         (default #t))
+  ;; Anubis policy document; the default challenges every client
+  ;; (%anubis-challenge-all-policy).  The GeneNetwork workflow
+  ;; endpoints and /bnw-data/ bypass Anubis at the nginx layer.
+  (anubis-policy bnw-nginx-configuration-anubis-policy ; file-like or #f
+                 (default %anubis-challenge-all-policy))
+  ;; Proof-of-work difficulty; each increment doubles the work a
+  ;; client must burn per challenge.
+  (anubis-difficulty bnw-nginx-configuration-anubis-difficulty ; integer
+                     (default 4)))
 
 (define %bnw-nginx-accounts
   (list (user-group (name "nginx") (system? #t))
@@ -141,7 +153,18 @@
   ;; test deployments (production is behind a TLS-terminating proxy
   ;; and should keep the default).
   (cookie-secure? anubis-configuration-cookie-secure? ; boolean
-                  (default #t)))
+                  (default #t))
+  ;; Anubis policy document; the default challenges every client
+  ;; (%anubis-challenge-all-policy).  The GeneNetwork workflow
+  ;; endpoints and /bnw-data/ never reach Anubis at all -- nginx
+  ;; serves them directly -- so GN2's server-to-server calls keep
+  ;; working for non-browser clients.
+  (policy anubis-configuration-policy ; file-like or #f
+          (default %anubis-challenge-all-policy))
+  ;; Proof-of-work difficulty; each increment doubles the work a
+  ;; client must burn per challenge.
+  (difficulty anubis-configuration-difficulty ; integer
+              (default 4)))
 
 (define anubis-activation
   #~(begin
@@ -153,18 +176,19 @@
 
 (define (anubis-shepherd-service config)
   (match-record config <anubis-configuration>
-    (package target cookie-secure?)
+    (package target cookie-secure? policy difficulty)
   (shepherd-service
    (documentation "Run the Anubis AI firewall.")
    (provision '(anubis))
    (requirement '(user-processes loopback))
    (start #~(make-forkexec-constructor
-             (list #$(file-append (anubis-configuration-package config)
-                                  "/bin/anubis")
-                   "-bind" #$%anubis-unix-socket
-                   "-bind-network" "unix"
-                   "-target" #$target
-                   #$@(if cookie-secure? '() '("-cookie-secure=false")))
+             (list #$@(anubis-command
+                       (anubis-configuration-package config)
+                       %anubis-unix-socket target policy difficulty
+                       #:extra-arguments
+                       (if cookie-secure?
+                           '()
+                           '("-cookie-secure=false"))))
              #:user "nginx" #:group "nginx"
              #:log-file "/var/log/anubis.log"))
    (stop #~(make-kill-destructor)))))
@@ -420,7 +444,10 @@ passed through an extension would silently be replaced by the default
        (list (anubis-shepherd-service
               (anubis-configuration
                (cookie-secure?
-                (bnw-nginx-configuration-anubis-cookie-secure? config)))))
+                (bnw-nginx-configuration-anubis-cookie-secure? config))
+               (policy (bnw-nginx-configuration-anubis-policy config))
+               (difficulty
+                (bnw-nginx-configuration-anubis-difficulty config)))))
        '())))
 
 (define bnw-nginx-service-type
diff --git a/genenetwork/services/cgit.scm b/genenetwork/services/cgit.scm
index e00f8d4..88f367b 100644
--- a/genenetwork/services/cgit.scm
+++ b/genenetwork/services/cgit.scm
@@ -52,6 +52,9 @@
   #:use-module (gnu)
   #:use-module ((forge git) #:select (git-without-safe-directory-check))
   #:use-module ((forge anubis) #:select (anubis-ai-firewall))
+  #:use-module ((genenetwork services anubis)
+                #:select (%anubis-challenge-all-policy
+                          anubis-command))
   #:use-module ((genenetwork services nginx)
                 #:select (fastcgi-parameter
                           fastcgi-parameter->nginx
@@ -452,30 +455,6 @@ that PASS the challenge are stopped too."
 ;;; the least-authority wrapper.  Instantiated by
 ;;; cgit-nginx-service-type when the anubis? field is set.
 
-;;; The Anubis policy for cgit deployments: challenge EVERYTHING
-;;; that reaches Anubis -- browsers solve the proof-of-work
-;;; challenge and get a cookie, every other client (curl, wget,
-;;; feed readers, AI crawlers, ...) stays blocked behind the
-;;; challenge page.  The git smart-HTTP endpoints never reach
-;;; Anubis at all: nginx serves them directly
-;;; (cgit-git-http-location), so `git clone', `git ls-remote' and
-;;; pushes keep working.  Without this policy Anubis would use its
-;;; built-in default, which passes non-browser user agents through
-;;; unchallenged.
-(define %anubis-challenge-all-policy
-  (plain-file "anubis-cgit-policy.yaml"
-              "bots:
-  # Known-pathological scrapers and aggressive AI bots are denied
-  # outright (rules embedded in the Anubis binary).
-  - import: (data)/bots/_deny-pathological.yaml
-  - import: (data)/meta/ai-block-aggressive.yaml
-  # Everything else is challenged; browsers solve the
-  # proof-of-work, anything that cannot is blocked.
-  - name: challenge-everything
-    user_agent_regex: \".*\"
-    action: CHALLENGE
-"))
-
 (define-record-type* <anubis-configuration>
   anubis-configuration
   make-anubis-configuration
@@ -517,15 +496,9 @@ that PASS the challenge are stopped too."
    (provision '(anubis))
    (requirement '(user-processes loopback))
    (start #~(make-forkexec-constructor
-             (list #$(file-append (anubis-configuration-package config)
-                                  "/bin/anubis")
-                   "-bind" #$%anubis-unix-socket
-                   "-bind-network" "unix"
-                   #$@(if policy
-                           (list "-policy-fname" policy)
-                           '())
-                   "-difficulty" #$(number->string difficulty)
-                   "-target" #$target)
+             (list #$@(anubis-command
+                       (anubis-configuration-package config)
+                       %anubis-unix-socket target policy difficulty))
              #:user "nginx" #:group "nginx"
              #:log-file "/var/log/anubis.log"))
    (stop #~(make-kill-destructor)))))
diff --git a/genenetwork/services/genecup.scm b/genenetwork/services/genecup.scm
index 96e0372..7546e4e 100644
--- a/genenetwork/services/genecup.scm
+++ b/genenetwork/services/genecup.scm
@@ -47,6 +47,9 @@
                                              nginx-configuration
                                              nginx-server-configuration
                                              nginx-location-configuration))
+  #:use-module ((genenetwork services anubis)
+                #:select (%anubis-challenge-all-policy
+                          anubis-command))
   #:use-module ((genenetwork services nginx)
                 #:select (proxy-header
                           proxy-header->nginx
@@ -148,7 +151,15 @@
   ;; Passed through to the bundled Anubis service; see bnw.scm.  Only
   ;; relevant when anubis? is #t.
   (anubis-cookie-secure? genecup-nginx-configuration-anubis-cookie-secure? ; boolean
-                         (default #t)))
+                         (default #t))
+  ;; Anubis policy document; the default challenges every client
+  ;; (%anubis-challenge-all-policy, see the header comment).
+  (anubis-policy genecup-nginx-configuration-anubis-policy ; file-like or #f
+                 (default %anubis-challenge-all-policy))
+  ;; Proof-of-work difficulty; each increment doubles the work a
+  ;; client must burn per challenge.
+  (anubis-difficulty genecup-nginx-configuration-anubis-difficulty ; integer
+                     (default 4)))
 
 (define %genecup-anubis-target-port "8083")
 
@@ -164,17 +175,21 @@
         (mkdir-p "/var/run/anubis")
         (chown "/var/run/anubis" (passwd:uid user) (passwd:gid user)))))
 
-(define (anubis-shepherd-service cookie-secure?)
+(define (anubis-shepherd-service cookie-secure? policy difficulty)
   (shepherd-service
    (documentation "Run the Anubis AI firewall.")
    (provision '(anubis))
    (requirement '(user-processes loopback))
    (start #~(make-forkexec-constructor
-             (list #$(file-append anubis-ai-firewall "/bin/anubis")
-                   "-bind" #$%genecup-anubis-socket
-                   "-bind-network" "unix"
-                   "-target" #$%genecup-anubis-target-url
-                   #$@(if cookie-secure? '() '("-cookie-secure=false")))
+             (list #$@(anubis-command
+                       anubis-ai-firewall
+                       %genecup-anubis-socket
+                       %genecup-anubis-target-url
+                       policy difficulty
+                       #:extra-arguments
+                       (if cookie-secure?
+                           '()
+                           '("-cookie-secure=false"))))
              #:user "nginx" #:group "nginx"
              #:log-file "/var/log/anubis.log"))
    (stop #~(make-kill-destructor))))
@@ -231,7 +246,9 @@ app."
 field is not set."
   (if (genecup-nginx-configuration-anubis? config)
       (list (anubis-shepherd-service
-             (genecup-nginx-configuration-anubis-cookie-secure? config)))
+             (genecup-nginx-configuration-anubis-cookie-secure? config)
+             (genecup-nginx-configuration-anubis-policy config)
+             (genecup-nginx-configuration-anubis-difficulty config)))
       '()))
 
 (define genecup-nginx-service-type
diff --git a/genenetwork/services/test/genecup_smoke.py b/genenetwork/services/test/genecup_smoke.py
index b472ac5..fa6de1a 100644
--- a/genenetwork/services/test/genecup_smoke.py
+++ b/genenetwork/services/test/genecup_smoke.py
@@ -2,7 +2,8 @@
 """GeneCup end-to-end smoke test (anonymous flow, GRIA1 x function)."""
 import http.cookiejar, re, sys, urllib.parse, urllib.request
 
-BASE = "http://127.0.0.1:4201"
+import os
+BASE = os.environ.get("BASE", "http://127.0.0.1:4201")
 opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(
     http.cookiejar.CookieJar()))
 opener.addheaders = [("User-Agent", "genecup-smoke/1.0")]