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:   - 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>
267 lines
7.5 KiB
Go
267 lines
7.5 KiB
Go
package expressions
|
|
|
|
import (
|
|
"math/rand/v2"
|
|
"strings"
|
|
|
|
"cel.dev/cel-go/cel"
|
|
"cel.dev/cel-go/common/types"
|
|
"cel.dev/cel-go/common/types/ref"
|
|
"cel.dev/cel-go/common/types/traits"
|
|
"cel.dev/cel-go/ext"
|
|
"github.com/TecharoHQ/anubis/internal/dns"
|
|
)
|
|
|
|
// BotEnvironment creates a new CEL environment, this is the set of
|
|
// variables and functions that are passed into the CEL scope so that
|
|
// Anubis can fail loudly and early when something is invalid instead
|
|
// of blowing up at runtime.
|
|
func BotEnvironment(dnsObj *dns.Dns) (*cel.Env, error) {
|
|
opts := []cel.EnvOption{
|
|
// Variables exposed to CEL programs:
|
|
cel.Variable("remoteAddress", cel.StringType),
|
|
cel.Variable("contentLength", cel.IntType),
|
|
cel.Variable("host", cel.StringType),
|
|
cel.Variable("method", cel.StringType),
|
|
cel.Variable("userAgent", cel.StringType),
|
|
cel.Variable("path", cel.StringType),
|
|
cel.Variable("query", cel.MapType(cel.StringType, cel.StringType)),
|
|
cel.Variable("headers", cel.MapType(cel.StringType, cel.StringType)),
|
|
cel.Variable("load_1m", cel.DoubleType),
|
|
cel.Variable("load_5m", cel.DoubleType),
|
|
cel.Variable("load_15m", cel.DoubleType),
|
|
|
|
// Bot-specific functions:
|
|
cel.Function("missingHeader",
|
|
cel.Overload("missingHeader_map_string_string_string",
|
|
[]*cel.Type{cel.MapType(cel.StringType, cel.StringType), cel.StringType},
|
|
cel.BoolType,
|
|
cel.BinaryBinding(func(headers, key ref.Val) ref.Val {
|
|
// Convert headers to a trait that supports Find
|
|
headersMap, ok := headers.(traits.Indexer)
|
|
if !ok {
|
|
return types.ValOrErr(headers, "headers is not a map, but is %T", headers)
|
|
}
|
|
|
|
keyStr, ok := key.(types.String)
|
|
if !ok {
|
|
return types.ValOrErr(key, "key is not a string, but is %T", key)
|
|
}
|
|
|
|
val := headersMap.Get(keyStr)
|
|
// Check if the key is missing by testing for an error
|
|
if types.IsError(val) {
|
|
return types.Bool(true) // header is missing
|
|
}
|
|
return types.Bool(false) // header is present
|
|
}),
|
|
),
|
|
),
|
|
|
|
cel.Function("reverseDNS",
|
|
cel.Overload("reverseDNS_string_list_string",
|
|
[]*cel.Type{cel.StringType},
|
|
cel.ListType(cel.StringType),
|
|
cel.UnaryBinding(func(addr ref.Val) ref.Val {
|
|
addrStr, ok := addr.(types.String)
|
|
if !ok {
|
|
return types.ValOrErr(addr, "addr is not a string, but is %T", addr)
|
|
}
|
|
|
|
names, err := dnsObj.ReverseDNS(string(addrStr))
|
|
if err != nil {
|
|
return types.NewStringList(types.DefaultTypeAdapter, []string{})
|
|
}
|
|
return types.NewStringList(types.DefaultTypeAdapter, names)
|
|
}),
|
|
),
|
|
),
|
|
|
|
cel.Function("lookupHost",
|
|
cel.Overload("lookupHost_string_list_string",
|
|
[]*cel.Type{cel.StringType},
|
|
cel.ListType(cel.StringType),
|
|
cel.UnaryBinding(func(host ref.Val) ref.Val {
|
|
hostStr, ok := host.(types.String)
|
|
if !ok {
|
|
return types.ValOrErr(host, "host is not a string, but is %T", host)
|
|
}
|
|
|
|
addrs, err := dnsObj.LookupHost(string(hostStr))
|
|
if err != nil {
|
|
return types.NewStringList(types.DefaultTypeAdapter, []string{})
|
|
}
|
|
return types.NewStringList(types.DefaultTypeAdapter, addrs)
|
|
}),
|
|
),
|
|
),
|
|
|
|
cel.Function("verifyFCrDNS",
|
|
cel.Overload("verifyFCrDNS_string_bool",
|
|
[]*cel.Type{cel.StringType},
|
|
cel.BoolType,
|
|
cel.UnaryBinding(func(addr ref.Val) ref.Val {
|
|
addrStr, ok := addr.(types.String)
|
|
if !ok {
|
|
return types.ValOrErr(addr, "addr is not a string")
|
|
}
|
|
return types.Bool(dnsObj.VerifyFCrDNS(string(addrStr), nil))
|
|
}),
|
|
),
|
|
cel.Overload("verifyFCrDNS_string_string_bool",
|
|
[]*cel.Type{cel.StringType, cel.StringType},
|
|
cel.BoolType,
|
|
cel.BinaryBinding(func(addr, pattern ref.Val) ref.Val {
|
|
addrStr, ok := addr.(types.String)
|
|
if !ok {
|
|
return types.ValOrErr(addr, "addr is not a string")
|
|
}
|
|
patternStr, ok := pattern.(types.String)
|
|
if !ok {
|
|
return types.ValOrErr(pattern, "pattern is not a string")
|
|
}
|
|
p := string(patternStr)
|
|
return types.Bool(dnsObj.VerifyFCrDNS(string(addrStr), &p))
|
|
}),
|
|
),
|
|
),
|
|
|
|
// arpaReverseIP transforms ip into arpa reverse notation like this
|
|
// 1.2.3.4 -> 4.3.2.1
|
|
// 2001:db8::1 -> 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2
|
|
cel.Function("arpaReverseIP",
|
|
cel.Overload("arpaReverseIP_string_string",
|
|
[]*cel.Type{cel.StringType},
|
|
cel.StringType,
|
|
cel.UnaryBinding(func(addr ref.Val) ref.Val {
|
|
s, ok := addr.(types.String)
|
|
if !ok {
|
|
return types.ValOrErr(addr, "addr is not a string")
|
|
}
|
|
|
|
reversedIp, err := dnsObj.ArpaReverseIP(string(s))
|
|
if err != nil {
|
|
return types.ValOrErr(addr, "%s", err.Error())
|
|
}
|
|
return types.String(reversedIp)
|
|
}),
|
|
),
|
|
),
|
|
|
|
// regexSafe escapes a string for insertion into a regular expression
|
|
cel.Function("regexSafe",
|
|
cel.Overload("regexSafe_string_string",
|
|
[]*cel.Type{cel.StringType},
|
|
cel.StringType,
|
|
cel.UnaryBinding(func(str ref.Val) ref.Val {
|
|
s, ok := str.(types.String)
|
|
if !ok {
|
|
return types.ValOrErr(str, "addr is not a string")
|
|
}
|
|
|
|
escapes := []string{"\\", ".", ":", "*", "?", "-", "[", "]", "(", ")", "+", "{", "}", "|", "^", "$"}
|
|
r := string(s)
|
|
|
|
for _, escape := range escapes {
|
|
r = strings.ReplaceAll(r, escape, "\\"+escape)
|
|
}
|
|
return types.String(r)
|
|
}),
|
|
),
|
|
),
|
|
|
|
cel.Function("segments",
|
|
cel.Overload("segments_string_list_string",
|
|
[]*cel.Type{cel.StringType},
|
|
cel.ListType(cel.StringType),
|
|
cel.UnaryBinding(func(path ref.Val) ref.Val {
|
|
pathStrType, ok := path.(types.String)
|
|
if !ok {
|
|
return types.ValOrErr(path, "path is not a string, but is %T", path)
|
|
}
|
|
|
|
pathStr := string(pathStrType)
|
|
if !strings.HasPrefix(pathStr, "/") {
|
|
return types.ValOrErr(path, "path does not start with /")
|
|
}
|
|
|
|
pathList := strings.Split(string(pathStr), "/")[1:]
|
|
|
|
return types.NewStringList(types.DefaultTypeAdapter, pathList)
|
|
}),
|
|
),
|
|
),
|
|
}
|
|
|
|
return New(append(opts, getBotVariables()...)...)
|
|
}
|
|
|
|
// NewThreshold creates a new CEL environment for threshold checking.
|
|
func ThresholdEnvironment() (*cel.Env, error) {
|
|
return New(
|
|
cel.Variable("weight", cel.IntType),
|
|
)
|
|
}
|
|
|
|
func New(opts ...cel.EnvOption) (*cel.Env, error) {
|
|
args := []cel.EnvOption{
|
|
ext.Strings(
|
|
ext.StringsLocale("en_US"),
|
|
ext.StringsValidateFormatCalls(true),
|
|
),
|
|
|
|
// default all timestamps to UTC
|
|
cel.DefaultUTCTimeZone(true),
|
|
|
|
// Functions exposed to all CEL programs:
|
|
cel.Function("randInt",
|
|
cel.Overload("randInt_int",
|
|
[]*cel.Type{cel.IntType},
|
|
cel.IntType,
|
|
cel.UnaryBinding(func(val ref.Val) ref.Val {
|
|
n, ok := val.(types.Int)
|
|
if !ok {
|
|
return types.ValOrErr(val, "value is not an integer, but is %T", val)
|
|
}
|
|
|
|
if n <= 0 {
|
|
return types.NewErr("randInt bound must be positive, got %d", int64(n))
|
|
}
|
|
|
|
bound := int(n)
|
|
if types.Int(bound) != n {
|
|
return types.NewErr("randInt bound %d overflows platform int", int64(n))
|
|
}
|
|
|
|
return types.Int(rand.IntN(bound))
|
|
}),
|
|
),
|
|
),
|
|
}
|
|
|
|
args = append(args, opts...)
|
|
return cel.NewEnv(args...)
|
|
}
|
|
|
|
// Compile takes CEL environment and syntax tree then emits an optimized
|
|
// Program for execution.
|
|
func Compile(env *cel.Env, src string) (cel.Program, error) {
|
|
intermediate, iss := env.Compile(src)
|
|
if iss != nil {
|
|
return nil, iss.Err()
|
|
}
|
|
|
|
ast, iss := env.Check(intermediate)
|
|
if iss != nil {
|
|
return nil, iss.Err()
|
|
}
|
|
|
|
return env.Program(
|
|
ast,
|
|
cel.EvalOptions(
|
|
// optimize regular expressions right now instead of on the fly
|
|
cel.OptOptimize,
|
|
),
|
|
)
|
|
}
|