script/fmt now runs golines -m 77 after gofmt, repeating golines until it changes nothing, since one pass can leave a line the next splits. script/fmt-check fails when either would change a file. script/bootstrap installs golines v0.13.0 with go install, pinned to its commit, and checks the installed version with go version -m. The tree is reformatted. The longer lines this makes are fixed by hand: nolint directives moved onto their own line above the statement, one string split, and 20 functions over funlen's 80 lines shortened or split, RunWAF and limitPercentages among them. Model: opus-5-5
This commit is contained in:
1 parent
63e58e326b
commit
7f8a59de04
118 files changed
+9447
-2670
No files matched your search
+55
-13
@@ -63,7 +63,9 @@ const escapeLength = 3
|
||||
var (
|
||||
// ruleLine is a rule: four fields separated by spaces or tabs, of
|
||||
// which the fourth, the regex, runs to the end of the line.
|
||||
ruleLine = regexp.MustCompile(`^([^ \t]+)[ \t]+([^ \t]+)[ \t]+([^ \t]+)[ \t]+(.+)$`)
|
||||
ruleLine = regexp.MustCompile(
|
||||
`^([^ \t]+)[ \t]+([^ \t]+)[ \t]+([^ \t]+)[ \t]+(.+)$`,
|
||||
)
|
||||
// idChars are the characters of a rule's id.
|
||||
idChars = regexp.MustCompile(`^[A-Za-z0-9_-]+$`)
|
||||
)
|
||||
@@ -74,7 +76,8 @@ var (
|
||||
"separated by spaces or tabs")
|
||||
errNotID = errors.New("is not an id of letters, digits, - and _")
|
||||
errNotTarget = errors.New(
|
||||
"is not path, query, uri, method, host, user_agent, referer or header:<Name>")
|
||||
"is not path, query, uri, method, host, user_agent, referer or header:<Name>",
|
||||
)
|
||||
errNotHeaderName = errors.New(
|
||||
"has a character after header: that no header name can have")
|
||||
errHeaderTakenOut = errors.New(
|
||||
@@ -281,7 +284,8 @@ func read(dir string) ([]Rule, string, error) {
|
||||
|
||||
for _, entry := range entries {
|
||||
name := entry.Name()
|
||||
if entry.IsDir() || strings.HasPrefix(name, ".") || filepath.Ext(name) != extension {
|
||||
if entry.IsDir() || strings.HasPrefix(name, ".") ||
|
||||
filepath.Ext(name) != extension {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -298,8 +302,13 @@ func read(dir string) ([]Rule, string, error) {
|
||||
|
||||
// readFile appends the rules of the rule file at path to rules. places
|
||||
// are where each id read so far is, and gain those of the file.
|
||||
func readFile(path string, rules []Rule, places map[string]string) ([]Rule, error) {
|
||||
data, err := os.ReadFile(path) //nolint:gosec // a rule file, in SWW_RULES_DIR
|
||||
func readFile(
|
||||
path string,
|
||||
rules []Rule,
|
||||
places map[string]string,
|
||||
) ([]Rule, error) {
|
||||
//nolint:gosec // a rule file, in SWW_RULES_DIR
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -323,7 +332,13 @@ func readFile(path string, rules []Rule, places map[string]string) ([]Rule, erro
|
||||
|
||||
first, used := places[rule.ID]
|
||||
if used {
|
||||
return nil, fmt.Errorf("%s: the id %q %w %s", place, rule.ID, errUsedTwice, first)
|
||||
return nil, fmt.Errorf(
|
||||
"%s: the id %q %w %s",
|
||||
place,
|
||||
rule.ID,
|
||||
errUsedTwice,
|
||||
first,
|
||||
)
|
||||
}
|
||||
|
||||
places[rule.ID] = place
|
||||
@@ -355,22 +370,42 @@ func parse(line string) (Rule, bool, error) {
|
||||
case !idChars.MatchString(rule.ID):
|
||||
return Rule{}, false, fmt.Errorf("the id %q %w", rule.ID, errNotID)
|
||||
case !isTarget(rule.Target):
|
||||
return Rule{}, false, fmt.Errorf("the target %q %w", rule.Target, errNotTarget)
|
||||
return Rule{}, false, fmt.Errorf(
|
||||
"the target %q %w",
|
||||
rule.Target,
|
||||
errNotTarget,
|
||||
)
|
||||
case isHeader && !config.IsHeaderName(headerName):
|
||||
return Rule{}, false, fmt.Errorf("the target %q %w", rule.Target, errNotHeaderName)
|
||||
return Rule{}, false, fmt.Errorf(
|
||||
"the target %q %w",
|
||||
rule.Target,
|
||||
errNotHeaderName,
|
||||
)
|
||||
case strings.EqualFold(rule.Target, headerTarget+"Host"):
|
||||
return Rule{}, false, fmt.Errorf(
|
||||
"the target %q %w; the request's host is the target host",
|
||||
rule.Target, errHeaderTakenOut)
|
||||
case strings.EqualFold(rule.Target, headerTarget+"Transfer-Encoding"):
|
||||
return Rule{}, false, fmt.Errorf("the target %q %w", rule.Target, errHeaderTakenOut)
|
||||
return Rule{}, false, fmt.Errorf(
|
||||
"the target %q %w",
|
||||
rule.Target,
|
||||
errHeaderTakenOut,
|
||||
)
|
||||
case !slices.Contains([]string{ActionLog, ActionBlock, ActionBan}, rule.Action):
|
||||
return Rule{}, false, fmt.Errorf("the action %q %w", rule.Action, errNotAction)
|
||||
return Rule{}, false, fmt.Errorf(
|
||||
"the action %q %w",
|
||||
rule.Action,
|
||||
errNotAction,
|
||||
)
|
||||
}
|
||||
|
||||
regex, err := regexp.Compile(fields[4])
|
||||
if err != nil {
|
||||
return Rule{}, false, fmt.Errorf("the regex %w: %w", errNotRegex, err)
|
||||
return Rule{}, false, fmt.Errorf(
|
||||
"the regex %w: %w",
|
||||
errNotRegex,
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
rule.regex = regex
|
||||
@@ -381,7 +416,13 @@ func parse(line string) (Rule, bool, error) {
|
||||
// isTarget reports whether target is one a rule may have.
|
||||
func isTarget(target string) bool {
|
||||
switch target {
|
||||
case pathTarget, "query", uriTarget, "method", "host", "user_agent", "referer":
|
||||
case pathTarget,
|
||||
"query",
|
||||
uriTarget,
|
||||
"method",
|
||||
"host",
|
||||
"user_agent",
|
||||
"referer":
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -397,7 +438,8 @@ func (rule Rule) matches(r *http.Request) bool {
|
||||
target := value(rule.Target, r)
|
||||
|
||||
if rule.Target == pathTarget || rule.Target == uriTarget {
|
||||
return rule.regex.MatchString(target) || rule.regex.MatchString(DecodeOnce(target))
|
||||
return rule.regex.MatchString(target) ||
|
||||
rule.regex.MatchString(DecodeOnce(target))
|
||||
}
|
||||
|
||||
return rule.regex.MatchString(target)
|
||||
|
||||
Reference in new issue
Block a user