Merge branch 'main' of https://github.com/TecharoHQ/anubis into fix/ogtags-sni

This commit is contained in:
Jason Cameron
2025-11-16 17:57:19 -05:00
26 changed files with 89 additions and 90 deletions
+1 -1
View File
@@ -22,9 +22,9 @@ type TestCase struct {
type TestOptions struct { type TestOptions struct {
format string format string
action string action string
crawlDelayWeight int
policyName string policyName string
deniedAction string deniedAction string
crawlDelayWeight int
} }
func TestDataFileConversion(t *testing.T) { func TestDataFileConversion(t *testing.T) {
+1 -1
View File
@@ -13,13 +13,13 @@ func Zilch[T any]() T {
// Impl is a lazy key->value map. It's a wrapper around a map and a mutex. If values exceed their time-to-live, they are pruned at Get time. // Impl is a lazy key->value map. It's a wrapper around a map and a mutex. If values exceed their time-to-live, they are pruned at Get time.
type Impl[K comparable, V any] struct { type Impl[K comparable, V any] struct {
data map[K]decayMapEntry[V] data map[K]decayMapEntry[V]
lock sync.RWMutex
// deleteCh receives decay-deletion requests from readers. // deleteCh receives decay-deletion requests from readers.
deleteCh chan deleteReq[K] deleteCh chan deleteReq[K]
// stopCh stops the background cleanup worker. // stopCh stops the background cleanup worker.
stopCh chan struct{} stopCh chan struct{}
wg sync.WaitGroup wg sync.WaitGroup
lock sync.RWMutex
} }
type decayMapEntry[V any] struct { type decayMapEntry[V any] struct {
+11 -11
View File
@@ -23,21 +23,21 @@ const (
) )
type OGTagCache struct { type OGTagCache struct {
cache store.JSON[map[string]string] ogOverride map[string]string
targetURL *url.URL targetURL *url.URL
client *http.Client client *http.Client
transport *http.Transport transport *http.Transport
cache store.JSON[map[string]string]
// Pre-built strings for optimization // Pre-built strings for optimization
unixPrefix string // "http://unix" unixPrefix string // "http://unix"
approvedTags []string
approvedPrefixes []string
ogTimeToLive time.Duration
ogCacheConsiderHost bool
ogPassthrough bool
ogOverride map[string]string
targetHost string
targetSNI string targetSNI string
targetHost string
approvedPrefixes []string
approvedTags []string
ogTimeToLive time.Duration
ogPassthrough bool
ogCacheConsiderHost bool
targetSNIAuto bool targetSNIAuto bool
insecureSkipVerify bool insecureSkipVerify bool
} }
+3 -3
View File
@@ -68,14 +68,14 @@ var (
type Server struct { type Server struct {
next http.Handler next http.Handler
store store.Interface
mux *http.ServeMux mux *http.ServeMux
policy *policy.ParsedConfig policy *policy.ParsedConfig
OGTags *ogtags.OGTagCache OGTags *ogtags.OGTagCache
logger *slog.Logger
opts Options
ed25519Priv ed25519.PrivateKey ed25519Priv ed25519.PrivateKey
hs512Secret []byte hs512Secret []byte
opts Options
store store.Interface
logger *slog.Logger
} }
func (s *Server) getTokenKeyfunc() jwt.Keyfunc { func (s *Server) getTokenKeyfunc() jwt.Keyfunc {
+2 -2
View File
@@ -154,8 +154,8 @@ func handleChallengeZeroDifficulty(t *testing.T, ts *httptest.Server, cli *http.
type loggingCookieJar struct { type loggingCookieJar struct {
t *testing.T t *testing.T
lock sync.Mutex
cookies map[string][]*http.Cookie cookies map[string][]*http.Cookie
lock sync.Mutex
} }
func (lcj *loggingCookieJar) Cookies(u *url.URL) []*http.Cookie { func (lcj *loggingCookieJar) Cookies(u *url.URL) []*http.Cookie {
@@ -747,9 +747,9 @@ func TestStripBasePrefixFromRequest(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
basePrefix string basePrefix string
stripBasePrefix bool
requestPath string requestPath string
expectedPath string expectedPath string
stripBasePrefix bool
}{ }{
{ {
name: "strip disabled - no change", name: "strip disabled - no change",
+8 -8
View File
@@ -4,12 +4,12 @@ import "time"
// Challenge is the metadata about a single challenge issuance. // Challenge is the metadata about a single challenge issuance.
type Challenge struct { type Challenge struct {
ID string `json:"id"` // UUID identifying the challenge IssuedAt time.Time `json:"issuedAt"`
Method string `json:"method"` // Challenge method Metadata map[string]string `json:"metadata"`
RandomData string `json:"randomData"` // The random data the client processes ID string `json:"id"`
IssuedAt time.Time `json:"issuedAt"` // When the challenge was issued Method string `json:"method"`
Metadata map[string]string `json:"metadata"` // Challenge metadata such as IP address and user agent RandomData string `json:"randomData"`
Spent bool `json:"spent"` // Has the challenge already been solved? PolicyRuleHash string `json:"policyRuleHash,omitempty"`
Difficulty int `json:"difficulty,omitempty"` // Difficulty that was in effect when issued Difficulty int `json:"difficulty,omitempty"`
PolicyRuleHash string `json:"policyRuleHash,omitempty"` // Hash of the policy rule that issued this challenge Spent bool `json:"spent"`
} }
+1 -1
View File
@@ -13,9 +13,9 @@ import (
func TestSetCookie(t *testing.T) { func TestSetCookie(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
name string name string
options Options
host string host string
cookieName string cookieName string
options Options
}{ }{
{ {
name: "basic", name: "basic",
+2 -2
View File
@@ -8,9 +8,9 @@ import (
func TestASNsValid(t *testing.T) { func TestASNsValid(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
name string
input *ASNs
err error err error
input *ASNs
name string
}{ }{
{ {
name: "basic valid", name: "basic valid",
+11 -10
View File
@@ -62,13 +62,14 @@ type BotConfig struct {
Expression *ExpressionOrList `json:"expression,omitempty" yaml:"expression,omitempty"` Expression *ExpressionOrList `json:"expression,omitempty" yaml:"expression,omitempty"`
Challenge *ChallengeRules `json:"challenge,omitempty" yaml:"challenge,omitempty"` Challenge *ChallengeRules `json:"challenge,omitempty" yaml:"challenge,omitempty"`
Weight *Weight `json:"weight,omitempty" yaml:"weight,omitempty"` Weight *Weight `json:"weight,omitempty" yaml:"weight,omitempty"`
Name string `json:"name" yaml:"name"`
Action Rule `json:"action" yaml:"action"`
RemoteAddr []string `json:"remote_addresses,omitempty" yaml:"remote_addresses,omitempty"`
// Thoth features // Thoth features
GeoIP *GeoIP `json:"geoip,omitempty"` GeoIP *GeoIP `json:"geoip,omitempty"`
ASNs *ASNs `json:"asns,omitempty"` ASNs *ASNs `json:"asns,omitempty"`
Name string `json:"name" yaml:"name"`
Action Rule `json:"action" yaml:"action"`
RemoteAddr []string `json:"remote_addresses,omitempty" yaml:"remote_addresses,omitempty"`
} }
func (b BotConfig) Zero() bool { func (b BotConfig) Zero() bool {
@@ -324,13 +325,13 @@ func (sc StatusCodes) Valid() error {
} }
type fileConfig struct { type fileConfig struct {
Bots []BotOrImport `json:"bots"`
DNSBL bool `json:"dnsbl"`
OpenGraph openGraphFileConfig `json:"openGraph,omitempty"` OpenGraph openGraphFileConfig `json:"openGraph,omitempty"`
Impressum *Impressum `json:"impressum,omitempty"` Impressum *Impressum `json:"impressum,omitempty"`
StatusCodes StatusCodes `json:"status_codes"`
Store *Store `json:"store"` Store *Store `json:"store"`
Bots []BotOrImport `json:"bots"`
Thresholds []Threshold `json:"thresholds"` Thresholds []Threshold `json:"thresholds"`
StatusCodes StatusCodes `json:"status_codes"`
DNSBL bool `json:"dnsbl"`
} }
func (c *fileConfig) Valid() error { func (c *fileConfig) Valid() error {
@@ -462,13 +463,13 @@ func Load(fin io.Reader, fname string) (*Config, error) {
} }
type Config struct { type Config struct {
Impressum *Impressum
Store *Store
OpenGraph OpenGraph
Bots []BotConfig Bots []BotConfig
Thresholds []Threshold Thresholds []Threshold
DNSBL bool
Impressum *Impressum
OpenGraph OpenGraph
StatusCodes StatusCodes StatusCodes StatusCodes
Store *Store DNSBL bool
} }
func (c Config) Valid() error { func (c Config) Valid() error {
+1 -1
View File
@@ -15,9 +15,9 @@ func p[V any](v V) *V { return &v }
func TestBotValid(t *testing.T) { func TestBotValid(t *testing.T) {
var tests = []struct { var tests = []struct {
bot BotConfig
err error err error
name string name string
bot BotConfig
}{ }{
{ {
name: "simple user agent", name: "simple user agent",
+7 -7
View File
@@ -11,10 +11,10 @@ import (
func TestExpressionOrListMarshalJSON(t *testing.T) { func TestExpressionOrListMarshalJSON(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
name string
input *ExpressionOrList
output []byte
err error err error
input *ExpressionOrList
name string
output []byte
}{ }{
{ {
name: "single expression", name: "single expression",
@@ -74,10 +74,10 @@ func TestExpressionOrListMarshalJSON(t *testing.T) {
func TestExpressionOrListMarshalYAML(t *testing.T) { func TestExpressionOrListMarshalYAML(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
name string
input *ExpressionOrList
output []byte
err error err error
input *ExpressionOrList
name string
output []byte
}{ }{
{ {
name: "single expression", name: "single expression",
@@ -217,8 +217,8 @@ func TestExpressionOrListUnmarshalJSON(t *testing.T) {
func TestExpressionOrListString(t *testing.T) { func TestExpressionOrListString(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
name string name string
in ExpressionOrList
out string out string
in ExpressionOrList
}{ }{
{ {
name: "single expression", name: "single expression",
+2 -2
View File
@@ -7,9 +7,9 @@ import (
func TestGeoIPValid(t *testing.T) { func TestGeoIPValid(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
name string
input *GeoIP
err error err error
input *GeoIP
name string
}{ }{
{ {
name: "basic valid", name: "basic valid",
+2 -2
View File
@@ -8,9 +8,9 @@ import (
func TestImpressumValid(t *testing.T) { func TestImpressumValid(t *testing.T) {
for _, cs := range []struct { for _, cs := range []struct {
name string
inp Impressum
err error err error
inp Impressum
name string
}{ }{
{ {
name: "basic happy path", name: "basic happy path",
+4 -4
View File
@@ -13,17 +13,17 @@ var (
) )
type openGraphFileConfig struct { type openGraphFileConfig struct {
Override map[string]string `json:"override,omitempty" yaml:"override,omitempty"`
TimeToLive string `json:"ttl" yaml:"ttl"`
Enabled bool `json:"enabled" yaml:"enabled"` Enabled bool `json:"enabled" yaml:"enabled"`
ConsiderHost bool `json:"considerHost" yaml:"enabled"` ConsiderHost bool `json:"considerHost" yaml:"enabled"`
TimeToLive string `json:"ttl" yaml:"ttl"`
Override map[string]string `json:"override,omitempty" yaml:"override,omitempty"`
} }
type OpenGraph struct { type OpenGraph struct {
Enabled bool `json:"enabled" yaml:"enabled"`
ConsiderHost bool `json:"considerHost" yaml:"enabled"`
Override map[string]string `json:"override,omitempty" yaml:"override,omitempty"` Override map[string]string `json:"override,omitempty" yaml:"override,omitempty"`
TimeToLive time.Duration `json:"ttl" yaml:"ttl"` TimeToLive time.Duration `json:"ttl" yaml:"ttl"`
Enabled bool `json:"enabled" yaml:"enabled"`
ConsiderHost bool `json:"considerHost" yaml:"enabled"`
} }
func (og *openGraphFileConfig) Valid() error { func (og *openGraphFileConfig) Valid() error {
+2 -2
View File
@@ -7,9 +7,9 @@ import (
func TestOpenGraphFileConfigValid(t *testing.T) { func TestOpenGraphFileConfigValid(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
name string
input *openGraphFileConfig
err error err error
input *openGraphFileConfig
name string
}{ }{
{ {
name: "basic happy path", name: "basic happy path",
+1 -1
View File
@@ -12,9 +12,9 @@ import (
func TestStoreValid(t *testing.T) { func TestStoreValid(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
err error
name string name string
input config.Store input config.Store
err error
}{ }{
{ {
name: "no backend", name: "no backend",
+2 -2
View File
@@ -31,10 +31,10 @@ var (
) )
type Threshold struct { type Threshold struct {
Name string `json:"name" yaml:"name"`
Expression *ExpressionOrList `json:"expression" yaml:"expression"` Expression *ExpressionOrList `json:"expression" yaml:"expression"`
Action Rule `json:"action" yaml:"action"`
Challenge *ChallengeRules `json:"challenge" yaml:"challenge"` Challenge *ChallengeRules `json:"challenge" yaml:"challenge"`
Name string `json:"name" yaml:"name"`
Action Rule `json:"action" yaml:"action"`
} }
func (t Threshold) Valid() error { func (t Threshold) Valid() error {
+2 -2
View File
@@ -10,9 +10,9 @@ import (
func TestThresholdValid(t *testing.T) { func TestThresholdValid(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
name string
input *Threshold
err error err error
input *Threshold
name string
}{ }{
{ {
name: "basic allow", name: "basic allow",
+5 -5
View File
@@ -14,11 +14,11 @@ func TestBotEnvironment(t *testing.T) {
t.Run("missingHeader", func(t *testing.T) { t.Run("missingHeader", func(t *testing.T) {
tests := []struct { tests := []struct {
headers map[string]string
name string name string
expression string expression string
headers map[string]string
expected types.Bool
description string description string
expected types.Bool
}{ }{
{ {
name: "missing-header", name: "missing-header",
@@ -167,10 +167,10 @@ func TestBotEnvironment(t *testing.T) {
t.Run("invalid", func(t *testing.T) { t.Run("invalid", func(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
env any
name string name string
description string description string
expression string expression string
env any
wantFailCompile bool wantFailCompile bool
wantFailEval bool wantFailEval bool
}{ }{
@@ -244,11 +244,11 @@ func TestThresholdEnvironment(t *testing.T) {
} }
tests := []struct { tests := []struct {
variables map[string]interface{}
name string name string
expression string expression string
variables map[string]interface{}
expected types.Bool
description string description string
expected types.Bool
shouldCompile bool shouldCompile bool
}{ }{
{ {
+1 -1
View File
@@ -10,8 +10,8 @@ import (
) )
type loadAvg struct { type loadAvg struct {
lock sync.RWMutex
data *load.AvgStat data *load.AvgStat
lock sync.RWMutex
} }
func (l *loadAvg) updateThread(ctx context.Context) { func (l *loadAvg) updateThread(ctx context.Context) {
+6 -7
View File
@@ -29,16 +29,15 @@ var (
) )
type ParsedConfig struct { type ParsedConfig struct {
orig *config.Config Store store.Interface
orig *config.Config
Bots []Bot
Thresholds []*Threshold
DNSBL bool
Impressum *config.Impressum Impressum *config.Impressum
OpenGraph config.OpenGraph OpenGraph config.OpenGraph
DefaultDifficulty int Bots []Bot
Thresholds []*Threshold
StatusCodes config.StatusCodes StatusCodes config.StatusCodes
Store store.Interface DefaultDifficulty int
DNSBL bool
} }
func newParsedConfig(orig *config.Config) *ParsedConfig { func newParsedConfig(orig *config.Config) *ParsedConfig {
+8 -9
View File
@@ -13,7 +13,7 @@ import (
func TestRedirectSecurity(t *testing.T) { func TestRedirectSecurity(t *testing.T) {
tests := []struct { tests := []struct {
name string reqHost string
testType string // "constructRedirectURL", "serveHTTPNext", "renderIndex" testType string // "constructRedirectURL", "serveHTTPNext", "renderIndex"
// For constructRedirectURL tests // For constructRedirectURL tests
@@ -23,17 +23,16 @@ func TestRedirectSecurity(t *testing.T) {
// For serveHTTPNext tests // For serveHTTPNext tests
redirParam string redirParam string
reqHost string name string
errorContains string
expectedStatus int
// For renderIndex tests // For renderIndex tests
returnHTTPStatusOnly bool returnHTTPStatusOnly bool
shouldError bool
// Expected results shouldNotRedirect bool
expectedStatus int shouldBlock bool
shouldError bool
shouldNotRedirect bool
shouldBlock bool
errorContains string
}{ }{
// constructRedirectURL tests - X-Forwarded-Proto validation // constructRedirectURL tests - X-Forwarded-Proto validation
{ {
+1 -1
View File
@@ -17,9 +17,9 @@ func TestFactoryValid(t *testing.T) {
t.Run("invalid config", func(t *testing.T) { t.Run("invalid config", func(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
err error
name string name string
cfg Config cfg Config
err error
}{ }{
{ {
name: "missing path", name: "missing path",
+1 -1
View File
@@ -88,8 +88,8 @@ func (Factory) Valid(data json.RawMessage) error {
} }
type Config struct { type Config struct {
PathStyle bool `json:"pathStyle"`
BucketName string `json:"bucketName"` BucketName string `json:"bucketName"`
PathStyle bool `json:"pathStyle"`
} }
func (c Config) Valid() error { func (c Config) Valid() error {
+2 -2
View File
@@ -17,10 +17,10 @@ import (
// mockS3 is an in-memory mock of the methods we use. // mockS3 is an in-memory mock of the methods we use.
type mockS3 struct { type mockS3 struct {
mu sync.RWMutex
bucket string
data map[string][]byte data map[string][]byte
meta map[string]map[string]string meta map[string]map[string]string
bucket string
mu sync.RWMutex
} }
func (m *mockS3) PutObject(ctx context.Context, in *s3.PutObjectInput, _ ...func(*s3.Options)) (*s3.PutObjectOutput, error) { func (m *mockS3) PutObject(ctx context.Context, in *s3.PutObjectInput, _ ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
+2 -2
View File
@@ -21,9 +21,9 @@ func Common(t *testing.T, f store.Factory, config json.RawMessage) {
} }
for _, tt := range []struct { for _, tt := range []struct {
name string
doer func(t *testing.T, s store.Interface) error
err error err error
doer func(t *testing.T, s store.Interface) error
name string
}{ }{
{ {
name: "basic get set delete", name: "basic get set delete",