Compare commits
16
Commits
27586c249b
..
next
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ba79d6de02 | ||
|
|
ed5125606a | ||
|
|
847f5c6fec | ||
|
|
142f0a1c4b | ||
|
|
72d8f2c12b | ||
|
|
f42c6214a0 | ||
|
|
a3d9bbfd81 | ||
|
|
bc8aa6bb96 | ||
|
|
bc7ef8240b | ||
|
|
8afd873813 | ||
|
|
c12668226f | ||
|
|
4221f71920 | ||
|
|
254873a656 | ||
|
|
0b63384423 | ||
|
|
c4fc156a8c | ||
|
|
12a232ee1b |
No files matched your search
@@ -1,55 +1,73 @@
|
||||
# AGENTS.md — Rules for AI Agents
|
||||
# Rules for coding agents
|
||||
|
||||
## Before Every Commit
|
||||
This file holds the rules for coding agents working in this repository.
|
||||
`CONVENTIONS.md` defines the architecture. `REPO_POLICIES.md` defines the
|
||||
repository standards. Both are copies of shared documents. Do not edit them
|
||||
here.
|
||||
|
||||
1. **Format**: `make fmt`
|
||||
2. **Check**: `make check` — the tests and the linter, which run in Docker, and
|
||||
the format check: all passing, zero issues
|
||||
## Before every commit
|
||||
|
||||
Never run `go test`, `golangci-lint` or `gofmt` directly; use the `make`
|
||||
targets.
|
||||
1. Run `make fmt`.
|
||||
2. Run `make check`. It runs the tests and the linter in Docker, then the format
|
||||
check. All three must pass with no findings.
|
||||
|
||||
No commit lands on main with lint errors, test failures, or formatting issues.
|
||||
Never run `go test`, `golangci-lint` or `gofmt` directly. Use the `make` targets
|
||||
instead, such as `make test` and `make lint`.
|
||||
|
||||
## Code Style
|
||||
## Branches and pull requests
|
||||
|
||||
- Follow CONVENTIONS.md exactly — it defines the architecture
|
||||
- All exported types, functions, methods, and vars must have doc comments
|
||||
- All packages must have a package doc comment
|
||||
- JSON tags use camelCase (not snake_case)
|
||||
- No magic numbers — use named constants
|
||||
- No inline error handling (`if err := ...; err != nil`) — use plain assignment
|
||||
- Blank line before every `return` statement
|
||||
- Blank line separating embedded fields from regular fields in structs
|
||||
- Blank imports must have a justification comment
|
||||
- Never commit to `main` or `next` directly. Every change is a pull request from
|
||||
a feature branch into `next`.
|
||||
- Only the owner merges `next` into `main`.
|
||||
- A pull request that closes an issue has the title `Description (closes #N)`.
|
||||
- One logical change per commit.
|
||||
- Commit messages are in the imperative mood and short.
|
||||
|
||||
## Git Workflow
|
||||
## Code style
|
||||
|
||||
- **Never commit directly to main** — all changes go on feature branches
|
||||
- Merge to main only when ready and passing all checks
|
||||
- PR titles: `Description (closes #N)` when closing an issue
|
||||
- One logical change per commit
|
||||
- Commit messages: imperative mood, concise
|
||||
- Follow `CONVENTIONS.md`.
|
||||
- Every package has a package doc comment. Test files need none.
|
||||
- Every exported type, function and variable has a doc comment. Test files need
|
||||
none.
|
||||
- JSON tags are camelCase. The HTTP API's `last_id`, `pow_token` and
|
||||
`hashcash_bits` stay as they are, each with a `//nolint:tagliatelle` comment.
|
||||
- Prefer named constants to magic numbers. The `mnd` linter checks for them
|
||||
outside test files.
|
||||
- No inline error handling such as `if err := f(); err != nil`. Assign the error
|
||||
on its own line, then test it.
|
||||
- A `return` that follows another statement in its block has a blank line before
|
||||
it.
|
||||
- A blank line between the embedded fields of a struct and its other fields.
|
||||
- Every blank import has a comment saying why it is there. Test files need none.
|
||||
|
||||
## Architecture
|
||||
|
||||
- Dependency injection via Uber fx — all components are fx providers
|
||||
- `internal/` for all application packages — one package per concern
|
||||
- Handlers return `http.HandlerFunc` using the closure pattern
|
||||
- Request/response structs defined inside handler closures
|
||||
- Models embed `models.Base` for database access to relation methods
|
||||
- Database migrations in `internal/db/schema/` — embedded via `go:embed`
|
||||
- SQLite by default (pure Go driver: modernc.org/sqlite)
|
||||
- The server uses `go.uber.org/fx` for dependency injection.
|
||||
`cmd/neoircd/main.go` lists its `fx` providers, such as `db.New` and
|
||||
`handlers.New`.
|
||||
- Application packages live under `internal/`, one package per concern.
|
||||
- `pkg/irc` holds the IRC command names and numeric reply codes.
|
||||
- The `web` package embeds the built web client.
|
||||
- Each exported `Handle…` method of `Handlers` returns an `http.HandlerFunc`.
|
||||
- A request struct is defined inside the method that decodes it.
|
||||
- The server uses the database only through the methods of `db.Database`.
|
||||
- The database migrations live in `internal/db/schema/` and are embedded with
|
||||
`go:embed`.
|
||||
- The database is SQLite, through the pure Go driver `modernc.org/sqlite`.
|
||||
|
||||
## Testing
|
||||
|
||||
- Table-driven tests preferred
|
||||
- Use `testing.T` helpers, not assertion libraries
|
||||
- Test files live next to the code they test
|
||||
- `_test.go` suffix, same package for white-box testing
|
||||
- Prefer table-driven tests.
|
||||
- Use `testing.T` helpers, not assertion libraries.
|
||||
- A test file lives next to the code it tests and ends in `_test.go`.
|
||||
- The linter requires each test file to be in the package's external test
|
||||
package, such as `handlers_test`. It allows `export_test.go` and files ending
|
||||
in `_internal_test.go` in the package itself.
|
||||
- An `export_test.go` file gives the external tests access to the package's
|
||||
unexported code.
|
||||
|
||||
## Dependencies
|
||||
|
||||
- Required libraries are listed in CONVENTIONS.md §1
|
||||
- No new dependencies without justification
|
||||
- Pure Go preferred (no CGO)
|
||||
- The required libraries are listed in `CONVENTIONS.md` §1.
|
||||
- No new dependency without a reason given in the pull request.
|
||||
- Prefer dependencies written in pure Go, which build without cgo.
|
||||
+15
-10
@@ -33,14 +33,18 @@ RUN golangci-lint run --config .golangci.yml ./...
|
||||
# golang:1.24.13-bookworm, 2026-10-06
|
||||
FROM golang@sha256:1a6d4452c65dea36aac2e2d606b01b4a029ec90cc1ae53890540ce6173ea77ac AS test
|
||||
WORKDIR /src
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
COPY . .
|
||||
RUN mkdir -p web/dist && touch web/dist/index.html web/dist/style.css web/dist/app.js
|
||||
# -p 4 because test runs on a shared build host cap their parallelism.
|
||||
RUN go test -p 4 -timeout 90s -race -cover ./... || \
|
||||
# The modules download in this RUN rather than a layer of their own, because
|
||||
# exporting a layer that holds them is slow. Nothing reads them or the build
|
||||
# cache afterwards, so go clean removes both.
|
||||
RUN set -e; \
|
||||
go mod download; \
|
||||
go test -p 4 -timeout 90s -race -cover ./... || \
|
||||
{ echo "--- Rerunning with -v for details ---"; \
|
||||
go test -p 4 -timeout 90s -race -v ./...; exit 1; }
|
||||
go test -p 4 -timeout 90s -race -v ./...; exit 1; }; \
|
||||
go clean -cache -modcache
|
||||
|
||||
# Build stage. Nothing is wanted from either phase above; the copies
|
||||
# are what make BuildKit build them first, so this stage cannot run
|
||||
@@ -79,14 +83,15 @@ RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /neoirc-cli ./cmd/neoir
|
||||
# stage's chain and nothing else.
|
||||
# alpine:3.21, 2026-02-26
|
||||
FROM alpine@sha256:c3f8e73fdb79deaebaa2037150150191b9dcbfba68b4a46d70103204c53f4709
|
||||
RUN apk add --no-cache ca-certificates \
|
||||
&& addgroup -S neoirc && adduser -S neoirc -G neoirc \
|
||||
&& mkdir -p /var/lib/neoirc \
|
||||
&& chown neoirc:neoirc /var/lib/neoirc
|
||||
# su-exec 0.2-r3, 2026-11-10
|
||||
RUN apk add --no-cache ca-certificates su-exec=0.2-r3 \
|
||||
&& addgroup -S neoirc && adduser -S neoirc -G neoirc
|
||||
COPY --from=builder /neoircd /usr/local/bin/neoircd
|
||||
COPY deploy/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
USER neoirc
|
||||
# No USER: the entrypoint starts as root to give /var/lib/neoirc to neoirc,
|
||||
# then runs neoircd as neoirc.
|
||||
EXPOSE 8080 6667
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD wget -qO- http://localhost:8080/.well-known/healthcheck.json || exit 1
|
||||
ENTRYPOINT ["neoircd"]
|
||||
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
|
||||
@@ -168,8 +168,10 @@ either.
|
||||
- Request bodies for `POST /api/v1/session`, `POST /api/v1/login` and
|
||||
`POST /api/v1/messages` are limited to `MAX_MESSAGE_SIZE` bytes (default
|
||||
`4096`). A larger body gets `400` `invalid request body`.
|
||||
- The server allows cross-origin requests from any origin, with credentials. The
|
||||
cookie is `SameSite=Strict`, so a browser sends it only from the same site.
|
||||
- `GET /api/v1/server` and `GET /.well-known/healthcheck.json` answer a request
|
||||
from any origin with `Access-Control-Allow-Origin: *`. Every other route sends
|
||||
no `Access-Control-Allow-*` header, so a page on another origin cannot read
|
||||
its responses.
|
||||
|
||||
### The auth cookie
|
||||
|
||||
@@ -186,8 +188,8 @@ of the response does not repeat it. A browser sends the cookie by itself. Any
|
||||
other client must save it and send it back, as `curl -c` and `curl -b` do.
|
||||
|
||||
The cookie is `Secure`. Chrome, Firefox, `curl` and `neoirc-cli` send it to a
|
||||
server on `localhost` over plain HTTP too; Python's `requests` does not. To any
|
||||
other host they send it only over HTTPS, which a reverse proxy provides.
|
||||
server on `localhost` over plain HTTP too; Python's `requests` does not. To a
|
||||
remote host they send it only over HTTPS, which a reverse proxy provides.
|
||||
|
||||
`QUIT`, `POST /api/v1/logout`, `KILL` and the idle cleanup invalidate the
|
||||
cookie. After that, every request with it gets `401`.
|
||||
@@ -984,9 +986,9 @@ HTTP response: `{"status": "ok"}`.
|
||||
|
||||
The server adds a missing `#`. The reply is `RPL_NAMREPLY` (`353`), when the
|
||||
channel has members, then `RPL_ENDOFNAMES` (`366`). `RPL_NAMREPLY` has `params`
|
||||
`["=", channel]` and a `body` with one string: the members' hostmasks in nick
|
||||
order, separated by spaces, each with `@` before a channel operator and `+`
|
||||
before a voiced member:
|
||||
`["=", channel]` and a `body` with one string. That string holds the members'
|
||||
hostmasks in nick order, separated by spaces. A `@` before a hostmask marks a
|
||||
channel operator, and a `+` marks a voiced member:
|
||||
|
||||
```json
|
||||
{
|
||||
@@ -1339,8 +1341,9 @@ Every session has a hostmask in IRC's form, `nick!username@hostname`:
|
||||
from, or that IP address when the lookup gives no name. The lookup waits up to
|
||||
three seconds.
|
||||
|
||||
Over the HTTP API, the server takes the client's IP address from the first entry
|
||||
of the `X-Forwarded-For` header, then `X-Real-IP`, then the TCP connection (see
|
||||
Over the HTTP API, the client's IP address is the address of the TCP connection.
|
||||
When that connection comes from a trusted proxy, the server reads the address
|
||||
from `X-Forwarded-For` or `X-Real-IP` instead (see
|
||||
[Reverse proxy](#reverse-proxy)).
|
||||
|
||||
Every user can see a hostmask: in `RPL_WHOISUSER` (`311`), `RPL_WHOREPLY`
|
||||
@@ -1450,10 +1453,9 @@ over the limit gets `429` `too many login attempts, try again later` with
|
||||
attempt is more than 15 minutes old. Session creation has no rate limit; the
|
||||
hashcash stamp is its cost.
|
||||
|
||||
The IP address comes from the first entry of `X-Forwarded-For`, else from
|
||||
`X-Real-IP`, else from the TCP connection. Without a reverse proxy that
|
||||
overwrites those headers, a client can set them itself and so pick any address
|
||||
(see [Reverse proxy](#reverse-proxy)).
|
||||
The IP address is the client's address as described in
|
||||
[Reverse proxy](#reverse-proxy). A client outside the trusted proxies cannot
|
||||
change it with `X-Forwarded-For` or `X-Real-IP`.
|
||||
|
||||
### Writing a client
|
||||
|
||||
@@ -1637,9 +1639,9 @@ They behave as over the HTTP API, with the IRC syntax and these differences:
|
||||
- `KICK` without a reason uses the kicked user's nick as the reason.
|
||||
- `MOTD` sends only `375` to `376`, or `ERR_NOMOTD` (`422`) when the message of
|
||||
the day is empty, without the welcome and `LUSERS` replies.
|
||||
- `PASS` after registration answers a password of the wrong length with a
|
||||
`NOTICE`, not `ERR_NEEDMOREPARAMS` (`461`), and a stored password with a
|
||||
`NOTICE` too.
|
||||
- `PASS` after registration answers with a `NOTICE` when it stores the password.
|
||||
A password of the wrong length also gets a `NOTICE`, not `ERR_NEEDMOREPARAMS`
|
||||
(`461`).
|
||||
|
||||
### What IRC clients see
|
||||
|
||||
@@ -1714,11 +1716,16 @@ Every setting is an environment variable. The server also reads a `.env` file in
|
||||
its working directory, and a YAML file `neoirc.yaml` in `/etc/neoirc/` or
|
||||
`$HOME/.config/neoirc/` with the same names as keys. A variable from the
|
||||
environment wins over one in `.env`, and both win over `neoirc.yaml`. An
|
||||
environment variable set to the empty string counts as unset, except
|
||||
`IRC_LISTEN_ADDR`, where the empty string turns the IRC listener off. A
|
||||
`neoirc.yaml` that is not valid YAML stops the server at startup. A setting that
|
||||
does not parse or is out of range also stops the server at startup, and the
|
||||
error names the setting.
|
||||
environment variable set to the empty string counts as unset, with two
|
||||
exceptions:
|
||||
|
||||
- For `IRC_LISTEN_ADDR`, the empty string turns the IRC listener off.
|
||||
- For `TRUSTED_PROXIES`, the empty string trusts no proxy.
|
||||
|
||||
A `neoirc.yaml` that is not valid YAML stops the server at startup. A setting
|
||||
that does not parse or is out of range also stops the server at startup, and the
|
||||
error names the setting. The same holds for a setting given a list or a map in
|
||||
`neoirc.yaml`.
|
||||
|
||||
- `PORT` (default `8080`): the HTTP port. `0` lets the system pick a free port.
|
||||
- `IRC_LISTEN_ADDR` (default `:6667`): the address of the IRC listener. The
|
||||
@@ -1747,6 +1754,11 @@ error names the setting.
|
||||
address. A fraction such as `0.5` is allowed. It must be above `0`.
|
||||
- `LOGIN_RATE_BURST` (default `5`): how many attempts an address may make at
|
||||
once before the rate applies. It must be at least `1`.
|
||||
- `TRUSTED_PROXIES` (default `10.0.0.0/8,172.16.0.0/12,192.168.0.0/16`): the
|
||||
reverse proxies whose `X-Forwarded-For` and `X-Real-IP` headers the server
|
||||
believes. It is a comma-separated list of networks, such as `10.0.0.0/8`, and
|
||||
single addresses, such as `192.0.2.1`. A list replaces the default entirely.
|
||||
The empty string trusts nothing. See [Reverse proxy](#reverse-proxy).
|
||||
- `SESSION_IDLE_TIMEOUT` (default `720h`, 30 days): a duration with a unit, `h`
|
||||
for hours, `m` for minutes or `s` for seconds, such as `720h` or `36h30m`. A
|
||||
client with no request for this long is removed, and a session left with no
|
||||
@@ -1781,13 +1793,17 @@ NEOIRC_HASHCASH_BITS=20
|
||||
the linter first and stops when either fails. The image:
|
||||
|
||||
- is Alpine with the `neoircd` binary and CA certificates;
|
||||
- runs as the user `neoirc`;
|
||||
- runs `neoircd` as the user `neoirc`;
|
||||
- exposes ports 8080 and 6667;
|
||||
- keeps its database in `/var/lib/neoirc`;
|
||||
- has a `HEALTHCHECK` that calls `/.well-known/healthcheck.json` every 30
|
||||
seconds.
|
||||
|
||||
Run it with a volume for the database and the settings as environment variables:
|
||||
The image prepares `/var/lib/neoirc` itself on every start. It creates the
|
||||
directory, gives it to `neoirc` and then runs `neoircd` as `neoirc`.
|
||||
|
||||
Run it with a named volume for the database and the settings as environment
|
||||
variables:
|
||||
|
||||
```bash
|
||||
docker run -d --name neoirc \
|
||||
@@ -1798,20 +1814,51 @@ docker run -d --name neoirc \
|
||||
neoirc
|
||||
```
|
||||
|
||||
A host directory works the same way in place of the named volume:
|
||||
|
||||
```bash
|
||||
docker run -d --name neoirc \
|
||||
-p 8080:8080 -p 6667:6667 \
|
||||
-v /srv/neoirc:/var/lib/neoirc \
|
||||
-e SERVER_NAME="My Server" \
|
||||
-e MOTD="Welcome!" \
|
||||
neoirc
|
||||
```
|
||||
|
||||
### Reverse proxy
|
||||
|
||||
Run the server behind a reverse proxy that terminates TLS. The server itself
|
||||
speaks plain HTTP, and the auth cookie is `Secure`, so browsers send it only
|
||||
over HTTPS. The proxy must:
|
||||
|
||||
- Set `X-Forwarded-For` to the client's address, replacing any value the client
|
||||
sent. The server reads that header first and trusts it from anyone; it reads
|
||||
`X-Real-IP` only when `X-Forwarded-For` is absent. Without a proxy that
|
||||
overwrites the header, a client can choose its own address, which sets its
|
||||
hostname and lets it dodge the login rate limit.
|
||||
- Connect from an address in `TRUSTED_PROXIES`. The default trusts the private
|
||||
ranges `10.0.0.0/8`, `172.16.0.0/12` and `192.168.0.0/16`. The setting must
|
||||
hold the address the server sees the proxy connect from. For a proxy on the
|
||||
same host:
|
||||
- With the server outside Docker, that address is `127.0.0.1` or `::1`. The
|
||||
default does not trust them, so set `TRUSTED_PROXIES=127.0.0.1,::1`.
|
||||
- Through a published Docker port, that address is the Docker bridge
|
||||
gateway, such as `172.17.0.1`. The default trusts it.
|
||||
- Add the client's address to `X-Forwarded-For`.
|
||||
- Wait at least 60 seconds for a response. A long poll can take 30 seconds, and
|
||||
the server's own write timeout is 60 seconds.
|
||||
|
||||
The server finds the client's address in these steps:
|
||||
|
||||
- A connection from outside `TRUSTED_PROXIES` is the client. The server ignores
|
||||
its `X-Forwarded-For` and `X-Real-IP` headers.
|
||||
- For a connection from a trusted proxy, the server reads `X-Forwarded-For` from
|
||||
right to left. It skips trusted addresses and takes the first untrusted one.
|
||||
When every entry is trusted, it takes the leftmost.
|
||||
- Without `X-Forwarded-For`, the server takes `X-Real-IP`.
|
||||
- When an entry it reads is not an IP address, the server uses the proxy's
|
||||
address.
|
||||
|
||||
The client's address sets the session's hostname and counts toward the login
|
||||
rate limit. Any connection from a trusted address can choose its client address
|
||||
through `X-Forwarded-For`. If clients connect directly from private addresses,
|
||||
set `TRUSTED_PROXIES` to the proxy's address alone.
|
||||
|
||||
Caddy:
|
||||
|
||||
```
|
||||
@@ -1848,22 +1895,34 @@ The IRC listener has no TLS.
|
||||
seconds. A request still running after 60 seconds is cancelled, and the
|
||||
connection closes without a response. Headers may be up to 1 MiB.
|
||||
- A long poll waits at most 30 seconds.
|
||||
- Every response to a well-formed request, except to a cross-origin preflight,
|
||||
carries a `Content-Security-Policy` header:
|
||||
- Every response to a well-formed request carries a `Content-Security-Policy`
|
||||
header:
|
||||
`default-src 'self'; script-src 'self'; style-src 'self'; connect-src 'self'; img-src 'self'; font-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'`.
|
||||
The web client works within it.
|
||||
- Cross-origin requests are allowed from any origin, with credentials. The
|
||||
allowed methods are `GET`, `POST`, `PUT`, `DELETE` and `OPTIONS`. The allowed
|
||||
request headers are `Accept`, `Content-Type` and `X-CSRF-Token`. The cookie's
|
||||
`SameSite=Strict` still keeps it to the same site.
|
||||
- `GET /api/v1/server` and `GET /.well-known/healthcheck.json` answer a request
|
||||
from any origin with `Access-Control-Allow-Origin: *`. Every other route sends
|
||||
no `Access-Control-Allow-*` header, so a page on another origin cannot read
|
||||
its responses.
|
||||
|
||||
### Logging
|
||||
|
||||
The server logs with Go's `slog`, one line per event, to standard output. The
|
||||
lines are JSON, or plain text when standard output is a terminal. Every HTTP
|
||||
request is logged with its method, URL, protocol, status, latency, remote
|
||||
address, user agent, referrer and request ID. `DEBUG=true` adds debug-level
|
||||
lines.
|
||||
request is logged with:
|
||||
|
||||
- start time
|
||||
- method
|
||||
- URL
|
||||
- protocol
|
||||
- status
|
||||
- latency
|
||||
- client address
|
||||
- user agent
|
||||
- referrer
|
||||
- request ID
|
||||
|
||||
The client address is the one found as described in
|
||||
[Reverse proxy](#reverse-proxy). `DEBUG=true` adds debug-level lines.
|
||||
|
||||
### The database
|
||||
|
||||
@@ -1946,8 +2005,8 @@ and their sessions to end. Then it closes the database.
|
||||
gets a message.
|
||||
- `internal/hashcash/`: stamp checking and minting.
|
||||
- `internal/ratelimit/`: the per-address login rate limiter.
|
||||
- `internal/middleware/`: request logging, cross-origin requests, the
|
||||
`Content-Security-Policy` header and metrics.
|
||||
- `internal/middleware/`: finding the client's address, request logging,
|
||||
cross-origin requests, the `Content-Security-Policy` header and metrics.
|
||||
- `internal/healthcheck/` and `internal/stats/`: the health check and its
|
||||
counters.
|
||||
- `internal/logger/` and `internal/globals/`: logging setup and build
|
||||
@@ -1959,6 +2018,8 @@ and their sessions to end. Then it closes the database.
|
||||
- `schema/`: JSON Schema files for the message envelope, with their own
|
||||
`README.md`.
|
||||
- `script/`: the entrypoints below.
|
||||
- `deploy/`: `docker-entrypoint.sh`, the image's entrypoint, which prepares
|
||||
`/var/lib/neoirc` and starts `neoircd` as `neoirc`.
|
||||
- `CONVENTIONS.md`, `REPO_POLICIES.md` and `AGENTS.md`: the architecture
|
||||
conventions, the repository policies and the rules for coding agents.
|
||||
|
||||
@@ -1970,15 +2031,16 @@ standard. Each script has a `make` target of the same name, except
|
||||
`script/install-precommit`, which is `make hooks`, and `script/cibuild`,
|
||||
`script/precommit` and `script/projectname`, which have none.
|
||||
|
||||
- `script/bootstrap`: installs `make`, `git`, Go, Node and `yarn`, and prettier
|
||||
through `yarn`. Docker must already be installed.
|
||||
- `script/bootstrap`: installs `make`, `git`, Go, Node and `yarn`, and
|
||||
`prettier` through `yarn`. Docker must already be installed.
|
||||
- `script/setup`: makes a fresh clone ready: runs `script/bootstrap`, then
|
||||
`script/install-precommit`.
|
||||
- `script/test`: builds the `Dockerfile`'s `test` stage, which runs the tests
|
||||
with `-race`.
|
||||
- `script/lint`: builds the `Dockerfile`'s `lint` stage, which runs
|
||||
golangci-lint.
|
||||
- `script/fmt`: formats the Go code with `gofmt` and the Markdown with prettier.
|
||||
`golangci-lint`.
|
||||
- `script/fmt`: formats the Go code with `gofmt` and the Markdown with
|
||||
`prettier`.
|
||||
- `script/fmt-check`: the same, read-only; fails if anything would change.
|
||||
- `script/check`: runs `script/test`, `script/lint` and `script/fmt-check`.
|
||||
- `script/docker`: builds the image, tagged `neoirc`.
|
||||
@@ -2010,8 +2072,8 @@ standard. Each script has a `make` target of the same name, except
|
||||
The tests and the linter run inside Docker, as stages of the `Dockerfile`, so
|
||||
every developer and the CI run the same toolchain. `make test` builds the `test`
|
||||
stage: `go test -race` over every package, with a 90-second limit. `make lint`
|
||||
builds the `lint` stage, which runs golangci-lint with the shared
|
||||
`.golangci.yml`. `make fmt-check` runs `gofmt` and prettier on the host. Tests
|
||||
builds the `lint` stage, which runs `golangci-lint` with the shared
|
||||
`.golangci.yml`. `make fmt-check` runs `gofmt` and `prettier` on the host. Tests
|
||||
live next to the code, most in `_test` packages. A test that needs a database
|
||||
gets an in-memory one of its own from `internal/dbtest`, which holds a second
|
||||
connection open to keep it.
|
||||
@@ -2019,9 +2081,9 @@ connection open to keep it.
|
||||
The `Dockerfile` has five stages:
|
||||
|
||||
1. `web-builder`: installs the web client's Node dependencies and bundles
|
||||
`web/src/app.jsx` with esbuild into `web/dist/`.
|
||||
2. `lint`: runs golangci-lint, with placeholder files in `web/dist/` so that it
|
||||
does not wait for the web build.
|
||||
`web/src/app.jsx` with `esbuild` into `web/dist/`.
|
||||
2. `lint`: runs `golangci-lint`, with placeholder files in `web/dist/` so that
|
||||
it does not wait for the web build.
|
||||
3. `test`: runs the tests, with the same placeholders.
|
||||
4. `builder`: runs only after `lint` and `test` have passed. It compiles
|
||||
`neoircd` with the real web client, and compiles `neoirc-cli` to check that
|
||||
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
# deploy/docker-entrypoint.sh: the image's ENTRYPOINT, run as root. A host
|
||||
# directory mounted at /var/lib/neoirc keeps the host's owner, so this gives
|
||||
# it to neoirc before running neoircd as neoirc.
|
||||
set -eu
|
||||
|
||||
main() {
|
||||
mkdir -p /var/lib/neoirc
|
||||
chown -R neoirc:neoirc /var/lib/neoirc
|
||||
chmod 0700 /var/lib/neoirc
|
||||
exec su-exec neoirc neoircd "$@"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
@@ -13,8 +13,10 @@ require (
|
||||
github.com/prometheus/client_golang v1.23.2
|
||||
github.com/rivo/tview v0.42.0
|
||||
github.com/slok/go-http-metrics v0.13.0
|
||||
github.com/spf13/cast v1.10.0
|
||||
github.com/spf13/viper v1.21.0
|
||||
go.uber.org/fx v1.24.0
|
||||
go4.org/netipx v0.0.0-20260823151212-3075585bcbeb
|
||||
golang.org/x/crypto v0.48.0
|
||||
golang.org/x/time v0.6.0
|
||||
modernc.org/sqlite v1.45.0
|
||||
@@ -40,7 +42,6 @@ require (
|
||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
||||
github.com/spf13/afero v1.15.0 // indirect
|
||||
github.com/spf13/cast v1.10.0 // indirect
|
||||
github.com/spf13/pflag v1.0.10 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
go.uber.org/dig v1.19.0 // indirect
|
||||
|
||||
@@ -111,6 +111,8 @@ go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
||||
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
||||
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
|
||||
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||
go4.org/netipx v0.0.0-20260823151212-3075585bcbeb h1:XBM4hvfwGAttkkiTIFfeigdfcL1xIfdKXqFdgiHGtDs=
|
||||
go4.org/netipx v0.0.0-20260823151212-3075585bcbeb/go.mod h1:PLyyIXexvUFg3Owu6p/WfdlivPbZJsZdgWZlrGope/Y=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
|
||||
|
||||
+183
-42
@@ -6,12 +6,16 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"math"
|
||||
"net/netip"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/fx"
|
||||
"go4.org/netipx"
|
||||
"sneak.berlin/go/neoirc/internal/globals"
|
||||
"sneak.berlin/go/neoirc/internal/logger"
|
||||
|
||||
@@ -32,8 +36,16 @@ Type /help for available commands.`
|
||||
const DefaultDBURL = "file:///var/lib/neoirc/state.db" +
|
||||
"?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)"
|
||||
|
||||
// DefaultTrustedProxies is TRUSTED_PROXIES when it is not set: the
|
||||
// private address ranges of RFC 1918.
|
||||
const DefaultTrustedProxies = "10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"
|
||||
|
||||
var errOutOfRange = errors.New("setting out of range")
|
||||
|
||||
var errNeverMatches = errors.New("entry can never match a connection")
|
||||
|
||||
var errListOrMap = errors.New("a list or a map")
|
||||
|
||||
// Params defines the dependencies for creating a Config.
|
||||
type Params struct {
|
||||
fx.In
|
||||
@@ -64,13 +76,15 @@ type Config struct {
|
||||
LoginRateLimit float64
|
||||
LoginRateBurst int
|
||||
IRCListenAddr string
|
||||
TrustedProxies *netipx.IPSet
|
||||
params *Params
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// New creates a new Config by reading from files and environment variables.
|
||||
// It returns an error when a number, true/false or duration setting does
|
||||
// not parse or is out of range.
|
||||
// It returns an error when the config file does not parse, when the file
|
||||
// gives a setting a list or a map, and when a number, true/false,
|
||||
// duration or address setting does not parse or is out of range.
|
||||
func New(
|
||||
_ fx.Lifecycle, params Params,
|
||||
) (*Config, error) {
|
||||
@@ -103,32 +117,20 @@ func New(
|
||||
viper.SetDefault("LOGIN_RATE_LIMIT", "1")
|
||||
viper.SetDefault("LOGIN_RATE_BURST", "5")
|
||||
viper.SetDefault("IRC_LISTEN_ADDR", ":6667")
|
||||
viper.SetDefault("TRUSTED_PROXIES", DefaultTrustedProxies)
|
||||
|
||||
err := viper.ReadInConfig()
|
||||
if err != nil {
|
||||
var notFound viper.ConfigFileNotFoundError
|
||||
if !errors.As(err, ¬Found) {
|
||||
log.Error("config file malformed", "error", err)
|
||||
panic(err)
|
||||
return nil, fmt.Errorf("cannot read %s: %w",
|
||||
viper.ConfigFileUsed(), err)
|
||||
}
|
||||
}
|
||||
|
||||
cfg := &Config{
|
||||
DBURL: viper.GetString("DBURL"),
|
||||
SentryDSN: viper.GetString("SENTRY_DSN"),
|
||||
MetricsUsername: viper.GetString("METRICS_USERNAME"),
|
||||
MetricsPassword: viper.GetString("METRICS_PASSWORD"),
|
||||
MessageMaxAge: viper.GetString("MESSAGE_MAX_AGE"),
|
||||
QueueMaxAge: viper.GetString("QUEUE_MAX_AGE"),
|
||||
MOTD: viper.GetString("MOTD"),
|
||||
ServerName: viper.GetString("SERVER_NAME"),
|
||||
FederationKey: viper.GetString("FEDERATION_KEY"),
|
||||
SessionIdleTimeout: viper.GetString("SESSION_IDLE_TIMEOUT"),
|
||||
OperName: viper.GetString("NEOIRC_OPER_NAME"),
|
||||
OperPassword: viper.GetString("NEOIRC_OPER_PASSWORD"),
|
||||
IRCListenAddr: viper.GetString("IRC_LISTEN_ADDR"),
|
||||
log: log,
|
||||
params: ¶ms,
|
||||
log: log,
|
||||
params: ¶ms,
|
||||
}
|
||||
|
||||
err = parseSettings(cfg)
|
||||
@@ -136,13 +138,6 @@ func New(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Viper counts an empty environment variable as unset, but an
|
||||
// empty IRC_LISTEN_ADDR must turn the IRC listener off.
|
||||
ircListenAddr, set := os.LookupEnv("IRC_LISTEN_ADDR")
|
||||
if set {
|
||||
cfg.IRCListenAddr = ircListenAddr
|
||||
}
|
||||
|
||||
if cfg.Debug {
|
||||
params.Logger.EnableDebugLogging()
|
||||
cfg.log = params.Logger.Get()
|
||||
@@ -151,13 +146,24 @@ func New(
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// parseSettings reads the number and true/false settings into cfg and
|
||||
// checks the duration settings. Each number and duration must be above
|
||||
// zero. PORT and NEOIRC_HASHCASH_BITS may also be zero: port 0 lets the
|
||||
// system pick a port, and 0 bits turns proof of work off. The error
|
||||
// names each setting that does not parse or is out of range.
|
||||
// parseSettings reads every setting into cfg. Each number and duration
|
||||
// must be above zero. PORT and NEOIRC_HASHCASH_BITS may also be zero:
|
||||
// port 0 lets the system pick a port, and 0 bits turns proof of work off.
|
||||
// The error names each setting that is a list or a map, does not parse or
|
||||
// is out of range.
|
||||
func parseSettings(cfg *Config) error {
|
||||
return errors.Join(
|
||||
parseString("DBURL", &cfg.DBURL),
|
||||
parseString("SENTRY_DSN", &cfg.SentryDSN),
|
||||
parseString("METRICS_USERNAME", &cfg.MetricsUsername),
|
||||
parseString("METRICS_PASSWORD", &cfg.MetricsPassword),
|
||||
parseString("MOTD", &cfg.MOTD),
|
||||
parseString("SERVER_NAME", &cfg.ServerName),
|
||||
parseString("FEDERATION_KEY", &cfg.FederationKey),
|
||||
parseString("NEOIRC_OPER_NAME", &cfg.OperName),
|
||||
parseString("NEOIRC_OPER_PASSWORD", &cfg.OperPassword),
|
||||
// The empty string turns the IRC listener off.
|
||||
parseStringAllowEmptyEnv("IRC_LISTEN_ADDR", &cfg.IRCListenAddr),
|
||||
parseInt("PORT", 0, &cfg.Port),
|
||||
parseInt("MAX_MESSAGE_SIZE", 1, &cfg.MaxMessageSize),
|
||||
parseInt("NEOIRC_HASHCASH_BITS", 0, &cfg.HashcashBits),
|
||||
@@ -165,16 +171,136 @@ func parseSettings(cfg *Config) error {
|
||||
parseFloat("LOGIN_RATE_LIMIT", &cfg.LoginRateLimit),
|
||||
parseBool("DEBUG", &cfg.Debug),
|
||||
parseBool("MAINTENANCE_MODE", &cfg.MaintenanceMode),
|
||||
checkDuration("MESSAGE_MAX_AGE", cfg.MessageMaxAge),
|
||||
checkDuration("QUEUE_MAX_AGE", cfg.QueueMaxAge),
|
||||
checkDuration("SESSION_IDLE_TIMEOUT", cfg.SessionIdleTimeout),
|
||||
parseDuration("MESSAGE_MAX_AGE", &cfg.MessageMaxAge),
|
||||
parseDuration("QUEUE_MAX_AGE", &cfg.QueueMaxAge),
|
||||
parseDuration("SESSION_IDLE_TIMEOUT", &cfg.SessionIdleTimeout),
|
||||
parseTrustedProxies(&cfg.TrustedProxies),
|
||||
)
|
||||
}
|
||||
|
||||
// getString returns the setting key as a string. A number or a true/false
|
||||
// value in the config file becomes its text, as with viper.GetString. A
|
||||
// list or a map in the config file is an error, where viper.GetString
|
||||
// would return the empty string. The error leaves out the value, which
|
||||
// may be a password.
|
||||
func getString(key string) (string, error) {
|
||||
raw, err := cast.ToStringE(viper.Get(key))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s is %w", key, errListOrMap)
|
||||
}
|
||||
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
// parseString sets *value to the setting key.
|
||||
func parseString(key string, value *string) error {
|
||||
raw, err := getString(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*value = raw
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseStringAllowEmptyEnv sets *value to the setting key, as parseString
|
||||
// does, but an environment variable set to the empty string counts as
|
||||
// set. Viper counts such a variable as unset and uses the config file or
|
||||
// the default instead.
|
||||
func parseStringAllowEmptyEnv(key string, value *string) error {
|
||||
fromEnv, set := os.LookupEnv(key)
|
||||
if set {
|
||||
*value = fromEnv
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return parseString(key, value)
|
||||
}
|
||||
|
||||
// parseTrustedProxies sets *value to the addresses in TRUSTED_PROXIES.
|
||||
func parseTrustedProxies(value **netipx.IPSet) error {
|
||||
var raw string
|
||||
|
||||
err := parseStringAllowEmptyEnv("TRUSTED_PROXIES", &raw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ipSet, err := ParseTrustedProxies(raw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot parse TRUSTED_PROXIES: %w", err)
|
||||
}
|
||||
|
||||
*value = ipSet
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseTrustedProxies returns the addresses in list, which has the form
|
||||
// of TRUSTED_PROXIES: a comma-separated list of networks such as
|
||||
// 10.0.0.0/8 and single addresses such as 192.0.2.1. The empty string
|
||||
// trusts nothing.
|
||||
func ParseTrustedProxies(list string) (*netipx.IPSet, error) {
|
||||
var builder netipx.IPSetBuilder
|
||||
|
||||
if strings.TrimSpace(list) != "" {
|
||||
for entry := range strings.SplitSeq(list, ",") {
|
||||
err := addTrustedProxy(&builder, strings.TrimSpace(entry))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return builder.IPSet()
|
||||
}
|
||||
|
||||
// addTrustedProxy adds entry, a network with a slash or a single
|
||||
// address, to builder. An IPv4 address written in IPv6 form, such as
|
||||
// ::ffff:192.0.2.1, is added as IPv4. A network in that form and an
|
||||
// address with a zone are errors, since no connection could match them.
|
||||
func addTrustedProxy(builder *netipx.IPSetBuilder, entry string) error {
|
||||
if strings.Contains(entry, "/") {
|
||||
prefix, err := netip.ParsePrefix(entry)
|
||||
if err != nil {
|
||||
return err //nolint:wrapcheck // the caller names the setting
|
||||
}
|
||||
|
||||
// A connection from an IPv4 address is checked in IPv4 form.
|
||||
if prefix.Addr().Is4In6() {
|
||||
return fmt.Errorf("%w: %s is an IPv4 network in IPv6 form",
|
||||
errNeverMatches, entry)
|
||||
}
|
||||
|
||||
builder.AddPrefix(prefix)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
addr, err := netip.ParseAddr(entry)
|
||||
if err != nil {
|
||||
return err //nolint:wrapcheck // the caller names the setting
|
||||
}
|
||||
|
||||
// IPSet.Contains is false for every address with a zone.
|
||||
if addr.Zone() != "" {
|
||||
return fmt.Errorf("%w: %s has a zone", errNeverMatches, entry)
|
||||
}
|
||||
|
||||
builder.Add(addr.Unmap())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseInt sets *value to the setting key read as a whole number. It
|
||||
// returns an error when the value does not parse or is below minimum.
|
||||
func parseInt(key string, minimum int, value *int) error {
|
||||
raw := viper.GetString(key)
|
||||
raw, err := getString(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parsed, err := strconv.Atoi(raw)
|
||||
if err != nil {
|
||||
@@ -195,7 +321,10 @@ func parseInt(key string, minimum int, value *int) error {
|
||||
// number. It returns an error when the value does not parse or is not a
|
||||
// finite number above zero.
|
||||
func parseFloat(key string, value *float64) error {
|
||||
raw := viper.GetString(key)
|
||||
raw, err := getString(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parsed, err := strconv.ParseFloat(raw, 64)
|
||||
if err != nil {
|
||||
@@ -215,7 +344,12 @@ func parseFloat(key string, value *float64) error {
|
||||
|
||||
// parseBool sets *value to the setting key read as true or false.
|
||||
func parseBool(key string, value *bool) error {
|
||||
parsed, err := strconv.ParseBool(viper.GetString(key))
|
||||
raw, err := getString(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parsed, err := strconv.ParseBool(raw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot parse %s: %w", key, err)
|
||||
}
|
||||
@@ -225,18 +359,25 @@ func parseBool(key string, value *bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkDuration returns an error naming the setting key when value is
|
||||
// not a Go duration above zero.
|
||||
func checkDuration(key, value string) error {
|
||||
duration, err := time.ParseDuration(value)
|
||||
// parseDuration sets *value to the setting key. It returns an error when
|
||||
// the value is not a Go duration above zero.
|
||||
func parseDuration(key string, value *string) error {
|
||||
raw, err := getString(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
duration, err := time.ParseDuration(raw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot parse %s: %w", key, err)
|
||||
}
|
||||
|
||||
if duration <= 0 {
|
||||
return fmt.Errorf("%w: %s is %s, but must be above zero",
|
||||
errOutOfRange, key, value)
|
||||
errOutOfRange, key, raw)
|
||||
}
|
||||
|
||||
*value = raw
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,11 +1,14 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/globals"
|
||||
"sneak.berlin/go/neoirc/internal/logger"
|
||||
@@ -140,8 +143,7 @@ func settingsOf(cfg *config.Config) settings {
|
||||
}
|
||||
|
||||
func TestBadSettingStopsStartup(t *testing.T) {
|
||||
// For each setting, the first value does not parse and the others
|
||||
// parse but are out of range.
|
||||
// Each value is invalid for its setting.
|
||||
tests := []struct {
|
||||
key string
|
||||
values []string
|
||||
@@ -158,6 +160,10 @@ func TestBadSettingStopsStartup(t *testing.T) {
|
||||
{key: "MESSAGE_MAX_AGE", values: []string{"30d", "0", "-1h"}},
|
||||
{key: "QUEUE_MAX_AGE", values: []string{"720", "0s", "-24h"}},
|
||||
{key: "SESSION_IDLE_TIMEOUT", values: []string{"a month", "0", "-1m"}},
|
||||
{key: "TRUSTED_PROXIES", values: []string{
|
||||
"proxy.example.com", "10.0.0.0/33", "192.0.2.1:8080",
|
||||
"::ffff:10.0.0.0/104", "fe80::1%eth0",
|
||||
}},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
@@ -206,3 +212,228 @@ func TestIRCListenAddrEmptyTurnsListenerOff(t *testing.T) {
|
||||
t.Errorf("IRCListenAddr = %q, want empty", cfg.IRCListenAddr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrustedProxies(t *testing.T) {
|
||||
// Each case lists addresses that must be in the trusted set and
|
||||
// addresses that must not.
|
||||
tests := []struct {
|
||||
name string
|
||||
set bool
|
||||
value string
|
||||
trusted []string
|
||||
untrusted []string
|
||||
}{
|
||||
{
|
||||
name: "unset trusts the RFC 1918 ranges",
|
||||
set: false,
|
||||
value: "",
|
||||
trusted: []string{
|
||||
"10.1.2.3", "172.16.0.1", "172.31.255.254", "192.168.1.1",
|
||||
},
|
||||
untrusted: []string{
|
||||
"127.0.0.1", "172.32.0.1", "192.0.2.1", "2001:db8::1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "a value replaces the default",
|
||||
set: true,
|
||||
value: "198.51.100.0/24, 192.0.2.7,2001:db8::/32",
|
||||
trusted: []string{"198.51.100.200", "192.0.2.7", "2001:db8::5"},
|
||||
untrusted: []string{"10.4.5.6", "192.168.4.4", "192.0.2.8"},
|
||||
},
|
||||
{
|
||||
name: "a mapped IPv6 address is trusted as IPv4",
|
||||
set: true,
|
||||
value: "::ffff:192.0.2.7",
|
||||
trusted: []string{"192.0.2.7"},
|
||||
untrusted: []string{"10.7.8.9"},
|
||||
},
|
||||
{
|
||||
name: "the empty string trusts nothing",
|
||||
set: true,
|
||||
value: "",
|
||||
trusted: nil,
|
||||
untrusted: []string{"10.0.0.1", "192.168.0.1", "::1"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
// t.Setenv puts back whatever value was there after the
|
||||
// test, including when Unsetenv removes it.
|
||||
t.Setenv("TRUSTED_PROXIES", test.value)
|
||||
|
||||
if !test.set {
|
||||
err := os.Unsetenv("TRUSTED_PROXIES")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
proxies := newConfig(t).TrustedProxies
|
||||
|
||||
for _, addr := range test.trusted {
|
||||
if !proxies.Contains(netip.MustParseAddr(addr)) {
|
||||
t.Errorf("%s is not trusted", addr)
|
||||
}
|
||||
}
|
||||
|
||||
for _, addr := range test.untrusted {
|
||||
if proxies.Contains(netip.MustParseAddr(addr)) {
|
||||
t.Errorf("%s is trusted", addr)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// unsetEnv removes the environment variable key for the rest of the
|
||||
// test, so that config.New reads key from the config file.
|
||||
func unsetEnv(t *testing.T, key string) {
|
||||
t.Helper()
|
||||
|
||||
// t.Setenv puts back whatever value was there after the test,
|
||||
// including when Unsetenv removes it.
|
||||
t.Setenv(key, "")
|
||||
|
||||
err := os.Unsetenv(key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// writeConfigFile writes content to the config file config.New reads
|
||||
// when HOME is home, and returns the file's path.
|
||||
func writeConfigFile(t *testing.T, home, content string) string {
|
||||
t.Helper()
|
||||
|
||||
// loadConfig names the app neoirc-test, so config.New reads
|
||||
// $HOME/.config/neoirc-test/neoirc-test.yaml.
|
||||
dir := filepath.Join(home, ".config", "neoirc-test")
|
||||
|
||||
err := os.MkdirAll(dir, 0o700)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
path := filepath.Join(dir, "neoirc-test.yaml")
|
||||
|
||||
err = os.WriteFile(path, []byte(content), 0o600)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Viper keeps the file and the values it read from it after the
|
||||
// file is gone. Reset clears them for later tests.
|
||||
t.Cleanup(viper.Reset)
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
func TestListOrMapInConfigFileStopsStartup(t *testing.T) {
|
||||
// YAML reads the first value as a list and the second as a map.
|
||||
values := []string{`[":6667"]`, `{addr: ":6667"}`}
|
||||
keys := []string{"IRC_LISTEN_ADDR", "MOTD", "TRUSTED_PROXIES"}
|
||||
|
||||
for _, key := range keys {
|
||||
for _, value := range values {
|
||||
t.Run(key+"="+value, func(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
unsetEnv(t, key)
|
||||
writeConfigFile(t, home, key+": "+value+"\n")
|
||||
|
||||
_, err := loadConfig(t)
|
||||
if err == nil || !strings.Contains(err.Error(), key) {
|
||||
t.Errorf("error = %v, want one naming %s", err, key)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListOrMapErrorLeavesOutValue(t *testing.T) {
|
||||
const key = "NEOIRC_OPER_PASSWORD"
|
||||
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
unsetEnv(t, key)
|
||||
// YAML reads an unquoted {hunter2} as a map.
|
||||
writeConfigFile(t, home, key+": {hunter2}\n")
|
||||
|
||||
_, err := loadConfig(t)
|
||||
if err == nil || !strings.Contains(err.Error(), key) {
|
||||
t.Fatalf("error = %v, want one naming %s", err, key)
|
||||
}
|
||||
|
||||
if strings.Contains(err.Error(), "hunter2") {
|
||||
t.Errorf("error %q shows the password", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyEnvironmentValueWinsOverConfigFile(t *testing.T) {
|
||||
const (
|
||||
ircListenAddr = "IRC_LISTEN_ADDR"
|
||||
trustedProxies = "TRUSTED_PROXIES"
|
||||
)
|
||||
|
||||
// Each setting is the empty string in the environment and a list in
|
||||
// the config file. The environment wins, so the list is never read.
|
||||
tests := []struct {
|
||||
key string
|
||||
empty func(cfg *config.Config) bool
|
||||
}{
|
||||
{
|
||||
key: ircListenAddr,
|
||||
empty: func(cfg *config.Config) bool {
|
||||
return cfg.IRCListenAddr == ""
|
||||
},
|
||||
},
|
||||
{
|
||||
key: trustedProxies,
|
||||
empty: func(cfg *config.Config) bool {
|
||||
return len(cfg.TrustedProxies.Prefixes()) == 0
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.key, func(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv(test.key, "")
|
||||
writeConfigFile(t, home, test.key+": [\":6667\"]\n")
|
||||
|
||||
cfg := newConfig(t)
|
||||
if !test.empty(cfg) {
|
||||
t.Errorf("%s is not the empty string", test.key)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSingleValuesInConfigFileAreUsed(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
unsetEnv(t, "PORT")
|
||||
unsetEnv(t, "DEBUG")
|
||||
unsetEnv(t, "MOTD")
|
||||
writeConfigFile(t, home, "PORT: 9000\nDEBUG: true\nMOTD: hello\n")
|
||||
|
||||
cfg := newConfig(t)
|
||||
if cfg.Port != 9000 || !cfg.Debug || cfg.MOTD != "hello" {
|
||||
t.Errorf("Port, Debug, MOTD = %d, %t, %q, want 9000, true, %q",
|
||||
cfg.Port, cfg.Debug, cfg.MOTD, "hello")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigFileThatDoesNotParseStopsStartup(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
path := writeConfigFile(t, home, "MOTD: [unclosed\n")
|
||||
|
||||
_, err := loadConfig(t)
|
||||
if err == nil || !strings.Contains(err.Error(), path) {
|
||||
t.Errorf("error = %v, want one naming %s", err, path)
|
||||
}
|
||||
}
|
||||
+1
-8
@@ -10,13 +10,6 @@ import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
//nolint:gochecknoglobals // var so tests can override via SetBcryptCost
|
||||
var bcryptCost = bcrypt.DefaultCost
|
||||
|
||||
// SetBcryptCost overrides the bcrypt cost.
|
||||
// Use bcrypt.MinCost in tests to avoid slow hashing.
|
||||
func SetBcryptCost(cost int) { bcryptCost = cost }
|
||||
|
||||
var errNoPassword = errors.New(
|
||||
"account has no password set",
|
||||
)
|
||||
@@ -29,7 +22,7 @@ func (database *Database) SetPassword(
|
||||
password string,
|
||||
) error {
|
||||
hash, err := bcrypt.GenerateFromPassword(
|
||||
[]byte(password), bcryptCost,
|
||||
[]byte(password), database.passwordCost,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("hash password: %w", err)
|
||||
|
||||
+18
-10
@@ -15,6 +15,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"go.uber.org/fx"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/logger"
|
||||
|
||||
@@ -39,13 +40,19 @@ type Params struct {
|
||||
|
||||
Logger *logger.Logger
|
||||
Config *config.Config
|
||||
|
||||
// PasswordCost is the bcrypt cost at which SetPassword hashes
|
||||
// a password. The server leaves it unset, which means
|
||||
// bcrypt.DefaultCost. Tests set a lower cost to save time.
|
||||
PasswordCost int `optional:"true"`
|
||||
}
|
||||
|
||||
// Database manages the SQLite connection and migrations.
|
||||
type Database struct {
|
||||
conn *sql.DB
|
||||
log *slog.Logger
|
||||
params *Params
|
||||
conn *sql.DB
|
||||
log *slog.Logger
|
||||
params *Params
|
||||
passwordCost int
|
||||
}
|
||||
|
||||
// New creates a new Database and registers lifecycle hooks.
|
||||
@@ -53,9 +60,15 @@ func New(
|
||||
lifecycle fx.Lifecycle,
|
||||
params Params,
|
||||
) (*Database, error) {
|
||||
passwordCost := params.PasswordCost
|
||||
if passwordCost == 0 {
|
||||
passwordCost = bcrypt.DefaultCost
|
||||
}
|
||||
|
||||
database := &Database{ //nolint:exhaustruct // conn set in OnStart
|
||||
params: ¶ms,
|
||||
log: params.Logger.Get(),
|
||||
params: ¶ms,
|
||||
log: params.Logger.Get(),
|
||||
passwordCost: passwordCost,
|
||||
}
|
||||
|
||||
database.log.Info("Database instantiated")
|
||||
@@ -85,11 +98,6 @@ func New(
|
||||
return database, nil
|
||||
}
|
||||
|
||||
// GetDB returns the underlying sql.DB connection.
|
||||
func (database *Database) GetDB() *sql.DB {
|
||||
return database.conn
|
||||
}
|
||||
|
||||
func (database *Database) connect(ctx context.Context) error {
|
||||
dbURL := database.params.Config.DBURL
|
||||
if dbURL == "" {
|
||||
|
||||
+85
-1
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"go.uber.org/fx/fxtest"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/db"
|
||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
||||
@@ -26,7 +27,11 @@ const (
|
||||
(SELECT 1 UNION ALL SELECT x + 1 FROM n)
|
||||
SELECT COUNT(*) FROM n`
|
||||
|
||||
endlessQueryTimeout = 100 * time.Millisecond
|
||||
// endlessQueryTimeout is how long the endless query runs
|
||||
// before its context ends. A query whose context ends before
|
||||
// it starts is never run, and the pool keeps its connection,
|
||||
// so the timeout leaves a loaded host time to start it.
|
||||
endlessQueryTimeout = 2 * time.Second
|
||||
)
|
||||
|
||||
// TestDefaultDBURLSetsWALAndBusyTimeout opens a file database
|
||||
@@ -236,6 +241,85 @@ func TestDBURLSettingForeignKeysFailsStartup(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestPasswordCost checks the bcrypt cost of the hash that SetPassword
|
||||
// stores: bcrypt.DefaultCost when db.Params leaves PasswordCost unset,
|
||||
// as the server does, and PasswordCost when it is set.
|
||||
func TestPasswordCost(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
passwordCost int
|
||||
wantCost int
|
||||
}{
|
||||
{name: "unset", wantCost: bcrypt.DefaultCost},
|
||||
{
|
||||
name: "bcrypt.MinCost",
|
||||
passwordCost: bcrypt.MinCost,
|
||||
wantCost: bcrypt.MinCost,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
lifecycle := fxtest.NewLifecycle(t)
|
||||
|
||||
log, err := logger.New(lifecycle, logger.Params{
|
||||
Globals: &globals.Globals{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
database, err := db.New(lifecycle, db.Params{
|
||||
Logger: log,
|
||||
Config: &config.Config{DBURL: dbtest.NewURL(t)},
|
||||
PasswordCost: test.passwordCost,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
lifecycle.RequireStart()
|
||||
t.Cleanup(lifecycle.RequireStop)
|
||||
|
||||
sessionID, _, _, err := database.CreateSession(
|
||||
t.Context(), "costuser", "", "", "",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = database.SetPassword(t.Context(), sessionID, "password123")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var hash string
|
||||
|
||||
err = database.GetDB().QueryRowContext(
|
||||
t.Context(),
|
||||
"SELECT password_hash FROM sessions WHERE id = ?",
|
||||
sessionID,
|
||||
).Scan(&hash)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cost, err := bcrypt.Cost([]byte(hash))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if cost != test.wantCost {
|
||||
t.Errorf("cost = %d, want %d", cost, test.wantCost)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// startDatabase starts a Database for dbURL the way the server does.
|
||||
// It returns the error that stopped it from starting, if any.
|
||||
func startDatabase(t *testing.T, dbURL string) (*db.Database, error) {
|
||||
|
||||
@@ -6,11 +6,14 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
||||
)
|
||||
|
||||
// NewTestDatabase creates an in-memory database for testing.
|
||||
// NewTestDatabase creates an in-memory database for testing. It
|
||||
// hashes passwords at bcrypt.MinCost.
|
||||
func NewTestDatabase(tb testing.TB) (*Database, error) {
|
||||
tb.Helper()
|
||||
|
||||
@@ -22,8 +25,9 @@ func NewTestDatabase(tb testing.TB) (*Database, error) {
|
||||
}
|
||||
|
||||
database := &Database{ //nolint:exhaustruct // test helper, params not needed
|
||||
conn: conn,
|
||||
log: slog.Default(),
|
||||
conn: conn,
|
||||
log: slog.Default(),
|
||||
passwordCost: bcrypt.MinCost,
|
||||
}
|
||||
|
||||
err = database.runMigrations(context.Background())
|
||||
@@ -67,3 +71,78 @@ func (database *Database) ExecForTest(
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDB returns the underlying sql.DB connection.
|
||||
func (database *Database) GetDB() *sql.DB {
|
||||
return database.conn
|
||||
}
|
||||
|
||||
// GetOrCreateChannel returns channel id, creating if needed.
|
||||
// Uses INSERT OR IGNORE to avoid TOCTOU races.
|
||||
func (database *Database) GetOrCreateChannel(
|
||||
ctx context.Context,
|
||||
name string,
|
||||
) (int64, error) {
|
||||
now := time.Now()
|
||||
|
||||
_, err := database.conn.ExecContext(ctx,
|
||||
`INSERT OR IGNORE INTO channels
|
||||
(name, created_at, updated_at)
|
||||
VALUES (?, ?, ?)`,
|
||||
name, now, now)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("create channel: %w", err)
|
||||
}
|
||||
|
||||
var channelID int64
|
||||
|
||||
err = database.conn.QueryRowContext(
|
||||
ctx,
|
||||
"SELECT id FROM channels WHERE name = ?",
|
||||
name,
|
||||
).Scan(&channelID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("get channel: %w", err)
|
||||
}
|
||||
|
||||
return channelID, nil
|
||||
}
|
||||
|
||||
// JoinChannel adds a session to a channel.
|
||||
func (database *Database) JoinChannel(
|
||||
ctx context.Context,
|
||||
channelID, sessionID int64,
|
||||
) error {
|
||||
_, err := database.conn.ExecContext(ctx,
|
||||
`INSERT OR IGNORE INTO channel_members
|
||||
(channel_id, session_id, joined_at)
|
||||
VALUES (?, ?, ?)`,
|
||||
channelID, sessionID, time.Now())
|
||||
if err != nil {
|
||||
return fmt.Errorf("join channel: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// HasChannelInvite checks if a session has been invited
|
||||
// to a channel.
|
||||
func (database *Database) HasChannelInvite(
|
||||
ctx context.Context,
|
||||
channelID, sessionID int64,
|
||||
) (bool, error) {
|
||||
var count int
|
||||
|
||||
err := database.conn.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM channel_invites
|
||||
WHERE channel_id = ? AND session_id = ?`,
|
||||
channelID, sessionID,
|
||||
).Scan(&count)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf(
|
||||
"check invite: %w", err,
|
||||
)
|
||||
}
|
||||
|
||||
return count > 0, nil
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package db_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"sneak.berlin/go/neoirc/internal/db"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
db.SetBcryptCost(bcrypt.MinCost)
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
@@ -320,29 +320,6 @@ type ClientHostInfo struct {
|
||||
Hostname string
|
||||
}
|
||||
|
||||
// GetClientHostInfo returns the IP and hostname for a
|
||||
// client.
|
||||
func (database *Database) GetClientHostInfo(
|
||||
ctx context.Context,
|
||||
clientID int64,
|
||||
) (*ClientHostInfo, error) {
|
||||
var info ClientHostInfo
|
||||
|
||||
err := database.conn.QueryRowContext(
|
||||
ctx,
|
||||
`SELECT ip, hostname
|
||||
FROM clients WHERE id = ?`,
|
||||
clientID,
|
||||
).Scan(&info.IP, &info.Hostname)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"get client host info: %w", err,
|
||||
)
|
||||
}
|
||||
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
// SetSessionOper sets the is_oper flag on a session.
|
||||
func (database *Database) SetSessionOper(
|
||||
ctx context.Context,
|
||||
@@ -433,54 +410,6 @@ func (database *Database) GetChannelByName(
|
||||
return channelID, nil
|
||||
}
|
||||
|
||||
// GetOrCreateChannel returns channel id, creating if needed.
|
||||
// Uses INSERT OR IGNORE to avoid TOCTOU races.
|
||||
func (database *Database) GetOrCreateChannel(
|
||||
ctx context.Context,
|
||||
name string,
|
||||
) (int64, error) {
|
||||
now := time.Now()
|
||||
|
||||
_, err := database.conn.ExecContext(ctx,
|
||||
`INSERT OR IGNORE INTO channels
|
||||
(name, created_at, updated_at)
|
||||
VALUES (?, ?, ?)`,
|
||||
name, now, now)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("create channel: %w", err)
|
||||
}
|
||||
|
||||
var channelID int64
|
||||
|
||||
err = database.conn.QueryRowContext(
|
||||
ctx,
|
||||
"SELECT id FROM channels WHERE name = ?",
|
||||
name,
|
||||
).Scan(&channelID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("get channel: %w", err)
|
||||
}
|
||||
|
||||
return channelID, nil
|
||||
}
|
||||
|
||||
// JoinChannel adds a session to a channel.
|
||||
func (database *Database) JoinChannel(
|
||||
ctx context.Context,
|
||||
channelID, sessionID int64,
|
||||
) error {
|
||||
_, err := database.conn.ExecContext(ctx,
|
||||
`INSERT OR IGNORE INTO channel_members
|
||||
(channel_id, session_id, joined_at)
|
||||
VALUES (?, ?, ?)`,
|
||||
channelID, sessionID, time.Now())
|
||||
if err != nil {
|
||||
return fmt.Errorf("join channel: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Errors that CreateOrJoinChannel refuses a join with.
|
||||
var (
|
||||
ErrBannedFromChannel = errors.New("banned from channel")
|
||||
@@ -1127,23 +1056,6 @@ func (database *Database) GetChannelMemberIDs(
|
||||
return scanInt64s(rows)
|
||||
}
|
||||
|
||||
// GetSessionChannelIDs returns channel IDs for a session.
|
||||
func (database *Database) GetSessionChannelIDs(
|
||||
ctx context.Context,
|
||||
sessionID int64,
|
||||
) ([]int64, error) {
|
||||
rows, err := database.conn.QueryContext(ctx,
|
||||
`SELECT channel_id FROM channel_members
|
||||
WHERE session_id = ?`, sessionID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"get session channel ids: %w", err,
|
||||
)
|
||||
}
|
||||
|
||||
return scanInt64s(rows)
|
||||
}
|
||||
|
||||
// InsertMessage stores a message and returns its DB ID.
|
||||
// toSessionID is the session a DM or INVITE is sent to and
|
||||
// toChannelID the channel a channel message is sent to;
|
||||
@@ -2460,28 +2372,6 @@ func (database *Database) AddChannelInvite(
|
||||
return nil
|
||||
}
|
||||
|
||||
// HasChannelInvite checks if a session has been invited
|
||||
// to a channel.
|
||||
func (database *Database) HasChannelInvite(
|
||||
ctx context.Context,
|
||||
channelID, sessionID int64,
|
||||
) (bool, error) {
|
||||
var count int
|
||||
|
||||
err := database.conn.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM channel_invites
|
||||
WHERE channel_id = ? AND session_id = ?`,
|
||||
channelID, sessionID,
|
||||
).Scan(&count)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf(
|
||||
"check invite: %w", err,
|
||||
)
|
||||
}
|
||||
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// --- Tier 2: Secret (+s) ---
|
||||
|
||||
// IsChannelSecret checks if a channel has +s mode.
|
||||
|
||||
@@ -125,7 +125,7 @@ func TestCreateSessionStoresIP(t *testing.T) {
|
||||
database := setupTestDB(t)
|
||||
ctx := t.Context()
|
||||
|
||||
sessionID, clientID, _, err := database.CreateSession(
|
||||
sessionID, _, _, err := database.CreateSession(
|
||||
ctx, "ipuser", "ident", "host.example.com",
|
||||
"192.168.1.42",
|
||||
)
|
||||
@@ -147,8 +147,8 @@ func TestCreateSessionStoresIP(t *testing.T) {
|
||||
)
|
||||
}
|
||||
|
||||
clientInfo, err := database.GetClientHostInfo(
|
||||
ctx, clientID,
|
||||
clientInfo, err := database.GetLatestClientForSession(
|
||||
ctx, sessionID,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -169,19 +169,6 @@ func TestCreateSessionStoresIP(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetClientHostInfoNotFound(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
|
||||
_, err := database.GetClientHostInfo(
|
||||
t.Context(), 99999,
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nonexistent client")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSessionHostInfoNotFound(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
// NewTestDatabaseFromConn creates a Database wrapping an
|
||||
// existing *sql.DB connection. Intended for integration
|
||||
// tests in other packages.
|
||||
func NewTestDatabaseFromConn(conn *sql.DB) *Database {
|
||||
return &Database{ //nolint:exhaustruct
|
||||
conn: conn,
|
||||
log: slog.Default(),
|
||||
}
|
||||
}
|
||||
|
||||
// RunMigrations applies all schema migrations. Exposed
|
||||
// for integration tests in other packages.
|
||||
func (database *Database) RunMigrations(
|
||||
ctx context.Context,
|
||||
) error {
|
||||
return database.runMigrations(ctx)
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
// Package dbtest gives each test a database of its own. Only tests
|
||||
// may import it.
|
||||
// Package dbtest gives each test a database of its own and runs raw
|
||||
// SQL on it. Only tests may import it.
|
||||
package dbtest
|
||||
|
||||
import (
|
||||
@@ -37,3 +37,23 @@ func NewURL(tb testing.TB) string {
|
||||
|
||||
return dbURL
|
||||
}
|
||||
|
||||
// Exec runs one raw SQL statement on the database at dbURL, over a
|
||||
// connection of its own. A test uses it for what the methods of
|
||||
// db.Database cannot do, such as creating a trigger that makes a
|
||||
// write fail.
|
||||
func Exec(tb testing.TB, dbURL, statement string) {
|
||||
tb.Helper()
|
||||
|
||||
pool, err := sql.Open("sqlite", dbURL)
|
||||
if err != nil {
|
||||
tb.Fatalf("open test database: %v", err)
|
||||
}
|
||||
|
||||
defer func() { _ = pool.Close() }()
|
||||
|
||||
_, err = pool.ExecContext(tb.Context(), statement)
|
||||
if err != nil {
|
||||
tb.Fatalf("run %q: %v", statement, err)
|
||||
}
|
||||
}
|
||||
@@ -42,25 +42,9 @@ func (hdlr *Handlers) maxBodySize() int64 {
|
||||
return defaultMaxBodySize
|
||||
}
|
||||
|
||||
// clientIP extracts the connecting client's IP address
|
||||
// from the request, checking X-Forwarded-For and
|
||||
// X-Real-IP headers before falling back to RemoteAddr.
|
||||
// clientIP returns the IP address in request.RemoteAddr, which the
|
||||
// ClientIP middleware has set to the client's address.
|
||||
func clientIP(request *http.Request) string {
|
||||
if forwarded := request.Header.Get("X-Forwarded-For"); forwarded != "" {
|
||||
// X-Forwarded-For can contain a comma-separated list;
|
||||
// the first entry is the original client.
|
||||
parts := strings.SplitN(forwarded, ",", 2) //nolint:mnd
|
||||
ip := strings.TrimSpace(parts[0])
|
||||
|
||||
if ip != "" {
|
||||
return ip
|
||||
}
|
||||
}
|
||||
|
||||
if realIP := request.Header.Get("X-Real-IP"); realIP != "" {
|
||||
return strings.TrimSpace(realIP)
|
||||
}
|
||||
|
||||
host, _, err := net.SplitHostPort(request.RemoteAddr)
|
||||
if err != nil {
|
||||
return request.RemoteAddr
|
||||
@@ -188,7 +172,7 @@ func (hdlr *Handlers) handleCreateSession(
|
||||
|
||||
var payload createRequest
|
||||
|
||||
err := json.NewDecoder(request.Body).Decode(&payload)
|
||||
err := decodeJSON(request, &payload)
|
||||
if err != nil {
|
||||
hdlr.respondError(
|
||||
writer, request,
|
||||
@@ -925,7 +909,7 @@ func (hdlr *Handlers) HandleSendCommand() http.HandlerFunc {
|
||||
|
||||
var payload commandRequest
|
||||
|
||||
err := json.NewDecoder(request.Body).Decode(&payload)
|
||||
err := decodeJSON(request, &payload)
|
||||
if err != nil {
|
||||
hdlr.respondError(
|
||||
writer, request,
|
||||
@@ -3238,7 +3222,7 @@ func (hdlr *Handlers) handleWho(
|
||||
// 315 RPL_ENDOFWHO (empty result)
|
||||
hdlr.enqueueNumeric(
|
||||
ctx, clientID, irc.RplEndOfWho, nick,
|
||||
[]string{target},
|
||||
[]string{channel},
|
||||
"End of /WHO list",
|
||||
)
|
||||
hdlr.broker.Notify(sessionID)
|
||||
|
||||
+442
-145
@@ -47,22 +47,25 @@ import (
|
||||
const templateDBURL = "file:template?mode=memory&cache=shared"
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
db.SetBcryptCost(bcrypt.MinCost)
|
||||
template := fx.New(
|
||||
fx.NopLogger,
|
||||
fx.Supply(
|
||||
&globals.Globals{},
|
||||
&config.Config{DBURL: templateDBURL},
|
||||
),
|
||||
fx.Provide(logger.New, newTestDB),
|
||||
// fx makes the database only if something asks for it.
|
||||
fx.Invoke(func(*db.Database) {}),
|
||||
)
|
||||
|
||||
template, err := sql.Open("sqlite", templateDBURL)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = db.NewTestDatabaseFromConn(template).
|
||||
RunMigrations(context.Background())
|
||||
err := template.Start(context.Background())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
code := m.Run()
|
||||
|
||||
_ = template.Close()
|
||||
_ = template.Stop(context.Background())
|
||||
|
||||
os.Exit(code)
|
||||
}
|
||||
@@ -137,38 +140,51 @@ type testServer struct {
|
||||
t *testing.T
|
||||
fxApp *fxtest.App
|
||||
broker *broker.Broker
|
||||
dbURL string
|
||||
database *db.Database
|
||||
handlers *handlers.Handlers
|
||||
}
|
||||
|
||||
// viperMu makes tests start their servers one at a time,
|
||||
// because config.New writes the global viper instance and
|
||||
// the server reads it as it starts.
|
||||
//
|
||||
//nolint:gochecknoglobals // guards a global, so must be one
|
||||
var viperMu sync.Mutex
|
||||
const (
|
||||
// maxMessageSize is MAX_MESSAGE_SIZE on every test server, the
|
||||
// setting's default.
|
||||
maxMessageSize = 4096
|
||||
|
||||
// loginRateLimit is LOGIN_RATE_LIMIT on every test server, the
|
||||
// setting's default.
|
||||
loginRateLimit = 1
|
||||
|
||||
// loginRateBurst is LOGIN_RATE_BURST on every test server, the
|
||||
// setting's default.
|
||||
loginRateBurst = 5
|
||||
)
|
||||
|
||||
func newTestServer(
|
||||
t *testing.T,
|
||||
) *testServer {
|
||||
t.Helper()
|
||||
|
||||
return newTestServerWith(t, 0, "", "")
|
||||
return newTestServerWith(t, 0, "", "", config.DefaultTrustedProxies)
|
||||
}
|
||||
|
||||
func newTestServerWith(
|
||||
t *testing.T,
|
||||
hashcashBits int,
|
||||
operName, operPassword string,
|
||||
operName, operPassword, trustedProxies string,
|
||||
) *testServer {
|
||||
t.Helper()
|
||||
|
||||
viperMu.Lock()
|
||||
defer viperMu.Unlock()
|
||||
|
||||
dbURL := dbtest.NewURL(t)
|
||||
copyTemplateDB(t, dbURL)
|
||||
|
||||
trusted, err := config.ParseTrustedProxies(trustedProxies)
|
||||
if err != nil {
|
||||
t.Fatalf("parse trusted proxies: %v", err)
|
||||
}
|
||||
|
||||
// Each duration setting defaults to 30 days.
|
||||
const thirtyDays = "720h"
|
||||
|
||||
var (
|
||||
srv *server.Server
|
||||
brk *broker.Broker
|
||||
@@ -180,31 +196,25 @@ func newTestServerWith(
|
||||
fx.Provide(
|
||||
newTestGlobals,
|
||||
logger.New,
|
||||
func(
|
||||
lifecycle fx.Lifecycle,
|
||||
globs *globals.Globals,
|
||||
log *logger.Logger,
|
||||
) (*config.Config, error) {
|
||||
cfg, err := config.New(
|
||||
lifecycle, config.Params{ //nolint:exhaustruct
|
||||
Globals: globs, Logger: log,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"test config: %w", err,
|
||||
)
|
||||
// Built here rather than by config.New, which writes
|
||||
// the global viper instance, so servers start in
|
||||
// parallel.
|
||||
func() *config.Config {
|
||||
return &config.Config{
|
||||
DBURL: dbURL,
|
||||
// One line: each line is a database write per new session.
|
||||
MOTD: "neoirc test server",
|
||||
MaxMessageSize: maxMessageSize,
|
||||
HashcashBits: hashcashBits,
|
||||
OperName: operName,
|
||||
OperPassword: operPassword,
|
||||
MessageMaxAge: thirtyDays,
|
||||
QueueMaxAge: thirtyDays,
|
||||
SessionIdleTimeout: thirtyDays,
|
||||
LoginRateLimit: loginRateLimit,
|
||||
LoginRateBurst: loginRateBurst,
|
||||
TrustedProxies: trusted,
|
||||
}
|
||||
|
||||
cfg.DBURL = dbURL
|
||||
cfg.Port = 0
|
||||
// One line: each line is a database write per new session.
|
||||
cfg.MOTD = "neoirc test server"
|
||||
cfg.HashcashBits = hashcashBits
|
||||
cfg.OperName = operName
|
||||
cfg.OperPassword = operPassword
|
||||
|
||||
return cfg, nil
|
||||
},
|
||||
newTestDB,
|
||||
broker.New,
|
||||
@@ -231,6 +241,7 @@ func newTestServerWith(
|
||||
t: t,
|
||||
fxApp: app,
|
||||
broker: brk,
|
||||
dbURL: dbURL,
|
||||
database: database,
|
||||
handlers: hdlr,
|
||||
}
|
||||
@@ -250,7 +261,7 @@ func newTestDB(
|
||||
cfg *config.Config,
|
||||
) (*db.Database, error) {
|
||||
database, err := db.New(lifecycle, db.Params{ //nolint:exhaustruct
|
||||
Logger: log, Config: cfg,
|
||||
Logger: log, Config: cfg, PasswordCost: bcrypt.MinCost,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("test db: %w", err)
|
||||
@@ -797,6 +808,101 @@ func TestCreateSessionMalformed(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func (tserver *testServer) postBody(
|
||||
path, token, body string,
|
||||
) (int, map[string]any) {
|
||||
tserver.t.Helper()
|
||||
|
||||
resp, err := doRequestAuth(
|
||||
tserver.t, http.MethodPost, tserver.url(path), token,
|
||||
strings.NewReader(body),
|
||||
)
|
||||
if err != nil {
|
||||
tserver.t.Fatalf("post %s: %v", path, err)
|
||||
}
|
||||
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
var result map[string]any
|
||||
|
||||
_ = json.NewDecoder(resp.Body).Decode(&result)
|
||||
|
||||
return resp.StatusCode, result
|
||||
}
|
||||
|
||||
// TestRequestBodyLimit sends each endpoint a small valid JSON value
|
||||
// followed by other bytes. Only whitespace may follow the value, and
|
||||
// the whole body counts towards MAX_MESSAGE_SIZE.
|
||||
func TestRequestBodyLimit(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const nick = "alice_bodylimit"
|
||||
|
||||
endpoints := []struct {
|
||||
name, path, value string
|
||||
acceptedStatus int
|
||||
}{
|
||||
{
|
||||
"create session", apiSession,
|
||||
`{"nick":"bob_bodylimit"}`,
|
||||
http.StatusCreated,
|
||||
},
|
||||
{
|
||||
"login", "/api/v1/login",
|
||||
`{"nick":"` + nick + `","password":"` + testPassword + `"}`,
|
||||
http.StatusOK,
|
||||
},
|
||||
{
|
||||
"post message", apiMessages,
|
||||
`{"command":"PING"}`,
|
||||
http.StatusOK,
|
||||
},
|
||||
}
|
||||
|
||||
for _, endpoint := range endpoints {
|
||||
t.Run(endpoint.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tserver := newTestServer(t)
|
||||
token := tserver.createSession(nick)
|
||||
|
||||
tserver.setPassword(nick)
|
||||
|
||||
bytesToLimit := maxMessageSize - len(endpoint.value)
|
||||
|
||||
for _, trailing := range []string{
|
||||
strings.Repeat(" ", bytesToLimit+1),
|
||||
strings.Repeat("x", bytesToLimit+1),
|
||||
"x",
|
||||
"{}",
|
||||
} {
|
||||
status, result := tserver.postBody(
|
||||
endpoint.path, token, endpoint.value+trailing,
|
||||
)
|
||||
if status != http.StatusBadRequest ||
|
||||
result["error"] != "invalid request body" {
|
||||
t.Errorf(
|
||||
"%d bytes of %.10q after the value: expected "+
|
||||
"400 invalid request body, got %d: %v",
|
||||
len(trailing), trailing, status, result,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
status, result := tserver.postBody(
|
||||
endpoint.path, token,
|
||||
endpoint.value+strings.Repeat(" ", bytesToLimit),
|
||||
)
|
||||
if status != endpoint.acceptedStatus {
|
||||
t.Errorf(
|
||||
"body of exactly the limit: expected %d, got %d: %v",
|
||||
endpoint.acceptedStatus, status, result,
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthNoCookie(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -1647,54 +1753,67 @@ func TestConcurrentQuitsSendOneQuit(t *testing.T) {
|
||||
// that the second session then holds one QUIT from the
|
||||
// first, and that the first session is deleted: its tokens
|
||||
// are refused and its nick is free. It does all this
|
||||
// quitRaceTrials times, each on a new server, because each
|
||||
// trial logs in and logins are rate limited.
|
||||
// quitRaceTrials times, at once, each on a new server, because
|
||||
// each trial logs in and logins are rate limited.
|
||||
func checkQuitRace(
|
||||
t *testing.T,
|
||||
leave func(tserver *testServer, tokens []string),
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
for trial := range quitRaceTrials {
|
||||
t.Run(fmt.Sprintf("trial %d", trial), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
checkQuitRaceOnce(t, newTestServer(t), leave)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// checkQuitRaceOnce runs one trial of checkQuitRace on
|
||||
// tserver.
|
||||
func checkQuitRaceOnce(
|
||||
t *testing.T,
|
||||
tserver *testServer,
|
||||
leave func(tserver *testServer, tokens []string),
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
const nick = "racingquitter"
|
||||
|
||||
for trial := range quitRaceTrials {
|
||||
tserver := newTestServer(t)
|
||||
firstToken := tserver.createSession(nick)
|
||||
observerToken := tserver.createSession("raceobserver")
|
||||
firstToken := tserver.createSession(nick)
|
||||
observerToken := tserver.createSession("raceobserver")
|
||||
|
||||
tserver.sendCommand(firstToken, map[string]any{
|
||||
commandKey: irc.CmdPass, bodyKey: []string{testPassword},
|
||||
tserver.setPassword(nick)
|
||||
|
||||
tokens := []string{firstToken, tserver.login(nick)}
|
||||
|
||||
for _, token := range []string{firstToken, observerToken} {
|
||||
tserver.sendCommand(token, map[string]any{
|
||||
commandKey: joinCmd, toKey: "#quitrace",
|
||||
})
|
||||
|
||||
tokens := []string{firstToken, tserver.login(nick)}
|
||||
|
||||
for _, token := range []string{firstToken, observerToken} {
|
||||
tserver.sendCommand(token, map[string]any{
|
||||
commandKey: joinCmd, toKey: "#quitrace",
|
||||
})
|
||||
}
|
||||
|
||||
_, lastID := tserver.pollMessages(observerToken, 0)
|
||||
|
||||
leave(tserver, tokens)
|
||||
|
||||
msgs, _ := tserver.pollMessages(observerToken, lastID)
|
||||
|
||||
quits := countQuits(msgs, nick)
|
||||
if quits != 1 {
|
||||
t.Errorf("trial %d: %d QUITs, want 1: %v", trial, quits, msgs)
|
||||
}
|
||||
|
||||
for _, token := range tokens {
|
||||
status, _ := tserver.getState(token)
|
||||
if status != http.StatusUnauthorized {
|
||||
t.Errorf("trial %d: state got %d, want 401", trial, status)
|
||||
}
|
||||
}
|
||||
|
||||
// createSession fails the test if the nick is taken.
|
||||
tserver.createSession(nick)
|
||||
}
|
||||
|
||||
_, lastID := tserver.pollMessages(observerToken, 0)
|
||||
|
||||
leave(tserver, tokens)
|
||||
|
||||
msgs, _ := tserver.pollMessages(observerToken, lastID)
|
||||
|
||||
quits := countQuits(msgs, nick)
|
||||
if quits != 1 {
|
||||
t.Errorf("%d QUITs, want 1: %v", quits, msgs)
|
||||
}
|
||||
|
||||
for _, token := range tokens {
|
||||
status, _ := tserver.getState(token)
|
||||
if status != http.StatusUnauthorized {
|
||||
t.Errorf("state got %d, want 401", status)
|
||||
}
|
||||
}
|
||||
|
||||
// createSession fails the test if the nick is taken.
|
||||
tserver.createSession(nick)
|
||||
}
|
||||
|
||||
// TestLogoutOfOneClientKeepsSession checks that when one of
|
||||
@@ -1714,9 +1833,7 @@ func TestLogoutOfOneClientKeepsSession(t *testing.T) {
|
||||
firstToken := tserver.createSession(nick)
|
||||
observerToken := tserver.createSession("logoutobserver")
|
||||
|
||||
tserver.sendCommand(firstToken, map[string]any{
|
||||
commandKey: irc.CmdPass, bodyKey: []string{testPassword},
|
||||
})
|
||||
tserver.setPassword(nick)
|
||||
|
||||
secondToken := tserver.login(nick)
|
||||
|
||||
@@ -1786,15 +1903,12 @@ func TestFailedLogoutChangesNothing(t *testing.T) {
|
||||
|
||||
_, lastID := tserver.pollMessages(observerToken, 0)
|
||||
|
||||
_, err := tserver.database.GetDB().ExecContext(t.Context(),
|
||||
dbtest.Exec(t, tserver.dbURL,
|
||||
`CREATE TRIGGER keep_sessions
|
||||
BEFORE DELETE ON sessions
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'sessions are kept');
|
||||
END`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
resp, err := doRequestAuth(
|
||||
t, http.MethodPost, tserver.url(apiLogout), token, nil,
|
||||
@@ -1837,11 +1951,28 @@ const (
|
||||
// return once the session has left.
|
||||
quitWakeDeadline = 5 * time.Second
|
||||
|
||||
// pollWaitStep is how often checkQuitWakesPoll checks
|
||||
// pollWaitStep is how often waitForLongPoll checks
|
||||
// whether the long poll is waiting yet.
|
||||
pollWaitStep = 10 * time.Millisecond
|
||||
)
|
||||
|
||||
// waitForLongPoll waits until a long poll of the session
|
||||
// sessionID is waiting, and fails the test if none is within
|
||||
// quitWakeDeadline.
|
||||
func (tserver *testServer) waitForLongPoll(sessionID int64) {
|
||||
tserver.t.Helper()
|
||||
|
||||
deadline := time.Now().Add(quitWakeDeadline)
|
||||
|
||||
for tserver.broker.ListenerCount(sessionID) == 0 {
|
||||
if time.Now().After(deadline) {
|
||||
tserver.t.Fatal("the long poll did not start waiting")
|
||||
}
|
||||
|
||||
time.Sleep(pollWaitStep)
|
||||
}
|
||||
}
|
||||
|
||||
// TestQuitWakesPeerLongPoll checks that a session's QUIT
|
||||
// wakes the long poll of a user who shares a channel with
|
||||
// it.
|
||||
@@ -1916,15 +2047,7 @@ func checkQuitWakesPoll(
|
||||
msgs, pollErr = tserver.longPoll(peerToken, lastID, quitWakePollTimeout)
|
||||
}()
|
||||
|
||||
deadline := time.Now().Add(quitWakeDeadline)
|
||||
|
||||
for tserver.broker.ListenerCount(peerID) == 0 {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("the long poll did not start waiting")
|
||||
}
|
||||
|
||||
time.Sleep(pollWaitStep)
|
||||
}
|
||||
tserver.waitForLongPoll(peerID)
|
||||
|
||||
leave(tserver, token)
|
||||
|
||||
@@ -2028,6 +2151,24 @@ func (tserver *testServer) longPoll(
|
||||
return result.Messages, nil
|
||||
}
|
||||
|
||||
// setPassword gives the session nick the password testPassword
|
||||
// without sending PASS. It hashes at bcrypt.MinCost, and a login
|
||||
// compares at the cost stored in the hash, so both are fast.
|
||||
func (tserver *testServer) setPassword(nick string) {
|
||||
tserver.t.Helper()
|
||||
|
||||
hash, err := bcrypt.GenerateFromPassword(
|
||||
[]byte(testPassword), bcrypt.MinCost,
|
||||
)
|
||||
if err != nil {
|
||||
tserver.t.Fatalf("hash password: %v", err)
|
||||
}
|
||||
|
||||
dbtest.Exec(tserver.t, tserver.dbURL,
|
||||
"UPDATE sessions SET password_hash = '"+string(hash)+
|
||||
"' WHERE nick = '"+nick+"'")
|
||||
}
|
||||
|
||||
// login logs in to the session nick with testPassword and
|
||||
// returns the new client's token.
|
||||
func (tserver *testServer) login(nick string) string {
|
||||
@@ -2736,7 +2877,12 @@ func TestLongPoll(t *testing.T) {
|
||||
pollMsgs = result.Messages
|
||||
}()
|
||||
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
bobID, err := tserver.database.GetSessionByNick(t.Context(), "lp_bob")
|
||||
if err != nil {
|
||||
t.Fatalf("get session of lp_bob: %v", err)
|
||||
}
|
||||
|
||||
tserver.waitForLongPoll(bobID)
|
||||
|
||||
tserver.sendCommand(aliceToken, map[string]any{
|
||||
commandKey: privmsgCmd,
|
||||
@@ -2895,18 +3041,18 @@ func TestGetMessagesRemovesWaitChannel(t *testing.T) {
|
||||
longTimeout = 10
|
||||
|
||||
// shortTimeout, in seconds, is the timeout of the
|
||||
// requests that wait it out.
|
||||
// request that waits it out.
|
||||
shortTimeout = 1
|
||||
)
|
||||
|
||||
// queued is whether a message is queued before the request,
|
||||
// whose timeout is in seconds. failingRead is as for
|
||||
// getMessagesFailingRead. wantReads is how many times the
|
||||
// handler reads the queue: once if it answers at once,
|
||||
// whose timeout is in seconds. failingRead and notified are
|
||||
// as for getMessagesFailingRead. wantReads is how many times
|
||||
// the handler reads the queue: once if it answers at once,
|
||||
// twice if it waits first.
|
||||
tests := []struct {
|
||||
name string
|
||||
queued bool
|
||||
queued, notified bool
|
||||
timeout, failingRead int
|
||||
wantReads, wantStatus int
|
||||
}{
|
||||
@@ -2927,9 +3073,9 @@ func TestGetMessagesRemovesWaitChannel(t *testing.T) {
|
||||
wantReads: 1, wantStatus: http.StatusInternalServerError,
|
||||
},
|
||||
{
|
||||
name: "read after the wait failed", timeout: shortTimeout,
|
||||
failingRead: 2, wantReads: 2,
|
||||
wantStatus: http.StatusInternalServerError,
|
||||
name: "read after the wait failed", notified: true,
|
||||
timeout: handlers.MaxLongPollTimeout, failingRead: 2,
|
||||
wantReads: 2, wantStatus: http.StatusInternalServerError,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -2961,7 +3107,7 @@ func TestGetMessagesRemovesWaitChannel(t *testing.T) {
|
||||
}
|
||||
|
||||
status, reads := tserver.getMessagesFailingRead(
|
||||
token, lastID, tc.timeout, tc.failingRead,
|
||||
token, sessionID, lastID, tc.timeout, tc.failingRead, tc.notified,
|
||||
)
|
||||
if status != tc.wantStatus {
|
||||
t.Errorf("status %d, want %d", status, tc.wantStatus)
|
||||
@@ -2978,14 +3124,17 @@ func TestGetMessagesRemovesWaitChannel(t *testing.T) {
|
||||
}
|
||||
|
||||
// getMessagesFailingRead sends GET /api/v1/messages for the
|
||||
// client token with timeout and after afterID. The handler's
|
||||
// read of the queue numbered failingRead, counting from 1,
|
||||
// fails; with 0, none does. It returns the response status
|
||||
// and how many times the handler read the queue.
|
||||
// client token of the session sessionID, with timeout and
|
||||
// after afterID. The handler's read of the queue numbered
|
||||
// failingRead, counting from 1, fails; with 0, none does.
|
||||
// With notified, it notifies the broker once the request
|
||||
// waits. It returns the response status and how many times
|
||||
// the handler read the queue.
|
||||
func (tserver *testServer) getMessagesFailingRead(
|
||||
token string,
|
||||
afterID int64,
|
||||
sessionID, afterID int64,
|
||||
timeout, failingRead int,
|
||||
notified bool,
|
||||
) (int, int) {
|
||||
tserver.t.Helper()
|
||||
|
||||
@@ -3007,20 +3156,45 @@ func (tserver *testServer) getMessagesFailingRead(
|
||||
)
|
||||
})
|
||||
|
||||
resp, err := doRequestAuth(
|
||||
tserver.t, http.MethodGet,
|
||||
tserver.url(fmt.Sprintf(
|
||||
"%s?timeout=%d&after=%d", apiMessages, timeout, afterID,
|
||||
)),
|
||||
token, nil,
|
||||
var (
|
||||
status int
|
||||
err error
|
||||
)
|
||||
|
||||
answered := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
defer close(answered)
|
||||
|
||||
var resp *http.Response
|
||||
|
||||
resp, err = doRequestAuth(
|
||||
tserver.t, http.MethodGet,
|
||||
tserver.url(fmt.Sprintf(
|
||||
"%s?timeout=%d&after=%d", apiMessages, timeout, afterID,
|
||||
)),
|
||||
token, nil,
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
status = resp.StatusCode
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
|
||||
if notified {
|
||||
tserver.waitForLongPoll(sessionID)
|
||||
tserver.broker.Notify(sessionID)
|
||||
}
|
||||
|
||||
<-answered
|
||||
|
||||
if err != nil {
|
||||
tserver.t.Fatalf("get messages: %v", err)
|
||||
}
|
||||
|
||||
_ = resp.Body.Close()
|
||||
|
||||
return resp.StatusCode, reads
|
||||
return status, reads
|
||||
}
|
||||
|
||||
// TestLongPollRemovesWaitChannelWhenClientGivesUp checks that
|
||||
@@ -3635,13 +3809,8 @@ func TestLoginValid(t *testing.T) {
|
||||
|
||||
tserver := newTestServer(t)
|
||||
|
||||
// Create session and set password via PASS command.
|
||||
token := tserver.createSession("loginuser")
|
||||
|
||||
tserver.sendCommand(token, map[string]any{
|
||||
commandKey: irc.CmdPass,
|
||||
bodyKey: []string{testPassword},
|
||||
})
|
||||
tserver.createSession("loginuser")
|
||||
tserver.setPassword("loginuser")
|
||||
|
||||
// Login with nick + password.
|
||||
loginBody, err := json.Marshal(map[string]string{
|
||||
@@ -3705,13 +3874,8 @@ func TestLoginWrongPassword(t *testing.T) {
|
||||
|
||||
tserver := newTestServer(t)
|
||||
|
||||
// Create session and set password via PASS command.
|
||||
token := tserver.createSession("wrongpwuser")
|
||||
|
||||
tserver.sendCommand(token, map[string]any{
|
||||
commandKey: irc.CmdPass,
|
||||
bodyKey: []string{"correctpass1"},
|
||||
})
|
||||
tserver.createSession("wrongpwuser")
|
||||
tserver.setPassword("wrongpwuser")
|
||||
|
||||
postJSONExpectStatus(
|
||||
t, tserver, "/api/v1/login",
|
||||
@@ -4039,6 +4203,31 @@ func assertWhoReplyUsername(
|
||||
)
|
||||
}
|
||||
|
||||
func TestWhoUnknownChannelEndReplyAddsHash(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tserver := newTestServer(t)
|
||||
|
||||
token := tserver.createSession("whoasker")
|
||||
|
||||
_, lastID := tserver.pollMessages(token, 0)
|
||||
|
||||
msgs, _ := sendAndPoll(tserver, token, lastID, map[string]any{
|
||||
commandKey: irc.CmdWho,
|
||||
toKey: "nosuch",
|
||||
})
|
||||
|
||||
endOfWho := findNumericWithParams(msgs, "315")
|
||||
if endOfWho == nil {
|
||||
t.Fatalf("expected RPL_ENDOFWHO (315), got %v", msgs)
|
||||
}
|
||||
|
||||
params := getNumericParams(endOfWho)
|
||||
if !slices.Equal(params, []string{"#nosuch"}) {
|
||||
t.Fatalf("expected params [#nosuch], got %v", params)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionUsernameDefault(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -4152,18 +4341,123 @@ func TestLoginRateLimitExceeded(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginRateLimitIgnoresForwardedHeaders(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// The test client connects from 127.0.0.1, which is outside the
|
||||
// default trusted proxies, so the server must ignore these headers.
|
||||
tests := []struct {
|
||||
name string
|
||||
header string
|
||||
}{
|
||||
{name: "X-Forwarded-For", header: "X-Forwarded-For"},
|
||||
{name: "X-Real-IP", header: "X-Real-IP"},
|
||||
}
|
||||
|
||||
loginBody, err := json.Marshal(map[string]string{
|
||||
nickKey: "unknownnick", passwordKey: "wrongpassword",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tserver := newTestServer(t)
|
||||
|
||||
// The default burst is 5 attempts, so the sixth gets 429
|
||||
// although each claims a new address.
|
||||
var status int
|
||||
|
||||
for attempt := range 6 {
|
||||
request, rErr := http.NewRequestWithContext(
|
||||
t.Context(), http.MethodPost,
|
||||
tserver.url("/api/v1/login"),
|
||||
bytes.NewReader(loginBody),
|
||||
)
|
||||
if rErr != nil {
|
||||
t.Fatal(rErr)
|
||||
}
|
||||
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set(
|
||||
test.header, fmt.Sprintf("203.0.113.%d", attempt+1),
|
||||
)
|
||||
|
||||
resp, dErr := http.DefaultClient.Do(request)
|
||||
if dErr != nil {
|
||||
t.Fatal(dErr)
|
||||
}
|
||||
|
||||
status = resp.StatusCode
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
|
||||
if status != http.StatusTooManyRequests {
|
||||
t.Fatalf("sixth attempt got %d, want 429", status)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginRateLimitPerClientBehindTrustedProxy(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// The test client connects from 127.0.0.1. Trusted as a proxy, it
|
||||
// passes each client's address in X-Forwarded-For.
|
||||
tserver := newTestServerWith(t, 0, "", "", "127.0.0.1")
|
||||
|
||||
// An empty JSON object is refused after the rate limit counts the
|
||||
// attempt, so no attempt checks a password or looks up a hostname.
|
||||
loginFrom := func(forwardedFor string) int {
|
||||
t.Helper()
|
||||
|
||||
request, err := http.NewRequestWithContext(
|
||||
t.Context(), http.MethodPost,
|
||||
tserver.url("/api/v1/login"), strings.NewReader("{}"),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("X-Forwarded-For", forwardedFor)
|
||||
|
||||
resp, err := http.DefaultClient.Do(request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_ = resp.Body.Close()
|
||||
|
||||
return resp.StatusCode
|
||||
}
|
||||
|
||||
// The default burst is 5 attempts per address.
|
||||
for range 5 {
|
||||
loginFrom("203.0.113.1")
|
||||
}
|
||||
|
||||
status := loginFrom("203.0.113.1")
|
||||
if status != http.StatusTooManyRequests {
|
||||
t.Errorf("sixth attempt from 203.0.113.1 got %d, want 429", status)
|
||||
}
|
||||
|
||||
status = loginFrom("203.0.113.2")
|
||||
if status == http.StatusTooManyRequests {
|
||||
t.Error("first attempt from 203.0.113.2 got 429")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginRateLimitAllowsNormalUse(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tserver := newTestServer(t)
|
||||
|
||||
// Create session and set password via PASS command.
|
||||
token := tserver.createSession("normaluser")
|
||||
|
||||
tserver.sendCommand(token, map[string]any{
|
||||
commandKey: irc.CmdPass,
|
||||
bodyKey: []string{testPassword},
|
||||
})
|
||||
tserver.createSession("normaluser")
|
||||
tserver.setPassword("normaluser")
|
||||
|
||||
// A single login should succeed without rate limiting.
|
||||
loginBody, err := json.Marshal(map[string]string{
|
||||
@@ -4844,7 +5138,9 @@ func TestRefusedSessionLeavesStampUnspent(t *testing.T) {
|
||||
badNick = "bad nick"
|
||||
)
|
||||
|
||||
tserver := newTestServerWith(t, bits, "", "")
|
||||
tserver := newTestServerWith(
|
||||
t, bits, "", "", config.DefaultTrustedProxies,
|
||||
)
|
||||
|
||||
// A server with no SERVER_NAME takes stamps for "neoirc".
|
||||
status := postSessionWithStamp(
|
||||
@@ -5050,6 +5346,7 @@ func newTestServerWithOper(
|
||||
|
||||
return newTestServerWith(
|
||||
t, 0, testOperName, testOperPassword,
|
||||
config.DefaultTrustedProxies,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -55,7 +54,7 @@ func (hdlr *Handlers) handleLogin(
|
||||
|
||||
var payload loginRequest
|
||||
|
||||
err := json.NewDecoder(request.Body).Decode(&payload)
|
||||
err := decodeJSON(request, &payload)
|
||||
if err != nil {
|
||||
hdlr.respondError(
|
||||
writer, request,
|
||||
|
||||
@@ -6,6 +6,10 @@ import (
|
||||
"sneak.berlin/go/neoirc/internal/db"
|
||||
)
|
||||
|
||||
// MaxLongPollTimeout is the longest HandleGetMessages waits, in
|
||||
// seconds.
|
||||
const MaxLongPollTimeout = maxLongPollTimeout
|
||||
|
||||
// SetPollMessages makes HandleGetMessages read the client
|
||||
// output queue with poll in place of Database.PollMessages.
|
||||
func (hdlr *Handlers) SetPollMessages(
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -159,6 +160,18 @@ func (hdlr *Handlers) respondError(
|
||||
)
|
||||
}
|
||||
|
||||
// decodeJSON reads the whole request body and decodes it into dest.
|
||||
// It fails if the body is larger than the http.MaxBytesReader limit,
|
||||
// or holds anything other than one JSON value and whitespace.
|
||||
func decodeJSON(request *http.Request, dest any) error {
|
||||
body, err := io.ReadAll(request.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal(body, dest)
|
||||
}
|
||||
|
||||
func (hdlr *Handlers) idleTimeout() time.Duration {
|
||||
raw := hdlr.params.Config.SessionIdleTimeout
|
||||
if raw == "" {
|
||||
|
||||
@@ -70,6 +70,10 @@ type Conn struct {
|
||||
closed bool
|
||||
killed bool
|
||||
|
||||
// pollInterval is how often the relay checks the client
|
||||
// output queue when no broker notification arrives.
|
||||
pollInterval time.Duration
|
||||
|
||||
// quitReason is the reason in the QUIT that peers are
|
||||
// sent when the connection ends. The client's QUIT
|
||||
// replaces it.
|
||||
@@ -83,6 +87,7 @@ func newConn(
|
||||
brk *broker.Broker,
|
||||
cfg *config.Config,
|
||||
svc *service.Service,
|
||||
poll time.Duration,
|
||||
) *Conn {
|
||||
host, _, _ := net.SplitHostPort(tcpConn.RemoteAddr().String())
|
||||
|
||||
@@ -101,6 +106,8 @@ func newConn(
|
||||
serverSfx: srvName,
|
||||
remoteIP: host,
|
||||
quitReason: "Connection closed",
|
||||
|
||||
pollInterval: poll,
|
||||
}
|
||||
|
||||
conn.commands = conn.buildCommandMap()
|
||||
|
||||
@@ -14,16 +14,16 @@ import (
|
||||
)
|
||||
|
||||
// NewTestServer creates a Server suitable for testing, whose
|
||||
// connections use interval and deadline in place of
|
||||
// pingInterval and pongDeadline, and resolve in place of
|
||||
// resolveHost.
|
||||
// connections use interval, deadline and poll in place of
|
||||
// pingInterval, pongDeadline and pollInterval, and resolve in
|
||||
// place of resolveHost.
|
||||
// The caller must call Stop() when finished.
|
||||
func NewTestServer(
|
||||
log *slog.Logger,
|
||||
cfg *config.Config,
|
||||
database *db.Database,
|
||||
brk *broker.Broker,
|
||||
interval, deadline time.Duration,
|
||||
interval, deadline, poll time.Duration,
|
||||
resolve func(ctx context.Context, addr string) string,
|
||||
) *Server {
|
||||
globs := &globals.Globals{
|
||||
@@ -46,10 +46,18 @@ func NewTestServer(
|
||||
|
||||
pingInterval: interval,
|
||||
pongDeadline: deadline,
|
||||
pollInterval: poll,
|
||||
stopTimeout: stopTimeout,
|
||||
resolveHost: resolve,
|
||||
}
|
||||
}
|
||||
|
||||
// SetStopTimeout makes Stop wait at most timeout for the
|
||||
// connections' cleanup.
|
||||
func (s *Server) SetStopTimeout(timeout time.Duration) {
|
||||
s.stopTimeout = timeout
|
||||
}
|
||||
|
||||
// ResolveHost is the reverse DNS lookup that a server made by
|
||||
// New does for each client.
|
||||
func ResolveHost(ctx context.Context, addr string) string {
|
||||
@@ -99,10 +107,6 @@ const PingInterval = pingInterval
|
||||
// for a line before it closes the connection.
|
||||
const PongDeadline = pongDeadline
|
||||
|
||||
// StopTimeout is the longest Stop waits for the connections'
|
||||
// cleanup.
|
||||
const StopTimeout = stopTimeout
|
||||
|
||||
// NewTestConn returns a Conn for nick on tcpConn, without a
|
||||
// server, database or service behind it.
|
||||
func NewTestConn(
|
||||
@@ -111,7 +115,7 @@ func NewTestConn(
|
||||
tcpConn net.Conn,
|
||||
nick string,
|
||||
) *Conn {
|
||||
conn := newConn(tcpConn, log, nil, nil, cfg, nil)
|
||||
conn := newConn(tcpConn, log, nil, nil, cfg, nil, pollInterval)
|
||||
conn.nick = nick
|
||||
|
||||
return conn
|
||||
|
||||
@@ -3,7 +3,6 @@ package ircserver_test
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
)
|
||||
@@ -1508,9 +1507,6 @@ func TestIntegrationThirdClientObserver(t *testing.T) {
|
||||
bob.joinAndDrain("#trio")
|
||||
carol.joinAndDrain("#trio")
|
||||
|
||||
// Drain join notifications.
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// alice sends; both bob and carol should receive.
|
||||
alice.send("PRIVMSG #trio :hello trio")
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
func (c *Conn) relayMessages(ctx context.Context) {
|
||||
// Use a ticker as a fallback; primary wakeup is via
|
||||
// broker notification.
|
||||
ticker := time.NewTicker(pollInterval)
|
||||
ticker := time.NewTicker(c.pollInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
|
||||
@@ -8,6 +8,9 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
||||
"sneak.berlin/go/neoirc/internal/ircserver"
|
||||
"sneak.berlin/go/neoirc/internal/service"
|
||||
)
|
||||
@@ -56,7 +59,11 @@ func (env *testEnv) queueMessage(
|
||||
func TestRelayKeepsOneWaitChannel(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
env := newTestEnvWithResolver(
|
||||
t, &config.Config{ServerName: testServerName},
|
||||
ircserver.PingInterval, ircserver.PongDeadline,
|
||||
ircserver.PollInterval, ircserver.ResolveHost,
|
||||
)
|
||||
|
||||
client := env.dial(t)
|
||||
client.register("idle")
|
||||
@@ -117,6 +124,63 @@ func (env *testEnv) checkOneWaitChannel(
|
||||
}
|
||||
}
|
||||
|
||||
// TestRelayDeliversAfterFailedRead checks that when the relay
|
||||
// cannot read the client output queue, it waits for the next
|
||||
// notification, and delivers once the queue can be read again.
|
||||
func TestRelayDeliversAfterFailedRead(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
client := env.dial(t)
|
||||
client.register("reader")
|
||||
|
||||
sessionID, err := env.database.GetSessionByNick(t.Context(), "reader")
|
||||
if err != nil {
|
||||
t.Fatalf("get session of reader: %v", err)
|
||||
}
|
||||
|
||||
dbtest.Exec(t, env.dbURL,
|
||||
"ALTER TABLE client_queues RENAME TO hidden_queues")
|
||||
|
||||
// Each pass of the relay takes a wait channel and then reads
|
||||
// the queue. Once it holds a channel after the second
|
||||
// notification, the read after the first one has failed.
|
||||
for range 2 {
|
||||
env.waitForWaitChannel(t, sessionID)
|
||||
env.brk.Notify(sessionID)
|
||||
}
|
||||
|
||||
env.waitForWaitChannel(t, sessionID)
|
||||
|
||||
dbtest.Exec(t, env.dbURL,
|
||||
"ALTER TABLE hidden_queues RENAME TO client_queues")
|
||||
|
||||
env.queueMessage(
|
||||
t, "reader", "PRIVMSG", "carol", "reader", []string{"readable"},
|
||||
)
|
||||
|
||||
client.readUntil(func(l string) bool {
|
||||
return strings.Contains(l, "readable")
|
||||
})
|
||||
}
|
||||
|
||||
// waitForWaitChannel waits until the session sessionID holds a
|
||||
// broker wait channel, and fails the test if it does not
|
||||
// within testTimeout.
|
||||
func (env *testEnv) waitForWaitChannel(t *testing.T, sessionID int64) {
|
||||
t.Helper()
|
||||
|
||||
deadline := time.Now().Add(testTimeout)
|
||||
|
||||
for env.brk.ListenerCount(sessionID) == 0 {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("the relay is not waiting for a notification")
|
||||
}
|
||||
|
||||
time.Sleep(relayCheckStep)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRelayMultiLineBody checks that each line of a PRIVMSG
|
||||
// or NOTICE body sent over the HTTP API reaches an IRC
|
||||
// listener user as its own line.
|
||||
@@ -583,8 +647,10 @@ func ownSessionCopies(
|
||||
|
||||
// aliceWithHTTPClient connects alice to the IRC listener and
|
||||
// has her join #chan, which bob is on too, then adds an HTTP API
|
||||
// client to her session with the password her connection set,
|
||||
// as POST /api/v1/login does. It returns her connection, bob's
|
||||
// client to her session with a password, as POST /api/v1/login
|
||||
// does. It stores that password without sending PASS. It hashes
|
||||
// at bcrypt.MinCost, and a login compares at the cost stored in
|
||||
// the hash, so both are fast. It returns her connection, bob's
|
||||
// connection, her session and her HTTP API client.
|
||||
func aliceWithHTTPClient(
|
||||
t *testing.T, env *testEnv,
|
||||
@@ -594,15 +660,24 @@ func aliceWithHTTPClient(
|
||||
const password = "alicepass"
|
||||
|
||||
alice := env.dial(t)
|
||||
alice.send("PASS " + password)
|
||||
alice.register("alice")
|
||||
// Registration stores the password before the JOIN runs.
|
||||
alice.joinAndDrain("#chan")
|
||||
|
||||
bob := env.dial(t)
|
||||
bob.register("bob")
|
||||
bob.joinAndDrain("#chan")
|
||||
|
||||
hash, err := bcrypt.GenerateFromPassword(
|
||||
[]byte(password), bcrypt.MinCost,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("hash password: %v", err)
|
||||
}
|
||||
|
||||
dbtest.Exec(t, env.dbURL,
|
||||
"UPDATE sessions SET password_hash = '"+string(hash)+
|
||||
"' WHERE nick = 'alice'")
|
||||
|
||||
sessionID, httpClientID, _, err := env.database.LoginUser(
|
||||
t.Context(), "alice", password, "127.0.0.1", "localhost",
|
||||
)
|
||||
|
||||
@@ -53,6 +53,11 @@ type Server struct {
|
||||
pingInterval time.Duration
|
||||
pongDeadline time.Duration
|
||||
|
||||
// pollInterval and stopTimeout are the constants of the
|
||||
// same names, except in tests, which change them.
|
||||
pollInterval time.Duration
|
||||
stopTimeout time.Duration
|
||||
|
||||
// resolveHost is the function of the same name, except in
|
||||
// tests, which replace it with one that blocks.
|
||||
resolveHost func(ctx context.Context, addr string) string
|
||||
@@ -79,6 +84,8 @@ func New(
|
||||
|
||||
pingInterval: pingInterval,
|
||||
pongDeadline: pongDeadline,
|
||||
pollInterval: pollInterval,
|
||||
stopTimeout: stopTimeout,
|
||||
resolveHost: resolveHost,
|
||||
}
|
||||
|
||||
@@ -154,10 +161,10 @@ func (s *Server) stop() {
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(stopTimeout):
|
||||
case <-time.After(s.stopTimeout):
|
||||
s.log.Error(
|
||||
"irc connections not cleaned up before stop timeout",
|
||||
"timeout", stopTimeout,
|
||||
"timeout", s.stopTimeout,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -181,7 +188,7 @@ func (s *Server) acceptLoop(ctx context.Context) {
|
||||
|
||||
client := newConn(
|
||||
tcpConn, s.log,
|
||||
s.database, s.brk, s.cfg, s.svc,
|
||||
s.database, s.brk, s.cfg, s.svc, s.pollInterval,
|
||||
)
|
||||
|
||||
s.mu.Lock()
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"time"
|
||||
|
||||
"go.uber.org/fx/fxtest"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"sneak.berlin/go/neoirc/internal/broker"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/db"
|
||||
@@ -30,16 +31,20 @@ import (
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
const testTimeout = 5 * time.Second
|
||||
// testTimeout is how long a test waits for something before it
|
||||
// fails. A passing test never waits this long, so it is generous
|
||||
// enough for a heavily loaded host.
|
||||
const testTimeout = 30 * time.Second
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
db.SetBcryptCost(4)
|
||||
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
// testPollInterval is how often the relay of a test server
|
||||
// checks the client output queue without a broker
|
||||
// notification. It is so long that the relay delivers only on
|
||||
// notification, and a test fails when one is missing.
|
||||
const testPollInterval = time.Hour
|
||||
|
||||
// testEnv holds the shared test infrastructure.
|
||||
type testEnv struct {
|
||||
dbURL string
|
||||
database *db.Database
|
||||
brk *broker.Broker
|
||||
cfg *config.Config
|
||||
@@ -96,48 +101,45 @@ func newTestEnvWithPingTimes(
|
||||
t.Helper()
|
||||
|
||||
return newTestEnvWithResolver(
|
||||
t, cfg, interval, deadline, ircserver.ResolveHost,
|
||||
t, cfg, interval, deadline, testPollInterval,
|
||||
ircserver.ResolveHost,
|
||||
)
|
||||
}
|
||||
|
||||
// newTestEnvWithResolver is newTestEnvWithPingTimes, except
|
||||
// that the server looks up a client's hostname with resolve.
|
||||
// that the relay checks the client output queue every poll
|
||||
// without a broker notification, and the server looks up a
|
||||
// client's hostname with resolve.
|
||||
func newTestEnvWithResolver(
|
||||
t *testing.T,
|
||||
cfg *config.Config,
|
||||
interval, deadline time.Duration,
|
||||
interval, deadline, poll time.Duration,
|
||||
resolve func(ctx context.Context, addr string) string,
|
||||
) *testEnv {
|
||||
t.Helper()
|
||||
|
||||
conn, err := sql.Open("sqlite", dbtest.NewURL(t))
|
||||
if err != nil {
|
||||
t.Fatalf("open db: %v", err)
|
||||
}
|
||||
dbURL := dbtest.NewURL(t)
|
||||
lifecycle := fxtest.NewLifecycle(t)
|
||||
|
||||
t.Cleanup(func() {
|
||||
err := conn.Close()
|
||||
if err != nil {
|
||||
t.Logf("close db: %v", err)
|
||||
}
|
||||
dbLog, err := logger.New(lifecycle, logger.Params{
|
||||
Globals: &globals.Globals{},
|
||||
})
|
||||
|
||||
conn.SetMaxOpenConns(1)
|
||||
|
||||
_, err = conn.ExecContext(
|
||||
t.Context(), "PRAGMA foreign_keys = ON",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("pragma: %v", err)
|
||||
t.Fatalf("new logger: %v", err)
|
||||
}
|
||||
|
||||
database := db.NewTestDatabaseFromConn(conn)
|
||||
|
||||
err = database.RunMigrations(t.Context())
|
||||
database, err := db.New(lifecycle, db.Params{
|
||||
Logger: dbLog,
|
||||
Config: &config.Config{DBURL: dbURL},
|
||||
PasswordCost: bcrypt.MinCost,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
t.Fatalf("new database: %v", err)
|
||||
}
|
||||
|
||||
lifecycle.RequireStart()
|
||||
t.Cleanup(lifecycle.RequireStop)
|
||||
|
||||
brk := broker.New()
|
||||
|
||||
log := slog.New(slog.NewTextHandler(
|
||||
@@ -146,7 +148,7 @@ func newTestEnvWithResolver(
|
||||
))
|
||||
|
||||
srv := ircserver.NewTestServer(
|
||||
log, cfg, database, brk, interval, deadline, resolve,
|
||||
log, cfg, database, brk, interval, deadline, poll, resolve,
|
||||
)
|
||||
|
||||
// Port 0 makes the system pick a free port. dial reads it
|
||||
@@ -161,6 +163,7 @@ func newTestEnvWithResolver(
|
||||
t.Cleanup(srv.Stop)
|
||||
|
||||
return &testEnv{
|
||||
dbURL: dbURL,
|
||||
database: database,
|
||||
brk: brk,
|
||||
cfg: cfg,
|
||||
@@ -441,7 +444,8 @@ func TestRegistrationWhileAnotherLookupIsPending(t *testing.T) {
|
||||
|
||||
env := newTestEnvWithResolver(
|
||||
t, &config.Config{ServerName: testServerName},
|
||||
ircserver.PingInterval, ircserver.PongDeadline, resolve,
|
||||
ircserver.PingInterval, ircserver.PongDeadline,
|
||||
testPollInterval, resolve,
|
||||
)
|
||||
|
||||
env.dial(t)
|
||||
@@ -564,27 +568,49 @@ func TestQuietClientAnsweringPingStaysConnected(t *testing.T) {
|
||||
func TestQuietClientNotAnsweringPingIsClosed(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// latest leaves room for a loaded host, but is sooner than
|
||||
// a read deadline of two ping intervals would close it.
|
||||
// A read deadline of two ping intervals would close the
|
||||
// connection a second after latest. The ping interval is
|
||||
// longer than testPingInterval, so that the time between
|
||||
// the expected close and latest leaves room for a loaded
|
||||
// host.
|
||||
const (
|
||||
earliest = testPingInterval + testPongDeadline
|
||||
latest = 2 * testPingInterval
|
||||
interval = 4 * time.Second
|
||||
earliest = interval + testPongDeadline
|
||||
latest = 2*interval - time.Second
|
||||
)
|
||||
|
||||
env := newTestEnvWithShortPings(t)
|
||||
env := newTestEnvWithPingTimes(
|
||||
t,
|
||||
&config.Config{ServerName: testServerName},
|
||||
interval, testPongDeadline,
|
||||
)
|
||||
client := env.dial(t)
|
||||
|
||||
lastSent := time.Now()
|
||||
|
||||
client.register("silent")
|
||||
client.readServerPing(lastSent)
|
||||
|
||||
// The server starts both waits once it has handled the
|
||||
// registration, so the time registration took does not
|
||||
// count against latest.
|
||||
registered := time.Now()
|
||||
|
||||
client.readUntil(isServerPing)
|
||||
client.readUntilClosed()
|
||||
|
||||
waited := time.Since(lastSent)
|
||||
if waited < earliest || waited >= latest {
|
||||
if waited < earliest {
|
||||
t.Errorf(
|
||||
"closed %v after the client's last line, want %v to %v",
|
||||
waited, earliest, latest,
|
||||
"closed %v after the client's last line, want at least %v",
|
||||
waited, earliest,
|
||||
)
|
||||
}
|
||||
|
||||
sinceRegistered := time.Since(registered)
|
||||
if sinceRegistered >= latest {
|
||||
t.Errorf(
|
||||
"closed %v after registration, want less than %v",
|
||||
sinceRegistered, latest,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -929,12 +955,7 @@ func quitSeenByPeers(t *testing.T, quit string) (string, []string) {
|
||||
t.Fatalf("create peer session: %v", err)
|
||||
}
|
||||
|
||||
channelID, err := env.database.GetChannelByName(ctx, "#bye")
|
||||
if err != nil {
|
||||
t.Fatalf("get channel: %v", err)
|
||||
}
|
||||
|
||||
err = env.database.JoinChannel(ctx, channelID, apiPeerID)
|
||||
_, _, err = env.database.CreateOrJoinChannel(ctx, "#bye", apiPeerID, "")
|
||||
if err != nil {
|
||||
t.Fatalf("join peer: %v", err)
|
||||
}
|
||||
@@ -1005,12 +1026,9 @@ func TestStopEndsSessions(t *testing.T) {
|
||||
t.Fatalf("create peer session: %v", err)
|
||||
}
|
||||
|
||||
channelID, err := env.database.GetChannelByName(ctx, "#shut")
|
||||
if err != nil {
|
||||
t.Fatalf("get channel: %v", err)
|
||||
}
|
||||
|
||||
err = env.database.JoinChannel(ctx, channelID, peerID)
|
||||
channelID, _, err := env.database.CreateOrJoinChannel(
|
||||
ctx, "#shut", peerID, "",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("join peer: %v", err)
|
||||
}
|
||||
@@ -1058,21 +1076,30 @@ func TestStopEndsSessions(t *testing.T) {
|
||||
}
|
||||
|
||||
// TestStopReturnsWhileCleanupIsStuck checks that Stop stops
|
||||
// waiting for a connection's cleanup after StopTimeout, so a
|
||||
// cleanup that cannot finish does not hold up stopping.
|
||||
// waiting for a connection's cleanup after its stop timeout,
|
||||
// so a cleanup that cannot finish does not hold up stopping.
|
||||
func TestStopReturnsWhileCleanupIsStuck(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const stopTimeout = 100 * time.Millisecond
|
||||
|
||||
env := newTestEnv(t)
|
||||
env.srv.SetStopTimeout(stopTimeout)
|
||||
|
||||
client := env.dial(t)
|
||||
|
||||
client.register("stuck")
|
||||
|
||||
// The test database has one connection. While the test
|
||||
// holds it, the cleanup's first query waits for it.
|
||||
held, err := env.database.GetDB().Conn(t.Context())
|
||||
// While a connection of the test's own holds a write
|
||||
// transaction, the cleanup's first write waits for it to end.
|
||||
holder, err := sql.Open("sqlite", env.dbURL)
|
||||
if err != nil {
|
||||
t.Fatalf("take the database connection: %v", err)
|
||||
t.Fatalf("open a second database connection: %v", err)
|
||||
}
|
||||
|
||||
_, err = holder.ExecContext(t.Context(), "BEGIN IMMEDIATE")
|
||||
if err != nil {
|
||||
t.Fatalf("begin a write transaction: %v", err)
|
||||
}
|
||||
|
||||
stopped := make(chan struct{})
|
||||
@@ -1084,14 +1111,15 @@ func TestStopReturnsWhileCleanupIsStuck(t *testing.T) {
|
||||
|
||||
select {
|
||||
case <-stopped:
|
||||
case <-time.After(ircserver.StopTimeout + testTimeout):
|
||||
case <-time.After(stopTimeout + testTimeout):
|
||||
t.Error("Stop did not return while a cleanup was stuck")
|
||||
}
|
||||
|
||||
// Lets the cleanup finish before the database is closed.
|
||||
err = held.Close()
|
||||
// Closing ends the transaction, which lets the cleanup finish
|
||||
// before the database is closed.
|
||||
err = holder.Close()
|
||||
if err != nil {
|
||||
t.Errorf("release the database connection: %v", err)
|
||||
t.Errorf("close the second database connection: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1135,9 +1163,6 @@ func TestDirectMessage(t *testing.T) {
|
||||
receiver := env.dial(t)
|
||||
receiver.register("dmreceiver")
|
||||
|
||||
// Give relay goroutines time to start.
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
sender.send("PRIVMSG dmreceiver :hello privately")
|
||||
|
||||
lines := receiver.readUntil(func(line string) bool {
|
||||
@@ -1370,14 +1395,11 @@ func TestPassPostRegistrationNotStored(t *testing.T) {
|
||||
|
||||
client.register("failpass")
|
||||
|
||||
_, err := env.database.GetDB().ExecContext(t.Context(),
|
||||
dbtest.Exec(t, env.dbURL,
|
||||
`CREATE TRIGGER refuse_password
|
||||
BEFORE UPDATE OF password_hash ON sessions
|
||||
BEGIN SELECT RAISE(ABORT, 'refused'); END`,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("create trigger: %v", err)
|
||||
}
|
||||
|
||||
client.send("PASS :mypassword123")
|
||||
|
||||
|
||||
@@ -5,6 +5,9 @@ import (
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
basicauth "github.com/99designs/basicauth-go"
|
||||
@@ -15,6 +18,7 @@ import (
|
||||
"github.com/slok/go-http-metrics/middleware/std"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/fx"
|
||||
"go4.org/netipx"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/globals"
|
||||
"sneak.berlin/go/neoirc/internal/logger"
|
||||
@@ -62,6 +66,77 @@ func ipFromHostPort(hostPort string) string {
|
||||
return host
|
||||
}
|
||||
|
||||
// ClientIP returns middleware that replaces request.RemoteAddr with the
|
||||
// client's address and keeps the port of the TCP connection. The
|
||||
// X-Forwarded-For and X-Real-IP headers count only when the connection
|
||||
// comes from an address in TRUSTED_PROXIES.
|
||||
func (mware *Middleware) ClientIP() func(http.Handler) http.Handler {
|
||||
trusted := mware.params.Config.TrustedProxies
|
||||
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(
|
||||
writer http.ResponseWriter,
|
||||
request *http.Request,
|
||||
) {
|
||||
peer, err := netip.ParseAddrPort(request.RemoteAddr)
|
||||
if err == nil {
|
||||
client := clientAddr(
|
||||
peer.Addr().Unmap(), request.Header, trusted,
|
||||
)
|
||||
request.RemoteAddr = netip.AddrPortFrom(
|
||||
client, peer.Port(),
|
||||
).String()
|
||||
}
|
||||
|
||||
next.ServeHTTP(writer, request)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// clientAddr returns the client's address for a request from peer. A
|
||||
// peer outside trusted is the client, whatever its headers say. A
|
||||
// trusted peer is a proxy. Its X-Forwarded-For list is read from the
|
||||
// right, past the trusted addresses, to the first untrusted one; when
|
||||
// all are trusted, the client is the leftmost. Without X-Forwarded-For,
|
||||
// the client is X-Real-IP. An entry that is not an IP address gives
|
||||
// peer.
|
||||
func clientAddr(
|
||||
peer netip.Addr, header http.Header, trusted *netipx.IPSet,
|
||||
) netip.Addr {
|
||||
if !trusted.Contains(peer) {
|
||||
return peer
|
||||
}
|
||||
|
||||
forwarded := strings.Join(header.Values("X-Forwarded-For"), ",")
|
||||
if forwarded == "" {
|
||||
realIP, err := netip.ParseAddr(
|
||||
strings.TrimSpace(header.Get("X-Real-IP")),
|
||||
)
|
||||
if err != nil {
|
||||
return peer
|
||||
}
|
||||
|
||||
return realIP.Unmap()
|
||||
}
|
||||
|
||||
client := peer
|
||||
|
||||
for _, entry := range slices.Backward(strings.Split(forwarded, ",")) {
|
||||
addr, err := netip.ParseAddr(strings.TrimSpace(entry))
|
||||
if err != nil {
|
||||
return peer
|
||||
}
|
||||
|
||||
client = addr.Unmap()
|
||||
if !trusted.Contains(client) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
type loggingResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
|
||||
@@ -125,24 +200,19 @@ func (mware *Middleware) Logging() func(http.Handler) http.Handler {
|
||||
}
|
||||
}
|
||||
|
||||
// CORS returns middleware that handles Cross-Origin Resource Sharing.
|
||||
// AllowCredentials is true so browsers include cookies in
|
||||
// cross-origin API requests.
|
||||
// CORS returns middleware that lets a page on any origin read the
|
||||
// response. Use it only on public, read-only routes.
|
||||
func (mware *Middleware) CORS() func(http.Handler) http.Handler {
|
||||
return cors.Handler(cors.Options{ //nolint:exhaustruct // optional fields
|
||||
AllowOriginFunc: func(
|
||||
_ *http.Request, _ string,
|
||||
) bool {
|
||||
return true
|
||||
},
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedMethods: []string{
|
||||
"GET", "POST", "PUT", "DELETE", "OPTIONS",
|
||||
},
|
||||
AllowedHeaders: []string{
|
||||
"Accept", "Content-Type", "X-CSRF-Token",
|
||||
"Accept", "Authorization", "Content-Type", "X-CSRF-Token",
|
||||
},
|
||||
ExposedHeaders: []string{"Link"},
|
||||
AllowCredentials: true,
|
||||
AllowCredentials: false,
|
||||
MaxAge: corsMaxAge,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
package middleware_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/netip"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go4.org/netipx"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/globals"
|
||||
"sneak.berlin/go/neoirc/internal/logger"
|
||||
"sneak.berlin/go/neoirc/internal/middleware"
|
||||
)
|
||||
|
||||
// The tests trust 10.0.0.0/8. Clients use the documentation ranges
|
||||
// 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24.
|
||||
const (
|
||||
trustedNetwork = "10.0.0.0/8"
|
||||
proxyPeer = "10.0.0.1:4000"
|
||||
directPeer = "192.0.2.1:4000"
|
||||
client = "203.0.113.7"
|
||||
clientWithPort = "203.0.113.7:4000"
|
||||
realIPClient = "203.0.113.8"
|
||||
)
|
||||
|
||||
type clientIPTest struct {
|
||||
name string
|
||||
remoteAddr string
|
||||
forwarded []string
|
||||
realIP string
|
||||
want string
|
||||
}
|
||||
|
||||
func clientIPTests() []clientIPTest {
|
||||
return []clientIPTest{
|
||||
{
|
||||
name: "untrusted peer ignores forged headers",
|
||||
remoteAddr: directPeer,
|
||||
forwarded: []string{client},
|
||||
realIP: realIPClient,
|
||||
want: directPeer,
|
||||
},
|
||||
{name: "trusted peer without headers", remoteAddr: proxyPeer, want: proxyPeer},
|
||||
{
|
||||
name: "trusted peer gives rightmost untrusted entry",
|
||||
remoteAddr: proxyPeer,
|
||||
forwarded: []string{"198.51.100.9, 203.0.113.7, 10.0.0.2"},
|
||||
want: clientWithPort,
|
||||
},
|
||||
{
|
||||
name: "entries split over several headers",
|
||||
remoteAddr: proxyPeer,
|
||||
forwarded: []string{"198.51.100.9", client, "10.0.0.2"},
|
||||
want: clientWithPort,
|
||||
},
|
||||
{
|
||||
name: "all-trusted chain gives leftmost entry",
|
||||
remoteAddr: proxyPeer,
|
||||
forwarded: []string{"10.0.0.3, 10.0.0.2"},
|
||||
want: "10.0.0.3:4000",
|
||||
},
|
||||
{
|
||||
name: "X-Real-IP without X-Forwarded-For",
|
||||
remoteAddr: proxyPeer,
|
||||
realIP: realIPClient,
|
||||
want: "203.0.113.8:4000",
|
||||
},
|
||||
{
|
||||
name: "X-Forwarded-For wins over X-Real-IP",
|
||||
remoteAddr: proxyPeer,
|
||||
forwarded: []string{client},
|
||||
realIP: realIPClient,
|
||||
want: clientWithPort,
|
||||
},
|
||||
{
|
||||
name: "unparseable X-Forwarded-For entry gives peer",
|
||||
remoteAddr: proxyPeer,
|
||||
forwarded: []string{"203.0.113.7, not-an-ip, 10.0.0.2"},
|
||||
want: proxyPeer,
|
||||
},
|
||||
{
|
||||
name: "unparseable entry past the client is not read",
|
||||
remoteAddr: proxyPeer,
|
||||
forwarded: []string{"not-an-ip, 203.0.113.7"},
|
||||
want: clientWithPort,
|
||||
},
|
||||
{
|
||||
name: "unparseable X-Real-IP gives peer",
|
||||
remoteAddr: proxyPeer,
|
||||
realIP: "not-an-ip",
|
||||
want: proxyPeer,
|
||||
},
|
||||
{
|
||||
name: "mapped IPv6 peer is unmapped and trusted",
|
||||
remoteAddr: "[::ffff:10.0.0.1]:4000",
|
||||
forwarded: []string{client},
|
||||
want: clientWithPort,
|
||||
},
|
||||
{
|
||||
name: "mapped IPv6 peer outside the set",
|
||||
remoteAddr: "[::ffff:192.0.2.1]:4000",
|
||||
forwarded: []string{client},
|
||||
want: directPeer,
|
||||
},
|
||||
{
|
||||
name: "IPv6 client",
|
||||
remoteAddr: proxyPeer,
|
||||
forwarded: []string{"2001:db8::1"},
|
||||
want: "[2001:db8::1]:4000",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientIP(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mware := newMiddleware(t, trustedNetwork)
|
||||
|
||||
for _, test := range clientIPTests() {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var got string
|
||||
|
||||
handler := mware.ClientIP()(http.HandlerFunc(func(
|
||||
_ http.ResponseWriter, request *http.Request,
|
||||
) {
|
||||
got = request.RemoteAddr
|
||||
}))
|
||||
|
||||
request := httptest.NewRequestWithContext(
|
||||
t.Context(), http.MethodGet, "/", nil,
|
||||
)
|
||||
request.RemoteAddr = test.remoteAddr
|
||||
|
||||
for _, value := range test.forwarded {
|
||||
request.Header.Add("X-Forwarded-For", value)
|
||||
}
|
||||
|
||||
if test.realIP != "" {
|
||||
request.Header.Set("X-Real-IP", test.realIP)
|
||||
}
|
||||
|
||||
handler.ServeHTTP(httptest.NewRecorder(), request)
|
||||
|
||||
if got != test.want {
|
||||
t.Errorf("RemoteAddr = %q, want %q", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// newMiddleware returns a Middleware that trusts the network trusted.
|
||||
func newMiddleware(t *testing.T, trusted string) *middleware.Middleware {
|
||||
t.Helper()
|
||||
|
||||
var builder netipx.IPSetBuilder
|
||||
|
||||
builder.AddPrefix(netip.MustParsePrefix(trusted))
|
||||
|
||||
trustedSet, err := builder.IPSet()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
globs := &globals.Globals{
|
||||
Appname: "neoirc-test",
|
||||
Version: "test",
|
||||
StartTime: time.Now(),
|
||||
}
|
||||
|
||||
log, err := logger.New(nil, logger.Params{Globals: globs})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mware, err := middleware.New(nil, middleware.Params{
|
||||
Logger: log,
|
||||
Globals: globs,
|
||||
Config: &config.Config{TrustedProxies: trustedSet},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return mware
|
||||
}
|
||||
@@ -23,13 +23,13 @@ func (srv *Server) SetupRoutes() {
|
||||
|
||||
srv.router.Use(middleware.Recoverer)
|
||||
srv.router.Use(middleware.RequestID)
|
||||
srv.router.Use(srv.mw.ClientIP())
|
||||
srv.router.Use(srv.mw.Logging())
|
||||
|
||||
if viper.GetString("METRICS_USERNAME") != "" {
|
||||
srv.router.Use(srv.mw.Metrics())
|
||||
}
|
||||
|
||||
srv.router.Use(srv.mw.CORS())
|
||||
srv.router.Use(srv.mw.CSP())
|
||||
srv.router.Use(middleware.Timeout(routeTimeout))
|
||||
|
||||
@@ -44,7 +44,7 @@ func (srv *Server) SetupRoutes() {
|
||||
}
|
||||
|
||||
// Health check.
|
||||
srv.router.Get(
|
||||
srv.router.With(srv.mw.CORS()).Get(
|
||||
"/.well-known/healthcheck.json",
|
||||
srv.handlers.HandleHealthCheck(),
|
||||
)
|
||||
@@ -68,7 +68,7 @@ func (srv *Server) SetupRoutes() {
|
||||
}
|
||||
|
||||
func (srv *Server) setupAPIv1(router chi.Router) {
|
||||
router.Get(
|
||||
router.With(srv.mw.CORS()).Get(
|
||||
"/server",
|
||||
srv.handlers.HandleServerInfo(),
|
||||
)
|
||||
|
||||
@@ -6,8 +6,10 @@ package server_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -188,6 +190,88 @@ func TestServeAfterStopDoesNotStopApp(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestCORSHeaders checks the Access-Control-Allow-* headers on the
|
||||
// answers to requests from another origin. The public, read-only
|
||||
// routes answer with Access-Control-Allow-Origin: * alone. Routes
|
||||
// that need the cookie, and a preflight to one, answer with none.
|
||||
func TestCORSHeaders(t *testing.T) {
|
||||
var srv *server.Server
|
||||
|
||||
startApp(t, fx.Populate(&srv))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
method string
|
||||
path string
|
||||
requestMethod string
|
||||
wantHeaders map[string]string
|
||||
}{
|
||||
{
|
||||
name: "server information",
|
||||
method: http.MethodGet,
|
||||
path: "/api/v1/server",
|
||||
wantHeaders: map[string]string{"Access-Control-Allow-Origin": "*"},
|
||||
},
|
||||
{
|
||||
name: "health check",
|
||||
method: http.MethodGet,
|
||||
path: "/.well-known/healthcheck.json",
|
||||
wantHeaders: map[string]string{"Access-Control-Allow-Origin": "*"},
|
||||
},
|
||||
{
|
||||
name: "send",
|
||||
method: http.MethodPost,
|
||||
path: "/api/v1/messages",
|
||||
},
|
||||
{
|
||||
name: "state",
|
||||
method: http.MethodGet,
|
||||
path: "/api/v1/state",
|
||||
},
|
||||
{
|
||||
name: "preflight for send",
|
||||
method: http.MethodOptions,
|
||||
path: "/api/v1/messages",
|
||||
requestMethod: http.MethodPost,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
request := httptest.NewRequestWithContext(
|
||||
t.Context(), test.method,
|
||||
"http://chat.example.com"+test.path, nil,
|
||||
)
|
||||
request.Header.Set("Origin", "http://blog.example.com")
|
||||
|
||||
if test.requestMethod != "" {
|
||||
request.Header.Set(
|
||||
"Access-Control-Request-Method", test.requestMethod,
|
||||
)
|
||||
}
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
srv.ServeHTTP(recorder, request)
|
||||
|
||||
gotHeaders := map[string]string{}
|
||||
|
||||
for name := range recorder.Header() {
|
||||
if strings.HasPrefix(name, "Access-Control-Allow-") {
|
||||
gotHeaders[name] = recorder.Header().Get(name)
|
||||
}
|
||||
}
|
||||
|
||||
if !maps.Equal(gotHeaders, test.wantHeaders) {
|
||||
t.Errorf(
|
||||
"Access-Control-Allow-* headers are %v, want %v",
|
||||
gotHeaders, test.wantHeaders,
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// startApp starts the app newApp builds, with the HTTP port
|
||||
// set to 0, so the system gives the server a free port.
|
||||
func startApp(
|
||||
|
||||
+134
-105
@@ -1,7 +1,3 @@
|
||||
// Tests use a global viper instance for configuration,
|
||||
// making parallel execution unsafe.
|
||||
//
|
||||
//nolint:paralleltest
|
||||
package service_test
|
||||
|
||||
import (
|
||||
@@ -9,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -18,7 +13,6 @@ import (
|
||||
|
||||
"go.uber.org/fx"
|
||||
"go.uber.org/fx/fxtest"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"sneak.berlin/go/neoirc/internal/broker"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/db"
|
||||
@@ -30,14 +24,10 @@ import (
|
||||
"sneak.berlin/go/neoirc/pkg/irc"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
db.SetBcryptCost(bcrypt.MinCost)
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
// testEnv holds all dependencies for a service test.
|
||||
type testEnv struct {
|
||||
svc *service.Service
|
||||
dbURL string
|
||||
db *db.Database
|
||||
broker *broker.Broker
|
||||
app *fxtest.App
|
||||
@@ -65,28 +55,12 @@ func newTestEnv(t *testing.T) *testEnv {
|
||||
}
|
||||
},
|
||||
logger.New,
|
||||
func(
|
||||
lifecycle fx.Lifecycle,
|
||||
globs *globals.Globals,
|
||||
log *logger.Logger,
|
||||
) (*config.Config, error) {
|
||||
cfg, err := config.New(
|
||||
lifecycle, config.Params{ //nolint:exhaustruct
|
||||
Globals: globs, Logger: log,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"test config: %w", err,
|
||||
)
|
||||
func() *config.Config {
|
||||
return &config.Config{
|
||||
DBURL: dbURL,
|
||||
OperName: "admin",
|
||||
OperPassword: "secret",
|
||||
}
|
||||
|
||||
cfg.DBURL = dbURL
|
||||
cfg.Port = 0
|
||||
cfg.OperName = "admin"
|
||||
cfg.OperPassword = "secret"
|
||||
|
||||
return cfg, nil
|
||||
},
|
||||
func(
|
||||
lifecycle fx.Lifecycle,
|
||||
@@ -111,6 +85,7 @@ func newTestEnv(t *testing.T) *testEnv {
|
||||
|
||||
return &testEnv{
|
||||
svc: svc,
|
||||
dbURL: dbURL,
|
||||
db: database,
|
||||
broker: brk,
|
||||
app: app,
|
||||
@@ -141,6 +116,8 @@ func createSession(
|
||||
// environment starts on an empty database while another one
|
||||
// is still open.
|
||||
func TestEachTestEnvHasItsOwnDatabase(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := t.Context()
|
||||
|
||||
first := newTestEnv(t)
|
||||
@@ -151,6 +128,8 @@ func TestEachTestEnvHasItsOwnDatabase(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFanOut(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -178,6 +157,8 @@ func TestFanOut(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestJoinChannel(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -218,6 +199,8 @@ func TestJoinChannel(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPartChannel(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -318,6 +301,8 @@ func joinConcurrently(
|
||||
// concurrent JOINs of a +l 2 channel with one member, one
|
||||
// gets in and the other gets ERR_CHANNELISFULL.
|
||||
func TestConcurrentJoinsRespectUserLimit(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const userLimit = 2
|
||||
|
||||
env := newTestEnv(t)
|
||||
@@ -380,6 +365,8 @@ func TestConcurrentJoinsRespectUserLimit(t *testing.T) {
|
||||
// two concurrent JOINs that create a channel, exactly one
|
||||
// makes its session the operator.
|
||||
func TestConcurrentFirstJoinsMakeOneOperator(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -431,6 +418,8 @@ func TestConcurrentFirstJoinsMakeOneOperator(t *testing.T) {
|
||||
// either joins a channel that still exists or gets
|
||||
// ERR_NOSUCHCHANNEL, and never fails otherwise.
|
||||
func TestJoinConcurrentWithLastPart(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -496,6 +485,8 @@ func TestJoinConcurrentWithLastPart(t *testing.T) {
|
||||
// quits, gets in only once the channel is deleted, into a
|
||||
// new channel of that name with no key.
|
||||
func TestJoinConcurrentWithLastQuit(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -574,81 +565,95 @@ const quitCrowdSize = 10
|
||||
// ahead of that JOIN. The quitting session is also on a
|
||||
// channel with quitCrowdSize other sessions, made before the
|
||||
// watcher's channel, so that its QUIT reaches the watcher
|
||||
// last.
|
||||
// last. Each trial runs on a database of its own.
|
||||
func TestQuitQueuedAheadOfNewNickHoldersJoin(t *testing.T) {
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
t.Parallel()
|
||||
|
||||
for trial := range concurrentJoinTrials {
|
||||
nick := fmt.Sprintf("reused%d", trial)
|
||||
crowd := fmt.Sprintf("#crowd%d", trial)
|
||||
shared := fmt.Sprintf("#shared%d", trial)
|
||||
quitterSID := createSession(ctx, t, env.db, nick)
|
||||
t.Run(fmt.Sprintf("trial %d", trial), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, channel := range []string{crowd, shared} {
|
||||
_, err := env.svc.JoinChannel(ctx, quitterSID, 0, nick, channel, "")
|
||||
if err != nil {
|
||||
t.Fatalf("join %s: %v", channel, err)
|
||||
}
|
||||
checkQuitQueuedAheadOfJoin(t, newTestEnv(t))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// checkQuitQueuedAheadOfJoin runs one trial of
|
||||
// TestQuitQueuedAheadOfNewNickHoldersJoin on env.
|
||||
func checkQuitQueuedAheadOfJoin(t *testing.T, env *testEnv) {
|
||||
t.Helper()
|
||||
|
||||
const (
|
||||
nick = "reused"
|
||||
crowd = "#crowd"
|
||||
shared = "#shared"
|
||||
watcherNick = "watcher"
|
||||
)
|
||||
|
||||
ctx := t.Context()
|
||||
quitterSID := createSession(ctx, t, env.db, nick)
|
||||
|
||||
for _, channel := range []string{crowd, shared} {
|
||||
_, err := env.svc.JoinChannel(ctx, quitterSID, 0, nick, channel, "")
|
||||
if err != nil {
|
||||
t.Fatalf("join %s: %v", channel, err)
|
||||
}
|
||||
}
|
||||
|
||||
for member := range quitCrowdSize {
|
||||
memberNick := fmt.Sprintf("crowd%dx%d", trial, member)
|
||||
memberSID := createSession(ctx, t, env.db, memberNick)
|
||||
for member := range quitCrowdSize {
|
||||
memberNick := fmt.Sprintf("crowd%d", member)
|
||||
memberSID := createSession(ctx, t, env.db, memberNick)
|
||||
|
||||
_, err := env.svc.JoinChannel(ctx, memberSID, 0, memberNick, crowd, "")
|
||||
if err != nil {
|
||||
t.Fatalf("join %s: %v", memberNick, err)
|
||||
}
|
||||
_, err := env.svc.JoinChannel(ctx, memberSID, 0, memberNick, crowd, "")
|
||||
if err != nil {
|
||||
t.Fatalf("join %s: %v", memberNick, err)
|
||||
}
|
||||
}
|
||||
|
||||
watcherNick := fmt.Sprintf("watcher%d", trial)
|
||||
watcherSID, watcherClientID, _, err := env.db.CreateSession(
|
||||
ctx, watcherNick, watcherNick, "localhost", "127.0.0.1",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("create session %s: %v", watcherNick, err)
|
||||
}
|
||||
|
||||
watcherSID, watcherClientID, _, err := env.db.CreateSession(
|
||||
ctx, watcherNick, watcherNick, "localhost", "127.0.0.1",
|
||||
_, err = env.svc.JoinChannel(ctx, watcherSID, 0, watcherNick, shared, "")
|
||||
if err != nil {
|
||||
t.Fatalf("join %s: %v", watcherNick, err)
|
||||
}
|
||||
|
||||
_, lastQueueID, err := env.db.PollMessages(ctx, watcherClientID, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("poll: %v", err)
|
||||
}
|
||||
|
||||
errs := runConcurrently(
|
||||
func() error {
|
||||
env.svc.BroadcastQuit(ctx, quitterSID, nick, "")
|
||||
|
||||
return nil
|
||||
},
|
||||
func() error {
|
||||
return takeNickAndJoin(ctx, env, nick, shared)
|
||||
},
|
||||
)
|
||||
if errs[1] != nil {
|
||||
t.Fatalf("new session: %v", errs[1])
|
||||
}
|
||||
|
||||
msgs, _, err := env.db.PollMessages(
|
||||
ctx, watcherClientID, lastQueueID, 0,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("poll: %v", err)
|
||||
}
|
||||
|
||||
commands := commandsFrom(msgs, nick)
|
||||
if !slices.Equal(commands, []string{irc.CmdQuit, irc.CmdJoin}) {
|
||||
t.Errorf(
|
||||
"watcher got %v from %s, want QUIT then JOIN",
|
||||
commands, nick,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("create session %s: %v", watcherNick, err)
|
||||
}
|
||||
|
||||
_, err = env.svc.JoinChannel(ctx, watcherSID, 0, watcherNick, shared, "")
|
||||
if err != nil {
|
||||
t.Fatalf("join %s: %v", watcherNick, err)
|
||||
}
|
||||
|
||||
_, lastQueueID, err := env.db.PollMessages(ctx, watcherClientID, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("poll: %v", err)
|
||||
}
|
||||
|
||||
errs := runConcurrently(
|
||||
func() error {
|
||||
env.svc.BroadcastQuit(ctx, quitterSID, nick, "")
|
||||
|
||||
return nil
|
||||
},
|
||||
func() error {
|
||||
return takeNickAndJoin(ctx, env, nick, shared)
|
||||
},
|
||||
)
|
||||
if errs[1] != nil {
|
||||
t.Fatalf("trial %d: new session: %v", trial, errs[1])
|
||||
}
|
||||
|
||||
msgs, _, err := env.db.PollMessages(
|
||||
ctx, watcherClientID, lastQueueID, 0,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("poll: %v", err)
|
||||
}
|
||||
|
||||
commands := commandsFrom(msgs, nick)
|
||||
if !slices.Equal(commands, []string{irc.CmdQuit, irc.CmdJoin}) {
|
||||
t.Errorf(
|
||||
"trial %d: watcher got %v from %s, want QUIT then JOIN",
|
||||
trial, commands, nick,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -696,6 +701,8 @@ func commandsFrom(msgs []db.IRCMessage, nick string) []string {
|
||||
// into a +i channel uses the invite up, so that a JOIN
|
||||
// after leaving gets ERR_INVITEONLYCHAN.
|
||||
func TestJoinUsesUpInvite(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const channel = "#club"
|
||||
|
||||
env := newTestEnv(t)
|
||||
@@ -741,6 +748,8 @@ func TestJoinUsesUpInvite(t *testing.T) {
|
||||
// session's nick, username and hostname refuses its JOIN
|
||||
// with ERR_BANNEDFROMCHAN.
|
||||
func TestJoinRefusedByHostmaskBan(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const channel = "#club"
|
||||
|
||||
env := newTestEnv(t)
|
||||
@@ -779,6 +788,8 @@ func TestJoinRefusedByHostmaskBan(t *testing.T) {
|
||||
// keeps a channel other members are still on and deletes
|
||||
// the channel when its last member parts.
|
||||
func TestPartDeletesChannelOnlyWhenEmpty(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const channel = "#room"
|
||||
|
||||
env := newTestEnv(t)
|
||||
@@ -826,6 +837,8 @@ func TestPartDeletesChannelOnlyWhenEmpty(t *testing.T) {
|
||||
// TestKickOfLastMemberDeletesChannel checks that a KICK
|
||||
// that leaves a channel with no members deletes it.
|
||||
func TestKickOfLastMemberDeletesChannel(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const channel = "#room"
|
||||
|
||||
env := newTestEnv(t)
|
||||
@@ -850,6 +863,8 @@ func TestKickOfLastMemberDeletesChannel(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSendChannelMessage(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -897,6 +912,8 @@ func TestSendChannelMessage(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBroadcastQuit(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -934,25 +951,24 @@ func TestBroadcastQuit(t *testing.T) {
|
||||
// error as a DM to an unknown nick. The trigger deletes the
|
||||
// target's session just before the DM is stored.
|
||||
func TestSendDirectMessageTargetQuits(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
sid := createSession(ctx, t, env.db, "alice")
|
||||
createSession(ctx, t, env.db, "bob")
|
||||
|
||||
_, err := env.db.GetDB().ExecContext(ctx,
|
||||
dbtest.Exec(t, env.dbURL,
|
||||
`CREATE TRIGGER delete_recipient
|
||||
BEFORE INSERT ON messages
|
||||
BEGIN
|
||||
DELETE FROM sessions WHERE id = NEW.to_session_id;
|
||||
END`)
|
||||
if err != nil {
|
||||
t.Fatalf("create trigger: %v", err)
|
||||
}
|
||||
|
||||
body, _ := json.Marshal([]string{"hi"}) //nolint:errchkjson
|
||||
|
||||
_, err = env.svc.SendDirectMessage(
|
||||
_, err := env.svc.SendDirectMessage(
|
||||
ctx, sid, 0, "alice", irc.CmdPrivmsg, "bob", body, nil,
|
||||
)
|
||||
|
||||
@@ -968,6 +984,8 @@ func TestSendDirectMessageTargetQuits(t *testing.T) {
|
||||
// trigger deletes the channel just before the message is
|
||||
// stored.
|
||||
func TestSendChannelMessageChannelDeleted(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -978,15 +996,12 @@ func TestSendChannelMessageChannelDeleted(t *testing.T) {
|
||||
t.Fatalf("join alice: %v", err)
|
||||
}
|
||||
|
||||
_, err = env.db.GetDB().ExecContext(ctx,
|
||||
dbtest.Exec(t, env.dbURL,
|
||||
`CREATE TRIGGER delete_recipient
|
||||
BEFORE INSERT ON messages
|
||||
BEGIN
|
||||
DELETE FROM channels WHERE id = NEW.to_channel_id;
|
||||
END`)
|
||||
if err != nil {
|
||||
t.Fatalf("create trigger: %v", err)
|
||||
}
|
||||
|
||||
body, _ := json.Marshal([]string{"hi"}) //nolint:errchkjson
|
||||
|
||||
@@ -1002,6 +1017,8 @@ func TestSendChannelMessageChannelDeleted(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSendChannelMessage_Moderated(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -1053,6 +1070,8 @@ func TestSendChannelMessage_Moderated(t *testing.T) {
|
||||
// not spend its hashcash stamp: the operator can then send
|
||||
// the same message with it.
|
||||
func TestRefusedChannelMessageLeavesStampUnspent(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const (
|
||||
channel = "#paid"
|
||||
bits = 2
|
||||
@@ -1078,6 +1097,8 @@ func TestRefusedChannelMessageLeavesStampUnspent(t *testing.T) {
|
||||
|
||||
for _, refusal := range refusals {
|
||||
t.Run(refusal.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -1130,6 +1151,8 @@ func TestRefusedChannelMessageLeavesStampUnspent(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestQueryUserMode(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -1180,6 +1203,8 @@ func TestQueryUserMode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUserhostReply(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -1285,10 +1310,14 @@ func applyUserModeTests() []applyUserModeTest {
|
||||
}
|
||||
|
||||
func TestApplyUserMode(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
|
||||
for i, test := range applyUserModeTests() {
|
||||
t.Run(test.modeStr, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := t.Context()
|
||||
sid := createSession(
|
||||
ctx, t, env.db, fmt.Sprintf("user%d", i),
|
||||
|
||||
Reference in new issue
Block a user