Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/anubis: add rule hashes for admin-configured denials #696

Merged
merged 2 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/anubis/index_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions cmd/anubis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ func main() {
log.Fatal(err)
}

fmt.Println("Rule error IDs:")
for _, rule := range s.policy.Bots {
if rule.Action != config.RuleDeny {
continue
}

hash, err := rule.Hash()
if err != nil {
log.Fatalf("can't calculate checksum of rule %s: %v", rule.Name, err)
}

fmt.Printf("* %s: %s\n", rule.Name, hash)
}
fmt.Println()

mux := http.NewServeMux()
xess.Mount(mux)

Expand Down Expand Up @@ -229,7 +244,7 @@ type Server struct {
}

func (s *Server) maybeReverseProxy(w http.ResponseWriter, r *http.Request) {
cr := s.check(r)
cr, rule := s.check(r)
r.Header.Add("X-Anubis-Rule", cr.Name)
r.Header.Add("X-Anubis-Action", string(cr.Rule))
lg := slog.With(
Expand Down Expand Up @@ -272,7 +287,19 @@ func (s *Server) maybeReverseProxy(w http.ResponseWriter, r *http.Request) {
case config.RuleDeny:
clearCookie(w)
lg.Info("explicit deny")
templ.Handler(base("Oh noes!", errorPage("Access Denied")), templ.WithStatus(http.StatusOK)).ServeHTTP(w, r)
if rule == nil {
lg.Error("rule is nil, cannot calculate checksum")
templ.Handler(base("Oh noes!", errorPage("Other internal server error (contact the admin)")), templ.WithStatus(http.StatusInternalServerError)).ServeHTTP(w, r)
return
}
hash, err := rule.Hash()
if err != nil {
lg.Error("can't calculate checksum of rule", "err", err)
templ.Handler(base("Oh noes!", errorPage("Other internal server error (contact the admin)")), templ.WithStatus(http.StatusInternalServerError)).ServeHTTP(w, r)
return
}
lg.Debug("rule hash", "hash", hash)
templ.Handler(base("Oh noes!", errorPage(fmt.Sprintf("Access Denied: error code %s", hash))), templ.WithStatus(http.StatusOK)).ServeHTTP(w, r)
return
case config.RuleChallenge:
lg.Debug("challenge requested")
Expand Down
21 changes: 17 additions & 4 deletions cmd/anubis/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ type Bot struct {
Action config.Rule `json:"action"`
}

func (b Bot) Hash() (string, error) {
var pathRex string
if b.Path != nil {
pathRex = b.Path.String()
}
var userAgentRex string
if b.UserAgent != nil {
userAgentRex = b.UserAgent.String()
}

return sha256sum(fmt.Sprintf("%s::%s::%s", b.Name, pathRex, userAgentRex))
}

func parseConfig(fin io.Reader, fname string) (*ParsedConfig, error) {
var c config.Config
if err := json.NewDecoder(fin).Decode(&c); err != nil {
Expand Down Expand Up @@ -110,20 +123,20 @@ func cr(name string, rule config.Rule) CheckResult {
}

// Check evaluates the list of rules, and returns the result
func (s *Server) check(r *http.Request) CheckResult {
func (s *Server) check(r *http.Request) (CheckResult, *Bot) {
for _, b := range s.policy.Bots {
if b.UserAgent != nil {
if b.UserAgent.MatchString(r.UserAgent()) {
return cr("bot/"+b.Name, b.Action)
return cr("bot/"+b.Name, b.Action), &b
}
}

if b.Path != nil {
if b.Path.MatchString(r.URL.Path) {
return cr("bot/"+b.Name, b.Action)
return cr("bot/"+b.Name, b.Action), &b
}
}
}

return cr("default/allow", config.RuleAllow)
return cr("default/allow", config.RuleAllow), nil
}
Binary file modified cmd/anubis/static/js/main.mjs.gz
Binary file not shown.
10 changes: 10 additions & 0 deletions cmd/anubis/testdata/everything_blocked.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"bots": [
{
"name": "everything",
"user_agent_regex": ".*",
"action": "DENY"
}
],
"dnsbl": false
}
Loading