diff --git a/SPEC.md b/SPEC.md index 5849f66..1afc9f3 100644 --- a/SPEC.md +++ b/SPEC.md @@ -817,8 +817,8 @@ The supported apps are: - `vaultwarden`; - `wordpress`. -Each app's fixes are in one file, such as `internal/coreruleset/gitea.conf`, and -one entry of `apps` in `internal/coreruleset/coreruleset.go`. gitea's fixes are: +Each app's fixes are in one file named for the app, such as +`internal/coreruleset/apps/gitea.conf`. gitea's fixes are: 1. Ten parameters are not checked against the Core Rule Set's lists of system files (930120), shell paths (932160) and command names (932260), on any path: @@ -851,8 +851,8 @@ one entry of `apps` in `internal/coreruleset/coreruleset.go`. gitea's fixes are: 3. On gitea's paths, some fields skip the rules that refused ordinary gitea requests there. They are the fields where gitea sends a search, a file's name or text, a commit message, a branch name or a repository's name. - `internal/coreruleset/gitea.conf` lists each rule and the request it refused. - The fields are: + `internal/coreruleset/apps/gitea.conf` lists each rule and the request it + refused. The fields are: - `q` on gitea's searches of code, of issues and pull requests, and of commits. A code search holds code, such as `` and `javascript:alert(1)` are still refused. @@ -1600,7 +1600,7 @@ Elsewhere these fields keep every rule. Nextcloud's fixes: on Nextcloud's paths, some fields skip the rules that refused ordinary Nextcloud requests there, and its WebDAV methods are allowed. -`internal/coreruleset/nextcloud.conf` lists each rule and the request it +`internal/coreruleset/apps/nextcloud.conf` lists each rule and the request it refused. The sync client and the Files app use WebDAV on these paths: @@ -1610,7 +1610,8 @@ The sync client and the Files app use WebDAV on these paths: - `/remote.php/webdav/`; - `/public.php/webdav/`. -These methods are allowed there, besides those of the first change: +These methods are allowed there, besides `PUT`, `PATCH` and `DELETE`, which +every app gets: - `PROPFIND` and `PROPPATCH`; - `MKCOL`; @@ -1754,7 +1755,7 @@ gets gitea's fixes too. Forgejo's own fixes hold only what Forgejo sends and gitea does not. On Forgejo's paths, some fields skip the rules that refused ordinary Forgejo -requests there. `internal/coreruleset/forgejo.conf` lists each rule and the +requests there. `internal/coreruleset/apps/forgejo.conf` lists each rule and the request it refused. `q` skips rules on some of Forgejo's searches. The searches are: @@ -1800,10 +1801,10 @@ The Core Rule Set reads the request without the `flash` cookie, in which Forgejo puts what gitea puts in `gitea_flash` (gitea's second fix). Ghost's fixes: on Ghost's paths, some fields skip the rules that refused -ordinary Ghost requests there. `internal/coreruleset/ghost.conf` lists each rule -and the request it refused. Ghost's admin and integrations call the Admin API -under `/ghost/api/admin/`. Themes and Ghost's scripts call the Content API under -`/ghost/api/content/`, and members call `/members/api/`. The admin calls +ordinary Ghost requests there. `internal/coreruleset/apps/ghost.conf` lists each +rule and the request it refused. Ghost's admin and integrations call the Admin +API under `/ghost/api/admin/`. Themes and Ghost's scripts call the Content API +under `/ghost/api/content/`, and members call `/members/api/`. The admin calls ActivityPub, a service of its own, under `/.ghost/activitypub/`. Eight rules refused words that name commands and files, such as `python3`, @@ -1941,9 +1942,9 @@ That name holds `.profile`, a file on the list of system files, so the name skips 930120 when a profile is saved. The passwords skip each rule that random passwords set off. The numbers are in -`internal/coreruleset/ghost.conf`. They are `password` when signing in, and the -passwords sent when creating the first staff user, accepting an invitation, and -resetting or changing a password. `' OR '1'='1`, `../../etc/passwd`, +`internal/coreruleset/apps/ghost.conf`. They are `password` when signing in, and +the passwords sent when creating the first staff user, accepting an invitation, +and resetting or changing a password. `' OR '1'='1`, `../../etc/passwd`, `; cat /etc/passwd` and `` sent as a password reach Ghost; `` and `javascript:alert(1)` are still refused. diff --git a/internal/coreruleset/drupal.conf b/internal/coreruleset/apps/drupal.conf similarity index 100% rename from internal/coreruleset/drupal.conf rename to internal/coreruleset/apps/drupal.conf diff --git a/internal/coreruleset/forgejo.conf b/internal/coreruleset/apps/forgejo.conf similarity index 100% rename from internal/coreruleset/forgejo.conf rename to internal/coreruleset/apps/forgejo.conf diff --git a/internal/coreruleset/ghost.conf b/internal/coreruleset/apps/ghost.conf similarity index 100% rename from internal/coreruleset/ghost.conf rename to internal/coreruleset/apps/ghost.conf diff --git a/internal/coreruleset/gitea.conf b/internal/coreruleset/apps/gitea.conf similarity index 100% rename from internal/coreruleset/gitea.conf rename to internal/coreruleset/apps/gitea.conf diff --git a/internal/coreruleset/mastodon.conf b/internal/coreruleset/apps/mastodon.conf similarity index 100% rename from internal/coreruleset/mastodon.conf rename to internal/coreruleset/apps/mastodon.conf diff --git a/internal/coreruleset/mediawiki.conf b/internal/coreruleset/apps/mediawiki.conf similarity index 100% rename from internal/coreruleset/mediawiki.conf rename to internal/coreruleset/apps/mediawiki.conf diff --git a/internal/coreruleset/nextcloud.conf b/internal/coreruleset/apps/nextcloud.conf similarity index 99% rename from internal/coreruleset/nextcloud.conf rename to internal/coreruleset/apps/nextcloud.conf index f5c3737..f8080fe 100644 --- a/internal/coreruleset/nextcloud.conf +++ b/internal/coreruleset/apps/nextcloud.conf @@ -37,9 +37,10 @@ SecRule &TX:app_nextcloud "@eq 0" \ "id:900090,phase:1,pass,nolog,skipAfter:END_NEXTCLOUD" -# The sync client and the Files app use these WebDAV methods besides those -# the first change allows, on /remote.php/dav/ and /public.php/dav/ and on the -# older /remote.php/webdav/ and /public.php/webdav/. 911100 refused each: +# The sync client and the Files app use these WebDAV methods besides PUT, +# PATCH and DELETE, which sww allows for every app, on /remote.php/dav/ and +# /public.php/dav/ and on the older /remote.php/webdav/ and /public.php/webdav/. +# 911100 refused each: # PROPFIND, PROPPATCH: listing a folder, marking a file a favourite # MKCOL: making a folder, and starting a chunked upload # COPY, MOVE: copying, moving and renaming, and ending a chunked upload diff --git a/internal/coreruleset/vaultwarden.conf b/internal/coreruleset/apps/vaultwarden.conf similarity index 100% rename from internal/coreruleset/vaultwarden.conf rename to internal/coreruleset/apps/vaultwarden.conf diff --git a/internal/coreruleset/wordpress.conf b/internal/coreruleset/apps/wordpress.conf similarity index 100% rename from internal/coreruleset/wordpress.conf rename to internal/coreruleset/apps/wordpress.conf diff --git a/internal/coreruleset/coreruleset.go b/internal/coreruleset/coreruleset.go index 83ae46a..603537f 100644 --- a/internal/coreruleset/coreruleset.go +++ b/internal/coreruleset/coreruleset.go @@ -12,11 +12,10 @@ package coreruleset import ( - _ "embed" + "embed" "errors" "fmt" "io" - "maps" "net" "net/http" "net/netip" @@ -35,7 +34,7 @@ import ( // directives are the Core Rule Set as sww runs it, with the // paranoia level for %d, bodyDirectives for the first %s while // SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is set, the exclusions file for -// the second, and the directives of every app in apps for the third. +// the second, and the fixes of every app in appFixes for the third. // Each rule sww adds has an id from 900000 to 900999, the ids the // Core Rule Set keeps for the rules that set it up, which // SWW_WAF_CORE_RULE_SET_DISABLED_RULES and the exclusions file refuse, so that no @@ -54,13 +53,12 @@ Include @crs-setup.conf.example SecAction "id:900000,phase:1,pass,nolog,\ setvar:tx.blocking_paranoia_level=%d" -# The first change: PUT, PATCH and DELETE are allowed besides GET, HEAD, -# POST and OPTIONS. +# PUT, PATCH and DELETE are allowed besides GET, HEAD, POST and OPTIONS. SecAction "id:900200,phase:1,pass,nolog,\ setvar:'tx.allowed_methods=GET HEAD POST OPTIONS PUT PATCH DELETE'" -# The second: Expect and Content-Encoding are taken off the Core Rule Set's -# list of the headers it refuses. Content-Encoding goes back on it for a +# Expect and Content-Encoding are taken off the Core Rule Set's list of the +# headers it refuses. Content-Encoding goes back on it for a # body the Core Rule Set reads (900260 in bodyDirectives). SecAction "id:900250,phase:1,pass,nolog,\ setvar:'tx.restricted_headers_basic=/proxy/ /lock-token/ /content-range/ \ @@ -86,18 +84,17 @@ SecRule ARGUMENTS_LIMIT_REACHED "@eq 1" "id:900300,phase:2,pass,\ # The apps' fixes come before the rules for the same reason. %s -# The fifth: only the rules for requests are loaded, and no response is -# inspected. +# Only the rules for requests are loaded, and no response is inspected. Include @owasp_crs/REQUEST-*.conf -# The third: redirect_uri is not checked for a URL naming an IP address or +# redirect_uri is not checked for a URL naming an IP address or # localhost. Coraza matches a parameter name here without regard to case. # ARGS holds the fields of a form data or multipart body Coraza reads as # well as the query parameters, so a field of this name is left out too. SecRuleUpdateTargetById 931100 "!ARGS:redirect_uri" SecRuleUpdateTargetById 934110 "!ARGS:redirect_uri" -# The fourth: Referer is not checked for a Unix command without arguments +# Referer is not checked for a Unix command without arguments # or for Java starting a process. SecRuleUpdateTargetById 932340 "!REQUEST_HEADERS:Referer" SecRuleUpdateTargetById 944110 "!REQUEST_HEADERS:Referer" @@ -122,9 +119,9 @@ SecRule REQUEST_HEADERS:Content-Type \ "@rx ^(?:application|text)/(?:[a-z0-9.-]+[+])?json" \ "id:900420,phase:1,pass,nolog,t:none,t:lowercase,ctl:requestBodyProcessor=JSON" -# The rest of the second change: Content-Encoding is refused again on a -# body of a kind the Core Rule Set reads, since a compressed body cannot be -# inspected. +# Content-Encoding goes back on the list of the headers the Core Rule Set +# refuses (900250 in directives) for a body of a kind it reads, since a +# compressed body cannot be inspected. SecRule REQBODY_PROCESSOR "@rx ^(?:URLENCODED|MULTIPART|JSON|XML)$" \ "id:900260,phase:1,pass,nolog,\ setvar:'tx.restricted_headers_basic=%%{tx.restricted_headers_basic} \ @@ -145,15 +142,19 @@ SecRule MULTIPART_STRICT_ERROR "!@eq 0" "id:900450,phase:2,pass,\ severity:'CRITICAL',setvar:'tx.inbound_anomaly_score_pl1=+5'" ` -// app is an app whose ordinary requests the Core Rule Set would refuse, -// with the fixes that let them pass. +// appFixes holds the fixes of each app whose ordinary requests the Core +// Rule Set would refuse, in a file named for the app by the name +// SWW_WAF_APPS gives it, such as apps/gitea.conf. The fixes are Coraza +// exclusions, loaded before the Core Rule Set's rules. Their first rule +// skips to the marker they end with, unless Inspect sets the transaction +// variable named for the app, such as app_gitea. The skip reaches only +// phase 1, so a rule of a later phase must check that variable itself. +// +//go:embed apps/*.conf +var appFixes embed.FS + +// app is what an app's file in appFixes cannot say about it. type app struct { - // directives are Coraza exclusions, loaded before the Core Rule Set's - // rules. Their first rule skips to the marker they end with, unless - // Inspect sets the transaction variable named for the app, such as - // app_gitea. The skip reaches only phase 1, so a rule of a later phase - // must check that variable itself. - directives string // cookies are the cookies the Core Rule Set reads a request without. cookies []string // forkOf names the app this one is a fork of, "" for none. A hostname @@ -161,60 +162,34 @@ type app struct { forkOf string } -//go:embed gitea.conf -var giteaDirectives string - -//go:embed wordpress.conf -var wordpressDirectives string - -//go:embed drupal.conf -var drupalDirectives string - -//go:embed mediawiki.conf -var mediawikiDirectives string - -//go:embed mastodon.conf -var mastodonDirectives string - -//go:embed vaultwarden.conf -var vaultwardenDirectives string - -//go:embed nextcloud.conf -var nextcloudDirectives string - -//go:embed forgejo.conf -var forgejoDirectives string - -//go:embed ghost.conf -var ghostDirectives string - -// apps are the apps whose fixes sww has, by the name SWW_WAF_APPS gives each. -// flash is Forgejo's name for gitea_flash. +// apps are the apps that need more than their file in appFixes, by the +// name SWW_WAF_APPS gives each. flash is Forgejo's name for gitea_flash. // //nolint:gochecknoglobals // a constant cannot be a map var apps = map[string]app{ - "drupal": {directives: drupalDirectives}, "forgejo": { - directives: forgejoDirectives, - cookies: []string{"flash"}, - forkOf: "gitea", + cookies: []string{"flash"}, + forkOf: "gitea", }, - "ghost": {directives: ghostDirectives}, "gitea": { - directives: giteaDirectives, - cookies: []string{"gitea_flash", "redirect_to"}, + cookies: []string{"gitea_flash", "redirect_to"}, }, - "mastodon": {directives: mastodonDirectives}, - "mediawiki": {directives: mediawikiDirectives}, - "nextcloud": {directives: nextcloudDirectives}, - "vaultwarden": {directives: vaultwardenDirectives}, - "wordpress": {directives: wordpressDirectives}, } // AppNames returns the names of the apps whose fixes sww has, in // alphabetical order. func AppNames() []string { - return slices.Sorted(maps.Keys(apps)) + // An embedded directory always reads. + files, _ := appFixes.ReadDir("apps") + + names := make([]string, len(files)) + for i, file := range files { + names[i] = strings.TrimSuffix(file.Name(), ".conf") + } + + slices.Sort(names) + + return names } // Params are what New needs. @@ -373,8 +348,11 @@ func New(params Params) (*CoreRuleSet, error) { } var fixes strings.Builder + for _, name := range AppNames() { - fixes.WriteString(apps[name].directives + "\n") + // An embedded file always reads. + file, _ := appFixes.ReadFile("apps/" + name + ".conf") + fixes.WriteString(string(file) + "\n") } text := fmt.Sprintf(directives, params.ParanoiaLevel, body, exclusions, diff --git a/internal/coreruleset/gitea_test.go b/internal/coreruleset/gitea_test.go index 406f3dd..d281827 100644 --- a/internal/coreruleset/gitea_test.go +++ b/internal/coreruleset/gitea_test.go @@ -17,7 +17,7 @@ const ( ) // giteaSearches are searches of the pages gitea and Forgejo share whose q -// skips the lists of system files and command names, in the eighth change. +// skips the lists of system files and command names, in gitea's fixes. // gitea serves its admin pages under /-/admin/ and Forgejo under /admin/. // //nolint:gochecknoglobals // a constant cannot be a list @@ -222,8 +222,8 @@ func TestGiteaSearchExclusionsAreNarrow(t *testing.T) { } // Under /admin/, where Forgejo serves its admin pages, only the hard - // values are left out: the tenth change lets them through in q on every - // path there, for Drupal. + // values are left out: Drupal's fixes let them through in q on every path + // there. for _, target := range []string{ "/admin/packages/delete?q=", "/admin/packages?other=",