Update to Anubis v1.28.2 (#1)

This upgrades the Masyunis fork from Anubis v1.25.0 to the latest stable release, **v1.28.2** (“Wuk Lamat Echo 2”), while keeping the fork’s custom styling and removals.

## Approach

The Gitea repo is a tree snapshot of v1.25.0, not a git-level fork of [TecharoHQ/anubis](https://github.com/TecharoHQ/anubis). The histories share no commits, so a merge or rebase onto the upstream tag is not usable.

The first commit on this branch replaces the tree with a snapshot of the **v1.28.2** tag (`git rm -r` plus `git checkout v1.28.2 -- .`; the import tree is identical to the tag). A short patch series on top restores the Masyunis customizations.

## Customizations kept

- Masyunya mascots (`web/static/img/{happy,pensive,reject}.webp`), hashes match current `master`.
- `web/static/img/ATTRIBUTIONS.txt` stays deleted.
- Challenge footer: drop the `made_with` (Canada / maple-leaf) line and the CELPHASE mascot credit; keep “Protected by Anubis from Techaro”. The generated `web/index_templ.go` matches `go generate ./web`.
- Short README (the old `anubisc` typo is fixed).
- Version stamped `v1.28.2/masyunis` via Makefile ldflags. `anubis.go` `init()` does not overwrite a link-time version; `go run` / `go test` still report `devel` on purpose.
- Upstream `.github`, `.devcontainer`, `.husky`, and `.vscode` stay deleted.
- `"uk"` is removed from `lib/localization/locales/manifest.json` **on purpose**, because of Alpine locale problems. `uk.json` and its test entry stay; the served manifest lists 27 languages.
- `package.json` `prepare` no longer runs `husky` (the hooks directory is gone). The rest of the script is unchanged. `husky` remains in devDependencies, as upstream left it.

## Deploy notes

- **Cookie rename (v1.27):** with the default `DYNAMIC_COOKIE_SUFFIX=true`, cookie names become e.g. `techaro.lol-anubis-auth-<hash>`. Existing clients get a **one-time re-challenge**. That is expected, not a loop.
- **Docker / ko / yeet builds** skip the Makefile ldflags, so the displayed version is `<git-version>/masyunis`. It is `v1.28.2/masyunis` only when built from a `v1.28.2` tag (or via `make build` / `make prebaked-build`).
- Go 1.27.3 is required (upstream bump in v1.28.2). Thoth (`THOTH_URL` / `THOTH_TOKEN`) was removed upstream; those settings now warn and do nothing. New WASM challenge methods stay disabled unless opted in.

## Verification

- `make build` succeeds. `./var/anubis --version` prints `Anubis v1.28.2/masyunis`.
- `SKIP_INTEGRATION=1 go test ./...` passes (unit tests; integration package is skipped).
- Local challenge/reject pages (en and ru) show Masyunya, version `v1.28.2/masyunis`, and no Canada / CELPHASE / mascot-credit text. Screenshots:

![Challenge page (en)](https://git.unvr.site/attachments/36112ff0-5155-495c-8bd2-59606ee3bd64)
![Reject page (en)](https://git.unvr.site/attachments/2cad0256-1b0e-4f71-8d06-d1a447fd5263)

- Upstream Playwright tests (`make test` without `SKIP_INTEGRATION`) fail on this box because the browser download fails. A clean v1.28.2 tree fails the same three Playwright tests here, so this is not a fork regression.
- `npm audit` reports **4** findings in upstream’s lockfile (2 moderate, 2 high: `brace-expansion`, `fast-uri`, `lodash.trim`, `source-map-js`). Left alone.

Please review and merge when ready. I will not merge this myself.

Reviewed-on: #1
Co-authored-by: Lead <3+lead_bot@noreply.git.unvr.site>
This commit was merged in pull request #1.
This commit is contained in:
sol_bot Bot authored and matiashik committed 2026-11-28 12:10:27 +00:00
1 parent eb3e46d8d0
commit caf4d72880
529 files changed
+33015 -6659

No files matched your search

+412 -120
View File
@@ -4,18 +4,24 @@ import (
"context"
"crypto/ed25519"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"net/netip"
"net/url"
"slices"
"strconv"
"strings"
"sync"
"time"
"cel.dev/cel-go/common/types"
"github.com/golang-jwt/jwt/v5"
"github.com/google/cel-go/common/types"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
@@ -27,6 +33,7 @@ import (
"github.com/TecharoHQ/anubis/internal/dnsbl"
"github.com/TecharoHQ/anubis/internal/ogtags"
"github.com/TecharoHQ/anubis/lib/challenge"
"github.com/TecharoHQ/anubis/lib/challenge/extension"
"github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/localization"
"github.com/TecharoHQ/anubis/lib/policy"
@@ -37,55 +44,101 @@ import (
_ "github.com/TecharoHQ/anubis/lib/challenge/metarefresh"
_ "github.com/TecharoHQ/anubis/lib/challenge/preact"
_ "github.com/TecharoHQ/anubis/lib/challenge/proofofwork"
_ "github.com/TecharoHQ/anubis/lib/challenge/wasm"
// extension implementations
_ "github.com/TecharoHQ/anubis/lib/challenge/extension/css-load"
)
type contextKey int
const asnContextKey contextKey = iota
type asnInfo struct {
ASN string
Description string
}
func asnFromContext(ctx context.Context) (string, string) {
if v, ok := ctx.Value(asnContextKey).(asnInfo); ok {
return v.ASN, v.Description
}
return "", ""
}
var (
challengesIssued = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "anubis_challenges_issued",
Help: "The total number of challenges issued",
}, []string{"method"})
}, []string{"method", "asn", "asn_description"})
challengesValidated = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "anubis_challenges_validated",
Help: "The total number of challenges validated",
}, []string{"method"})
}, []string{"method", "asn", "asn_description"})
droneBLHits = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "anubis_dronebl_hits",
Help: "The total number of hits from DroneBL",
}, []string{"status"})
}, []string{"status", "asn", "asn_description"})
failedValidations = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "anubis_failed_validations",
Help: "The total number of failed validations",
}, []string{"method"})
}, []string{"method", "asn", "asn_description"})
requestsProxied = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "anubis_proxied_requests_total",
Help: "Number of requests proxied through Anubis to upstream targets",
}, []string{"host"})
}, []string{"host", "asn", "asn_description"})
requestsByASN = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "anubis_requests_by_asn_total",
Help: "Number of requests by ASN",
}, []string{"asn", "asn_description"})
)
type Server struct {
next http.Handler
store store.Interface
mux *http.ServeMux
policy *policy.ParsedConfig
OGTags *ogtags.OGTagCache
logger *slog.Logger
opts Options
ed25519Priv ed25519.PrivateKey
hs512Secret []byte
challengeLocks [256]sync.Mutex
next http.Handler
store store.Interface
mux *http.ServeMux
policy *policy.ParsedConfig
OGTags *ogtags.OGTagCache
logger *slog.Logger
opts Options
ed25519Priv ed25519.PrivateKey
hs512Secret []byte
}
func (s *Server) getRequestLogger(r *http.Request) (*slog.Logger, *http.Request) {
lg := internal.GetRequestLogger(s.logger, r)
if s.policy.LogASN && s.policy.GeoIP.HasASN() {
if addr, err := netip.ParseAddr(r.Header.Get("X-Real-IP")); err == nil {
if asNumber, description, ok := s.policy.GeoIP.LookupASN(addr); ok {
asn := strconv.FormatUint(uint64(asNumber), 10)
lg = lg.With("asn", asNumber, "asn_description", description)
requestsByASN.WithLabelValues(asn, description).Inc()
r = r.WithContext(context.WithValue(r.Context(), asnContextKey, asnInfo{
ASN: asn,
Description: description,
}))
}
}
}
return lg, r
}
func (s *Server) getTokenKeyfunc() jwt.Keyfunc {
// return ED25519 key if HS512 is not set
if len(s.hs512Secret) == 0 {
return func(token *jwt.Token) (interface{}, error) {
return func(token *jwt.Token) (any, error) {
return s.ed25519Priv.Public().(ed25519.PublicKey), nil
}
} else {
return func(token *jwt.Token) (interface{}, error) {
return func(token *jwt.Token) (any, error) {
return s.hs512Secret, nil
}
}
@@ -100,16 +153,50 @@ func (s *Server) getChallenge(r *http.Request) (*challenge.Challenge, error) {
return &chall, err
}
// shouldRetryMissingTestCookie reports whether a client that reached
// PassChallenge without the verification cookie should be sent through a fresh
// challenge instead of being shown the cookies disabled error. Some browsers
// drop the verification cookie in edge cases such as an HTTP to HTTPS upgrade
// on the first navigation, and a fresh challenge then succeeds. See
// https://github.com/TecharoHQ/anubis/issues/1916
//
// A retry is only granted for a live, unspent challenge and at most once per
// client (IP address and User-Agent) per challenge lifetime, so clients that
// never send cookies get the error after one extra challenge instead of
// looping.
func (s *Server) shouldRetryMissingTestCookie(r *http.Request) bool {
chall, err := s.getChallenge(r)
if err != nil || chall.Spent {
return false
}
key := "cookie-retry:" + internal.SHA256sum(r.Header.Get("X-Real-IP")+":"+r.Header.Get("User-Agent"))
if _, err := s.store.Get(r.Context(), key); err == nil {
return false
}
// Same lifetime as an issued challenge.
return s.store.Set(r.Context(), key, []byte("1"), 30*time.Minute) == nil
}
func (s *Server) issueChallenge(ctx context.Context, r *http.Request, lg *slog.Logger, cr policy.CheckResult, rule *policy.Bot) (*challenge.Challenge, error) {
if cr.Rule != config.RuleChallenge {
slog.Error("this should be impossible, asked to issue a challenge but the rule is not a challenge rule", "cr", cr, "rule", rule)
slog.ErrorContext(ctx, "this should be impossible, asked to issue a challenge but the rule is not a challenge rule", "cr", cr, "rule", rule)
//return nil, errors.New("[unexpected] this codepath should be impossible, asked to issue a challenge for a non-challenge rule")
}
if rule.Challenge == nil {
rule.Challenge = &config.ChallengeRules{
Difficulty: s.policy.DefaultDifficulty,
Algorithm: config.DefaultAlgorithm,
}
}
id, err := uuid.NewV7()
if err != nil {
return nil, err
}
idStr := id.String()
var randomData = make([]byte, 64)
if _, err := rand.Read(randomData); err != nil {
@@ -117,24 +204,26 @@ func (s *Server) issueChallenge(ctx context.Context, r *http.Request, lg *slog.L
}
chall := challenge.Challenge{
ID: id.String(),
ID: idStr,
Method: rule.Challenge.Algorithm,
RandomData: fmt.Sprintf("%x", randomData),
RandomData: hex.EncodeToString(randomData),
IssuedAt: time.Now(),
Difficulty: rule.Challenge.Difficulty,
PolicyRuleHash: rule.Hash(),
Extensions: slices.Clone(rule.Challenge.Extensions),
Metadata: map[string]string{
"User-Agent": r.Header.Get("User-Agent"),
"X-Real-Ip": r.Header.Get("X-Real-Ip"),
"X-Real-IP": r.Header.Get("X-Real-IP"),
"Referer": r.Header.Get("Referer"),
},
}
j := store.JSON[challenge.Challenge]{Underlying: s.store}
if err := j.Set(ctx, "challenge:"+id.String(), chall, 30*time.Minute); err != nil {
if err := j.Set(ctx, "challenge:"+idStr, chall, 30*time.Minute); err != nil {
return nil, err
}
lg.Info("new challenge issued", "challenge", id.String())
lg.InfoContext(ctx, "new challenge issued", "challenge", idStr, "weight", cr.Weight)
return &chall, err
}
@@ -148,6 +237,14 @@ func (s *Server) hydrateChallengeRule(rule *policy.Bot, chall *challenge.Challen
rule = &policy.Bot{
Rules: &checker.List{},
}
} else {
copiedRule := *rule
if rule.Challenge != nil {
copiedChallenge := *rule.Challenge
copiedChallenge.Extensions = slices.Clone(rule.Challenge.Extensions)
copiedRule.Challenge = &copiedChallenge
}
rule = &copiedRule
}
if chall.Difficulty == 0 {
@@ -185,14 +282,51 @@ func (s *Server) maybeReverseProxyOrPage(w http.ResponseWriter, r *http.Request)
s.maybeReverseProxy(w, r, false)
}
func (s *Server) maybeReverseProxy(w http.ResponseWriter, r *http.Request, httpStatusOnly bool) {
lg := internal.GetRequestLogger(s.logger, r)
const (
downstreamRiskRuleHeader = "X-Anubis-Rule"
downstreamRiskActionHeader = "X-Anubis-Action"
downstreamRiskStatusHeader = "X-Anubis-Status"
)
if val, _ := s.store.Get(r.Context(), fmt.Sprintf("ogtags:allow:%s%s", r.Host, r.URL.String())); val != nil {
lg.Debug("serving opengraph tag asset")
s.ServeHTTPNext(w, r)
func isDownstreamRiskHeader(name string) bool {
switch http.CanonicalHeaderKey(name) {
case downstreamRiskRuleHeader, downstreamRiskActionHeader, downstreamRiskStatusHeader:
return true
}
return false
}
func removeDownstreamRiskConnectionTokens(header http.Header) {
connectionValues := header.Values("Connection")
header.Del("Connection")
for _, value := range connectionValues {
var remaining []string
for token := range strings.SplitSeq(value, ",") {
token = strings.TrimSpace(token)
if token == "" || isDownstreamRiskHeader(token) {
continue
}
remaining = append(remaining, token)
}
if len(remaining) != 0 {
header.Add("Connection", strings.Join(remaining, ", "))
}
}
}
func setDownstreamRiskHeaders(header http.Header, cr policy.CheckResult, status string) {
removeDownstreamRiskConnectionTokens(header)
header.Set(downstreamRiskRuleHeader, cr.Name)
header.Set(downstreamRiskActionHeader, string(cr.Rule))
if status == "" {
header.Del(downstreamRiskStatusHeader)
return
}
header.Set(downstreamRiskStatusHeader, status)
}
func (s *Server) maybeReverseProxy(w http.ResponseWriter, r *http.Request, httpStatusOnly bool) {
lg, r := s.getRequestLogger(r)
// Adjust cookie path if base prefix is not empty
cookiePath := "/"
@@ -202,18 +336,19 @@ func (s *Server) maybeReverseProxy(w http.ResponseWriter, r *http.Request, httpS
cr, rule, err := s.check(r, lg)
if err != nil {
lg.Error("check failed", "err", err)
lg.ErrorContext(r.Context(), "check failed", "err", err)
localizer := localization.GetLocalizer(r)
s.respondWithError(w, r, fmt.Sprintf("%s \"maybeReverseProxy\"", localizer.T("internal_server_error")), makeCode(err))
return
}
r.Header.Add("X-Anubis-Rule", cr.Name)
r.Header.Add("X-Anubis-Action", string(cr.Rule))
lg = lg.With("check_result", cr)
policy.Applications.WithLabelValues(cr.Name, string(cr.Rule)).Add(1)
{
asn, asnDesc := asnFromContext(r.Context())
policy.Applications.WithLabelValues(cr.Name, string(cr.Rule), asn, asnDesc).Add(1)
}
ip := r.Header.Get("X-Real-Ip")
ip := r.Header.Get("X-Real-IP")
if s.handleDNSBL(w, r, ip, lg) {
return
@@ -223,23 +358,25 @@ func (s *Server) maybeReverseProxy(w http.ResponseWriter, r *http.Request, httpS
return
}
ckie, err := r.Cookie(anubis.CookieName)
ckie, err := s.getCookie(r, anubis.CookieName)
if err != nil {
lg.Debug("cookie not found", "path", r.URL.Path)
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
// Don't clear the cookie, there is none to clear. This response can land
// after a concurrent pass-challenge issued a valid one and would delete
// it. See https://github.com/TecharoHQ/anubis/issues/1314
lg.DebugContext(r.Context(), "cookie not found", "path", r.URL.Path)
s.RenderIndex(w, r, cr, rule, httpStatusOnly)
return
}
if err := ckie.Valid(); err != nil {
lg.Debug("cookie is invalid", "err", err)
lg.DebugContext(r.Context(), "cookie is invalid", "err", err)
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
s.RenderIndex(w, r, cr, rule, httpStatusOnly)
return
}
if time.Now().After(ckie.Expires) && !ckie.Expires.IsZero() {
lg.Debug("cookie expired", "path", r.URL.Path)
lg.DebugContext(r.Context(), "cookie expired", "path", r.URL.Path)
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
s.RenderIndex(w, r, cr, rule, httpStatusOnly)
return
@@ -248,7 +385,7 @@ func (s *Server) maybeReverseProxy(w http.ResponseWriter, r *http.Request, httpS
token, err := jwt.ParseWithClaims(ckie.Value, jwt.MapClaims{}, s.getTokenKeyfunc(), jwt.WithExpirationRequired(), jwt.WithStrictDecoding())
if err != nil || !token.Valid {
lg.Debug("invalid token", "path", r.URL.Path, "err", err)
lg.DebugContext(r.Context(), "invalid token", "path", r.URL.Path, "err", err)
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
s.RenderIndex(w, r, cr, rule, httpStatusOnly)
return
@@ -256,7 +393,7 @@ func (s *Server) maybeReverseProxy(w http.ResponseWriter, r *http.Request, httpS
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
lg.Debug("invalid token claims type", "path", r.URL.Path)
lg.DebugContext(r.Context(), "invalid token claims type", "path", r.URL.Path)
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
s.RenderIndex(w, r, cr, rule, httpStatusOnly)
return
@@ -264,30 +401,49 @@ func (s *Server) maybeReverseProxy(w http.ResponseWriter, r *http.Request, httpS
policyRule, ok := claims["policyRule"].(string)
if !ok {
lg.Debug("policyRule claim is not a string")
lg.DebugContext(r.Context(), "policyRule claim is not a string")
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
s.RenderIndex(w, r, cr, rule, httpStatusOnly)
return
}
if policyRule != rule.Hash() {
lg.Debug("user originally passed with a different rule, issuing new challenge", "old", policyRule, "new", rule.Name)
lg.DebugContext(r.Context(), "user originally passed with a different rule, issuing new challenge", "old", policyRule, "new", rule.Name)
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
s.RenderIndex(w, r, cr, rule, httpStatusOnly)
return
}
if s.opts.JWTRestrictionHeader != "" && claims["restriction"] != internal.SHA256sum(r.Header.Get(s.opts.JWTRestrictionHeader)) {
lg.Debug("JWT restriction header is invalid")
lg.DebugContext(r.Context(), "JWT restriction header is invalid")
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
s.RenderIndex(w, r, cr, rule, httpStatusOnly)
return
}
r.Header.Add("X-Anubis-Status", "PASS")
s.restoreOriginalReferer(w, r, cookiePath)
setDownstreamRiskHeaders(r.Header, cr, "PASS")
s.ServeHTTPNext(w, r)
}
// restoreOriginalReferer looks for the one-shot cookie set by PassChallenge and, if
// present, overwrites the request's Referer header with the value it carries before
// the request is proxied upstream. The cookie only survives the single redirect from
// PassChallenge back to the original URL, so this only ever fires for that one request.
func (s *Server) restoreOriginalReferer(w http.ResponseWriter, r *http.Request, cookiePath string) {
ckie, err := s.getCookie(r, anubis.OriginalRefererCookieName)
if err != nil || ckie.Value == "" {
return
}
s.ClearCookie(w, CookieOpts{Name: anubis.OriginalRefererCookieName, Path: cookiePath, Host: r.Host})
if referer, err := url.QueryUnescape(ckie.Value); err == nil {
r.Header.Set("Referer", referer)
}
}
func (s *Server) checkRules(w http.ResponseWriter, r *http.Request, cr policy.CheckResult, lg *slog.Logger, rule *policy.Bot) bool {
// Adjust cookie path if base prefix is not empty
cookiePath := "/"
@@ -299,31 +455,32 @@ func (s *Server) checkRules(w http.ResponseWriter, r *http.Request, cr policy.Ch
switch cr.Rule {
case config.RuleAllow:
lg.Debug("allowing traffic to origin (explicit)")
lg.DebugContext(r.Context(), "allowing traffic to origin (explicit)")
setDownstreamRiskHeaders(r.Header, cr, "")
s.ServeHTTPNext(w, r)
return true
case config.RuleDeny:
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
lg.Info("explicit deny")
lg.InfoContext(r.Context(), "explicit deny")
if rule == nil {
lg.Error("rule is nil, cannot calculate checksum")
lg.ErrorContext(r.Context(), "rule is nil, cannot calculate checksum")
s.respondWithError(w, r, fmt.Sprintf("%s \"maybeReverseProxy.RuleDeny\"", localizer.T("internal_server_error")), makeCode(ErrActualAnubisBug))
return true
}
hash := rule.Hash()
lg.Debug("rule hash", "hash", hash)
lg.DebugContext(r.Context(), "rule hash", "hash", hash)
s.respondWithStatus(w, r, fmt.Sprintf("%s %s", localizer.T("access_denied"), hash), "", s.policy.StatusCodes.Deny)
return true
case config.RuleChallenge:
lg.Debug("challenge requested")
lg.DebugContext(r.Context(), "challenge requested")
case config.RuleBenchmark:
lg.Debug("serving benchmark page")
lg.DebugContext(r.Context(), "serving benchmark page")
s.RenderBench(w, r)
return true
default:
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
lg.Error("CONFIG ERROR: unknown rule", "rule", cr.Rule)
lg.ErrorContext(r.Context(), "CONFIG ERROR: unknown rule", "rule", cr.Rule)
s.respondWithError(w, r, fmt.Sprintf("%s \"maybeReverseProxy.Rules\"", localizer.T("internal_server_error")), makeCode(ErrActualAnubisBug))
return true
}
@@ -331,21 +488,28 @@ func (s *Server) checkRules(w http.ResponseWriter, r *http.Request, cr policy.Ch
}
func (s *Server) handleDNSBL(w http.ResponseWriter, r *http.Request, ip string, lg *slog.Logger) bool {
return s.handleDNSBLWithLookup(w, r, ip, lg, dnsbl.Lookup)
}
func (s *Server) handleDNSBLWithLookup(w http.ResponseWriter, r *http.Request, ip string, lg *slog.Logger, lookup func(string) (dnsbl.DroneBLResponse, error)) bool {
db := &store.JSON[dnsbl.DroneBLResponse]{Underlying: s.store, Prefix: "dronebl:"}
if s.policy.DNSBL && ip != "" {
resp, err := db.Get(r.Context(), ip)
if err != nil {
lg.Debug("looking up ip in dnsbl")
resp, err := dnsbl.Lookup(ip)
lg.DebugContext(r.Context(), "looking up ip in dnsbl")
resp, err = lookup(ip)
if err != nil {
lg.Error("can't look up ip in dnsbl", "err", err)
lg.ErrorContext(r.Context(), "can't look up ip in dnsbl", "err", err)
s.respondWithStatus(w, r, localization.GetLocalizer(r).T("internal_server_error"), "", http.StatusServiceUnavailable)
return true
}
db.Set(r.Context(), ip, resp, 24*time.Hour)
droneBLHits.WithLabelValues(resp.String()).Inc()
_ = db.Set(r.Context(), ip, resp, 24*time.Hour) // worst case we do the dns lookup again
asn, asnDesc := asnFromContext(r.Context())
droneBLHits.WithLabelValues(resp.String(), asn, asnDesc).Inc()
}
if resp != dnsbl.AllGood {
lg.Info("DNSBL hit", "status", resp.String())
lg.InfoContext(r.Context(), "DNSBL hit", "status", resp.String())
localizer := localization.GetLocalizer(r)
s.respondWithStatus(w, r, fmt.Sprintf("%s: %s, %s https://dronebl.org/lookup?ip=%s",
localizer.T("dronebl_entry"),
@@ -359,19 +523,25 @@ func (s *Server) handleDNSBL(w http.ResponseWriter, r *http.Request, ip string,
}
func (s *Server) MakeChallenge(w http.ResponseWriter, r *http.Request) {
lg := internal.GetRequestLogger(s.logger, r)
if !prepareChallengeForm(w, r) {
return
}
defer cleanupChallengeForm(r)
lg, r := s.getRequestLogger(r)
localizer := localization.GetLocalizer(r)
redir := r.FormValue("redir")
if redir == "" {
w.WriteHeader(http.StatusBadRequest)
encoder := json.NewEncoder(w)
lg.Error("invalid invocation of MakeChallenge", "redir", redir)
encoder.Encode(struct {
lg.ErrorContext(r.Context(), "invalid invocation of MakeChallenge", "redir", redir)
if err := encoder.Encode(struct {
Error string `json:"error"`
}{
Error: localizer.T("invalid_invocation"),
})
}); err != nil {
s.logger.DebugContext(r.Context(), "can't write invalid invocation error to client, did they disconnect?", "err", err)
}
return
}
@@ -380,7 +550,7 @@ func (s *Server) MakeChallenge(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
cr, rule, err := s.check(r, lg)
if err != nil {
lg.Error("check failed", "err", err)
lg.ErrorContext(r.Context(), "check failed", "err", err)
w.WriteHeader(http.StatusInternalServerError)
err := encoder.Encode(struct {
Error string `json:"error"`
@@ -388,7 +558,7 @@ func (s *Server) MakeChallenge(w http.ResponseWriter, r *http.Request) {
Error: fmt.Sprintf("%s \"makeChallenge\"", localizer.T("internal_server_error")),
})
if err != nil {
lg.Error("failed to encode error response", "err", err)
lg.ErrorContext(r.Context(), "failed to encode error response", "err", err)
w.WriteHeader(http.StatusInternalServerError)
}
return
@@ -397,7 +567,7 @@ func (s *Server) MakeChallenge(w http.ResponseWriter, r *http.Request) {
chall, err := s.issueChallenge(r.Context(), r, lg, cr, rule)
if err != nil {
lg.Error("failed to fetch or issue challenge", "err", err)
lg.ErrorContext(r.Context(), "failed to fetch or issue challenge", "err", err)
w.WriteHeader(http.StatusInternalServerError)
err := encoder.Encode(struct {
Error string `json:"error"`
@@ -405,7 +575,7 @@ func (s *Server) MakeChallenge(w http.ResponseWriter, r *http.Request) {
Error: fmt.Sprintf("%s \"makeChallenge\"", localizer.T("internal_server_error")),
})
if err != nil {
lg.Error("failed to encode error response", "err", err)
lg.ErrorContext(r.Context(), "failed to encode error response", "err", err)
w.WriteHeader(http.StatusInternalServerError)
}
return
@@ -423,32 +593,40 @@ func (s *Server) MakeChallenge(w http.ResponseWriter, r *http.Request) {
ID: chall.ID,
})
if err != nil {
lg.Error("failed to encode challenge", "err", err)
lg.ErrorContext(r.Context(), "failed to encode challenge", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
lg.Debug("made challenge", "challenge", chall, "rules", rule.Challenge, "cr", cr)
challengesIssued.WithLabelValues("api").Inc()
lg.DebugContext(r.Context(), "made challenge", "challenge", chall, "rules", rule.Challenge, "cr", cr)
{
asn, asnDesc := asnFromContext(r.Context())
challengesIssued.WithLabelValues("api", asn, asnDesc).Inc()
}
}
func (s *Server) validateExtension(name string, r *http.Request, lg *slog.Logger, in *challenge.ValidateInput) error {
ext, ok := extension.Get(name)
if !ok {
return fmt.Errorf("%w: %q", ErrUnknownChallengeExtension, name)
}
return ext.Validate(r, lg, in)
}
func (s *Server) PassChallenge(w http.ResponseWriter, r *http.Request) {
lg := internal.GetRequestLogger(s.logger, r)
if !prepareChallengeForm(w, r) {
return
}
defer cleanupChallengeForm(r)
lg, r := s.getRequestLogger(r)
localizer := localization.GetLocalizer(r)
redir := r.FormValue("redir")
redirURL, err := url.ParseRequestURI(redir)
if err != nil {
lg.Error("invalid redirect", "err", err)
s.respondWithStatus(w, r, localizer.T("invalid_redirect"), makeCode(err), http.StatusBadRequest)
return
redirURL, err := s.validateRedirect(redir)
if err == nil && (redir == "" || (redirURL.Scheme == "" && !strings.HasPrefix(redir, "/"))) {
err = ErrInvalidRedirect
}
switch redirURL.Scheme {
case "", "http", "https":
// allowed
default:
lg.Error("XSS attempt blocked, invalid redirect scheme", "scheme", redirURL.Scheme)
s.respondWithStatus(w, r, localizer.T("invalid_redirect"), "", http.StatusBadRequest)
if err != nil {
s.rejectRedirect(w, r, err)
return
}
@@ -458,10 +636,15 @@ func (s *Server) PassChallenge(w http.ResponseWriter, r *http.Request) {
cookiePath = strings.TrimSuffix(anubis.BasePrefix, "/") + "/"
}
if _, err := r.Cookie(anubis.TestCookieName); errors.Is(err, http.ErrNoCookie) {
if _, err := s.getCookie(r, anubis.TestCookieName); errors.Is(err, http.ErrNoCookie) {
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
s.ClearCookie(w, CookieOpts{Name: anubis.TestCookieName, Host: r.Host})
lg.Warn("user has cookies disabled, this is not an anubis bug")
if s.shouldRetryMissingTestCookie(r) {
lg.DebugContext(r.Context(), "verification cookie missing, retrying with a fresh challenge")
http.Redirect(w, r, redir, http.StatusFound)
return
}
lg.WarnContext(r.Context(), "user has cookies disabled, this is not an anubis bug")
s.respondWithError(w, r, localizer.T("cookies_disabled"), "")
return
}
@@ -469,43 +652,44 @@ func (s *Server) PassChallenge(w http.ResponseWriter, r *http.Request) {
// used by the path checker rule
r.URL = redirURL
urlParsed, err := r.URL.Parse(redir)
if err != nil {
s.respondWithError(w, r, localizer.T("redirect_not_parseable"), makeCode(err))
return
}
if (len(urlParsed.Host) > 0 && len(s.opts.RedirectDomains) != 0 && !matchRedirectDomain(s.opts.RedirectDomains, urlParsed.Host)) || urlParsed.Host != r.URL.Host {
lg.Debug("domain not allowed", "domain", urlParsed.Host)
s.respondWithError(w, r, localizer.T("redirect_domain_not_allowed"), "")
return
}
cr, rule, err := s.check(r, lg)
if err != nil {
lg.Error("check failed", "err", err)
lg.ErrorContext(r.Context(), "check failed", "err", err)
s.respondWithError(w, r, fmt.Sprintf("%s \"passChallenge\"", localizer.T("internal_server_error")), makeCode(err))
return
}
lg = lg.With("check_result", cr)
lockIndex := sha256.Sum256([]byte(r.FormValue("id")))
lock := &s.challengeLocks[lockIndex[0]]
lock.Lock()
defer lock.Unlock()
chall, err := s.getChallenge(r)
if err != nil {
lg.Error("getChallenge failed", "err", err)
s.respondWithError(w, r, fmt.Sprintf("%s: %s", localizer.T("internal_server_error"), rule.Challenge.Algorithm), makeCode(err))
lg.ErrorContext(r.Context(), "getChallenge failed", "err", err)
s.respondWithError(w, r, fmt.Sprintf("%s: %s", localizer.T("internal_server_error"), "getChallenge failed"), makeCode(err))
return
}
if chall.Spent {
lg.Error("double spend prevented", "reason", "double_spend")
lg.ErrorContext(r.Context(), "double spend prevented", "reason", "double_spend")
s.respondWithError(w, r, fmt.Sprintf("%s: %s", localizer.T("internal_server_error"), "double_spend"), "")
return
}
if cr.Rule != config.RuleChallenge || rule == nil || rule.Challenge == nil ||
chall.PolicyRuleHash != rule.Hash() || chall.Method != rule.Challenge.Algorithm ||
chall.Difficulty != rule.Challenge.Difficulty || !slices.Equal(chall.Extensions, rule.Challenge.Extensions) {
s.respondWithStatus(w, r, localizer.T("internal_server_error"), makeCode(challenge.ErrFailed), http.StatusForbidden)
return
}
rule = s.hydrateChallengeRule(rule, chall, lg)
impl, ok := challenge.Get(chall.Method)
if !ok {
lg.Error("check failed", "err", err)
lg.ErrorContext(r.Context(), "check failed", "err", err)
s.respondWithError(w, r, fmt.Sprintf("%s: %s", localizer.T("internal_server_error"), rule.Challenge.Algorithm), makeCode(ErrActualAnubisBug))
return
}
@@ -519,26 +703,56 @@ func (s *Server) PassChallenge(w http.ResponseWriter, r *http.Request) {
}
if err := impl.Validate(r, lg, in); err != nil {
failedValidations.WithLabelValues(rule.Challenge.Algorithm).Inc()
asn, asnDesc := asnFromContext(r.Context())
failedValidations.WithLabelValues(rule.Challenge.Algorithm, asn, asnDesc).Inc()
var cerr *challenge.Error
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
lg.Debug("challenge validate call failed", "err", err)
lg.DebugContext(r.Context(), "challenge validate call failed", "err", err)
switch {
case errors.As(err, &cerr):
switch {
case errors.Is(err, challenge.ErrFailed):
lg.Error("challenge failed", "err", err)
lg.ErrorContext(r.Context(), "challenge failed", "err", err)
s.respondWithStatus(w, r, cerr.PublicReason, makeCode(err), cerr.StatusCode)
return
case errors.Is(err, challenge.ErrInvalidFormat), errors.Is(err, challenge.ErrMissingField):
lg.Error("invalid challenge format", "err", err)
lg.ErrorContext(r.Context(), "invalid challenge format", "err", err)
s.respondWithError(w, r, cerr.PublicReason, makeCode(err))
return
}
}
s.respondWithError(w, r, localizer.T("internal_server_error"), makeCode(err))
return
}
for _, name := range chall.Extensions {
if err := s.validateExtension(name, r, lg, in); err != nil {
asn, asnDesc := asnFromContext(r.Context())
failedValidations.WithLabelValues("extension/"+name, asn, asnDesc).Inc()
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
lg.ErrorContext(r.Context(), "challenge extension failed", "extension", name, "err", err)
var cerr *challenge.Error
if errors.As(err, &cerr) {
s.respondWithStatus(w, r, cerr.PublicReason, makeCode(err), cerr.StatusCode)
} else {
s.respondWithError(w, r, localizer.T("internal_server_error"), makeCode(err))
}
return
}
}
chall.Spent = true
j := store.JSON[challenge.Challenge]{Underlying: s.store}
if err := j.Set(r.Context(), "challenge:"+chall.ID, *chall, 30*time.Minute); err != nil {
lg.DebugContext(r.Context(), "can't update information about challenge", "err", err)
s.respondWithError(w, r, localizer.T("internal_server_error"), makeCode(err))
return
}
lg.InfoContext(r.Context(), "challenge accepted")
// generate JWT cookie
var tokenString string
@@ -551,7 +765,7 @@ func (s *Server) PassChallenge(w http.ResponseWriter, r *http.Request) {
}
if s.opts.JWTRestrictionHeader != "" {
if r.Header.Get(s.opts.JWTRestrictionHeader) == "" {
lg.Error("JWTRestrictionHeader is set in config but not found in request, please check your reverse proxy config.")
lg.ErrorContext(r.Context(), "JWTRestrictionHeader is set in config but not found in request, please check your reverse proxy config.")
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
s.respondWithError(w, r, "failed to sign JWT", makeCode(err))
return
@@ -565,7 +779,7 @@ func (s *Server) PassChallenge(w http.ResponseWriter, r *http.Request) {
tokenString, err = s.signJWT(claims)
if err != nil {
lg.Error("failed to sign JWT", "err", err)
lg.ErrorContext(r.Context(), "failed to sign JWT", "err", err)
s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host})
s.respondWithError(w, r, localizer.T("failed_to_sign_jwt"), makeCode(err))
return
@@ -573,15 +787,69 @@ func (s *Server) PassChallenge(w http.ResponseWriter, r *http.Request) {
s.SetCookie(w, CookieOpts{Path: cookiePath, Host: r.Host, Value: tokenString})
chall.Spent = true
j := store.JSON[challenge.Challenge]{Underlying: s.store}
if err := j.Set(r.Context(), "challenge:"+chall.ID, *chall, 30*time.Minute); err != nil {
lg.Debug("can't update information about challenge", "err", err)
if origReferer := chall.Metadata["Referer"]; origReferer != "" {
s.SetCookie(w, CookieOpts{
Path: cookiePath,
Host: r.Host,
Name: anubis.OriginalRefererCookieName,
Value: url.QueryEscape(origReferer),
Expiry: 1 * time.Minute,
})
}
challengesValidated.WithLabelValues(rule.Challenge.Algorithm).Inc()
lg.Debug("challenge passed, redirecting to app")
http.Redirect(w, r, redir, http.StatusFound)
{
asn, asnDesc := asnFromContext(r.Context())
challengesValidated.WithLabelValues(rule.Challenge.Algorithm, asn, asnDesc).Inc()
}
lg.DebugContext(r.Context(), "challenge passed, redirecting to app")
http.Redirect(w, r, s.withRefererAttributionQuery(redir, chall.Metadata["Referer"], r.Host), http.StatusFound)
}
// refererAttributionQueryParams lists query parameters that already carry
// campaign or referrer attribution. When the redirect target has any of these,
// withRefererAttributionQuery leaves it alone rather than overwrite genuine
// campaign data.
var refererAttributionQueryParams = []string{
"ref", "source",
"utm_source", "utm_medium", "utm_campaign", "utm_content", "utm_term",
"gclid", "msclkid",
}
// withRefererAttributionQuery appends utm_source/utm_medium query parameters
// derived from the original external Referer to redir, so client-side analytics
// tools that fall back to query parameters when document.referrer is unusable
// (e.g. Plausible, GA4) can still attribute the visit. This exists because
// document.referrer itself cannot be fixed here: it reflects the browser's
// actual navigation history -- the challenge page -- and neither Anubis nor any
// page script can override it. Opt-in via PreserveRefererQueryParam since it
// changes the URL the visitor lands on. See
// https://github.com/TecharoHQ/anubis/issues/1596
func (s *Server) withRefererAttributionQuery(redir, origReferer, host string) string {
if !s.opts.PreserveRefererQueryParam || origReferer == "" {
return redir
}
refURL, err := url.Parse(origReferer)
if err != nil || refURL.Host == "" || refURL.Host == host {
return redir
}
u, err := url.Parse(redir)
if err != nil {
return redir
}
q := u.Query()
for _, name := range refererAttributionQueryParams {
if q.Has(name) {
return redir
}
}
q.Set("utm_source", refURL.Host)
q.Set("utm_medium", "referral")
u.RawQuery = q.Encode()
return u.String()
}
func cr(name string, rule config.Rule, weight int) policy.CheckResult {
@@ -594,9 +862,12 @@ func cr(name string, rule config.Rule, weight int) policy.CheckResult {
// Check evaluates the list of rules, and returns the result
func (s *Server) check(r *http.Request, lg *slog.Logger) (policy.CheckResult, *policy.Bot, error) {
host := r.Header.Get("X-Real-Ip")
if err := s.policy.ValidateRequestPath(r); err != nil {
return policy.CheckResult{}, nil, err
}
host := r.Header.Get("X-Real-IP")
if host == "" {
return decaymap.Zilch[policy.CheckResult](), nil, fmt.Errorf("[misconfiguration] X-Real-Ip header is not set")
return decaymap.Zilch[policy.CheckResult](), nil, fmt.Errorf("[misconfiguration] X-Real-IP header is not set")
}
addr := net.ParseIP(host)
@@ -606,7 +877,9 @@ func (s *Server) check(r *http.Request, lg *slog.Logger) (policy.CheckResult, *p
weight := 0
for _, b := range s.policy.Bots {
// Ranging by index keeps b from escaping to the heap on every iteration.
for i := range s.policy.Bots {
b := &s.policy.Bots[i]
match, err := b.Rules.Check(r)
if err != nil {
return decaymap.Zilch[policy.CheckResult](), nil, fmt.Errorf("can't run check %s: %w", b.Name, err)
@@ -615,10 +888,13 @@ func (s *Server) check(r *http.Request, lg *slog.Logger) (policy.CheckResult, *p
if match {
switch b.Action {
case config.RuleDeny, config.RuleAllow, config.RuleBenchmark, config.RuleChallenge:
return cr("bot/"+b.Name, b.Action, weight), &b, nil
// Return a copy of the rule, as the shared policy must not be modified.
bot := *b
return cr("bot/"+b.Name, b.Action, weight), &bot, nil
case config.RuleWeigh:
lg.Debug("adjusting weight", "name", b.Name, "delta", b.Weight.Adjust)
policy.Applications.WithLabelValues("bot/"+b.Name, "WEIGH").Add(1)
lg.DebugContext(r.Context(), "adjusting weight", "name", b.Name, "delta", b.Weight.Adjust)
asn, asnDesc := asnFromContext(r.Context())
policy.Applications.WithLabelValues("bot/"+b.Name, "WEIGH", asn, asnDesc).Add(1)
weight += b.Weight.Adjust
}
}
@@ -627,7 +903,7 @@ func (s *Server) check(r *http.Request, lg *slog.Logger) (policy.CheckResult, *p
for _, t := range s.policy.Thresholds {
result, _, err := t.Program.ContextEval(r.Context(), &policy.ThresholdRequest{Weight: weight})
if err != nil {
lg.Error("error when evaluating threshold expression", "expression", t.Expression.String(), "err", err)
lg.ErrorContext(r.Context(), "error when evaluating threshold expression", "expression", t.Expression.String(), "err", err)
continue
}
@@ -638,14 +914,30 @@ func (s *Server) check(r *http.Request, lg *slog.Logger) (policy.CheckResult, *p
}
if matches {
challRules := t.Challenge
if challRules == nil {
// Non-CHALLENGE thresholds (ALLOW/DENY) don't have challenge config.
// Use an empty struct so hydrateChallengeRule can fill from stored
// challenge data during validation, rather than baking in defaults
// that could mismatch the difficulty the client actually solved for.
challRules = &config.ChallengeRules{}
}
identity, err := json.Marshal(t.Threshold)
if err != nil {
return policy.CheckResult{}, nil, err
}
return cr("threshold/"+t.Name, t.Action, weight), &policy.Bot{
Challenge: t.Challenge,
Name: "threshold/" + t.Name + ":" + internal.FastHash(string(identity)),
Action: t.Action,
Challenge: challRules,
Rules: &checker.List{},
}, nil
}
}
return cr("default/allow", config.RuleAllow, weight), &policy.Bot{
Name: "default/allow",
Action: config.RuleAllow,
Challenge: &config.ChallengeRules{
Difficulty: s.policy.DefaultDifficulty,
Algorithm: config.DefaultAlgorithm,