mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
exact_domain and domain_suffix patterns are now checked before they are stored: whitespace or control characters, empty labels (including more than one trailing dot), a single label, an IP literal or all-digit last label, and a non-ASCII or xn-- label the IDNA profile refuses are answered 400 on the global and task rule APIs. fuzzy_domain refuses whitespace, control characters and a doubled dot. Valid domains keep the canonical form they had, and CanonicalInterceptDomain stays lenient for matching. intercept/target.go now uses idna.Lookup, the profile the rules use. The previous profile had the same options in x/net v0.62.0; its comment said it accepted underscore labels, which it did not.
188 lines
4.9 KiB
Go
188 lines
4.9 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/netip"
|
|
"strings"
|
|
|
|
"github.com/Autumn-27/artex/db"
|
|
)
|
|
|
|
// --- asset intercept rule CRUD ---
|
|
//
|
|
// Global asset blocklist: exact/fuzzy domain·ip·url + CIDR. This layer only
|
|
// persists rules; the actual match/enforcement logic lives elsewhere.
|
|
|
|
func (s *Server) assetInterceptListRules(w http.ResponseWriter, r *http.Request) {
|
|
pg := s.pg(w)
|
|
if pg == nil {
|
|
return
|
|
}
|
|
rules, err := pg.ListAssetInterceptRules()
|
|
if err != nil {
|
|
writeErr(w, 500, err.Error())
|
|
return
|
|
}
|
|
if rules == nil {
|
|
rules = []db.AssetInterceptRule{}
|
|
}
|
|
writeJSON(w, 200, map[string]any{"rules": rules})
|
|
}
|
|
|
|
func (s *Server) assetInterceptCreateRule(w http.ResponseWriter, r *http.Request) {
|
|
pg := s.pg(w)
|
|
if pg == nil {
|
|
return
|
|
}
|
|
var req assetInterceptRuleReq
|
|
if err := decode(w, r, &req); err != nil {
|
|
writeErr(w, 400, err.Error())
|
|
return
|
|
}
|
|
if err := validateAssetInterceptRuleReq(&req); err != nil {
|
|
writeErr(w, 400, err.Error())
|
|
return
|
|
}
|
|
rule, err := pg.CreateAssetInterceptRule(req.Kind, req.Pattern, req.Note, req.Enabled)
|
|
if err != nil {
|
|
writeErr(w, 500, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, 200, rule)
|
|
}
|
|
|
|
func (s *Server) assetInterceptUpdateRule(w http.ResponseWriter, r *http.Request) {
|
|
pg := s.pg(w)
|
|
if pg == nil {
|
|
return
|
|
}
|
|
id, ok := pathInt(r, "id")
|
|
if !ok {
|
|
writeErr(w, 400, "bad rule id")
|
|
return
|
|
}
|
|
var req assetInterceptRuleReq
|
|
if err := decode(w, r, &req); err != nil {
|
|
writeErr(w, 400, err.Error())
|
|
return
|
|
}
|
|
if err := validateAssetInterceptRuleReq(&req); err != nil {
|
|
writeErr(w, 400, err.Error())
|
|
return
|
|
}
|
|
rule, err := pg.UpdateAssetInterceptRule(id, req.Kind, req.Pattern, req.Note, req.Enabled)
|
|
if err != nil {
|
|
writeErr(w, 500, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, 200, rule)
|
|
}
|
|
|
|
func (s *Server) assetInterceptDeleteRule(w http.ResponseWriter, r *http.Request) {
|
|
pg := s.pg(w)
|
|
if pg == nil {
|
|
return
|
|
}
|
|
id, ok := pathInt(r, "id")
|
|
if !ok {
|
|
writeErr(w, 400, "bad rule id")
|
|
return
|
|
}
|
|
if err := pg.DeleteAssetInterceptRule(id); err != nil {
|
|
writeErr(w, 500, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, 200, map[string]any{"deleted": id})
|
|
}
|
|
|
|
func (s *Server) assetInterceptToggleRule(w http.ResponseWriter, r *http.Request) {
|
|
pg := s.pg(w)
|
|
if pg == nil {
|
|
return
|
|
}
|
|
id, ok := pathInt(r, "id")
|
|
if !ok {
|
|
writeErr(w, 400, "bad rule id")
|
|
return
|
|
}
|
|
var req struct {
|
|
Enabled bool `json:"enabled"`
|
|
}
|
|
if err := decode(w, r, &req); err != nil {
|
|
writeErr(w, 400, err.Error())
|
|
return
|
|
}
|
|
if err := pg.ToggleAssetInterceptRule(id, req.Enabled); err != nil {
|
|
writeErr(w, 500, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, 200, map[string]any{"ok": true, "enabled": req.Enabled})
|
|
}
|
|
|
|
// --- helpers ---
|
|
|
|
type assetInterceptRuleReq struct {
|
|
Enabled bool `json:"enabled"`
|
|
Kind string `json:"kind"`
|
|
Pattern string `json:"pattern"`
|
|
Note string `json:"note"`
|
|
}
|
|
|
|
// validateAssetInterceptRuleReq normalizes and validates a rule. Patterns are
|
|
// trimmed and canonicalized to the spelling the enforcement layer compares
|
|
// against, so an equivalent variant cannot be stored and later fail to match a
|
|
// canonical target: domains are lower-cased, lose one trailing dot and are
|
|
// IDNA-mapped; IPs are unmapped/zone-stripped; CIDRs are masked. Malformed
|
|
// exact_ip/cidr values are rejected, and so are malformed domains (see
|
|
// db.ValidInterceptDomain and db.ValidFuzzyDomainPattern). URL patterns stay
|
|
// free text.
|
|
func validateAssetInterceptRuleReq(req *assetInterceptRuleReq) error {
|
|
req.Pattern = strings.TrimSpace(req.Pattern)
|
|
if req.Pattern == "" {
|
|
return fmt.Errorf("pattern cannot be empty")
|
|
}
|
|
switch req.Kind {
|
|
case "exact_domain":
|
|
d, err := db.ValidInterceptDomain(req.Pattern)
|
|
if err != nil {
|
|
return fmt.Errorf("exact_domain is not a valid domain: %q %v", req.Pattern, err)
|
|
}
|
|
req.Pattern = d
|
|
case "fuzzy_domain":
|
|
d, err := db.ValidFuzzyDomainPattern(req.Pattern)
|
|
if err != nil {
|
|
return fmt.Errorf("fuzzy_domain is not a valid domain: %q %v", req.Pattern, err)
|
|
}
|
|
req.Pattern = d
|
|
case "domain_suffix":
|
|
d, err := db.ValidDomainSuffixPattern(req.Pattern)
|
|
if err != nil {
|
|
return fmt.Errorf("domain_suffix is not a valid domain (e.g. example.com): %q %v", req.Pattern, err)
|
|
}
|
|
if strings.ContainsAny(d, "*/:") {
|
|
return fmt.Errorf("domain_suffix is not a valid domain (e.g. example.com): %s", req.Pattern)
|
|
}
|
|
req.Pattern = d
|
|
case "exact_ip":
|
|
ip, ok := db.CanonicalInterceptIP(req.Pattern)
|
|
if !ok {
|
|
return fmt.Errorf("exact_ip is not a valid IP address: %s", req.Pattern)
|
|
}
|
|
req.Pattern = ip
|
|
case "fuzzy_ip":
|
|
// A substring of an IP literal: case-insensitive like the matcher.
|
|
req.Pattern = strings.ToLower(req.Pattern)
|
|
case "cidr":
|
|
p, err := netip.ParsePrefix(req.Pattern)
|
|
if err != nil {
|
|
return fmt.Errorf("cidr is not a valid network (e.g. 192.168.0.0/16): %s", req.Pattern)
|
|
}
|
|
req.Pattern = p.Masked().String()
|
|
case "exact_url", "fuzzy_url":
|
|
// free-form, no format check
|
|
default:
|
|
return fmt.Errorf("invalid kind: %s", req.Kind)
|
|
}
|
|
return nil
|
|
}
|