Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1f1e97cb4e |
No files matched your search
@@ -1,12 +1,11 @@
|
|||||||
.PHONY: bootstrap setup test lint fmt fmt-check tidy check docker publish \
|
.PHONY: bootstrap setup test lint fmt fmt-check tidy check docker hooks build \
|
||||||
hooks build run example-app
|
run example-app
|
||||||
|
|
||||||
# Makefile targets are thin shims; the implementations live in script/
|
# Makefile targets are thin shims; the implementations live in script/
|
||||||
# per the scripts-to-rule-them-all pattern (see the Entrypoints section
|
# per the scripts-to-rule-them-all pattern (see the Entrypoints section
|
||||||
# of README.md). tidy writes go.mod and go.sum as `go mod tidy` does,
|
# of README.md). tidy writes go.mod and go.sum as `go mod tidy` does,
|
||||||
# which test checks. build and run are for working on the code by hand;
|
# which test checks. build and run are for working on the code by hand;
|
||||||
# example-app checks the image with an app built on it; publish builds
|
# example-app checks the image with an app built on it.
|
||||||
# the image on the Docker host and pushes it to the registry.
|
|
||||||
|
|
||||||
bootstrap:
|
bootstrap:
|
||||||
@script/bootstrap
|
@script/bootstrap
|
||||||
@@ -35,9 +34,6 @@ check:
|
|||||||
docker:
|
docker:
|
||||||
@script/docker
|
@script/docker
|
||||||
|
|
||||||
publish:
|
|
||||||
@script/publish
|
|
||||||
|
|
||||||
hooks:
|
hooks:
|
||||||
@script/install-precommit
|
@script/install-precommit
|
||||||
|
|
||||||
|
|||||||
@@ -68,9 +68,8 @@ Not built, and not planned:
|
|||||||
|
|
||||||
### The image
|
### The image
|
||||||
|
|
||||||
The image is published at `git.eeqj.de/sneak/smallwebwaf`. You can also build it
|
No image is published. Build it from a clone. `make docker` runs the tests and
|
||||||
from a clone. `make docker` runs the tests and the linter, then builds the
|
the linter, then builds the image, tagged `smallwebwaf`.
|
||||||
image, tagged `smallwebwaf`.
|
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone https://git.eeqj.de/sneak/smallwebwaf.git
|
git clone https://git.eeqj.de/sneak/smallwebwaf.git
|
||||||
@@ -217,7 +216,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. |
|
||||||
@@ -259,7 +258,7 @@ Every setting the code reads, with its default:
|
|||||||
| `SWW_ADD_LOOKUP_HEADERS` | `false` | `true` passes the app the client's AS number and country in `X-Client-ASN` and `X-Client-Country`. |
|
| `SWW_ADD_LOOKUP_HEADERS` | `false` | `true` passes the app the client's AS number and country in `X-Client-ASN` and `X-Client-Country`. |
|
||||||
| `SWW_EXCLUSIVELY_ALLOWED_COUNTRIES` | empty | When set, the only countries whose clients get through; a client the lookup cannot place is refused too. |
|
| `SWW_EXCLUSIVELY_ALLOWED_COUNTRIES` | empty | When set, the only countries whose clients get through; a client the lookup cannot place is refused too. |
|
||||||
| `SWW_TAG_LISTS` | empty | Address lists that each add a tag to their clients' requests, as `name=URL` pairs, such as `torexit=https://example.com/tor.txt`. |
|
| `SWW_TAG_LISTS` | empty | Address lists that each add a tag to their clients' requests, as `name=URL` pairs, such as `torexit=https://example.com/tor.txt`. |
|
||||||
| `SWW_TAG_DENY` | empty | Tags whose requests are refused, such as `torexit,country=CN`. |
|
| `SWW_TAG_DENY` | empty | Tags whose requests are refused, such as `torexit,country=cn`. |
|
||||||
| `SWW_TAG_LIMIT_PERCENT` | empty | Tags whose clients get a percentage, from 1 to 99, of every request and byte limit, such as `vpn:50,asn=14061:25`; the lowest applies. |
|
| `SWW_TAG_LIMIT_PERCENT` | empty | Tags whose clients get a percentage, from 1 to 99, of every request and byte limit, such as `vpn:50,asn=14061:25`; the lowest applies. |
|
||||||
| `SWW_ASN_BYTES_PERCENT` | empty | A percentage of the byte limits alone for the AS numbers listed, in place of their `asn=` tag's percentage. |
|
| `SWW_ASN_BYTES_PERCENT` | empty | A percentage of the byte limits alone for the AS numbers listed, in place of their `asn=` tag's percentage. |
|
||||||
| `SWW_COUNTRY_BYTES_PERCENT` | empty | A percentage of the byte limits alone for the countries listed, in place of their `country=` tag's percentage. |
|
| `SWW_COUNTRY_BYTES_PERCENT` | empty | A percentage of the byte limits alone for the countries listed, in place of their `country=` tag's percentage. |
|
||||||
@@ -275,7 +274,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, such as `git.example.com:gitea;cloud.example.com:gitea`; 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`. |
|
||||||
@@ -415,7 +413,7 @@ services:
|
|||||||
SWW_RATE_LIMIT_PER_HOUR: "600"
|
SWW_RATE_LIMIT_PER_HOUR: "600"
|
||||||
SWW_RATE_LIMIT_PER_DAY: "3000"
|
SWW_RATE_LIMIT_PER_DAY: "3000"
|
||||||
# Hosting networks and one country get a share of those limits.
|
# Hosting networks and one country get a share of those limits.
|
||||||
SWW_TAG_LIMIT_PERCENT: asn=14061:50,asn=16276:25,country=CN:25
|
SWW_TAG_LIMIT_PERCENT: asn=14061:50,asn=16276:25,country=cn:25
|
||||||
# Paths only scanners ask for; one request bans the client.
|
# Paths only scanners ask for; one request bans the client.
|
||||||
SWW_TRAP_PATHS: /wp-login.php,/xmlrpc.php
|
SWW_TRAP_PATHS: /wp-login.php,/xmlrpc.php
|
||||||
# Answer a refused client with 429 rather than 403.
|
# Answer a refused client with 429 rather than 403.
|
||||||
@@ -507,10 +505,10 @@ of the request reaches the app.
|
|||||||
|
|
||||||
`discriminator` takes the checks:
|
`discriminator` takes the checks:
|
||||||
|
|
||||||
3. The tag lists.
|
3. The static lists, `SWW_ALLOW_NETS` and `SWW_DENY_NETS`.
|
||||||
4. The static lists, `SWW_ALLOW_NETS` and `SWW_DENY_NETS`.
|
4. The bans.
|
||||||
5. The bans.
|
5. The lookup of the client's AS number and country.
|
||||||
6. The lookup of the client's AS number and country.
|
6. The tag lists.
|
||||||
7. The country list.
|
7. The country list.
|
||||||
8. The reputation sources.
|
8. The reputation sources.
|
||||||
9. The tag refusals of `SWW_TAG_DENY`.
|
9. The tag refusals of `SWW_TAG_DENY`.
|
||||||
@@ -574,15 +572,11 @@ 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 eight ways so that it lets ordinary
|
||||||
requests to a code forge through, such as git over HTTP and sign-in through
|
requests to a code forge through, such as git over HTTP and sign-in through
|
||||||
Git Credential Manager. No setting undoes the changes; "Attack detection" in
|
Git Credential Manager. On gitea's own paths, its searches, file names and
|
||||||
[`SPEC.md`](SPEC.md) lists them.
|
branch names get through too. No setting undoes the changes; "Attack
|
||||||
- Each supported app also has its own fixes, which let its ordinary requests
|
detection" in [`SPEC.md`](SPEC.md) lists them.
|
||||||
through. gitea's let its searches, file names and branch names through.
|
|
||||||
`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
|
||||||
@@ -688,15 +682,6 @@ on the host.
|
|||||||
`make check`.
|
`make check`.
|
||||||
- `script/docker`: builds the image, whose build runs the tests and the linter
|
- `script/docker`: builds the image, whose build runs the tests and the linter
|
||||||
first. `make docker`.
|
first. `make docker`.
|
||||||
- `script/publish`: runs `script/docker`, whose build happens on the Docker host
|
|
||||||
named by `DOCKER_HOST` (`ssh://root@fsn1app1` unless set). It then pushes the
|
|
||||||
image to `PUBLISH_IMAGE` (`git.eeqj.de/sneak/smallwebwaf` unless set). It
|
|
||||||
refuses a working tree with uncommitted changes. `make publish`. It pushes
|
|
||||||
four tags:
|
|
||||||
- the UTC date as `YYYY-MM-DD`;
|
|
||||||
- the short commit hash;
|
|
||||||
- the branch name;
|
|
||||||
- `latest`.
|
|
||||||
- `script/cibuild`: what CI runs: `script/bootstrap`, `script/check`, then the
|
- `script/cibuild`: what CI runs: `script/bootstrap`, `script/check`, then the
|
||||||
image build.
|
image build.
|
||||||
- `script/precommit`: run by the git pre-commit hook; runs `script/check`.
|
- `script/precommit`: run by the git pre-commit hook; runs `script/check`.
|
||||||
|
|||||||
@@ -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/waf`: the Core Rule Set, with the six changes and the apps'
|
- `internal/waf`: the Core Rule Set, with the eight changes listed under
|
||||||
fixes listed under "Attack detection", as Coraza directives.
|
"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
|
||||||
@@ -143,13 +143,11 @@ request log's `heuristic` field gives that name. `refusal_limit` and
|
|||||||
- An IPv6 client is its group: the netblock of `SWW_IPV6_GROUP_PREFIX`
|
- An IPv6 client is its group: the netblock of `SWW_IPV6_GROUP_PREFIX`
|
||||||
around its address, a /64 by default, since one abuser usually controls a
|
around its address, a /64 by default, since one abuser usually controls a
|
||||||
whole /64. A client is therefore one IPv4 address or one IPv6 group.
|
whole /64. A client is therefore one IPv4 address or one IPv6 group.
|
||||||
3. The tag lists (`tag_lists`; see "Request tags"). Each list that holds the
|
3. The static lists. A client in `SWW_ALLOW_NETS` skips steps 4 to 15, the byte
|
||||||
client adds its tag. A client in `SWW_ALLOW_NETS` gets no tag.
|
|
||||||
4. The static lists. A client in `SWW_ALLOW_NETS` skips steps 5 to 15, the byte
|
|
||||||
limits and the error burst. A client in `SWW_DENY_NETS` is refused
|
limits and the error burst. A client in `SWW_DENY_NETS` is refused
|
||||||
(`deny_nets`); a client in both lists is let through.
|
(`deny_nets`); a client in both lists is let through.
|
||||||
5. Bans. A client under a ban is refused (`active_ban`; see "Bans").
|
4. Bans. A client under a ban is refused (`active_ban`; see "Bans").
|
||||||
6. The lookup, unless `SWW_LOOKUP_SOURCE` is `off` (see "Lookup of AS number and
|
5. The lookup, unless `SWW_LOOKUP_SOURCE` is `off` (see "Lookup of AS number and
|
||||||
country"). The request goes on at once. It waits for the first GeoJS answer,
|
country"). The request goes on at once. It waits for the first GeoJS answer,
|
||||||
up to `SWW_LOOKUP_TIMEOUT`, only when one of these needs it:
|
up to `SWW_LOOKUP_TIMEOUT`, only when one of these needs it:
|
||||||
- the country list;
|
- the country list;
|
||||||
@@ -160,6 +158,8 @@ request log's `heuristic` field gives that name. `refusal_limit` and
|
|||||||
|
|
||||||
A request that ends before the answer comes is logged without it.
|
A request that ends before the answer comes is logged without it.
|
||||||
|
|
||||||
|
6. The tag lists (`tag_lists`; see "Request tags"). Each list that holds the
|
||||||
|
client adds its tag.
|
||||||
7. The country list (`countries`; see "Country list").
|
7. The country list (`countries`; see "Country list").
|
||||||
8. The reputation sources (see "Reputation"), in this order:
|
8. The reputation sources (see "Reputation"), in this order:
|
||||||
- A client on a fetched blocklist gets `SWW_BLOCKLIST_ACTION`
|
- A client on a fetched blocklist gets `SWW_BLOCKLIST_ACTION`
|
||||||
@@ -401,7 +401,7 @@ Conventions:
|
|||||||
|
|
||||||
### Static lists
|
### Static lists
|
||||||
|
|
||||||
- `SWW_ALLOW_NETS` skips the checks step 4 names, for monitoring or the owner's
|
- `SWW_ALLOW_NETS` skips the checks step 3 names, for monitoring or the owner's
|
||||||
own networks. `SWW_RATE_LIMIT_EXEMPT_NETS` bypasses the request and byte
|
own networks. `SWW_RATE_LIMIT_EXEMPT_NETS` bypasses the request and byte
|
||||||
limits only; the error burst, attack detection and bans still apply.
|
limits only; the error burst, attack detection and bans still apply.
|
||||||
- Many people can share one address, as in an office, on a mobile carrier or
|
- Many people can share one address, as in an office, on a mobile carrier or
|
||||||
@@ -480,7 +480,7 @@ service that keeps its visitors' addresses on its own host.
|
|||||||
dropped from that table. The answer reaches the client's history and ban notes
|
dropped from that table. The answer reaches the client's history and ban notes
|
||||||
when it comes.
|
when it comes.
|
||||||
- `SWW_LOOKUP_TIMEOUT` is how long a visitor's request waits for its client's
|
- `SWW_LOOKUP_TIMEOUT` is how long a visitor's request waits for its client's
|
||||||
first answer, when a setting needs it (see step 6). It also bounds each
|
first answer, when a setting needs it (see step 5). It also bounds each
|
||||||
request to GeoJS. A client whose answer has not come in time counts as unknown
|
request to GeoJS. A client whose answer has not come in time counts as unknown
|
||||||
until it comes, and its later requests do not wait. Its address is asked about
|
until it comes, and its later requests do not wait. Its address is asked about
|
||||||
again in the background.
|
again in the background.
|
||||||
@@ -534,10 +534,8 @@ the log line as `tags`, sorted, with empty lists left out (see
|
|||||||
|
|
||||||
Each tag and where it comes from:
|
Each tag and where it comes from:
|
||||||
|
|
||||||
- `asn=<number>` and `country=<code>`, such as `asn=64496` and `country=DE`,
|
- `asn=<number>` and `country=<code>`, in lower case, such as `asn=64496` and
|
||||||
from the lookup. A client the lookup cannot place has neither. A request
|
`country=de`, from the lookup. A client the lookup cannot place has neither.
|
||||||
refused by `deny_nets` or `active_ban` has neither, because those checks come
|
|
||||||
before the lookup.
|
|
||||||
- One tag per list of `SWW_TAG_LISTS` that holds the client (`tag_lists`), the
|
- One tag per list of `SWW_TAG_LISTS` that holds the client (`tag_lists`), the
|
||||||
tag being the list's name, such as `torexit` or `vpn=mullvad`. These lists are
|
tag being the list's name, such as `torexit` or `vpn=mullvad`. These lists are
|
||||||
fetched and kept like a blocklist. `discriminator` ships none of its own.
|
fetched and kept like a blocklist. `discriminator` ships none of its own.
|
||||||
@@ -548,9 +546,8 @@ Each tag and where it comes from:
|
|||||||
For the Core Rule Set a match is a score at or over
|
For the Core Rule Set a match is a score at or over
|
||||||
`SWW_WAF_ANOMALY_THRESHOLD`.
|
`SWW_WAF_ANOMALY_THRESHOLD`.
|
||||||
|
|
||||||
A client in `SWW_ALLOW_NETS` skips the checks and gets no tag. Every tag is in
|
A client in `SWW_ALLOW_NETS` skips the checks and gets no tag. `SWW_TAG_DENY`
|
||||||
lower case, except a country code, which is in capitals. `SWW_TAG_DENY` and
|
and `SWW_TAG_LIMIT_PERCENT` are read case-insensitively.
|
||||||
`SWW_TAG_LIMIT_PERCENT` accept tags in either case.
|
|
||||||
|
|
||||||
Two settings act on tags:
|
Two settings act on tags:
|
||||||
|
|
||||||
@@ -583,16 +580,16 @@ abuse comes from.
|
|||||||
`SWW_ASN_LIMIT_PERCENT_URL`; for a country, its `country=` tag's percentage.
|
`SWW_ASN_LIMIT_PERCENT_URL`; for a country, its `country=` tag's percentage.
|
||||||
- `SWW_ASN_LIMIT_PERCENT_URL` names a text file of `AS:percent` lines, so one
|
- `SWW_ASN_LIMIT_PERCENT_URL` names a text file of `AS:percent` lines, so one
|
||||||
abuse-source list can be shared by every `discriminator` in the fleet. It is
|
abuse-source list can be shared by every `discriminator` in the fleet. It is
|
||||||
fetched and kept like a blocklist (see "Reputation"). Each entry counts like
|
fetched and kept like a blocklist (see "Reputation"). Each entry counts as an
|
||||||
an `asn=` tag in `SWW_TAG_LIMIT_PERCENT`. Where both give a percentage, the
|
`asn=` tag in `SWW_TAG_LIMIT_PERCENT` does. Where both give a percentage, the
|
||||||
lower applies.
|
lower applies.
|
||||||
- A client's limit percentage is the lowest of those that apply to it: its AS
|
- A client's limit percentage is the lowest of those that apply to it: its AS
|
||||||
number's, its country's, its tags', or a reputation hit's `limit:<percent>`. A
|
number's, its country's, or a reputation hit's `limit:<percent>`. A client
|
||||||
client without a country gets `SWW_UNKNOWN_LIMIT_PERCENT` in place of a
|
without a country gets `SWW_UNKNOWN_LIMIT_PERCENT` in place of a country's.
|
||||||
country's. The percentage is 100 if none applies. Clients given a lower
|
The percentage is 100 if none applies. Clients given a lower percentage are
|
||||||
percentage are banned after fewer requests, first for a while and then, for
|
banned after fewer requests, first for a while and then, for repeated abuse,
|
||||||
repeated abuse, permanently. The log line of each counted request, and the
|
permanently. The log line of each counted request, and the notes of each ban,
|
||||||
notes of each ban, say which setting lowered the limit.
|
say which setting lowered the limit.
|
||||||
- No budget is shared by a whole country or AS number. One abuser could use it
|
- No budget is shared by a whole country or AS number. One abuser could use it
|
||||||
up and lock out everyone else there: a denial of service nobody chose, and the
|
up and lock out everyone else there: a denial of service nobody chose, and the
|
||||||
one this tool exists to prevent. Refusing a whole country is left to the
|
one this tool exists to prevent. Refusing a whole country is left to the
|
||||||
@@ -638,7 +635,7 @@ 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; gitea's fixes below hold gitea's.
|
holds an app's own exclusions; the eighth change below holds gitea's.
|
||||||
- `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.
|
||||||
@@ -684,7 +681,7 @@ 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 eight ways, since in front of
|
||||||
gitea it would otherwise refuse ordinary requests. The changes hold in front of
|
gitea it would otherwise refuse ordinary requests. The changes hold in front of
|
||||||
every app, and no setting undoes them:
|
every app, and no setting undoes them:
|
||||||
|
|
||||||
@@ -703,52 +700,8 @@ every app, and no setting undoes them:
|
|||||||
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. An app's fixes hold for the hostnames `SWW_APPS` lists the app for:
|
|
||||||
|
|
||||||
- `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;code.example.com:gitea`.
|
|
||||||
- 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.
|
|
||||||
|
|
||||||
`gitea` is the only app supported. Each app's fixes are in one file, such as
|
|
||||||
`internal/waf/gitea.conf`, and one entry of `apps` in `internal/waf/waf.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`.
|
||||||
@@ -761,18 +714,41 @@ gitea's fixes are:
|
|||||||
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, except on the
|
||||||
gitea paths the third fix names.
|
gitea paths the eighth change names. 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/waf/gitea.conf` lists each rule and the request
|
or a branch name. `internal/waf/gitea.conf` lists each rule and the request
|
||||||
it refused. The fields are:
|
it refused. The fields are:
|
||||||
@@ -790,7 +766,8 @@ 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`.
|
||||||
@@ -828,14 +805,11 @@ 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.
|
||||||
|
|
||||||
On a hostname that gets gitea's fixes, an app other than gitea must check these
|
The parameter and cookie names in these changes are matched without regard to
|
||||||
fields and cookies itself. Listing each hostname's own apps in `SWW_APPS` keeps
|
case, as Coraza matches them.
|
||||||
the full check for the others.
|
|
||||||
|
|
||||||
The parameter names in these changes and fixes are matched without regard to
|
|
||||||
case, and the cookie names exactly.
|
|
||||||
|
|
||||||
### Ban lengths, response and scope
|
### Ban lengths, response and scope
|
||||||
|
|
||||||
@@ -1291,7 +1265,7 @@ seconds, and `SWRP_DEFAULT_DISPOSITION` then decides. So at the defaults,
|
|||||||
and whatever its outside sources do.
|
and whatever its outside sources do.
|
||||||
|
|
||||||
GeoJS is the only outside source a question can wait on. A question waits only
|
GeoJS is the only outside source a question can wait on. A question waits only
|
||||||
when step 6 says so, and for at most `SWW_LOOKUP_TIMEOUT`, by default 800
|
when step 5 says so, and for at most `SWW_LOOKUP_TIMEOUT`, by default 800
|
||||||
milliseconds. A client without an answer by then counts as unknown. No question
|
milliseconds. A client without an answer by then counts as unknown. No question
|
||||||
waits on these, which run in the background:
|
waits on these, which run in the background:
|
||||||
|
|
||||||
|
|||||||
+5
-38
@@ -90,11 +90,11 @@ between the two (see "The image's command" below).
|
|||||||
## Static lists
|
## Static lists
|
||||||
|
|
||||||
- `SWW_ALLOW_NETS` (default empty): netblocks you trust, such as your monitoring
|
- `SWW_ALLOW_NETS` (default empty): netblocks you trust, such as your monitoring
|
||||||
(see step 4 in [`SPEC.md`](../SPEC.md)).
|
(see step 3 in [`SPEC.md`](../SPEC.md)).
|
||||||
- `SWW_RATE_LIMIT_EXEMPT_NETS` (default empty): netblocks whose clients are
|
- `SWW_RATE_LIMIT_EXEMPT_NETS` (default empty): netblocks whose clients are
|
||||||
exempt from rate limiting and the byte limits.
|
exempt from rate limiting and the byte limits.
|
||||||
- `SWW_DENY_NETS` (default empty): netblocks whose clients are refused (see step
|
- `SWW_DENY_NETS` (default empty): netblocks whose clients are refused (see step
|
||||||
4 in [`SPEC.md`](../SPEC.md)).
|
3 in [`SPEC.md`](../SPEC.md)).
|
||||||
|
|
||||||
## Request rate limits, per client
|
## Request rate limits, per client
|
||||||
|
|
||||||
@@ -164,7 +164,7 @@ Every request gets tags from what `discriminator` knows of its client. See
|
|||||||
So is a URL that is also in `SWW_BLOCKLIST_URLS`, `SWW_ASN_LIMIT_PERCENT_URL`,
|
So is a URL that is also in `SWW_BLOCKLIST_URLS`, `SWW_ASN_LIMIT_PERCENT_URL`,
|
||||||
or the CrowdSec decision list.
|
or the CrowdSec decision list.
|
||||||
- `SWW_TAG_DENY` (default empty): tags whose requests are refused, such as
|
- `SWW_TAG_DENY` (default empty): tags whose requests are refused, such as
|
||||||
`torexit,country=CN`. A tag listed twice, an `asn=` tag without a number, or a
|
`torexit,country=cn`. A tag listed twice, an `asn=` tag without a number, or a
|
||||||
`country=` tag without a country, is refused.
|
`country=` tag without a country, is refused.
|
||||||
- `SWW_TAG_LIMIT_PERCENT` (default empty): tags that lower a client's limits, as
|
- `SWW_TAG_LIMIT_PERCENT` (default empty): tags that lower a client's limits, as
|
||||||
tag and percentage pairs, such as `vpn:50,asn=14061:25`. A percentage is from
|
tag and percentage pairs, such as `vpn:50,asn=14061:25`. A percentage is from
|
||||||
@@ -173,21 +173,6 @@ Every request gets tags from what `discriminator` knows of its client. See
|
|||||||
`exploitsignature`: a request gets that tag after the rate limits have counted
|
`exploitsignature`: a request gets that tag after the rate limits have counted
|
||||||
it.
|
it.
|
||||||
|
|
||||||
`SWW_TAG_DENY` and `SWW_TAG_LIMIT_PERCENT` accept tags in either case. A tag
|
|
||||||
that no request can get stops the start. A request can get only these:
|
|
||||||
|
|
||||||
- a name in `SWW_TAG_LISTS`;
|
|
||||||
- an `asn=` or `country=` tag;
|
|
||||||
- a `dnsbl=` tag of a zone in `SWW_DNSBL_ZONES`, in lower case, with a key
|
|
||||||
masked as the `reputation` field shows it;
|
|
||||||
- `abuseipdb`, while `SWW_ABUSEIPDB_KEY` is set;
|
|
||||||
- `exploitsignature`, in `SWW_TAG_DENY` only, while `SWW_RULES_ENABLED` is
|
|
||||||
`true`, or while neither `SWW_WAF_MODE` nor `SWW_WAF_ANOMALY_THRESHOLD` is
|
|
||||||
`off`.
|
|
||||||
|
|
||||||
`crowdsec` stops the start in both settings, because a CrowdSec decision refuses
|
|
||||||
the request first.
|
|
||||||
|
|
||||||
## Biased thresholds
|
## Biased thresholds
|
||||||
|
|
||||||
- `SWW_ASN_BYTES_PERCENT` and `SWW_COUNTRY_BYTES_PERCENT` (default empty): a
|
- `SWW_ASN_BYTES_PERCENT` and `SWW_COUNTRY_BYTES_PERCENT` (default empty): a
|
||||||
@@ -199,8 +184,8 @@ the request first.
|
|||||||
lookup cannot place.
|
lookup cannot place.
|
||||||
- `SWW_ASN_LIMIT_PERCENT_URL` (default unset): the `http` or `https` URL of a
|
- `SWW_ASN_LIMIT_PERCENT_URL` (default unset): the `http` or `https` URL of a
|
||||||
text file with one AS number and percentage on each line, such as
|
text file with one AS number and percentage on each line, such as
|
||||||
`AS64496:50`. Each entry counts like an `asn=` tag in `SWW_TAG_LIMIT_PERCENT`.
|
`AS64496:50`. Each entry counts as an `asn=` tag in `SWW_TAG_LIMIT_PERCENT`
|
||||||
A URL that is also in `SWW_BLOCKLIST_URLS` is refused.
|
does. A URL that is also in `SWW_BLOCKLIST_URLS` is refused.
|
||||||
|
|
||||||
## Attack detection
|
## Attack detection
|
||||||
|
|
||||||
@@ -233,24 +218,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;code.example.com:gitea`.
|
|
||||||
- `gitea` is the only app supported.
|
|
||||||
- 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, it 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 of one client in
|
- `SWW_ERROR_BURST_THRESHOLD` (default `30`): the most refusals of one client in
|
||||||
a minute (see "Attack detection" in `SPEC.md`). A count or `off`.
|
a minute (see "Attack detection" in `SPEC.md`). A count or `off`.
|
||||||
|
|
||||||
|
|||||||
+59
-272
@@ -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/remotelog"
|
"sneak.berlin/go/smallwebwaf/internal/remotelog"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config is the settings of `sww discriminator`. A time or size limit of
|
// Config is the settings of `sww discriminator`. A time or size limit of
|
||||||
@@ -102,8 +101,8 @@ type Config struct {
|
|||||||
// the clients they hold (SWW_TAG_LISTS). TagDeny are the tags whose
|
// the clients they hold (SWW_TAG_LISTS). TagDeny are the tags whose
|
||||||
// requests are refused (SWW_TAG_DENY). TagLimitPercent gives the clients
|
// requests are refused (SWW_TAG_DENY). TagLimitPercent gives the clients
|
||||||
// with a tag that percentage of every rate limit and byte limit, from 1
|
// with a tag that percentage of every rate limit and byte limit, from 1
|
||||||
// to 99 (SWW_TAG_LIMIT_PERCENT). All hold tags as requests carry them: in
|
// to 99 (SWW_TAG_LIMIT_PERCENT). All hold tags in lower case, as requests
|
||||||
// lower case, a country code in capitals.
|
// carry them.
|
||||||
TagLists []TagList
|
TagLists []TagList
|
||||||
TagDeny []string
|
TagDeny []string
|
||||||
TagLimitPercent map[string]int64
|
TagLimitPercent map[string]int64
|
||||||
@@ -116,7 +115,7 @@ type Config struct {
|
|||||||
// UnknownLimitPercent is the percentage of every limit a client without
|
// UnknownLimitPercent is the percentage of every limit a client without
|
||||||
// a country gets (SWW_UNKNOWN_LIMIT_PERCENT). ASNLimitPercentURL is
|
// a country gets (SWW_UNKNOWN_LIMIT_PERCENT). ASNLimitPercentURL is
|
||||||
// where a file of AS:percent lines is fetched from, whose percentages
|
// where a file of AS:percent lines is fetched from, whose percentages
|
||||||
// count like asn= tags in TagLimitPercent (SWW_ASN_LIMIT_PERCENT_URL),
|
// count as asn= tags in TagLimitPercent do (SWW_ASN_LIMIT_PERCENT_URL),
|
||||||
// "" while it is unset.
|
// "" while it is unset.
|
||||||
ASNBytesPercent map[string]int64
|
ASNBytesPercent map[string]int64
|
||||||
CountryBytesPercent map[string]int64
|
CountryBytesPercent map[string]int64
|
||||||
@@ -166,8 +165,8 @@ type Config struct {
|
|||||||
// BanResponse is the status a refused client is answered with, 403
|
// BanResponse is the status a refused client is answered with, 403
|
||||||
// or 429, or 0 to close the connection without an answer
|
// or 429, or 0 to close the connection without an answer
|
||||||
// (SWW_BAN_RESPONSE). It answers a banned client, a request that
|
// (SWW_BAN_RESPONSE). It answers a banned client, a request that
|
||||||
// breaks a rate limit or matches a ban rule, SWW_DENY_NETS,
|
// breaks a rate limit or matches a ban rule, SWW_DENY_NETS and the
|
||||||
// SWW_EXCLUSIVELY_ALLOWED_COUNTRIES and SWW_TAG_DENY.
|
// country lists.
|
||||||
BanResponse int
|
BanResponse int
|
||||||
// LimitBanDuration is the ban for a first broken rate limit
|
// LimitBanDuration is the ban for a first broken rate limit
|
||||||
// (SWW_LIMIT_BAN_DURATION). A limit broken again within
|
// (SWW_LIMIT_BAN_DURATION). A limit broken again within
|
||||||
@@ -232,11 +231,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
|
||||||
@@ -404,26 +398,18 @@ var (
|
|||||||
errNotPathPrefix = errors.New(
|
errNotPathPrefix = errors.New(
|
||||||
"is not a path prefix starting with /, such as /assets/")
|
"is not a path prefix starting with /, such as /assets/")
|
||||||
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(
|
||||||
"is not the id of a Core Rule Set rule, a whole number such as 942100",
|
"is not the id of a Core Rule Set rule, a whole number such as 942100")
|
||||||
)
|
|
||||||
errSetupRuleID = errors.New(
|
errSetupRuleID = errors.New(
|
||||||
"is from 900000 to 900999, the ids of the rules that set the Core Rule Set " +
|
"is from 900000 to 900999, the ids of the rules that set the Core Rule Set " +
|
||||||
"up and of sww's own, which cannot be switched off")
|
"up and of sww's own, which cannot be switched off")
|
||||||
errNotBoolean = errors.New("is not true or false")
|
errNotBoolean = errors.New("is not true or false")
|
||||||
errNotLogRemoteURL = errors.New(
|
errNotLogRemoteURL = errors.New(
|
||||||
"is not syslog+udp, syslog+tcp or syslog+tls with a host and a port, " +
|
"is not syslog+udp, syslog+tcp or syslog+tls with a host and a port, " +
|
||||||
"and nothing more, such as syslog+tls://logs.example:6514",
|
"and nothing more, such as syslog+tls://logs.example:6514")
|
||||||
)
|
|
||||||
errNoCertificate = errors.New("holds no PEM certificate")
|
errNoCertificate = errors.New("holds no PEM certificate")
|
||||||
errNotFacility = errors.New("is not a syslog facility such as local0 or daemon")
|
errNotFacility = errors.New("is not a syslog facility such as local0 or daemon")
|
||||||
errNotAppName = errors.New(
|
errNotAppName = errors.New(
|
||||||
@@ -436,21 +422,16 @@ var (
|
|||||||
"is not a header name followed by : and the header's value, " +
|
"is not a header name followed by : and the header's value, " +
|
||||||
"such as Authorization:Bearer <token>")
|
"such as Authorization:Bearer <token>")
|
||||||
errControlCharacter = errors.New(
|
errControlCharacter = errors.New(
|
||||||
"holds a control character, such as the carriage return of a Windows line end",
|
"holds a control character, such as the carriage return of a Windows line end")
|
||||||
)
|
|
||||||
errNotAlertEvent = errors.New(
|
errNotAlertEvent = errors.New(
|
||||||
"is not ban, permanent_ban, waf_block, anomaly, reputation_hit, " +
|
"is not ban, permanent_ban, waf_block, anomaly, reputation_hit, " +
|
||||||
"source_failure or file_error")
|
"source_failure or file_error")
|
||||||
errNotNumberOrOff = errors.New(
|
errNotNumberOrOff = errors.New("is not a whole number above zero, such as 60, or off")
|
||||||
"is not a whole number above zero, such as 60, or off",
|
|
||||||
)
|
|
||||||
errNotUTF8 = errors.New("is not valid UTF-8")
|
errNotUTF8 = errors.New("is not valid UTF-8")
|
||||||
errNotASN = errors.New("is not an AS number such as AS64496")
|
errNotASN = errors.New("is not an AS number such as AS64496")
|
||||||
errNotPercentItem = errors.New(
|
errNotPercentItem = errors.New(
|
||||||
"is not a code, : and a percentage, such as AS64496:50 or cn:25")
|
"is not a code, : and a percentage, such as AS64496:50 or cn:25")
|
||||||
errNotPercent = errors.New(
|
errNotPercent = errors.New("is not a percentage, a whole number from 0 to 100")
|
||||||
"is not a percentage, a whole number from 0 to 100",
|
|
||||||
)
|
|
||||||
errListedTwice = errors.New("is listed twice")
|
errListedTwice = errors.New("is listed twice")
|
||||||
errNotListURL = errors.New(
|
errNotListURL = errors.New(
|
||||||
"is not an http or https URL without a user or a fragment, " +
|
"is not an http or https URL without a user or a fragment, " +
|
||||||
@@ -459,17 +440,11 @@ var (
|
|||||||
errNotAnHourOrMore = errors.New("is not a duration of 1h or more, such as 24h")
|
errNotAnHourOrMore = errors.New("is not a duration of 1h or more, such as 24h")
|
||||||
errNotAction = errors.New(
|
errNotAction = errors.New(
|
||||||
"is not deny, limit:<percent> such as limit:25, or log")
|
"is not deny, limit:<percent> such as limit:25, or log")
|
||||||
errNotZone = errors.New(
|
errNotZone = errors.New("is not a DNS zone such as dnsbl.dronebl.org")
|
||||||
"is not a DNS zone such as dnsbl.dronebl.org",
|
errZoneTooLong = errors.New("is longer than 189 characters, too long for the " +
|
||||||
)
|
"names IPv6 clients are asked about by")
|
||||||
errZoneTooLong = errors.New(
|
errNotResolver = errors.New("is not an IP address with an optional port, " +
|
||||||
"is longer than 189 characters, too long for the " +
|
"such as 192.0.2.53 or [2001:db8::53]:5353")
|
||||||
"names IPv6 clients are asked about by",
|
|
||||||
)
|
|
||||||
errNotResolver = errors.New(
|
|
||||||
"is not an IP address with an optional port, " +
|
|
||||||
"such as 192.0.2.53 or [2001:db8::53]:5353",
|
|
||||||
)
|
|
||||||
errNotLAPIURL = errors.New(
|
errNotLAPIURL = errors.New(
|
||||||
"is not an http or https URL without a user or a fragment, " +
|
"is not an http or https URL without a user or a fragment, " +
|
||||||
"such as http://172.17.0.1:8080")
|
"such as http://172.17.0.1:8080")
|
||||||
@@ -489,13 +464,6 @@ var (
|
|||||||
errExploitTag = errors.New(
|
errExploitTag = errors.New(
|
||||||
"cannot lower limits: the rate limits count a request before its " +
|
"cannot lower limits: the rate limits count a request before its " +
|
||||||
"rules are matched")
|
"rules are matched")
|
||||||
errCrowdSecTag = errors.New(
|
|
||||||
"cannot be used: a CrowdSec decision refuses the request first")
|
|
||||||
errNoRequestGetsTag = errors.New(
|
|
||||||
"is a tag no request can get: it is not a name in SWW_TAG_LISTS, an " +
|
|
||||||
"asn= or country= tag, a dnsbl= tag of a zone in SWW_DNSBL_ZONES, " +
|
|
||||||
"abuseipdb with SWW_ABUSEIPDB_KEY set, or exploitsignature with " +
|
|
||||||
"rule files or the Core Rule Set on")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// FromEnvironment reads the settings with lookupEnv, normally
|
// FromEnvironment reads the settings with lookupEnv, normally
|
||||||
@@ -505,9 +473,7 @@ var (
|
|||||||
// setting that is set but invalid is an error that names it.
|
// setting that is set but invalid is an error that names it.
|
||||||
//
|
//
|
||||||
//nolint:funlen // one line for each setting, a list that grows with them
|
//nolint:funlen // one line for each setting, a list that grows with them
|
||||||
func FromEnvironment(
|
func FromEnvironment(lookupEnv func(string) (string, bool)) (*Config, error) {
|
||||||
lookupEnv func(string) (string, bool),
|
|
||||||
) (*Config, error) {
|
|
||||||
env := &environment{lookupEnv: lookupEnv}
|
env := &environment{lookupEnv: lookupEnv}
|
||||||
cfg := &Config{
|
cfg := &Config{
|
||||||
ListenAddr: env.address("SWW_LISTEN_ADDR"),
|
ListenAddr: env.address("SWW_LISTEN_ADDR"),
|
||||||
@@ -577,7 +543,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"),
|
||||||
@@ -616,7 +581,6 @@ func FromEnvironment(
|
|||||||
env.checkASNLimitPercentURL(cfg)
|
env.checkASNLimitPercentURL(cfg)
|
||||||
env.checkCrowdSec(cfg)
|
env.checkCrowdSec(cfg)
|
||||||
env.checkTagLists(cfg)
|
env.checkTagLists(cfg)
|
||||||
env.checkTags(cfg)
|
|
||||||
|
|
||||||
if env.err != nil {
|
if env.err != nil {
|
||||||
return nil, env.err
|
return nil, env.err
|
||||||
@@ -708,10 +672,7 @@ func (e *environment) lookup(name string) (string, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if set {
|
if set {
|
||||||
e.check(
|
e.check(name, fmt.Errorf("is set, and so is %s; %w", fileName, errSetTwice))
|
||||||
name,
|
|
||||||
fmt.Errorf("is set, and so is %s; %w", fileName, errSetTwice),
|
|
||||||
)
|
|
||||||
|
|
||||||
return value, set
|
return value, set
|
||||||
}
|
}
|
||||||
@@ -833,25 +794,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 {
|
||||||
@@ -1013,10 +955,7 @@ func (e *environment) zones(name string) []string {
|
|||||||
logged[i] = MaskZoneKey(zone)
|
logged[i] = MaskZoneKey(zone)
|
||||||
}
|
}
|
||||||
|
|
||||||
e.settings = append(
|
e.settings = append(e.settings, slog.String(name, strings.Join(logged, ",")))
|
||||||
e.settings,
|
|
||||||
slog.String(name, strings.Join(logged, ",")),
|
|
||||||
)
|
|
||||||
|
|
||||||
return zones
|
return zones
|
||||||
}
|
}
|
||||||
@@ -1085,7 +1024,7 @@ func (e *environment) checkLookupDBPath(cfg *Config) {
|
|||||||
// limit, and the anomaly thresholds per AS number.
|
// limit, and the anomaly thresholds per AS number.
|
||||||
func (e *environment) checkCountriesAndLookups(cfg *Config) {
|
func (e *environment) checkCountriesAndLookups(cfg *Config) {
|
||||||
for _, country := range cfg.ExclusivelyAllowedCountries {
|
for _, country := range cfg.ExclusivelyAllowedCountries {
|
||||||
if slices.Contains(cfg.TagDeny, CountryTagPrefix+country) {
|
if slices.Contains(cfg.TagDeny, countryTagPrefix+strings.ToLower(country)) {
|
||||||
e.check("SWW_EXCLUSIVELY_ALLOWED_COUNTRIES",
|
e.check("SWW_EXCLUSIVELY_ALLOWED_COUNTRIES",
|
||||||
fmt.Errorf("%q %w", country, errOnBothLists))
|
fmt.Errorf("%q %w", country, errOnBothLists))
|
||||||
}
|
}
|
||||||
@@ -1159,56 +1098,6 @@ func (e *environment) checkTagLists(cfg *Config) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkTags refuses a tag of SWW_TAG_DENY or SWW_TAG_LIMIT_PERCENT that would
|
|
||||||
// never apply: crowdsec, whose request is refused before tags are acted on, and
|
|
||||||
// any tag these settings cannot give a request.
|
|
||||||
func (e *environment) checkTags(cfg *Config) {
|
|
||||||
for _, tag := range cfg.TagDeny {
|
|
||||||
e.check("SWW_TAG_DENY", checkTag(cfg, tag))
|
|
||||||
}
|
|
||||||
|
|
||||||
for tag := range cfg.TagLimitPercent {
|
|
||||||
e.check("SWW_TAG_LIMIT_PERCENT", checkTag(cfg, tag))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// checkTag returns an error when tag would never apply, as checkTags says.
|
|
||||||
func checkTag(cfg *Config, tag string) error {
|
|
||||||
if tag == CrowdSecTag {
|
|
||||||
return fmt.Errorf("%q %w", tag, errCrowdSecTag)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !requestCanGetTag(cfg, tag) {
|
|
||||||
return fmt.Errorf("%q %w", MaskZoneKey(tag), errNoRequestGetsTag)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// requestCanGetTag reports whether anything cfg sets up can give a request tag.
|
|
||||||
func requestCanGetTag(cfg *Config, tag string) bool {
|
|
||||||
isListName := func(list TagList) bool { return list.Name == tag }
|
|
||||||
if isLookupTag(tag) || slices.ContainsFunc(cfg.TagLists, isListName) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if tag == ExploitSignatureTag {
|
|
||||||
coreRuleSetOn := cfg.WAFMode != WAFModeOff && cfg.WAFAnomalyThreshold != 0
|
|
||||||
|
|
||||||
return cfg.RulesEnabled || coreRuleSetOn
|
|
||||||
}
|
|
||||||
|
|
||||||
if tag == AbuseIPDBTag {
|
|
||||||
return cfg.AbuseIPDBKey != ""
|
|
||||||
}
|
|
||||||
|
|
||||||
zone, isZoneTag := strings.CutPrefix(tag, DNSBLTagPrefix)
|
|
||||||
|
|
||||||
return isZoneTag && slices.ContainsFunc(cfg.DNSBLZones, func(each string) bool {
|
|
||||||
return strings.ToLower(MaskZoneKey(each)) == zone
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// TagsNeedLookup reports whether SWW_TAG_DENY or SWW_TAG_LIMIT_PERCENT
|
// TagsNeedLookup reports whether SWW_TAG_DENY or SWW_TAG_LIMIT_PERCENT
|
||||||
// names an asn= or country= tag, which only the lookup can give a request.
|
// names an asn= or country= tag, which only the lookup can give a request.
|
||||||
func (c *Config) TagsNeedLookup() bool {
|
func (c *Config) TagsNeedLookup() bool {
|
||||||
@@ -1229,7 +1118,7 @@ func tagLimitNeedsLookups(cfg *Config) bool {
|
|||||||
|
|
||||||
// isLookupTag reports whether tag is an asn= or country= tag.
|
// isLookupTag reports whether tag is an asn= or country= tag.
|
||||||
func isLookupTag(tag string) bool {
|
func isLookupTag(tag string) bool {
|
||||||
return strings.HasPrefix(tag, ASNTagPrefix) || strings.HasPrefix(tag, CountryTagPrefix)
|
return strings.HasPrefix(tag, asnTagPrefix) || strings.HasPrefix(tag, countryTagPrefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
// headerNames reads a setting that is a list of header names, and
|
// headerNames reads a setting that is a list of header names, and
|
||||||
@@ -1243,9 +1132,7 @@ func (e *environment) headerNames(name, defaultValue string) []string {
|
|||||||
|
|
||||||
// durationNotOff reads a setting that is a duration and, unlike a
|
// durationNotOff reads a setting that is a duration and, unlike a
|
||||||
// timeout, cannot be off.
|
// timeout, cannot be off.
|
||||||
func (e *environment) durationNotOff(
|
func (e *environment) durationNotOff(name, defaultValue string) time.Duration {
|
||||||
name, defaultValue string,
|
|
||||||
) time.Duration {
|
|
||||||
duration, err := ParseDurationNotOff(e.value(name, defaultValue))
|
duration, err := ParseDurationNotOff(e.value(name, defaultValue))
|
||||||
e.check(name, err)
|
e.check(name, err)
|
||||||
|
|
||||||
@@ -1444,10 +1331,7 @@ func (e *environment) instanceName() string {
|
|||||||
// appName reads the setting that is the APP-NAME of the records the log
|
// appName reads the setting that is the APP-NAME of the records the log
|
||||||
// lines are sent in, by default the instance name. Its value is checked
|
// lines are sent in, by default the instance name. Its value is checked
|
||||||
// when it is set, and, while lines are sent, when it is the instance name.
|
// when it is set, and, while lines are sent, when it is the instance name.
|
||||||
func (e *environment) appName(
|
func (e *environment) appName(name, instanceName string, sending bool) string {
|
||||||
name, instanceName string,
|
|
||||||
sending bool,
|
|
||||||
) string {
|
|
||||||
value, set := e.lookup(name)
|
value, set := e.lookup(name)
|
||||||
if !set {
|
if !set {
|
||||||
value = instanceName
|
value = instanceName
|
||||||
@@ -1470,10 +1354,7 @@ func (e *environment) appName(
|
|||||||
// checkInstanceNameForNtfy refuses an instance name that holds a control
|
// checkInstanceNameForNtfy refuses an instance name that holds a control
|
||||||
// character while ntfySet, SWW_ALERT_NTFY_URL being set: ntfy is sent
|
// character while ntfySet, SWW_ALERT_NTFY_URL being set: ntfy is sent
|
||||||
// the instance name in a header, which cannot hold one.
|
// the instance name in a header, which cannot hold one.
|
||||||
func (e *environment) checkInstanceNameForNtfy(
|
func (e *environment) checkInstanceNameForNtfy(instanceName string, ntfySet bool) {
|
||||||
instanceName string,
|
|
||||||
ntfySet bool,
|
|
||||||
) {
|
|
||||||
if ntfySet && strings.ContainsFunc(instanceName, unicode.IsControl) {
|
if ntfySet && strings.ContainsFunc(instanceName, unicode.IsControl) {
|
||||||
e.check("SWW_INSTANCE_NAME", fmt.Errorf(
|
e.check("SWW_INSTANCE_NAME", fmt.Errorf(
|
||||||
"%q %w, and is sent to ntfy in a header while SWW_ALERT_NTFY_URL is set",
|
"%q %w, and is sent to ntfy in a header while SWW_ALERT_NTFY_URL is set",
|
||||||
@@ -1797,10 +1678,7 @@ func parseNamedNetblocks(value string) ([]anomaly.NamedNetblock, error) {
|
|||||||
return nil, fmt.Errorf("%q %w", name, errListedTwice)
|
return nil, fmt.Errorf("%q %w", name, errListedTwice)
|
||||||
}
|
}
|
||||||
|
|
||||||
named = append(
|
named = append(named, anomaly.NamedNetblock{Name: name, Netblock: netblock})
|
||||||
named,
|
|
||||||
anomaly.NamedNetblock{Name: name, Netblock: netblock},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return named, nil
|
return named, nil
|
||||||
@@ -1842,65 +1720,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 waf.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(waf.AppNames(), name) {
|
|
||||||
return nil, fmt.Errorf("%q %w %s", name, errNotApp,
|
|
||||||
strings.Join(waf.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.
|
||||||
@@ -2007,19 +1826,16 @@ func ParseASN(value string) (string, error) {
|
|||||||
return "AS" + strconv.FormatUint(number, 10), nil
|
return "AS" + strconv.FormatUint(number, 10), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// The tags a request can get that the settings can name, as discriminator
|
// The prefixes of the tags the lookup gives a request.
|
||||||
// writes them. The prefixes are followed by a value.
|
|
||||||
const (
|
const (
|
||||||
ASNTagPrefix = "asn="
|
asnTagPrefix = "asn="
|
||||||
CountryTagPrefix = "country="
|
countryTagPrefix = "country="
|
||||||
DNSBLTagPrefix = "dnsbl="
|
|
||||||
AbuseIPDBTag = "abuseipdb"
|
|
||||||
CrowdSecTag = "crowdsec"
|
|
||||||
// ExploitSignatureTag is the tag of a request a rule file or the Core
|
|
||||||
// Rule Set matched.
|
|
||||||
ExploitSignatureTag = "exploitsignature"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ExploitSignatureTag is the tag of a request a rule file or the Core Rule
|
||||||
|
// Set matched.
|
||||||
|
const ExploitSignatureTag = "exploitsignature"
|
||||||
|
|
||||||
// TagList is an address list of SWW_TAG_LISTS: the tag it adds to the
|
// TagList is an address list of SWW_TAG_LISTS: the tag it adds to the
|
||||||
// requests of the clients it holds, and the URL it is fetched from.
|
// requests of the clients it holds, and the URL it is fetched from.
|
||||||
type TagList struct {
|
type TagList struct {
|
||||||
@@ -2060,13 +1876,11 @@ func parseTagLists(value string) ([]TagList, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parseTags reads a comma-separated list of tags, which parseTag reads. A
|
// parseTags reads a comma-separated list of tags, which parseTag reads. A
|
||||||
// tag listed twice is an error, which shows the tag as MaskZoneKey does.
|
// tag listed twice is an error.
|
||||||
func parseTags(value string) ([]string, error) {
|
func parseTags(value string) ([]string, error) {
|
||||||
items, err := parseList(value)
|
items, err := parseList(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// parseList's error, for an empty item, shows the whole value, keys
|
return nil, err
|
||||||
// included.
|
|
||||||
return nil, errEmptyItem
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tags := make([]string, 0, len(items))
|
tags := make([]string, 0, len(items))
|
||||||
@@ -2078,7 +1892,7 @@ func parseTags(value string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if slices.Contains(tags, tag) {
|
if slices.Contains(tags, tag) {
|
||||||
return nil, fmt.Errorf("%q %w", MaskZoneKey(item), errListedTwice)
|
return nil, fmt.Errorf("%q %w", item, errListedTwice)
|
||||||
}
|
}
|
||||||
|
|
||||||
tags = append(tags, tag)
|
tags = append(tags, tag)
|
||||||
@@ -2087,30 +1901,27 @@ func parseTags(value string) ([]string, error) {
|
|||||||
return tags, nil
|
return tags, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseTag reads a tag in either case, with spaces around it, and returns it
|
// parseTag reads a tag in either case, and returns it as a request carries
|
||||||
// as a request carries it: in lower case, but for a country code, which is in
|
// it: in lower case, an asn= tag's number without leading zeros. An asn= or
|
||||||
// capitals, and an asn= tag's number without leading zeros. An asn= or
|
// country= tag must name a number or a country, so that a mistyped one
|
||||||
// country= tag must name a number or a country, so that a mistyped one stops
|
// stops the start instead of never matching.
|
||||||
// the start instead of never matching.
|
|
||||||
func parseTag(value string) (string, error) {
|
func parseTag(value string) (string, error) {
|
||||||
tag := strings.ToLower(strings.TrimSpace(value))
|
tag := strings.ToLower(value)
|
||||||
|
|
||||||
if digits, isASN := strings.CutPrefix(tag, ASNTagPrefix); isASN {
|
if digits, isASN := strings.CutPrefix(tag, asnTagPrefix); isASN {
|
||||||
number, err := strconv.ParseUint(digits, 10, 32)
|
number, err := strconv.ParseUint(digits, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("%q %w", value, errNotASNTag)
|
return "", fmt.Errorf("%q %w", value, errNotASNTag)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ASNTagPrefix + strconv.FormatUint(number, 10), nil
|
return asnTagPrefix + strconv.FormatUint(number, 10), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if code, isCountry := strings.CutPrefix(tag, CountryTagPrefix); isCountry {
|
if code, isCountry := strings.CutPrefix(tag, countryTagPrefix); isCountry {
|
||||||
country, err := parseCountry(code)
|
_, err := parseCountry(code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return CountryTagPrefix + country, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return tag, nil
|
return tag, nil
|
||||||
@@ -2123,9 +1934,7 @@ func parseTag(value string) (string, error) {
|
|||||||
func parseTagPercents(value string) (map[string]int64, error) {
|
func parseTagPercents(value string) (map[string]int64, error) {
|
||||||
items, err := parseList(value)
|
items, err := parseList(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// parseList's error, for an empty item, shows the whole value, keys
|
return nil, err
|
||||||
// included.
|
|
||||||
return nil, errEmptyItem
|
|
||||||
}
|
}
|
||||||
|
|
||||||
percents := make(map[string]int64, len(items))
|
percents := make(map[string]int64, len(items))
|
||||||
@@ -2133,7 +1942,7 @@ func parseTagPercents(value string) (map[string]int64, error) {
|
|||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
colon := strings.LastIndex(item, ":")
|
colon := strings.LastIndex(item, ":")
|
||||||
if colon <= 0 {
|
if colon <= 0 {
|
||||||
return nil, fmt.Errorf("%q %w", MaskZoneKey(item), errNotTagPercentItem)
|
return nil, fmt.Errorf("%q %w", item, errNotTagPercentItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
tag, err := parseTag(item[:colon])
|
tag, err := parseTag(item[:colon])
|
||||||
@@ -2143,7 +1952,7 @@ func parseTagPercents(value string) (map[string]int64, error) {
|
|||||||
|
|
||||||
percent, err := strconv.ParseInt(item[colon+1:], 10, 64)
|
percent, err := strconv.ParseInt(item[colon+1:], 10, 64)
|
||||||
if err != nil || percent < 1 || percent > 99 {
|
if err != nil || percent < 1 || percent > 99 {
|
||||||
return nil, fmt.Errorf("%q %w", MaskZoneKey(item[colon+1:]), errNotTagPercent)
|
return nil, fmt.Errorf("%q %w", item[colon+1:], errNotTagPercent)
|
||||||
}
|
}
|
||||||
|
|
||||||
if tag == ExploitSignatureTag {
|
if tag == ExploitSignatureTag {
|
||||||
@@ -2151,9 +1960,7 @@ func parseTagPercents(value string) (map[string]int64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if _, listed := percents[tag]; listed {
|
if _, listed := percents[tag]; listed {
|
||||||
// tag, since item[:colon] may end in a space, which MaskZoneKey
|
return nil, fmt.Errorf("%q %w", item[:colon], errListedTwice)
|
||||||
// would not mask.
|
|
||||||
return nil, fmt.Errorf("%q %w", MaskZoneKey(tag), errListedTwice)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
percents[tag] = percent
|
percents[tag] = percent
|
||||||
@@ -2253,11 +2060,8 @@ func parseHeaderNames(value string) ([]string, error) {
|
|||||||
header := strings.ToLower(item)
|
header := strings.ToLower(item)
|
||||||
switch header {
|
switch header {
|
||||||
case "host":
|
case "host":
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf("%q %w; the request's host is the field host",
|
||||||
"%q %w; the request's host is the field host",
|
item, errHeaderTakenOut)
|
||||||
item,
|
|
||||||
errHeaderTakenOut,
|
|
||||||
)
|
|
||||||
case "transfer-encoding":
|
case "transfer-encoding":
|
||||||
return nil, fmt.Errorf("%q %w", item, errHeaderTakenOut)
|
return nil, fmt.Errorf("%q %w", item, errHeaderTakenOut)
|
||||||
}
|
}
|
||||||
@@ -2294,11 +2098,9 @@ func parseUpstreamURL(value string) (*url.URL, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onlySchemeAndHost := (upstream.Scheme == "http" || upstream.Scheme == "https") &&
|
onlySchemeAndHost := (upstream.Scheme == "http" || upstream.Scheme == "https") &&
|
||||||
upstream.Hostname() != "" && upstream.User == nil &&
|
upstream.Hostname() != "" && upstream.User == nil && upstream.Opaque == "" &&
|
||||||
upstream.Opaque == "" &&
|
|
||||||
(upstream.Path == "" || upstream.Path == "/") &&
|
(upstream.Path == "" || upstream.Path == "/") &&
|
||||||
upstream.RawQuery == "" &&
|
upstream.RawQuery == "" && upstream.Fragment == ""
|
||||||
upstream.Fragment == ""
|
|
||||||
if !onlySchemeAndHost {
|
if !onlySchemeAndHost {
|
||||||
return nil, fmt.Errorf("%q %w", value, errNotUpstreamURL)
|
return nil, fmt.Errorf("%q %w", value, errNotUpstreamURL)
|
||||||
}
|
}
|
||||||
@@ -2322,11 +2124,7 @@ func parseLogRemoteURL(value string) (*url.URL, error) {
|
|||||||
return nil, fmt.Errorf("%q %w", value, errNotLogRemoteURL)
|
return nil, fmt.Errorf("%q %w", value, errNotLogRemoteURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
schemes := []string{
|
schemes := []string{remotelog.SchemeUDP, remotelog.SchemeTCP, remotelog.SchemeTLS}
|
||||||
remotelog.SchemeUDP,
|
|
||||||
remotelog.SchemeTCP,
|
|
||||||
remotelog.SchemeTLS,
|
|
||||||
}
|
|
||||||
port, err := strconv.ParseUint(remote.Port(), 10, 16)
|
port, err := strconv.ParseUint(remote.Port(), 10, 16)
|
||||||
|
|
||||||
onlySchemeHostAndPort := slices.Contains(schemes, remote.Scheme) &&
|
onlySchemeHostAndPort := slices.Contains(schemes, remote.Scheme) &&
|
||||||
@@ -2389,9 +2187,7 @@ func isHTTPURL(u *url.URL) bool {
|
|||||||
|
|
||||||
return (u.Scheme == "http" || u.Scheme == "https") && u.Hostname() != "" &&
|
return (u.Scheme == "http" || u.Scheme == "https") && u.Hostname() != "" &&
|
||||||
(u.Port() == "" || (err == nil && port != 0)) &&
|
(u.Port() == "" || (err == nil && port != 0)) &&
|
||||||
u.User == nil &&
|
u.User == nil && u.Opaque == "" && u.Fragment == ""
|
||||||
u.Opaque == "" &&
|
|
||||||
u.Fragment == ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// isListURL reports whether value is a URL a list can be fetched from, as
|
// isListURL reports whether value is a URL a list can be fetched from, as
|
||||||
@@ -2458,12 +2254,9 @@ func parseZones(value string) ([]string, error) {
|
|||||||
|
|
||||||
for i, zone := range zones {
|
for i, zone := range zones {
|
||||||
shown := MaskZoneKey(zone)
|
shown := MaskZoneKey(zone)
|
||||||
listedBefore := slices.ContainsFunc(
|
listedBefore := slices.ContainsFunc(zones[:i], func(earlier string) bool {
|
||||||
zones[:i],
|
|
||||||
func(earlier string) bool {
|
|
||||||
return strings.EqualFold(MaskZoneKey(earlier), shown)
|
return strings.EqualFold(MaskZoneKey(earlier), shown)
|
||||||
},
|
})
|
||||||
)
|
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case len(zone) > maxZoneLength:
|
case len(zone) > maxZoneLength:
|
||||||
@@ -2496,8 +2289,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 {
|
||||||
@@ -2551,13 +2343,8 @@ func parseWebhookHeaders(value string) (http.Header, string, error) {
|
|||||||
name, headerValue, found := strings.Cut(item, ":")
|
name, headerValue, found := strings.Cut(item, ":")
|
||||||
name = strings.TrimSpace(name)
|
name = strings.TrimSpace(name)
|
||||||
|
|
||||||
if !found || !IsHeaderName(name) ||
|
if !found || !IsHeaderName(name) || strings.ContainsAny(headerValue, "\r\n\x00") {
|
||||||
strings.ContainsAny(headerValue, "\r\n\x00") {
|
return nil, "", fmt.Errorf("item %d %w", i+1, errNotWebhookHeader)
|
||||||
return nil, "", fmt.Errorf(
|
|
||||||
"item %d %w",
|
|
||||||
i+1,
|
|
||||||
errNotWebhookHeader,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
headers.Add(name, strings.TrimSpace(headerValue))
|
headers.Add(name, strings.TrimSpace(headerValue))
|
||||||
|
|||||||
+14
-226
@@ -88,7 +88,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"
|
||||||
@@ -209,9 +208,6 @@ const off = "off"
|
|||||||
// enabled is true, as a setting's value.
|
// enabled is true, as a setting's value.
|
||||||
const enabled = "true"
|
const enabled = "true"
|
||||||
|
|
||||||
// disabled is false, as a setting's value.
|
|
||||||
const disabled = "false"
|
|
||||||
|
|
||||||
// defaultLookupSource is the default of SWW_LOOKUP_SOURCE, and
|
// defaultLookupSource is the default of SWW_LOOKUP_SOURCE, and
|
||||||
// fileSource the source that is the lookup database.
|
// fileSource the source that is the lookup database.
|
||||||
const (
|
const (
|
||||||
@@ -349,7 +345,7 @@ func TestValuesAsSet(t *testing.T) {
|
|||||||
metricsToken: token,
|
metricsToken: token,
|
||||||
metricsTopN: "10",
|
metricsTopN: "10",
|
||||||
rulesDir: "/srv/waf-rules",
|
rulesDir: "/srv/waf-rules",
|
||||||
rulesEnabled: disabled,
|
rulesEnabled: "false",
|
||||||
})
|
})
|
||||||
|
|
||||||
wantSettings(t, cfg, config.Config{
|
wantSettings(t, cfg, config.Config{
|
||||||
@@ -582,92 +578,6 @@ func TestCoreRuleSetSettings(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
} {
|
|
||||||
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,nextcloud",
|
|
||||||
`"nextcloud" is not an app sww has fixes for; the apps are gitea`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"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()
|
||||||
|
|
||||||
@@ -1189,7 +1099,7 @@ func TestInstanceNameNotUTF8StopsTheStart(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCodeInAllowedCountriesAndTagDenyStopsTheStart(t *testing.T) {
|
func TestCodeOnBothCountryListsStopsTheStart(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
_, err := config.FromEnvironment(environment{
|
_, err := config.FromEnvironment(environment{
|
||||||
@@ -1197,7 +1107,7 @@ func TestCodeInAllowedCountriesAndTagDenyStopsTheStart(t *testing.T) {
|
|||||||
allowedCountries: "de,RU",
|
allowedCountries: "de,RU",
|
||||||
}.lookupEnv)
|
}.lookupEnv)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("ru in SWW_EXCLUSIVELY_ALLOWED_COUNTRIES and SWW_TAG_DENY was accepted")
|
t.Fatal("ru on both country lists was accepted")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(err.Error(), allowedCountries+": ") ||
|
if !strings.HasPrefix(err.Error(), allowedCountries+": ") ||
|
||||||
@@ -1270,7 +1180,7 @@ func TestSettingNeedingLookupsStopsTheStartWhileTheyAreOff(t *testing.T) {
|
|||||||
for name, value := range map[string]string{
|
for name, value := range map[string]string{
|
||||||
allowedCountries: "de",
|
allowedCountries: "de",
|
||||||
addLookupHeaders: enabled,
|
addLookupHeaders: enabled,
|
||||||
tagDeny: "country=KP",
|
tagDeny: "country=kp",
|
||||||
tagLimitPercent: "asn=64496:50",
|
tagLimitPercent: "asn=64496:50",
|
||||||
asnLimitPercentURL: asnURL,
|
asnLimitPercentURL: asnURL,
|
||||||
asnBytesPercent: "AS64496:50",
|
asnBytesPercent: "AS64496:50",
|
||||||
@@ -1300,7 +1210,6 @@ func TestSettingNeedingLookupsStopsTheStartWhileTheyAreOff(t *testing.T) {
|
|||||||
// any other anomaly threshold, or a blocklist.
|
// any other anomaly threshold, or a blocklist.
|
||||||
env := environment{
|
env := environment{
|
||||||
lookupSource: off, allowedCountries: "", asnBytesPercent: "",
|
lookupSource: off, allowedCountries: "", asnBytesPercent: "",
|
||||||
tagLists: "torexit=https://example.com/tor.txt,vpn=https://example.com/vpn.txt",
|
|
||||||
tagDeny: torExitTag, tagLimitPercent: "vpn:50",
|
tagDeny: torExitTag, tagLimitPercent: "vpn:50",
|
||||||
countryBytesPercent: "", unknownLimitPercent: "100", asnLimitPercentURL: "",
|
countryBytesPercent: "", unknownLimitPercent: "100", asnLimitPercentURL: "",
|
||||||
blocklistURLs: dropURL,
|
blocklistURLs: dropURL,
|
||||||
@@ -1447,12 +1356,8 @@ func TestInvalidAnomalySettingStopsTheStartSayingWhatIsWrong(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// torExitTag, zenTag and exploitTag are tags these tests name.
|
// torExitTag is a tag these tests name.
|
||||||
const (
|
const torExitTag = "torexit"
|
||||||
torExitTag = "torexit"
|
|
||||||
zenTag = "dnsbl=zen.example"
|
|
||||||
exploitTag = "exploitsignature"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestTagSettingsAsSet(t *testing.T) {
|
func TestTagSettingsAsSet(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
@@ -1464,9 +1369,8 @@ func TestTagSettingsAsSet(t *testing.T) {
|
|||||||
cfg := fromEnvironment(t, environment{
|
cfg := fromEnvironment(t, environment{
|
||||||
tagLists: "TorExit=" + torURL +
|
tagLists: "TorExit=" + torURL +
|
||||||
", vpn=mullvad=https://example.com/mullvad.txt",
|
", vpn=mullvad=https://example.com/mullvad.txt",
|
||||||
dnsblZones: "Zen.example",
|
tagDeny: "TorExit, Country=KP, asn=014061",
|
||||||
tagDeny: "TorExit, Country=kp, asn=014061",
|
tagLimitPercent: "vpn:50, asn=014061:25, COUNTRY=cn:1, dnsbl=zen.example:99",
|
||||||
tagLimitPercent: "vpn=mullvad :50, asn=014061:25, COUNTRY=cn:1, dnsbl=zen.example:99",
|
|
||||||
})
|
})
|
||||||
|
|
||||||
wantLists := []config.TagList{
|
wantLists := []config.TagList{
|
||||||
@@ -1477,112 +1381,19 @@ func TestTagSettingsAsSet(t *testing.T) {
|
|||||||
t.Errorf("%s gave %+v, want %+v", tagLists, cfg.TagLists, wantLists)
|
t.Errorf("%s gave %+v, want %+v", tagLists, cfg.TagLists, wantLists)
|
||||||
}
|
}
|
||||||
|
|
||||||
wantDeny := []string{torExitTag, "country=KP", "asn=14061"}
|
wantDeny := []string{torExitTag, "country=kp", "asn=14061"}
|
||||||
if !slices.Equal(cfg.TagDeny, wantDeny) {
|
if !slices.Equal(cfg.TagDeny, wantDeny) {
|
||||||
t.Errorf("%s gave %v, want %v", tagDeny, cfg.TagDeny, wantDeny)
|
t.Errorf("%s gave %v, want %v", tagDeny, cfg.TagDeny, wantDeny)
|
||||||
}
|
}
|
||||||
|
|
||||||
wantPercents := map[string]int64{
|
wantPercents := map[string]int64{
|
||||||
"vpn=mullvad": 50, "asn=14061": 25, "country=CN": 1, zenTag: 99,
|
"vpn": 50, "asn=14061": 25, "country=cn": 1, "dnsbl=zen.example": 99,
|
||||||
}
|
}
|
||||||
if !maps.Equal(cfg.TagLimitPercent, wantPercents) {
|
if !maps.Equal(cfg.TagLimitPercent, wantPercents) {
|
||||||
t.Errorf("%s gave %v, want %v", tagLimitPercent, cfg.TagLimitPercent, wantPercents)
|
t.Errorf("%s gave %v, want %v", tagLimitPercent, cfg.TagLimitPercent, wantPercents)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTagThatNoRequestCanGetIsToldApartFromOneItCan(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
const (
|
|
||||||
list = "torexit=https://example.com/tor.txt"
|
|
||||||
key = "abc123"
|
|
||||||
zone = key + ".xbl.dq.spamhaus.net"
|
|
||||||
masked = "********.xbl.dq.spamhaus.net"
|
|
||||||
)
|
|
||||||
|
|
||||||
for _, tc := range []struct {
|
|
||||||
name string
|
|
||||||
env environment
|
|
||||||
want string // "" is accepted
|
|
||||||
}{
|
|
||||||
{"a list's name", environment{tagLists: list, tagDeny: torExitTag}, ""},
|
|
||||||
{
|
|
||||||
"a mistyped list's name", environment{tagLists: list, tagDeny: "torexti"},
|
|
||||||
`"torexti"`,
|
|
||||||
},
|
|
||||||
{"a masked zone", environment{dnsblZones: zone, tagDeny: "dnsbl=" + masked}, ""},
|
|
||||||
{
|
|
||||||
"a zone with its key", environment{dnsblZones: zone, tagDeny: "dnsbl=" + zone},
|
|
||||||
masked,
|
|
||||||
},
|
|
||||||
{"another zone", environment{dnsblZones: zone, tagDeny: zenTag}, zenTag},
|
|
||||||
{
|
|
||||||
"abuseipdb with a key",
|
|
||||||
environment{abuseIPDBKey: "k", tagLimitPercent: "abuseipdb:50"}, "",
|
|
||||||
},
|
|
||||||
{"crowdsec, though a list has its name", environment{
|
|
||||||
tagLists: "crowdsec=https://example.com/c.txt", tagDeny: "crowdsec",
|
|
||||||
}, `"crowdsec" cannot be used`},
|
|
||||||
{exploitTag, environment{tagDeny: exploitTag}, ""},
|
|
||||||
{
|
|
||||||
exploitTag + " with rule files off",
|
|
||||||
environment{tagDeny: exploitTag, rulesEnabled: disabled}, "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
exploitTag + " with the Core Rule Set off",
|
|
||||||
environment{tagDeny: exploitTag, wafMode: off}, "",
|
|
||||||
},
|
|
||||||
} {
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
_, err := config.FromEnvironment(tc.env.lookupEnv)
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case tc.want == "" && err != nil:
|
|
||||||
t.Errorf("error %v, want none", err)
|
|
||||||
case tc.want != "" && (err == nil || !strings.Contains(err.Error(), tc.want)):
|
|
||||||
t.Errorf("error %v, want one holding %s", err, tc.want)
|
|
||||||
case err != nil && strings.Contains(err.Error(), key):
|
|
||||||
t.Errorf("error %v shows the zone's key", err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestErrorOfATagSettingLeavesOutAZoneKey(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
const (
|
|
||||||
tag = "dnsbl=" + spamhaus
|
|
||||||
emptyItem = "has an empty item in its list"
|
|
||||||
twice = `"` + spamhausMasked + `" is listed twice`
|
|
||||||
)
|
|
||||||
|
|
||||||
for _, tc := range []struct{ name, value, want string }{
|
|
||||||
{tagDeny, tag + "," + tag, twice},
|
|
||||||
{tagDeny, tag + ",,", emptyItem},
|
|
||||||
{
|
|
||||||
tagLimitPercent, tag,
|
|
||||||
`"` + spamhausMasked + `" is not a tag, : and a percentage, ` +
|
|
||||||
"such as vpn:50 or asn=14061:25",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tagLimitPercent, "50:" + tag,
|
|
||||||
`"` + spamhausMasked + `" is not a percentage from 1 to 99`,
|
|
||||||
},
|
|
||||||
{tagLimitPercent, tag + ":50," + tag + " :25", twice},
|
|
||||||
{tagLimitPercent, tag + ":50,,", emptyItem},
|
|
||||||
} {
|
|
||||||
_, err := config.FromEnvironment(environment{tc.name: tc.value}.lookupEnv)
|
|
||||||
|
|
||||||
want := tc.name + ": " + tc.want
|
|
||||||
if err == nil || err.Error() != want {
|
|
||||||
t.Errorf("%s=%s gave the error %v, want %s", tc.name, tc.value, err, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTagListURLFetchedAsAnotherListStopsTheStart(t *testing.T) {
|
func TestTagListURLFetchedAsAnotherListStopsTheStart(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -2273,12 +2084,9 @@ func TestInvalidValueStopsTheStart(t *testing.T) {
|
|||||||
{countryBytesPercent, "cn"}, {countryBytesPercent, "cn:"},
|
{countryBytesPercent, "cn"}, {countryBytesPercent, "cn:"},
|
||||||
{countryBytesPercent, "cn:25:50"}, {countryBytesPercent, "china:25"},
|
{countryBytesPercent, "cn:25:50"}, {countryBytesPercent, "china:25"},
|
||||||
{tagDeny, "a,,b"}, {tagDeny, "country=nk"}, {tagDeny, "asn=abc"},
|
{tagDeny, "a,,b"}, {tagDeny, "country=nk"}, {tagDeny, "asn=abc"},
|
||||||
{tagDeny, "asn="}, {tagDeny, "torexit,TOREXIT"}, {tagDeny, "torexti"},
|
{tagDeny, "asn="}, {tagDeny, "torexit,TOREXIT"},
|
||||||
{tagDeny, zenTag}, {tagDeny, "abuseipdb"}, {tagDeny, "crowdsec"},
|
{tagLimitPercent, "vpn"}, {tagLimitPercent, ":50"}, {tagLimitPercent, "vpn:"},
|
||||||
{tagLimitPercent, "torexti:50"}, {tagLimitPercent, "dnsbl=zen.example:50"},
|
{tagLimitPercent, "vpn:0"}, {tagLimitPercent, "vpn:100"},
|
||||||
{tagLimitPercent, "abuseipdb:50"}, {tagLimitPercent, "crowdsec:50"},
|
|
||||||
{tagLimitPercent, "vpn :50"}, {tagLimitPercent, "vpn"}, {tagLimitPercent, ":50"},
|
|
||||||
{tagLimitPercent, "vpn:"}, {tagLimitPercent, "vpn:0"}, {tagLimitPercent, "vpn:100"},
|
|
||||||
{tagLimitPercent, "vpn:1.5"}, {tagLimitPercent, "vpn:50,VPN:25"},
|
{tagLimitPercent, "vpn:1.5"}, {tagLimitPercent, "vpn:50,VPN:25"},
|
||||||
{tagLimitPercent, "exploitsignature:50"}, {tagLimitPercent, "asn=abc:50"},
|
{tagLimitPercent, "exploitsignature:50"}, {tagLimitPercent, "asn=abc:50"},
|
||||||
{tagLists, torExitTag}, {tagLists, "=https://example.com/a.txt"},
|
{tagLists, torExitTag}, {tagLists, "=https://example.com/a.txt"},
|
||||||
@@ -2297,25 +2105,6 @@ func TestInvalidValueStopsTheStart(t *testing.T) {
|
|||||||
{logLevel, "INFO"}, {logLevel, "warning"}, {logLevel, "trace"}, {logLevel, ""},
|
{logLevel, "INFO"}, {logLevel, "warning"}, {logLevel, "trace"}, {logLevel, ""},
|
||||||
{rulesEnabled, "yes"}, {rulesEnabled, "True"},
|
{rulesEnabled, "yes"}, {rulesEnabled, "True"},
|
||||||
})
|
})
|
||||||
|
|
||||||
wantExploitTagStopsTheStartWithRulesAndCoreRuleSetOff(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
// wantExploitTagStopsTheStartWithRulesAndCoreRuleSetOff checks that with rule
|
|
||||||
// files off, and either setting turning off the Core Rule Set, the tag
|
|
||||||
// exploitsignature in SWW_TAG_DENY stops the start.
|
|
||||||
func wantExploitTagStopsTheStartWithRulesAndCoreRuleSetOff(t *testing.T) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
for _, name := range []string{wafMode, wafAnomalyThreshold} {
|
|
||||||
_, err := config.FromEnvironment(environment{
|
|
||||||
tagDeny: exploitTag, rulesEnabled: disabled, name: off,
|
|
||||||
}.lookupEnv)
|
|
||||||
if err == nil || !strings.HasPrefix(err.Error(), tagDeny+": ") {
|
|
||||||
t.Errorf("%s=off with rule files off gave the error %v, want one naming %s",
|
|
||||||
name, err, tagDeny)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidBanOrStateValueStopsTheStart(t *testing.T) {
|
func TestInvalidBanOrStateValueStopsTheStart(t *testing.T) {
|
||||||
@@ -2570,7 +2359,7 @@ func TestLogsEachSettingWithItsValue(t *testing.T) {
|
|||||||
lookupSource: defaultLookupSource,
|
lookupSource: defaultLookupSource,
|
||||||
lookupDBPath: "",
|
lookupDBPath: "",
|
||||||
lookupTimeout: "800ms",
|
lookupTimeout: "800ms",
|
||||||
addLookupHeaders: disabled,
|
addLookupHeaders: "false",
|
||||||
allowedCountries: "",
|
allowedCountries: "",
|
||||||
tagLists: "",
|
tagLists: "",
|
||||||
tagDeny: "",
|
tagDeny: "",
|
||||||
@@ -2617,7 +2406,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: "",
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
@@ -41,7 +40,7 @@ func (abuseIPDB) check(ctx context.Context, rq *request, _ time.Time) *verdict {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rq.abuseIPDBHit = true
|
rq.abuseIPDBHit = true
|
||||||
rq.addTag(config.AbuseIPDBTag)
|
rq.addTag("abuseipdb")
|
||||||
rq.noteHit(bans.ReputationHit{Source: reputation.AbuseIPDBSource, Score: &score},
|
rq.noteHit(bans.ReputationHit{Source: reputation.AbuseIPDBSource, Score: &score},
|
||||||
"scored by AbuseIPDB at or over SWW_ABUSEIPDB_MIN_SCORE")
|
"scored by AbuseIPDB at or over SWW_ABUSEIPDB_MIN_SCORE")
|
||||||
|
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ const (
|
|||||||
// The same, as SWW_TAG_LIMIT_PERCENT names fromDE's tags.
|
// The same, as SWW_TAG_LIMIT_PERCENT names fromDE's tags.
|
||||||
asnTagDEHalf = "asn=64496:50"
|
asnTagDEHalf = "asn=64496:50"
|
||||||
asnTagDEQuarter = "asn=64496:25"
|
asnTagDEQuarter = "asn=64496:25"
|
||||||
countryTagDEHalf = "country=DE:50"
|
countryTagDEHalf = "country=de:50"
|
||||||
countryTagDEQuarter = "country=DE:25"
|
countryTagDEQuarter = "country=de:25"
|
||||||
// noCountry is in an AS of its own, AS64500, and in no country.
|
// noCountry is in an AS of its own, AS64500, and in no country.
|
||||||
noCountry = "192.0.2.80"
|
noCountry = "192.0.2.80"
|
||||||
// fourAMinute is the rate limit these tests set: half of it is 2
|
// fourAMinute is the rate limit these tests set: half of it is 2
|
||||||
@@ -101,7 +101,7 @@ func TestEachBiasedThresholdLowersTheByteLimits(t *testing.T) {
|
|||||||
setting, value, from string
|
setting, value, from string
|
||||||
}{
|
}{
|
||||||
{tagLimitPercent, asnTagDEHalf, fromDE},
|
{tagLimitPercent, asnTagDEHalf, fromDE},
|
||||||
{tagLimitPercent, "Country=de:50", fromDE},
|
{tagLimitPercent, "Country=DE:50", fromDE},
|
||||||
{unknownLimitPercent, "50", unplaced},
|
{unknownLimitPercent, "50", unplaced},
|
||||||
{asnBytesPercent, "as64496:50", fromDE},
|
{asnBytesPercent, "as64496:50", fromDE},
|
||||||
{countryBytesPercent, countryDEHalf, fromDE},
|
{countryBytesPercent, countryDEHalf, fromDE},
|
||||||
@@ -328,9 +328,7 @@ func TestRequestWaitsForItsLookupWhileABiasedThresholdIsSet(t *testing.T) {
|
|||||||
// says, with a GeoJS that never answers.
|
// says, with a GeoJS that never answers.
|
||||||
synctest.Test(t, func(t *testing.T) {
|
synctest.Test(t, func(t *testing.T) {
|
||||||
server, out, _ := newDiscriminator(t, unansweredGeoJSURL, time.Now,
|
server, out, _ := newDiscriminator(t, unansweredGeoJSURL, time.Now,
|
||||||
map[string]string{
|
map[string]string{lookupTimeout: timeout.String(), tc.setting: tc.value})
|
||||||
lookupTimeout: timeout.String(), tagLists: "vpn=" + vpnList, tc.setting: tc.value,
|
|
||||||
})
|
|
||||||
began := time.Now()
|
began := time.Now()
|
||||||
|
|
||||||
serveFromDE(t, server, http.MethodPost, strings.NewReader("ab"))
|
serveFromDE(t, server, http.MethodPost, strings.NewReader("ab"))
|
||||||
|
|||||||
@@ -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"
|
||||||
)
|
)
|
||||||
@@ -284,27 +282,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.ActionWAFBlocked)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAttackInAFormBodyIsRefusedOnlyWhileBodiesAreRead(t *testing.T) {
|
func TestAttackInAFormBodyIsRefusedOnlyWhileBodiesAreRead(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
)
|
)
|
||||||
@@ -27,7 +26,7 @@ func (crowdSec) check(_ context.Context, rq *request, now time.Time) *verdict {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rq.addTag(config.CrowdSecTag)
|
rq.addTag("crowdsec")
|
||||||
rq.noteHit(bans.ReputationHit{Source: rq.h.config.CrowdSecDecisionsURL},
|
rq.noteHit(bans.ReputationHit{Source: rq.h.config.CrowdSecDecisionsURL},
|
||||||
"listed by the CrowdSec decision list")
|
"listed by the CrowdSec decision list")
|
||||||
rq.banForCrowdSec(now, decision)
|
rq.banForCrowdSec(now, decision)
|
||||||
|
|||||||
@@ -5,10 +5,8 @@ package discriminator
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,7 +27,7 @@ func (dnsbl) check(ctx context.Context, rq *request, _ time.Time) *verdict {
|
|||||||
rq.noteListed(listedBy, "listed by a DNSBL zone")
|
rq.noteListed(listedBy, "listed by a DNSBL zone")
|
||||||
|
|
||||||
for _, zone := range listedBy {
|
for _, zone := range listedBy {
|
||||||
rq.addTag(config.DNSBLTagPrefix + strings.ToLower(zone))
|
rq.addTag("dnsbl=" + zone)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !rq.dnsblListed || rq.h.config.ReputationAction != deny {
|
if !rq.dnsblListed || rq.h.config.ReputationAction != deny {
|
||||||
|
|||||||
@@ -43,7 +43,6 @@ const (
|
|||||||
// country are looked up, so that a client they refuse is not looked up.
|
// country are looked up, so that a client they refuse is not looked up.
|
||||||
func beforeLookup() []heuristic {
|
func beforeLookup() []heuristic {
|
||||||
return []heuristic{
|
return []heuristic{
|
||||||
tagLists{},
|
|
||||||
denyNets{},
|
denyNets{},
|
||||||
activeBan{},
|
activeBan{},
|
||||||
}
|
}
|
||||||
@@ -54,6 +53,7 @@ func beforeLookup() []heuristic {
|
|||||||
// last, on the tag rule_files and core_rule_set can add.
|
// last, on the tag rule_files and core_rule_set can add.
|
||||||
func afterLookup() []heuristic {
|
func afterLookup() []heuristic {
|
||||||
return []heuristic{
|
return []heuristic{
|
||||||
|
tagLists{},
|
||||||
countries{},
|
countries{},
|
||||||
blocklists{},
|
blocklists{},
|
||||||
crowdSec{},
|
crowdSec{},
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/lookup"
|
"sneak.berlin/go/smallwebwaf/internal/lookup"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -67,17 +66,17 @@ func asnTag(asn string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
return config.ASNTagPrefix + strings.TrimPrefix(asn, "AS")
|
return "asn=" + strings.TrimPrefix(asn, "AS")
|
||||||
}
|
}
|
||||||
|
|
||||||
// countryTag returns the tag of country, a code as the lookup gives it, such
|
// countryTag returns the tag of country, a code as the lookup gives it, such
|
||||||
// as country=DE for DE, or "" for no country.
|
// as country=de for DE, or "" for no country.
|
||||||
func countryTag(country string) string {
|
func countryTag(country string) string {
|
||||||
if country == "" {
|
if country == "" {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
return config.CountryTagPrefix + country
|
return "country=" + strings.ToLower(country)
|
||||||
}
|
}
|
||||||
|
|
||||||
// addLookup adds answer, an answer about a client from the lookup
|
// addLookup adds answer, an answer about a client from the lookup
|
||||||
|
|||||||
@@ -212,8 +212,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 waf.New refuses is
|
// JSON or XML body over its limit. An exclusions file that waf.New refuses is
|
||||||
@@ -228,7 +227,6 @@ func NewCoreRuleSet(cfg *config.Config) (*waf.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)
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ const (
|
|||||||
addLookupHeaders = "SWW_ADD_LOOKUP_HEADERS"
|
addLookupHeaders = "SWW_ADD_LOOKUP_HEADERS"
|
||||||
tagDeny = "SWW_TAG_DENY"
|
tagDeny = "SWW_TAG_DENY"
|
||||||
// denyKP is the value of SWW_TAG_DENY that refuses fromKP's country.
|
// denyKP is the value of SWW_TAG_DENY that refuses fromKP's country.
|
||||||
denyKP = "country=KP"
|
denyKP = "country=kp"
|
||||||
allowedCountries = "SWW_EXCLUSIVELY_ALLOWED_COUNTRIES"
|
allowedCountries = "SWW_EXCLUSIVELY_ALLOWED_COUNTRIES"
|
||||||
banResponse = "SWW_BAN_RESPONSE"
|
banResponse = "SWW_BAN_RESPONSE"
|
||||||
limitBanDuration = "SWW_LIMIT_BAN_DURATION"
|
limitBanDuration = "SWW_LIMIT_BAN_DURATION"
|
||||||
|
|||||||
@@ -162,8 +162,8 @@ func TestRateLimitExemptPathsAreNeitherCountedNorRefused(t *testing.T) {
|
|||||||
// /assets/, and breaks the limit.
|
// /assets/, and breaks the limit.
|
||||||
s.request(client, "/assets", http.StatusForbidden, requestlog.ActionRateLimited)
|
s.request(client, "/assets", http.StatusForbidden, requestlog.ActionRateLimited)
|
||||||
|
|
||||||
// A ban, SWW_DENY_NETS and SWW_TAG_DENY still refuse a path under a
|
// A ban, SWW_DENY_NETS and the country lists still refuse a path
|
||||||
// prefix.
|
// under a prefix.
|
||||||
s.request(client, "/assets/app.js", http.StatusForbidden, requestlog.ActionBanned)
|
s.request(client, "/assets/app.js", http.StatusForbidden, requestlog.ActionBanned)
|
||||||
s.request(denied, "/assets/app.js", http.StatusForbidden, requestlog.ActionDenied)
|
s.request(denied, "/assets/app.js", http.StatusForbidden, requestlog.ActionDenied)
|
||||||
s.request(fromKP, "/assets/app.js",
|
s.request(fromKP, "/assets/app.js",
|
||||||
|
|||||||
@@ -168,7 +168,7 @@ func TestReputationRefusalNamesItsHeuristicAndWhatListedTheClient(t *testing.T)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBlocklistsComeAfterAllowedCountriesAndSkipAllowNets(t *testing.T) {
|
func TestBlocklistsComeAfterTheCountryListsAndSkipAllowNets(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
s, server, queue := startWithLookups(t, map[string]string{
|
s, server, queue := startWithLookups(t, map[string]string{
|
||||||
@@ -499,7 +499,7 @@ func TestReputationSourcesTagTheRequestWhateverTheirAction(t *testing.T) {
|
|||||||
func(server *discriminator.Server) {
|
func(server *discriminator.Server) {
|
||||||
loadVerdicts(server, map[string][]string{fromDE: {dnsblZone}})
|
loadVerdicts(server, map[string][]string{fromDE: {dnsblZone}})
|
||||||
},
|
},
|
||||||
[]string{"asn=64496", "country=DE", "dnsbl=" + dnsblZone},
|
[]string{"asn=64496", "country=de", "dnsbl=" + dnsblZone},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
abuseipdb,
|
abuseipdb,
|
||||||
@@ -507,7 +507,7 @@ func TestReputationSourcesTagTheRequestWhateverTheirAction(t *testing.T) {
|
|||||||
func(server *discriminator.Server) {
|
func(server *discriminator.Server) {
|
||||||
loadScores(server, map[string]int64{fromDE: 80})
|
loadScores(server, map[string]int64{fromDE: 80})
|
||||||
},
|
},
|
||||||
[]string{abuseipdb, "asn=64496", "country=DE"},
|
[]string{abuseipdb, "asn=64496", "country=de"},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
|||||||
@@ -138,9 +138,11 @@ func (h *handler) startRequest(
|
|||||||
return rq
|
return rq
|
||||||
}
|
}
|
||||||
|
|
||||||
// addTag adds tag to the request's tags, which stay sorted and hold each tag
|
// addTag adds tag, in lower case, to the request's tags, which stay sorted and
|
||||||
// once.
|
// hold each tag once.
|
||||||
func (rq *request) addTag(tag string) {
|
func (rq *request) addTag(tag string) {
|
||||||
|
tag = strings.ToLower(tag)
|
||||||
|
|
||||||
at, found := slices.BinarySearch(rq.line.Tags, tag)
|
at, found := slices.BinarySearch(rq.line.Tags, tag)
|
||||||
if !found {
|
if !found {
|
||||||
rq.line.Tags = slices.Insert(rq.line.Tags, at, tag)
|
rq.line.Tags = slices.Insert(rq.line.Tags, at, tag)
|
||||||
|
|||||||
@@ -36,8 +36,9 @@ func TestAllowNetsSkipEveryCheck(t *testing.T) {
|
|||||||
rateLimitPerMinute: "1",
|
rateLimitPerMinute: "1",
|
||||||
})
|
})
|
||||||
|
|
||||||
// Neither SWW_DENY_NETS, SWW_TAG_DENY nor the limit of one request a
|
// Neither SWW_DENY_NETS, the country lists nor the limit of one
|
||||||
// minute refuses the client, and its country is not looked up.
|
// request a minute refuses the client, and its country is not looked
|
||||||
|
// up.
|
||||||
wantAnswers(t, addr, out, []sentRequest{
|
wantAnswers(t, addr, out, []sentRequest{
|
||||||
{fromKP, http.StatusOK, requestlog.ActionForward},
|
{fromKP, http.StatusOK, requestlog.ActionForward},
|
||||||
{fromKP, http.StatusOK, requestlog.ActionForward},
|
{fromKP, http.StatusOK, requestlog.ActionForward},
|
||||||
@@ -146,8 +147,8 @@ func TestRateLimitExemptNetsAreNeitherCountedNorRefused(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// listedAddr's requests are neither refused nor counted, so the first
|
// listedAddr's requests are neither refused nor counted, so the first
|
||||||
// request from unlistedAddr is within the limit of one a minute.
|
// request from unlistedAddr is within the limit of one a minute. The
|
||||||
// SWW_TAG_DENY still refuses an exempt client.
|
// country lists still refuse an exempt client.
|
||||||
wantAnswers(t, addr, out, []sentRequest{
|
wantAnswers(t, addr, out, []sentRequest{
|
||||||
{listedAddr, http.StatusOK, requestlog.ActionForward},
|
{listedAddr, http.StatusOK, requestlog.ActionForward},
|
||||||
{listedAddr, http.StatusOK, requestlog.ActionForward},
|
{listedAddr, http.StatusOK, requestlog.ActionForward},
|
||||||
|
|||||||
@@ -10,13 +10,11 @@ import (
|
|||||||
func TestTagDenyRefusesALookupTagAndNamesTheTag(t *testing.T) {
|
func TestTagDenyRefusesALookupTagAndNamesTheTag(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// The setting takes a country code in either case; the request's tag is
|
s, _, _ := startWithLookups(t, map[string]string{tagDeny: "Country=KP"})
|
||||||
// in capitals.
|
|
||||||
s, _, _ := startWithLookups(t, map[string]string{tagDeny: "Country=kp"})
|
|
||||||
|
|
||||||
line := s.get(fromKP, http.StatusForbidden, requestlog.ActionDenied)
|
line := s.get(fromKP, http.StatusForbidden, requestlog.ActionDenied)
|
||||||
wantHeuristic(t, line, "tag_deny", `{"tag": "country=KP"}`)
|
wantHeuristic(t, line, "tag_deny", `{"tag": "country=kp"}`)
|
||||||
wantTags(t, line, "asn=64511", "country=KP")
|
wantTags(t, line, "asn=64511", "country=kp")
|
||||||
|
|
||||||
s.get(fromDE, http.StatusOK, requestlog.ActionForward)
|
s.get(fromDE, http.StatusOK, requestlog.ActionForward)
|
||||||
}
|
}
|
||||||
@@ -46,22 +44,6 @@ func TestTagDenyRefusesAListTagBeforeTheRateLimitsCount(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTagDenyRefusesAMixedCaseZoneByItsLowerCaseTag(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
const mixedCaseZone = "DNSBL.example"
|
|
||||||
|
|
||||||
// The zone's tag is in lower case, however SWW_DNSBL_ZONES writes it.
|
|
||||||
s, server, _ := startWithLookups(t, map[string]string{
|
|
||||||
dnsblZones: mixedCaseZone, dnsblResolver: noResolver,
|
|
||||||
reputationAction: actionLog, tagDeny: "dnsbl=" + dnsblZone,
|
|
||||||
})
|
|
||||||
loadVerdicts(server, map[string][]string{fromDE: {mixedCaseZone}})
|
|
||||||
|
|
||||||
wantHeuristic(t, s.get(fromDE, http.StatusForbidden, requestlog.ActionDenied),
|
|
||||||
"tag_deny", `{"tag": "dnsbl=`+dnsblZone+`"}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTagDenyRefusesAnExploitSignatureOfARuleThatOnlyLogs(t *testing.T) {
|
func TestTagDenyRefusesAnExploitSignatureOfARuleThatOnlyLogs(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -91,31 +73,3 @@ func TestTagLimitPercentLowersALimitTheLowestWinning(t *testing.T) {
|
|||||||
wantPercent(t, "limit_percent", line.LimitPercent, line.LimitPercentSetting,
|
wantPercent(t, "limit_percent", line.LimitPercent, line.LimitPercentSetting,
|
||||||
"25 from "+tagLimitPercent)
|
"25 from "+tagLimitPercent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTagLimitPercentLowersAByteLimitByAListTag(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
s, server, _ := startWithLookups(t, map[string]string{
|
|
||||||
bytesLimitPerMinute: twoUploads,
|
|
||||||
tagLists: "vpn=" + vpnList,
|
|
||||||
tagLimitPercent: "vpn:50",
|
|
||||||
})
|
|
||||||
loadLists(t, server, map[string][]string{vpnList: {fromDE}})
|
|
||||||
|
|
||||||
// The upload's 100 bytes are over half of 199, 99.
|
|
||||||
line := s.uploadFrom(fromDE)
|
|
||||||
if line.LimitHit != minuteBytes {
|
|
||||||
t.Errorf("log line has limit_hit %q, want %s", line.LimitHit, minuteBytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
wantPercent(t, "bytes_percent", line.BytesPercent, line.BytesPercentSetting,
|
|
||||||
"50 from "+tagLimitPercent)
|
|
||||||
|
|
||||||
// fromKP, which the list does not hold, has the whole limit.
|
|
||||||
line = s.uploadFrom(fromKP)
|
|
||||||
if line.LimitHit != "" {
|
|
||||||
t.Errorf("log line for %s has limit_hit %q, want none", fromKP, line.LimitHit)
|
|
||||||
}
|
|
||||||
|
|
||||||
wantPercent(t, "bytes_percent", line.BytesPercent, line.BytesPercentSetting, none)
|
|
||||||
}
|
|
||||||
@@ -43,9 +43,9 @@ func TestLookupAndTagListsGiveTheRequestItsTagsSorted(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
wantTags(t, s.get(fromDE, http.StatusOK, requestlog.ActionForward),
|
wantTags(t, s.get(fromDE, http.StatusOK, requestlog.ActionForward),
|
||||||
"asn=64496", "country=DE", "torexit", "vpn=mullvad")
|
"asn=64496", "country=de", "torexit", "vpn=mullvad")
|
||||||
wantTags(t, s.get(fromKP, http.StatusOK, requestlog.ActionForward),
|
wantTags(t, s.get(fromKP, http.StatusOK, requestlog.ActionForward),
|
||||||
"asn=64511", "country=KP", "torexit")
|
"asn=64511", "country=kp", "torexit")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRequestWithoutATagLeavesTheFieldOut(t *testing.T) {
|
func TestRequestWithoutATagLeavesTheFieldOut(t *testing.T) {
|
||||||
@@ -69,32 +69,5 @@ func TestTagListsTagEvenARequestTheCountryListRefuses(t *testing.T) {
|
|||||||
loadLists(t, server, map[string][]string{vpnList: {fromKP}})
|
loadLists(t, server, map[string][]string{vpnList: {fromKP}})
|
||||||
|
|
||||||
wantTags(t, s.get(fromKP, http.StatusForbidden, requestlog.ActionCountryDenied),
|
wantTags(t, s.get(fromKP, http.StatusForbidden, requestlog.ActionCountryDenied),
|
||||||
"asn=64511", "country=KP", "vpn")
|
"asn=64511", "country=kp", "vpn")
|
||||||
}
|
|
||||||
|
|
||||||
func TestTagListsTagARequestRefusedBeforeTheLookup(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
s, server, _ := startWithLookups(t, map[string]string{
|
|
||||||
tagLists: "vpn=" + vpnList, denyNets: fromKP, rateLimitPerMinute: "1",
|
|
||||||
})
|
|
||||||
loadLists(t, server, map[string][]string{vpnList: {fromDE, fromKP}})
|
|
||||||
|
|
||||||
line := s.get(fromKP, http.StatusForbidden, requestlog.ActionDenied)
|
|
||||||
if line.Heuristic != "deny_nets" {
|
|
||||||
t.Errorf("log line has heuristic %q, want deny_nets", line.Heuristic)
|
|
||||||
}
|
|
||||||
|
|
||||||
wantTags(t, line, "vpn")
|
|
||||||
|
|
||||||
// The second request breaks the limit of one a minute, which bans the client.
|
|
||||||
s.get(fromDE, http.StatusOK, requestlog.ActionForward)
|
|
||||||
s.get(fromDE, http.StatusForbidden, requestlog.ActionRateLimited)
|
|
||||||
|
|
||||||
line = s.get(fromDE, http.StatusForbidden, requestlog.ActionBanned)
|
|
||||||
if line.Heuristic != "active_ban" {
|
|
||||||
t.Errorf("log line has heuristic %q, want active_ban", line.Heuristic)
|
|
||||||
}
|
|
||||||
|
|
||||||
wantTags(t, line, "vpn")
|
|
||||||
}
|
}
|
||||||
+4
-14
@@ -133,11 +133,8 @@ func (f *File) Watch(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
f.params.ProcessLog.Error(
|
f.params.ProcessLog.Error("cannot watch the lookup database for replacements",
|
||||||
"cannot watch the lookup database for replacements",
|
"error", err.Error())
|
||||||
"error",
|
|
||||||
err.Error(),
|
|
||||||
)
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -197,10 +194,7 @@ func (f *File) readAgain() {
|
|||||||
f.params.Alerts.Raise(alerts.Alert{
|
f.params.Alerts.Raise(alerts.Alert{
|
||||||
Event: alerts.EventFileError,
|
Event: alerts.EventFileError,
|
||||||
Reason: kept,
|
Reason: kept,
|
||||||
Detail: map[string]any{
|
Detail: map[string]any{"file": f.params.Path, "error": err.Error()},
|
||||||
"file": f.params.Path,
|
|
||||||
"error": err.Error(),
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
f.params.ProcessLog.Error(kept, "error", err.Error())
|
f.params.ProcessLog.Error(kept, "error", err.Error())
|
||||||
|
|
||||||
@@ -218,11 +212,7 @@ func (f *File) use(reader *maxminddb.Reader) {
|
|||||||
f.lastRead = f.params.Now()
|
f.lastRead = f.params.Now()
|
||||||
f.mu.Unlock()
|
f.mu.Unlock()
|
||||||
|
|
||||||
f.params.ProcessLog.Info(
|
f.params.ProcessLog.Info("read the lookup database", "file", f.params.Path)
|
||||||
"read the lookup database",
|
|
||||||
"file",
|
|
||||||
f.params.Path,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// read reads the lookup database at path. The whole file is read into
|
// read reads the lookup database at path. The whole file is read into
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ func (b *busiest) Collect(ch chan<- prometheus.Metric) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// vecs returns the metrics: by AS number, those of requests and bytes; by
|
// vecs returns the metrics: by AS number, those of requests and bytes; by
|
||||||
// country, the refusals by the country list as well.
|
// country, the refusals by the country lists as well.
|
||||||
func (b *busiest) vecs() []*prometheus.CounterVec {
|
func (b *busiest) vecs() []*prometheus.CounterVec {
|
||||||
vecs := []*prometheus.CounterVec{b.requests, b.requestBytes, b.responseBytes}
|
vecs := []*prometheus.CounterVec{b.requests, b.requestBytes, b.responseBytes}
|
||||||
if b.refused != nil {
|
if b.refused != nil {
|
||||||
|
|||||||
@@ -115,11 +115,11 @@ type list struct {
|
|||||||
failures int
|
failures int
|
||||||
}
|
}
|
||||||
|
|
||||||
// entries are what the lines of a copy say: for a blocklist or a tag list, the
|
// entries are what the lines of a copy say: for a blocklist, the netblocks
|
||||||
// netblocks it names, with the lengths among them, for the file of AS:percent
|
// it names, with the lengths among them, for the file of AS:percent lines,
|
||||||
// lines, the percentage it gives each AS number, and for the CrowdSec decision
|
// the percentage it gives each AS number, and for the CrowdSec decision
|
||||||
// list, the decision on each netblock that ends last, with the lengths among
|
// list, the decision on each netblock that ends last, with the lengths
|
||||||
// them.
|
// among them.
|
||||||
type entries struct {
|
type entries struct {
|
||||||
netblocks map[netip.Prefix]bool
|
netblocks map[netip.Prefix]bool
|
||||||
lengths []int
|
lengths []int
|
||||||
@@ -504,10 +504,10 @@ func (l *Lists) get(ctx context.Context, listURL string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parse reads the lines of the list at listURL, fetched at fetched: those
|
// parse reads the lines of the list at listURL, fetched at fetched: those
|
||||||
// of a blocklist or a tag list, of the file of AS:percent lines, or of the
|
// of a blocklist, of the file of AS:percent lines, or of the CrowdSec
|
||||||
// CrowdSec decision list. In all but the decision list, anything after a ; or
|
// decision list. In the first two, anything after a ; or a # on a line is
|
||||||
// a # on a line is left out, and so is a line left blank. Any other line that
|
// left out, and so is a line left blank. Any other line that does not read
|
||||||
// does not read is an error naming it by its number.
|
// is an error naming it by its number.
|
||||||
func (l *Lists) parse(
|
func (l *Lists) parse(
|
||||||
listURL string, lines []string, fetched time.Time,
|
listURL string, lines []string, fetched time.Time,
|
||||||
) (entries, error) {
|
) (entries, error) {
|
||||||
@@ -521,8 +521,8 @@ func (l *Lists) parse(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseNetblocks reads the lines of a blocklist or a tag list, each an address
|
// parseNetblocks reads a blocklist's lines, each an address or a netblock
|
||||||
// or a netblock as the settings take them.
|
// as the settings take them.
|
||||||
func parseNetblocks(lines []string) (entries, error) {
|
func parseNetblocks(lines []string) (entries, error) {
|
||||||
found := entries{netblocks: map[netip.Prefix]bool{}}
|
found := entries{netblocks: map[netip.Prefix]bool{}}
|
||||||
|
|
||||||
|
|||||||
@@ -19,34 +19,27 @@ const (
|
|||||||
// ActionForward is a request discriminator allowed, which rproxy passes to
|
// ActionForward is a request discriminator allowed, which rproxy passes to
|
||||||
// the app.
|
// the app.
|
||||||
ActionForward = "forward"
|
ActionForward = "forward"
|
||||||
|
|
||||||
// ActionTooLarge is a request over the header limits.
|
// ActionTooLarge is a request over the header limits.
|
||||||
ActionTooLarge = "too_large"
|
ActionTooLarge = "too_large"
|
||||||
|
|
||||||
// ActionRateLimited is a request refused because it took its client
|
// ActionRateLimited is a request refused because it took its client
|
||||||
// over a rate limit, which bans the client.
|
// over a rate limit, which bans the client.
|
||||||
ActionRateLimited = "rate_limited"
|
ActionRateLimited = "rate_limited"
|
||||||
|
|
||||||
// ActionBanned is a request refused because a ban covers its client,
|
// ActionBanned is a request refused because a ban covers its client,
|
||||||
// or because it matched a ban rule, asked for a trap path or the
|
// or because it matched a ban rule, asked for a trap path or the
|
||||||
// CrowdSec decision list lists its client, each of which bans the
|
// CrowdSec decision list lists its client, each of which bans the
|
||||||
// client.
|
// client.
|
||||||
ActionBanned = "banned"
|
ActionBanned = "banned"
|
||||||
|
|
||||||
// ActionRuleBlocked is a request refused because it matched a block
|
// ActionRuleBlocked is a request refused because it matched a block
|
||||||
// rule.
|
// rule.
|
||||||
ActionRuleBlocked = "rule_blocked"
|
ActionRuleBlocked = "rule_blocked"
|
||||||
|
|
||||||
// ActionWAFBlocked is a request refused because the Core Rule Set
|
// ActionWAFBlocked is a request refused because the Core Rule Set
|
||||||
// scored it at or over SWW_WAF_ANOMALY_THRESHOLD.
|
// scored it at or over SWW_WAF_ANOMALY_THRESHOLD.
|
||||||
ActionWAFBlocked = "waf_blocked"
|
ActionWAFBlocked = "waf_blocked"
|
||||||
|
|
||||||
// ActionDenied is a request refused because its client is in
|
// ActionDenied is a request refused because its client is in
|
||||||
// SWW_DENY_NETS, in a blocklist while SWW_BLOCKLIST_ACTION is deny,
|
// SWW_DENY_NETS, in a blocklist while SWW_BLOCKLIST_ACTION is deny,
|
||||||
// or listed by a DNSBL zone, or scored a hit by AbuseIPDB, while
|
// or listed by a DNSBL zone, or scored a hit by AbuseIPDB, while
|
||||||
// SWW_REPUTATION_ACTION is deny, or that carries a tag in SWW_TAG_DENY.
|
// SWW_REPUTATION_ACTION is deny, or that carries a tag in SWW_TAG_DENY.
|
||||||
ActionDenied = "denied"
|
ActionDenied = "denied"
|
||||||
|
|
||||||
// ActionCountryDenied is a request refused because its client's country
|
// ActionCountryDenied is a request refused because its client's country
|
||||||
// is not in SWW_EXCLUSIVELY_ALLOWED_COUNTRIES.
|
// is not in SWW_EXCLUSIVELY_ALLOWED_COUNTRIES.
|
||||||
ActionCountryDenied = "country_denied"
|
ActionCountryDenied = "country_denied"
|
||||||
@@ -265,11 +258,7 @@ func FormatTime(t time.Time) string {
|
|||||||
|
|
||||||
// Milliseconds is d in milliseconds, to the microsecond.
|
// Milliseconds is d in milliseconds, to the microsecond.
|
||||||
func Milliseconds(d time.Duration) float64 {
|
func Milliseconds(d time.Duration) float64 {
|
||||||
return float64(
|
return float64(d.Microseconds()) / float64(time.Millisecond/time.Microsecond)
|
||||||
d.Microseconds(),
|
|
||||||
) / float64(
|
|
||||||
time.Millisecond/time.Microsecond,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewProcessLogger returns the logger for the process's own messages:
|
// NewProcessLogger returns the logger for the process's own messages:
|
||||||
@@ -277,25 +266,17 @@ func Milliseconds(d time.Duration) float64 {
|
|||||||
// as a request line's, and instanceName, SWW_INSTANCE_NAME, as instance.
|
// as a request line's, and instanceName, SWW_INSTANCE_NAME, as instance.
|
||||||
// It writes only the messages at level, SWW_LOG_LEVEL, or more severe;
|
// It writes only the messages at level, SWW_LOG_LEVEL, or more severe;
|
||||||
// the request lines Write writes are never held back.
|
// the request lines Write writes are never held back.
|
||||||
func NewProcessLogger(
|
func NewProcessLogger(w io.Writer, instanceName string, level slog.Level) *slog.Logger {
|
||||||
w io.Writer,
|
|
||||||
instanceName string,
|
|
||||||
level slog.Level,
|
|
||||||
) *slog.Logger {
|
|
||||||
handler := slog.NewJSONHandler(w, &slog.HandlerOptions{
|
handler := slog.NewJSONHandler(w, &slog.HandlerOptions{
|
||||||
Level: level,
|
Level: level,
|
||||||
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
|
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
|
||||||
if attr.Key == slog.TimeKey && len(groups) == 0 {
|
if attr.Key == slog.TimeKey && len(groups) == 0 {
|
||||||
return slog.String(
|
return slog.String(slog.TimeKey, FormatTime(attr.Value.Time()))
|
||||||
slog.TimeKey,
|
|
||||||
FormatTime(attr.Value.Time()),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return attr
|
return attr
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
return slog.New(handler).
|
return slog.New(handler).With("type", "process", "instance", instanceName)
|
||||||
With("type", "process", "instance", instanceName)
|
|
||||||
}
|
}
|
||||||
@@ -618,7 +618,7 @@ func TestLookupDatabaseReplacedWhileRunningTakesEffect(t *testing.T) {
|
|||||||
instanceName: instance,
|
instanceName: instance,
|
||||||
lookupSource: "file",
|
lookupSource: "file",
|
||||||
lookupDBPath: path,
|
lookupDBPath: path,
|
||||||
"SWW_TAG_DENY": "country=KP",
|
"SWW_TAG_DENY": "country=kp",
|
||||||
// The requests sent until a replacement takes effect, and those
|
// The requests sent until a replacement takes effect, and those
|
||||||
// for the metrics, must not break a rate limit, whose ban would
|
// for the metrics, must not break a rate limit, whose ban would
|
||||||
// refuse them too.
|
// refuse them too.
|
||||||
|
|||||||
+7
-52
@@ -1,51 +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.
|
||||||
|
|
||||||
# A request whose hostname SWW_APPS does not list gitea for skips every rule
|
|
||||||
# below. Inspect sets tx.app_gitea on the others.
|
|
||||||
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:
|
||||||
@@ -198,8 +155,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$" \
|
||||||
@@ -324,5 +281,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
|
|
||||||
+57
-101
@@ -1,9 +1,9 @@
|
|||||||
// Package waf runs the OWASP Core Rule Set 4.25.0, through Coraza, on the
|
// Package waf 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 eight 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,8 @@ 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, and
|
||||||
// directives of every app in apps for the third.
|
// giteaExclusions for the third.
|
||||||
// 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 +81,44 @@ 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 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,36 +168,18 @@ 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 the rest unless Inspect sets the
|
|
||||||
// transaction variable app_ and the app's name, and they end with the
|
|
||||||
// marker it skips to.
|
|
||||||
directives string
|
|
||||||
// cookies are the cookies the Core Rule Set reads a request without.
|
|
||||||
cookies []string
|
|
||||||
}
|
|
||||||
|
|
||||||
//go:embed gitea.conf
|
|
||||||
var giteaDirectives 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
|
||||||
"gitea": {
|
|
||||||
directives: giteaDirectives,
|
|
||||||
cookies: []string{"gitea_flash", "redirect_to"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// AppNames returns the names of the apps whose fixes sww has, in
|
// cookiesNotRead are the cookies the Core Rule Set reads a request
|
||||||
// alphabetical order.
|
// without, the rest of the fifth change.
|
||||||
func AppNames() []string {
|
//
|
||||||
return slices.Sorted(maps.Keys(apps))
|
//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 {
|
||||||
@@ -199,10 +195,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
|
||||||
@@ -294,15 +286,13 @@ func checkExclusions(exclusions string) error {
|
|||||||
type CoreRuleSet struct {
|
type CoreRuleSet struct {
|
||||||
waf coraza.WAF
|
waf 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 eight 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 {
|
||||||
@@ -325,13 +315,8 @@ 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)
|
||||||
|
|
||||||
if len(params.DisabledRules) > 0 {
|
if len(params.DisabledRules) > 0 {
|
||||||
ids := make([]string, len(params.DisabledRules))
|
ids := make([]string, len(params.DisabledRules))
|
||||||
@@ -349,7 +334,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{waf: waf, bodyLimit: params.BodyLimit, apps: params.Apps}, nil
|
return &CoreRuleSet{waf: waf, bodyLimit: params.BodyLimit}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Result is what the Core Rule Set found in a request.
|
// Result is what the Core Rule Set found in a request.
|
||||||
@@ -361,12 +346,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) {
|
||||||
@@ -374,24 +358,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
|
||||||
}
|
}
|
||||||
@@ -484,26 +457,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, ";") {
|
||||||
|
|||||||
@@ -120,8 +120,8 @@ func atDefaults(t *testing.T) *waf.CoreRuleSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// wantChange checks that crs lets through passes, a gitea request one of
|
// wantChange checks that crs lets through passes, a gitea request one of
|
||||||
// the six changes or gitea's fixes are for, and still finds result in
|
// the eight changes is for, and still finds result in refused, a request
|
||||||
// refused, a request like it that they are not for.
|
// like it that the change is not for.
|
||||||
func wantChange(
|
func wantChange(
|
||||||
t *testing.T, crs *waf.CoreRuleSet, passes, refused request, result waf.Result,
|
t *testing.T, crs *waf.CoreRuleSet, passes, refused request, result waf.Result,
|
||||||
) {
|
) {
|
||||||
@@ -381,96 +381,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 waf.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"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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) *waf.CoreRuleSet {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
crs, err := waf.New(waf.Params{
|
|
||||||
ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, Apps: apps,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("load the Core Rule Set: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return crs
|
|
||||||
}
|
|
||||||
|
|
||||||
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), waf.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), waf.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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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) (*waf.CoreRuleSet, error) {
|
func withExclusions(path string) (*waf.CoreRuleSet, error) {
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
# script/publish: build the image on the Docker host and push it to the
|
|
||||||
# registry under four tags: the UTC date (YYYY-MM-DD), the short commit
|
|
||||||
# hash, the branch name and latest. The build is script/docker's, so the
|
|
||||||
# tests and the linter run first. The host and the registry name are
|
|
||||||
# environment variables with defaults, for running it against another.
|
|
||||||
#
|
|
||||||
# The working tree must be clean: the hash tag names a commit, and an
|
|
||||||
# image built from uncommitted changes would carry a hash that does not
|
|
||||||
# describe it.
|
|
||||||
set -eu
|
|
||||||
|
|
||||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
|
|
||||||
ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"
|
|
||||||
|
|
||||||
: "${DOCKER_HOST:=ssh://root@fsn1app1}"
|
|
||||||
: "${PUBLISH_IMAGE:=git.eeqj.de/sneak/smallwebwaf}"
|
|
||||||
export DOCKER_HOST
|
|
||||||
|
|
||||||
main() {
|
|
||||||
cd "$ROOT"
|
|
||||||
|
|
||||||
# Own line: a failing command substitution inside an argument does
|
|
||||||
# not trip `set -e`.
|
|
||||||
changes="$(git status --porcelain)"
|
|
||||||
if [ -n "$changes" ]; then
|
|
||||||
echo "script/publish: the working tree is not clean; commit or stash first" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
date="$(date -u +%Y-%m-%d)"
|
|
||||||
hash="$(git rev-parse --short HEAD)"
|
|
||||||
branch="$(git rev-parse --abbrev-ref HEAD)"
|
|
||||||
if [ "$branch" = "HEAD" ]; then
|
|
||||||
echo "script/publish: detached HEAD; check out a branch first" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
# A tag is [A-Za-z0-9_.-]; a branch name such as feature/x is not.
|
|
||||||
branch="$(printf '%s' "$branch" | tr -c 'A-Za-z0-9_.-' '-')"
|
|
||||||
|
|
||||||
local_tag="$("$SCRIPT_DIR/projectname")"
|
|
||||||
"$SCRIPT_DIR/docker"
|
|
||||||
|
|
||||||
for tag in "$date" "$hash" "$branch" latest; do
|
|
||||||
docker tag "$local_tag" "$PUBLISH_IMAGE:$tag"
|
|
||||||
done
|
|
||||||
for tag in "$date" "$hash" "$branch" latest; do
|
|
||||||
docker push "$PUBLISH_IMAGE:$tag"
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "pushed $PUBLISH_IMAGE with tags: $date $hash $branch latest"
|
|
||||||
}
|
|
||||||
|
|
||||||
main "$@"
|
|
||||||
Reference in new issue
Block a user