Compare commits

...
Author SHA1 Message Date
HampusandGitHub 4a93b677af feat(api): retire prices safely and add a self-serve switch (#2637) 2026-09-10 17:28:16 +02:00
HampusandGitHub d79cd99050 refactor(api): tidy message helper internals (#2636) 2026-09-10 02:07:03 +02:00
HampusandGitHub 167862a8a6 perf(api): harvest messages a page at a time (#2633) 2026-09-09 20:59:38 +02:00
HampusandGitHub 48b569b9d4 fix(api): harvest every authored message, not the first 100000 (#2631) 2026-09-09 11:52:55 +02:00
HampusandGitHub 75be6aa492 fix(gateway): deliver mention updates to passive sessions (#2632) 2026-09-09 11:31:17 +02:00
HampusandGitHub 9fe65d5036 fix(api): honour the configured S3 addressing on uploads (#2626) 2026-09-09 11:26:30 +02:00
HampusandGitHub bfa9bf221d docs(api): tidy up the reference prose (#2628) 2026-09-09 02:11:38 +02:00
HampusandGitHub 184eeb0846 fix(gateway): keep dispatch ordered under broadcaster load (#2627) 2026-09-09 02:06:36 +02:00
HampusandGitHub 0eff26a1c9 test(config): match the configurable client-IP trust defaults (#2625) 2026-09-09 01:32:36 +02:00
HampusandGitHub d729f641ff fix(app): do not crash when the browser translates the page (#2624) 2026-09-09 01:24:14 +02:00
HampusandGitHub 044a2c101d feat(self-hosting): make the bundled services configurable (#2621) 2026-09-09 01:17:58 +02:00
HampusandGitHub 38e2c8db3e fix(admin): keep server traits when an operator saves traits (#2622) 2026-09-09 00:13:07 +02:00
HampusandGitHub 1b22d14f3d feat(self-hosting): run postgres or the object store outside (#2620) 2026-09-08 23:36:52 +02:00
HampusandGitHub 98cceae59d fix(i18n): point static catalog translation at weblate (#2619) 2026-09-08 23:10:10 +02:00
HampusandGitHub 3e32414849 test(config): expand the shipped stack on another port (#2612) 2026-09-08 23:10:00 +02:00
HampusandGitHub 7707b9531c chore(i18n): translate the new setup and email domain strings (#2613) 2026-09-08 22:57:07 +02:00
HampusandHampus Kraft 86745e01e9 docs(operator): cover serving on a non-default port (#2611) 2026-09-08 22:18:19 +02:00
HampusandHampus Kraft 098830a95a fix(admin): compare the request origin against an origin (#2610) 2026-09-08 22:18:10 +02:00
HampusandHampus Kraft cf83f66911 fix(app): keep the new admin when setup meets one 401 (#2609) 2026-09-08 22:18:02 +02:00
HampusandHampus Kraft c577b97f35 fix(api): reject a mail-less email domain by its own code (#2608) 2026-09-08 22:17:53 +02:00
HampusandGitHub 8cc485cf81 fix(self-host): tie the public address to a single origin (#2605) 2026-09-08 22:17:39 +02:00
HampusandGitHub 6c36d934f7 fix(api): widen guild IP ban guard to shared-access networks (#2607) 2026-09-08 22:09:39 +02:00
HampusandGitHub 63e3be5750 fix(media-proxy): tone map HDR video instead of refusing it (#2606) 2026-09-08 21:18:55 +02:00
HampusandGitHub cdecda7f78 ci: exclude the gateway build output from the rebar3 cache (#2604) 2026-09-08 19:47:44 +02:00
HampusandGitHub 4ad2858773 test(api): isolate the instance policy test files (#2603) 2026-09-08 19:46:07 +02:00
HampusandGitHub fda41bb57a style(gateway): apply erlfmt to the voice disconnect modules (#2602) 2026-09-08 19:29:38 +02:00
HampusandGitHub ce08f82a92 refactor(gateway): remove voice reconciliation v3 (#2601) 2026-09-08 19:17:48 +02:00
HampusandGitHub ef067f36c6 fix(voice): report real state in voice diagnostics (#2600) 2026-09-08 19:16:22 +02:00
HampusandGitHub fc2b6b5299 fix(app): correct shortcuts, nagbar, stream menu, share audio (#2599) 2026-09-08 19:09:59 +02:00
HampusandGitHub 55846b24ea fix(api): respect age gating in search and stabilise discovery (#2598) 2026-09-08 19:07:59 +02:00
HampusandGitHub 20a15ac11d fix(voice): scope disconnects, correct VAD and stream lifecycle (#2597) 2026-09-08 19:06:42 +02:00
HampusandGitHub 667ac7da8e fix(voice): rank h264 baseline first and gate opus stereo (#2596) 2026-09-08 19:04:24 +02:00
HampusandGitHub 1b81c14c48 fix(api): stop treating a LiveKit 404 as an empty room (#2595) 2026-09-08 19:02:43 +02:00
HampusandGitHub ceec183d38 docs: remove duplicated statements from the reference (#2594) 2026-09-08 17:55:55 +02:00
HampusandGitHub 69ddc07ebb docs(operator): tighten the get started guide (#2593) 2026-09-08 17:38:29 +02:00
HampusandGitHub 2f008b8653 docs: rewrite reference prose and correct field code citations (#2592) 2026-09-08 17:13:37 +02:00
HampusandGitHub 3d38d3f694 fix(self-host): add FLUXER_NATS_AUTH_TOKEN to .env.example (#2590) 2026-09-08 16:32:29 +02:00
HampusandGitHub ad86a04e67 feat(app): describe every role and channel permission toggle (#2589) 2026-09-08 16:20:37 +02:00
HampusandGitHub 600c15e17d chore(github): drop mobile build hint from the bug report form (#2588) 2026-09-08 15:37:26 +02:00
HampusandGitHub 08c9fe9886 docs: correct misreadable and factually wrong reference prose (#2587) 2026-09-08 15:36:50 +02:00
HampusandGitHub 43924e3ac5 chore(github): link mobile bug reports, drop security duplicate (#2586) 2026-09-08 15:34:34 +02:00
HampusandGitHub 824b5c86c9 fix(api): dedupe and budget ipinfo lookups across api pods (#2584) 2026-09-08 15:13:32 +02:00
HampusandGitHub a2a68847fd fix(api): let channel managers edit a mature channel (#2583) 2026-09-08 14:51:47 +02:00
HampusandGitHub 2019909a5e fix(api): skip the mature gate when no birth date is collected (#2582) 2026-09-08 14:51:41 +02:00
HampusandGitHub d46c8d49c6 fix(svc): authenticate to nats with the configured token (#2581) 2026-09-08 14:51:34 +02:00
HampusandGitHub 45530ebbf5 docs(operator): drop the redundant caddy forwarded-for setter (#2580) 2026-09-08 14:51:29 +02:00
HampusandGitHub 9cdad046b1 fix(self-host): keep seaweedfs inside its memory ceiling (#2579) 2026-09-08 14:51:24 +02:00
HampusandGitHub b6c6928073 fix(self-host): strip the caddy file capability in fluxer-static (#2578) 2026-09-08 14:51:19 +02:00
HampusandGitHub dd1ee999a4 fix(docs): drop visible pipe escapes from union notation prose (#2577) 2026-09-08 14:27:41 +02:00
HampusandGitHub c506d6d5e3 fix(webhook): stop gating webhook file uploads on creator perms (#2576) 2026-09-08 14:25:59 +02:00
511 changed files with 37248 additions and 12010 deletions
+1 -2
View File
@@ -36,8 +36,7 @@ body:
label: Build information
description: >-
Open User Settings, scroll to the bottom of the left sidebar, and select
the build information. Fluxer copies it to the clipboard. On mobile,
select the build information at the bottom of the settings list.
the build information. Fluxer copies it to the clipboard.
validations:
required: true
+3 -3
View File
@@ -1,15 +1,15 @@
# yaml-language-server: $schema=https://www.schemastore.org/github-issue-config.json
blank_issues_enabled: false
contact_links:
- name: Mobile client bugs
url: https://github.com/fluxerapp/flutter_client#bug-reporting
about: Read the reporting instructions for the Fluxer mobile client.
- name: Account and billing support
url: https://fluxer.app/help
about: Find account help and support contact details.
- name: Feature proposals
url: https://github.com/orgs/fluxerapp/discussions
about: Propose a feature in a discussion.
- name: Security vulnerabilities
url: https://github.com/fluxerapp/fluxer/security/advisories/new
about: Submit a private vulnerability report.
- name: Translations
url: https://weblate.fluxer.tools
about: Improve an existing locale or start a new one.
+29 -4
View File
@@ -314,8 +314,8 @@ jobs:
path: |
~/.cache/rebar3
fluxer_gateway/_build
!fluxer_gateway/_build/default/lib/fluxer_gateway
!fluxer_gateway/_build/test/lib/fluxer_gateway
!fluxer_gateway/_build/default/lib/fluxer_gateway/**
!fluxer_gateway/_build/test/lib/fluxer_gateway/**
key: >-
rebar3-${{ runner.os }}-otp28-rebar3.24.0-${{ hashFiles('fluxer_gateway/rebar.lock',
'fluxer_gateway/rebar.config') }}
@@ -345,8 +345,8 @@ jobs:
path: |
~/.cache/rebar3
fluxer_gateway/_build
!fluxer_gateway/_build/default/lib/fluxer_gateway
!fluxer_gateway/_build/test/lib/fluxer_gateway
!fluxer_gateway/_build/default/lib/fluxer_gateway/**
!fluxer_gateway/_build/test/lib/fluxer_gateway/**
key: >-
rebar3-${{ runner.os }}-otp28-rebar3.24.0-${{ hashFiles('fluxer_gateway/rebar.lock',
'fluxer_gateway/rebar.config') }}
@@ -426,6 +426,31 @@ jobs:
'packages/markdown_parser/rust/Cargo.toml', 'packages/markdown_parser/rust/.cargo/config.toml',
'packages/markdown_parser/rust/src/**') }}
lint:
runs-on: ubuntu-24.04
timeout-minutes: 15
steps:
- name: Checkout code
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0
- name: Install pnpm
uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271
- name: Install Node.js
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e
with:
node-version: '24'
cache: 'pnpm'
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Check formatting and lint
run: pnpm exec biome ci .
- name: Lint JSX for browser-translation safety
run: pnpm exec eslint . --max-warnings 0
i18n:
runs-on: ubuntu-24.04
timeout-minutes: 25
+23 -1
View File
@@ -84,7 +84,18 @@
},
"useConst": "error",
"noNonNullAssertion": "off",
"noParameterAssign": "off"
"noParameterAssign": "off",
"noRestrictedImports": {
"level": "error",
"options": {
"paths": {
"@lingui/react": {
"importNames": ["I18nProvider"],
"message": "Use AppI18nProvider from @app/features/i18n/components/AppI18nProvider so <Trans> output stays safe under page translation."
}
}
}
}
},
"a11y": {
"recommended": true,
@@ -116,10 +127,21 @@
},
"assist": {"actions": {"source": {"organizeImports": "on"}}},
"overrides": [
{
"includes": ["fluxer_app/src/**/*.tsx"],
"plugins": ["./tools/lint/no-adjacent-jsx-text.grit"]
},
{
"includes": ["fluxer_docs/scripts/VerifyDocsCoverage.ts"],
"linter": {"rules": {"suspicious": {"noTemplateCurlyInString": "off"}}}
},
{
"includes": [
"fluxer_app/src/features/i18n/components/AppI18nProvider.tsx",
"fluxer_app/src/features/i18n/components/AppI18nProvider.test.tsx"
],
"linter": {"rules": {"style": {"noRestrictedImports": "off"}}}
},
{
"includes": ["**/*.astro"],
"linter": {"rules": {"correctness": {"noUnusedImports": "off", "noUnusedVariables": "off"}}},
+134 -35
View File
@@ -3,12 +3,20 @@
# A name absent from this file is one Compose does not forward, and it reaches a
# service only through a Compose override file that adds it to that service's
# environment. packages/config/src/__tests__/DeployEnvCoverage.test.ts fails when
# a Compose edit forgets the matching line here.
# a Compose edit forgets the matching line here. Compose expands this file from
# top to bottom, so a line written with ${...} has to sit below every name it
# reads.
FLUXER_DOMAIN=chat.example.com
FLUXER_PUBLIC_SCHEME=https
FLUXER_PUBLIC_PORT=443
# The three lines above are the address browsers use, and every endpoint the
# services advertise carries the port from FLUXER_PUBLIC_PORT. They do not move
# what the host publishes. FLUXER_HTTP_PORT and FLUXER_HTTPS_PORT further down
# do that, and a non-default port needs the matching one set as well. Both
# complete recipes are written out beside them.
# How browsers reach this instance.
#
# Default: Fluxer binds 80 and 443 and gets its own Let's Encrypt certificate.
@@ -33,50 +41,71 @@ FLUXER_PUBLIC_PORT=443
# address if it reaches Fluxer from a public IP.
#FLUXER_EDGE_TRUSTED_PROXIES=private_ranges
# The public origin browsers use, without a trailing slash. Derived from the three
# values above and correct for the usual https-on-443 setup, so leave it alone
# unless you serve Fluxer on a non-default port, where the port must appear here.
# The origin browsers see, without a trailing slash. Leave it unset and each
# service builds one from the three values at the top of this file. Set it and it
# wins: every service reads the host, the scheme and the port out of it and
# ignores those three names. Use it when browsers reach the instance on a host
# FLUXER_DOMAIN does not name. It has to be a bare origin, a scheme and a host
# and an optional port and nothing after them, or the services refuse to start.
# It does not move the edge listener or the published ports either, so set the
# publish below to the port written here.
#FLUXER_PUBLIC_ORIGIN=https://chat.example.com
# Overrides the address Fluxer's edge listens on. Honoured in the default mode
# only: docker-compose.proxy.yml sets the literal :8080 and Compose lets the last
# file win, so a value here is discarded under the proxy overlay with no warning.
# Set it only for an unusual default-mode layout, such as serving several
# hostnames or binding a non-default TLS port.
#FLUXER_EDGE_SITE_ADDRESS=chat.example.com
# Overrides the address the edge listens on inside its container. Compose builds
# it from FLUXER_PUBLIC_SCHEME and FLUXER_DOMAIN with no port, and the edge keeps
# its container ports at 80 and 443 whatever the public port is. Caddy matches a
# site by host and ignores the port in the Host header, so a request arriving on
# a non-default published port still lands on this site. Put a port in this value
# only if you also publish that same container port below, or nothing will be
# listening where the publish points. Honoured in the default mode only:
# docker-compose.proxy.yml sets the literal :8080 and tunnel.compose.yml the
# literal :80, and Compose lets the last file win, so a value here is discarded
# under either overlay with no warning. Set it for an unusual default-mode
# layout, such as serving several hostnames. Write the scheme into it: a bare
# hostname means automatic HTTPS on 443 whatever FLUXER_PUBLIC_SCHEME says.
#FLUXER_EDGE_SITE_ADDRESS=https://chat.example.com
# The old name for the value above. It is read only when
# FLUXER_EDGE_SITE_ADDRESS is unset, so an existing .env keeps the listener
# it already had. Rename it to FLUXER_EDGE_SITE_ADDRESS at your convenience.
#FLUXER_CADDY_SITE_ADDRESS=
# FLUXER_PUBLIC_ORIGIN is the origin browsers see. It must carry the port
# whenever FLUXER_PUBLIC_PORT is not the default for its scheme, because an
# origin written with a default port never matches a browser Origin header.
# Serving on any other port means setting all three, plus the published port
# below, and pointing FLUXER_EDGE_SITE_ADDRESS at the same scheme and host.
# Compose expands this file from top to bottom, so FLUXER_PUBLIC_ORIGIN has to
# stay below the two values it reads. Above them it silently expands to a bare
# host with a trailing colon.
#FLUXER_PUBLIC_SCHEME=http
#FLUXER_PUBLIC_PORT=19080
#FLUXER_PUBLIC_ORIGIN=${FLUXER_PUBLIC_SCHEME}://${FLUXER_DOMAIN}:${FLUXER_PUBLIC_PORT}
#FLUXER_HTTP_PORT=19080
# Ports Caddy publishes on the host. Caddy still listens on 80 and 443 inside
# the container, so change only these when something else already owns the
# standard ports or another proxy sits in front. Both take an optional bind
# address in front of the port, and 127.0.0.1 keeps the publish off every
# public interface. FLUXER_HTTPS_PORT moves the TCP and the UDP publish
# together, because HTTP/3 needs both on the same port.
# Host side of the edge's publishes, and the only two names that decide which
# host ports Fluxer binds. The container side is fixed. Container 80 carries the
# HTTP to HTTPS redirect and the Let's Encrypt HTTP challenge under an https
# scheme, and the site itself under an http one. Container 443 carries the TLS
# site. FLUXER_HTTPS_PORT moves the TCP and the UDP publish together, because
# HTTP/3 needs both on the same port. Both take an optional bind address in front
# of the port, and 127.0.0.1 keeps the publish off every public interface. Give
# them different host ports: the same host port on both is two publishes of one
# port and the edge refuses to start.
#FLUXER_HTTP_PORT=80
#FLUXER_HTTPS_PORT=443
#FLUXER_HTTP_PORT=127.0.0.1:80
#FLUXER_HTTPS_PORT=127.0.0.1:443
# HTTPS on 8443, complete. Host 80 stays published and still answers the ACME
# challenge. Let's Encrypt only ever connects to the public 80 or 443, so the
# certificate is issued if a router in front forwards public 80 to this host and
# is not issued otherwise. Serve your own certificate from the Caddyfile when it
# cannot.
#FLUXER_PUBLIC_PORT=8443
#FLUXER_HTTPS_PORT=8443
# Plain HTTP on 19080, complete. The port 80 publish moves to 19080, so nothing
# binds host 80. Under an http scheme nothing listens on container 443, so the
# last line parks that publish on loopback for a host that wants 443 for
# something else. Drop it and 443 is published and idle, which is what earlier
# releases did.
#FLUXER_PUBLIC_SCHEME=http
#FLUXER_PUBLIC_PORT=19080
#FLUXER_HTTP_PORT=19080
#FLUXER_HTTPS_PORT=127.0.0.1:443
# A tunnel or another proxy in front of the stack needs no HTTPS publish at all.
# tunnel.compose.yml ships beside this file and replaces Caddy's published ports
# with a single loopback HTTP publish, so nothing binds 443. FLUXER_HTTP_PORT
# with a single loopback HTTP publish, so nothing binds 443, and points the edge
# at plain HTTP on that publish so it stops redirecting to https. FLUXER_HTTP_PORT
# still moves that one publish. Set the line below and plain docker compose
# commands pick the file up, or add it to your own -f flags if you pass any. The
# file uses the !override tag, which needs Compose 2.24.4 or newer.
@@ -88,6 +117,60 @@ FLUXER_IMAGE_TAG=v1
POSTGRES_PASSWORD=CHANGE_ME
MEILI_MASTER_KEY=CHANGE_ME
# The stack ships its own Postgres and its own object store, and points at both
# by service name. Set these to run either one outside the stack. Leave them
# unset and the bundled services are used. Taking a service out of the stack
# means an upgrade skips the backup step that reaches into it, and backing that
# store up belongs to whoever runs it.
#FLUXER_POSTGRES_HOST=db.example.com
#FLUXER_POSTGRES_PORT=5432
#FLUXER_POSTGRES_DATABASE=fluxer
#FLUXER_POSTGRES_USERNAME=fluxer
#FLUXER_POSTGRES_SSL=true
#FLUXER_S3_ENDPOINT=https://s3.eu-central-1.amazonaws.com
#FLUXER_S3_PUBLIC_ENDPOINT=https://cdn.example.com
#FLUXER_S3_REGION=eu-central-1
#FLUXER_S3_FORCE_PATH_STYLE=false
# Bucket names. The bundled object store creates whichever names these hold, so
# the two stay in step. An object store outside the stack needs the buckets to
# exist already.
#FLUXER_S3_BUCKET_CDN=fluxer
#FLUXER_S3_BUCKET_UPLOADS=fluxer-uploads
#FLUXER_S3_BUCKET_DOWNLOADS=fluxer-downloads
#FLUXER_S3_BUCKET_REPORTS=fluxer-reports
#FLUXER_S3_BUCKET_HARVESTS=fluxer-harvests
# The rest of the bundled services, pointed somewhere else the same way. Leave a
# line unset and the service in the stack is used. Taking a service out of the
# stack goes in an override file listed in COMPOSE_FILE, because an upgrade
# replaces docker-compose.yml.
#FLUXER_KV_URL=redis://cache.example.com:6379/0
#FLUXER_NATS_URL=nats://mq.example.com:4222
#FLUXER_NATS_JETSTREAM_URL=nats://mq.example.com:4222
#FLUXER_SVC_NATS_URL=nats://mq.example.com:4222
#FLUXER_SEARCH_URL=https://search.example.com
#FLUXER_LIVEKIT_INTERNAL_URL=http://livekit.example.com:7880
# Voice off. The livekit service still runs until an override file takes it out.
#FLUXER_LIVEKIT_ENABLED=false
# Optional systems, each off unless the instance is configured for it.
#FLUXER_SMS_ENABLED=false
#FLUXER_STRIPE_ENABLED=false
#FLUXER_NCMEC_ENABLED=false
#FLUXER_CLAMAV_ENABLED=false
# The client address. Set the header name a proxy in front actually writes, and
# turn the trust off when nothing sits in front, because a trusted header an
# attacker can set is a spoofed client address.
#FLUXER_CLIENT_IP_HEADER_NAME=cf-connecting-ip
#FLUXER_TRUST_CLIENT_IP_HEADER=true
# How much the services write. trace, debug, info, warn, error or fatal. Every
# service names the object storage endpoint and its addressing at info on start,
# so a bucket that answers 404 is visible without raising this.
#LOG_LEVEL=debug
FLUXER_S3_ACCESS_KEY=fluxer
FLUXER_S3_SECRET_KEY=CHANGE_ME
@@ -100,6 +183,12 @@ FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SECRET_BASE64=CHANGE_ME
FLUXER_ADMIN_SECRET_KEY_BASE=CHANGE_ME
FLUXER_ADMIN_OAUTH_CLIENT_SECRET=CHANGE_ME
# The token every service sends to NATS. The bundled NATS runs without
# authentication, so this stays empty unless a Compose override points the stack
# at an external NATS that requires a token. Compose forwards the name to every
# container that connects.
#FLUXER_NATS_AUTH_TOKEN=
FLUXER_VAPID_PUBLIC_KEY=CHANGE_ME
FLUXER_VAPID_PRIVATE_KEY=CHANGE_ME
@@ -147,9 +236,11 @@ FLUXER_VAPID_PRIVATE_KEY=CHANGE_ME
LIVEKIT_API_KEY=fluxer
LIVEKIT_API_SECRET=CHANGE_ME
# The URL browsers use for voice signalling. Derived from FLUXER_PUBLIC_SCHEME,
# FLUXER_DOMAIN and FLUXER_PUBLIC_PORT as wss://host[:port]/livekit when empty.
# Set it only when LiveKit is served from another host.
# The URL browsers use for voice signalling. Compose builds it from
# FLUXER_PUBLIC_ORIGIN, or from FLUXER_PUBLIC_SCHEME, FLUXER_DOMAIN and
# FLUXER_PUBLIC_PORT, as that origin followed by /livekit. The client rewrites a
# leading http to ws itself. Set it only when LiveKit is served from another
# host.
#FLUXER_LIVEKIT_URL=
# Media ports. LiveKit advertises these in ICE candidates, so the host must
@@ -188,7 +279,7 @@ FLUXER_CAPTCHA_TURNSTILE_SITE_KEY=
FLUXER_CAPTCHA_TURNSTILE_SECRET_KEY=
FLUXER_DISCOVERY_ENABLED=true
# Container memory. The 25 limits sum to 16.75 GiB, which is a sum of ceilings and
# Container memory. The 25 limits sum to 18.25 GiB, which is a sum of ceilings and
# not an allocation, so the defaults fit a host with 8 GB and are sized for 16 GB.
# The four reservations are cgroup memory.low, which biases the kernel away from
# reclaiming from the services whose death takes the whole instance down. They do
@@ -199,7 +290,7 @@ FLUXER_DISCOVERY_ENABLED=true
#FLUXER_VALKEY_MEMORY_LIMIT=256mb
#FLUXER_NATS_MEMORY_LIMIT=256mb
#FLUXER_MEILISEARCH_MEMORY_LIMIT=768mb
#FLUXER_SEAWEEDFS_MEMORY_LIMIT=512mb
#FLUXER_SEAWEEDFS_MEMORY_LIMIT=2gb
#FLUXER_SEAWEEDFS_INIT_MEMORY_LIMIT=128mb
#FLUXER_LIVEKIT_MEMORY_LIMIT=512mb
#FLUXER_API_MEMORY_LIMIT=2560mb
@@ -227,6 +318,14 @@ FLUXER_DISCOVERY_ENABLED=true
# which is the container ceiling the indexer shares with the search process.
#FLUXER_MEILISEARCH_MAX_INDEXING_MEMORY=384mb
# SeaweedFS heap ceiling. Go collects against this value instead of against the
# container limit, which it cannot see, so without it an upload burst grows the
# heap past FLUXER_SEAWEEDFS_MEMORY_LIMIT and the kernel OOM-kills the container
# mid-upload (exit 137). Keep it near three quarters of that limit, and raise both
# together: the peak is the parts of one upload in flight at once, which is 25 MB
# times 20 for a 500 MB attachment.
#FLUXER_SEAWEEDFS_GOMEMLIMIT=1536MiB
# Node sizes its own heap from the container memory limit by default, at roughly
# 55 percent of it, which always leaves room for the buffers and stacks that live
# outside the heap. Leave these unset unless you have a reason to pin the value.
+11 -33
View File
@@ -13,73 +13,51 @@
}
handle_path /api/* {
reverse_proxy api:8080 {
header_up X-Forwarded-For {client_ip}
}
reverse_proxy api:8080
}
handle /gateway {
rewrite * /
reverse_proxy gateway:8080 {
header_up X-Forwarded-For {client_ip}
}
reverse_proxy gateway:8080
}
handle_path /gateway/* {
reverse_proxy gateway:8080 {
header_up X-Forwarded-For {client_ip}
}
reverse_proxy gateway:8080
}
handle_path /media/* {
reverse_proxy media-proxy:8080 {
header_up X-Forwarded-For {client_ip}
}
reverse_proxy media-proxy:8080
}
handle_path /livekit/* {
reverse_proxy livekit:7880 {
header_up X-Forwarded-For {client_ip}
}
reverse_proxy livekit:7880
}
handle /admin {
rewrite * /
reverse_proxy admin:8080 {
header_up X-Forwarded-For {client_ip}
}
reverse_proxy admin:8080
}
handle_path /admin/* {
reverse_proxy admin:8080 {
header_up X-Forwarded-For {client_ip}
}
reverse_proxy admin:8080
}
@staticAssets path /web/* /emoji/* /libs/* /avatars/* /badges/* /desktop/* /embeds/*
handle @staticAssets {
reverse_proxy static-proxy:8080 {
header_up X-Forwarded-For {client_ip}
}
reverse_proxy static-proxy:8080
}
handle /.well-known/fluxer {
reverse_proxy api:8080 {
header_up X-Forwarded-For {client_ip}
}
reverse_proxy api:8080
}
handle {
reverse_proxy app-proxy:8080 {
header_up X-Forwarded-For {client_ip}
}
reverse_proxy app-proxy:8080
}
}
:8088 {
handle /.well-known/fluxer {
reverse_proxy api:8080 {
header_up X-Forwarded-For {client_ip}
}
reverse_proxy api:8080
}
}
+46 -32
View File
@@ -2,60 +2,63 @@ name: fluxer
x-fluxer-postgres-env: &fluxer-postgres-env
FLUXER_DATABASE_BACKEND: postgres
FLUXER_POSTGRES_HOST: postgres
FLUXER_POSTGRES_PORT: "5432"
FLUXER_POSTGRES_DATABASE: fluxer
FLUXER_POSTGRES_USERNAME: fluxer
FLUXER_POSTGRES_HOST: ${FLUXER_POSTGRES_HOST:-postgres}
FLUXER_POSTGRES_PORT: "${FLUXER_POSTGRES_PORT:-5432}"
FLUXER_POSTGRES_DATABASE: ${FLUXER_POSTGRES_DATABASE:-fluxer}
FLUXER_POSTGRES_USERNAME: ${FLUXER_POSTGRES_USERNAME:-fluxer}
FLUXER_POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}
FLUXER_POSTGRES_SSL: "false"
FLUXER_POSTGRES_SSL: "${FLUXER_POSTGRES_SSL:-false}"
FLUXER_POSTGRES_PREPARED_STATEMENTS: ${FLUXER_POSTGRES_PREPARED_STATEMENTS:-true}
x-fluxer-env: &fluxer-env
<<: *fluxer-postgres-env
FLUXER_ENV: production
NODE_ENV: production
LOG_LEVEL: ${LOG_LEVEL:-info}
FLUXER_SELF_HOSTED: "true"
FLUXER_BASE_DOMAIN: ${FLUXER_DOMAIN:?set FLUXER_DOMAIN in .env}
FLUXER_PUBLIC_SCHEME: ${FLUXER_PUBLIC_SCHEME:-https}
FLUXER_PUBLIC_PORT: ${FLUXER_PUBLIC_PORT:-443}
FLUXER_TRUST_CLIENT_IP_HEADER: "true"
FLUXER_CLIENT_IP_HEADER_NAME: x-forwarded-for
FLUXER_PUBLIC_ORIGIN: ${FLUXER_PUBLIC_ORIGIN:-}
FLUXER_TRUST_CLIENT_IP_HEADER: "${FLUXER_TRUST_CLIENT_IP_HEADER:-true}"
FLUXER_CLIENT_IP_HEADER_NAME: ${FLUXER_CLIENT_IP_HEADER_NAME:-x-forwarded-for}
FLUXER_API_HEADERS_TIMEOUT_MS: ${FLUXER_API_HEADERS_TIMEOUT_MS:-30000}
FLUXER_API_REQUEST_TIMEOUT_MS: ${FLUXER_API_REQUEST_TIMEOUT_MS:-120000}
FLUXER_KV_URL: redis://valkey:6379/0
FLUXER_NATS_URL: nats://nats:4222
FLUXER_NATS_JETSTREAM_URL: nats://nats:4222
FLUXER_SVC_NATS_URL: nats://nats:4222
FLUXER_KV_URL: ${FLUXER_KV_URL:-redis://valkey:6379/0}
FLUXER_NATS_URL: ${FLUXER_NATS_URL:-nats://nats:4222}
FLUXER_NATS_JETSTREAM_URL: ${FLUXER_NATS_JETSTREAM_URL:-${FLUXER_NATS_URL:-nats://nats:4222}}
FLUXER_NATS_AUTH_TOKEN: ${FLUXER_NATS_AUTH_TOKEN:-}
FLUXER_SVC_NATS_URL: ${FLUXER_SVC_NATS_URL:-${FLUXER_NATS_URL:-nats://nats:4222}}
FLUXER_SVC_SHARD_COUNT: "1"
FLUXER_SEARCH_ENGINE: meilisearch
FLUXER_SEARCH_URL: http://meilisearch:7700
FLUXER_SEARCH_URL: ${FLUXER_SEARCH_URL:-http://meilisearch:7700}
FLUXER_SEARCH_API_KEY: ${MEILI_MASTER_KEY:?set MEILI_MASTER_KEY in .env}
FLUXER_S3_ENDPOINT: http://seaweedfs:8333
FLUXER_S3_PUBLIC_ENDPOINT: http://seaweedfs:8333
FLUXER_S3_REGION: us-east-1
FLUXER_S3_ENDPOINT: ${FLUXER_S3_ENDPOINT:-http://seaweedfs:8333}
FLUXER_S3_PUBLIC_ENDPOINT: ${FLUXER_S3_PUBLIC_ENDPOINT:-${FLUXER_S3_ENDPOINT:-http://seaweedfs:8333}}
FLUXER_S3_REGION: ${FLUXER_S3_REGION:-us-east-1}
FLUXER_S3_ACCESS_KEY_ID: ${FLUXER_S3_ACCESS_KEY:?set FLUXER_S3_ACCESS_KEY in .env}
FLUXER_S3_SECRET_ACCESS_KEY: ${FLUXER_S3_SECRET_KEY:?set FLUXER_S3_SECRET_KEY in .env}
FLUXER_S3_FORCE_PATH_STYLE: "true"
FLUXER_S3_BUCKET_CDN: fluxer
FLUXER_S3_BUCKET_UPLOADS: fluxer-uploads
FLUXER_S3_BUCKET_DOWNLOADS: fluxer-downloads
FLUXER_S3_BUCKET_REPORTS: fluxer-reports
FLUXER_S3_BUCKET_HARVESTS: fluxer-harvests
FLUXER_S3_FORCE_PATH_STYLE: "${FLUXER_S3_FORCE_PATH_STYLE:-true}"
FLUXER_S3_BUCKET_CDN: ${FLUXER_S3_BUCKET_CDN:-fluxer}
FLUXER_S3_BUCKET_UPLOADS: ${FLUXER_S3_BUCKET_UPLOADS:-fluxer-uploads}
FLUXER_S3_BUCKET_DOWNLOADS: ${FLUXER_S3_BUCKET_DOWNLOADS:-fluxer-downloads}
FLUXER_S3_BUCKET_REPORTS: ${FLUXER_S3_BUCKET_REPORTS:-fluxer-reports}
FLUXER_S3_BUCKET_HARVESTS: ${FLUXER_S3_BUCKET_HARVESTS:-fluxer-harvests}
AWS_ACCESS_KEY_ID: ${FLUXER_S3_ACCESS_KEY:?set FLUXER_S3_ACCESS_KEY in .env}
AWS_SECRET_ACCESS_KEY: ${FLUXER_S3_SECRET_KEY:?set FLUXER_S3_SECRET_KEY in .env}
AWS_DEFAULT_REGION: us-east-1
AWS_DEFAULT_REGION: ${FLUXER_S3_REGION:-us-east-1}
AWS_EC2_METADATA_DISABLED: "true"
FLUXER_LIVEKIT_ENABLED: "true"
FLUXER_LIVEKIT_ENABLED: "${FLUXER_LIVEKIT_ENABLED:-true}"
FLUXER_LIVEKIT_API_KEY: ${LIVEKIT_API_KEY:?set LIVEKIT_API_KEY in .env}
FLUXER_LIVEKIT_API_SECRET: ${LIVEKIT_API_SECRET:?set LIVEKIT_API_SECRET in .env}
FLUXER_LIVEKIT_INTERNAL_URL: http://livekit:7880
FLUXER_LIVEKIT_INTERNAL_URL: ${FLUXER_LIVEKIT_INTERNAL_URL:-http://livekit:7880}
FLUXER_LIVEKIT_WEBHOOK_URL: http://api:8080/webhooks/livekit
FLUXER_LIVEKIT_DEFAULT_REGION: '{"id":"default","name":"Default","emoji":"🌍","latitude":0,"longitude":0}'
FLUXER_LIVEKIT_URL: ${FLUXER_LIVEKIT_URL:-${FLUXER_PUBLIC_ORIGIN:-${FLUXER_PUBLIC_SCHEME:-https}://${FLUXER_DOMAIN}}/livekit}
FLUXER_LIVEKIT_URL: ${FLUXER_LIVEKIT_URL:-${FLUXER_PUBLIC_ORIGIN:-${FLUXER_PUBLIC_SCHEME:-https}://${FLUXER_DOMAIN}:${FLUXER_PUBLIC_PORT:-443}}/livekit}
FLUXER_KLIPY_API_KEY: ${FLUXER_KLIPY_API_KEY:-}
@@ -70,16 +73,16 @@ x-fluxer-env: &fluxer-env
FLUXER_EMAIL_SMTP_PASSWORD: ${FLUXER_EMAIL_SMTP_PASSWORD:-}
FLUXER_EMAIL_SMTP_SECURE: ${FLUXER_EMAIL_SMTP_SECURE:-true}
FLUXER_SMS_ENABLED: "false"
FLUXER_SMS_ENABLED: "${FLUXER_SMS_ENABLED:-false}"
FLUXER_CAPTCHA_ENABLED: ${FLUXER_CAPTCHA_ENABLED:-false}
FLUXER_CAPTCHA_PROVIDER: ${FLUXER_CAPTCHA_PROVIDER:-none}
FLUXER_CAPTCHA_HCAPTCHA_SITE_KEY: ${FLUXER_CAPTCHA_HCAPTCHA_SITE_KEY:-}
FLUXER_CAPTCHA_HCAPTCHA_SECRET_KEY: ${FLUXER_CAPTCHA_HCAPTCHA_SECRET_KEY:-}
FLUXER_CAPTCHA_TURNSTILE_SITE_KEY: ${FLUXER_CAPTCHA_TURNSTILE_SITE_KEY:-}
FLUXER_CAPTCHA_TURNSTILE_SECRET_KEY: ${FLUXER_CAPTCHA_TURNSTILE_SECRET_KEY:-}
FLUXER_STRIPE_ENABLED: "false"
FLUXER_NCMEC_ENABLED: "false"
FLUXER_CLAMAV_ENABLED: "false"
FLUXER_STRIPE_ENABLED: "${FLUXER_STRIPE_ENABLED:-false}"
FLUXER_NCMEC_ENABLED: "${FLUXER_NCMEC_ENABLED:-false}"
FLUXER_CLAMAV_ENABLED: "${FLUXER_CLAMAV_ENABLED:-false}"
FLUXER_DISCOVERY_ENABLED: ${FLUXER_DISCOVERY_ENABLED:-true}
FLUXER_SUDO_MODE_SECRET: ${FLUXER_SUDO_MODE_SECRET:?set FLUXER_SUDO_MODE_SECRET in .env}
@@ -131,7 +134,7 @@ services:
- "${FLUXER_HTTPS_PORT:-443}:443"
- "${FLUXER_HTTPS_PORT:-443}:443/udp"
environment:
FLUXER_EDGE_SITE_ADDRESS: ${FLUXER_EDGE_SITE_ADDRESS:-${FLUXER_CADDY_SITE_ADDRESS:-${FLUXER_DOMAIN:?set FLUXER_DOMAIN in .env}}}
FLUXER_EDGE_SITE_ADDRESS: ${FLUXER_EDGE_SITE_ADDRESS:-${FLUXER_CADDY_SITE_ADDRESS:-${FLUXER_PUBLIC_SCHEME:-https}://${FLUXER_DOMAIN:?set FLUXER_DOMAIN in .env}}}
FLUXER_EDGE_TRUSTED_PROXIES: ${FLUXER_EDGE_TRUSTED_PROXIES:-private_ranges}
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile:ro
@@ -258,9 +261,11 @@ services:
deploy:
resources:
limits:
memory: ${FLUXER_SEAWEEDFS_MEMORY_LIMIT:-512mb}
memory: ${FLUXER_SEAWEEDFS_MEMORY_LIMIT:-2gb}
restart: unless-stopped
networks: [fluxer]
environment:
GOMEMLIMIT: ${FLUXER_SEAWEEDFS_GOMEMLIMIT:-1536MiB}
command: ["server", "-s3", "-dir=/data"]
volumes:
- seaweedfs-data:/data
@@ -284,11 +289,16 @@ services:
environment:
FLUXER_S3_ACCESS_KEY: ${FLUXER_S3_ACCESS_KEY:?set FLUXER_S3_ACCESS_KEY in .env}
FLUXER_S3_SECRET_KEY: ${FLUXER_S3_SECRET_KEY:?set FLUXER_S3_SECRET_KEY in .env}
FLUXER_S3_BUCKET_CDN: ${FLUXER_S3_BUCKET_CDN:-fluxer}
FLUXER_S3_BUCKET_UPLOADS: ${FLUXER_S3_BUCKET_UPLOADS:-fluxer-uploads}
FLUXER_S3_BUCKET_DOWNLOADS: ${FLUXER_S3_BUCKET_DOWNLOADS:-fluxer-downloads}
FLUXER_S3_BUCKET_REPORTS: ${FLUXER_S3_BUCKET_REPORTS:-fluxer-reports}
FLUXER_S3_BUCKET_HARVESTS: ${FLUXER_S3_BUCKET_HARVESTS:-fluxer-harvests}
entrypoint:
- /bin/sh
- -c
- >
buckets="fluxer fluxer-uploads fluxer-downloads fluxer-reports fluxer-harvests";
buckets="$$FLUXER_S3_BUCKET_CDN $$FLUXER_S3_BUCKET_UPLOADS $$FLUXER_S3_BUCKET_DOWNLOADS $$FLUXER_S3_BUCKET_REPORTS $$FLUXER_S3_BUCKET_HARVESTS";
missing="$$buckets";
for attempt in $$(seq 1 60); do
if ! nc -z seaweedfs 9333 2>/dev/null; then
@@ -491,6 +501,10 @@ services:
environment:
FLUXER_APP_PROXY_HOST: 0.0.0.0
FLUXER_APP_PROXY_PORT: "8080"
FLUXER_BASE_DOMAIN: ${FLUXER_DOMAIN:?set FLUXER_DOMAIN in .env}
FLUXER_PUBLIC_SCHEME: ${FLUXER_PUBLIC_SCHEME:-https}
FLUXER_PUBLIC_PORT: ${FLUXER_PUBLIC_PORT:-443}
FLUXER_PUBLIC_ORIGIN: ${FLUXER_PUBLIC_ORIGIN:-}
DISCOVERY_UPSTREAM_URL: http://edge:8088/.well-known/fluxer
PUBLIC_BOOTSTRAP_API_ENDPOINT: /api
PUBLIC_BOOTSTRAP_API_PUBLIC_ENDPOINT: ${FLUXER_PUBLIC_ORIGIN:-${FLUXER_PUBLIC_SCHEME:-https}://${FLUXER_DOMAIN}}/api
+2
View File
@@ -2,3 +2,5 @@ services:
edge:
ports: !override
- "${FLUXER_HTTP_PORT:-127.0.0.1:80}:80"
environment:
FLUXER_EDGE_SITE_ADDRESS: ":80"
+38
View File
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import reactGoogleTranslate from 'eslint-plugin-react-google-translate';
import tseslint from 'typescript-eslint';
export default [
{
ignores: [
'**/node_modules/**',
'**/dist/**',
'**/build/**',
'**/coverage/**',
'**/*.generated.*',
'fluxer_app/src/features/i18n/locales/*/messages.mjs',
],
},
{
files: ['fluxer_app/src/**/*.tsx'],
linterOptions: {
reportUnusedDisableDirectives: 'error',
},
languageOptions: {
parser: tseslint.parser,
parserOptions: {
project: './fluxer_app/tsconfig.json',
tsconfigRootDir: import.meta.dirname,
},
},
plugins: {'react-google-translate': reactGoogleTranslate},
rules: {
'react-google-translate/no-conditional-text-nodes-with-siblings': [
'error',
{ignoreParents: ['Trans', 'Plural', 'Select', 'SelectOrdinal']},
],
'react-google-translate/no-return-text-nodes': 'error',
},
},
];
+2 -16
View File
@@ -12287,14 +12287,7 @@
"max_concurrent_guild_starts": {"type": "integer", "minimum": 1, "maximum": 10000, "format": "int32"},
"gateway_dispatch_relay_shards": {"type": "integer", "minimum": 1, "maximum": 10000, "format": "int32"},
"gateway_dispatch_relay_max_queue": {"type": "integer", "minimum": 0, "maximum": 1000000, "format": "int32"},
"voice_e2ee_scope": {"enum": ["guild_feature_only", "platform_wide"], "type": "string"},
"voice_reconciliation_v3_percentage": {"type": "number", "minimum": 0, "maximum": 100},
"voice_reconciliation_v3_interval_ms": {
"type": "integer",
"minimum": 500,
"maximum": 60000,
"format": "int32"
}
"voice_e2ee_scope": {"enum": ["guild_feature_only", "platform_wide"], "type": "string"}
}
},
"InstanceConfigUpdateRequest": {
@@ -12534,14 +12527,7 @@
"max_concurrent_guild_starts": {"type": "integer", "minimum": 1, "maximum": 10000, "format": "int32"},
"gateway_dispatch_relay_shards": {"type": "integer", "minimum": 1, "maximum": 10000, "format": "int32"},
"gateway_dispatch_relay_max_queue": {"type": "integer", "minimum": 0, "maximum": 1000000, "format": "int32"},
"voice_e2ee_scope": {"enum": ["guild_feature_only", "platform_wide"], "type": "string"},
"voice_reconciliation_v3_percentage": {"type": "number", "minimum": 0, "maximum": 100},
"voice_reconciliation_v3_interval_ms": {
"type": "integer",
"minimum": 500,
"maximum": 60000,
"format": "int32"
}
"voice_e2ee_scope": {"enum": ["guild_feature_only", "platform_wide"], "type": "string"}
}
},
"BrandingAssetUploadRequest": {
+7 -1
View File
@@ -129,6 +129,11 @@ impl AdminConfig {
pub fn secure_cookies(&self) -> bool {
self.admin_endpoint.starts_with("https://")
}
pub fn admin_origin(&self) -> Option<String> {
let origin = url::Url::parse(&self.admin_endpoint).ok()?.origin();
origin.is_tuple().then(|| origin.ascii_serialization())
}
}
impl RuntimeEnv {
@@ -202,6 +207,7 @@ mod tests {
unsafe { env::remove_var(name) };
}
unsafe { env::remove_var("FLUXER_PUBLIC_PORT") };
unsafe { env::remove_var("FLUXER_PUBLIC_ORIGIN") };
unsafe { env::set_var("FLUXER_ADMIN_SECRET_KEY_BASE", "test-secret") };
for (name, value) in vars {
unsafe { env::set_var(name, value) };
@@ -350,7 +356,7 @@ mod tests {
("FLUXER_BASE_DOMAIN", "fluxer.example"),
("FLUXER_PUBLIC_PORT", "19080"),
("FLUXER_ADMIN_ENDPOINT", "http://fluxer.example/admin"),
("FLUXER_APP_ENDPOINT", "http://fluxer.example:19080"),
("FLUXER_APP_ENDPOINT", "http://fluxer.example"),
("FLUXER_MEDIA_ENDPOINT", "http://fluxer.example/media"),
("FLUXER_STATIC_CDN_ENDPOINT", "https://cdn.example.net"),
(
+95 -4
View File
@@ -27,7 +27,6 @@ pub async fn csrf_protection(
) -> Response {
let config = state.config();
let secret = config.secret_key_base.clone();
let admin_endpoint = config.admin_endpoint.clone();
let secure_cookies = config.secure_cookies();
let user_id = request
@@ -50,7 +49,7 @@ pub async fn csrf_protection(
.iter()
.any(|suffix| path.ends_with(suffix));
if !is_ignored {
if !is_same_site_request(&request, &admin_endpoint) {
if !is_same_site_request(&request, config.admin_origin().as_deref()) {
return StatusCode::FORBIDDEN.into_response();
}
let header_token = extract_csrf_header(&request);
@@ -167,7 +166,7 @@ async fn extract_csrf_from_form_body(
Ok((request, token))
}
fn is_same_site_request(request: &Request, admin_endpoint: &str) -> bool {
fn is_same_site_request(request: &Request, admin_origin: Option<&str>) -> bool {
if let Some(site) = request
.headers()
.get("sec-fetch-site")
@@ -180,7 +179,7 @@ fn is_same_site_request(request: &Request, admin_endpoint: &str) -> bool {
.get(header::ORIGIN)
.and_then(|value| value.to_str().ok())
{
Some(origin) => origin == admin_endpoint,
Some(origin) => admin_origin.is_some_and(|expected| origin == expected),
None => true,
}
}
@@ -275,6 +274,98 @@ mod tests {
);
}
async fn action_status(admin_endpoint: &str, origin: &str) -> StatusCode {
let state = state_with_admin_endpoint(admin_endpoint);
let app = Router::new()
.route("/", get(|| async { "ok" }).post(|| async { "ok" }))
.layer(from_fn_with_state(state, csrf_protection));
let issued = app
.clone()
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
.await
.expect("router responds");
let cookie = issued
.headers()
.get_all(header::SET_COOKIE)
.iter()
.filter_map(|value| value.to_str().ok())
.filter_map(|value| value.split(';').next())
.find(|pair| pair.contains("csrf_token=") && !pair.ends_with('='))
.expect("a csrf cookie is issued")
.to_owned();
let token = cookie.split_once('=').expect("a cookie value").1.to_owned();
let response = app
.oneshot(
Request::builder()
.method(Method::POST)
.uri("/")
.header(header::COOKIE, cookie.as_str())
.header(header::ORIGIN, origin)
.header(CSRF_HEADER_NAME, token.as_str())
.body(Body::empty())
.unwrap(),
)
.await
.expect("router responds");
response.status()
}
#[tokio::test]
async fn a_matching_origin_passes_the_same_site_check() {
let status = action_status(
"https://admin.example.test/admin",
"https://admin.example.test",
)
.await;
assert_eq!(status, StatusCode::OK);
}
#[tokio::test]
async fn a_matching_origin_on_a_non_default_port_passes_the_same_site_check() {
let status = action_status(
"https://admin.example.test:19080/admin",
"https://admin.example.test:19080",
)
.await;
assert_eq!(status, StatusCode::OK);
}
#[tokio::test]
async fn a_foreign_origin_fails_the_same_site_check() {
let status = action_status(
"https://admin.example.test:19080/admin",
"https://evil.example.test:19080",
)
.await;
assert_eq!(status, StatusCode::FORBIDDEN);
}
#[tokio::test]
async fn another_port_on_the_admin_host_fails_the_same_site_check() {
let status = action_status(
"https://admin.example.test:19080/admin",
"https://admin.example.test",
)
.await;
assert_eq!(status, StatusCode::FORBIDDEN);
}
#[tokio::test]
async fn an_unparseable_admin_endpoint_fails_closed() {
let status = action_status("not-an-endpoint", "https://admin.example.test").await;
assert_eq!(status, StatusCode::FORBIDDEN);
}
#[tokio::test]
async fn an_explicit_default_port_matches_a_portless_origin() {
let status = action_status(
"https://admin.example.test:443/admin",
"https://admin.example.test",
)
.await;
assert_eq!(status, StatusCode::OK);
}
#[test]
fn oauth2_callback_is_exempt() {
let exempt = IGNORED_PATH_SUFFIXES
@@ -560,6 +560,8 @@ fn traits_form(
}
}
const DERIVED_TRAITS: [&str; 1] = ["premium"];
fn parse_trait_definitions(limit_config: Option<&LimitConfigResponse>) -> Vec<&str> {
limit_config
.map(|response| {
@@ -569,6 +571,7 @@ fn parse_trait_definitions(limit_config: Option<&LimitConfigResponse>) -> Vec<&s
.iter()
.map(|value| value.trim())
.filter(|value| !value.is_empty())
.filter(|value| !DERIVED_TRAITS.contains(value))
.collect()
})
.unwrap_or_default()
@@ -579,5 +582,6 @@ fn custom_traits<'a>(user: &'a AdminUser, trait_definitions: &[&str]) -> Vec<&'a
.iter()
.map(String::as_str)
.filter(|trait_name| !trait_definitions.contains(trait_name))
.filter(|trait_name| !DERIVED_TRAITS.contains(trait_name))
.collect()
}
+17 -3
View File
@@ -18,6 +18,7 @@ use tower::ServiceExt;
const SECRET_KEY: &str = "legacy-csrf-cookie-test-secret";
const ADMIN_ORIGIN: &str = "https://admin.example.test";
const LEGACY_HEX_TOKEN: &str = "8f14e45fceea167a5a36dedd4bea25438f14e45fceea167a5a36dedd4bea2543";
const CREATED_KEY_SECRET: &str = "fa_1900000000000000001_OneTimeSecretForTests";
struct TestApp {
router: Router,
@@ -128,6 +129,10 @@ async fn load_page(app: &TestApp, cookie: &str) -> (String, String) {
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
let text = String::from_utf8(body.to_vec()).unwrap();
assert_eq!(status, StatusCode::OK, "{text}");
assert!(
text.contains("AdminUser"),
"the page did not render the admin the mock API returns"
);
let cookie_token = host_csrf_cookie(&headers)
.unwrap_or_else(|| panic!("no __Host-csrf_token in Set-Cookie: {headers:?}"));
let page_token = form_csrf_value(&text).expect("no _csrf hidden input rendered");
@@ -166,7 +171,16 @@ async fn submit_action(app: &TestApp, cookie: &str, form_token: &str) -> StatusC
)
.await
.unwrap();
response.status()
let status = response.status();
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
let text = String::from_utf8(body.to_vec()).unwrap();
if status == StatusCode::OK {
assert!(
text.contains(CREATED_KEY_SECRET),
"the action did not render the key the mock API creates"
);
}
status
}
fn host_csrf_cookie(headers: &HeaderMap) -> Option<String> {
@@ -207,11 +221,11 @@ async fn spawn_mock_api() -> String {
async fn mock_api(method: Method, uri: Uri) -> Response {
match (method, uri.path()) {
(Method::GET, "/admin/users/me") => Json(json!({ "user": admin_user() })).into_response(),
(Method::GET, "/admin/users/@me") => Json(json!({ "user": admin_user() })).into_response(),
(Method::GET, "/admin/api-keys") => Json(json!([])).into_response(),
(Method::POST, "/admin/api-keys") => Json(json!({
"key_id": "1900000000000000001",
"key": "fa_1900000000000000001_OneTimeSecretForTests",
"key": CREATED_KEY_SECRET,
"name": "Legacy Cookie Key",
"created_at": "2026-07-10T15:00:00.000Z",
"expires_at": null,
+125 -1
View File
@@ -2,7 +2,7 @@
import {getRegionDisplayName} from '@fluxer/geo_utils/src/RegionFormatting';
import {getSameIpDecisionKey, isValidIp, normalizeIpString} from '@fluxer/ip_utils/src/IpAddress';
import maxmind, {type CityResponse, type Reader} from 'maxmind';
import maxmind, {type AsnResponse, type CityResponse, type Reader} from 'maxmind';
export const UNKNOWN_LOCATION = 'Unknown Location';
@@ -15,6 +15,15 @@ export interface GeoipResult {
countryName: string | null;
latitude?: number | null;
longitude?: number | null;
accuracyRadiusKm?: number | null;
timeZone?: string | null;
}
export interface GeoipAsnResult {
normalizedIp: string | null;
asn: number | null;
asnOrg: string | null;
available: boolean;
}
type CacheEntry = {
@@ -22,12 +31,21 @@ type CacheEntry = {
expiresAt: number;
};
type AsnCacheEntry = {
result: GeoipAsnResult;
expiresAt: number;
};
const CACHE_TTL_MS = 10 * 60 * 1000;
const CACHE_MAX_ENTRIES = 10_000;
const geoipCache = new Map<string, CacheEntry>();
const asnCache = new Map<string, AsnCacheEntry>();
let maxmindReader: Reader<CityResponse> | null = null;
let maxmindReaderPromise: Promise<Reader<CityResponse>> | null = null;
let maxmindAsnReader: Reader<AsnResponse> | null = null;
let maxmindAsnReaderPromise: Promise<Reader<AsnResponse>> | null = null;
let maxmindAsnUnavailable = false;
function buildFallbackResult(normalizedIp: string): GeoipResult {
return {
@@ -39,6 +57,17 @@ function buildFallbackResult(normalizedIp: string): GeoipResult {
countryName: null,
latitude: null,
longitude: null,
accuracyRadiusKm: null,
timeZone: null,
};
}
function buildAsnFallbackResult(normalizedIp: string | null): GeoipAsnResult {
return {
normalizedIp: normalizedIp || null,
asn: null,
asnOrg: null,
available: false,
};
}
@@ -59,6 +88,24 @@ async function ensureReader(dbPath: string): Promise<Reader<CityResponse>> {
return maxmindReaderPromise;
}
async function ensureAsnReader(dbPath: string): Promise<Reader<AsnResponse>> {
if (maxmindAsnReader) return maxmindAsnReader;
if (!maxmindAsnReaderPromise) {
maxmindAsnReaderPromise = maxmind
.open<AsnResponse>(dbPath, {watchForUpdates: true, watchForUpdatesNonPersistent: true})
.then((reader) => {
maxmindAsnReader = reader;
return reader;
})
.catch((error) => {
maxmindAsnReaderPromise = null;
maxmindAsnUnavailable = true;
throw error;
});
}
return maxmindAsnReaderPromise;
}
function stateLabel(record?: CityResponse): string | null {
const subdivision = record?.subdivisions?.[0];
if (!subdivision) return null;
@@ -112,6 +159,31 @@ function setCachedGeoipResult(cacheKey: string, result: GeoipResult): void {
geoipCache.set(cacheKey, {result, expiresAt: Date.now() + CACHE_TTL_MS});
}
function getCachedAsnResult(cacheKey: string, normalizedIp: string): GeoipAsnResult | null {
const cached = asnCache.get(cacheKey);
if (!cached) {
return null;
}
if (Date.now() >= cached.expiresAt) {
asnCache.delete(cacheKey);
return null;
}
asnCache.delete(cacheKey);
asnCache.set(cacheKey, cached);
return {...cached.result, normalizedIp};
}
function setCachedAsnResult(cacheKey: string, result: GeoipAsnResult): void {
asnCache.delete(cacheKey);
if (asnCache.size >= CACHE_MAX_ENTRIES) {
const oldestKey = asnCache.keys().next().value;
if (oldestKey !== undefined) {
asnCache.delete(oldestKey);
}
}
asnCache.set(cacheKey, {result, expiresAt: Date.now() + CACHE_TTL_MS});
}
async function lookupMaxmind(clean: string, dbPath: string): Promise<GeoipResult> {
try {
const reader = await ensureReader(dbPath);
@@ -128,12 +200,32 @@ async function lookupMaxmind(clean: string, dbPath: string): Promise<GeoipResult
countryName: record.country?.names?.en ?? (countryCode ? countryDisplayName(countryCode) : null) ?? null,
latitude: record.location?.latitude ?? null,
longitude: record.location?.longitude ?? null,
accuracyRadiusKm: record.location?.accuracy_radius ?? null,
timeZone: record.location?.time_zone ?? null,
};
} catch {
return buildFallbackResult(clean);
}
}
async function lookupMaxmindAsn(clean: string, dbPath: string): Promise<GeoipAsnResult> {
try {
const reader = await ensureAsnReader(dbPath);
const record = reader.get(clean);
if (!record) {
return {normalizedIp: clean, asn: null, asnOrg: null, available: true};
}
return {
normalizedIp: clean,
asn: record.autonomous_system_number ?? null,
asnOrg: record.autonomous_system_organization ?? null,
available: true,
};
} catch {
return buildAsnFallbackResult(clean);
}
}
async function resolveGeoip(clean: string, dbPath: string): Promise<GeoipResult> {
const cacheKey = getSameIpDecisionKey(clean) ?? clean;
const cached = getCachedGeoipResult(cacheKey, clean);
@@ -145,6 +237,17 @@ async function resolveGeoip(clean: string, dbPath: string): Promise<GeoipResult>
return result;
}
async function resolveAsn(clean: string, dbPath: string): Promise<GeoipAsnResult> {
const cacheKey = getSameIpDecisionKey(clean) ?? clean;
const cached = getCachedAsnResult(cacheKey, clean);
if (cached) {
return cached;
}
const result = await lookupMaxmindAsn(clean, dbPath);
setCachedAsnResult(cacheKey, result);
return result;
}
export async function lookupGeoipByIp(ip: string, dbPath: string | undefined): Promise<GeoipResult> {
if (!dbPath) {
return buildFallbackResult(ip);
@@ -156,6 +259,27 @@ export async function lookupGeoipByIp(ip: string, dbPath: string | undefined): P
return resolveGeoip(clean, dbPath);
}
export async function lookupAsnByIp(ip: string, asnDbPath: string | undefined): Promise<GeoipAsnResult> {
if (!asnDbPath || maxmindAsnUnavailable) {
return buildAsnFallbackResult(null);
}
const clean = normalizeIpString(ip);
if (!isValidIp(clean)) {
return buildAsnFallbackResult(clean);
}
return resolveAsn(clean, asnDbPath);
}
export function resetGeoipReadersForTesting(): void {
maxmindReader = null;
maxmindReaderPromise = null;
maxmindAsnReader = null;
maxmindAsnReaderPromise = null;
maxmindAsnUnavailable = false;
geoipCache.clear();
asnCache.clear();
}
export function formatGeoipLocation(result: GeoipResult): string | null {
const parts: Array<string> = [];
if (result.city) parts.push(result.city);
+4 -1
View File
@@ -12,6 +12,7 @@ const GEOIP_DOWNLOAD_PATH_QUERY_PARAM = 'download_path';
const GEOIP_ASN_DOWNLOAD_PATH_QUERY_PARAM = 'asn_download_path';
const GEOIP_ASN_KEY_QUERY_PARAM = 'asn_key';
const DEFAULT_GEOIP_TEMPORARY_DIRECTORY = '/tmp/fluxer/geoip';
const DEFAULT_GEOIP_ASN_DB_BASENAME = 'GeoLite2-ASN.mmdb';
type GeoipSourceMode = 'filesystem' | 's3';
@@ -167,9 +168,11 @@ async function downloadS3Object(
}
function createGeoipFilesystemSourceConfig(rawValue: string | undefined): GeoipFilesystemSourceConfig {
const maxmindDbPath = rawValue === '' ? undefined : rawValue;
return {
mode: 'filesystem',
maxmindDbPath: rawValue === '' ? undefined : rawValue,
maxmindDbPath,
maxmindAsnDbPath: maxmindDbPath ? path.join(path.dirname(maxmindDbPath), DEFAULT_GEOIP_ASN_DB_BASENAME) : undefined,
};
}
+77 -6
View File
@@ -9,6 +9,11 @@ const CACHE_KEY_PREFIX = 'ipinfo:max:';
const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/u;
const POSITIVE_CACHE_TTL_SECONDS = 7 * 24 * 60 * 60;
const NEGATIVE_CACHE_TTL_SECONDS = 14 * 24 * 60 * 60;
const FAILURE_TTL_REQUEST_FAILED_SECONDS = 60;
const FAILURE_TTL_HTTP_ERROR_SECONDS = 300;
const FAILURE_TTL_QUOTA_SECONDS = 900;
const FAILURE_TTL_SCHEMA_MISMATCH_SECONDS = 600;
const FAILURE_TTL_BACKGROUND_CAP_SECONDS = 120;
export interface IpInfoGeoBlock {
countryCode: string | null;
@@ -73,6 +78,41 @@ export interface IpInfoCache {
set<T>(key: string, value: T, ttlSeconds?: number): Promise<void>;
}
export type IpInfoLookupPriority = 'critical' | 'standard' | 'background';
export interface IpInfoLookupBudget {
tryConsume(priority: IpInfoLookupPriority): Promise<boolean>;
}
export interface CachedIpInfoFailure extends IpInfoLookupResult {
cachedFailure: true;
failureOutcome: 'http_error' | 'request_failed' | 'schema_mismatch';
failureHttpStatus: number | null;
cachedAtMs: number;
}
export function resolveIpInfoLookupPriority(source: string | undefined): IpInfoLookupPriority {
if (source === 'admin.ip_ban' || source === 'admin.scheduled_deletion_suspicious_ip') return 'critical';
if (source === 'AbusiveIpAutoBanner') return 'background';
return 'standard';
}
export function isCachedIpInfoFailure(value: unknown): value is CachedIpInfoFailure {
return typeof value === 'object' && value !== null && (value as {available?: unknown}).available === false;
}
function failureCacheTtlSeconds(
outcome: CachedIpInfoFailure['failureOutcome'],
httpStatus: number | null,
priority: IpInfoLookupPriority,
): number {
let ttl = FAILURE_TTL_HTTP_ERROR_SECONDS;
if (outcome === 'request_failed') ttl = FAILURE_TTL_REQUEST_FAILED_SECONDS;
else if (outcome === 'schema_mismatch') ttl = FAILURE_TTL_SCHEMA_MISMATCH_SECONDS;
else if (httpStatus === 402 || httpStatus === 403 || httpStatus === 429) ttl = FAILURE_TTL_QUOTA_SECONDS;
return priority === 'background' ? Math.min(ttl, FAILURE_TTL_BACKGROUND_CAP_SECONDS) : ttl;
}
export interface IpInfoLookupContext {
source?: string;
reason?: string;
@@ -86,7 +126,7 @@ export interface IpInfoRequestAuditEvent {
source: string;
reason: string | null;
metadata?: Record<string, string | number | boolean | null>;
outcome: 'http_success' | 'http_error' | 'request_failed' | 'schema_mismatch';
outcome: 'http_success' | 'http_error' | 'request_failed' | 'schema_mismatch' | 'budget_shed';
httpStatus: number | null;
available: boolean;
riskNote: string;
@@ -110,6 +150,7 @@ interface IpInfoServiceContext {
apiKey: string;
cache: IpInfoCache;
auditLogger?: IpInfoRequestAuditLogger;
budget?: IpInfoLookupBudget;
}
export interface IpInfoService {
@@ -177,8 +218,12 @@ export function createIpInfoService(ctx: IpInfoServiceContext): IpInfoService {
return {
async lookup(ip: string, context?: IpInfoLookupContext): Promise<IpInfoLookupResult> {
const cacheKey = `${CACHE_KEY_PREFIX}${getSameIpDecisionKey(ip) ?? ip}`;
const priority = resolveIpInfoLookupPriority(context?.source);
const cached = await ctx.cache.get<IpInfoLookupResult>(cacheKey);
if (cached !== null) {
if (isCachedIpInfoFailure(cached)) {
return unavailable(ip, cached.riskNote);
}
return {...cached, ip};
}
const existing = inflight.get(cacheKey);
@@ -222,6 +267,30 @@ export function createIpInfoService(ctx: IpInfoServiceContext): IpInfoService {
return params.result;
};
const performLookup = async (): Promise<IpInfoLookupResult> => {
if (ctx.budget && !(await ctx.budget.tryConsume(priority))) {
return finalize({
result: unavailable(ip, `IPInfo lookup shed (budget exhausted, priority: ${priority})`),
outcome: 'budget_shed',
httpStatus: null,
});
}
const finalizeFailure = async (params: {
result: IpInfoLookupResult;
outcome: CachedIpInfoFailure['failureOutcome'];
httpStatus: number | null;
}): Promise<IpInfoLookupResult> => {
const entry: CachedIpInfoFailure = {
...params.result,
cachedFailure: true,
failureOutcome: params.outcome,
failureHttpStatus: params.httpStatus,
cachedAtMs: Date.now(),
};
await ctx.cache
.set(cacheKey, entry, failureCacheTtlSeconds(params.outcome, params.httpStatus, priority))
.catch(() => {});
return finalize(params);
};
let payload: unknown;
try {
const res = await fetch(fetchUrl, {
@@ -229,7 +298,7 @@ export function createIpInfoService(ctx: IpInfoServiceContext): IpInfoService {
headers: {Accept: 'application/json'},
});
if (!res.ok) {
return finalize({
return finalizeFailure({
result: unavailable(ip, `IPInfo HTTP ${res.status}`),
outcome: 'http_error',
httpStatus: res.status,
@@ -238,7 +307,7 @@ export function createIpInfoService(ctx: IpInfoServiceContext): IpInfoService {
payload = await res.json();
} catch (err) {
const detail = err instanceof Error ? err.message : String(err);
return finalize({
return finalizeFailure({
result: unavailable(ip, `IPInfo request failed: ${detail}`),
outcome: 'request_failed',
httpStatus: null,
@@ -246,7 +315,7 @@ export function createIpInfoService(ctx: IpInfoServiceContext): IpInfoService {
}
const parsedResponse = RawIpInfoResponseSchema.safeParse(payload);
if (!parsedResponse.success) {
return finalize({
return finalizeFailure({
result: unavailable(ip, formatSchemaMismatch(parsedResponse.error)),
outcome: 'schema_mismatch',
httpStatus: 200,
@@ -261,8 +330,10 @@ export function createIpInfoService(ctx: IpInfoServiceContext): IpInfoService {
httpStatus: 200,
});
};
const promise = performLookup().finally(() => {
inflight.delete(cacheKey);
const promise: Promise<IpInfoLookupResult> = performLookup().finally(() => {
if (inflight.get(cacheKey) === promise) {
inflight.delete(cacheKey);
}
});
inflight.set(cacheKey, promise);
return promise;
@@ -8,6 +8,7 @@ interface TieredIpInfoCacheOptions {
hot: IpInfoCache;
cold: IpInfoCache;
hotTtlSeconds?: number;
skipColdWrite?: (value: unknown) => boolean;
}
export function createTieredIpInfoCache(opts: TieredIpInfoCacheOptions): IpInfoCache {
@@ -18,14 +19,17 @@ export function createTieredIpInfoCache(opts: TieredIpInfoCacheOptions): IpInfoC
if (hit !== null) return hit;
const cold = await opts.cold.get<T>(key).catch(() => null);
if (cold === null) return null;
if (opts.skipColdWrite?.(cold) === true) return cold;
void opts.hot.set(key, cold, hotTtl).catch(() => {});
return cold;
},
async set<T>(key: string, value: T, ttlSeconds?: number): Promise<void> {
await Promise.all([
opts.hot.set(key, value, hotTtl).catch(() => {}),
opts.cold.set(key, value, ttlSeconds).catch(() => {}),
]);
const effectiveHotTtl = Math.max(1, Math.min(hotTtl, ttlSeconds ?? hotTtl));
const writes: Array<Promise<void>> = [opts.hot.set(key, value, effectiveHotTtl).catch(() => {})];
if (opts.skipColdWrite?.(value) !== true) {
writes.push(opts.cold.set(key, value, ttlSeconds).catch(() => {}));
}
await Promise.all(writes);
},
};
}
+55
View File
@@ -81,6 +81,22 @@ function withUploadRelaySecret(master: MasterConfig, secretBase64: string): Mast
};
}
function withStripeLegacyPrices(
master: MasterConfig,
legacyPrices: Record<string, Array<string> | undefined> | undefined,
): MasterConfig {
return {
...master,
integrations: {
...master.integrations,
stripe: {
...master.integrations.stripe,
legacy_prices: legacyPrices,
},
},
};
}
describe('buildAPIConfigFromMaster upload relay secret', () => {
let master: MasterConfig;
beforeAll(async () => {
@@ -111,3 +127,42 @@ describe('buildAPIConfigFromMaster upload relay secret', () => {
);
});
});
describe('buildAPIConfigFromMaster stripe legacy prices', () => {
let master: MasterConfig;
beforeAll(async () => {
master = await loadConfig();
});
it('carries the retired stripe price map from master config onto the api config', () => {
const legacyPrices = {
monthly_brl: ['price_retired_monthly_brl'],
yearly_brl: ['price_retired_yearly_brl_a', 'price_retired_yearly_brl_b'],
monthly_try: ['price_1TMYpdFPC94Os7FdZVRx98Up'],
};
expect(buildAPIConfigFromMaster(withStripeLegacyPrices(master, legacyPrices)).stripe.legacyPrices).toEqual(
legacyPrices,
);
});
it('carries the retired price map even when no live prices are configured', () => {
const withoutPrices: MasterConfig = {
...master,
integrations: {
...master.integrations,
stripe: {
...master.integrations.stripe,
prices: undefined,
legacy_prices: {monthly_try: ['price_1TMYpdFPC94Os7FdZVRx98Up']},
},
},
};
const config = buildAPIConfigFromMaster(withoutPrices);
expect(config.stripe.prices).toBeUndefined();
expect(config.stripe.legacyPrices).toEqual({monthly_try: ['price_1TMYpdFPC94Os7FdZVRx98Up']});
});
it('leaves the retired price map undefined when master config does not set one', () => {
expect(buildAPIConfigFromMaster(withStripeLegacyPrices(master, undefined)).stripe.legacyPrices).toBeUndefined();
});
});
+1
View File
@@ -395,6 +395,7 @@ export function buildAPIConfigFromMaster(master: MasterConfig): APIConfig {
gift1YearTry: master.integrations.stripe.prices.gift_1_year_try,
}
: undefined,
legacyPrices: master.integrations.stripe.legacy_prices,
},
bunny: {
purgeEnabled: master.integrations.bunny.purge_enabled,
@@ -30,8 +30,8 @@ import {phraseBlocklistCache} from '../../middleware/PhraseBlocklistCache';
import {profileSubstringBlocklistCache} from '../../middleware/ProfileSubstringBlocklistCache';
import {urlBlocklistCache} from '../../middleware/UrlBlocklistCache';
import {
getIpBanBlastRadiusVerdict,
getSuspiciousIpSkipReason,
hasHighCgnatBlastRadiusRisk,
isSingleIpBanCandidate,
} from '../../risk/IpBanCgnatGuard';
import {isIpBanExempt} from '../../risk/IpBanExemptions';
@@ -292,7 +292,7 @@ export class AdminBanManagementService {
return false;
}
try {
const highRisk = await hasHighCgnatBlastRadiusRisk(ip, this.deps.ipInfoService, {
const {cgnat: highRisk} = await getIpBanBlastRadiusVerdict(ip, this.deps.ipInfoService, {
source: 'admin.ip_ban',
reason: 'pre_write_cgnat_guard',
});
@@ -45,6 +45,7 @@ import {Logger} from '../../Logger';
import {getInstanceConfigRepository} from '../../middleware/ServiceSingletons';
import type {IRiskHistoryRepository} from '../../risk/HistoricalOutcomeRepository';
import type {HistoricalOutcomeCode} from '../../risk/RiskHistoryTypes';
import {resolveAssignedTraits} from '../../user/UserTraits';
import {getIpAddressReverse, getLocationLabelFromIp} from '../../utils/IpUtils';
import {resolveSessionClientInfo} from '../../utils/SessionClientIdentity';
import {mapUserToAdminResponse} from '../models/UserTypes';
@@ -383,7 +384,8 @@ export class AdminUserSecurityService {
if (!user) {
throw new UnknownUserError();
}
const traitSet = data.traits.length > 0 ? new Set(data.traits) : null;
const assigned = resolveAssignedTraits(user.traits ?? [], data.traits);
const traitSet = assigned.size > 0 ? assigned : null;
const updatedUser = await userRepository.patchUpsert(
userId,
{
+1 -1
View File
@@ -209,7 +209,7 @@ export async function forgotPassword(ctx: ApiContext, {data, request}: ForgotPas
}
const hasValidDns = await emailDnsValidation.hasValidDnsRecords(data.email);
if (!hasValidDns) {
throw InputValidationError.fromCode('email', ValidationErrorCodes.INVALID_EMAIL_ADDRESS);
throw InputValidationError.fromCode('email', ValidationErrorCodes.EMAIL_DOMAIN_CANNOT_RECEIVE_MAIL);
}
const user = await users.findByEmail(data.email);
if (!user) {
+1 -1
View File
@@ -187,7 +187,7 @@ export async function register(
contactDomain = normalizePolicyContactDomain(extractEmailDomain(rawEmail));
const hasValidDns = await emailDnsValidation.hasValidDnsRecords(rawEmail);
if (!hasValidDns) {
throw InputValidationError.fromCode('email', ValidationErrorCodes.INVALID_EMAIL_ADDRESS);
throw InputValidationError.fromCode('email', ValidationErrorCodes.EMAIL_DOMAIN_CANNOT_RECEIVE_MAIL);
}
contactDomainBlocked = accountPolicyEvaluator.isBlockedRegistrationEmailDomain(contactDomain);
if (contactDomainBlocked) {
@@ -123,6 +123,7 @@ export function ChannelController(app: HonoApp) {
const existing = await ctx.get('channelService').channelData.operations.getChannel({
userId: ctx.get('user').id,
channelId,
skipNsfwValidation: true,
});
ctx.set('channelUpdateType', existing.type);
return undefined;
@@ -11,6 +11,7 @@ import {
} from '@fluxer/constants/src/LimitConstants';
import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';
import {CannotSendMessageToNonTextChannelError} from '@fluxer/errors/src/domains/channel/CannotSendMessageToNonTextChannelError';
import {UnknownChannelError} from '@fluxer/errors/src/domains/channel/UnknownChannelError';
import {UnknownMessageError} from '@fluxer/errors/src/domains/channel/UnknownMessageError';
import {FeatureTemporarilyDisabledError} from '@fluxer/errors/src/domains/core/FeatureTemporarilyDisabledError';
import {FileSizeTooLargeError} from '@fluxer/errors/src/domains/core/FileSizeTooLargeError';
@@ -18,6 +19,7 @@ import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidat
import {MissingPermissionsError} from '@fluxer/errors/src/domains/core/MissingPermissionsError';
import {UnknownUserError} from '@fluxer/errors/src/domains/user/UnknownUserError';
import {ServiceUnavailableError} from '@fluxer/errors/src/HttpErrors';
import type {GuildResponse} from '@fluxer/schema/src/domains/guild/GuildResponseSchemas';
import type {
CompleteMultipartAttachmentUploadItem,
CompleteMultipartAttachmentUploadResult,
@@ -26,7 +28,9 @@ import type {
} from '@fluxer/schema/src/domains/message/AttachmentUploadSchemas';
import type {AttachmentID, ChannelID, MessageID, UserID} from '../../BrandedTypes';
import {Config} from '../../Config';
import {SYSTEM_USER_ID} from '../../constants/Core';
import type {IPurgeQueue} from '../../infrastructure/BunnyPurgeQueue';
import type {IGatewayService} from '../../infrastructure/IGatewayService';
import type {IStorageService} from '../../infrastructure/IStorageService';
import type {LimitConfigService} from '../../limits/LimitConfigService';
import {resolveLimitSafe} from '../../limits/LimitConfigUtils';
@@ -64,6 +68,8 @@ interface DeleteAttachmentParams {
requestCache: RequestCache;
}
type UploadActor = 'member' | 'webhook';
interface UploadFormDataAttachmentsParams {
userId: UserID;
channelId: ChannelID;
@@ -76,6 +82,7 @@ interface UploadFormDataAttachmentsParams {
id: number;
filename: string;
}>;
actor?: UploadActor;
}
interface RequestPresignedAttachmentUploadUrlsParams {
@@ -104,6 +111,7 @@ export class AttachmentUploadService {
private messageInteractionService: MessageInteractionService,
private messageService: MessageService,
private limitConfigService: LimitConfigService,
private gatewayService: IGatewayService,
) {}
async uploadFormDataAttachments({
@@ -112,8 +120,9 @@ export class AttachmentUploadService {
clientIp,
files,
attachmentMetadata,
actor = 'member',
}: UploadFormDataAttachmentsParams): Promise<Array<UploadedAttachment>> {
const {maxFileSize} = await this.getUploadPermissionAndLimit({userId, channelId});
const {maxFileSize} = await this.getUploadPermissionAndLimit({userId, channelId, actor});
assertAttachmentFileSizesWithinLimit(
files.map(({file}) => file.size),
maxFileSize,
@@ -168,7 +177,7 @@ export class AttachmentUploadService {
if (!Config.presignedAttachmentUploadsEnabled) {
throw new FeatureTemporarilyDisabledError();
}
const {maxFileSize} = await this.getUploadPermissionAndLimit({userId, channelId});
const {maxFileSize} = await this.getUploadPermissionAndLimit({userId, channelId, actor: 'member'});
assertAttachmentFileSizesWithinLimit(
attachments.map(({file_size}) => file_size),
maxFileSize,
@@ -275,7 +284,7 @@ export class AttachmentUploadService {
if (!Config.presignedAttachmentUploadsEnabled) {
throw new FeatureTemporarilyDisabledError();
}
const {maxFileSize} = await this.getUploadPermissionAndLimit({userId, channelId});
const {maxFileSize} = await this.getUploadPermissionAndLimit({userId, channelId, actor: 'member'});
const bucket = Config.s3.buckets.uploads;
return Promise.all(
uploads.map(async ({upload_filename, upload_id}, index) => {
@@ -412,21 +421,24 @@ export class AttachmentUploadService {
}
}
private async getUploadPermissionAndLimit({userId, channelId}: {userId: UserID; channelId: ChannelID}): Promise<{
private async getUploadPermissionAndLimit({
userId,
channelId,
actor,
}: {
userId: UserID;
channelId: ChannelID;
actor: UploadActor;
}): Promise<{
maxFileSize: number;
}> {
const {channel, guild, checkPermission, member} =
await this.messageInteractionService.authService.getChannelAuthenticated({
userId,
channelId,
});
const {channel, guild} =
actor === 'webhook'
? await this.getWebhookUploadChannel(channelId)
: await this.getMemberUploadChannel({userId, channelId});
if (!TEXT_BASED_CHANNEL_TYPES.has(channel.type)) {
throw new CannotSendMessageToNonTextChannelError();
}
if (guild) {
await checkPermission(Permissions.SEND_MESSAGES | Permissions.ATTACH_FILES);
assertGuildMemberCanCommunicate(member);
}
const user = await this.userRepository.findUnique(userId);
if (!user) {
throw new UnknownUserError();
@@ -439,6 +451,41 @@ export class AttachmentUploadService {
const maxFileSize = user.isBot ? Math.min(resolvedMaxFileSize, ATTACHMENT_MAX_SIZE_BOT) : resolvedMaxFileSize;
return {maxFileSize};
}
private async getMemberUploadChannel({userId, channelId}: {userId: UserID; channelId: ChannelID}): Promise<{
channel: Channel;
guild: GuildResponse | null;
}> {
const {channel, guild, checkPermission, member} =
await this.messageInteractionService.authService.getChannelAuthenticated({
userId,
channelId,
});
if (guild) {
await checkPermission(Permissions.SEND_MESSAGES | Permissions.ATTACH_FILES);
assertGuildMemberCanCommunicate(member);
}
return {channel, guild};
}
private async getWebhookUploadChannel(channelId: ChannelID): Promise<{
channel: Channel;
guild: GuildResponse | null;
}> {
const channel = await this.channelRepository.channelData.findUnique(channelId);
if (!channel) {
throw new UnknownChannelError();
}
if (!channel.guildId) {
return {channel, guild: null};
}
const guild = await this.gatewayService.getGuildData({
guildId: channel.guildId,
userId: SYSTEM_USER_ID,
skipMembershipCheck: true,
});
return {channel, guild};
}
}
async function mapWithConcurrency<T, TResult>(
@@ -111,7 +111,7 @@ export class ChannelDataService {
clientFeatures: ReadonlySet<string>;
requestCache: RequestCache;
}): Promise<Channel> {
const {channel} = await this.auth.getChannelAuthenticated({userId, channelId});
const {channel} = await this.auth.getChannelAuthenticated({userId, channelId, skipNsfwValidation: true});
if (channel.type === ChannelTypes.GROUP_DM) {
return await this.groupDmUpdate.updateGroupDmChannel({
userId,
@@ -162,6 +162,7 @@ export class ChannelService {
this.interactions,
this.messages,
limitConfigService,
gatewayService,
);
this.groupDms = new GroupDmOperationsService(
channelRepository,
@@ -90,8 +90,20 @@ export class ChannelOperationsService {
private rateLimitService: IRateLimitService,
) {}
async getChannel({userId, channelId}: {userId: UserID; channelId: ChannelID}): Promise<Channel> {
const {channel} = await this.channelAuthService.getChannelAuthenticated({userId, channelId});
async getChannel({
userId,
channelId,
skipNsfwValidation,
}: {
userId: UserID;
channelId: ChannelID;
skipNsfwValidation?: boolean;
}): Promise<Channel> {
const {channel} = await this.channelAuthService.getChannelAuthenticated({
userId,
channelId,
skipNsfwValidation,
});
return channel;
}
@@ -127,6 +139,7 @@ export class ChannelOperationsService {
const {channel, guild, checkPermission} = await this.channelAuthService.getChannelAuthenticated({
userId,
channelId,
skipNsfwValidation: true,
});
if (channel.type === ChannelTypes.GROUP_DM) {
throw new InvalidChannelTypeError();
@@ -456,7 +469,11 @@ export class ChannelOperationsService {
if (this.voiceAvailabilityService === null) {
return [];
}
const {channel, guild} = await this.channelAuthService.getChannelAuthenticated({userId, channelId});
const {channel, guild} = await this.channelAuthService.getChannelAuthenticated({
userId,
channelId,
skipNsfwValidation: true,
});
if (channel.type !== ChannelTypes.GUILD_VOICE) {
throw new InvalidChannelTypeError();
}
@@ -308,7 +308,7 @@ export async function createMessageSnapshotsForForward(
return [new MessageSnapshotModel(snapshotData)];
}
function collectEmbedReferencedAttachmentCdnKeys(message: Message): Array<string> {
function collectEmbedReferencedAttachmentCdnKeys(message: Message, ownKeys: ReadonlySet<string>): Array<string> {
const mediaPrefix = `${Config.endpoints.media}/`;
const keys = new Set<string>();
const consider = (url: string | null | undefined): void => {
@@ -316,7 +316,7 @@ function collectEmbedReferencedAttachmentCdnKeys(message: Message): Array<string
return;
}
const key = url.slice(mediaPrefix.length);
if (key.startsWith('attachments/')) {
if (ownKeys.has(key)) {
keys.add(key);
}
};
@@ -342,6 +342,11 @@ export async function purgeMessageAttachments(
): Promise<void> {
const cdnKeys = new Set<string>();
const cdnUrls: Array<string> = [];
const ownedCdnKeys = new Set<string>(
collectMessageAttachments(message).map((attachment) =>
makeAttachmentCdnKey(message.channelId, attachment.id, attachment.filename),
),
);
for (const attachment of collectMessageAttachments(message)) {
const cdnKey = makeAttachmentCdnKey(message.channelId, attachment.id, attachment.filename);
if (cdnKeys.has(cdnKey)) {
@@ -352,7 +357,7 @@ export async function purgeMessageAttachments(
cdnUrls.push(makeAttachmentCdnUrl(message.channelId, attachment.id, attachment.filename));
}
}
for (const embedKey of collectEmbedReferencedAttachmentCdnKeys(message)) {
for (const embedKey of collectEmbedReferencedAttachmentCdnKeys(message, ownedCdnKeys)) {
if (cdnKeys.has(embedKey)) {
continue;
}
@@ -34,6 +34,7 @@ type AttachmentMetadata = ClientAttachmentRequest | ClientUploadedAttachmentRequ
interface ParseMultipartMessageDataOptions {
onPayloadParsed?: (payload: unknown) => void;
actor?: 'member' | 'webhook';
}
export async function parseMultipartMessageData(
@@ -158,6 +159,7 @@ export async function parseMultipartMessageData(
clientIp,
files: filesWithIndices,
attachmentMetadata: inlineNewAttachments,
actor: options?.actor,
});
const uploadedMap = new Map(uploadedAttachments.map((attachment) => [attachment.id, attachment]));
const processedInlineAttachments = inlineNewAttachments.map((clientData) => {
@@ -9,7 +9,12 @@ import {
SENDABLE_MESSAGE_FLAGS,
} from '@fluxer/constants/src/ChannelConstants';
import {GuildNSFWLevel, GuildOperations} from '@fluxer/constants/src/GuildConstants';
import {RelationshipTypes, SensitiveMediaFilterLevel, UserFlags} from '@fluxer/constants/src/UserConstants';
import {
DELETED_USER_ID,
RelationshipTypes,
SensitiveMediaFilterLevel,
UserFlags,
} from '@fluxer/constants/src/UserConstants';
import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';
import {UnknownChannelError} from '@fluxer/errors/src/domains/channel/UnknownChannelError';
import {UnknownMessageError} from '@fluxer/errors/src/domains/channel/UnknownMessageError';
@@ -211,15 +216,18 @@ export class MessageSendService {
return processed.length > 0 ? processed : undefined;
}
private resolveWebhookAttachmentUploadUserId(
private async resolveWebhookAttachmentUploadUserId(
webhook: Webhook,
attachments?: Array<AttachmentRequestData>,
): UserID | undefined {
const uploadUserId = webhook.creatorId ?? undefined;
if (uploadUserId === undefined && this.attachmentsToProcess(attachments) !== undefined) {
throw InputValidationError.fromCode('attachments', ValidationErrorCodes.INVALID_MESSAGE_DATA);
): Promise<UserID | undefined> {
if (this.attachmentsToProcess(attachments) === undefined) {
return webhook.creatorId ?? undefined;
}
return uploadUserId;
if (!webhook.creatorId) {
return createUserID(DELETED_USER_ID);
}
const creator = await this.deps.userRepository.findUnique(webhook.creatorId);
return creator ? webhook.creatorId : createUserID(DELETED_USER_ID);
}
private getOneToOneDmRecipientId(channel: Channel, senderId: UserID): UserID | null {
@@ -1184,7 +1192,7 @@ export class MessageSendService {
flags: this.deps.validationService.calculateMessageFlags(data),
embeds: data.embeds,
attachments: this.attachmentsToProcess(data.attachments),
attachmentUploadUserId: this.resolveWebhookAttachmentUploadUserId(webhook, data.attachments),
attachmentUploadUserId: await this.resolveWebhookAttachmentUploadUserId(webhook, data.attachments),
stickerIds: data.sticker_ids ? data.sticker_ids.flatMap((stickerId) => createStickerID(stickerId)) : undefined,
messageReference,
messageSnapshots,
@@ -1269,7 +1277,7 @@ export class MessageSendService {
data,
channel,
guild,
attachmentUploadUserId: this.resolveWebhookAttachmentUploadUserId(webhook, data.attachments),
attachmentUploadUserId: await this.resolveWebhookAttachmentUploadUserId(webhook, data.attachments),
allowEmbeds: true,
});
await this.deps.dispatchService.dispatchMessageUpdate({channel, message: updatedMessage, requestCache});
@@ -46,6 +46,18 @@ describe('Channel Operation Permissions', () => {
.expect(HTTP_STATUS.FORBIDDEN)
.execute();
});
it('should let a minor manage a mature channel without reading it', async () => {
const owner = await createTestAccount(harness, {dateOfBirth: '2010-01-01'});
const guild = await createGuild(harness, owner.token, 'Mature Channel Guild');
const systemChannel = await getChannel(harness, owner.token, guild.system_channel_id!);
await updateChannel(harness, owner.token, systemChannel.id, {nsfw: true});
const renamed = await updateChannel(harness, owner.token, systemChannel.id, {name: 'still-manageable'});
expect(renamed.name).toBe('still-manageable');
await createBuilder(harness, owner.token)
.get(`/channels/${systemChannel.id}/messages`)
.expect(HTTP_STATUS.FORBIDDEN)
.execute();
});
it('should reject member from updating channel without MANAGE_CHANNELS', async () => {
const owner = await createTestAccount(harness);
const member = await createTestAccount(harness);
+1
View File
@@ -248,6 +248,7 @@ export interface APIConfig {
gift1YearPln?: string;
gift1YearTry?: string;
};
legacyPrices?: Record<string, Array<string> | undefined>;
};
bunny: {
purgeEnabled: boolean;
@@ -40,7 +40,7 @@ export class DonationCheckoutService {
}
const hasValidDns = await this.emailDnsValidationService.hasValidDnsRecords(params.email);
if (!hasValidDns) {
throw InputValidationError.fromCode('email', ValidationErrorCodes.INVALID_EMAIL_ADDRESS);
throw InputValidationError.fromCode('email', ValidationErrorCodes.EMAIL_DOMAIN_CANNOT_RECEIVE_MAIL);
}
const isRecurring = params.interval !== null;
const existingDonor = await this.donationRepository.findDonorByEmail(params.email);
@@ -24,7 +24,7 @@ export class DonationMagicLinkService {
async sendMagicLink(email: string): Promise<void> {
const hasValidDns = await this.emailDnsValidationService.hasValidDnsRecords(email);
if (!hasValidDns) {
throw InputValidationError.fromCode('email', ValidationErrorCodes.INVALID_EMAIL_ADDRESS);
throw InputValidationError.fromCode('email', ValidationErrorCodes.EMAIL_DOMAIN_CANNOT_RECEIVE_MAIL);
}
const donor = await this.donationRepository.findDonorByEmail(email);
if (!donor) {
@@ -401,7 +401,7 @@ export class GuildDiscoveryService extends IGuildDiscoveryService {
const language =
params.primaryLanguage && isValidDiscoveryLanguage(params.primaryLanguage) ? params.primaryLanguage : undefined;
const tag = params.tag && params.tag.trim().length > 0 ? normalizeDiscoveryTag(params.tag) : undefined;
const sortBy = params.sortBy === 'member_count' ? 'memberCount' : 'relevance';
const sortBy = params.sortBy === 'relevance' ? 'relevance' : 'memberCount';
const filters: GuildSearchFilters = {
isDiscoverable: true,
discoveryCategory: params.categoryId,
@@ -444,7 +444,6 @@ export class GuildDiscoveryService extends IGuildDiscoveryService {
for (const guild of guilds) {
const counts = freshCounts.get(BigInt(guild.id) as GuildID);
if (counts) {
guild.member_count = counts.memberCount;
guild.online_count = counts.onlineCount;
}
}
@@ -19,7 +19,7 @@ import type {UserCacheService} from '../../infrastructure/UserCacheService';
import {Logger} from '../../Logger';
import type {RequestCache} from '../../middleware/RequestCacheMiddleware';
import type {GuildBan} from '../../models/GuildBan';
import {hasHighCgnatBlastRadiusRisk, isSingleIpBanCandidate} from '../../risk/IpBanCgnatGuard';
import {getIpBanBlastRadiusVerdict, isSingleIpBanCandidate} from '../../risk/IpBanCgnatGuard';
import {isIpBanExempt} from '../../risk/IpBanExemptions';
import type {IUserRepository} from '../../user/IUserRepository';
import type {WorkerTaskName} from '../../worker/WorkerLaneConfig';
@@ -237,19 +237,20 @@ export class GuildModerationService {
return true;
}
try {
const highRisk = await hasHighCgnatBlastRadiusRisk(userIp, this.ipInfoService, {
const {cgnat, sharedAccess} = await getIpBanBlastRadiusVerdict(userIp, this.ipInfoService, {
source: 'guild.ip_ban',
reason: 'join_cgnat_guard',
});
const highRisk = cgnat || sharedAccess;
if (highRisk) {
Logger.warn(
{userIp, bannedIp},
'Skipping guild IP ban match because IPInfo indicates high CGNAT blast-radius risk',
'Skipping guild IP ban match because IPInfo indicates high shared-network blast-radius risk',
);
}
return !highRisk;
} catch (error) {
Logger.warn({error, userIp, bannedIp}, 'IPInfo CGNAT guard failed while checking guild IP ban');
Logger.warn({error, userIp, bannedIp}, 'IPInfo blast-radius guard failed while checking guild IP ban');
return true;
}
}
@@ -68,23 +68,10 @@ export class GuildSearchService {
const includeNsfwRequested = searchParams.include_nsfw ?? false;
const canUserAccessNsfw =
guildIsAgeRestricted || includeNsfwRequested ? await this.getCanUserAccessNsfw(userId) : false;
if (guildIsAgeRestricted) {
if (!canUserAccessNsfw) {
throw new NsfwContentRequiresAgeVerificationError();
}
if (!includeNsfwRequested) {
const hitsPerPage = searchParams.hits_per_page ?? 25;
const page = searchParams.page ?? 1;
return {
channels: [],
messages: [],
total: 0,
hits_per_page: hitsPerPage,
page,
};
}
if (guildIsAgeRestricted && !canUserAccessNsfw) {
throw new NsfwContentRequiresAgeVerificationError();
}
const canIncludeNsfw = includeNsfwRequested && canUserAccessNsfw;
const canIncludeNsfw = canUserAccessNsfw && (includeNsfwRequested || guildIsAgeRestricted);
const guildNsfw = guildData?.nsfw ?? false;
const channels = await this.channelRepository.listChannels(channelIds);
const channelMap = new Map<string, Channel>();
@@ -14,7 +14,7 @@ import type {GuildID, RoleID, UserID} from '../../../BrandedTypes';
import {guildIdToRoleId} from '../../../BrandedTypes';
import {Logger} from '../../../Logger';
import type {GuildMember} from '../../../models/GuildMember';
import {hasHighCgnatBlastRadiusRisk, isSingleIpBanCandidate} from '../../../risk/IpBanCgnatGuard';
import {getIpBanBlastRadiusVerdict, isSingleIpBanCandidate} from '../../../risk/IpBanCgnatGuard';
import {isIpBanExempt} from '../../../risk/IpBanExemptions';
import type {IUserRepository} from '../../../user/IUserRepository';
import type {IGuildRepositoryAggregate} from '../../repositories/IGuildRepositoryAggregate';
@@ -119,14 +119,15 @@ export class GuildMemberValidationService {
return true;
}
try {
const highRisk = await hasHighCgnatBlastRadiusRisk(userIp, this.ipInfoService, {
const {cgnat, sharedAccess} = await getIpBanBlastRadiusVerdict(userIp, this.ipInfoService, {
source: 'guild.member_ip_ban',
reason: 'join_cgnat_guard',
});
const highRisk = cgnat || sharedAccess;
if (highRisk) {
Logger.warn(
{userIp, bannedIp},
'Skipping guild member IP ban match because IPInfo indicates high CGNAT blast-radius risk',
'Skipping guild member IP ban match because IPInfo indicates high shared-network blast-radius risk',
);
}
return !highRisk;
@@ -8,13 +8,21 @@ import type {
DiscoveryCategoryResponse,
DiscoveryGuildListResponse,
} from '@fluxer/schema/src/domains/guild/GuildDiscoverySchemas';
import type {WorkerTaskHelpers} from '@pkgs/worker/src/contracts/WorkerTask';
import {afterEach, beforeEach, describe, expect, test} from 'vitest';
import {createTestAccount, setUserACLs} from '../../auth/tests/AuthTestUtils';
import type {GuildID} from '../../BrandedTypes';
import {createTestBotAccount} from '../../bot/tests/BotTestUtils';
import {setInjectedGatewayService} from '../../middleware/ServiceRegistry';
import {getGuildRepository} from '../../middleware/ServiceSingletons';
import {banUser} from '../../moderation/tests/ModerationTestUtils';
import {type ApiTestHarness, createApiTestHarness} from '../../test/ApiTestHarness';
import {NoopLogger} from '../../test/mocks/NoopLogger';
import {NoopGatewayService} from '../../test/NoopGatewayService';
import {HTTP_STATUS, TEST_IDS} from '../../test/TestConstants';
import {createBuilder, createBuilderWithoutAuth} from '../../test/TestRequestBuilder';
import syncDiscoveryIndex from '../../worker/tasks/SyncDiscoveryIndex';
import {clearWorkerDependencies, setWorkerDependenciesForTest} from '../../worker/WorkerContext';
import {createGuild, getUserGuilds} from './GuildTestUtils';
async function setGuildMemberCount(harness: ApiTestHarness, guildId: string, memberCount: number): Promise<void> {
@@ -24,6 +32,30 @@ async function setGuildMemberCount(harness: ApiTestHarness, guildId: string, mem
.execute();
}
interface LiveGuildCounts {
memberCount: number;
onlineCount: number;
}
const WORKER_HELPERS = {logger: new NoopLogger()} as unknown as WorkerTaskHelpers;
class LiveCountsGatewayService extends NoopGatewayService {
constructor(private readonly liveCounts: Map<string, LiveGuildCounts>) {
super();
}
override async getDiscoveryGuildCounts(guildIds: Array<GuildID>): Promise<Map<GuildID, LiveGuildCounts>> {
const counts = new Map<GuildID, LiveGuildCounts>();
for (const guildId of guildIds) {
const live = this.liveCounts.get(guildId.toString());
if (live) {
counts.set(guildId, live);
}
}
return counts;
}
}
async function applyAndApprove(
harness: ApiTestHarness,
ownerToken: string,
@@ -44,12 +76,39 @@ async function applyAndApprove(
.execute();
}
async function createApprovedDiscoveryGuild(
harness: ApiTestHarness,
adminToken: string,
name: string,
memberCount: number,
): Promise<string> {
const owner = await createTestAccount(harness);
const guild = await createGuild(harness, owner.token, name);
await setGuildMemberCount(harness, guild.id, memberCount);
await applyAndApprove(
harness,
owner.token,
adminToken,
guild.id,
`${name} welcomes everyone`,
DiscoveryCategories.GAMING,
);
return guild.id;
}
function expectNonIncreasing(counts: Array<number>): void {
for (let index = 1; index < counts.length; index++) {
expect(counts[index]).toBeLessThanOrEqual(counts[index - 1]);
}
}
describe('Discovery Search and Join', () => {
let harness: ApiTestHarness;
beforeEach(async () => {
harness = await createApiTestHarness({search: 'enabled'});
});
afterEach(async () => {
clearWorkerDependencies();
await harness?.shutdown();
});
describe('categories', () => {
@@ -257,6 +316,82 @@ describe('Discovery Search and Join', () => {
.execute();
expect(results.guilds.length).toBeLessThanOrEqual(2);
});
test('should order results by the member count it reports back', async () => {
const liveCounts = new Map<string, LiveGuildCounts>();
setInjectedGatewayService(new LiveCountsGatewayService(liveCounts));
const admin = await createTestAccount(harness);
await setUserACLs(harness, admin, ['admin:authenticate', 'discovery:review']);
const guildIds: Array<string> = [];
for (const memberCount of [50, 40, 30, 20, 10]) {
guildIds.push(
await createApprovedDiscoveryGuild(harness, admin.token, `Ordered Guild ${memberCount}`, memberCount),
);
}
liveCounts.set(guildIds[0], {memberCount: 5, onlineCount: 3});
liveCounts.set(guildIds[4], {memberCount: 500, onlineCount: 7});
const searcher = await createTestAccount(harness);
const results = await createBuilder<DiscoveryGuildListResponse>(harness, searcher.token)
.get('/discovery/guilds?sort_by=member_count&limit=48')
.expect(HTTP_STATUS.OK)
.execute();
expect(results.guilds.map((guild) => guild.id)).toEqual(guildIds);
expectNonIncreasing(results.guilds.map((guild) => guild.member_count));
expect(results.guilds[0].online_count).toBe(3);
expect(results.guilds[4].online_count).toBe(7);
});
test('should rank by member count when the client omits sort_by', async () => {
const admin = await createTestAccount(harness);
await setUserACLs(harness, admin, ['admin:authenticate', 'discovery:review']);
const guildsByCount = new Map<number, string>();
for (const memberCount of [30, 10, 20]) {
guildsByCount.set(
memberCount,
await createApprovedDiscoveryGuild(harness, admin.token, `Unsorted Guild ${memberCount}`, memberCount),
);
}
const searcher = await createTestAccount(harness);
const results = await createBuilder<DiscoveryGuildListResponse>(harness, searcher.token)
.get('/discovery/guilds?limit=48')
.expect(HTTP_STATUS.OK)
.execute();
expect(results.guilds.map((guild) => guild.id)).toEqual([
guildsByCount.get(30),
guildsByCount.get(20),
guildsByCount.get(10),
]);
expectNonIncreasing(results.guilds.map((guild) => guild.member_count));
});
test('should not repeat guilds across pages when the discovery index is resynced', async () => {
const liveCounts = new Map<string, LiveGuildCounts>();
const gatewayService = new LiveCountsGatewayService(liveCounts);
setInjectedGatewayService(gatewayService);
const admin = await createTestAccount(harness);
await setUserACLs(harness, admin, ['admin:authenticate', 'discovery:review']);
const guildIds: Array<string> = [];
for (const [index, memberCount] of [60, 50, 40, 40, 30, 30].entries()) {
guildIds.push(await createApprovedDiscoveryGuild(harness, admin.token, `Paged Guild ${index}`, memberCount));
}
const searcher = await createTestAccount(harness);
const firstPage = await createBuilder<DiscoveryGuildListResponse>(harness, searcher.token)
.get('/discovery/guilds?sort_by=member_count&limit=2&offset=0')
.expect(HTTP_STATUS.OK)
.execute();
expect(firstPage.guilds.map((guild) => guild.id)).toEqual([guildIds[0], guildIds[1]]);
liveCounts.set(guildIds[0], {memberCount: 5, onlineCount: 0});
setWorkerDependenciesForTest({guildRepository: getGuildRepository(), gatewayService});
await syncDiscoveryIndex({}, WORKER_HELPERS);
const secondPage = await createBuilder<DiscoveryGuildListResponse>(harness, searcher.token)
.get('/discovery/guilds?sort_by=member_count&limit=2&offset=2')
.expect(HTTP_STATUS.OK)
.execute();
const thirdPage = await createBuilder<DiscoveryGuildListResponse>(harness, searcher.token)
.get('/discovery/guilds?sort_by=member_count&limit=2&offset=4')
.expect(HTTP_STATUS.OK)
.execute();
const paged = [...firstPage.guilds, ...secondPage.guilds, ...thirdPage.guilds].map((guild) => guild.id);
expect(new Set(paged).size).toBe(paged.length);
expect([...paged].sort()).toEqual([...guildIds].sort());
});
test('should require login to search', async () => {
await createBuilderWithoutAuth(harness).get('/discovery/guilds').expect(HTTP_STATUS.UNAUTHORIZED).execute();
});
@@ -27,25 +27,51 @@ interface EmailDnsValidationServiceOptions {
enforceInTestMode?: boolean;
positiveTtlMs?: number;
negativeTtlMs?: number;
lookupTimeoutMs?: number;
maxCachedDomains?: number;
isEmailEnabled?: () => Promise<boolean>;
}
type DnsResolutionResult = 'valid' | 'invalid' | 'fallback' | 'transient_error';
type DomainVerdict = 'valid' | 'invalid' | 'unverified';
const DOMAIN_NOT_FOUND_CODES = new Set(['ENOTFOUND', 'ENONAME', 'EAI_NONAME', 'NXDOMAIN']);
const DOMAIN_NO_RECORD_CODES = new Set(['ENODATA', 'ENOENT', 'NODATA']);
const DNS_LOOKUP_TIMEOUT_MS = 2000;
const DNS_LOOKUP_TRIES = 1;
const MAX_CACHED_DOMAINS = 10000;
async function isInstanceEmailEnabled(): Promise<boolean> {
const {getInstanceConfigRepository} = await import('../middleware/ServiceSingletons');
return getInstanceConfigRepository().isEmailEnabled();
}
function createLookupTimeoutError(): NodeJS.ErrnoException {
const error: NodeJS.ErrnoException = new Error('Email DNS lookup timed out');
error.code = 'ETIMEOUT';
return error;
}
export class EmailDnsValidationService implements IEmailDnsValidationService {
private readonly resolver: IDnsResolver;
private readonly enforceInTestMode: boolean;
private readonly positiveTtlMs: number;
private readonly negativeTtlMs: number;
private readonly lookupTimeoutMs: number;
private readonly maxCachedDomains: number;
private readonly isEmailEnabled: () => Promise<boolean>;
private readonly domainCache = new Map<string, DomainValidationCacheEntry>();
constructor(options: EmailDnsValidationServiceOptions = {}) {
this.resolver = options.resolver ?? new Resolver();
this.lookupTimeoutMs = options.lookupTimeoutMs ?? DNS_LOOKUP_TIMEOUT_MS;
this.resolver =
options.resolver ??
new Resolver({timeout: this.lookupTimeoutMs, tries: DNS_LOOKUP_TRIES, maxTimeout: this.lookupTimeoutMs});
this.enforceInTestMode = options.enforceInTestMode ?? false;
this.positiveTtlMs = options.positiveTtlMs ?? ms('30 minutes');
this.negativeTtlMs = options.negativeTtlMs ?? ms('5 minutes');
this.maxCachedDomains = options.maxCachedDomains ?? MAX_CACHED_DOMAINS;
this.isEmailEnabled = options.isEmailEnabled ?? isInstanceEmailEnabled;
}
async hasValidDnsRecords(email: string): Promise<boolean> {
@@ -56,13 +82,19 @@ export class EmailDnsValidationService implements IEmailDnsValidationService {
if (!domain) {
return false;
}
if (!(await this.isEmailEnabled())) {
return true;
}
const cached = this.getCachedDomainResult(domain);
if (cached !== null) {
return cached;
}
const isValid = await this.resolveDomain(domain);
this.setCachedDomainResult(domain, isValid);
return isValid;
const verdict = await this.resolveDomain(domain);
if (verdict === 'invalid') {
Logger.warn({domain}, 'Email domain publishes no mail exchange or address records, rejecting the address');
}
this.setCachedDomainResult(domain, verdict);
return verdict !== 'invalid';
}
private extractDomain(email: string): string | null {
@@ -82,41 +114,49 @@ export class EmailDnsValidationService implements IEmailDnsValidationService {
this.domainCache.delete(domain);
return null;
}
this.domainCache.delete(domain);
this.domainCache.set(domain, cached);
return cached.valid;
}
private setCachedDomainResult(domain: string, isValid: boolean): void {
const ttlMs = isValid ? this.positiveTtlMs : this.negativeTtlMs;
private setCachedDomainResult(domain: string, verdict: DomainVerdict): void {
const ttlMs = verdict === 'valid' ? this.positiveTtlMs : this.negativeTtlMs;
if (this.domainCache.size >= this.maxCachedDomains && !this.domainCache.has(domain)) {
const oldestDomain = this.domainCache.keys().next().value;
if (oldestDomain !== undefined) {
this.domainCache.delete(oldestDomain);
}
}
this.domainCache.set(domain, {
valid: isValid,
valid: verdict !== 'invalid',
expiresAtMs: Date.now() + ttlMs,
});
}
private async resolveDomain(domain: string): Promise<boolean> {
private async resolveDomain(domain: string): Promise<DomainVerdict> {
const mxResult = await this.resolveMx(domain);
if (mxResult === 'valid') {
return true;
return 'valid';
}
if (mxResult === 'invalid') {
return false;
return 'invalid';
}
if (mxResult === 'transient_error') {
return true;
return 'unverified';
}
const addressResult = await this.resolveAddressRecords(domain);
if (addressResult === 'valid') {
return true;
return 'valid';
}
if (addressResult === 'invalid') {
return false;
return 'invalid';
}
return true;
return 'unverified';
}
private async resolveMx(domain: string): Promise<DnsResolutionResult> {
try {
const records = await this.resolver.resolveMx(domain);
const records = await this.withLookupDeadline(this.resolver.resolveMx(domain));
if (records.length > 0) {
return 'valid';
}
@@ -128,8 +168,8 @@ export class EmailDnsValidationService implements IEmailDnsValidationService {
private async resolveAddressRecords(domain: string): Promise<DnsResolutionResult> {
const [ipv4Result, ipv6Result] = await Promise.allSettled([
this.resolver.resolve4(domain),
this.resolver.resolve6(domain),
this.withLookupDeadline(this.resolver.resolve4(domain)),
this.withLookupDeadline(this.resolver.resolve6(domain)),
]);
if (ipv4Result.status === 'fulfilled' && ipv4Result.value.length > 0) {
return 'valid';
@@ -147,6 +187,20 @@ export class EmailDnsValidationService implements IEmailDnsValidationService {
return 'invalid';
}
private async withLookupDeadline<T>(lookup: Promise<T>): Promise<T> {
let timer: ReturnType<typeof setTimeout> | undefined;
try {
return await Promise.race([
lookup,
new Promise<never>((_resolve, reject) => {
timer = setTimeout(() => reject(createLookupTimeoutError()), this.lookupTimeoutMs);
}),
]);
} finally {
clearTimeout(timer);
}
}
private classifyResolverError(
error: unknown,
domain: string,
@@ -324,9 +324,6 @@ export class LiveKitService extends ILiveKitService {
participants: participants.map((participant) => ({identity: participant.identity})),
};
} catch (error) {
if (LiveKitService.isHttp404(error)) {
return {status: 'ok', participants: []};
}
Logger.warn({error, regionId, serverId, roomName}, 'LiveKit listParticipants failed');
const status = LiveKitService.getHttpStatus(error);
const isRetryable = status != null && status >= 500;
@@ -143,6 +143,32 @@ describe('StorageService.getPresignedUploadURL', () => {
},
);
});
it('gives both clients the configured addressing rather than pinning one to path style', async () => {
await withS3Config(
{
endpoint: 'https://s3.example.test',
presignedUrlBase: '',
forcePathStyle: false,
region: 'eu-central-1',
accessKeyId: 'fluxer',
secretAccessKey: 'fluxer-secret',
buckets: {uploads: 'fluxer-uploads'},
},
async () => {
const service = new StorageService();
const probe = service as unknown as {
client: {config: {forcePathStyle?: unknown}};
presignClient: {config: {forcePathStyle?: unknown}};
};
const resolve = async (value: unknown): Promise<unknown> =>
typeof value === 'function' ? await (value as () => Promise<unknown>)() : value;
expect(await resolve(probe.client.config.forcePathStyle)).toBe(false);
expect(await resolve(probe.presignClient.config.forcePathStyle)).toBe(false);
},
);
});
});
describe('StorageService.copyObjectWithMetadataStripping', () => {
@@ -127,7 +127,7 @@ export class StorageService implements IStorageService {
region: this.provider.region,
accessKeyId: this.provider.accessKeyId,
secretAccessKey: this.provider.secretAccessKey,
forcePathStyle: true,
forcePathStyle: this.provider.forcePathStyle,
});
this.presignClient = buildPooledS3Client({
endpoint: this.resolvePresignEndpoint(),
@@ -136,6 +136,15 @@ export class StorageService implements IStorageService {
secretAccessKey: this.provider.secretAccessKey,
forcePathStyle: this.provider.forcePathStyle,
});
Logger.info(
{
endpoint: this.provider.endpoint,
presignEndpoint: this.resolvePresignEndpoint(),
region: this.provider.region,
addressing: this.provider.forcePathStyle ? 'path' : 'virtual-host',
},
'Object storage client ready',
);
}
private resolvePresignEndpoint(): string {
@@ -0,0 +1,220 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {ms} from 'itty-time';
import {describe, expect, it} from 'vitest';
import {Config} from '../../Config';
import {getInstanceConfigRepository} from '../../middleware/ServiceSingletons';
import {EmailDnsValidationService} from '../EmailDnsValidationService';
interface MxRecord {
exchange: string;
priority: number;
}
function dnsError(code: string): NodeJS.ErrnoException {
const error: NodeJS.ErrnoException = new Error(`dns lookup failed with ${code}`);
error.code = code;
return error;
}
class FakeDnsResolver {
readonly lookups: Array<string> = [];
mxRecords: Array<MxRecord> = [{exchange: 'mx.example.com', priority: 10}];
mxErrorCode: string | null = null;
addressErrorCode: string | null = 'ENOTFOUND';
addresses: Array<string> = [];
stall = false;
async resolveMx(domain: string): Promise<Array<MxRecord>> {
this.lookups.push(`mx:${domain}`);
if (this.stall) {
return new Promise<Array<MxRecord>>(() => {});
}
if (this.mxErrorCode) {
throw dnsError(this.mxErrorCode);
}
return this.mxRecords;
}
async resolve4(domain: string): Promise<Array<string>> {
this.lookups.push(`a:${domain}`);
if (this.addressErrorCode) {
throw dnsError(this.addressErrorCode);
}
return this.addresses;
}
async resolve6(domain: string): Promise<Array<string>> {
this.lookups.push(`aaaa:${domain}`);
if (this.addressErrorCode) {
throw dnsError(this.addressErrorCode);
}
return this.addresses;
}
}
describe('EmailDnsValidationService', () => {
it('skips the lookup when the instance sends no mail', async () => {
const resolver = new FakeDnsResolver();
resolver.mxErrorCode = 'ENOTFOUND';
const service = new EmailDnsValidationService({
resolver,
enforceInTestMode: true,
isEmailEnabled: async () => false,
});
expect(await service.hasValidDnsRecords('[email protected]')).toBe(true);
expect(resolver.lookups).toEqual([]);
});
it('looks the domain up when the instance sends mail', async () => {
const resolver = new FakeDnsResolver();
const service = new EmailDnsValidationService({
resolver,
enforceInTestMode: true,
isEmailEnabled: async () => true,
});
expect(await service.hasValidDnsRecords('[email protected]')).toBe(true);
expect(resolver.lookups).toEqual(['mx:gmail.com']);
});
it('follows the env email flag when no gate is supplied and the operator set nothing', async () => {
expect(Config.email.enabled).toBe(true);
const resolver = new FakeDnsResolver();
const service = new EmailDnsValidationService({resolver, enforceInTestMode: true});
expect(await service.hasValidDnsRecords('[email protected]')).toBe(true);
expect(resolver.lookups).toEqual(['mx:gmail.com']);
});
it('follows the runtime instance email setting over the env flag', async () => {
expect(Config.email.enabled).toBe(true);
await getInstanceConfigRepository().setInstanceIntegrationsConfig({email: {enabled: false}});
const resolver = new FakeDnsResolver();
resolver.mxErrorCode = 'ENOTFOUND';
const service = new EmailDnsValidationService({resolver, enforceInTestMode: true});
expect(await service.hasValidDnsRecords('[email protected]')).toBe(true);
expect(resolver.lookups).toEqual([]);
});
it('still rejects a syntactically broken address when the instance sends no mail', async () => {
const resolver = new FakeDnsResolver();
const service = new EmailDnsValidationService({
resolver,
enforceInTestMode: true,
isEmailEnabled: async () => false,
});
expect(await service.hasValidDnsRecords('probe@')).toBe(false);
expect(resolver.lookups).toEqual([]);
});
it('rejects a domain that does not exist', async () => {
const resolver = new FakeDnsResolver();
resolver.mxErrorCode = 'ENOTFOUND';
const service = new EmailDnsValidationService({
resolver,
enforceInTestMode: true,
isEmailEnabled: async () => true,
});
expect(await service.hasValidDnsRecords('[email protected]')).toBe(false);
expect(resolver.lookups).toEqual(['mx:asdf.asdf']);
});
it('accepts a domain that publishes address records but no mail records', async () => {
const resolver = new FakeDnsResolver();
resolver.mxErrorCode = 'ENODATA';
resolver.addressErrorCode = null;
resolver.addresses = ['198.51.100.10'];
const service = new EmailDnsValidationService({
resolver,
enforceInTestMode: true,
isEmailEnabled: async () => true,
});
expect(await service.hasValidDnsRecords('[email protected]')).toBe(true);
expect(resolver.lookups).toEqual(['mx:mail-less.test', 'a:mail-less.test', 'aaaa:mail-less.test']);
});
it('rejects a domain that publishes neither mail nor address records', async () => {
const resolver = new FakeDnsResolver();
resolver.mxErrorCode = 'ENODATA';
resolver.addressErrorCode = 'ENODATA';
const service = new EmailDnsValidationService({
resolver,
enforceInTestMode: true,
isEmailEnabled: async () => true,
});
expect(await service.hasValidDnsRecords('[email protected]')).toBe(false);
expect(resolver.lookups).toEqual(['mx:no-records.test', 'a:no-records.test', 'aaaa:no-records.test']);
});
it('allows the address when the resolver fails transiently', async () => {
const resolver = new FakeDnsResolver();
resolver.mxErrorCode = 'ESERVFAIL';
const service = new EmailDnsValidationService({
resolver,
enforceInTestMode: true,
isEmailEnabled: async () => true,
});
expect(await service.hasValidDnsRecords('[email protected]')).toBe(true);
});
it('does not cache a transient failure as a verified domain', async () => {
const resolver = new FakeDnsResolver();
resolver.mxErrorCode = 'ESERVFAIL';
const service = new EmailDnsValidationService({
resolver,
enforceInTestMode: true,
isEmailEnabled: async () => true,
positiveTtlMs: ms('30 minutes'),
negativeTtlMs: 0,
});
expect(await service.hasValidDnsRecords('[email protected]')).toBe(true);
expect(await service.hasValidDnsRecords('[email protected]')).toBe(true);
expect(resolver.lookups).toEqual(['mx:asdf.asdf', 'mx:asdf.asdf']);
});
it('caches a verified domain for the positive ttl', async () => {
const resolver = new FakeDnsResolver();
const service = new EmailDnsValidationService({
resolver,
enforceInTestMode: true,
isEmailEnabled: async () => true,
positiveTtlMs: ms('30 minutes'),
negativeTtlMs: 0,
});
expect(await service.hasValidDnsRecords('[email protected]')).toBe(true);
expect(await service.hasValidDnsRecords('[email protected]')).toBe(true);
expect(resolver.lookups).toEqual(['mx:gmail.com']);
});
it('gives up on a stalled resolver instead of hanging', async () => {
const resolver = new FakeDnsResolver();
resolver.stall = true;
const service = new EmailDnsValidationService({
resolver,
enforceInTestMode: true,
isEmailEnabled: async () => true,
lookupTimeoutMs: 10,
});
const startedAtMs = Date.now();
expect(await service.hasValidDnsRecords('[email protected]')).toBe(true);
expect(Date.now() - startedAtMs).toBeLessThan(ms('5 seconds'));
expect(resolver.lookups).toEqual(['mx:stalled.test']);
});
it('bounds the domain cache', async () => {
const resolver = new FakeDnsResolver();
resolver.mxErrorCode = 'ENOTFOUND';
const service = new EmailDnsValidationService({
resolver,
enforceInTestMode: true,
isEmailEnabled: async () => true,
maxCachedDomains: 2,
});
expect(await service.hasValidDnsRecords('[email protected]')).toBe(false);
expect(await service.hasValidDnsRecords('[email protected]')).toBe(false);
expect(await service.hasValidDnsRecords('[email protected]')).toBe(false);
expect(await service.hasValidDnsRecords('[email protected]')).toBe(false);
expect(resolver.lookups).toEqual(['mx:first.test', 'mx:second.test', 'mx:third.test']);
expect(await service.hasValidDnsRecords('[email protected]')).toBe(false);
expect(resolver.lookups).toEqual(['mx:first.test', 'mx:second.test', 'mx:third.test', 'mx:first.test']);
});
});
@@ -2,7 +2,8 @@
import {AccessToken, TrackSource} from 'livekit-server-sdk';
import {describe, expect, it} from 'vitest';
import {computeLiveKitPublishSources, VOICE_TOKEN_TTL_SECONDS} from '../LiveKitService';
import {createChannelID, createGuildID} from '../../BrandedTypes';
import {computeLiveKitPublishSources, LiveKitService, VOICE_TOKEN_TTL_SECONDS} from '../LiveKitService';
function decodeJwtPayload(token: string): Record<string, unknown> {
const [, payload] = token.split('.');
@@ -57,3 +58,78 @@ describe('LiveKitService publish permissions', () => {
expect(exp - nowSeconds).toBeGreaterThan(0);
});
});
class FakeTwirpError extends Error {
status: number;
code?: string;
constructor(message: string, status: number, code?: string) {
super(message);
this.name = 'TwirpError';
this.status = status;
this.code = code;
}
}
function createServiceWithRoomServiceClient(roomServiceClient: unknown): LiveKitService {
const service = Object.create(LiveKitService.prototype) as LiveKitService;
Reflect.set(
service,
'serverClients',
new Map([
[
'region-1',
new Map([
[
'region-1-server-1',
{
endpoint: 'ws://livekit.test/livekit',
apiKey: 'test-key',
apiSecret: 'test-secret',
isActive: true,
roomServiceClient,
},
],
]),
],
]),
);
return service;
}
describe('LiveKitService listParticipants', () => {
const params = {
guildId: createGuildID(1n),
channelId: createChannelID(2n),
regionId: 'region-1',
serverId: 'region-1-server-1',
};
it('reports a 404 as an unreadable room instead of an empty one', async () => {
const service = createServiceWithRoomServiceClient({
listParticipants: async () => {
throw new FakeTwirpError('not_found', 404, 'not_found');
},
});
const result = await service.listParticipants(params);
expect(result.status).toBe('error');
});
it('reports a bad_route 404 as an unreadable room instead of an empty one', async () => {
const service = createServiceWithRoomServiceClient({
listParticipants: async () => {
throw new FakeTwirpError('invalid path prefix', 404, 'bad_route');
},
});
const result = await service.listParticipants(params);
expect(result.status).toBe('error');
expect(result.status === 'error' && result.retryable).toBe(false);
});
it('still reports a genuinely empty room as empty', async () => {
const service = createServiceWithRoomServiceClient({
listParticipants: async () => [],
});
const result = await service.listParticipants(params);
expect(result).toEqual({status: 'ok', participants: []});
});
});
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {Config} from '../Config';
let cachedCollectDateOfBirth: boolean | null = null;
export function getDefaultDateOfBirthCollection(): boolean {
return !Config.instance.selfHosted;
}
export function instanceCollectsDateOfBirth(): boolean {
return cachedCollectDateOfBirth ?? getDefaultDateOfBirthCollection();
}
export function setCachedDateOfBirthCollection(collect: boolean): void {
cachedCollectDateOfBirth = collect;
}
@@ -63,8 +63,6 @@ describe('GatewayRolloutConfigPublisher', () => {
gateway_dispatch_relay_shards: 32,
gateway_dispatch_relay_max_queue: 50000,
voice_e2ee_scope: 'guild_feature_only',
voice_reconciliation_v3_percentage: 100,
voice_reconciliation_v3_interval_ms: 2000,
};
await publisher.publish(config);
@@ -17,6 +17,7 @@ import {resolveDeferredPhoneGateEnabled, setCachedDeferredPhoneGateEnabled} from
import {InstanceConfiguration} from '../Tables';
import {DEFAULT_DECAY_CONSTANTS, DEFAULT_RENEWAL_CONSTANTS} from '../utils/AttachmentDecay';
import {isJsonRecord, parseJsonArray, parseJsonRecord} from '../utils/JsonBoundaryUtils';
import {getDefaultDateOfBirthCollection, setCachedDateOfBirthCollection} from './DateOfBirthCollectionCache';
import {normalizeSsoAllowedEmailDomains} from './SsoConfigValidation';
const GATEWAY_ROLLOUT_CONFIG_KEY = 'gateway_rollout_config';
@@ -41,8 +42,6 @@ const DEFAULT_GATEWAY_ROLLOUT_CONFIG: GatewayRolloutConfig = {
gateway_dispatch_relay_shards: 32,
gateway_dispatch_relay_max_queue: 50000,
voice_e2ee_scope: 'guild_feature_only',
voice_reconciliation_v3_percentage: 100,
voice_reconciliation_v3_interval_ms: 2000,
};
export type InstanceRegistrationMode = 'open' | 'approval' | 'closed';
export interface InstanceRegistrationConfig {
@@ -357,11 +356,21 @@ function getDefaultAppPublicConfig(): InstanceAppPublicConfig {
privacy_url: null,
},
registration: {
collect_date_of_birth: !Config.instance.selfHosted,
collect_date_of_birth: getDefaultDateOfBirthCollection(),
},
};
}
function parseAppPublicConfig(raw: string): InstanceAppPublicConfig {
try {
const parsed: unknown = JSON.parse(raw);
return normalizeAppPublicConfig(parsed);
} catch (error) {
Logger.warn({error}, 'Invalid app public config JSON, returning defaults');
return getDefaultAppPublicConfig();
}
}
function normalizeAppPublicConfig(value: unknown): InstanceAppPublicConfig {
const defaults = getDefaultAppPublicConfig();
if (!isJsonRecord(value)) {
@@ -939,6 +948,7 @@ export class InstanceConfigRepository {
this.configCache = await this.fetchAllConfigsFromDatabase();
} while (this.refreshRequested);
this.syncDeferredPhoneGateCache(this.configCache.get(INSTANCE_POLICY_CONFIG_KEY) ?? null);
this.syncDateOfBirthCollectionCache(this.configCache.get(APP_PUBLIC_CONFIG_KEY) ?? null);
})().finally(() => {
this.refreshPromise = null;
});
@@ -950,6 +960,11 @@ export class InstanceConfigRepository {
setCachedDeferredPhoneGateEnabled(resolveDeferredPhoneGateEnabled(policy));
}
private syncDateOfBirthCollectionCache(raw: string | null): void {
const appPublic = raw ? normalizeAppPublicConfig(parseJsonRecord(raw)) : getDefaultAppPublicConfig();
setCachedDateOfBirthCollection(appPublic.registration.collect_date_of_birth);
}
private updateCachedConfigs(entries: Array<[string, string]>): void {
if (!this.configCache) {
return;
@@ -1067,16 +1082,9 @@ export class InstanceConfigRepository {
async getAppPublicConfig(): Promise<InstanceAppPublicConfig> {
const raw = await this.getConfig(APP_PUBLIC_CONFIG_KEY);
if (!raw) {
return getDefaultAppPublicConfig();
}
try {
const parsed: unknown = JSON.parse(raw);
return normalizeAppPublicConfig(parsed);
} catch (error) {
Logger.warn({error}, 'Invalid app public config JSON, returning defaults');
return getDefaultAppPublicConfig();
}
const config = raw ? parseAppPublicConfig(raw) : getDefaultAppPublicConfig();
setCachedDateOfBirthCollection(config.registration.collect_date_of_birth);
return config;
}
async setAppPublicConfig(config: {
@@ -1105,6 +1113,7 @@ export class InstanceConfigRepository {
},
});
await this.setConfig(APP_PUBLIC_CONFIG_KEY, JSON.stringify(next));
setCachedDateOfBirthCollection(next.registration.collect_date_of_birth);
return next;
}
@@ -15,6 +15,7 @@ import type {HonoEnv} from '../types/HonoEnv';
import {parseJsonRecord} from '../utils/JsonBoundaryUtils';
import {ipBanCache} from './IpBanMiddleware';
import {getIpInfoService} from './ServiceMiddleware';
import {getKVClient} from './ServiceRegistry';
import {getCacheService} from './ServiceSingletons';
type IpClass = 'datacenter' | 'anonymous' | 'mobile' | 'residential' | 'unknown';
@@ -51,6 +52,22 @@ interface AbuseSignalOptions {
weight?: number;
}
interface PeerIpClassHint {
ipClass: IpClass;
expiresAtMs: number;
}
interface OutboundIpClass {
lookupIp: string;
ipClass: IpClass;
}
interface ResolvedBanClass {
ipClass: IpClass;
authoritative: boolean;
blocked: boolean;
}
const WINDOW_MS = positiveNumberFromEnv('FLUXER_ABUSE_WINDOW_MS', 60_000);
const THRESHOLD_DATACENTER = positiveNumberFromEnv('FLUXER_ABUSE_THRESHOLD_DATACENTER', 20);
const THRESHOLD_ANONYMOUS = positiveNumberFromEnv('FLUXER_ABUSE_THRESHOLD_ANONYMOUS', 500);
@@ -73,6 +90,14 @@ const REQUIRED_SCORE_WINDOWS_FOR_AUTO_BAN = positiveNumberFromEnv(
3,
);
const REPLICATION_CHANNEL = 'abuse_tracker:ticks';
const IP_CLASS_CHANNEL = 'abuse_tracker:ipclass';
const IP_CLASS_CLAIM_PREFIX = 'abuse:ipclass:claim:';
const IP_CLASS_CLAIM_ENABLED = process.env.FLUXER_ABUSE_IP_CLASS_CLAIM_ENABLED !== '0';
const IP_CLASS_CLAIM_TTL_SECONDS = positiveNumberFromEnv('FLUXER_ABUSE_IP_CLASS_CLAIM_TTL_SEC', 15);
const DEFAULT_IP_CLASS_PENDING_TTL_MS = positiveNumberFromEnv('FLUXER_ABUSE_IP_CLASS_PENDING_TTL_MS', 20_000);
const DEFAULT_IP_CLASS_NEGATIVE_TTL_MS = positiveNumberFromEnv('FLUXER_ABUSE_IP_CLASS_NEGATIVE_TTL_MS', 300_000);
const DEFAULT_IP_CLASS_HINT_TTL_MS = positiveNumberFromEnv('FLUXER_ABUSE_IP_CLASS_HINT_TTL_MS', 600_000);
const IP_CLASSES = ['datacenter', 'anonymous', 'mobile', 'residential', 'unknown'] as const;
const POD_ID = process.env.HOSTNAME ?? randomUUID();
type ReplicatedTick = [banKey: string, scoreDelta: number, tokenHashes: Array<string>, lookupIp: string];
@@ -83,11 +108,23 @@ interface ReplicationMessage {
ts: number;
}
type IpClassEntry = [banKey: string, lookupIp: string, ipClass: IpClass];
interface IpClassMessage {
sender: string;
entries: Array<IpClassEntry>;
ts: number;
}
const records = new Map<string, AbuseRecord>();
const outboundDeltas = new Map<string, OutboundEntry>();
const persistentScoreWindows = new Map<string, PersistentScoreState>();
const ipClassCache = new Map<string, IpClass>();
const ipClassPending = new Set<string>();
const ipClassPending = new Map<string, number>();
const ipClassNegativeUntil = new Map<string, number>();
const peerIpClassHints = new Map<string, PeerIpClassHint>();
const outboundIpClasses = new Map<string, OutboundIpClass>();
const pendingIpClassTasks = new Set<Promise<void>>();
const recordedClientErrorRequests = new WeakSet<Request>();
const pendingAutoBanTasks = new Set<Promise<void>>();
const adminRepository = new AdminRepository();
@@ -97,6 +134,9 @@ let flushTimer: NodeJS.Timeout | null = null;
let kvSubscription: IKVSubscription | null = null;
let messageHandler: ((channel: string, message: string) => void) | null = null;
let errorHandler: ((error: Error) => void) | null = null;
let ipClassPendingTtlMs = DEFAULT_IP_CLASS_PENDING_TTL_MS;
let ipClassNegativeTtlMs = DEFAULT_IP_CLASS_NEGATIVE_TTL_MS;
let ipClassHintTtlMs = DEFAULT_IP_CLASS_HINT_TTL_MS;
function positiveNumberFromEnv(name: string, fallback: number): number {
const raw = process.env[name];
@@ -164,13 +204,99 @@ function shouldSkipAutoBanForIpClass(ipClass: IpClass): boolean {
return ipClass === 'mobile';
}
function isIpClass(value: unknown): value is IpClass {
return typeof value === 'string' && (IP_CLASSES as ReadonlyArray<string>).includes(value);
}
function getOwnIpClass(key: string, now: number): IpClass | null {
const cached = ipClassCache.get(key);
if (cached === undefined) return null;
const negativeUntilMs = ipClassNegativeUntil.get(key);
if (negativeUntilMs !== undefined && negativeUntilMs <= now) {
ipClassCache.delete(key);
ipClassNegativeUntil.delete(key);
return null;
}
return cached;
}
function isOwnIpClassNegative(key: string, now: number): boolean {
const negativeUntilMs = ipClassNegativeUntil.get(key);
return negativeUntilMs !== undefined && negativeUntilMs > now;
}
function setOwnIpClass(key: string, lookupIp: string, ipClass: IpClass, negative: boolean): void {
ipClassCache.set(key, ipClass);
if (negative) {
ipClassNegativeUntil.set(key, Date.now() + ipClassNegativeTtlMs);
} else {
ipClassNegativeUntil.delete(key);
peerIpClassHints.delete(key);
}
ipClassPending.delete(key);
if (!negative && ipClass !== 'unknown') {
queueOutboundIpClass(key, lookupIp, ipClass);
}
const rec = records.get(key);
if (rec) maybeFireAutoBan(key, rec);
}
function isIpClassPending(key: string, now: number): boolean {
const expiresAtMs = ipClassPending.get(key);
if (expiresAtMs === undefined) return false;
if (expiresAtMs <= now) {
ipClassPending.delete(key);
return false;
}
return true;
}
function getPeerClassHint(key: string, now: number): IpClass | null {
const hint = peerIpClassHints.get(key);
if (!hint) return null;
if (hint.expiresAtMs <= now) {
peerIpClassHints.delete(key);
return null;
}
return hint.ipClass;
}
function recordPeerClassHint(key: string, ipClass: IpClass): void {
peerIpClassHints.set(key, {ipClass, expiresAtMs: Date.now() + ipClassHintTtlMs});
}
function isStricterThanUnknown(ipClass: IpClass): boolean {
return (
scoreThresholdFor(ipClass) <= scoreThresholdFor('unknown') &&
tokenDiversityThresholdFor(ipClass) <= tokenDiversityThresholdFor('unknown')
);
}
function resolveClassForBan(key: string, now: number): ResolvedBanClass {
const own = getOwnIpClass(key, now);
if (own !== null && !isOwnIpClassNegative(key, now)) return {ipClass: own, authoritative: true, blocked: false};
const hint = getPeerClassHint(key, now);
if (hint !== null && isStricterThanUnknown(hint)) return {ipClass: hint, authoritative: true, blocked: false};
if (hint !== null) return {ipClass: 'unknown', authoritative: false, blocked: true};
if (own !== null) return {ipClass: own, authoritative: true, blocked: false};
return {ipClass: 'unknown', authoritative: false, blocked: isIpClassPending(key, now)};
}
function pruneIfNeeded(now: number): void {
if (records.size < MAX_TRACKED_IPS) return;
for (const [key, expiresAtMs] of ipClassPending) {
if (expiresAtMs <= now) ipClassPending.delete(key);
}
for (const [key, hint] of peerIpClassHints) {
if (hint.expiresAtMs <= now) peerIpClassHints.delete(key);
}
for (const [key, rec] of records) {
if (rec.windowStartMs + WINDOW_MS < now) {
if (rec.windowStartMs + WINDOW_MS < now && !ipClassPending.has(key)) {
records.delete(key);
ipClassCache.delete(key);
ipClassPending.delete(key);
ipClassNegativeUntil.delete(key);
peerIpClassHints.delete(key);
outboundIpClasses.delete(key);
}
if (records.size < MAX_TRACKED_IPS * 0.9) return;
}
@@ -222,9 +348,11 @@ function queueOutboundDelta(
}
}
function shouldEnsureIpClassLookup(key: string, rec: AbuseRecord): boolean {
if (ipClassCache.has(key) || ipClassPending.has(key)) return false;
return rec.score >= MIN_SCORE_FOR_IP_LOOKUP || rec.distinctTokenHashes.size >= MIN_TOKENS_FOR_IP_LOOKUP;
function shouldEnsureIpClassLookup(key: string, rec: AbuseRecord, now: number): boolean {
if (getOwnIpClass(key, now) !== null || isIpClassPending(key, now)) return false;
if (getPeerClassHint(key, now) !== null) return false;
if (rec.score < MIN_SCORE_FOR_IP_LOOKUP && rec.distinctTokenHashes.size < MIN_TOKENS_FOR_IP_LOOKUP) return false;
return ipBanCache.getMatch(rec.lookupIp) === null;
}
function markScoreThresholdWindow(key: string, rec: AbuseRecord, now: number): number {
@@ -247,33 +375,53 @@ function markScoreThresholdWindow(key: string, rec: AbuseRecord, now: number): n
return state.count;
}
async function claimIpClassLookup(key: string): Promise<boolean> {
if (!IP_CLASS_CLAIM_ENABLED) return true;
if (!kvPublisher) return true;
try {
return await getKVClient().setnx(`${IP_CLASS_CLAIM_PREFIX}${key}`, POD_ID, IP_CLASS_CLAIM_TTL_SECONDS);
} catch {
return true;
}
}
async function runIpClassLookup(key: string, lookupIp: string): Promise<void> {
try {
if (!(await claimIpClassLookup(key))) return;
const result = await getIpInfoService().lookup(lookupIp, {source: 'AbusiveIpAutoBanner', reason: 'classify'});
setOwnIpClass(key, lookupIp, classifyIpInfo(result), !result.available);
} catch (err) {
setOwnIpClass(key, lookupIp, 'unknown', true);
Logger.warn({err, ip: lookupIp}, '[abuse-auto-ban] IP classification lookup failed');
}
}
function ensureIpClassLookup(key: string, lookupIp: string): void {
if (ipClassCache.has(key) || ipClassPending.has(key)) return;
ipClassPending.add(key);
void (async () => {
try {
const result = await getIpInfoService().lookup(lookupIp, {source: 'AbusiveIpAutoBanner', reason: 'classify'});
ipClassCache.set(key, classifyIpInfo(result));
} catch (err) {
ipClassCache.set(key, 'unknown');
Logger.warn({err, ip: lookupIp}, '[abuse-auto-ban] IP classification lookup failed');
} finally {
ipClassPending.delete(key);
const rec = records.get(key);
if (rec) maybeFireAutoBan(key, rec);
}
})();
const now = Date.now();
if (getOwnIpClass(key, now) !== null || isIpClassPending(key, now)) return;
ipClassPending.set(key, now + ipClassPendingTtlMs);
const task = runIpClassLookup(key, lookupIp);
pendingIpClassTasks.add(task);
void task.finally(() => {
pendingIpClassTasks.delete(task);
});
}
function maybeFireAutoBan(key: string, rec: AbuseRecord): void {
if (rec.autoBanFired) return;
const ipClass = ipClassCache.get(key) ?? 'unknown';
const now = Date.now();
if (ipBanCache.getMatch(rec.lookupIp) !== null) {
rec.autoBanFired = true;
return;
}
const resolved = resolveClassForBan(key, now);
const ipClass = resolved.ipClass;
const scoreThreshold = scoreThresholdFor(ipClass);
const tokenThreshold = tokenDiversityThresholdFor(ipClass);
const overScore = rec.score >= scoreThreshold;
const overTokenDiversity = rec.distinctTokenHashes.size >= tokenThreshold;
if (!overScore && !overTokenDiversity) return;
if (!ipClassCache.has(key) && ipClassPending.has(key)) {
if (resolved.blocked) {
return;
}
if (shouldSkipAutoBanForIpClass(ipClass)) {
@@ -369,7 +517,7 @@ export function recordAbuseSignal(ip: string | null, reason: string, opts: Abuse
queuedTokenHash = opts.tokenHash;
}
queueOutboundDelta(signalIp, weight, queuedTokenHash, hadToken);
if (shouldEnsureIpClassLookup(signalIp.banKey, rec)) {
if (shouldEnsureIpClassLookup(signalIp.banKey, rec, now)) {
ensureIpClassLookup(signalIp.banKey, signalIp.lookupIp);
}
maybeFireAutoBan(signalIp.banKey, rec);
@@ -404,7 +552,7 @@ function applyReplicatedTick(tick: ReplicatedTick): void {
if (rec.distinctTokenHashes.size >= MAX_TOKEN_HASHES_PER_IP) break;
rec.distinctTokenHashes.add(tokenHash);
}
if (shouldEnsureIpClassLookup(banKey, rec)) {
if (shouldEnsureIpClassLookup(banKey, rec, now)) {
ensureIpClassLookup(banKey, lookupIp);
}
maybeFireAutoBan(banKey, rec);
@@ -435,7 +583,56 @@ async function flushOutbound(): Promise<void> {
}
}
function queueOutboundIpClass(key: string, lookupIp: string, ipClass: IpClass): void {
if (!kvPublisher) return;
if (!outboundIpClasses.has(key) && outboundIpClasses.size >= MAX_TRACKED_IPS) return;
outboundIpClasses.set(key, {lookupIp, ipClass});
}
async function flushOutboundIpClasses(): Promise<void> {
if (!kvPublisher || outboundIpClasses.size === 0) return;
const entries: Array<IpClassEntry> = [];
const selectedKeys: Array<string> = [];
for (const [key, entry] of outboundIpClasses) {
entries.push([key, entry.lookupIp, entry.ipClass]);
selectedKeys.push(key);
if (entries.length >= MAX_BATCH_TICKS) break;
}
const message: IpClassMessage = {sender: POD_ID, entries, ts: Date.now()};
try {
await kvPublisher.publish(IP_CLASS_CHANNEL, JSON.stringify(message));
for (const key of selectedKeys) {
outboundIpClasses.delete(key);
}
} catch (err) {
Logger.warn({err, entryCount: entries.length}, '[abuse-auto-ban] Failed to publish abuse IP class batch');
}
}
function handleIpClassMessage(message: string): void {
const msg = parseJsonRecord(message);
if (!msg || msg.sender === POD_ID || !Array.isArray(msg.entries)) return;
for (const rawEntry of msg.entries) {
if (!Array.isArray(rawEntry) || rawEntry.length < 3) continue;
const [banKey, lookupIp, ipClass] = rawEntry;
if (typeof banKey !== 'string' || typeof lookupIp !== 'string' || !isIpClass(ipClass)) continue;
if (ipClass === 'unknown') continue;
const signalIp = normalizeSignalIp(lookupIp);
if (!signalIp || signalIp.banKey !== banKey) continue;
const rec = records.get(banKey);
if (!rec) continue;
const now = Date.now();
if (getOwnIpClass(banKey, now) !== null && !isOwnIpClassNegative(banKey, now)) continue;
recordPeerClassHint(banKey, ipClass);
maybeFireAutoBan(banKey, rec);
}
}
function handleReplicationMessage(channel: string, message: string): void {
if (channel === IP_CLASS_CHANNEL) {
handleIpClassMessage(message);
return;
}
if (channel !== REPLICATION_CHANNEL) return;
const msg = parseJsonRecord(message);
if (!msg || msg.sender === POD_ID || !Array.isArray(msg.ticks)) return;
@@ -469,11 +666,12 @@ export async function startAbuseReplicationSubscriber(kvClient: IKVProvider | nu
};
try {
await subscription.connect();
await subscription.subscribe(REPLICATION_CHANNEL);
await subscription.subscribe(REPLICATION_CHANNEL, IP_CLASS_CHANNEL);
subscription.on('message', messageHandler);
subscription.on('error', errorHandler);
flushTimer = setInterval(() => {
void flushOutbound();
void flushOutboundIpClasses();
}, BATCH_FLUSH_MS);
if (typeof flushTimer === 'object' && flushTimer && 'unref' in flushTimer) {
(flushTimer as {unref(): void}).unref();
@@ -514,11 +712,28 @@ export async function drainAbuseAutoBanTasksForTests(): Promise<void> {
await Promise.all([...pendingAutoBanTasks]);
}
export async function drainAbuseIpClassLookupsForTests(): Promise<void> {
await Promise.all([...pendingIpClassTasks]);
}
export function setAbuseIpClassTtlsForTests(opts: {negativeMs?: number; hintMs?: number; pendingMs?: number}): void {
if (opts.negativeMs !== undefined) ipClassNegativeTtlMs = opts.negativeMs;
if (opts.hintMs !== undefined) ipClassHintTtlMs = opts.hintMs;
if (opts.pendingMs !== undefined) ipClassPendingTtlMs = opts.pendingMs;
}
export function resetAbuseTrackingForTests(): void {
records.clear();
outboundDeltas.clear();
persistentScoreWindows.clear();
ipClassCache.clear();
ipClassPending.clear();
ipClassNegativeUntil.clear();
peerIpClassHints.clear();
outboundIpClasses.clear();
pendingIpClassTasks.clear();
pendingAutoBanTasks.clear();
ipClassPendingTtlMs = DEFAULT_IP_CLASS_PENDING_TTL_MS;
ipClassNegativeTtlMs = DEFAULT_IP_CLASS_NEGATIVE_TTL_MS;
ipClassHintTtlMs = DEFAULT_IP_CLASS_HINT_TTL_MS;
}
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import crypto from 'node:crypto';
import {lookupAsnByIp, lookupGeoipByIp} from '@pkgs/geoip/src/GeoipLookup';
import {createIpInfoService, createUnavailableIpInfoService, type IpInfoService} from '@pkgs/geoip/src/IpInfoService';
import {createMiddleware} from 'hono/factory';
import type {ApiContext} from '../ApiContext';
@@ -58,8 +59,14 @@ import {createIpInfoChecker} from '../risk/adapters/IpInfoAdapter';
import {createReverseDnsLookup} from '../risk/adapters/ReverseDnsAdapter';
import {DeterministicRiskEngine} from '../risk/DeterministicRiskEngine';
import {CassandraHistoricalOutcomeRepository} from '../risk/HistoricalOutcomeRepository';
import {createKvIpInfoLookupBudget} from '../risk/IpInfoBudget';
import {buildIpInfoCache, buildIpInfoRequestAuditLogger} from '../risk/IpInfoCacheFactory';
import {CassandraRegistrationEventsRepository} from '../risk/RegistrationEventsRepository';
import {
type IpInfoPrescreenVerdict,
ipInfoPrescreenOptionsFromEnv,
prescreenIpInfoLookup,
} from '../risk/RegistrationIpPrescreen';
import {CassandraRiskAssessmentRepository} from '../risk/RiskAssessmentRepository';
import {createRiskToolbox} from '../risk/RiskToolboxFactory';
import {CassandraSuspiciousIpRepository} from '../risk/SuspiciousIpRepository';
@@ -268,6 +275,7 @@ export function getIpInfoService(): IpInfoService {
apiKey: Config.risk.ipinfoApiKey,
cache,
auditLogger: buildIpInfoRequestAuditLogger(),
budget: createKvIpInfoLookupBudget({getKvClient: getKVClient}),
});
return _ipInfoService;
}
@@ -293,7 +301,14 @@ function getRegistrationRiskEvaluator(): IRegistrationRiskEvaluator {
return _registrationRiskEvaluator;
}
const ipInfoService = getIpInfoService();
const ipInfoChecker = Config.risk.ipinfoApiKey ? createIpInfoChecker({ipInfoService}) : undefined;
const lookupLocalCity = (ip: string) => lookupGeoipByIp(ip, Config.geoip.maxmindDbPath);
const lookupLocalAsn = (ip: string) => lookupAsnByIp(ip, Config.geoip.maxmindAsnDbPath);
const prescreenOptions = ipInfoPrescreenOptionsFromEnv();
const prescreen = async (ip: string): Promise<IpInfoPrescreenVerdict> => {
const [city, asn] = await Promise.all([lookupLocalCity(ip), lookupLocalAsn(ip)]);
return prescreenIpInfoLookup({countryIso: city.countryCode, asn: asn.asn, asnOrg: asn.asnOrg}, prescreenOptions);
};
const ipInfoChecker = Config.risk.ipinfoApiKey ? createIpInfoChecker({ipInfoService, prescreen}) : undefined;
const cacheService = getCacheService();
const reverseDnsLookup = createReverseDnsLookup({cacheService});
const toolbox = createRiskToolbox({
@@ -305,6 +320,8 @@ function getRegistrationRiskEvaluator(): IRegistrationRiskEvaluator {
historicalOutcomeRepository: getHistoricalOutcomeRepository(),
suspiciousIpRepository: getSuspiciousIpRepository(),
cacheService,
lookupLocalCity,
lookupLocalAsn,
});
const engine = new DeterministicRiskEngine(toolbox, {
logger: Logger,
@@ -1,15 +1,21 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {getSameIpDecisionKey} from '@fluxer/ip_utils/src/IpAddress';
import type {IpInfoLookupResult} from '@pkgs/geoip/src/IpInfoService';
import {afterAll, beforeAll, beforeEach, describe, expect, it} from 'vitest';
import {AdminRepository} from '../../admin/AdminRepository';
import type {ApiTestHarness} from '../../test/ApiTestHarness';
import {createApiTestHarness} from '../../test/ApiTestHarness';
import type {MockKVProvider} from '../../test/mocks/MockKVProvider';
import {
drainAbuseAutoBanTasksForTests,
drainAbuseIpClassLookupsForTests,
hashAuthToken,
recordAbuseSignal,
resetAbuseTrackingForTests,
setAbuseIpClassTtlsForTests,
startAbuseReplicationSubscriber,
stopAbuseReplicationSubscriber,
} from '../AbusiveIpAutoBanner';
import {ipBanCache} from '../IpBanMiddleware';
import {setInjectedIpInfoService} from '../ServiceMiddleware';
@@ -65,6 +71,11 @@ function ipInfoResult(ip: string, overrides: Partial<IpInfoLookupResult> = {}):
};
}
function claimCallCount(harness: ApiTestHarness, banKey: string): number {
const kvProvider = harness.kvProvider as MockKVProvider;
return kvProvider.setnxSpy.mock.calls.filter(([key]) => key === `abuse:ipclass:claim:${banKey}`).length;
}
async function waitForAssertion(assertion: () => void): Promise<void> {
const deadline = Date.now() + 1000;
let lastError: unknown;
@@ -84,6 +95,7 @@ async function waitForAssertion(assertion: () => void): Promise<void> {
describe('AbusiveIpAutoBanner', () => {
let harness: ApiTestHarness;
let adminRepository: AdminRepository;
let lookupCount = 0;
beforeAll(async () => {
harness = await createApiTestHarness();
adminRepository = new AdminRepository();
@@ -92,13 +104,18 @@ describe('AbusiveIpAutoBanner', () => {
await harness.reset();
resetAbuseTrackingForTests();
ipBanCache.resetCaches();
lookupCount = 0;
setInjectedIpInfoService({
async lookup(ip: string) {
lookupCount += 1;
return ipInfoResult(ip);
},
});
await stopAbuseReplicationSubscriber();
await startAbuseReplicationSubscriber(harness.kvProvider);
});
afterAll(async () => {
await stopAbuseReplicationSubscriber();
setInjectedIpInfoService(undefined);
await harness.shutdown();
});
@@ -117,7 +134,7 @@ describe('AbusiveIpAutoBanner', () => {
it('does not auto-ban after a single score-only spike', async () => {
const ip = '8.8.4.4';
recordAbuseSignal(ip, 'http_429', {weight: 150});
await new Promise((resolve) => setTimeout(resolve, 25));
await drainAbuseIpClassLookupsForTests();
await drainAbuseAutoBanTasksForTests();
expect(ipBanCache.isBanned(ip)).toBe(false);
await expect(adminRepository.isIpBanned(ip)).resolves.toBe(false);
@@ -135,7 +152,7 @@ describe('AbusiveIpAutoBanner', () => {
for (let i = 0; i < 10; i += 1) {
recordAbuseSignal(ip, 'auth_failure:session', {tokenHash: hashAuthToken(`mobile-invalid-${i}`)});
}
await new Promise((resolve) => setTimeout(resolve, 25));
await drainAbuseIpClassLookupsForTests();
await drainAbuseAutoBanTasksForTests();
expect(ipBanCache.isBanned(ip)).toBe(false);
await expect(adminRepository.isIpBanned(ip)).resolves.toBe(false);
@@ -153,7 +170,7 @@ describe('AbusiveIpAutoBanner', () => {
for (let i = 0; i < 100; i += 1) {
recordAbuseSignal(ip, 'auth_failure:session', {tokenHash: hashAuthToken(`mobile-threshold-${i}`)});
}
await new Promise((resolve) => setTimeout(resolve, 25));
await drainAbuseIpClassLookupsForTests();
await drainAbuseAutoBanTasksForTests();
expect(ipBanCache.isBanned(ip)).toBe(false);
await expect(adminRepository.isIpBanned(ip)).resolves.toBe(false);
@@ -168,4 +185,68 @@ describe('AbusiveIpAutoBanner', () => {
await expect(adminRepository.isIpBanned(ip)).resolves.toBe(false);
}
});
it('claims the class lookup exactly once for a burst on the same IP', async () => {
const ip = '8.8.8.8';
for (let i = 0; i < 10; i += 1) {
recordAbuseSignal(ip, 'auth_failure:session', {tokenHash: hashAuthToken(`claim-${i}`)});
}
await drainAbuseIpClassLookupsForTests();
await drainAbuseAutoBanTasksForTests();
expect(claimCallCount(harness, ip)).toBe(1);
expect(lookupCount).toBe(1);
});
it('does not pay for a lookup or ban when another pod owns the class claim', async () => {
const ip = '8.8.8.8';
await harness.kvProvider.setnx(`abuse:ipclass:claim:${ip}`, 'other-pod', 60);
for (let i = 0; i < 10; i += 1) {
recordAbuseSignal(ip, 'auth_failure:session', {tokenHash: hashAuthToken(`claim-loser-${i}`)});
}
await drainAbuseIpClassLookupsForTests();
await drainAbuseAutoBanTasksForTests();
expect(lookupCount).toBe(0);
expect(ipBanCache.isBanned(ip)).toBe(false);
});
it('does not classify an IPv4 address that is already banned', async () => {
const ip = '8.8.8.8';
ipBanCache.banTemp(ip, 3600);
for (let i = 0; i < 20; i += 1) {
recordAbuseSignal(ip, 'auth_failure:session', {tokenHash: hashAuthToken(`already-banned-${i}`)});
}
await drainAbuseIpClassLookupsForTests();
await drainAbuseAutoBanTasksForTests();
expect(lookupCount).toBe(0);
expect(claimCallCount(harness, ip)).toBe(0);
});
it('does not classify an IPv6 address inside an already banned /64', async () => {
const ip = '2606:4700:4700::1111';
const banKey = getSameIpDecisionKey(ip) ?? ip;
ipBanCache.banTemp(banKey, 3600);
for (let i = 0; i < 20; i += 1) {
recordAbuseSignal(ip, 'auth_failure:session', {tokenHash: hashAuthToken(`already-banned-v6-${i}`)});
}
await drainAbuseIpClassLookupsForTests();
await drainAbuseAutoBanTasksForTests();
expect(lookupCount).toBe(0);
expect(claimCallCount(harness, banKey)).toBe(0);
});
it('retries a failed classification once the negative TTL elapses', async () => {
const ip = '9.9.9.9';
setAbuseIpClassTtlsForTests({negativeMs: 50});
setInjectedIpInfoService({
async lookup(candidateIp: string) {
lookupCount += 1;
return ipInfoResult(candidateIp, {available: false});
},
});
recordAbuseSignal(ip, 'http_429', {weight: 25});
await drainAbuseIpClassLookupsForTests();
await drainAbuseAutoBanTasksForTests();
expect(lookupCount).toBe(1);
await new Promise((resolve) => setTimeout(resolve, 60));
await harness.kvProvider.del(`abuse:ipclass:claim:${ip}`);
recordAbuseSignal(ip, 'http_429', {weight: 25});
await drainAbuseIpClassLookupsForTests();
await drainAbuseAutoBanTasksForTests();
expect(lookupCount).toBe(2);
});
});
@@ -0,0 +1,203 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import type {IpInfoLookupResult} from '@pkgs/geoip/src/IpInfoService';
import {afterAll, beforeAll, beforeEach, describe, expect, it} from 'vitest';
import type {ApiTestHarness} from '../../test/ApiTestHarness';
import {createApiTestHarness} from '../../test/ApiTestHarness';
import type {MockKVProvider} from '../../test/mocks/MockKVProvider';
import {
drainAbuseAutoBanTasksForTests,
drainAbuseIpClassLookupsForTests,
hashAuthToken,
recordAbuseSignal,
resetAbuseTrackingForTests,
setAbuseIpClassTtlsForTests,
startAbuseReplicationSubscriber,
stopAbuseReplicationSubscriber,
} from '../AbusiveIpAutoBanner';
import {ipBanCache} from '../IpBanMiddleware';
import {setInjectedIpInfoService} from '../ServiceMiddleware';
function ipInfoResult(ip: string, overrides: Partial<IpInfoLookupResult> = {}): IpInfoLookupResult {
return {
ip,
available: true,
riskNote: 'test',
geo: {
countryCode: 'US',
countryName: 'United States',
continent: 'North America',
continentCode: 'NA',
region: null,
regionCode: null,
city: null,
postalCode: null,
timezone: null,
latitude: null,
longitude: null,
accuracyRadiusKm: null,
},
asn: {
asn: 'AS64500',
number: 64500,
name: 'Test ISP',
domain: null,
type: null,
},
mobile: {
name: null,
mcc: null,
mnc: null,
},
anonymous: {
isAnonymous: false,
providerName: null,
isVpn: false,
isProxy: false,
isResidentialProxy: false,
isTor: false,
isRelay: false,
percentDaysSeen: null,
},
flags: {
isAnycast: false,
isHosting: false,
isMobile: false,
isSatellite: false,
},
...overrides,
};
}
function publishPeerIpClasses(harness: ApiTestHarness, entries: Array<[string, string, unknown]>): void {
const kvProvider = harness.kvProvider as MockKVProvider;
kvProvider
.getSubscription()
.simulateMessage('abuse_tracker:ipclass', JSON.stringify({sender: 'other-pod', entries, ts: Date.now()}));
}
function claimCallCount(harness: ApiTestHarness, banKey: string): number {
const kvProvider = harness.kvProvider as MockKVProvider;
return kvProvider.setnxSpy.mock.calls.filter(([key]) => key === `abuse:ipclass:claim:${banKey}`).length;
}
function recordTokenSignals(ip: string, prefix: string, from: number, to: number): void {
for (let i = from; i < to; i += 1) {
recordAbuseSignal(ip, 'auth_failure:session', {tokenHash: hashAuthToken(`${prefix}-${i}`)});
}
}
async function drainAbuseWork(): Promise<void> {
await drainAbuseIpClassLookupsForTests();
await drainAbuseAutoBanTasksForTests();
}
describe('AbusiveIpAutoBanner IP class replication', () => {
let harness: ApiTestHarness;
let lookupCount = 0;
beforeAll(async () => {
harness = await createApiTestHarness();
});
beforeEach(async () => {
await harness.reset();
resetAbuseTrackingForTests();
ipBanCache.resetCaches();
lookupCount = 0;
setInjectedIpInfoService({
async lookup(ip: string) {
lookupCount += 1;
return ipInfoResult(ip);
},
});
await stopAbuseReplicationSubscriber();
await startAbuseReplicationSubscriber(harness.kvProvider);
});
afterAll(async () => {
await stopAbuseReplicationSubscriber();
setInjectedIpInfoService(undefined);
await harness.shutdown();
});
it('adopts a datacenter class from a peer without paying for its own lookup', async () => {
const ip = '8.8.8.8';
recordTokenSignals(ip, 'peer-datacenter', 0, 3);
publishPeerIpClasses(harness, [[ip, ip, 'datacenter']]);
recordTokenSignals(ip, 'peer-datacenter', 3, 10);
await drainAbuseWork();
expect(lookupCount).toBe(0);
expect(claimCallCount(harness, ip)).toBe(0);
expect(ipBanCache.isBanned(ip)).toBe(true);
});
it('never adopts a mobile class from a peer and leaves the IP bannable', async () => {
const ip = '8.8.4.4';
recordTokenSignals(ip, 'peer-mobile', 0, 3);
publishPeerIpClasses(harness, [[ip, ip, 'mobile']]);
recordTokenSignals(ip, 'peer-mobile', 3, 10);
await drainAbuseWork();
expect(lookupCount).toBe(0);
expect(ipBanCache.isBanned(ip)).toBe(false);
publishPeerIpClasses(harness, [[ip, ip, 'datacenter']]);
await drainAbuseWork();
expect(ipBanCache.isBanned(ip)).toBe(true);
});
it('ignores an unrecognised class from a peer and classifies the IP itself', async () => {
const ip = '4.4.4.4';
recordTokenSignals(ip, 'peer-invalid', 0, 3);
publishPeerIpClasses(harness, [
[ip, ip, 'datacentre'],
[ip, ip, 42],
]);
recordTokenSignals(ip, 'peer-invalid', 3, 10);
await drainAbuseWork();
expect(lookupCount).toBe(1);
expect(ipBanCache.isBanned(ip)).toBe(true);
});
it('ignores an unknown class from a peer and classifies the IP itself', async () => {
const ip = '9.9.9.9';
recordTokenSignals(ip, 'peer-unknown', 0, 3);
publishPeerIpClasses(harness, [[ip, ip, 'unknown']]);
recordTokenSignals(ip, 'peer-unknown', 3, 10);
await drainAbuseWork();
expect(lookupCount).toBe(1);
expect(ipBanCache.isBanned(ip)).toBe(true);
});
it('ignores a peer class for an IP it is not tracking', async () => {
const ip = '208.67.222.222';
publishPeerIpClasses(harness, [[ip, ip, 'datacenter']]);
recordTokenSignals(ip, 'peer-untracked', 0, 10);
await drainAbuseWork();
expect(lookupCount).toBe(1);
expect(claimCallCount(harness, ip)).toBe(1);
});
it('accepts a peer mobile class after its own lookup failed and keeps the IP unbanned', async () => {
const ip = '199.85.126.10';
setInjectedIpInfoService({
async lookup(lookupIp: string) {
lookupCount += 1;
return ipInfoResult(lookupIp, {available: false});
},
});
recordAbuseSignal(ip, 'http_429', {weight: 25});
await drainAbuseWork();
expect(lookupCount).toBe(1);
publishPeerIpClasses(harness, [[ip, ip, 'mobile']]);
recordTokenSignals(ip, 'negative-then-mobile', 0, 10);
await drainAbuseWork();
expect(lookupCount).toBe(1);
expect(ipBanCache.isBanned(ip)).toBe(false);
});
it('resumes classifying once a peer class hint expires', async () => {
const ip = '77.88.8.8';
setAbuseIpClassTtlsForTests({hintMs: 50});
recordAbuseSignal(ip, 'http_429', {weight: 3});
publishPeerIpClasses(harness, [[ip, ip, 'datacenter']]);
recordAbuseSignal(ip, 'http_429', {weight: 25});
await drainAbuseWork();
expect(lookupCount).toBe(0);
await new Promise((resolve) => setTimeout(resolve, 80));
recordAbuseSignal(ip, 'http_429', {weight: 25});
await drainAbuseWork();
expect(lookupCount).toBe(1);
expect(claimCallCount(harness, ip)).toBe(1);
expect(ipBanCache.isBanned(ip)).toBe(false);
});
});
+307
View File
@@ -13628,6 +13628,73 @@
]
}
},
"/premium/switch-to-list-price": {
"post": {
"operationId": "switch_subscription_to_list_price",
"summary": "Switch subscription to the current list price",
"tags": ["Premium"],
"responses": {
"200": {
"description": "Success",
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/SwitchToListPriceResponse"}}}
},
"400": {
"description": "Bad Request - The request was malformed or contained invalid data",
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}
},
"401": {
"description": "Unauthorized - Authentication is required or the token is invalid",
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}
},
"403": {
"description": "Forbidden - You do not have permission to perform this action",
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}
},
"429": {
"description": "Too Many Requests - You are being rate limited",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"code": {"type": "string", "enum": ["RATE_LIMITED"]},
"message": {"type": "string"},
"retry_after": {"type": "number", "description": "Seconds to wait before retrying"},
"global": {"type": "boolean", "description": "Whether this is a global rate limit"}
},
"required": ["code", "message", "retry_after"]
}
}
},
"headers": {
"Retry-After": {
"description": "Number of seconds to wait before retrying (only on 429)",
"schema": {"type": "integer"}
},
"X-RateLimit-Limit": {
"description": "The number of requests that can be made in the current window",
"schema": {"type": "integer"}
},
"X-RateLimit-Remaining": {
"description": "The number of remaining requests that can be made",
"schema": {"type": "integer"}
},
"X-RateLimit-Reset": {
"description": "Unix timestamp when the rate limit resets",
"schema": {"type": "integer"}
}
}
},
"500": {
"description": "Internal Server Error - An unexpected error occurred",
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}
}
},
"x-mint": {"metadata": {"title": "Switch subscription to the current list price"}},
"description": "Moves the authenticated user's grandfathered premium subscription down to the current list price for the same currency and billing cycle, effective at the end of the current billing period. The target price is resolved on the server and the switch is refused unless it lowers the amount charged.",
"security": [{"sessionToken": []}]
}
},
"/premium/visionary/rejoin": {
"post": {
"operationId": "rejoin_visionary_guild",
@@ -32194,6 +32261,7 @@
"pending_subscription_change": {
"anyOf": [{"$ref": "#/components/schemas/PendingSubscriptionChangeResponse"}, {"type": "null"}]
},
"list_price_switch": {"$ref": "#/components/schemas/ListPriceSwitchState"},
"subscription": {
"anyOf": [{"$ref": "#/components/schemas/PremiumBillingSubscriptionResponse"}, {"type": "null"}]
},
@@ -32209,6 +32277,7 @@
"stripe_customer_id",
"current_subscription_price",
"pending_subscription_change",
"list_price_switch",
"subscription",
"invoices",
"invoices_has_more",
@@ -32241,6 +32310,12 @@
"type": "string",
"description": "Stripe subscription schedule ID managing the pending change"
},
"change_kind": {
"enum": ["billing_cycle", "price"],
"type": "string",
"x-enumNames": ["billing_cycle", "price"],
"description": "Whether the pending change moves the billing cycle or only the price within the same cycle"
},
"current_billing_cycle": {
"anyOf": [
{"enum": ["monthly", "yearly"], "type": "string", "x-enumNames": ["monthly", "yearly"]},
@@ -32263,6 +32338,10 @@
"anyOf": [{"type": "string"}, {"type": "null"}],
"description": "Stripe price ID that will be used after the change"
},
"target_amount_minor": {
"anyOf": [{"type": "integer", "format": "int53"}, {"type": "null"}],
"description": "Unit amount of the price that will be used after the change, in the currency minor unit"
},
"currency": {
"anyOf": [
{
@@ -32289,11 +32368,13 @@
},
"required": [
"schedule_id",
"change_kind",
"current_billing_cycle",
"target_billing_cycle",
"effective_at",
"current_price_id",
"target_price_id",
"target_amount_minor",
"currency",
"initial_amount_minor",
"recurring_amount_minor",
@@ -32303,6 +32384,104 @@
{"type": "null"}
]
},
"ListPriceSwitchState": {
"type": "object",
"properties": {
"available": {
"type": "boolean",
"description": "Whether the authenticated user can move their subscription down to the current list price right now"
},
"reason": {
"anyOf": [{"$ref": "#/components/schemas/ListPriceSwitchIneligibilityReason"}, {"type": "null"}],
"description": "Why the switch is unavailable, when available is false"
},
"pending": {
"type": "boolean",
"description": "Whether a switch to the current list price is already scheduled"
},
"current_price_id": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"description": "Stripe price ID the subscription is billed against today"
},
"current_amount_minor": {
"anyOf": [{"type": "integer", "format": "int53"}, {"type": "null"}],
"description": "Amount the subscription is billed today, in the currency minor unit"
},
"list_price_id": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"description": "Current list Stripe price ID for the same cycle and currency"
},
"list_amount_minor": {
"anyOf": [{"type": "integer", "format": "int53"}, {"type": "null"}],
"description": "Current list price for the same cycle and currency, in the currency minor unit"
},
"currency": {
"anyOf": [
{
"enum": ["USD", "EUR", "BRL", "INR", "PLN", "TRY"],
"type": "string",
"x-enumNames": ["USD", "EUR", "BRL", "INR", "PLN", "TRY"]
},
{"type": "null"}
],
"description": "Currency of both the current and the list amount"
},
"billing_cycle": {
"anyOf": [
{"enum": ["monthly", "yearly"], "type": "string", "x-enumNames": ["monthly", "yearly"]},
{"type": "null"}
],
"description": "Recurring billing cycle the switch applies to"
},
"effective_at": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"description": "ISO timestamp the switch takes effect, which is the end of the current billing period"
}
},
"required": [
"available",
"reason",
"pending",
"current_price_id",
"current_amount_minor",
"list_price_id",
"list_amount_minor",
"currency",
"billing_cycle",
"effective_at"
]
},
"ListPriceSwitchIneligibilityReason": {
"enum": [
"feature_unavailable",
"no_active_subscription",
"subscription_not_chargeable",
"unsupported_subscription",
"no_list_price",
"already_on_list_price",
"not_a_price_decrease",
"subscription_cancelling",
"cancellation_managed_by_schedule",
"conflicting_pending_change",
"missing_period_end",
"switch_in_progress"
],
"type": "string",
"x-enumNames": [
"feature_unavailable",
"no_active_subscription",
"subscription_not_chargeable",
"unsupported_subscription",
"no_list_price",
"already_on_list_price",
"not_a_price_decrease",
"subscription_cancelling",
"cancellation_managed_by_schedule",
"conflicting_pending_change",
"missing_period_end",
"switch_in_progress"
]
},
"PremiumBillingSubscriptionResponse": {
"type": "object",
"properties": {
@@ -32544,6 +32723,134 @@
"status"
]
},
"SwitchToListPriceResponse": {
"oneOf": [
{"$ref": "#/components/schemas/ScheduledSwitchToListPriceResponse"},
{"$ref": "#/components/schemas/AlreadyScheduledSwitchToListPriceResponse"},
{"$ref": "#/components/schemas/IneligibleSwitchToListPriceResponse"}
]
},
"ScheduledSwitchToListPriceResponse": {
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["scheduled"],
"description": "The switch was scheduled for the end of the current billing period"
},
"effective_at": {"type": "string", "description": "ISO timestamp the switch takes effect"},
"target_price_id": {
"type": "string",
"description": "Stripe price ID the subscription will be billed against after the switch"
},
"target_amount_minor": {
"type": "integer",
"format": "int53",
"description": "Amount billed after the switch, in the currency minor unit"
},
"current_amount_minor": {
"type": "integer",
"format": "int53",
"description": "Amount billed before the switch, in the currency minor unit"
},
"currency": {
"enum": ["USD", "EUR", "BRL", "INR", "PLN", "TRY"],
"type": "string",
"x-enumNames": ["USD", "EUR", "BRL", "INR", "PLN", "TRY"],
"description": "Currency of both amounts"
}
},
"required": [
"status",
"effective_at",
"target_price_id",
"target_amount_minor",
"current_amount_minor",
"currency"
]
},
"AlreadyScheduledSwitchToListPriceResponse": {
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["already_scheduled"],
"description": "The switch was already scheduled by an earlier request"
},
"effective_at": {"type": "string", "description": "ISO timestamp the switch takes effect"},
"target_price_id": {
"type": "string",
"description": "Stripe price ID the subscription will be billed against after the switch"
},
"target_amount_minor": {
"type": "integer",
"format": "int53",
"description": "Amount billed after the switch, in the currency minor unit"
},
"current_amount_minor": {
"type": "integer",
"format": "int53",
"description": "Amount billed before the switch, in the currency minor unit"
},
"currency": {
"enum": ["USD", "EUR", "BRL", "INR", "PLN", "TRY"],
"type": "string",
"x-enumNames": ["USD", "EUR", "BRL", "INR", "PLN", "TRY"],
"description": "Currency of both amounts"
}
},
"required": [
"status",
"effective_at",
"target_price_id",
"target_amount_minor",
"current_amount_minor",
"currency"
]
},
"IneligibleSwitchToListPriceResponse": {
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["ineligible"],
"description": "The subscription cannot be moved to the current list price"
},
"reason": {
"enum": [
"feature_unavailable",
"no_active_subscription",
"subscription_not_chargeable",
"unsupported_subscription",
"no_list_price",
"already_on_list_price",
"not_a_price_decrease",
"subscription_cancelling",
"cancellation_managed_by_schedule",
"conflicting_pending_change",
"missing_period_end",
"switch_in_progress"
],
"type": "string",
"x-enumNames": [
"feature_unavailable",
"no_active_subscription",
"subscription_not_chargeable",
"unsupported_subscription",
"no_list_price",
"already_on_list_price",
"not_a_price_decrease",
"subscription_cancelling",
"cancellation_managed_by_schedule",
"conflicting_pending_change",
"missing_period_end",
"switch_in_progress"
],
"description": "Why the switch was refused"
}
},
"required": ["status", "reason"]
},
"ReadStateAckResponse": {
"type": "object",
"properties": {
+1 -1
View File
@@ -343,7 +343,7 @@ export class ReportService {
const normalizedEmail = this.normalizeEmail(email);
const hasValidDns = await this.emailDnsValidationService.hasValidDnsRecords(normalizedEmail);
if (!hasValidDns) {
throw InputValidationError.fromCode('email', ValidationErrorCodes.INVALID_EMAIL_ADDRESS);
throw InputValidationError.fromCode('email', ValidationErrorCodes.EMAIL_DOMAIN_CANNOT_RECEIVE_MAIL);
}
const verificationCode = this.generateDsaVerificationCode();
const expiresAt = new Date(Date.now() + ms('10 minutes'));
+16 -8
View File
@@ -7,9 +7,14 @@ import {isTrustedCommercialPrivacyProvider} from './TrustedPrivacyProviders';
const VERDICT_CACHE_TTL_MS = 60 * 60 * 1000;
interface IpBanBlastRadiusVerdict {
cgnat: boolean;
sharedAccess: boolean;
}
interface CachedVerdict {
expiresAtMs: number;
highRisk: boolean;
verdict: IpBanBlastRadiusVerdict;
}
const verdictCache = new Map<string, CachedVerdict>();
@@ -48,7 +53,7 @@ export function getSuspiciousIpSkipReason(result: IpInfoLookupResult): Suspiciou
return null;
}
function isHighSharedAccessBlastRadiusRisk(result: IpInfoLookupResult): boolean {
export function isHighSharedAccessBlastRadiusRisk(result: IpInfoLookupResult): boolean {
if (result.flags.isHosting || isAnonymousAccess(result)) {
return false;
}
@@ -60,29 +65,32 @@ export function isSingleIpBanCandidate(value: string): boolean {
return parseIpBanEntry(value)?.type === 'single';
}
export async function hasHighCgnatBlastRadiusRisk(
export async function getIpBanBlastRadiusVerdict(
ip: string,
ipInfoService: IpInfoService,
context: {
source: string;
reason: string;
},
): Promise<boolean> {
): Promise<IpBanBlastRadiusVerdict> {
const now = Date.now();
const cacheKey = getSameIpDecisionKey(ip) ?? ip;
const cached = verdictCache.get(cacheKey);
if (cached && cached.expiresAtMs > now) {
return cached.highRisk;
return cached.verdict;
}
const result = await ipInfoService.lookup(ip, {
source: context.source,
reason: context.reason,
metadata: {policy: 'ip_ban_cgnat_guard'},
});
const highRisk = isHighCgnatBlastRadiusRisk(result);
const verdict: IpBanBlastRadiusVerdict = {
cgnat: isHighCgnatBlastRadiusRisk(result),
sharedAccess: isHighSharedAccessBlastRadiusRisk(result),
};
verdictCache.set(cacheKey, {
highRisk,
verdict,
expiresAtMs: now + VERDICT_CACHE_TTL_MS,
});
return highRisk;
return verdict;
}
+105
View File
@@ -0,0 +1,105 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import type {IpInfoLookupBudget, IpInfoLookupPriority} from '@pkgs/geoip/src/IpInfoService';
import type {IKVProvider} from '@pkgs/kv_client/src/IKVProvider';
import {Logger} from '../Logger';
const BURST_KEY_PREFIX = 'ipinfo:budget:burst:';
const MONTH_KEY_PREFIX = 'ipinfo:budget:month:';
const MONTH_KEY_TTL_SECONDS = 40 * 24 * 3600;
const BURST_REFILL_INTERVAL_MS = 60_000;
const BUDGET_LOG_INTERVAL_MS = 60_000;
let lastBudgetErrorLogMs = 0;
function positiveNumberFromEnv(name: string, fallback: number): number {
const raw = process.env[name];
if (!raw) return fallback;
const parsed = Number(raw);
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
}
function budgetEnabled(): boolean {
return process.env.FLUXER_IPINFO_BUDGET_ENABLED !== '0';
}
function burstConfigFor(priority: IpInfoLookupPriority): {maxTokens: number; refillPerMin: number} {
if (priority === 'critical') {
return {
maxTokens: positiveNumberFromEnv('FLUXER_IPINFO_BUDGET_CRITICAL_BURST', 60),
refillPerMin: positiveNumberFromEnv('FLUXER_IPINFO_BUDGET_CRITICAL_REFILL_PER_MIN', 60),
};
}
if (priority === 'background') {
return {
maxTokens: positiveNumberFromEnv('FLUXER_IPINFO_BUDGET_BACKGROUND_BURST', 120),
refillPerMin: positiveNumberFromEnv('FLUXER_IPINFO_BUDGET_BACKGROUND_REFILL_PER_MIN', 30),
};
}
return {
maxTokens: positiveNumberFromEnv('FLUXER_IPINFO_BUDGET_STANDARD_BURST', 240),
refillPerMin: positiveNumberFromEnv('FLUXER_IPINFO_BUDGET_STANDARD_REFILL_PER_MIN', 120),
};
}
function monthlyCeilingFor(priority: IpInfoLookupPriority): number {
const monthlyMax = positiveNumberFromEnv('FLUXER_IPINFO_BUDGET_MONTHLY_MAX', 140000);
if (priority === 'critical') return monthlyMax;
const percent =
priority === 'background'
? positiveNumberFromEnv('FLUXER_IPINFO_BUDGET_BACKGROUND_MONTHLY_PCT', 60)
: positiveNumberFromEnv('FLUXER_IPINFO_BUDGET_STANDARD_MONTHLY_PCT', 90);
return Math.floor((monthlyMax * Math.min(percent, 100)) / 100);
}
function currentMonthKey(): string {
const now = new Date();
const month = String(now.getUTCMonth() + 1).padStart(2, '0');
return `${MONTH_KEY_PREFIX}${now.getUTCFullYear()}-${month}`;
}
function logThrottled(payload: Record<string, unknown>, message: string): void {
const now = Date.now();
if (now - lastBudgetErrorLogMs < BUDGET_LOG_INTERVAL_MS) return;
lastBudgetErrorLogMs = now;
Logger.warn(payload, message);
}
export function createKvIpInfoLookupBudget(opts: {getKvClient: () => IKVProvider}): IpInfoLookupBudget {
return {
async tryConsume(priority: IpInfoLookupPriority): Promise<boolean> {
if (!budgetEnabled()) {
return true;
}
try {
const kv = opts.getKvClient();
const burst = burstConfigFor(priority);
const consumed = await kv.tryConsumeTokens(
`${BURST_KEY_PREFIX}${priority}`,
1,
burst.maxTokens,
burst.refillPerMin,
BURST_REFILL_INTERVAL_MS,
);
if (consumed < 1) {
logThrottled({priority, reason: 'burst'}, 'IPInfo lookup budget shed');
return false;
}
const monthKey = currentMonthKey();
const used = Number((await kv.get(monthKey)) ?? '0');
if (used >= monthlyCeilingFor(priority)) {
logThrottled({priority, reason: 'monthly', used}, 'IPInfo lookup budget shed');
return false;
}
const value = await kv.incr(monthKey);
if (value === 1) {
await kv.expire(monthKey, MONTH_KEY_TTL_SECONDS);
}
return true;
} catch (error) {
logThrottled({error, priority}, 'IPInfo lookup budget check failed, admitting lookup');
return true;
}
},
};
}
@@ -3,7 +3,7 @@
import {getDefaultCassandraClient} from '@pkgs/cassandra/src/Client';
import {createCassandraIpInfoCache} from '@pkgs/geoip/src/CassandraIpInfoCache';
import {createCassandraIpInfoRequestAuditLogger} from '@pkgs/geoip/src/CassandraIpInfoRequestAudit';
import type {IpInfoCache, IpInfoRequestAuditLogger} from '@pkgs/geoip/src/IpInfoService';
import {type IpInfoCache, type IpInfoRequestAuditLogger, isCachedIpInfoFailure} from '@pkgs/geoip/src/IpInfoService';
import {createPostgresIpInfoCache, createPostgresIpInfoRequestAuditLogger} from '@pkgs/geoip/src/PostgresIpInfoKv';
import {createTieredIpInfoCache} from '@pkgs/geoip/src/TieredIpInfoCache';
import {getDefaultPostgresClient} from '@pkgs/postgres/src/Client';
@@ -22,11 +22,13 @@ export function buildIpInfoCache(options: BuildIpInfoCacheOptions): IpInfoCache
getClient: getDefaultPostgresClient,
onError: (error, operation) => Logger.warn({error, operation}, 'Postgres IPInfo cache operation failed'),
}),
skipColdWrite: isCachedIpInfoFailure,
});
}
return createTieredIpInfoCache({
hot: options.hot,
cold: createCassandraIpInfoCache({getClient: getDefaultCassandraClient}),
skipColdWrite: isCachedIpInfoFailure,
});
}
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {
classifyAccountPolicyReverseDnsHostname,
isAccountPolicyEducationOrganizationName,
isAccountPolicyTrustedCommercialPrivacyProvider,
} from './AccountPolicyService';
const ASN_ENTRY_REGEX = /^\d+$/u;
export interface LocalIpIntel {
countryIso: string | null;
asn: number | null;
asnOrg: string | null;
}
export interface IpInfoPrescreenOptions {
enabled: boolean;
allowedAsns: ReadonlySet<number>;
}
export type IpInfoPrescreenVerdict = 'consult' | 'skip';
export function prescreenIpInfoLookup(local: LocalIpIntel, opts: IpInfoPrescreenOptions): IpInfoPrescreenVerdict {
if (!opts.enabled) return 'consult';
if (opts.allowedAsns.size === 0) return 'consult';
if (local.countryIso === null) return 'consult';
if (local.asn === null) return 'consult';
if (!opts.allowedAsns.has(local.asn)) return 'consult';
if (isAccountPolicyTrustedCommercialPrivacyProvider(local.asnOrg)) return 'consult';
if (isAccountPolicyEducationOrganizationName(local.asnOrg)) return 'consult';
if (classifyAccountPolicyReverseDnsHostname(local.asnOrg) === 'cellular') return 'consult';
return 'skip';
}
export function ipInfoPrescreenOptionsFromEnv(): IpInfoPrescreenOptions {
return {
enabled: booleanFromEnv(process.env.FLUXER_RISK_IPINFO_PRESCREEN_ENABLED),
allowedAsns: asnSetFromEnv(process.env.FLUXER_RISK_IPINFO_PRESCREEN_ALLOW_ASNS),
};
}
function booleanFromEnv(rawValue: string | undefined): boolean {
if (!rawValue) return false;
const normalized = rawValue.trim().toLowerCase();
return normalized === '1' || normalized === 'true';
}
function asnSetFromEnv(rawValue: string | undefined): ReadonlySet<number> {
const allowedAsns = new Set<number>();
if (!rawValue) return allowedAsns;
for (const entry of rawValue.split(',')) {
const trimmed = entry.trim();
if (!ASN_ENTRY_REGEX.test(trimmed)) continue;
const asn = Number.parseInt(trimmed, 10);
if (!Number.isSafeInteger(asn)) continue;
allowedAsns.add(asn);
}
return allowedAsns;
}
+13 -21
View File
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import type {ICacheService} from '@pkgs/cache/src/ICacheService';
import type {GeoipAsnResult, GeoipResult} from '@pkgs/geoip/src/GeoipLookup';
import type {IpInfoService} from '@pkgs/geoip/src/IpInfoService';
import type {IAdminRepository} from '../admin/IAdminRepository';
import {createDisposableDomainChecker} from './adapters/DisposableDomainChecker';
@@ -9,6 +10,7 @@ import {createDomainAgeChecker} from './adapters/DomainAgeChecker';
import {analyzeEmailSyntax} from './adapters/EmailSyntaxAnalyzer';
import {createGeoIpAsnAdapter, createGeoIpCityAdapter} from './adapters/GeoIpAdapters';
import {createHistoricalOutcomeAdapter} from './adapters/HistoricalOutcomeAdapter';
import {unavailableIpInfoAnonymousResult} from './adapters/IpInfoAdapter';
import {checkGeoVsLocale} from './adapters/LocaleGeoMatcher';
import {analyzeRegistrationTiming} from './adapters/RegistrationTimingAnalyzer';
import {analyzeUserAgent} from './adapters/UserAgentAnalyzer';
@@ -29,12 +31,20 @@ interface RiskToolboxFactoryOptions {
mxResolver?: MxResolver;
mxCacheTtlMs?: number;
cacheService?: ICacheService;
lookupLocalCity?: (ip: string) => Promise<GeoipResult>;
lookupLocalAsn?: (ip: string) => Promise<GeoipAsnResult>;
}
export function createRiskToolbox(opts: RiskToolboxFactoryOptions): RiskToolbox {
const checkDomainDisposable = createDisposableDomainChecker({adminRepository: opts.adminRepository});
const lookupGeoIpCity = createGeoIpCityAdapter({ipInfoService: opts.ipInfoService});
const lookupGeoIpAsn = createGeoIpAsnAdapter({ipInfoService: opts.ipInfoService});
const lookupGeoIpCity = createGeoIpCityAdapter({
ipInfoService: opts.ipInfoService,
lookupLocalCity: opts.lookupLocalCity,
});
const lookupGeoIpAsn = createGeoIpAsnAdapter({
ipInfoService: opts.ipInfoService,
lookupLocalAsn: opts.lookupLocalAsn,
});
const checkMx = createDnsMxChecker({
resolver: opts.mxResolver ?? new NodeDnsMxResolver(),
cacheTtlMs: opts.mxCacheTtlMs,
@@ -46,25 +56,7 @@ export function createRiskToolbox(opts: RiskToolboxFactoryOptions): RiskToolbox
});
const lookupIpInfo = opts.ipInfoChecker
? async (args: {ip: string}) => opts.ipInfoChecker!(args.ip)
: async (args: {ip: string}) =>
({
ip: args.ip,
available: false,
isAnonymous: false,
providerName: null,
isVpn: false,
isProxy: false,
isResidentialProxy: false,
isTor: false,
isRelay: false,
isHosting: false,
isMobile: false,
asnType: null,
asnOrg: null,
connectionType: 'unknown',
percentDaysSeen: null,
riskNote: 'IPInfo not configured (no API key)',
}) as IpInfoAnonymousResult;
: async (args: {ip: string}) => unavailableIpInfoAnonymousResult(args.ip, 'IPInfo not configured (no API key)');
const lookupReverseDns = opts.reverseDnsLookup
? async (args: {ip: string}) => opts.reverseDnsLookup!(args.ip)
: async (args: {ip: string}) => ({
@@ -0,0 +1,289 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import type {GeoipAsnResult, GeoipResult} from '@pkgs/geoip/src/GeoipLookup';
import type {IpInfoLookupResult, IpInfoService} from '@pkgs/geoip/src/IpInfoService';
import {describe, expect, it} from 'vitest';
import {createGeoIpAsnAdapter, createGeoIpCityAdapter} from '../adapters/GeoIpAdapters';
function throwingIpInfoService(): IpInfoService {
return {
lookup: async () => {
throw new Error('ipinfo must not be consulted');
},
};
}
function countingIpInfoService(result: IpInfoLookupResult): {service: IpInfoService; calls: () => number} {
let calls = 0;
return {
service: {
lookup: async () => {
calls += 1;
return result;
},
},
calls: () => calls,
};
}
function ipInfoResult(overrides: Partial<IpInfoLookupResult> = {}): IpInfoLookupResult {
return {
ip: '198.51.100.1',
available: true,
riskNote: 'test',
geo: {
countryCode: 'US',
countryName: 'United States',
continent: 'North America',
continentCode: 'NA',
region: 'California',
regionCode: 'CA',
city: 'San Jose',
postalCode: null,
timezone: 'America/Los_Angeles',
latitude: 37.3,
longitude: -121.9,
accuracyRadiusKm: 20,
},
asn: {
asn: 'AS64500',
number: 64500,
name: 'Test ISP',
domain: null,
type: null,
},
mobile: {
name: null,
mcc: null,
mnc: null,
},
anonymous: {
isAnonymous: false,
providerName: null,
isVpn: false,
isProxy: false,
isResidentialProxy: false,
isTor: false,
isRelay: false,
percentDaysSeen: null,
},
flags: {
isAnycast: false,
isHosting: false,
isMobile: false,
isSatellite: false,
},
...overrides,
};
}
function geoipResult(overrides: Partial<GeoipResult> = {}): GeoipResult {
return {
countryCode: 'SE',
normalizedIp: '198.51.100.7',
city: 'Stockholm',
region: 'Stockholm County',
regionCode: 'AB',
countryName: 'Sweden',
latitude: 59.33,
longitude: 18.06,
accuracyRadiusKm: 5,
timeZone: 'Europe/Stockholm',
...overrides,
};
}
function geoipAsnResult(overrides: Partial<GeoipAsnResult> = {}): GeoipAsnResult {
return {
normalizedIp: '198.51.100.7',
asn: 64510,
asnOrg: 'Example Broadband ISP',
available: true,
...overrides,
};
}
describe('createGeoIpCityAdapter', () => {
it('answers from the local city database without touching IPInfo', async () => {
const lookupGeoIpCity = createGeoIpCityAdapter({
ipInfoService: throwingIpInfoService(),
lookupLocalCity: async () => geoipResult(),
});
await expect(lookupGeoIpCity({ip: '198.51.100.7'})).resolves.toEqual({
ip: '198.51.100.7',
available: true,
found: true,
countryIso: 'SE',
country: 'Sweden',
region: 'Stockholm County',
city: 'Stockholm',
latitude: 59.33,
longitude: 18.06,
accuracyRadiusKm: 5,
timeZone: 'Europe/Stockholm',
});
});
it('normalizes the IP before handing it to the local city database', async () => {
const seen: Array<string> = [];
const lookupGeoIpCity = createGeoIpCityAdapter({
ipInfoService: throwingIpInfoService(),
lookupLocalCity: async (ip) => {
seen.push(ip);
return geoipResult();
},
});
await lookupGeoIpCity({ip: '[2001:0db8:0000::0001]'});
expect(seen).toEqual(['2001:db8::1']);
});
it('coalesces missing optional local fields to null', async () => {
const lookupGeoIpCity = createGeoIpCityAdapter({
ipInfoService: throwingIpInfoService(),
lookupLocalCity: async () => ({
countryCode: 'SE',
normalizedIp: '198.51.100.7',
city: null,
region: null,
countryName: null,
}),
});
await expect(lookupGeoIpCity({ip: '198.51.100.7'})).resolves.toEqual({
ip: '198.51.100.7',
available: true,
found: true,
countryIso: 'SE',
country: null,
region: null,
city: null,
latitude: null,
longitude: null,
accuracyRadiusKm: null,
timeZone: null,
});
});
it('falls back to IPInfo when the local city database has no country', async () => {
const counting = countingIpInfoService(ipInfoResult());
const lookupGeoIpCity = createGeoIpCityAdapter({
ipInfoService: counting.service,
lookupLocalCity: async () => geoipResult({countryCode: null}),
});
await expect(lookupGeoIpCity({ip: '198.51.100.1'})).resolves.toEqual({
ip: '198.51.100.1',
available: true,
found: true,
countryIso: 'US',
country: 'United States',
region: 'California',
city: 'San Jose',
latitude: 37.3,
longitude: -121.9,
accuracyRadiusKm: 20,
timeZone: 'America/Los_Angeles',
});
expect(counting.calls()).toBe(1);
});
it('falls back to IPInfo when no local city lookup is wired', async () => {
const counting = countingIpInfoService(ipInfoResult());
const lookupGeoIpCity = createGeoIpCityAdapter({ipInfoService: counting.service});
const result = await lookupGeoIpCity({ip: '198.51.100.1'});
expect(result.countryIso).toBe('US');
expect(counting.calls()).toBe(1);
});
it('returns an available not-found result for an unparseable IP without any lookup', async () => {
const lookupGeoIpCity = createGeoIpCityAdapter({
ipInfoService: throwingIpInfoService(),
lookupLocalCity: async () => {
throw new Error('local city must not be consulted');
},
});
await expect(lookupGeoIpCity({ip: 'not-an-ip'})).resolves.toEqual({
ip: 'not-an-ip',
available: true,
found: false,
countryIso: null,
country: null,
region: null,
city: null,
latitude: null,
longitude: null,
accuracyRadiusKm: null,
timeZone: null,
});
});
});
describe('createGeoIpAsnAdapter', () => {
it('answers from the local ASN database without touching IPInfo', async () => {
const lookupGeoIpAsn = createGeoIpAsnAdapter({
ipInfoService: throwingIpInfoService(),
lookupLocalAsn: async () => geoipAsnResult(),
});
await expect(lookupGeoIpAsn({ip: '198.51.100.7'})).resolves.toEqual({
ip: '198.51.100.7',
available: true,
found: true,
asn: 64510,
asnOrg: 'Example Broadband ISP',
});
});
it('normalizes the IP before handing it to the local ASN database', async () => {
const seen: Array<string> = [];
const lookupGeoIpAsn = createGeoIpAsnAdapter({
ipInfoService: throwingIpInfoService(),
lookupLocalAsn: async (ip) => {
seen.push(ip);
return geoipAsnResult();
},
});
await lookupGeoIpAsn({ip: '[2001:0db8:0000::0001]'});
expect(seen).toEqual(['2001:db8::1']);
});
it('falls back to IPInfo when the local ASN database has no ASN', async () => {
const counting = countingIpInfoService(ipInfoResult());
const lookupGeoIpAsn = createGeoIpAsnAdapter({
ipInfoService: counting.service,
lookupLocalAsn: async () => geoipAsnResult({asn: null, asnOrg: null}),
});
await expect(lookupGeoIpAsn({ip: '198.51.100.1'})).resolves.toEqual({
ip: '198.51.100.1',
available: true,
found: true,
asn: 64500,
asnOrg: 'Test ISP',
});
expect(counting.calls()).toBe(1);
});
it('falls back to IPInfo when the local ASN database is unavailable', async () => {
const counting = countingIpInfoService(ipInfoResult());
const lookupGeoIpAsn = createGeoIpAsnAdapter({
ipInfoService: counting.service,
lookupLocalAsn: async () => geoipAsnResult({normalizedIp: null, asn: null, asnOrg: null, available: false}),
});
const result = await lookupGeoIpAsn({ip: '198.51.100.1'});
expect(result.asn).toBe(64500);
expect(counting.calls()).toBe(1);
});
it('returns an available not-found result for an unparseable IP without any lookup', async () => {
const lookupGeoIpAsn = createGeoIpAsnAdapter({
ipInfoService: throwingIpInfoService(),
lookupLocalAsn: async () => {
throw new Error('local ASN must not be consulted');
},
});
await expect(lookupGeoIpAsn({ip: 'not-an-ip'})).resolves.toEqual({
ip: 'not-an-ip',
available: true,
found: false,
asn: null,
asnOrg: null,
});
});
});
@@ -2,7 +2,11 @@
import type {IpInfoLookupResult} from '@pkgs/geoip/src/IpInfoService';
import {describe, expect, it} from 'vitest';
import {isHighCgnatBlastRadiusRisk, isSingleIpBanCandidate} from '../IpBanCgnatGuard';
import {
isHighCgnatBlastRadiusRisk,
isHighSharedAccessBlastRadiusRisk,
isSingleIpBanCandidate,
} from '../IpBanCgnatGuard';
function ipInfoResult(overrides: Partial<IpInfoLookupResult> = {}): IpInfoLookupResult {
return {
@@ -96,4 +100,63 @@ describe('IpBanCgnatGuard', () => {
),
).toBe(false);
});
it('flags satellite, anycast and education networks as high blast-radius risk', () => {
expect(
isHighSharedAccessBlastRadiusRisk(
ipInfoResult({
flags: {isAnycast: false, isHosting: false, isMobile: false, isSatellite: true},
}),
),
).toBe(true);
expect(
isHighSharedAccessBlastRadiusRisk(
ipInfoResult({
flags: {isAnycast: true, isHosting: false, isMobile: false, isSatellite: false},
}),
),
).toBe(true);
expect(
isHighSharedAccessBlastRadiusRisk(
ipInfoResult({asn: {asn: 'AS64500', number: 64500, name: 'Test University', domain: null, type: 'education'}}),
),
).toBe(true);
});
it('does not flag ordinary residential networks as shared-access risk', () => {
expect(isHighSharedAccessBlastRadiusRisk(ipInfoResult())).toBe(false);
});
it('does not treat shared-access networks as CGNAT risk', () => {
expect(
isHighCgnatBlastRadiusRisk(
ipInfoResult({
flags: {isAnycast: false, isHosting: false, isMobile: false, isSatellite: true},
}),
),
).toBe(false);
});
it('does not exempt hosting or anonymous shared-access infrastructure', () => {
expect(
isHighSharedAccessBlastRadiusRisk(
ipInfoResult({
flags: {isAnycast: true, isHosting: true, isMobile: false, isSatellite: false},
}),
),
).toBe(false);
expect(
isHighSharedAccessBlastRadiusRisk(
ipInfoResult({
anonymous: {
isAnonymous: true,
providerName: 'Example VPN',
isVpn: true,
isProxy: false,
isResidentialProxy: false,
isTor: false,
isRelay: false,
percentDaysSeen: null,
},
flags: {isAnycast: false, isHosting: false, isMobile: false, isSatellite: true},
}),
),
).toBe(false);
});
});
@@ -0,0 +1,156 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import type {IpInfoLookupContext, IpInfoLookupResult, IpInfoService} from '@pkgs/geoip/src/IpInfoService';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {createCurrentBehaviorTestAccountPolicyEvaluator} from '../../test/AccountPolicyTestEvaluator';
import {setInjectedAccountPolicyEvaluator} from '../AccountPolicyService';
import {createIpInfoChecker, unavailableIpInfoAnonymousResult} from '../adapters/IpInfoAdapter';
function ipInfoResult(overrides: Partial<IpInfoLookupResult> = {}): IpInfoLookupResult {
return {
ip: '198.51.100.1',
available: true,
riskNote: 'live lookup',
geo: {
countryCode: 'US',
countryName: 'United States',
continent: 'North America',
continentCode: 'NA',
region: null,
regionCode: null,
city: null,
postalCode: null,
timezone: null,
latitude: null,
longitude: null,
accuracyRadiusKm: null,
},
asn: {
asn: 'AS64500',
number: 64500,
name: 'Test ISP',
domain: null,
type: 'isp',
},
mobile: {
name: null,
mcc: null,
mnc: null,
},
anonymous: {
isAnonymous: true,
providerName: 'Example VPN',
isVpn: true,
isProxy: false,
isResidentialProxy: false,
isTor: false,
isRelay: false,
percentDaysSeen: 42,
},
flags: {
isAnycast: false,
isHosting: false,
isMobile: false,
isSatellite: false,
},
...overrides,
};
}
function countingIpInfoService(result: IpInfoLookupResult): {
service: IpInfoService;
calls: () => number;
contexts: () => Array<IpInfoLookupContext | undefined>;
} {
const contexts: Array<IpInfoLookupContext | undefined> = [];
return {
service: {
lookup: async (_ip, context) => {
contexts.push(context);
return result;
},
},
calls: () => contexts.length,
contexts: () => contexts,
};
}
beforeEach(() => {
setInjectedAccountPolicyEvaluator(createCurrentBehaviorTestAccountPolicyEvaluator());
});
afterEach(() => {
setInjectedAccountPolicyEvaluator(undefined);
});
describe('createIpInfoChecker pre-screen', () => {
it('returns an unavailable result with zero lookups when the pre-screen skips', async () => {
const counting = countingIpInfoService(ipInfoResult());
const checkIpInfo = createIpInfoChecker({
ipInfoService: counting.service,
prescreen: async () => 'skip',
});
await expect(checkIpInfo('198.51.100.1')).resolves.toEqual(
unavailableIpInfoAnonymousResult('198.51.100.1', 'IPInfo skipped (local pre-screen)'),
);
expect(counting.calls()).toBe(0);
});
it('calls IPInfo exactly once and preserves the mapping when the pre-screen consults', async () => {
const counting = countingIpInfoService(ipInfoResult());
const checkIpInfo = createIpInfoChecker({
ipInfoService: counting.service,
prescreen: async () => 'consult',
});
await expect(checkIpInfo('198.51.100.1')).resolves.toEqual({
ip: '198.51.100.1',
available: true,
isAnonymous: true,
providerName: 'Example VPN',
isVpn: true,
isProxy: false,
isResidentialProxy: false,
isTor: false,
isRelay: false,
isHosting: false,
isMobile: false,
asnType: 'isp',
asnOrg: 'Test ISP',
connectionType: 'residential',
percentDaysSeen: 42,
riskNote: 'live lookup',
});
expect(counting.calls()).toBe(1);
expect(counting.contexts()).toEqual([{source: 'risk.ipinfo_checker', reason: 'registration_risk'}]);
});
it('is identical to a consulting pre-screen when no pre-screen is wired', async () => {
const withoutPrescreen = countingIpInfoService(ipInfoResult());
const withPrescreen = countingIpInfoService(ipInfoResult());
const baseline = await createIpInfoChecker({ipInfoService: withoutPrescreen.service})('198.51.100.1');
const consulted = await createIpInfoChecker({
ipInfoService: withPrescreen.service,
prescreen: async () => 'consult',
})('198.51.100.1');
expect(baseline).toEqual(consulted);
expect(withoutPrescreen.calls()).toBe(1);
expect(withPrescreen.calls()).toBe(1);
});
it('never synthesizes a clean attestation for a skipped lookup', () => {
const skipped = unavailableIpInfoAnonymousResult('198.51.100.1', 'IPInfo skipped (local pre-screen)');
expect(skipped.available).toBe(false);
expect(skipped.isAnonymous).toBe(false);
expect(skipped.isVpn).toBe(false);
expect(skipped.isProxy).toBe(false);
expect(skipped.isResidentialProxy).toBe(false);
expect(skipped.isTor).toBe(false);
expect(skipped.isRelay).toBe(false);
expect(skipped.isHosting).toBe(false);
expect(skipped.isMobile).toBe(false);
expect(skipped.asnType).toBeNull();
expect(skipped.asnOrg).toBeNull();
expect(skipped.connectionType).toBe('unknown');
expect(skipped.percentDaysSeen).toBeNull();
});
});
@@ -0,0 +1,134 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {MockKVProvider} from '../../test/mocks/MockKVProvider';
import {createKvIpInfoLookupBudget} from '../IpInfoBudget';
const BUDGET_ENV_KEYS = [
'FLUXER_IPINFO_BUDGET_ENABLED',
'FLUXER_IPINFO_BUDGET_MONTHLY_MAX',
'FLUXER_IPINFO_BUDGET_BACKGROUND_MONTHLY_PCT',
'FLUXER_IPINFO_BUDGET_STANDARD_MONTHLY_PCT',
'FLUXER_IPINFO_BUDGET_CRITICAL_BURST',
'FLUXER_IPINFO_BUDGET_STANDARD_BURST',
'FLUXER_IPINFO_BUDGET_BACKGROUND_BURST',
'FLUXER_IPINFO_BUDGET_CRITICAL_REFILL_PER_MIN',
'FLUXER_IPINFO_BUDGET_STANDARD_REFILL_PER_MIN',
'FLUXER_IPINFO_BUDGET_BACKGROUND_REFILL_PER_MIN',
];
function monthKey(): string {
const now = new Date();
const month = String(now.getUTCMonth() + 1).padStart(2, '0');
return `ipinfo:budget:month:${now.getUTCFullYear()}-${month}`;
}
describe('IpInfoBudget', () => {
const savedEnv = new Map<string, string | undefined>();
beforeEach(() => {
for (const key of BUDGET_ENV_KEYS) {
savedEnv.set(key, process.env[key]);
delete process.env[key];
}
process.env.FLUXER_IPINFO_BUDGET_MONTHLY_MAX = '100';
});
afterEach(() => {
for (const [key, value] of savedEnv) {
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
}
savedEnv.clear();
});
it('sheds background lookups at the background monthly ceiling while standard still admits', async () => {
const kv = new MockKVProvider();
await kv.set(monthKey(), '60');
const budget = createKvIpInfoLookupBudget({getKvClient: () => kv});
expect(await budget.tryConsume('background')).toBe(false);
expect(await budget.tryConsume('standard')).toBe(true);
});
it('sheds standard lookups at the standard monthly ceiling', async () => {
const kv = new MockKVProvider();
await kv.set(monthKey(), '90');
const budget = createKvIpInfoLookupBudget({getKvClient: () => kv});
expect(await budget.tryConsume('standard')).toBe(false);
});
it('admits critical lookups above the standard ceiling', async () => {
const kv = new MockKVProvider();
await kv.set(monthKey(), '95');
const budget = createKvIpInfoLookupBudget({getKvClient: () => kv});
expect(await budget.tryConsume('critical')).toBe(true);
});
it('never increments the monthly counter for a shed lookup', async () => {
const kv = new MockKVProvider();
await kv.set(monthKey(), '60');
const budget = createKvIpInfoLookupBudget({getKvClient: () => kv});
const outcomes = [
await budget.tryConsume('background'),
await budget.tryConsume('background'),
await budget.tryConsume('standard'),
await budget.tryConsume('critical'),
];
expect(outcomes).toEqual([false, false, true, true]);
expect(kv.incrSpy).toHaveBeenCalledTimes(2);
expect(await kv.get(monthKey())).toBe('62');
});
it('sheds once the per-priority burst bucket is drained', async () => {
process.env.FLUXER_IPINFO_BUDGET_BACKGROUND_BURST = '3';
process.env.FLUXER_IPINFO_BUDGET_BACKGROUND_REFILL_PER_MIN = '1';
const kv = new MockKVProvider();
const budget = createKvIpInfoLookupBudget({getKvClient: () => kv});
expect(await budget.tryConsume('background')).toBe(true);
expect(await budget.tryConsume('background')).toBe(true);
expect(await budget.tryConsume('background')).toBe(true);
expect(await budget.tryConsume('background')).toBe(false);
expect(await budget.tryConsume('standard')).toBe(true);
});
it('expires the month key only on the first increment', async () => {
const kv = new MockKVProvider();
const budget = createKvIpInfoLookupBudget({getKvClient: () => kv});
expect(await budget.tryConsume('standard')).toBe(true);
expect(await budget.tryConsume('standard')).toBe(true);
expect(await budget.tryConsume('standard')).toBe(true);
expect(kv.expireSpy).toHaveBeenCalledTimes(1);
expect(kv.expireSpy).toHaveBeenCalledWith(monthKey(), 40 * 24 * 3600);
});
it('fails open for every priority when the KV provider throws', async () => {
const kv = new MockKVProvider();
vi.spyOn(kv, 'get').mockRejectedValue(new Error('kv unavailable'));
const budget = createKvIpInfoLookupBudget({getKvClient: () => kv});
expect(await budget.tryConsume('background')).toBe(true);
expect(await budget.tryConsume('standard')).toBe(true);
expect(await budget.tryConsume('critical')).toBe(true);
});
it('admits everything when the budget is disabled', async () => {
process.env.FLUXER_IPINFO_BUDGET_ENABLED = '0';
const kv = new MockKVProvider();
await kv.set(monthKey(), '1000');
const budget = createKvIpInfoLookupBudget({getKvClient: () => kv});
expect(await budget.tryConsume('background')).toBe(true);
expect(kv.tryConsumeTokensSpy).not.toHaveBeenCalled();
});
});
@@ -0,0 +1,276 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import type {
CachedIpInfoFailure,
IpInfoCache,
IpInfoLookupBudget,
IpInfoLookupPriority,
IpInfoRequestAuditEvent,
IpInfoRequestAuditLogger,
} from '@pkgs/geoip/src/IpInfoService';
import {createIpInfoService, resolveIpInfoLookupPriority} from '@pkgs/geoip/src/IpInfoService';
import {delay, HttpResponse, http} from 'msw';
import {describe, expect, it} from 'vitest';
import {server} from '../../test/msw/server';
interface RecordedSet {
key: string;
value: unknown;
ttlSeconds: number | undefined;
}
interface RecordingCache {
cache: IpInfoCache;
sets: Array<RecordedSet>;
}
function createRecordingCache(): RecordingCache {
const store = new Map<string, unknown>();
const sets: Array<RecordedSet> = [];
return {
sets,
cache: {
async get<T>(key: string): Promise<T | null> {
return (store.get(key) as T | undefined) ?? null;
},
async set<T>(key: string, value: T, ttlSeconds?: number): Promise<void> {
store.set(key, value);
sets.push({key, value, ttlSeconds});
},
},
};
}
function createRecordingAuditLogger(): {logger: IpInfoRequestAuditLogger; events: Array<IpInfoRequestAuditEvent>} {
const events: Array<IpInfoRequestAuditEvent> = [];
return {
events,
logger: {
async record(event: IpInfoRequestAuditEvent): Promise<void> {
events.push(event);
},
},
};
}
function useLookupHandler(handler: () => Response | Promise<Response>): {count: () => number} {
let calls = 0;
server.use(
http.get('https://api.ipinfo.io/lookup/:ip', async () => {
calls += 1;
return await handler();
}),
);
return {count: () => calls};
}
function successPayload(ip: string, anonymous: Record<string, boolean> = {}): Response {
return HttpResponse.json({
ip,
geo: {country_code: 'US', country: 'United States'},
as: {asn: 'AS64500', name: 'Test ISP'},
anonymous,
});
}
describe('IpInfoService caching', () => {
it('negative-caches an HTTP error and serves the second lookup without a request', async () => {
const requests = useLookupHandler(() => new HttpResponse(null, {status: 500}));
const {cache, sets} = createRecordingCache();
const service = createIpInfoService({apiKey: 'token', cache});
const first = await service.lookup('203.0.113.1');
const second = await service.lookup('203.0.113.1');
expect(first.available).toBe(false);
expect(second.available).toBe(false);
expect(requests.count()).toBe(1);
expect(sets).toHaveLength(1);
expect(sets[0]?.ttlSeconds).toBe(300);
});
it('negative-caches a request failure for a short window', async () => {
useLookupHandler(async () => {
await delay(5000);
return successPayload('203.0.113.2');
});
const {cache, sets} = createRecordingCache();
const service = createIpInfoService({apiKey: 'token', cache});
const result = await service.lookup('203.0.113.2');
expect(result.available).toBe(false);
expect(sets[0]?.ttlSeconds).toBe(60);
expect((sets[0]?.value as CachedIpInfoFailure).failureOutcome).toBe('request_failed');
});
it('negative-caches a schema mismatch', async () => {
useLookupHandler(() => HttpResponse.json({}));
const {cache, sets} = createRecordingCache();
const service = createIpInfoService({apiKey: 'token', cache});
const result = await service.lookup('203.0.113.3');
expect(result.available).toBe(false);
expect(sets[0]?.ttlSeconds).toBe(600);
expect((sets[0]?.value as CachedIpInfoFailure).failureOutcome).toBe('schema_mismatch');
expect((sets[0]?.value as CachedIpInfoFailure).failureHttpStatus).toBe(200);
});
it('negative-caches a quota rejection for longer', async () => {
useLookupHandler(() => new HttpResponse(null, {status: 429}));
const {cache, sets} = createRecordingCache();
const service = createIpInfoService({apiKey: 'token', cache});
await service.lookup('203.0.113.4');
expect(sets[0]?.ttlSeconds).toBe(900);
});
it('caps the quota rejection TTL for background lookups', async () => {
useLookupHandler(() => new HttpResponse(null, {status: 429}));
const {cache, sets} = createRecordingCache();
const service = createIpInfoService({apiKey: 'token', cache});
await service.lookup('203.0.113.5', {source: 'AbusiveIpAutoBanner', reason: 'classify'});
expect(sets[0]?.ttlSeconds).toBe(120);
});
it('returns a cached failure as a clean unavailable result', async () => {
useLookupHandler(() => new HttpResponse(null, {status: 500}));
const {cache} = createRecordingCache();
const service = createIpInfoService({apiKey: 'token', cache});
await service.lookup('203.0.113.6');
const cached = await service.lookup('203.0.113.6');
expect(cached).not.toHaveProperty('cachedFailure');
expect(cached).not.toHaveProperty('failureOutcome');
expect(cached).not.toHaveProperty('failureHttpStatus');
expect(cached).not.toHaveProperty('cachedAtMs');
expect(cached.ip).toBe('203.0.113.6');
expect(cached.riskNote).toBe('IPInfo HTTP 500');
});
it('writes a cached failure that older readers can still consume', async () => {
useLookupHandler(() => new HttpResponse(null, {status: 500}));
const {cache, sets} = createRecordingCache();
const service = createIpInfoService({apiKey: 'token', cache});
await service.lookup('203.0.113.7');
const entry = sets[0]?.value as CachedIpInfoFailure;
expect(entry.cachedFailure).toBe(true);
expect(entry.failureOutcome).toBe('http_error');
expect(entry.failureHttpStatus).toBe(500);
expect(typeof entry.cachedAtMs).toBe('number');
const legacyView = {...entry, ip: '203.0.113.7'};
expect(legacyView.available).toBe(false);
expect(legacyView.geo.countryCode).toBeNull();
expect(legacyView.asn.number).toBeNull();
expect(legacyView.mobile.name).toBeNull();
expect(legacyView.anonymous.isAnonymous).toBe(false);
expect(legacyView.flags.isMobile).toBe(false);
});
it('keeps the existing success TTL selection', async () => {
useLookupHandler(() => successPayload('203.0.113.8'));
const plain = createRecordingCache();
await createIpInfoService({apiKey: 'token', cache: plain.cache}).lookup('203.0.113.8');
useLookupHandler(() => successPayload('203.0.113.9', {is_vpn: true}));
const anonymous = createRecordingCache();
await createIpInfoService({apiKey: 'token', cache: anonymous.cache}).lookup('203.0.113.9');
expect(plain.sets[0]?.ttlSeconds).toBe(14 * 24 * 60 * 60);
expect(anonymous.sets[0]?.ttlSeconds).toBe(7 * 24 * 60 * 60);
});
it('coalesces concurrent lookups across a failure', async () => {
const requests = useLookupHandler(() => new HttpResponse(null, {status: 500}));
const {cache, sets} = createRecordingCache();
const service = createIpInfoService({apiKey: 'token', cache});
const [first, second] = await Promise.all([service.lookup('203.0.113.10'), service.lookup('203.0.113.10')]);
expect(requests.count()).toBe(1);
expect(sets).toHaveLength(1);
expect(first.available).toBe(false);
expect(second.available).toBe(false);
});
it('sheds a lookup when the budget refuses it', async () => {
const requests = useLookupHandler(() => successPayload('203.0.113.11'));
const {cache, sets} = createRecordingCache();
const {logger, events} = createRecordingAuditLogger();
const refused: Array<IpInfoLookupPriority> = [];
const budget: IpInfoLookupBudget = {
async tryConsume(priority: IpInfoLookupPriority): Promise<boolean> {
refused.push(priority);
return priority !== 'background';
},
};
const service = createIpInfoService({apiKey: 'token', cache, auditLogger: logger, budget});
const result = await service.lookup('203.0.113.11', {source: 'AbusiveIpAutoBanner', reason: 'classify'});
expect(refused).toEqual(['background']);
expect(requests.count()).toBe(0);
expect(result.available).toBe(false);
expect(result.riskNote).toContain('shed');
expect(sets).toHaveLength(0);
expect(events).toHaveLength(1);
expect(events[0]?.outcome).toBe('budget_shed');
expect(events[0]?.httpStatus).toBeNull();
});
it('still coalesces concurrent lookups when a budget is configured', async () => {
const requests = useLookupHandler(() => successPayload('203.0.113.13'));
const {cache} = createRecordingCache();
const consumed: Array<IpInfoLookupPriority> = [];
const budget: IpInfoLookupBudget = {
async tryConsume(priority: IpInfoLookupPriority): Promise<boolean> {
consumed.push(priority);
return true;
},
};
const service = createIpInfoService({apiKey: 'token', cache, budget});
const results = await Promise.all([
service.lookup('203.0.113.13', {source: 'risk.geoip_city'}),
service.lookup('203.0.113.13', {source: 'risk.geoip_asn'}),
service.lookup('203.0.113.13', {source: 'risk.ipinfo_checker'}),
]);
expect(requests.count()).toBe(1);
expect(consumed).toEqual(['standard']);
expect(results.every((result) => result.available)).toBe(true);
});
it('lets an admitting budget through', async () => {
const requests = useLookupHandler(() => successPayload('203.0.113.12'));
const {cache} = createRecordingCache();
const budget: IpInfoLookupBudget = {
async tryConsume(): Promise<boolean> {
return true;
},
};
const service = createIpInfoService({apiKey: 'token', cache, budget});
const result = await service.lookup('203.0.113.12', {source: 'risk.ipinfo_checker'});
expect(requests.count()).toBe(1);
expect(result.available).toBe(true);
});
it('maps every lookup source to a priority', () => {
expect(resolveIpInfoLookupPriority('admin.ip_ban')).toBe('critical');
expect(resolveIpInfoLookupPriority('admin.scheduled_deletion_suspicious_ip')).toBe('critical');
expect(resolveIpInfoLookupPriority('AbusiveIpAutoBanner')).toBe('background');
expect(resolveIpInfoLookupPriority('risk.ipinfo_checker')).toBe('standard');
expect(resolveIpInfoLookupPriority('risk.geoip_city')).toBe('standard');
expect(resolveIpInfoLookupPriority('risk.geoip_asn')).toBe('standard');
expect(resolveIpInfoLookupPriority(undefined)).toBe('standard');
});
});
@@ -0,0 +1,110 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {createCurrentBehaviorTestAccountPolicyEvaluator} from '../../test/AccountPolicyTestEvaluator';
import {setInjectedAccountPolicyEvaluator} from '../AccountPolicyService';
import {
type IpInfoPrescreenOptions,
ipInfoPrescreenOptionsFromEnv,
type LocalIpIntel,
prescreenIpInfoLookup,
} from '../RegistrationIpPrescreen';
const CLEAN_LOCAL: LocalIpIntel = {countryIso: 'SE', asn: 64500, asnOrg: 'Example Broadband ISP'};
function options(overrides: Partial<IpInfoPrescreenOptions> = {}): IpInfoPrescreenOptions {
return {enabled: true, allowedAsns: new Set([64500]), ...overrides};
}
function local(overrides: Partial<LocalIpIntel> = {}): LocalIpIntel {
return {...CLEAN_LOCAL, ...overrides};
}
beforeEach(() => {
setInjectedAccountPolicyEvaluator(createCurrentBehaviorTestAccountPolicyEvaluator());
});
afterEach(() => {
setInjectedAccountPolicyEvaluator(undefined);
delete process.env.FLUXER_RISK_IPINFO_PRESCREEN_ENABLED;
delete process.env.FLUXER_RISK_IPINFO_PRESCREEN_ALLOW_ASNS;
});
describe('prescreenIpInfoLookup', () => {
it('skips the lookup only when every local signal is clean and allowlisted', () => {
expect(prescreenIpInfoLookup(local(), options())).toBe('skip');
});
it('consults when the pre-screen is disabled', () => {
expect(prescreenIpInfoLookup(local(), options({enabled: false}))).toBe('consult');
});
it('consults when the allowlist is empty', () => {
expect(prescreenIpInfoLookup(local(), options({allowedAsns: new Set()}))).toBe('consult');
});
it('consults when the local city database has no country', () => {
expect(prescreenIpInfoLookup(local({countryIso: null}), options())).toBe('consult');
});
it('consults when the local ASN database has no ASN', () => {
expect(prescreenIpInfoLookup(local({asn: null}), options())).toBe('consult');
});
it('consults when the ASN is not on the allowlist', () => {
expect(prescreenIpInfoLookup(local({asn: 64501}), options())).toBe('consult');
});
it('consults for a trusted commercial privacy provider even on the allowlist', () => {
expect(prescreenIpInfoLookup(local({asnOrg: 'Example Privacy Relay LLC'}), options())).toBe('consult');
});
it('consults for an education organization even on the allowlist', () => {
expect(prescreenIpInfoLookup(local({asnOrg: 'North Example Academy'}), options())).toBe('consult');
});
it('consults for a cellular organization even on the allowlist', () => {
expect(prescreenIpInfoLookup(local({asnOrg: 'Example cell-net Wireless'}), options())).toBe('consult');
});
it('skips when the allowlisted ASN carries no organization name to veto', () => {
expect(prescreenIpInfoLookup(local({asnOrg: null}), options())).toBe('skip');
});
});
describe('ipInfoPrescreenOptionsFromEnv', () => {
it('is disabled with an empty allowlist by default', () => {
const parsed = ipInfoPrescreenOptionsFromEnv();
expect(parsed.enabled).toBe(false);
expect(parsed.allowedAsns.size).toBe(0);
});
it('treats only 1 and true as enabled', () => {
process.env.FLUXER_RISK_IPINFO_PRESCREEN_ENABLED = '1';
expect(ipInfoPrescreenOptionsFromEnv().enabled).toBe(true);
process.env.FLUXER_RISK_IPINFO_PRESCREEN_ENABLED = 'TRUE';
expect(ipInfoPrescreenOptionsFromEnv().enabled).toBe(true);
process.env.FLUXER_RISK_IPINFO_PRESCREEN_ENABLED = 'yes';
expect(ipInfoPrescreenOptionsFromEnv().enabled).toBe(false);
process.env.FLUXER_RISK_IPINFO_PRESCREEN_ENABLED = '0';
expect(ipInfoPrescreenOptionsFromEnv().enabled).toBe(false);
});
it('parses a comma separated allowlist and drops non numeric entries', () => {
process.env.FLUXER_RISK_IPINFO_PRESCREEN_ALLOW_ASNS = ' 64500, 64501 ,,notanasn,64502x,-3,64503 ';
expect([...ipInfoPrescreenOptionsFromEnv().allowedAsns]).toEqual([64500, 64501, 64503]);
});
it('parses an empty allowlist from an empty or whitespace value', () => {
process.env.FLUXER_RISK_IPINFO_PRESCREEN_ALLOW_ASNS = '';
expect(ipInfoPrescreenOptionsFromEnv().allowedAsns.size).toBe(0);
process.env.FLUXER_RISK_IPINFO_PRESCREEN_ALLOW_ASNS = ' , ,';
expect(ipInfoPrescreenOptionsFromEnv().allowedAsns.size).toBe(0);
});
it('yields a consult verdict for every input with the shipped defaults', () => {
const shipped = ipInfoPrescreenOptionsFromEnv();
expect(prescreenIpInfoLookup(local(), shipped)).toBe('consult');
expect(prescreenIpInfoLookup(local({asnOrg: null}), shipped)).toBe('consult');
});
});
@@ -0,0 +1,130 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import type {IpInfoCache} from '@pkgs/geoip/src/IpInfoService';
import {createTieredIpInfoCache} from '@pkgs/geoip/src/TieredIpInfoCache';
import {describe, expect, it} from 'vitest';
interface RecordedSet {
key: string;
value: unknown;
ttlSeconds: number | undefined;
}
interface RecordingCache {
cache: IpInfoCache;
store: Map<string, unknown>;
sets: Array<RecordedSet>;
}
function createRecordingCache(): RecordingCache {
const store = new Map<string, unknown>();
const sets: Array<RecordedSet> = [];
return {
store,
sets,
cache: {
async get<T>(key: string): Promise<T | null> {
return (store.get(key) as T | undefined) ?? null;
},
async set<T>(key: string, value: T, ttlSeconds?: number): Promise<void> {
store.set(key, value);
sets.push({key, value, ttlSeconds});
},
},
};
}
describe('TieredIpInfoCache', () => {
it('clamps the hot TTL to the requested TTL and passes the raw TTL to the cold tier', async () => {
const hot = createRecordingCache();
const cold = createRecordingCache();
const tiered = createTieredIpInfoCache({hot: hot.cache, cold: cold.cache});
await tiered.set('a', {available: false}, 60);
expect(hot.sets).toEqual([{key: 'a', value: {available: false}, ttlSeconds: 60}]);
expect(cold.sets).toEqual([{key: 'a', value: {available: false}, ttlSeconds: 60}]);
});
it('caps the hot TTL at the configured hot window', async () => {
const hot = createRecordingCache();
const cold = createRecordingCache();
const tiered = createTieredIpInfoCache({hot: hot.cache, cold: cold.cache});
await tiered.set('a', {available: true}, 100000);
expect(hot.sets[0]?.ttlSeconds).toBe(600);
expect(cold.sets[0]?.ttlSeconds).toBe(100000);
});
it('uses the hot window when no TTL is supplied', async () => {
const hot = createRecordingCache();
const cold = createRecordingCache();
const tiered = createTieredIpInfoCache({hot: hot.cache, cold: cold.cache});
await tiered.set('a', {available: true});
expect(hot.sets[0]?.ttlSeconds).toBe(600);
expect(cold.sets[0]?.ttlSeconds).toBeUndefined();
});
it('skips the cold write when skipColdWrite matches', async () => {
const hot = createRecordingCache();
const cold = createRecordingCache();
const tiered = createTieredIpInfoCache({
hot: hot.cache,
cold: cold.cache,
skipColdWrite: (value) => (value as {available?: unknown}).available === false,
});
await tiered.set('a', {available: false}, 60);
await tiered.set('b', {available: true}, 60);
expect(hot.sets.map((entry) => entry.key)).toEqual(['a', 'b']);
expect(cold.sets.map((entry) => entry.key)).toEqual(['b']);
});
it('promotes a cold hit into the hot tier', async () => {
const hot = createRecordingCache();
const cold = createRecordingCache();
cold.store.set('a', {available: true});
const tiered = createTieredIpInfoCache({hot: hot.cache, cold: cold.cache});
const hit = await tiered.get('a');
expect(hit).toEqual({available: true});
expect(hot.sets).toEqual([{key: 'a', value: {available: true}, ttlSeconds: 600}]);
});
it('never promotes a cold hit that skipColdWrite matches', async () => {
const hot = createRecordingCache();
const cold = createRecordingCache();
cold.store.set('a', {available: false});
const tiered = createTieredIpInfoCache({
hot: hot.cache,
cold: cold.cache,
skipColdWrite: (value) => (value as {available?: unknown}).available === false,
});
const hit = await tiered.get('a');
expect(hit).toEqual({available: false});
expect(hot.sets).toEqual([]);
});
it('never writes a zero TTL', async () => {
const hot = createRecordingCache();
const cold = createRecordingCache();
const tiered = createTieredIpInfoCache({hot: hot.cache, cold: cold.cache});
await tiered.set('a', {available: false}, 60);
await tiered.set('b', {available: true}, 100000);
await tiered.set('c', {available: true});
cold.store.set('d', {available: true});
await tiered.get('d');
for (const entry of [...hot.sets, ...cold.sets]) {
expect(entry.ttlSeconds === undefined || entry.ttlSeconds > 0).toBe(true);
}
});
});
@@ -1,15 +1,18 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {parseIpAddress} from '@fluxer/ip_utils/src/IpAddress';
import type {GeoipAsnResult, GeoipResult} from '@pkgs/geoip/src/GeoipLookup';
import type {IpInfoService} from '@pkgs/geoip/src/IpInfoService';
import type {GeoIpAsnResult, GeoIpCityResult} from '../RiskTypes';
interface GeoIpCityContext {
ipInfoService: IpInfoService;
lookupLocalCity?: (ip: string) => Promise<GeoipResult>;
}
interface GeoIpAsnContext {
ipInfoService: IpInfoService;
lookupLocalAsn?: (ip: string) => Promise<GeoipAsnResult>;
}
export function createGeoIpCityAdapter(ctx: GeoIpCityContext) {
@@ -19,6 +22,22 @@ export function createGeoIpCityAdapter(ctx: GeoIpCityContext) {
if (!parsed) {
return notFound(ip, true);
}
const local = ctx.lookupLocalCity ? await ctx.lookupLocalCity(parsed.normalized) : null;
if (local && local.countryCode !== null) {
return {
ip,
available: true,
found: true,
countryIso: local.countryCode,
country: local.countryName,
region: local.region,
city: local.city,
latitude: local.latitude ?? null,
longitude: local.longitude ?? null,
accuracyRadiusKm: local.accuracyRadiusKm ?? null,
timeZone: local.timeZone ?? null,
};
}
const info = await ctx.ipInfoService.lookup(parsed.normalized, {
source: 'risk.geoip_city',
reason: 'registration_risk',
@@ -63,6 +82,10 @@ export function createGeoIpAsnAdapter(ctx: GeoIpAsnContext) {
if (!parsed) {
return {ip, available: true, found: false, asn: null, asnOrg: null};
}
const local = ctx.lookupLocalAsn ? await ctx.lookupLocalAsn(parsed.normalized) : null;
if (local && local.asn !== null) {
return {ip, available: true, found: true, asn: local.asn, asnOrg: local.asnOrg};
}
const info = await ctx.ipInfoService.lookup(parsed.normalized, {
source: 'risk.geoip_asn',
reason: 'registration_risk',
@@ -2,14 +2,40 @@
import type {IpInfoService} from '@pkgs/geoip/src/IpInfoService';
import {isAccountPolicyEducationOrganizationName} from '../AccountPolicyService';
import type {IpInfoPrescreenVerdict} from '../RegistrationIpPrescreen';
import type {IpConnectionType, IpInfoAnonymousResult} from '../RiskTypes';
interface IpInfoCheckerContext {
ipInfoService: IpInfoService;
prescreen?: (ip: string) => Promise<IpInfoPrescreenVerdict>;
}
export function unavailableIpInfoAnonymousResult(ip: string, riskNote: string): IpInfoAnonymousResult {
return {
ip,
available: false,
isAnonymous: false,
providerName: null,
isVpn: false,
isProxy: false,
isResidentialProxy: false,
isTor: false,
isRelay: false,
isHosting: false,
isMobile: false,
asnType: null,
asnOrg: null,
connectionType: 'unknown',
percentDaysSeen: null,
riskNote,
};
}
export function createIpInfoChecker(ctx: IpInfoCheckerContext) {
return async function checkIpInfo(ip: string): Promise<IpInfoAnonymousResult> {
if (ctx.prescreen && (await ctx.prescreen(ip)) === 'skip') {
return unavailableIpInfoAnonymousResult(ip, 'IPInfo skipped (local pre-screen)');
}
const result = await ctx.ipInfoService.lookup(ip, {
source: 'risk.ipinfo_checker',
reason: 'registration_risk',
@@ -344,7 +344,7 @@ describe('Message Search Permissions', () => {
}
expect(result.messages.length).toBe(0);
});
test('age-restricted guild requires include_nsfw: true', async () => {
test('age-restricted guild is searchable by an adult member without include_nsfw', async () => {
const owner = await createTestAccount(harness, {dateOfBirth: '2000-01-01'});
const guild = await createGuild(harness, owner.token, 'Age Restricted Search Guild');
const systemChannelId = guild.system_channel_id!;
@@ -363,7 +363,7 @@ describe('Message Search Permissions', () => {
if (!isSearchResult(excluded)) {
expect.fail('Expected search result but got indexing response');
}
expect(excluded.messages.length).toBe(0);
expect(excluded.messages.some((m) => m.channel_id === systemChannelId)).toBe(true);
const included = await createBuilder<MessageSearchResponse>(harness, owner.token)
.post('/search/messages')
.body({
@@ -379,6 +379,30 @@ describe('Message Search Permissions', () => {
expect(included.messages.length).toBeGreaterThan(0);
expect(included.messages.some((m) => m.channel_id === systemChannelId)).toBe(true);
});
test('age-restricted guild is searchable in a channel pinned to nsfw_override: false', async () => {
const owner = await createTestAccount(harness, {dateOfBirth: '2000-01-01'});
const guild = await createGuild(harness, owner.token, 'Age Restricted Override Guild');
const channel = await createBuilder<{id: string; nsfw_override?: boolean | null}>(harness, owner.token)
.post(`/guilds/${guild.id}/channels`)
.body({name: 'override-channel', type: ChannelTypes.GUILD_TEXT, nsfw: false})
.execute();
expect(channel.nsfw_override).toBe(false);
await sendChannelMessage(harness, owner.token, channel.id, 'age restricted override searchable message');
await updateGuild(harness, owner.token, guild.id, {nsfw_level: GuildNSFWLevel.AGE_RESTRICTED});
await markGuildChannelsAsIndexed(harness, owner.token, guild.id);
const result = await createBuilder<MessageSearchResponse>(harness, owner.token)
.post('/search/messages')
.body({
content: 'age restricted override searchable',
context_guild_id: guild.id,
})
.expect(HTTP_STATUS.OK)
.execute();
if (!isSearchResult(result)) {
expect.fail('Expected search result but got indexing response');
}
expect(result.messages.some((m) => m.channel_id === channel.id)).toBe(true);
});
test('underage user cannot search messages in an age-restricted guild', async () => {
const owner = await createTestAccount(harness, {dateOfBirth: '2000-01-01'});
const underageMember = await createTestAccount(harness, {dateOfBirth: '2012-01-01'});
@@ -2,6 +2,7 @@
import {UserPremiumTypes} from '@fluxer/constants/src/UserConstants';
import {Config} from '../Config';
import {Logger} from '../Logger';
import type {Currency} from '../utils/CurrencyUtils';
export enum ProductType {
@@ -22,10 +23,66 @@ export interface ProductInfo {
billingCycle?: RecurringBillingCycle;
}
const LEGACY_SLOT_SHAPES: Record<string, Omit<ProductInfo, 'currency'> | undefined> = {
monthly: {
type: ProductType.MONTHLY_SUBSCRIPTION,
premiumType: UserPremiumTypes.SUBSCRIPTION,
durationMonths: 1,
isGift: false,
billingCycle: 'monthly',
},
yearly: {
type: ProductType.YEARLY_SUBSCRIPTION,
premiumType: UserPremiumTypes.SUBSCRIPTION,
durationMonths: 12,
isGift: false,
billingCycle: 'yearly',
},
gift_1_month: {
type: ProductType.GIFT_1_MONTH,
premiumType: UserPremiumTypes.SUBSCRIPTION,
durationMonths: 1,
isGift: true,
},
gift_1_year: {
type: ProductType.GIFT_1_YEAR,
premiumType: UserPremiumTypes.SUBSCRIPTION,
durationMonths: 12,
isGift: true,
},
};
const LEGACY_SLOT_CURRENCIES: Record<string, Currency | undefined> = {
usd: 'USD',
eur: 'EUR',
brl: 'BRL',
inr: 'INR',
pln: 'PLN',
try: 'TRY',
};
function parseLegacySlot(slot: string): ProductInfo | null {
const separatorIndex = slot.lastIndexOf('_');
if (separatorIndex <= 0) {
return null;
}
const shape = LEGACY_SLOT_SHAPES[slot.slice(0, separatorIndex)];
const currency = LEGACY_SLOT_CURRENCIES[slot.slice(separatorIndex + 1).toLowerCase()];
if (!shape || !currency) {
return null;
}
return {...shape, currency};
}
export class ProductRegistry {
private products = new Map<string, ProductInfo>();
constructor() {
this.registerConfiguredProducts();
this.registerLegacyProducts();
}
private registerConfiguredProducts(): void {
const prices = Config.stripe.prices;
if (!prices) return;
this.registerProduct(prices.monthlyUsd, {
@@ -210,6 +267,32 @@ export class ProductRegistry {
});
}
private registerLegacyProducts(): void {
const legacyPrices = Config.stripe.legacyPrices;
if (!legacyPrices) return;
if (typeof legacyPrices !== 'object' || Array.isArray(legacyPrices)) {
Logger.warn({}, 'Ignoring legacy Stripe price configuration that is not an object of slot names to price ids');
return;
}
for (const [slot, priceIds] of Object.entries(legacyPrices)) {
if (!Array.isArray(priceIds)) {
Logger.warn({slot}, 'Ignoring legacy Stripe price slot that is not a list of price IDs');
continue;
}
const info = parseLegacySlot(slot);
if (!info) {
Logger.warn({slot}, 'Ignoring legacy Stripe price slot with an unrecognised name or currency');
continue;
}
for (const priceId of priceIds) {
if (!priceId || this.products.has(priceId)) {
continue;
}
this.products.set(priceId, info);
}
}
}
private registerProduct(priceId: string | undefined, info: ProductInfo): void {
if (priceId) {
this.products.set(priceId, info);
@@ -18,6 +18,7 @@ import {
PriceIdsResponse,
SelfServeRefundEligibilityResponse,
SelfServeRefundResponse,
SwitchToListPriceResponse,
UrlResponse,
WebhookReceivedResponse,
} from '@fluxer/schema/src/domains/premium/PremiumSchemas';
@@ -494,6 +495,27 @@ export function StripeController(app: HonoApp) {
return ctx.body(null, 204);
},
);
app.post(
'/premium/switch-to-list-price',
RateLimitMiddleware(RateLimitConfigs.STRIPE_SUBSCRIPTION_CHANGE),
LoginRequired,
DefaultUserOnly,
OpenAPI({
operationId: 'switch_subscription_to_list_price',
summary: 'Switch subscription to the current list price',
description:
"Moves the authenticated user's grandfathered premium subscription down to the current list price for the same currency and billing cycle, effective at the end of the current billing period. The target price is resolved on the server and the switch is refused unless it lowers the amount charged.",
responseSchema: SwitchToListPriceResponse,
statusCode: 200,
security: ['bearerToken', 'sessionToken'],
tags: 'Premium',
}),
async (ctx) => {
const userId = ctx.get('user').id;
const result = await ctx.get('stripeService').switchSubscriptionToCurrentListPrice(userId);
return ctx.json(result);
},
);
app.post(
'/premium/cancel-pending-subscription-change',
RateLimitMiddleware(RateLimitConfigs.STRIPE_SUBSCRIPTION_CHANGE),
@@ -7,6 +7,7 @@ import type {
PricingMode,
SelfServeRefundEligibilityResponse,
SelfServeRefundResponse,
SwitchToListPriceResponse,
} from '@fluxer/schema/src/domains/premium/PremiumSchemas';
import type {ICacheService} from '@pkgs/cache/src/ICacheService';
import Stripe from 'stripe';
@@ -201,6 +202,10 @@ export class StripeService {
return this.subscriptionService.changeBillingCycle(userId, billingCycle, effectiveAt);
}
async switchSubscriptionToCurrentListPrice(userId: UserID): Promise<SwitchToListPriceResponse> {
return this.subscriptionService.switchToCurrentListPrice(userId);
}
async cancelPendingSubscriptionChange(userId: UserID): Promise<void> {
return this.subscriptionService.cancelPendingSubscriptionChange(userId);
}
@@ -4,6 +4,8 @@ import {PremiumFlags, UserPremiumTypes} from '@fluxer/constants/src/UserConstant
import {UnknownUserError} from '@fluxer/errors/src/domains/user/UnknownUserError';
import type {
CurrentSubscriptionPriceResponse,
ListPriceSwitchState,
PendingSubscriptionChangeKind,
PendingSubscriptionChangeResponse,
PremiumBillingInvoiceResponse,
PremiumBillingPaymentMethodResponse,
@@ -264,6 +266,9 @@ export class PremiumStateService {
this.resolvePricing(countryCode),
]);
const refundEligibilityState = await this.resolveRefundEligibility(user, invoices.allRows);
const listPriceSwitch = this.resolveListPriceSwitch(subscription, subscriptionPrice, pendingSubscriptionChange);
const pendingBillingCycleChange =
pendingSubscriptionChange?.change_kind === 'billing_cycle' ? pendingSubscriptionChange : null;
const activePaidPremium = checkHasActivePaidPremium(user);
const isEffectivePremium = user.isPremium();
const actualPremiumEndAt = user.effectivePremiumUntil;
@@ -298,7 +303,8 @@ export class PremiumStateService {
billing: {
stripe_customer_id: customerIds[0] ?? user.stripeCustomerId ?? null,
current_subscription_price: subscriptionPrice,
pending_subscription_change: pendingSubscriptionChange,
pending_subscription_change: pendingBillingCycleChange,
list_price_switch: listPriceSwitch,
subscription: subscription ? await this.mapSubscription(subscription) : null,
invoices: invoices.rows.map(mapInvoice),
invoices_has_more: invoices.hasMore,
@@ -576,8 +582,16 @@ export class PremiumStateService {
const targetItem = futurePhase.items[0] ?? null;
const targetPriceDetails = await this.resolveStripePriceDetails(targetItem?.price ?? null);
const metadataTargetBillingCycle = normalizeBillingCycle(schedule.metadata?.pending_billing_cycle);
const targetBillingCycle = targetPriceDetails.billingCycle ?? metadataTargetBillingCycle;
if (!targetBillingCycle || targetBillingCycle === currentBillingCycle) {
const targetBillingCycle = targetPriceDetails.billingCycle ?? metadataTargetBillingCycle ?? currentBillingCycle;
if (!targetBillingCycle) {
return null;
}
const changeKind: PendingSubscriptionChangeKind =
targetBillingCycle === currentBillingCycle ? 'price' : 'billing_cycle';
if (
changeKind === 'price' &&
(targetPriceDetails.priceId == null || targetPriceDetails.priceId === currentPriceDetails.priceId)
) {
return null;
}
const quantity = targetItem?.quantity ?? currentItem?.quantity ?? 1;
@@ -598,11 +612,13 @@ export class PremiumStateService {
const creditAmountMinor = firstInvoiceAdjustmentTotal < 0 ? -firstInvoiceAdjustmentTotal : null;
return {
schedule_id: schedule.id,
change_kind: changeKind,
current_billing_cycle: currentBillingCycle,
target_billing_cycle: targetBillingCycle,
effective_at: new Date(futurePhase.start_date * 1000).toISOString(),
current_price_id: currentPriceDetails.priceId,
target_price_id: targetPriceDetails.priceId,
target_amount_minor: targetPriceDetails.amountMinor,
currency: targetPriceDetails.currency,
initial_amount_minor: initialAmountMinor,
recurring_amount_minor: recurringAmountMinor,
@@ -610,6 +626,62 @@ export class PremiumStateService {
};
}
private resolveListPriceSwitch(
subscription: BillingSubscriptionRow | null,
subscriptionPrice: CurrentSubscriptionPriceResponse,
pendingChange: PendingSubscriptionChangeResponse,
): ListPriceSwitchState {
const base = {
pending: false,
current_price_id: subscriptionPrice?.price_id ?? null,
current_amount_minor: subscriptionPrice?.amount_minor ?? null,
list_price_id: subscriptionPrice?.list_price_id ?? null,
list_amount_minor: subscriptionPrice?.list_amount_minor ?? null,
currency: subscriptionPrice?.currency ?? null,
billing_cycle: subscriptionPrice?.billing_cycle ?? null,
effective_at: toIso(subscription?.current_period_end),
};
if (Config.instance.selfHosted || !Config.stripe.enabled || !Config.stripe.secretKey) {
return {...base, available: false, reason: 'feature_unavailable'};
}
if (!subscription) {
return {...base, available: false, reason: 'no_active_subscription'};
}
if (!subscriptionPrice) {
return {...base, available: false, reason: 'unsupported_subscription'};
}
if (!subscription.status || !ACTIVE_SUBSCRIPTION_STATUSES.has(subscription.status)) {
return {...base, available: false, reason: 'subscription_not_chargeable'};
}
if (subscription.cancel_at != null || subscription.cancel_at_period_end === true) {
return {...base, available: false, reason: 'subscription_cancelling'};
}
if (pendingChange) {
const targetsListPrice =
subscriptionPrice.list_price_id != null && pendingChange.target_price_id === subscriptionPrice.list_price_id;
return {
...base,
available: false,
reason: targetsListPrice ? null : 'conflicting_pending_change',
pending: targetsListPrice,
effective_at: pendingChange.effective_at,
};
}
if (subscriptionPrice.list_price_id == null || subscriptionPrice.list_amount_minor == null) {
return {...base, available: false, reason: 'no_list_price'};
}
if (subscriptionPrice.list_price_id === subscriptionPrice.price_id) {
return {...base, available: false, reason: 'already_on_list_price'};
}
if (subscriptionPrice.list_amount_minor >= subscriptionPrice.amount_minor) {
return {...base, available: false, reason: 'not_a_price_decrease'};
}
if (base.effective_at == null) {
return {...base, available: false, reason: 'missing_period_end'};
}
return {...base, available: true, reason: null};
}
private async resolveStripePriceDetails(
priceRef: Stripe.Price | Stripe.DeletedPrice | string | null | undefined,
): Promise<StripePriceDetails> {
@@ -5,11 +5,13 @@ import {PremiumFlags, UserFlags} from '@fluxer/constants/src/UserConstants';
import {StripeError} from '@fluxer/errors/src/domains/payment/StripeError';
import type {IEmailService} from '@pkgs/email/src/IEmailService';
import type Stripe from 'stripe';
import type {UserRow} from '../../database/types/UserTypes';
import type {IDonationRepository} from '../../donation/IDonationRepository';
import type {IGatewayService} from '../../infrastructure/IGatewayService';
import type {KVAccountDeletionQueueService} from '../../infrastructure/KVAccountDeletionQueueService';
import type {UserCacheService} from '../../infrastructure/UserCacheService';
import {Logger} from '../../Logger';
import {getBillingRepository} from '../../middleware/ServiceRegistry';
import type {GiftCode} from '../../models/GiftCode';
import type {User} from '../../models/User';
import type {IUserRepository} from '../../user/IUserRepository';
@@ -19,6 +21,8 @@ import {extractId} from '../StripeUtils';
import type {StripeGiftReversalHandler} from './StripeGiftReversalHandler';
import type {StripePaymentFraudService} from './StripePaymentFraudService';
export const REFUND_ALLOWANCE_CLAIM_PREFIX = 'refund-allowance';
export class StripeDisputeWebhookHandler {
constructor(
private userRepository: IUserRepository,
@@ -105,7 +109,7 @@ export class StripeDisputeWebhookHandler {
}
Logger.debug(
{userId: payment.userId},
'User unsuspended after chargeback withdrawal - 30 day purchase block applied',
'User unsuspended after chargeback withdrawal - 30 day self-serve refund cooldown applied',
);
}
}
@@ -167,25 +171,94 @@ export class StripeDisputeWebhookHandler {
);
user = foundUser;
}
if (!user.firstRefundAt) {
const updatedUser = await this.userRepository.patchUpsert(user.id, {first_refund_at: new Date()}, user.toRow());
await this.dispatchUser(updatedUser);
const claimKeys = await this.resolveRefundAllowanceClaimKeys(charge);
if (claimKeys.length === 0) {
Logger.debug(
{userId: user.id, chargeId: charge.id, paymentIntentId},
'First refund recorded - 30 day purchase block applied',
);
} else {
const updatedUser = await this.userRepository.patchUpsert(
user.id,
{premium_flags: user.premiumFlags | PremiumFlags.PURCHASE_DISABLED},
user.toRow(),
);
await this.dispatchUser(updatedUser);
Logger.debug(
{userId: user.id, chargeId: charge.id, paymentIntentId},
'Second refund recorded - permanent purchase block applied',
'Refund was issued by Fluxer - not counted against the user refund allowance',
);
return;
}
const claimedKeys: Array<string> = [];
let alreadyCounted = false;
for (const claimKey of claimKeys) {
const claim = await getBillingRepository().webhookEvents.tryClaim(claimKey);
if (claim === 'claimed') {
claimedKeys.push(claimKey);
} else {
alreadyCounted = true;
}
}
if (alreadyCounted) {
for (const claimKey of claimedKeys) {
await getBillingRepository().webhookEvents.markProcessed(claimKey);
}
Logger.debug(
{userId: user.id, chargeId: charge.id, paymentIntentId, claimKeys},
'Refund already counted against the user refund allowance',
);
return;
}
const isFirstRefund = !user.firstRefundAt;
const patch: Partial<UserRow> = isFirstRefund
? {first_refund_at: new Date()}
: {premium_flags: user.premiumFlags | PremiumFlags.PURCHASE_DISABLED};
let updatedUser: User;
try {
updatedUser = await this.userRepository.patchUpsert(user.id, patch, user.toRow());
} catch (error) {
for (const claimKey of claimedKeys) {
await getBillingRepository().webhookEvents.releaseClaim(claimKey);
}
throw error;
}
for (const claimKey of claimedKeys) {
await getBillingRepository().webhookEvents.markProcessed(claimKey);
}
await this.dispatchUser(updatedUser);
Logger.debug(
{userId: user.id, chargeId: charge.id, paymentIntentId},
isFirstRefund
? 'First refund recorded - 30 day self-serve refund cooldown applied'
: 'Second refund recorded - permanent purchase block applied',
);
}
private async resolveRefundAllowanceClaimKeys(charge: Stripe.Charge): Promise<Array<string>> {
const chargeClaimKey = `${REFUND_ALLOWANCE_CLAIM_PREFIX}:${charge.id}`;
const inlined = charge.refunds?.data ?? [];
const refunds = (
inlined.length > 0
? inlined.map((refund) => ({
id: refund.id,
createdAtMs: refund.created * 1000,
status: refund.status,
rejectionReason: refund.metadata?.rejection_reason ?? null,
}))
: (await getBillingRepository().refunds.listByCharge(charge.id)).map((row) => ({
id: row.provider_id,
createdAtMs: row.stripe_created_at?.getTime() ?? 0,
status: row.status,
rejectionReason: row.metadata?.get('rejection_reason') ?? null,
}))
).filter((refund) => refund.status !== 'failed' && refund.status !== 'canceled');
if (refunds.length === 0) {
Logger.warn(
{chargeId: charge.id},
'No refund records found for refunded charge; counting the charge once against the user refund allowance',
);
return [chargeClaimKey];
}
const customerRefunds = refunds
.filter((refund) => refund.rejectionReason === null)
.sort((left, right) => left.createdAtMs - right.createdAtMs || left.id.localeCompare(right.id));
const earliest = customerRefunds[0];
const latest = customerRefunds[customerRefunds.length - 1];
if (!earliest || !latest) {
return [];
}
const latestClaimKey = `${REFUND_ALLOWANCE_CLAIM_PREFIX}:${latest.id}`;
return latest.id === earliest.id ? [chargeClaimKey, latestClaimKey] : [latestClaimKey];
}
private async handleGiftChargeback(giftCode: GiftCode, dispute: Stripe.Dispute): Promise<void> {
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {PremiumFlags} from '@fluxer/constants/src/UserConstants';
import {FeatureNotAvailableSelfHostedError} from '@fluxer/errors/src/domains/core/FeatureNotAvailableSelfHostedError';
import {StripeError} from '@fluxer/errors/src/domains/payment/StripeError';
import {StripeNoPurchaseHistoryError} from '@fluxer/errors/src/domains/payment/StripeNoPurchaseHistoryError';
@@ -15,11 +16,13 @@ import type {
import type Stripe from 'stripe';
import {createUserID, type UserID} from '../../BrandedTypes';
import {Config} from '../../Config';
import type {UserRow} from '../../database/types/UserTypes';
import {Logger} from '../../Logger';
import {getBillingRepository} from '../../middleware/ServiceRegistry';
import type {User} from '../../models/User';
import type {IUserRepository} from '../../user/IUserRepository';
import {extractId} from '../StripeUtils';
import {REFUND_ALLOWANCE_CLAIM_PREFIX} from './StripeDisputeWebhookHandler';
import type {StripeSubscriptionService} from './StripeSubscriptionService';
const MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
@@ -260,16 +263,35 @@ export class StripeRefundService {
try {
await this.subscriptionService.cancelSubscriptionImmediately(user.id, 'self_serve_refund');
} catch (error) {
Logger.warn(
Logger.error(
{error, userId: user.id.toString(), subscriptionId},
'Self-serve refund confirmed but subscription cancellation failed; will reconcile via webhook',
'Self-serve refund confirmed but subscription cancellation failed; the subscription is still active and will keep billing until it is cancelled manually',
);
}
}
await this.userRepository.patchUpsert(user.id, {first_refund_at: new Date()}, user.toRow());
const claimKey = `${REFUND_ALLOWANCE_CLAIM_PREFIX}:${refund.id}`;
const claim = await getBillingRepository().webhookEvents.tryClaim(claimKey);
if (claim !== 'claimed') {
Logger.debug(
{userId: user.id.toString(), refundId: refund.id, claim},
'Self-serve refund already counted against the user refund allowance',
);
return;
}
const isFirstRefund = !user.firstRefundAt;
const patch: Partial<UserRow> = isFirstRefund
? {first_refund_at: new Date()}
: {premium_flags: user.premiumFlags | PremiumFlags.PURCHASE_DISABLED};
try {
await this.userRepository.patchUpsert(user.id, patch, user.toRow());
} catch (error) {
await getBillingRepository().webhookEvents.releaseClaim(claimKey);
throw error;
}
await getBillingRepository().webhookEvents.markProcessed(claimKey);
Logger.info(
{userId: user.id.toString(), refundId: refund.id, subscriptionId: subscriptionId || null},
'Self-serve refund confirmed succeeded; cooldown and cancellation finalized',
{userId: user.id.toString(), refundId: refund.id, subscriptionId: subscriptionId || null, isFirstRefund},
'Self-serve refund confirmed succeeded; refund allowance and cancellation finalized',
);
}
@@ -10,7 +10,10 @@ import {StripePaymentNotAvailableError} from '@fluxer/errors/src/domains/payment
import {StripeSubscriptionAlreadyCancelingError} from '@fluxer/errors/src/domains/payment/StripeSubscriptionAlreadyCancelingError';
import {StripeSubscriptionNotCancelingError} from '@fluxer/errors/src/domains/payment/StripeSubscriptionNotCancelingError';
import {UnknownUserError} from '@fluxer/errors/src/domains/user/UnknownUserError';
import type {CurrentSubscriptionPriceResponse} from '@fluxer/schema/src/domains/premium/PremiumSchemas';
import type {
CurrentSubscriptionPriceResponse,
SwitchToListPriceResponse,
} from '@fluxer/schema/src/domains/premium/PremiumSchemas';
import type {ICacheService} from '@pkgs/cache/src/ICacheService';
import {seconds} from 'itty-time';
import type Stripe from 'stripe';
@@ -25,7 +28,7 @@ import type {User} from '../../models/User';
import type {IUserRepository} from '../../user/IUserRepository';
import {mapUserToPrivateResponse} from '../../user/UserMappers';
import type {Currency} from '../../utils/CurrencyUtils';
import type {RecurringBillingCycle} from '../ProductRegistry';
import type {ProductInfo, RecurringBillingCycle} from '../ProductRegistry';
import {
getPrimarySubscriptionItem,
getSubscriptionEntitlementPeriodEndUnix,
@@ -36,12 +39,80 @@ import {extractId} from '../StripeUtils';
type BillingCycleChangeEffectiveAt = 'now' | 'period_end';
const CHARGEABLE_SUBSCRIPTION_STATUSES = new Set<Stripe.Subscription.Status>(['active', 'trialing']);
function mapStripeIds(values: Array<string | {id: string}> | null | undefined): Array<string> | null {
const ids = (values ?? []).map((value) => extractId(value)).filter((id): id is string => id !== null);
return ids.length > 0 ? ids : null;
}
function mapSchedulePhaseDiscounts(
discounts:
| Array<{
coupon: string | {id: string} | null;
discount: unknown;
promotion_code: string | {id: string} | null;
}>
| null
| undefined,
): Array<Stripe.SubscriptionScheduleUpdateParams.Phase.Discount> | null {
const mapped = (discounts ?? [])
.map((discount): Stripe.SubscriptionScheduleUpdateParams.Phase.Discount | null => {
const coupon = extractId(discount.coupon);
if (coupon) {
return {coupon};
}
const promotionCode = extractId(discount.promotion_code);
if (promotionCode) {
return {promotion_code: promotionCode};
}
return typeof discount.discount === 'string' ? {discount: discount.discount} : null;
})
.filter((discount): discount is Stripe.SubscriptionScheduleUpdateParams.Phase.Discount => discount !== null);
return mapped.length > 0 ? mapped : null;
}
function mapSchedulePhaseAddInvoiceItems(
addInvoiceItems: Array<Stripe.SubscriptionSchedule.Phase.AddInvoiceItem> | null | undefined,
): Array<Stripe.SubscriptionScheduleUpdateParams.Phase.AddInvoiceItem> | null {
const mapped = (addInvoiceItems ?? [])
.map((addInvoiceItem) => {
const price = extractId(addInvoiceItem.price);
if (!price) {
return null;
}
const mappedItem: Stripe.SubscriptionScheduleUpdateParams.Phase.AddInvoiceItem = {
price,
quantity: addInvoiceItem.quantity ?? 1,
period: addInvoiceItem.period,
};
const taxRates = mapStripeIds(addInvoiceItem.tax_rates);
if (taxRates) {
mappedItem.tax_rates = taxRates;
}
const discounts = mapSchedulePhaseDiscounts(addInvoiceItem.discounts);
if (discounts) {
mappedItem.discounts = discounts;
}
if (addInvoiceItem.metadata) {
mappedItem.metadata = addInvoiceItem.metadata;
}
return mappedItem;
})
.filter(
(addInvoiceItem): addInvoiceItem is Stripe.SubscriptionScheduleUpdateParams.Phase.AddInvoiceItem =>
addInvoiceItem !== null,
);
return mapped.length > 0 ? mapped : null;
}
export class StripeSubscriptionService {
constructor(
private stripe: Stripe | null,
private userRepository: IUserRepository,
private productRegistry: {
getRecurringSubscriptionPriceId: (billingCycle: RecurringBillingCycle, currency: string) => string | null;
getProduct: (priceId: string) => ProductInfo | null;
},
private cacheService: ICacheService,
private gatewayService: IGatewayService,
@@ -251,7 +322,7 @@ export class StripeSubscriptionService {
return;
}
await this.stripe.subscriptionSchedules.release(schedule.id, {
preserve_cancel_date: false,
preserve_cancel_date: Boolean(subscription.cancel_at || subscription.cancel_at_period_end),
});
const releasedSubscription = await this.stripe.subscriptions.retrieve(subscription.id, {
expand: ['items.data.price'],
@@ -268,7 +339,7 @@ export class StripeSubscriptionService {
);
}
const patch: Record<string, unknown> = {
premium_will_cancel: false,
premium_will_cancel: Boolean(releasedSubscription.cancel_at || releasedSubscription.cancel_at_period_end),
};
const computedPremiumUntil = getSubscriptionPremiumPeriodEnd(releasedSubscription);
if (computedPremiumUntil) {
@@ -433,6 +504,142 @@ export class StripeSubscriptionService {
}
}
async switchToCurrentListPrice(userId: UserID): Promise<SwitchToListPriceResponse> {
if (!this.stripe) {
throw new StripePaymentNotAvailableError();
}
const user = await this.userRepository.findUnique(userId);
if (!user) {
throw new UnknownUserError();
}
if (!user.stripeSubscriptionId) {
throw new StripeNoActiveSubscriptionError();
}
const lockKey = `list_price_switch_lock:${user.id}`;
const lockToken = await this.cacheService.acquireLock(
lockKey,
StripeSubscriptionService.LIST_PRICE_SWITCH_LOCK_TTL_SECONDS,
);
if (!lockToken) {
Logger.debug(
{userId, subscriptionId: user.stripeSubscriptionId, lockKey},
'List price switch skipped because another switch is already in flight',
);
return {status: 'ineligible', reason: 'switch_in_progress'};
}
try {
const subscription = await this.stripe.subscriptions.retrieve(user.stripeSubscriptionId, {
expand: ['items.data.price', 'schedule'],
});
const item = getPrimarySubscriptionItem(subscription);
if (!item?.id || !item.price?.recurring || !item.price.currency || item.price.unit_amount == null) {
return {status: 'ineligible', reason: 'unsupported_subscription'};
}
if (!CHARGEABLE_SUBSCRIPTION_STATUSES.has(subscription.status)) {
return {status: 'ineligible', reason: 'subscription_not_chargeable'};
}
if (subscription.cancel_at != null || subscription.cancel_at_period_end) {
return {status: 'ineligible', reason: 'subscription_cancelling'};
}
const billingCycle = this.getBillingCycleFromInterval(item.price.recurring.interval);
if (!billingCycle) {
return {status: 'ineligible', reason: 'unsupported_subscription'};
}
const currency = item.price.currency.toUpperCase() as Currency;
const targetPriceId = this.productRegistry.getRecurringSubscriptionPriceId(billingCycle, currency);
if (!targetPriceId) {
return {status: 'ineligible', reason: 'no_list_price'};
}
if (targetPriceId === item.price.id) {
return {status: 'ineligible', reason: 'already_on_list_price'};
}
const targetProduct = this.productRegistry.getProduct(targetPriceId);
if (!targetProduct || targetProduct.currency !== currency) {
return {status: 'ineligible', reason: 'no_list_price'};
}
const targetAmountMinor = await this.getListPriceAmountMinor(targetPriceId);
const currentAmountMinor = item.price.unit_amount;
if (targetAmountMinor == null) {
return {status: 'ineligible', reason: 'no_list_price'};
}
if (targetAmountMinor >= currentAmountMinor) {
return {status: 'ineligible', reason: 'not_a_price_decrease'};
}
const periodEnd = getSubscriptionEntitlementPeriodEndUnix(subscription, item);
if (!periodEnd || periodEnd <= Math.floor(Date.now() / 1000)) {
return {status: 'ineligible', reason: 'missing_period_end'};
}
const switched = {
effective_at: new Date(periodEnd * 1000).toISOString(),
target_price_id: targetPriceId,
target_amount_minor: targetAmountMinor,
current_amount_minor: currentAmountMinor,
currency,
};
const schedule = await this.loadSubscriptionSchedule(subscription.schedule);
const pendingSchedule =
schedule && (schedule.status === 'active' || schedule.status === 'not_started') ? schedule : null;
if (pendingSchedule) {
if (this.subscriptionScheduleHasFutureTargetPrice(pendingSchedule, targetPriceId)) {
Logger.debug(
{userId, subscriptionId: subscription.id, scheduleId: pendingSchedule.id, targetPriceId},
'List price switch already scheduled for period end',
);
return {status: 'already_scheduled', ...switched};
}
if (pendingSchedule.end_behavior === 'cancel') {
return {status: 'ineligible', reason: 'cancellation_managed_by_schedule'};
}
if (this.subscriptionScheduleHasPendingBillingCycleChange(pendingSchedule, subscription)) {
return {status: 'ineligible', reason: 'conflicting_pending_change'};
}
}
await this.scheduleBillingCycleChangeAtPeriodEnd({
user,
subscription,
item,
currentBillingCycle: billingCycle,
targetBillingCycle: billingCycle,
targetPriceId,
clearCancellation: false,
});
Logger.debug(
{
userId,
subscriptionId: subscription.id,
billingCycle,
currency,
currentPriceId: item.price.id,
targetPriceId,
currentAmountMinor,
targetAmountMinor,
periodEnd,
},
'Subscription switch to current list price scheduled for period end',
);
return {status: 'scheduled', ...switched};
} catch (error: unknown) {
Logger.error(
{error, userId, subscriptionId: user.stripeSubscriptionId},
'Failed to switch subscription to the current list price',
);
if (error instanceof StripeError || error instanceof StripeInvalidProductConfigurationError) {
throw error;
}
const message = error instanceof Error ? error.message : 'Failed to switch subscription to the list price';
throw new StripeError(message);
} finally {
try {
const released = await this.cacheService.releaseLock(lockKey, lockToken);
if (!released) {
Logger.warn({userId, lockKey}, 'List price switch lock token no longer matched on release');
}
} catch (error) {
Logger.error({error, userId, lockKey}, 'Failed to release list price switch lock');
}
}
}
private async scheduleBillingCycleChangeAtPeriodEnd({
user,
subscription,
@@ -440,6 +647,7 @@ export class StripeSubscriptionService {
currentBillingCycle,
targetBillingCycle,
targetPriceId,
clearCancellation = true,
}: {
user: User;
subscription: Stripe.Subscription;
@@ -447,6 +655,7 @@ export class StripeSubscriptionService {
currentBillingCycle: RecurringBillingCycle;
targetBillingCycle: RecurringBillingCycle;
targetPriceId: string;
clearCancellation?: boolean;
}): Promise<void> {
if (!this.stripe) {
throw new StripePaymentNotAvailableError();
@@ -456,7 +665,7 @@ export class StripeSubscriptionService {
throw new StripeError('Subscription is missing a future period end for scheduled billing cycle change');
}
let currentSubscription = subscription;
if (subscription.cancel_at || subscription.cancel_at_period_end) {
if (clearCancellation && (subscription.cancel_at || subscription.cancel_at_period_end)) {
currentSubscription = await this.stripe.subscriptions.update(
subscription.id,
this.getClearCancellationUpdateParams(subscription) ?? {proration_behavior: 'none'},
@@ -491,7 +700,6 @@ export class StripeSubscriptionService {
firstInvoiceCredit?.price_data?.unit_amount != null ? -firstInvoiceCredit.price_data.unit_amount : null;
const targetPhase: Stripe.SubscriptionScheduleUpdateParams.Phase = {
start_date: periodEnd,
billing_cycle_anchor: 'phase_start',
items: [
{
price: targetPriceId,
@@ -500,6 +708,9 @@ export class StripeSubscriptionService {
],
proration_behavior: 'none',
};
if (currentBillingCycle !== targetBillingCycle) {
targetPhase.billing_cycle_anchor = 'phase_start';
}
if (firstInvoiceCredit) {
targetPhase.add_invoice_items = [firstInvoiceCredit];
targetPhase.metadata = {
@@ -508,7 +719,7 @@ export class StripeSubscriptionService {
};
}
await this.stripe.subscriptionSchedules.update(schedule.id, {
end_behavior: 'release',
end_behavior: clearCancellation ? 'release' : schedule.end_behavior,
proration_behavior: 'none',
metadata: {
user_id: user.id.toString(),
@@ -516,14 +727,33 @@ export class StripeSubscriptionService {
},
phases: [currentPhase, targetPhase],
});
let scheduledSubscription = currentSubscription;
if (!clearCancellation) {
scheduledSubscription = await this.stripe.subscriptions.retrieve(currentSubscription.id, {
expand: ['items.data.price'],
});
try {
await getBillingRepository().subscriptions.upsertFromStripe(scheduledSubscription, {
knownUserId: user.id,
snapshotCapturedAt: new Date(),
});
} catch (mirrorErr) {
Logger.error(
{mirrorErr, subId: scheduledSubscription.id},
'Mirror upsert failed after scheduling a period-end price change; reconciler will heal',
);
}
}
const patch: Record<string, unknown> = {
premium_will_cancel: false,
premium_will_cancel: clearCancellation
? false
: Boolean(scheduledSubscription.cancel_at || scheduledSubscription.cancel_at_period_end),
};
const computedPremiumUntil = getSubscriptionPremiumPeriodEnd(currentSubscription);
const computedPremiumUntil = getSubscriptionPremiumPeriodEnd(scheduledSubscription);
if (computedPremiumUntil) {
patch['premium_until'] = computedPremiumUntil;
}
const updatedCustomerId = extractId(currentSubscription.customer);
const updatedCustomerId = extractId(scheduledSubscription.customer);
if (updatedCustomerId && updatedCustomerId !== user.stripeCustomerId) {
patch['stripe_customer_id'] = updatedCustomerId;
}
@@ -532,7 +762,7 @@ export class StripeSubscriptionService {
Logger.debug(
{
userId: user.id,
subscriptionId: currentSubscription.id,
subscriptionId: scheduledSubscription.id,
scheduleId: schedule.id,
fromBillingCycle: currentBillingCycle,
toBillingCycle: targetBillingCycle,
@@ -742,12 +972,28 @@ export class StripeSubscriptionService {
if (!price) {
return null;
}
return {
const mappedItem: Stripe.SubscriptionScheduleUpdateParams.Phase.Item = {
price,
quantity: phaseItem.quantity ?? 1,
};
const itemTaxRates = mapStripeIds(phaseItem.tax_rates);
if (itemTaxRates) {
mappedItem.tax_rates = itemTaxRates;
}
const itemDiscounts = mapSchedulePhaseDiscounts(phaseItem.discounts);
if (itemDiscounts) {
mappedItem.discounts = itemDiscounts;
}
if (phaseItem.metadata) {
mappedItem.metadata = phaseItem.metadata;
}
if (phaseItem.billing_thresholds?.usage_gte != null) {
mappedItem.billing_thresholds = {usage_gte: phaseItem.billing_thresholds.usage_gte};
}
return mappedItem;
})
.filter((phaseItem): phaseItem is {price: string; quantity: number} => phaseItem !== null) ?? [];
.filter((phaseItem): phaseItem is Stripe.SubscriptionScheduleUpdateParams.Phase.Item => phaseItem !== null) ??
[];
const currentPhase: Stripe.SubscriptionScheduleUpdateParams.Phase = {
start_date: phase?.start_date ?? subscription.start_date ?? subscription.created,
end_date: periodEnd,
@@ -762,6 +1008,99 @@ export class StripeSubscriptionService {
],
proration_behavior: 'none',
};
if (phase) {
const phaseDiscounts = mapSchedulePhaseDiscounts(phase.discounts);
if (phaseDiscounts) {
currentPhase.discounts = phaseDiscounts;
}
const defaultTaxRates = mapStripeIds(phase.default_tax_rates);
if (defaultTaxRates) {
currentPhase.default_tax_rates = defaultTaxRates;
}
if (phase.metadata) {
currentPhase.metadata = phase.metadata;
}
if (phase.currency) {
currentPhase.currency = phase.currency;
}
if (phase.description != null) {
currentPhase.description = phase.description;
}
if (phase.collection_method) {
currentPhase.collection_method = phase.collection_method;
}
if (phase.application_fee_percent != null) {
currentPhase.application_fee_percent = phase.application_fee_percent;
}
const defaultPaymentMethod = extractId(phase.default_payment_method);
if (defaultPaymentMethod) {
currentPhase.default_payment_method = defaultPaymentMethod;
}
const onBehalfOf = extractId(phase.on_behalf_of);
if (onBehalfOf) {
currentPhase.on_behalf_of = onBehalfOf;
}
if (phase.automatic_tax) {
const automaticTax: Stripe.SubscriptionScheduleUpdateParams.Phase.AutomaticTax = {
enabled: phase.automatic_tax.enabled,
};
if (phase.automatic_tax.liability) {
automaticTax.liability = {type: phase.automatic_tax.liability.type};
const liabilityAccount = extractId(phase.automatic_tax.liability.account);
if (liabilityAccount) {
automaticTax.liability.account = liabilityAccount;
}
}
currentPhase.automatic_tax = automaticTax;
}
if (phase.invoice_settings) {
const invoiceSettings: Stripe.SubscriptionScheduleUpdateParams.Phase.InvoiceSettings = {};
const accountTaxIds = mapStripeIds(phase.invoice_settings.account_tax_ids);
if (accountTaxIds) {
invoiceSettings.account_tax_ids = accountTaxIds;
}
if (phase.invoice_settings.days_until_due != null) {
invoiceSettings.days_until_due = phase.invoice_settings.days_until_due;
}
if (phase.invoice_settings.issuer) {
invoiceSettings.issuer = {type: phase.invoice_settings.issuer.type};
const issuerAccount = extractId(phase.invoice_settings.issuer.account);
if (issuerAccount) {
invoiceSettings.issuer.account = issuerAccount;
}
}
if (Object.keys(invoiceSettings).length > 0) {
currentPhase.invoice_settings = invoiceSettings;
}
}
if (phase.billing_thresholds) {
const billingThresholds: Stripe.SubscriptionScheduleUpdateParams.Phase.BillingThresholds = {};
if (phase.billing_thresholds.amount_gte != null) {
billingThresholds.amount_gte = phase.billing_thresholds.amount_gte;
}
if (phase.billing_thresholds.reset_billing_cycle_anchor != null) {
billingThresholds.reset_billing_cycle_anchor = phase.billing_thresholds.reset_billing_cycle_anchor;
}
if (Object.keys(billingThresholds).length > 0) {
currentPhase.billing_thresholds = billingThresholds;
}
}
if (phase.transfer_data) {
const destination = extractId(phase.transfer_data.destination);
if (destination) {
currentPhase.transfer_data = {destination};
if (phase.transfer_data.amount_percent != null) {
currentPhase.transfer_data.amount_percent = phase.transfer_data.amount_percent;
}
}
}
if (phase.start_date > now) {
const addInvoiceItems = mapSchedulePhaseAddInvoiceItems(phase.add_invoice_items);
if (addInvoiceItems) {
currentPhase.add_invoice_items = addInvoiceItems;
}
}
}
const trialEnd = subscription.trial_end;
if (trialEnd != null && trialEnd > now) {
if (trialEnd >= periodEnd) {
@@ -770,6 +1109,10 @@ export class StripeSubscriptionService {
currentPhase.trial_end = trialEnd;
}
}
const phaseIsTrial = currentPhase.trial === true || currentPhase.trial_end != null;
if (phase?.billing_cycle_anchor && !(phaseIsTrial && phase.billing_cycle_anchor === 'phase_start')) {
currentPhase.billing_cycle_anchor = phase.billing_cycle_anchor;
}
return currentPhase;
}
@@ -1026,6 +1369,7 @@ export class StripeSubscriptionService {
private static readonly CURRENT_PRICE_CACHE_TTL_SECONDS = seconds('5 minutes');
private static readonly LIST_PRICE_CACHE_TTL_SECONDS = seconds('1 hour');
private static readonly PRICE_CACHE_PRODUCE_TIMEOUT_MS = 90000;
private static readonly LIST_PRICE_SWITCH_LOCK_TTL_SECONDS = seconds('30 seconds');
private static readonly USER_TRIAL_LOCK_TTL_SECONDS = seconds('30 seconds');
private static readonly USER_TRIAL_LOCK_MAX_WAIT_MS = 15000;
private static readonly USER_TRIAL_LOCK_RETRY_DELAY_MS = 100;
@@ -65,6 +65,14 @@ export class StripeSubscriptionWebhookHandler {
Logger.error({invoiceId: invoice.id, billingReason}, 'No subscription ID found in subscription invoice');
throw new StripeError('Invoice missing subscription id');
}
const donor = await this.donationRepository.findDonorByStripeSubscriptionId(subscriptionId);
if (donor) {
Logger.debug(
{invoiceId: invoice.id, eventId, subscriptionId, donorEmail: donor.email},
'Skipping invoice payment for donation subscription',
);
return;
}
if (this.isSubscriptionUpdateInvoice(invoice)) {
Logger.debug(
{
@@ -309,6 +309,10 @@ export class StripeWebhookService {
await this.safeMirrorUpsert(event, () => this.billingRepository.paymentMethods.markDetached(pm.id, new Date()));
break;
}
case 'mandate.updated': {
await this.handleMandateUpdated(event.data.object as Stripe.Mandate);
break;
}
case 'payment_intent.created':
case 'payment_intent.processing':
case 'payment_intent.succeeded':
@@ -369,6 +373,18 @@ export class StripeWebhookService {
}
}
private async handleMandateUpdated(mandate: Stripe.Mandate): Promise<void> {
if (mandate.status !== 'inactive') {
return;
}
const paymentMethodId =
typeof mandate.payment_method === 'string' ? mandate.payment_method : (mandate.payment_method?.id ?? null);
Logger.warn(
{mandateId: mandate.id, paymentMethodId, status: mandate.status},
'Stripe mandate is no longer active; recurring payments on this payment method will fail',
);
}
private async resolveRefundCustomerId(refund: Stripe.Refund): Promise<string | null> {
const chargeId = typeof refund.charge === 'string' ? refund.charge : (refund.charge?.id ?? null);
if (chargeId !== null) {
@@ -0,0 +1,370 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {UserPremiumTypes} from '@fluxer/constants/src/UserConstants';
import {afterAll, beforeAll, describe, expect, test} from 'vitest';
import {Config} from '../../Config';
import {ProductRegistry, ProductType} from '../ProductRegistry';
// Mirrors the live BRL/TRY configuration after the repricing: the BRL slots point at the new list
// prices, TRY is not configured at all (production has no TRY prices), and every retired price id
// lives only in the legacy map.
const MOCK_PRICES = {
monthlyUsd: 'price_monthly_usd',
monthlyBrl: 'price_1TMbqsFPC94Os7FdfElrIIaZ',
yearlyUsd: 'price_yearly_usd',
yearlyBrl: 'price_1TMbqtFPC94Os7Fd7VJqmDyt',
gift1MonthBrl: 'price_gift_1_month_brl',
gift1YearBrl: 'price_gift_1_year_brl',
};
const LEGACY_MONTHLY_BRL = 'price_legacy_monthly_brl';
const LEGACY_YEARLY_BRL = 'price_legacy_yearly_brl';
const LEGACY_GIFT_1_MONTH_BRL = 'price_legacy_gift_1_month_brl';
const LEGACY_GIFT_1_YEAR_BRL = 'price_legacy_gift_1_year_brl';
const LEGACY_MONTHLY_TRY = 'price_1TMYpdFPC94Os7FdZVRx98Up';
const MOCK_LEGACY_PRICES: Record<string, Array<string> | undefined> = {
monthly_brl: [LEGACY_MONTHLY_BRL],
yearly_brl: [LEGACY_YEARLY_BRL],
gift_1_month_brl: [LEGACY_GIFT_1_MONTH_BRL],
gift_1_year_brl: [LEGACY_GIFT_1_YEAR_BRL],
monthly_try: [LEGACY_MONTHLY_TRY],
};
describe('ProductRegistry - legacy prices', () => {
let originalPrices: typeof Config.stripe.prices | undefined;
let originalLegacyPrices: typeof Config.stripe.legacyPrices | undefined;
beforeAll(() => {
originalPrices = Config.stripe.prices;
originalLegacyPrices = Config.stripe.legacyPrices;
});
afterAll(() => {
Config.stripe.prices = originalPrices;
Config.stripe.legacyPrices = originalLegacyPrices;
});
function buildRegistry(
prices: typeof Config.stripe.prices,
legacyPrices: typeof Config.stripe.legacyPrices,
): ProductRegistry {
Config.stripe.prices = prices;
Config.stripe.legacyPrices = legacyPrices;
return new ProductRegistry();
}
describe('slot shapes', () => {
test('a legacy monthly slot resolves to a monthly recurring product in its currency', () => {
const registry = buildRegistry(MOCK_PRICES, MOCK_LEGACY_PRICES);
expect(registry.getProduct(LEGACY_MONTHLY_BRL)).toEqual({
type: ProductType.MONTHLY_SUBSCRIPTION,
premiumType: UserPremiumTypes.SUBSCRIPTION,
durationMonths: 1,
isGift: false,
currency: 'BRL',
billingCycle: 'monthly',
});
});
test('a legacy yearly slot resolves to a yearly recurring product in its currency', () => {
const registry = buildRegistry(MOCK_PRICES, MOCK_LEGACY_PRICES);
expect(registry.getProduct(LEGACY_YEARLY_BRL)).toEqual({
type: ProductType.YEARLY_SUBSCRIPTION,
premiumType: UserPremiumTypes.SUBSCRIPTION,
durationMonths: 12,
isGift: false,
currency: 'BRL',
billingCycle: 'yearly',
});
});
test('a legacy gift_1_month slot resolves to a one-month gift with no billing cycle', () => {
const registry = buildRegistry(MOCK_PRICES, MOCK_LEGACY_PRICES);
const info = registry.getProduct(LEGACY_GIFT_1_MONTH_BRL);
expect(info).toEqual({
type: ProductType.GIFT_1_MONTH,
premiumType: UserPremiumTypes.SUBSCRIPTION,
durationMonths: 1,
isGift: true,
currency: 'BRL',
});
expect(info?.billingCycle).toBeUndefined();
});
test('a legacy gift_1_year slot resolves to a one-year gift with no billing cycle', () => {
const registry = buildRegistry(MOCK_PRICES, MOCK_LEGACY_PRICES);
const info = registry.getProduct(LEGACY_GIFT_1_YEAR_BRL);
expect(info).toEqual({
type: ProductType.GIFT_1_YEAR,
premiumType: UserPremiumTypes.SUBSCRIPTION,
durationMonths: 12,
isGift: true,
currency: 'BRL',
});
expect(info?.billingCycle).toBeUndefined();
});
test('the currency comes from the slot suffix, not from the price id', () => {
const registry = buildRegistry(MOCK_PRICES, MOCK_LEGACY_PRICES);
// Same shape of id, different slot: only the slot suffix decides the currency.
expect(registry.getProduct(LEGACY_MONTHLY_TRY)?.currency).toBe('TRY');
expect(registry.getProduct(LEGACY_MONTHLY_BRL)?.currency).toBe('BRL');
});
test('every supported currency suffix is recognised', () => {
const registry = buildRegistry(
{},
{
monthly_usd: ['legacy_usd'],
monthly_eur: ['legacy_eur'],
monthly_brl: ['legacy_brl'],
monthly_inr: ['legacy_inr'],
monthly_pln: ['legacy_pln'],
monthly_try: ['legacy_try'],
},
);
expect(registry.getProduct('legacy_usd')?.currency).toBe('USD');
expect(registry.getProduct('legacy_eur')?.currency).toBe('EUR');
expect(registry.getProduct('legacy_brl')?.currency).toBe('BRL');
expect(registry.getProduct('legacy_inr')?.currency).toBe('INR');
expect(registry.getProduct('legacy_pln')?.currency).toBe('PLN');
expect(registry.getProduct('legacy_try')?.currency).toBe('TRY');
});
test('an uppercased currency suffix is still recognised', () => {
const registry = buildRegistry({}, {monthly_BRL: ['legacy_upper_brl']});
expect(registry.getProduct('legacy_upper_brl')?.currency).toBe('BRL');
});
test('a slot may retire more than one price id', () => {
const registry = buildRegistry({}, {monthly_brl: ['legacy_one', 'legacy_two', 'legacy_three']});
expect(registry.getProduct('legacy_one')?.type).toBe(ProductType.MONTHLY_SUBSCRIPTION);
expect(registry.getProduct('legacy_two')?.type).toBe(ProductType.MONTHLY_SUBSCRIPTION);
expect(registry.getProduct('legacy_three')?.type).toBe(ProductType.MONTHLY_SUBSCRIPTION);
});
test('legacy prices register even when no prices are configured at all', () => {
const registry = buildRegistry(undefined, MOCK_LEGACY_PRICES);
expect(registry.getProduct(LEGACY_MONTHLY_BRL)?.type).toBe(ProductType.MONTHLY_SUBSCRIPTION);
expect(registry.getProduct(MOCK_PRICES.monthlyBrl)).toBeNull();
});
test('an unknown price id still resolves to null', () => {
const registry = buildRegistry(MOCK_PRICES, MOCK_LEGACY_PRICES);
expect(registry.getProduct('price_never_seen')).toBeNull();
});
});
describe('honoured but unpurchasable', () => {
test('a legacy recurring price resolves through getProduct but is never the checkout price', () => {
const registry = buildRegistry(MOCK_PRICES, MOCK_LEGACY_PRICES);
// Direction 1: the retired price is honoured on renewal.
expect(registry.getProduct(LEGACY_MONTHLY_BRL)).not.toBeNull();
expect(registry.getProduct(LEGACY_YEARLY_BRL)).not.toBeNull();
// Direction 2: checkout only ever offers the authored list price.
expect(registry.getRecurringSubscriptionPriceId('monthly', 'BRL')).toBe(MOCK_PRICES.monthlyBrl);
expect(registry.getRecurringSubscriptionPriceId('monthly', 'BRL')).not.toBe(LEGACY_MONTHLY_BRL);
expect(registry.getRecurringSubscriptionPriceId('yearly', 'BRL')).toBe(MOCK_PRICES.yearlyBrl);
expect(registry.getRecurringSubscriptionPriceId('yearly', 'BRL')).not.toBe(LEGACY_YEARLY_BRL);
});
test('a legacy gift price resolves through getProduct but is never the gift checkout price', () => {
const registry = buildRegistry(MOCK_PRICES, MOCK_LEGACY_PRICES);
expect(registry.getProduct(LEGACY_GIFT_1_MONTH_BRL)).not.toBeNull();
expect(registry.getProduct(LEGACY_GIFT_1_YEAR_BRL)).not.toBeNull();
expect(registry.getGiftPriceId('gift_1_month', 'BRL')).toBe(MOCK_PRICES.gift1MonthBrl);
expect(registry.getGiftPriceId('gift_1_month', 'BRL')).not.toBe(LEGACY_GIFT_1_MONTH_BRL);
expect(registry.getGiftPriceId('gift_1_year', 'BRL')).toBe(MOCK_PRICES.gift1YearBrl);
expect(registry.getGiftPriceId('gift_1_year', 'BRL')).not.toBe(LEGACY_GIFT_1_YEAR_BRL);
});
test('an archived price in a currency the instance does not configure renews but cannot be bought', () => {
// The real production case: one live subscription sits on an archived TRY price that
// production never configured. getProduct must resolve it; checkout must still refuse TRY.
const registry = buildRegistry(MOCK_PRICES, MOCK_LEGACY_PRICES);
expect(registry.getProduct(LEGACY_MONTHLY_TRY)).toEqual({
type: ProductType.MONTHLY_SUBSCRIPTION,
premiumType: UserPremiumTypes.SUBSCRIPTION,
durationMonths: 1,
isGift: false,
currency: 'TRY',
billingCycle: 'monthly',
});
expect(registry.getRecurringSubscriptionPriceId('monthly', 'TRY')).toBeNull();
expect(registry.getRecurringSubscriptionPriceId('yearly', 'TRY')).toBeNull();
expect(registry.getGiftPriceId('gift_1_month', 'TRY')).toBeNull();
expect(registry.getGiftPriceId('gift_1_year', 'TRY')).toBeNull();
});
test('no legacy price id is ever returned by either price getter, for any cycle or currency', () => {
const registry = buildRegistry(MOCK_PRICES, MOCK_LEGACY_PRICES);
const legacyIds = new Set(Object.values(MOCK_LEGACY_PRICES).flatMap((ids) => ids ?? []));
const currencies = ['USD', 'EUR', 'BRL', 'INR', 'PLN', 'TRY'];
const offered: Array<string> = [];
for (const currency of currencies) {
for (const cycle of ['monthly', 'yearly'] as const) {
const priceId = registry.getRecurringSubscriptionPriceId(cycle, currency);
if (priceId) offered.push(priceId);
}
for (const duration of ['gift_1_month', 'gift_1_year'] as const) {
const priceId = registry.getGiftPriceId(duration, currency);
if (priceId) offered.push(priceId);
}
}
expect(offered.length).toBeGreaterThan(0);
expect(offered.filter((priceId) => legacyIds.has(priceId))).toEqual([]);
// And every legacy id is still honoured by the registry.
for (const legacyId of legacyIds) {
expect(registry.getProduct(legacyId)).not.toBeNull();
}
});
test('a legacy recurring product is still classified as a recurring subscription', () => {
const registry = buildRegistry(MOCK_PRICES, MOCK_LEGACY_PRICES);
const recurring = registry.getProduct(LEGACY_MONTHLY_BRL);
const gift = registry.getProduct(LEGACY_GIFT_1_YEAR_BRL);
expect(recurring).not.toBeNull();
expect(gift).not.toBeNull();
expect(registry.isRecurringSubscription(recurring!)).toBe(true);
expect(registry.isRecurringSubscription(gift!)).toBe(false);
});
});
describe('precedence', () => {
test('an authored price wins over a legacy entry claiming the same id', () => {
// Deliberate collision: the live monthly BRL list price is also listed as a retired yearly
// USD price. The authored ProductInfo must survive.
const registry = buildRegistry(MOCK_PRICES, {
yearly_usd: [MOCK_PRICES.monthlyBrl],
});
expect(registry.getProduct(MOCK_PRICES.monthlyBrl)).toEqual({
type: ProductType.MONTHLY_SUBSCRIPTION,
premiumType: UserPremiumTypes.SUBSCRIPTION,
durationMonths: 1,
isGift: false,
currency: 'BRL',
billingCycle: 'monthly',
});
});
test('an authored gift price wins over a legacy entry claiming the same id', () => {
const registry = buildRegistry(MOCK_PRICES, {
monthly_try: [MOCK_PRICES.gift1YearBrl],
});
expect(registry.getProduct(MOCK_PRICES.gift1YearBrl)).toEqual({
type: ProductType.GIFT_1_YEAR,
premiumType: UserPremiumTypes.SUBSCRIPTION,
durationMonths: 12,
isGift: true,
currency: 'BRL',
});
});
test('the first legacy slot to claim an id wins over a later one', () => {
const registry = buildRegistry(
{},
{
monthly_brl: ['legacy_shared'],
yearly_usd: ['legacy_shared'],
},
);
expect(registry.getProduct('legacy_shared')).toEqual({
type: ProductType.MONTHLY_SUBSCRIPTION,
premiumType: UserPremiumTypes.SUBSCRIPTION,
durationMonths: 1,
isGift: false,
currency: 'BRL',
billingCycle: 'monthly',
});
});
});
describe('malformed configuration', () => {
test('a slot whose value is a bare string is ignored and the rest still registers', () => {
const registry = buildRegistry(MOCK_PRICES, {
monthly_brl: 'price_legacy_bare_string' as unknown as Array<string>,
yearly_brl: [LEGACY_YEARLY_BRL],
});
expect(registry.getProduct('price_legacy_bare_string')).toBeNull();
// Not accidentally registered character by character either.
expect(registry.getProduct('p')).toBeNull();
expect(registry.getProduct(LEGACY_YEARLY_BRL)?.type).toBe(ProductType.YEARLY_SUBSCRIPTION);
});
test('a slot with an unrecognised name is ignored and the rest still registers', () => {
const registry = buildRegistry(MOCK_PRICES, {
weekly_brl: ['legacy_weekly'],
visionary_usd: ['legacy_visionary'],
brl: ['legacy_no_separator'],
_brl: ['legacy_leading_separator'],
yearly_brl: [LEGACY_YEARLY_BRL],
});
expect(registry.getProduct('legacy_weekly')).toBeNull();
expect(registry.getProduct('legacy_visionary')).toBeNull();
expect(registry.getProduct('legacy_no_separator')).toBeNull();
expect(registry.getProduct('legacy_leading_separator')).toBeNull();
expect(registry.getProduct(LEGACY_YEARLY_BRL)?.type).toBe(ProductType.YEARLY_SUBSCRIPTION);
});
test('a slot with an unknown currency is ignored and the rest still registers', () => {
const registry = buildRegistry(MOCK_PRICES, {
monthly_jpy: ['legacy_jpy'],
gift_1_year_gbp: ['legacy_gbp'],
monthly_brl: [LEGACY_MONTHLY_BRL],
});
expect(registry.getProduct('legacy_jpy')).toBeNull();
expect(registry.getProduct('legacy_gbp')).toBeNull();
expect(registry.getProduct(LEGACY_MONTHLY_BRL)?.currency).toBe('BRL');
});
test('empty, blank and undefined entries inside a slot are skipped', () => {
const registry = buildRegistry(MOCK_PRICES, {
monthly_brl: ['', undefined as unknown as string, LEGACY_MONTHLY_BRL],
});
expect(registry.getProduct('')).toBeNull();
expect(registry.getProduct(LEGACY_MONTHLY_BRL)?.currency).toBe('BRL');
});
test('an undefined slot value is ignored', () => {
const registry = buildRegistry(MOCK_PRICES, {
monthly_try: undefined,
monthly_brl: [LEGACY_MONTHLY_BRL],
});
expect(registry.getProduct(LEGACY_MONTHLY_BRL)?.currency).toBe('BRL');
});
test('an array-valued legacy map is ignored without throwing', () => {
const registry = buildRegistry(MOCK_PRICES, [
'price_legacy_array',
] as unknown as typeof Config.stripe.legacyPrices);
expect(registry.getProduct('price_legacy_array')).toBeNull();
// The authored prices are untouched.
expect(registry.getProduct(MOCK_PRICES.monthlyBrl)?.currency).toBe('BRL');
expect(registry.getRecurringSubscriptionPriceId('monthly', 'BRL')).toBe(MOCK_PRICES.monthlyBrl);
});
test('a string-valued legacy map is ignored without throwing', () => {
const registry = buildRegistry(MOCK_PRICES, 'nonsense' as unknown as typeof Config.stripe.legacyPrices);
expect(registry.getProduct('nonsense')).toBeNull();
expect(registry.getProduct(MOCK_PRICES.monthlyBrl)?.currency).toBe('BRL');
});
test('a number-valued legacy map is ignored without throwing', () => {
const registry = buildRegistry(MOCK_PRICES, 42 as unknown as typeof Config.stripe.legacyPrices);
expect(registry.getProduct(MOCK_PRICES.monthlyBrl)?.currency).toBe('BRL');
});
test('an absent or empty legacy map leaves the authored registry intact', () => {
expect(buildRegistry(MOCK_PRICES, undefined).getProduct(MOCK_PRICES.monthlyBrl)?.currency).toBe('BRL');
expect(buildRegistry(MOCK_PRICES, {}).getProduct(MOCK_PRICES.monthlyBrl)?.currency).toBe('BRL');
});
});
});
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import crypto from 'node:crypto';
import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';
import {PremiumFlags} from '@fluxer/constants/src/UserConstants';
import type {
SelfServeRefundEligibilityResponse,
SelfServeRefundResponse,
@@ -9,10 +11,17 @@ import {HttpResponse, http} from 'msw';
import {afterAll, afterEach, beforeAll, beforeEach, describe, expect, test} from 'vitest';
import {createTestAccount, type TestAccount} from '../../auth/tests/AuthTestUtils';
import {createUserID} from '../../BrandedTypes';
import {Config} from '../../Config';
import {type ApiTestHarness, createApiTestHarness} from '../../test/ApiTestHarness';
import {createStripeApiHandlers} from '../../test/msw/handlers/StripeApiHandlers';
import {
createMockWebhookPayload,
createStripeApiHandlers,
type StripeWebhookEventData,
} from '../../test/msw/handlers/StripeApiHandlers';
import {server} from '../../test/msw/server';
import {createBuilder} from '../../test/TestRequestBuilder';
import {UserRepository} from '../../user/repositories/UserRepository';
import {setupSyncStripeWebhookWorker} from './StripeWebhookTestUtils';
const MOCK_CUSTOMER_ID = 'cus_self_serve_refund';
const MOCK_SUBSCRIPTION_ID = 'sub_self_serve_refund';
@@ -107,6 +116,7 @@ function invoiceListHandler(invoices: ReadonlyArray<MockStripeInvoice>) {
}
function refundCreateHandler(opts?: {
refundId?: string;
status?: 'succeeded' | 'pending' | 'failed';
failureReason?: string;
onRequest?: (idempotencyKey: string | null) => void;
@@ -121,7 +131,7 @@ function refundCreateHandler(opts?: {
if (match) metadata[match[1]] = value as string;
}
return HttpResponse.json({
id: 're_test_self_serve',
id: opts?.refundId ?? 're_test_self_serve',
object: 'refund',
amount: Number.parseInt((params.amount as string) ?? '0', 10),
currency: 'usd',
@@ -156,7 +166,27 @@ describe('StripeRefundService self-serve refund', () => {
let harness: ApiTestHarness;
beforeAll(async () => {
harness = await createApiTestHarness();
setupSyncStripeWebhookWorker();
});
function createWebhookSignature(payload: string, timestamp: number, secret: string): string {
const signedPayload = `${timestamp}.${payload}`;
const signature = crypto.createHmac('sha256', secret).update(signedPayload).digest('hex');
return `t=${timestamp},v1=${signature}`;
}
async function sendWebhook(eventData: StripeWebhookEventData): Promise<{
received: boolean;
}> {
const {payload, timestamp} = createMockWebhookPayload(eventData);
const signature = createWebhookSignature(payload, timestamp, Config.stripe.webhookSecret!);
return createBuilder<{
received: boolean;
}>(harness, '')
.post('/stripe/webhook')
.header('stripe-signature', signature)
.header('content-type', 'application/json')
.body(payload)
.execute();
}
beforeEach(async () => {
await harness.reset();
});
@@ -345,6 +375,102 @@ describe('StripeRefundService self-serve refund', () => {
const updatedUser = await new UserRepository().findUnique(createUserID(BigInt(account.userId)));
expect(updatedUser!.firstRefundAt).toBeNull();
});
test('counts one self-serve refund once even when charge.refunded arrives afterwards and is retried', async () => {
server.use(...createStripeApiHandlers().handlers);
const invoice = buildInvoice({
id: 'in_cross_path',
paidAtSecondsAgo: SECONDS_PER_DAY,
subscriptionId: null,
});
server.use(invoiceListHandler([invoice]), refundCreateHandler({refundId: 're_cross_path'}));
const account = await createTestAccount(harness);
const userId = createUserID(BigInt(account.userId));
await setStripeIds(harness, account, {stripe_customer_id: MOCK_CUSTOMER_ID});
const response = await createBuilder<SelfServeRefundResponse>(harness, account.token)
.post('/premium/refund-latest')
.execute();
expect(response.refund_id).toBe('re_cross_path');
expect(response.status).toBe('succeeded');
const userRepository = new UserRepository();
const afterSelfServe = await userRepository.findUnique(userId);
expect(afterSelfServe!.firstRefundAt).not.toBeNull();
expect(afterSelfServe!.premiumFlags & PremiumFlags.PURCHASE_DISABLED).toBe(0);
const chargeEvent = {
type: 'charge.refunded' as const,
data: {
object: {
id: invoice.chargeId,
payment_intent: invoice.paymentIntentId,
customer: MOCK_CUSTOMER_ID,
amount_refunded: 2500,
refunds: {
object: 'list',
has_more: false,
url: `/v1/charges/${invoice.chargeId}/refunds`,
data: [
{
id: 're_cross_path',
object: 'refund',
charge: invoice.chargeId,
payment_intent: invoice.paymentIntentId,
amount: 2500,
currency: 'usd',
status: 'succeeded',
created: Math.floor(Date.now() / 1000),
metadata: {
refund_kind: 'self_serve',
user_id: account.userId,
invoice_id: invoice.id,
},
},
],
},
},
},
};
await sendWebhook({...chargeEvent, id: 'evt_cross_path_1'});
await sendWebhook({...chargeEvent, id: 'evt_cross_path_2'});
const afterWebhook = await userRepository.findUnique(userId);
expect(afterWebhook!.firstRefundAt!.getTime()).toBe(afterSelfServe!.firstRefundAt!.getTime());
expect(afterWebhook!.premiumFlags & PremiumFlags.PURCHASE_DISABLED).toBe(0);
});
test('counts one self-serve refund once even when refund.updated confirms it after the endpoint already did', async () => {
server.use(...createStripeApiHandlers().handlers);
const invoice = buildInvoice({
id: 'in_double_confirm',
paidAtSecondsAgo: SECONDS_PER_DAY,
subscriptionId: null,
});
server.use(invoiceListHandler([invoice]), refundCreateHandler({refundId: 're_double_confirm'}));
const account = await createTestAccount(harness);
const userId = createUserID(BigInt(account.userId));
await setStripeIds(harness, account, {stripe_customer_id: MOCK_CUSTOMER_ID});
await createBuilder<SelfServeRefundResponse>(harness, account.token).post('/premium/refund-latest').execute();
const userRepository = new UserRepository();
const afterSelfServe = await userRepository.findUnique(userId);
expect(afterSelfServe!.firstRefundAt).not.toBeNull();
await sendWebhook({
id: 'evt_double_confirm_1',
type: 'refund.updated',
data: {
object: {
id: 're_double_confirm',
object: 'refund',
status: 'succeeded',
amount: 2500,
currency: 'usd',
metadata: {
refund_kind: 'self_serve',
user_id: account.userId,
invoice_id: invoice.id,
},
},
},
});
const afterWebhook = await userRepository.findUnique(userId);
expect(afterWebhook!.firstRefundAt!.getTime()).toBe(afterSelfServe!.firstRefundAt!.getTime());
expect(afterWebhook!.premiumFlags & PremiumFlags.PURCHASE_DISABLED).toBe(0);
});
test('retries with a fresh idempotency key once a prior attempt has failed at the provider', async () => {
server.use(...createStripeApiHandlers().handlers);
server.use(invoiceListHandler([buildInvoice({id: 'in_recent', paidAtSecondsAgo: SECONDS_PER_DAY})]));
@@ -0,0 +1,589 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';
import type {
ListPriceSwitchIneligibilityReason,
PremiumStateResponse,
SwitchToListPriceResponse,
} from '@fluxer/schema/src/domains/premium/PremiumSchemas';
import {HttpResponse, http} from 'msw';
import Stripe from 'stripe';
import {afterAll, beforeAll, beforeEach, describe, expect, test} from 'vitest';
import {createTestAccount} from '../../auth/tests/AuthTestUtils';
import {createUserID, type UserID} from '../../BrandedTypes';
import {Config} from '../../Config';
import {getBillingRepository} from '../../middleware/ServiceRegistry';
import {type ApiTestHarness, createApiTestHarness} from '../../test/ApiTestHarness';
import {createPwnedPasswordsRangeHandler} from '../../test/msw/handlers/PwnedPasswordsHandlers';
import {createStripeApiHandlers, type StripeApiHandlers} from '../../test/msw/handlers/StripeApiHandlers';
import {server} from '../../test/msw/server';
import {createBuilder} from '../../test/TestRequestBuilder';
import {STRIPE_API_VERSION} from '../StripeApiVersion';
const MOCK_PRICES = {
monthlyUsd: 'price_list_monthly_usd',
yearlyUsd: 'price_list_yearly_usd',
monthlyBrl: 'price_list_monthly_brl',
yearlyBrl: 'price_list_yearly_brl',
};
const RETIRED_MONTHLY_BRL = 'price_retired_monthly_brl';
const RETIRED_CHEAP_MONTHLY_BRL = 'price_retired_cheap_monthly_brl';
const RETIRED_MONTHLY_TRY = 'price_retired_monthly_try';
const RETIRED_EQUAL_MONTHLY_BRL = 'price_retired_equal_monthly_brl';
const LIST_MONTHLY_BRL_MINOR = 1890;
const LIST_YEARLY_BRL_MINOR = 18900;
const RETIRED_MONTHLY_BRL_MINOR = 2499;
const RETIRED_CHEAP_MONTHLY_BRL_MINOR = 990;
const RETIRED_MONTHLY_TRY_MINOR = 2999;
const MOCK_PRICE_SEEDS: Record<
string,
{
unit_amount: number;
currency: string;
interval: 'month' | 'year';
}
> = {
[MOCK_PRICES.monthlyUsd]: {unit_amount: 499, currency: 'usd', interval: 'month'},
[MOCK_PRICES.yearlyUsd]: {unit_amount: 4999, currency: 'usd', interval: 'year'},
[MOCK_PRICES.monthlyBrl]: {unit_amount: LIST_MONTHLY_BRL_MINOR, currency: 'brl', interval: 'month'},
[MOCK_PRICES.yearlyBrl]: {unit_amount: LIST_YEARLY_BRL_MINOR, currency: 'brl', interval: 'year'},
[RETIRED_MONTHLY_BRL]: {unit_amount: RETIRED_MONTHLY_BRL_MINOR, currency: 'brl', interval: 'month'},
[RETIRED_CHEAP_MONTHLY_BRL]: {unit_amount: RETIRED_CHEAP_MONTHLY_BRL_MINOR, currency: 'brl', interval: 'month'},
[RETIRED_MONTHLY_TRY]: {unit_amount: RETIRED_MONTHLY_TRY_MINOR, currency: 'try', interval: 'month'},
[RETIRED_EQUAL_MONTHLY_BRL]: {unit_amount: LIST_MONTHLY_BRL_MINOR, currency: 'brl', interval: 'month'},
};
interface SubscriberFixture {
subscriptionId: string;
priceId: string;
status?: 'active' | 'trialing' | 'past_due';
cancelAt?: number | null;
cancelAtPeriodEnd?: boolean;
periodStart?: number;
periodEnd?: number;
}
interface ResolvedSubscriber {
token: string;
userId: UserID;
subscriptionId: string;
priceId: string;
periodStart: number;
periodEnd: number;
}
describe('StripeSubscriptionListPriceSwitch', () => {
let harness: ApiTestHarness;
let stripeHandlers: StripeApiHandlers;
let originalPrices: typeof Config.stripe.prices | undefined;
function applyStripeHandlers(fixture: Required<SubscriberFixture>): void {
const seed = MOCK_PRICE_SEEDS[fixture.priceId];
if (!seed) {
throw new Error(`Missing explicit price seed for ${fixture.priceId}`);
}
stripeHandlers = createStripeApiHandlers({
subscriptions: {
[fixture.subscriptionId]: {
customer: `cus_${fixture.subscriptionId}`,
price_id: fixture.priceId,
unit_amount: seed.unit_amount,
currency: seed.currency,
interval: seed.interval,
item_id: `si_${fixture.subscriptionId}`,
current_period_start: fixture.periodStart,
current_period_end: fixture.periodEnd,
status: fixture.status,
cancel_at: fixture.cancelAt,
cancel_at_period_end: fixture.cancelAtPeriodEnd,
},
},
prices: MOCK_PRICE_SEEDS,
});
server.use(...stripeHandlers.handlers, createPwnedPasswordsRangeHandler());
}
async function mirrorStripeState(userId: UserID, subscriptionId: string): Promise<void> {
const stripe = new Stripe(Config.stripe.secretKey ?? 'sk_test_fluxer', {
apiVersion: STRIPE_API_VERSION,
httpClient: Stripe.createFetchHttpClient(),
});
const subscription = await stripe.subscriptions.retrieve(subscriptionId, {expand: ['items.data.price']});
await getBillingRepository().subscriptions.upsertFromStripe(subscription, {
knownUserId: userId,
snapshotCapturedAt: new Date(),
});
for (const priceId of Object.keys(MOCK_PRICE_SEEDS)) {
const price = await stripe.prices.retrieve(priceId);
await getBillingRepository().prices.upsertFromStripe(price);
}
}
async function createSubscriber(fixture: SubscriberFixture): Promise<ResolvedSubscriber> {
const periodStart = fixture.periodStart ?? Math.floor(Date.now() / 1000) - 3 * 24 * 60 * 60;
const periodEnd = fixture.periodEnd ?? periodStart + 30 * 24 * 60 * 60;
const resolved: Required<SubscriberFixture> = {
subscriptionId: fixture.subscriptionId,
priceId: fixture.priceId,
status: fixture.status ?? 'active',
cancelAt: fixture.cancelAt ?? null,
cancelAtPeriodEnd: fixture.cancelAtPeriodEnd ?? false,
periodStart,
periodEnd,
};
const account = await createTestAccount(harness);
applyStripeHandlers(resolved);
await createBuilder(harness, account.token)
.post(`/test/users/${account.userId}/premium`)
.body({
stripe_subscription_id: resolved.subscriptionId,
premium_type: 1,
premium_billing_cycle: MOCK_PRICE_SEEDS[resolved.priceId]?.interval === 'year' ? 'yearly' : 'monthly',
premium_until: new Date(periodEnd * 1000).toISOString(),
premium_will_cancel: Boolean(resolved.cancelAt) || resolved.cancelAtPeriodEnd,
})
.execute();
const userId = createUserID(BigInt(account.userId));
await mirrorStripeState(userId, resolved.subscriptionId);
return {
token: account.token,
userId,
subscriptionId: resolved.subscriptionId,
priceId: resolved.priceId,
periodStart,
periodEnd,
};
}
function switchToListPrice(subscriber: ResolvedSubscriber): Promise<SwitchToListPriceResponse> {
return createBuilder<SwitchToListPriceResponse>(harness, subscriber.token)
.post('/premium/switch-to-list-price')
.expect(200)
.execute();
}
function getPremiumState(subscriber: ResolvedSubscriber): Promise<PremiumStateResponse> {
return createBuilder<PremiumStateResponse>(harness, subscriber.token).get('/premium/state').expect(200).execute();
}
function expectNoStripeWrites(): void {
expect(stripeHandlers.spies.updatedSubscriptions).toHaveLength(0);
expect(stripeHandlers.spies.cancelledSubscriptions).toHaveLength(0);
expect(stripeHandlers.spies.createdSubscriptionSchedules).toHaveLength(0);
expect(stripeHandlers.spies.updatedSubscriptionSchedules).toHaveLength(0);
expect(stripeHandlers.spies.releasedSubscriptionSchedules).toHaveLength(0);
}
async function expectRefusal(
subscriber: ResolvedSubscriber,
reason: ListPriceSwitchIneligibilityReason,
): Promise<void> {
const result = await switchToListPrice(subscriber);
expect(result).toEqual({status: 'ineligible', reason});
const state = await getPremiumState(subscriber);
expect(state.billing.list_price_switch.available).toBe(false);
expect(state.billing.list_price_switch.reason).toBe(reason);
}
beforeAll(async () => {
originalPrices = Config.stripe.prices;
Config.stripe.prices = MOCK_PRICES;
harness = await createApiTestHarness();
stripeHandlers = createStripeApiHandlers({prices: MOCK_PRICE_SEEDS});
server.use(...stripeHandlers.handlers);
});
afterAll(async () => {
await harness.shutdown();
Config.stripe.prices = originalPrices;
});
beforeEach(async () => {
await harness.resetData();
Config.stripe.prices = MOCK_PRICES;
stripeHandlers.resetAll();
server.use(...stripeHandlers.handlers, createPwnedPasswordsRangeHandler());
});
describe('POST /premium/switch-to-list-price', () => {
test('schedules a grandfathered BRL monthly subscriber onto the current list price at period end', async () => {
const moneyMoves: Array<string> = [];
server.use(
http.post('https://api.stripe.com/v1/invoices', () => {
moneyMoves.push('invoices.create');
return HttpResponse.json({});
}),
http.post('https://api.stripe.com/v1/charges', () => {
moneyMoves.push('charges.create');
return HttpResponse.json({});
}),
http.post('https://api.stripe.com/v1/payment_intents', () => {
moneyMoves.push('payment_intents.create');
return HttpResponse.json({});
}),
);
const subscriber = await createSubscriber({
subscriptionId: 'sub_brl_grandfathered',
priceId: RETIRED_MONTHLY_BRL,
});
const result = await switchToListPrice(subscriber);
expect(result).toEqual({
status: 'scheduled',
effective_at: new Date(subscriber.periodEnd * 1000).toISOString(),
target_price_id: MOCK_PRICES.monthlyBrl,
target_amount_minor: LIST_MONTHLY_BRL_MINOR,
current_amount_minor: RETIRED_MONTHLY_BRL_MINOR,
currency: 'BRL',
});
expect(stripeHandlers.spies.createdSubscriptionSchedules).toHaveLength(1);
expect(stripeHandlers.spies.createdSubscriptionSchedules[0]?.from_subscription).toBe('sub_brl_grandfathered');
expect(stripeHandlers.spies.updatedSubscriptionSchedules).toHaveLength(1);
const scheduleUpdate = stripeHandlers.spies.updatedSubscriptionSchedules[0];
expect(scheduleUpdate?.params.end_behavior).toBe('release');
expect(scheduleUpdate?.params.proration_behavior).toBe('none');
expect(scheduleUpdate?.params.phases).toHaveLength(2);
expect(scheduleUpdate?.params.phases?.[0]?.end_date).toBe(String(subscriber.periodEnd));
expect(scheduleUpdate?.params.phases?.[0]?.items?.[0]?.price).toBe(RETIRED_MONTHLY_BRL);
expect(scheduleUpdate?.params.phases?.[1]?.start_date).toBe(String(subscriber.periodEnd));
expect(scheduleUpdate?.params.phases?.[1]?.items?.[0]?.price).toBe(MOCK_PRICES.monthlyBrl);
expect(scheduleUpdate?.params.phases?.[1]?.proration_behavior).toBe('none');
expect(scheduleUpdate?.params.phases?.[1]?.billing_cycle_anchor).toBeUndefined();
expect(scheduleUpdate?.params.phases?.[1]?.add_invoice_items).toBeUndefined();
expect(stripeHandlers.spies.updatedSubscriptions).toHaveLength(0);
expect(moneyMoves).toEqual([]);
const me = await createBuilder<{
premium_will_cancel: boolean;
}>(harness, subscriber.token)
.get('/users/@me')
.execute();
expect(me.premium_will_cancel).toBe(false);
const state = await getPremiumState(subscriber);
expect(state.billing.pending_subscription_change).toBeNull();
expect(state.billing.list_price_switch).toEqual(
expect.objectContaining({
available: false,
reason: null,
pending: true,
effective_at: new Date(subscriber.periodEnd * 1000).toISOString(),
current_price_id: RETIRED_MONTHLY_BRL,
current_amount_minor: RETIRED_MONTHLY_BRL_MINOR,
list_price_id: MOCK_PRICES.monthlyBrl,
list_amount_minor: LIST_MONTHLY_BRL_MINOR,
currency: 'BRL',
billing_cycle: 'monthly',
}),
);
});
test('preserves the phase-level settings Stripe would otherwise unset on the live subscription', async () => {
const subscriber = await createSubscriber({
subscriptionId: 'sub_brl_phase_settings',
priceId: RETIRED_MONTHLY_BRL,
});
const scheduleId = 'sub_sched_phase_settings';
const richSchedule = {
id: scheduleId,
object: 'subscription_schedule',
status: 'active',
subscription: subscriber.subscriptionId,
end_behavior: 'release',
metadata: {},
current_phase: {start_date: subscriber.periodStart, end_date: subscriber.periodEnd},
phases: [
{
start_date: subscriber.periodStart,
end_date: subscriber.periodEnd,
currency: 'brl',
collection_method: 'send_invoice',
description: 'Grandfathered Plutonium',
metadata: {campaign: 'brl_2026'},
discounts: [{coupon: {id: 'co_grandfather_10'}, discount: null, promotion_code: null}],
default_tax_rates: [{id: 'txr_br_icms'}],
automatic_tax: {enabled: true, disabled_reason: null, liability: {type: 'self'}},
invoice_settings: {account_tax_ids: null, days_until_due: 14, issuer: null},
billing_cycle_anchor: 'automatic',
add_invoice_items: [],
items: [
{
price: RETIRED_MONTHLY_BRL,
quantity: 1,
metadata: {seat: 'primary'},
discounts: [{coupon: null, discount: 'di_existing', promotion_code: null}],
tax_rates: [{id: 'txr_br_iss'}],
billing_thresholds: null,
},
],
proration_behavior: 'none',
},
],
livemode: false,
created: Math.floor(Date.now() / 1000),
};
const scheduleUpdates: Array<Record<string, string>> = [];
server.use(
http.post('https://api.stripe.com/v1/subscription_schedules', () => HttpResponse.json(richSchedule)),
http.post(`https://api.stripe.com/v1/subscription_schedules/${scheduleId}`, async ({request}) => {
const formData = await request.formData();
const entries: Record<string, string> = {};
for (const [key, value] of formData.entries()) {
entries[key] = String(value);
}
scheduleUpdates.push(entries);
return HttpResponse.json(richSchedule);
}),
);
const result = await switchToListPrice(subscriber);
expect(result.status).toBe('scheduled');
expect(scheduleUpdates).toHaveLength(1);
const update = scheduleUpdates[0]!;
expect(update['phases[0][items][0][price]']).toBe(RETIRED_MONTHLY_BRL);
expect(update['phases[0][discounts][0][coupon]']).toBe('co_grandfather_10');
expect(update['phases[0][default_tax_rates][0]']).toBe('txr_br_icms');
expect(update['phases[0][items][0][tax_rates][0]']).toBe('txr_br_iss');
expect(update['phases[0][items][0][discounts][0][discount]']).toBe('di_existing');
expect(update['phases[0][items][0][metadata][seat]']).toBe('primary');
expect(update['phases[0][metadata][campaign]']).toBe('brl_2026');
expect(update['phases[0][collection_method]']).toBe('send_invoice');
expect(update['phases[0][description]']).toBe('Grandfathered Plutonium');
expect(update['phases[0][currency]']).toBe('brl');
expect(update['phases[0][automatic_tax][enabled]']).toBe('true');
expect(update['phases[0][automatic_tax][liability][type]']).toBe('self');
expect(update['phases[0][invoice_settings][days_until_due]']).toBe('14');
expect(update['phases[0][billing_cycle_anchor]']).toBe('automatic');
expect(update['phases[0][end_date]']).toBe(String(subscriber.periodEnd));
expect(update['phases[1][items][0][price]']).toBe(MOCK_PRICES.monthlyBrl);
expect(update['phases[1][discounts][0][coupon]']).toBeUndefined();
});
test('refuses to move a subscriber up to a more expensive list price', async () => {
const subscriber = await createSubscriber({
subscriptionId: 'sub_brl_below_list',
priceId: RETIRED_CHEAP_MONTHLY_BRL,
});
await expectRefusal(subscriber, 'not_a_price_decrease');
expectNoStripeWrites();
});
test('refuses a subscriber who is cancelling at period end', async () => {
const periodStart = Math.floor(Date.now() / 1000) - 3 * 24 * 60 * 60;
const periodEnd = periodStart + 30 * 24 * 60 * 60;
const subscriber = await createSubscriber({
subscriptionId: 'sub_brl_cancel_at_period_end',
priceId: RETIRED_MONTHLY_BRL,
cancelAtPeriodEnd: true,
periodStart,
periodEnd,
});
await expectRefusal(subscriber, 'subscription_cancelling');
expectNoStripeWrites();
const me = await createBuilder<{
premium_will_cancel: boolean;
}>(harness, subscriber.token)
.get('/users/@me')
.execute();
expect(me.premium_will_cancel).toBe(true);
});
test('refuses a subscriber who is cancelling on an explicit cancel_at date', async () => {
const periodStart = Math.floor(Date.now() / 1000) - 3 * 24 * 60 * 60;
const periodEnd = periodStart + 30 * 24 * 60 * 60;
const subscriber = await createSubscriber({
subscriptionId: 'sub_brl_cancel_at',
priceId: RETIRED_MONTHLY_BRL,
cancelAt: periodEnd,
cancelAtPeriodEnd: false,
periodStart,
periodEnd,
});
await expectRefusal(subscriber, 'subscription_cancelling');
expectNoStripeWrites();
const me = await createBuilder<{
premium_will_cancel: boolean;
}>(harness, subscriber.token)
.get('/users/@me')
.execute();
expect(me.premium_will_cancel).toBe(true);
});
test('is idempotent and reports an already scheduled switch on the second call', async () => {
const subscriber = await createSubscriber({
subscriptionId: 'sub_brl_idempotent',
priceId: RETIRED_MONTHLY_BRL,
});
const first = await switchToListPrice(subscriber);
expect(first.status).toBe('scheduled');
const second = await switchToListPrice(subscriber);
expect(second).toEqual({
status: 'already_scheduled',
effective_at: new Date(subscriber.periodEnd * 1000).toISOString(),
target_price_id: MOCK_PRICES.monthlyBrl,
target_amount_minor: LIST_MONTHLY_BRL_MINOR,
current_amount_minor: RETIRED_MONTHLY_BRL_MINOR,
currency: 'BRL',
});
expect(stripeHandlers.spies.createdSubscriptionSchedules).toHaveLength(1);
expect(stripeHandlers.spies.updatedSubscriptionSchedules).toHaveLength(1);
});
test('refuses a retired price that costs exactly the same as the list price', async () => {
const subscriber = await createSubscriber({
subscriptionId: 'sub_brl_equal_price',
priceId: RETIRED_EQUAL_MONTHLY_BRL,
});
await expectRefusal(subscriber, 'not_a_price_decrease');
expectNoStripeWrites();
});
test('lets a scheduled switch be withdrawn again and restores eligibility', async () => {
const subscriber = await createSubscriber({
subscriptionId: 'sub_brl_withdrawn',
priceId: RETIRED_MONTHLY_BRL,
});
expect((await switchToListPrice(subscriber)).status).toBe('scheduled');
await createBuilder(harness, subscriber.token)
.post('/premium/cancel-pending-subscription-change')
.expect(204)
.execute();
expect(stripeHandlers.spies.releasedSubscriptionSchedules).toHaveLength(1);
expect(stripeHandlers.spies.releasedSubscriptionSchedules[0]?.params.preserve_cancel_date).toBe('false');
const state = await getPremiumState(subscriber);
expect(state.billing.pending_subscription_change).toBeNull();
expect(state.billing.list_price_switch).toEqual(
expect.objectContaining({
available: true,
reason: null,
pending: false,
current_price_id: RETIRED_MONTHLY_BRL,
current_amount_minor: RETIRED_MONTHLY_BRL_MINOR,
list_price_id: MOCK_PRICES.monthlyBrl,
list_amount_minor: LIST_MONTHLY_BRL_MINOR,
}),
);
});
test('refuses a subscriber who is already on the current list price', async () => {
const subscriber = await createSubscriber({
subscriptionId: 'sub_brl_on_list_price',
priceId: MOCK_PRICES.monthlyBrl,
});
await expectRefusal(subscriber, 'already_on_list_price');
expectNoStripeWrites();
});
test('refuses a currency that has no configured list price', async () => {
const subscriber = await createSubscriber({
subscriptionId: 'sub_try_unconfigured',
priceId: RETIRED_MONTHLY_TRY,
});
await expectRefusal(subscriber, 'no_list_price');
expectNoStripeWrites();
});
test('refuses a subscription that is not chargeable', async () => {
const subscriber = await createSubscriber({
subscriptionId: 'sub_brl_past_due',
priceId: RETIRED_MONTHLY_BRL,
status: 'past_due',
});
await expectRefusal(subscriber, 'subscription_not_chargeable');
expectNoStripeWrites();
});
test('refuses a subscriber whose cancellation is managed by a subscription schedule', async () => {
const subscriber = await createSubscriber({
subscriptionId: 'sub_brl_schedule_cancel',
priceId: RETIRED_MONTHLY_BRL,
});
await createBuilder(harness, subscriber.token)
.post('/premium/change-subscription')
.body({billing_cycle: 'yearly', effective_at: 'period_end'})
.expect(204)
.execute();
await createBuilder(harness, subscriber.token).post('/premium/cancel-subscription').expect(204).execute();
stripeHandlers.spies.updatedSubscriptions.length = 0;
stripeHandlers.spies.createdSubscriptionSchedules.length = 0;
stripeHandlers.spies.updatedSubscriptionSchedules.length = 0;
stripeHandlers.spies.releasedSubscriptionSchedules.length = 0;
const result = await switchToListPrice(subscriber);
expect(result).toEqual({status: 'ineligible', reason: 'subscription_cancelling'});
expectNoStripeWrites();
const me = await createBuilder<{
premium_will_cancel: boolean;
}>(harness, subscriber.token)
.get('/users/@me')
.execute();
expect(me.premium_will_cancel).toBe(true);
});
test('refuses when a billing cycle change is already pending', async () => {
const subscriber = await createSubscriber({
subscriptionId: 'sub_brl_pending_cycle_change',
priceId: RETIRED_MONTHLY_BRL,
});
await createBuilder(harness, subscriber.token)
.post('/premium/change-subscription')
.body({billing_cycle: 'yearly', effective_at: 'period_end'})
.expect(204)
.execute();
stripeHandlers.spies.updatedSubscriptions.length = 0;
stripeHandlers.spies.createdSubscriptionSchedules.length = 0;
stripeHandlers.spies.updatedSubscriptionSchedules.length = 0;
await expectRefusal(subscriber, 'conflicting_pending_change');
expectNoStripeWrites();
});
test('rejects a user without an active subscription and mirrors that in premium state', async () => {
const account = await createTestAccount(harness);
await createBuilder(harness, account.token)
.post('/premium/switch-to-list-price')
.expect(400, APIErrorCodes.STRIPE_NO_ACTIVE_SUBSCRIPTION)
.execute();
const state = await createBuilder<PremiumStateResponse>(harness, account.token)
.get('/premium/state')
.expect(200)
.execute();
expect(state.billing.list_price_switch.available).toBe(false);
expect(state.billing.list_price_switch.reason).toBe('no_active_subscription');
expectNoStripeWrites();
});
});
describe('POST /premium/change-subscription (unchanged behaviour)', () => {
test('still clears a pending cancellation and re-anchors billing when the cycle actually changes', async () => {
const periodStart = Math.floor(Date.now() / 1000) - 3 * 24 * 60 * 60;
const periodEnd = periodStart + 30 * 24 * 60 * 60;
const subscriber = await createSubscriber({
subscriptionId: 'sub_brl_cycle_change',
priceId: RETIRED_MONTHLY_BRL,
cancelAt: periodEnd,
cancelAtPeriodEnd: false,
periodStart,
periodEnd,
});
await createBuilder(harness, subscriber.token)
.post('/premium/change-subscription')
.body({billing_cycle: 'yearly', effective_at: 'period_end'})
.expect(204)
.execute();
expect(stripeHandlers.spies.updatedSubscriptions).toHaveLength(1);
const clearCancelUpdate = stripeHandlers.spies.updatedSubscriptions[0];
expect(clearCancelUpdate?.id).toBe('sub_brl_cycle_change');
expect(clearCancelUpdate?.params.cancel_at).toBe('');
expect(clearCancelUpdate?.params.proration_behavior).toBe('none');
expect(clearCancelUpdate?.params.items).toBeUndefined();
expect(stripeHandlers.spies.updatedSubscriptionSchedules).toHaveLength(1);
const scheduleUpdate = stripeHandlers.spies.updatedSubscriptionSchedules[0];
expect(scheduleUpdate?.params.end_behavior).toBe('release');
expect(scheduleUpdate?.params.phases?.[1]?.start_date).toBe(String(periodEnd));
expect(scheduleUpdate?.params.phases?.[1]?.billing_cycle_anchor).toBe('phase_start');
expect(scheduleUpdate?.params.phases?.[1]?.items?.[0]?.price).toBe(MOCK_PRICES.yearlyBrl);
const me = await createBuilder<{
premium_will_cancel: boolean;
}>(harness, subscriber.token)
.get('/users/@me')
.execute();
expect(me.premium_will_cancel).toBe(false);
});
});
});
@@ -4,12 +4,14 @@ import crypto from 'node:crypto';
import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';
import {UserPremiumTypes} from '@fluxer/constants/src/UserConstants';
import {HttpResponse, http} from 'msw';
import {afterAll, afterEach, beforeAll, beforeEach, describe, expect, test} from 'vitest';
import {afterAll, afterEach, beforeAll, beforeEach, describe, expect, test, vi} from 'vitest';
import {createTestAccount} from '../../auth/tests/AuthTestUtils';
import {createUserID} from '../../BrandedTypes';
import {Config} from '../../Config';
import {Logger} from '../../Logger';
import {getBillingRepository} from '../../middleware/ServiceRegistry';
import {type ApiTestHarness, createApiTestHarness} from '../../test/ApiTestHarness';
import {NoopLogger} from '../../test/mocks/NoopLogger';
import {
createInvoiceFinalizationFailedEvent,
createInvoicePaidEvent,
@@ -41,16 +43,24 @@ const MOCK_PRICES = {
gift1YearEur: 'price_gift_1_year_eur',
};
const LEGACY_MONTHLY_BRL_PRICE = 'price_legacy_monthly_brl';
const LEGACY_YEARLY_BRL_PRICE = 'price_legacy_yearly_brl';
const UNMAPPED_PRICE = 'price_retired_unmapped_brl';
const MANDATE_REVOKED_WARNING =
'Stripe mandate is no longer active; recurring payments on this payment method will fail';
describe('Stripe Webhook - Invoice Events', () => {
let harness: ApiTestHarness;
let stripeHandlers: StripeApiHandlers;
let originalWebhookSecret: string | undefined;
let originalPrices: typeof Config.stripe.prices | undefined;
let originalLegacyPrices: typeof Config.stripe.legacyPrices | undefined;
beforeAll(async () => {
harness = await createApiTestHarness();
setupSyncStripeWebhookWorker();
originalWebhookSecret = Config.stripe.webhookSecret;
originalPrices = Config.stripe.prices;
originalLegacyPrices = Config.stripe.legacyPrices;
Config.stripe.webhookSecret = 'whsec_test_secret';
Config.stripe.prices = MOCK_PRICES;
stripeHandlers = createStripeApiHandlers();
@@ -60,9 +70,11 @@ describe('Stripe Webhook - Invoice Events', () => {
await harness.shutdown();
Config.stripe.webhookSecret = originalWebhookSecret;
Config.stripe.prices = originalPrices;
Config.stripe.legacyPrices = originalLegacyPrices;
});
beforeEach(async () => {
await harness.resetData();
Config.stripe.legacyPrices = undefined;
stripeHandlers.reset();
server.use(...stripeHandlers.handlers);
});
@@ -104,7 +116,7 @@ describe('Stripe Webhook - Invoice Events', () => {
subscriptionId: string;
priceId: string;
productType: string;
}): Promise<void> {
}): Promise<string> {
const {userId, subscriptionId, priceId, productType} = params;
const checkoutSessionId = `cs_test_${crypto.randomUUID()}`;
const {PaymentRepository} = await import('../../user/repositories/PaymentRepository');
@@ -127,6 +139,7 @@ describe('Stripe Webhook - Invoice Events', () => {
currency: 'usd',
completed_at: new Date(),
});
return checkoutSessionId;
}
async function setSubscriptionUserState(params: {
accountUserId: string;
@@ -856,4 +869,365 @@ describe('Stripe Webhook - Invoice Events', () => {
expect(me.premium_until).not.toBeNull();
});
});
describe('renewals on retired prices', () => {
test('renews a subscription whose price is only known to the legacy price map', async () => {
Config.stripe.legacyPrices = {monthly_brl: [LEGACY_MONTHLY_BRL_PRICE]};
const account = await createTestAccount(harness);
const subscriptionId = 'sub_legacy_brl_renewal';
const customerId = 'cus_legacy_brl_renewal';
const invoiceId = 'in_legacy_brl_renewal';
const checkoutSessionId = await createPaymentRecord({
userId: account.userId,
subscriptionId,
priceId: LEGACY_MONTHLY_BRL_PRICE,
productType: ProductType.MONTHLY_SUBSCRIPTION,
});
const currentPeriodStart = Math.floor(Date.now() / 1000) - 2 * 24 * 60 * 60;
server.use(
...createStripeApiHandlers({
subscriptions: {
[subscriptionId]: {
customer: customerId,
price_id: LEGACY_MONTHLY_BRL_PRICE,
currency: 'brl',
interval: 'month',
item_id: 'si_legacy_brl_renewal',
current_period_start: currentPeriodStart,
current_period_end: currentPeriodStart + 30 * 24 * 60 * 60,
},
},
}).handlers,
);
const result = await sendWebhook({
type: 'invoice.payment_succeeded',
data: {
object: {
id: invoiceId,
billing_reason: 'subscription_cycle',
customer: customerId,
parent: {subscription_details: {subscription: subscriptionId}},
},
},
});
expect(result.received).toBe(true);
const me = await createBuilder<{
premium_billing_cycle: string | null;
premium_type: number | null;
premium_until: string | null;
}>(harness, account.token)
.get('/users/@me')
.execute();
expect(me.premium_type).toBe(UserPremiumTypes.SUBSCRIPTION);
expect(me.premium_billing_cycle).toBe('monthly');
expect(me.premium_until).not.toBeNull();
expect(new Date(me.premium_until!).toISOString()).toBe(
new Date((currentPeriodStart + 30 * 24 * 60 * 60) * 1000).toISOString(),
);
const {PaymentRepository} = await import('../../user/repositories/PaymentRepository');
const payment = await new PaymentRepository().getPaymentByCheckoutSession(checkoutSessionId);
expect(payment?.invoiceId).toBe(invoiceId);
});
test('renews a subscription whose retired price sits in a legacy slot alongside other price ids', async () => {
Config.stripe.legacyPrices = {
monthly_brl: ['price_legacy_monthly_brl_older', LEGACY_MONTHLY_BRL_PRICE],
yearly_brl: [LEGACY_YEARLY_BRL_PRICE],
};
const account = await createTestAccount(harness);
const subscriptionId = 'sub_legacy_brl_yearly_renewal';
const customerId = 'cus_legacy_brl_yearly_renewal';
await createPaymentRecord({
userId: account.userId,
subscriptionId,
priceId: LEGACY_YEARLY_BRL_PRICE,
productType: ProductType.YEARLY_SUBSCRIPTION,
});
const currentPeriodStart = Math.floor(Date.now() / 1000) - 2 * 24 * 60 * 60;
server.use(
...createStripeApiHandlers({
subscriptions: {
[subscriptionId]: {
customer: customerId,
price_id: LEGACY_YEARLY_BRL_PRICE,
currency: 'brl',
interval: 'year',
item_id: 'si_legacy_brl_yearly_renewal',
current_period_start: currentPeriodStart,
current_period_end: currentPeriodStart + 365 * 24 * 60 * 60,
},
},
}).handlers,
);
const result = await sendWebhook({
type: 'invoice.payment_succeeded',
data: {
object: {
id: 'in_legacy_brl_yearly_renewal',
billing_reason: 'subscription_cycle',
customer: customerId,
parent: {subscription_details: {subscription: subscriptionId}},
},
},
});
expect(result.received).toBe(true);
const me = await createBuilder<{
premium_billing_cycle: string | null;
premium_type: number | null;
premium_until: string | null;
}>(harness, account.token)
.get('/users/@me')
.execute();
expect(me.premium_type).toBe(UserPremiumTypes.SUBSCRIPTION);
expect(me.premium_billing_cycle).toBe('yearly');
expect(new Date(me.premium_until!).toISOString()).toBe(
new Date((currentPeriodStart + 365 * 24 * 60 * 60) * 1000).toISOString(),
);
});
test('still rejects a renewal whose price is in neither the configured nor the legacy price map', async () => {
Config.stripe.legacyPrices = {monthly_brl: [LEGACY_MONTHLY_BRL_PRICE]};
const account = await createTestAccount(harness);
const subscriptionId = 'sub_unmapped_price_renewal';
const customerId = 'cus_unmapped_price_renewal';
await createPaymentRecord({
userId: account.userId,
subscriptionId,
priceId: UNMAPPED_PRICE,
productType: ProductType.MONTHLY_SUBSCRIPTION,
});
await createBuilder(harness, account.token)
.post(`/test/users/${account.userId}/premium`)
.body({
stripe_subscription_id: subscriptionId,
stripe_customer_id: customerId,
})
.execute();
const currentPeriodStart = Math.floor(Date.now() / 1000) - 2 * 24 * 60 * 60;
server.use(
...createStripeApiHandlers({
subscriptions: {
[subscriptionId]: {
customer: customerId,
price_id: UNMAPPED_PRICE,
currency: 'brl',
interval: 'month',
item_id: 'si_unmapped_price_renewal',
current_period_start: currentPeriodStart,
current_period_end: currentPeriodStart + 30 * 24 * 60 * 60,
},
},
}).handlers,
);
await sendWebhookExpectStripeError({
type: 'invoice.payment_succeeded',
data: {
object: {
id: 'in_unmapped_price_renewal',
billing_reason: 'subscription_cycle',
customer: customerId,
parent: {subscription_details: {subscription: subscriptionId}},
},
},
});
const me = await createBuilder<{
premium_type: number | null;
premium_until: string | null;
}>(harness, account.token)
.get('/users/@me')
.execute();
expect(me.premium_type).toBe(UserPremiumTypes.NONE);
expect(me.premium_until).toBeNull();
});
test('lets an authored price win over a legacy entry that claims the same price id', async () => {
Config.stripe.legacyPrices = {yearly_brl: [MOCK_PRICES.monthlyUsd]};
const account = await createTestAccount(harness);
const subscriptionId = 'sub_authored_wins_over_legacy';
await createPaymentRecord({
userId: account.userId,
subscriptionId,
priceId: MOCK_PRICES.monthlyUsd,
productType: ProductType.MONTHLY_SUBSCRIPTION,
});
const result = await sendWebhook({
type: 'invoice.payment_succeeded',
data: {
object: {
id: 'in_authored_wins_over_legacy',
billing_reason: 'subscription_cycle',
parent: {subscription_details: {subscription: subscriptionId}},
},
},
});
expect(result.received).toBe(true);
const me = await createBuilder<{
premium_billing_cycle: string | null;
premium_type: number | null;
}>(harness, account.token)
.get('/users/@me')
.execute();
expect(me.premium_type).toBe(UserPremiumTypes.SUBSCRIPTION);
expect(me.premium_billing_cycle).toBe('monthly');
});
});
describe('donation subscription guard', () => {
test('does not extend premium for an invoice on a donation subscription', async () => {
const account = await createTestAccount(harness);
const subscriptionId = 'sub_donation_recurring';
await createPaymentRecord({
userId: account.userId,
subscriptionId,
priceId: MOCK_PRICES.monthlyUsd,
productType: ProductType.MONTHLY_SUBSCRIPTION,
});
const {DonationRepository} = await import('../../donation/DonationRepository');
await new DonationRepository().createDonor({
email: '[email protected]',
stripeCustomerId: 'cus_donation_recurring',
stripeSubscriptionId: subscriptionId,
subscriptionAmountCents: 1000,
subscriptionCurrency: 'usd',
subscriptionInterval: 'month',
subscriptionCurrentPeriodEnd: null,
});
const result = await sendWebhook({
type: 'invoice.payment_succeeded',
data: {
object: {
id: 'in_donation_recurring',
billing_reason: 'subscription_cycle',
customer: 'cus_donation_recurring',
parent: {subscription_details: {subscription: subscriptionId}},
},
},
});
expect(result.received).toBe(true);
const me = await createBuilder<{
premium_type: number | null;
premium_until: string | null;
}>(harness, account.token)
.get('/users/@me')
.execute();
expect(me.premium_type).toBe(UserPremiumTypes.NONE);
expect(me.premium_until).toBeNull();
});
test('still renews when only the donor customer id matches and the subscription id does not', async () => {
const account = await createTestAccount(harness);
const sharedCustomerId = 'cus_donor_and_subscriber';
const premiumSubscriptionId = 'sub_premium_beside_donation';
await createPaymentRecord({
userId: account.userId,
subscriptionId: premiumSubscriptionId,
priceId: MOCK_PRICES.monthlyUsd,
productType: ProductType.MONTHLY_SUBSCRIPTION,
});
const {DonationRepository} = await import('../../donation/DonationRepository');
await new DonationRepository().createDonor({
email: '[email protected]',
stripeCustomerId: sharedCustomerId,
stripeSubscriptionId: 'sub_donation_beside_premium',
subscriptionAmountCents: 1000,
subscriptionCurrency: 'usd',
subscriptionInterval: 'month',
subscriptionCurrentPeriodEnd: null,
});
const result = await sendWebhook({
type: 'invoice.payment_succeeded',
data: {
object: {
id: 'in_premium_beside_donation',
billing_reason: 'subscription_cycle',
customer: sharedCustomerId,
parent: {subscription_details: {subscription: premiumSubscriptionId}},
},
},
});
expect(result.received).toBe(true);
const me = await createBuilder<{
premium_type: number | null;
premium_until: string | null;
}>(harness, account.token)
.get('/users/@me')
.execute();
expect(me.premium_type).toBe(UserPremiumTypes.SUBSCRIPTION);
expect(me.premium_until).not.toBeNull();
});
});
describe('mandate.updated', () => {
function captureLoggerWarnings(): {
messages: Array<unknown>;
restore: () => void;
} {
const activeLogger = Logger.child({}) as unknown as NoopLogger;
expect(activeLogger).toBeInstanceOf(NoopLogger);
const messages: Array<unknown> = [];
const spy = vi.spyOn(activeLogger, 'warn').mockImplementation((...args: Array<unknown>) => {
messages.push(args[args.length - 1]);
});
return {messages, restore: () => spy.mockRestore()};
}
function revocationWarnings(messages: Array<unknown>): Array<unknown> {
return messages.filter((message) => message === MANDATE_REVOKED_WARNING);
}
test('logs the revocation warning and leaves premium untouched when a mandate goes inactive', async () => {
const account = await createTestAccount(harness);
const premiumUntil = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000);
await createBuilder(harness, account.token)
.post(`/test/users/${account.userId}/premium`)
.body({
premium_type: UserPremiumTypes.SUBSCRIPTION,
premium_until: premiumUntil.toISOString(),
stripe_subscription_id: 'sub_pix_mandate',
stripe_customer_id: 'cus_pix_mandate',
})
.execute();
const warnings = captureLoggerWarnings();
try {
const result = await sendWebhook({
type: 'mandate.updated',
data: {
object: {
id: 'mandate_test_inactive',
object: 'mandate',
status: 'inactive',
payment_method: 'pm_pix_mandate',
type: 'multi_use',
},
},
});
expect(result.received).toBe(true);
expect(revocationWarnings(warnings.messages)).toHaveLength(1);
} finally {
warnings.restore();
}
const me = await createBuilder<{
premium_type: number | null;
premium_until: string | null;
}>(harness, account.token)
.get('/users/@me')
.execute();
expect(me.premium_type).toBe(UserPremiumTypes.SUBSCRIPTION);
expect(me.premium_until).toBe(premiumUntil.toISOString());
});
test('does not log the revocation warning for a pending or still-active mandate', async () => {
const warnings = captureLoggerWarnings();
try {
for (const status of ['pending', 'active'] as const) {
const result = await sendWebhook({
type: 'mandate.updated',
data: {
object: {
id: `mandate_test_${status}`,
object: 'mandate',
status,
payment_method: {id: 'pm_pix_mandate', object: 'payment_method', type: 'pix'},
type: 'multi_use',
},
},
});
expect(result.received).toBe(true);
}
expect(revocationWarnings(warnings.messages)).toHaveLength(0);
} finally {
warnings.restore();
}
});
});
});
@@ -49,14 +49,14 @@ describe('Stripe Webhook Refund', () => {
.body(payload)
.execute();
}
function useRefundListHandler(chargeId: string): void {
function useRefundListHandler(chargeId: string, refunds: Array<Record<string, unknown>> = []): void {
server.use(
http.get(
({request}) => request.url === `https://api.stripe.com/v1/refunds?charge=${chargeId}&limit=100`,
() =>
HttpResponse.json({
object: 'list',
data: [],
data: refunds,
has_more: false,
url: '/v1/refunds',
}),
@@ -155,6 +155,366 @@ describe('Stripe Webhook Refund', () => {
expect(updatedPayment).not.toBeNull();
expect(updatedPayment!.status).toBe('refunded');
});
test('counts a refund once when the same charge.refunded event is redelivered', async () => {
const account = await createTestAccount(harness);
const userId = createUserID(BigInt(account.userId));
const {PaymentRepository} = await import('../../user/repositories/PaymentRepository');
const {UserRepository} = await import('../../user/repositories/UserRepository');
const paymentRepository = new PaymentRepository();
const userRepository = new UserRepository();
const paymentIntentId = 'pi_test_refund_retry_123';
const checkoutSessionId = 'cs_test_refund_retry_123';
const chargeId = 'ch_test_refund_retry_123';
await paymentRepository.createPayment({
checkout_session_id: checkoutSessionId,
user_id: userId,
price_id: 'price_test_monthly',
product_type: 'monthly_subscription',
status: 'completed',
is_gift: false,
created_at: new Date(),
});
await paymentRepository.updatePayment({
checkout_session_id: checkoutSessionId,
payment_intent_id: paymentIntentId,
completed_at: new Date(),
});
useRefundListHandler(chargeId, [
{
id: 're_test_refund_retry_123',
object: 'refund',
charge: chargeId,
payment_intent: paymentIntentId,
amount: 2500,
currency: 'brl',
status: 'succeeded',
created: Math.floor(Date.now() / 1000),
metadata: {},
},
]);
const chargeEvent = {
type: 'charge.refunded' as const,
data: {
object: {
id: chargeId,
payment_intent: paymentIntentId,
amount_refunded: 2500,
},
},
};
await sendWebhook({...chargeEvent, id: 'evt_test_refund_retry_1'});
await sendWebhook({...chargeEvent, id: 'evt_test_refund_retry_2'});
const updatedUser = await userRepository.findUnique(userId);
expect(updatedUser!.firstRefundAt).not.toBeNull();
expect(updatedUser!.premiumFlags & PremiumFlags.PURCHASE_DISABLED).toBe(0);
});
test('counts a refund once when charge.refunded lands before the refund record exists', async () => {
const account = await createTestAccount(harness);
const userId = createUserID(BigInt(account.userId));
const {PaymentRepository} = await import('../../user/repositories/PaymentRepository');
const {UserRepository} = await import('../../user/repositories/UserRepository');
const paymentRepository = new PaymentRepository();
const userRepository = new UserRepository();
const paymentIntentId = 'pi_test_refund_late_record';
const checkoutSessionId = 'cs_test_refund_late_record';
const chargeId = 'ch_test_refund_late_record';
const refundId = 're_test_refund_late_record';
await paymentRepository.createPayment({
checkout_session_id: checkoutSessionId,
user_id: userId,
price_id: 'price_test_monthly',
product_type: 'monthly_subscription',
status: 'completed',
is_gift: false,
created_at: new Date(),
});
await paymentRepository.updatePayment({
checkout_session_id: checkoutSessionId,
payment_intent_id: paymentIntentId,
completed_at: new Date(),
});
const refund = {
id: refundId,
object: 'refund',
charge: chargeId,
payment_intent: paymentIntentId,
amount: 2500,
currency: 'brl',
status: 'succeeded',
created: Math.floor(Date.now() / 1000),
metadata: {},
};
const chargeEvent = {
type: 'charge.refunded' as const,
data: {
object: {
id: chargeId,
payment_intent: paymentIntentId,
amount_refunded: 2500,
},
},
};
useRefundListHandler(chargeId);
await sendWebhook({...chargeEvent, id: 'evt_test_refund_late_record_1'});
const afterFallback = await userRepository.findUnique(userId);
expect(afterFallback!.firstRefundAt).not.toBeNull();
expect(afterFallback!.premiumFlags & PremiumFlags.PURCHASE_DISABLED).toBe(0);
await sendWebhook({
id: 'evt_test_refund_late_record_2',
type: 'refund.created',
data: {object: refund},
});
useRefundListHandler(chargeId, [refund]);
await sendWebhook({...chargeEvent, id: 'evt_test_refund_late_record_3'});
const afterRecord = await userRepository.findUnique(userId);
expect(afterRecord!.firstRefundAt!.getTime()).toBe(afterFallback!.firstRefundAt!.getTime());
expect(afterRecord!.premiumFlags & PremiumFlags.PURCHASE_DISABLED).toBe(0);
});
test('does not count a refund Fluxer issued itself against the refund allowance', async () => {
const account = await createTestAccount(harness);
const userId = createUserID(BigInt(account.userId));
const {PaymentRepository} = await import('../../user/repositories/PaymentRepository');
const {UserRepository} = await import('../../user/repositories/UserRepository');
const paymentRepository = new PaymentRepository();
const userRepository = new UserRepository();
const paymentIntentId = 'pi_test_refund_system_123';
const checkoutSessionId = 'cs_test_refund_system_123';
const chargeId = 'ch_test_refund_system_123';
await paymentRepository.createPayment({
checkout_session_id: checkoutSessionId,
user_id: userId,
price_id: 'price_test_monthly',
product_type: 'monthly_subscription',
status: 'completed',
is_gift: false,
created_at: new Date(),
});
await paymentRepository.updatePayment({
checkout_session_id: checkoutSessionId,
payment_intent_id: paymentIntentId,
completed_at: new Date(),
});
useRefundListHandler(chargeId, [
{
id: 're_test_refund_system_123',
object: 'refund',
charge: chargeId,
payment_intent: paymentIntentId,
amount: 2500,
currency: 'brl',
status: 'succeeded',
created: Math.floor(Date.now() / 1000),
metadata: {rejection_reason: 'duplicate_active_subscription'},
},
]);
await sendWebhook({
type: 'charge.refunded',
data: {
object: {
id: chargeId,
payment_intent: paymentIntentId,
amount_refunded: 2500,
},
},
});
const updatedUser = await userRepository.findUnique(userId);
expect(updatedUser!.firstRefundAt).toBeNull();
expect(updatedUser!.premiumFlags & PremiumFlags.PURCHASE_DISABLED).toBe(0);
});
test('does not count a refund Fluxer issued for a localized card country mismatch', async () => {
const account = await createTestAccount(harness);
const userId = createUserID(BigInt(account.userId));
const {PaymentRepository} = await import('../../user/repositories/PaymentRepository');
const {UserRepository} = await import('../../user/repositories/UserRepository');
const paymentRepository = new PaymentRepository();
const userRepository = new UserRepository();
const paymentIntentId = 'pi_test_refund_card_mismatch';
const checkoutSessionId = 'cs_test_refund_card_mismatch';
const chargeId = 'ch_test_refund_card_mismatch';
await paymentRepository.createPayment({
checkout_session_id: checkoutSessionId,
user_id: userId,
price_id: 'price_test_monthly',
product_type: 'monthly_subscription',
status: 'completed',
is_gift: false,
created_at: new Date(),
});
await paymentRepository.updatePayment({
checkout_session_id: checkoutSessionId,
payment_intent_id: paymentIntentId,
completed_at: new Date(),
});
useRefundListHandler(chargeId, [
{
id: 're_test_refund_card_mismatch',
object: 'refund',
charge: chargeId,
payment_intent: paymentIntentId,
amount: 2500,
currency: 'brl',
status: 'succeeded',
created: Math.floor(Date.now() / 1000),
metadata: {rejection_reason: 'localized_card_country_mismatch'},
},
]);
await sendWebhook({
type: 'charge.refunded',
data: {
object: {
id: chargeId,
payment_intent: paymentIntentId,
amount_refunded: 2500,
},
},
});
const updatedUser = await userRepository.findUnique(userId);
expect(updatedUser!.firstRefundAt).toBeNull();
expect(updatedUser!.premiumFlags & PremiumFlags.PURCHASE_DISABLED).toBe(0);
const updatedPayment = await userRepository.getPaymentByPaymentIntent(paymentIntentId);
expect(updatedPayment!.status).toBe('refunded');
});
test('counts only the customer refund when Fluxer issued a later system refund on the same charge', async () => {
const account = await createTestAccount(harness);
const userId = createUserID(BigInt(account.userId));
const {PaymentRepository} = await import('../../user/repositories/PaymentRepository');
const {UserRepository} = await import('../../user/repositories/UserRepository');
const paymentRepository = new PaymentRepository();
const userRepository = new UserRepository();
const paymentIntentId = 'pi_test_refund_mixed';
const checkoutSessionId = 'cs_test_refund_mixed';
const chargeId = 'ch_test_refund_mixed';
await paymentRepository.createPayment({
checkout_session_id: checkoutSessionId,
user_id: userId,
price_id: 'price_test_monthly',
product_type: 'monthly_subscription',
status: 'completed',
is_gift: false,
created_at: new Date(),
});
await paymentRepository.updatePayment({
checkout_session_id: checkoutSessionId,
payment_intent_id: paymentIntentId,
completed_at: new Date(),
});
const nowSeconds = Math.floor(Date.now() / 1000);
const chargeEvent = {
type: 'charge.refunded' as const,
data: {
object: {
id: chargeId,
payment_intent: paymentIntentId,
amount_refunded: 2500,
refunds: {
object: 'list',
has_more: false,
url: `/v1/charges/${chargeId}/refunds`,
data: [
{
id: 're_test_refund_mixed_customer',
object: 'refund',
charge: chargeId,
payment_intent: paymentIntentId,
amount: 1500,
currency: 'brl',
status: 'succeeded',
created: nowSeconds - 600,
metadata: {},
},
{
id: 're_test_refund_mixed_system',
object: 'refund',
charge: chargeId,
payment_intent: paymentIntentId,
amount: 1000,
currency: 'brl',
status: 'succeeded',
created: nowSeconds,
metadata: {rejection_reason: 'duplicate_active_subscription'},
},
],
},
},
},
};
await sendWebhook({...chargeEvent, id: 'evt_test_refund_mixed_1'});
const afterFirst = await userRepository.findUnique(userId);
expect(afterFirst!.firstRefundAt).not.toBeNull();
expect(afterFirst!.premiumFlags & PremiumFlags.PURCHASE_DISABLED).toBe(0);
await sendWebhook({...chargeEvent, id: 'evt_test_refund_mixed_2'});
const afterRetry = await userRepository.findUnique(userId);
expect(afterRetry!.firstRefundAt!.getTime()).toBe(afterFirst!.firstRefundAt!.getTime());
expect(afterRetry!.premiumFlags & PremiumFlags.PURCHASE_DISABLED).toBe(0);
});
test('counts a second, distinct refund against the allowance after the first one was already counted', async () => {
const account = await createTestAccount(harness);
const userId = createUserID(BigInt(account.userId));
const {PaymentRepository} = await import('../../user/repositories/PaymentRepository');
const {UserRepository} = await import('../../user/repositories/UserRepository');
const paymentRepository = new PaymentRepository();
const userRepository = new UserRepository();
const paymentIntentId = 'pi_test_refund_ladder';
const checkoutSessionId = 'cs_test_refund_ladder';
const chargeId = 'ch_test_refund_ladder';
await paymentRepository.createPayment({
checkout_session_id: checkoutSessionId,
user_id: userId,
price_id: 'price_test_monthly',
product_type: 'monthly_subscription',
status: 'completed',
is_gift: false,
created_at: new Date(),
});
await paymentRepository.updatePayment({
checkout_session_id: checkoutSessionId,
payment_intent_id: paymentIntentId,
completed_at: new Date(),
});
const nowSeconds = Math.floor(Date.now() / 1000);
function chargeEventWithRefunds(refunds: Array<Record<string, unknown>>) {
return {
type: 'charge.refunded' as const,
data: {
object: {
id: chargeId,
payment_intent: paymentIntentId,
amount_refunded: 2500,
refunds: {
object: 'list',
has_more: false,
url: `/v1/charges/${chargeId}/refunds`,
data: refunds,
},
},
},
};
}
const firstRefund = {
id: 're_test_refund_ladder_1',
object: 'refund',
charge: chargeId,
payment_intent: paymentIntentId,
amount: 1200,
currency: 'brl',
status: 'succeeded',
created: nowSeconds - 600,
metadata: {},
};
const secondRefund = {
...firstRefund,
id: 're_test_refund_ladder_2',
amount: 1300,
created: nowSeconds,
};
await sendWebhook({...chargeEventWithRefunds([firstRefund]), id: 'evt_test_refund_ladder_1'});
const afterFirst = await userRepository.findUnique(userId);
expect(afterFirst!.firstRefundAt).not.toBeNull();
expect(afterFirst!.premiumFlags & PremiumFlags.PURCHASE_DISABLED).toBe(0);
await sendWebhook({...chargeEventWithRefunds([firstRefund, secondRefund]), id: 'evt_test_refund_ladder_2'});
const afterSecond = await userRepository.findUnique(userId);
expect(afterSecond!.premiumFlags & PremiumFlags.PURCHASE_DISABLED).toBe(PremiumFlags.PURCHASE_DISABLED);
});
test('falls back to customer ID when payment intent is not indexed (subscription mode)', async () => {
const account = await createTestAccount(harness);
const userId = createUserID(BigInt(account.userId));
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {resetGeoipReadersForTesting} from '@pkgs/geoip/src/GeoipLookup';
import {resetSudoModeServiceForTesting} from '../auth/services/SudoModeService';
import {resetSsoRequestUrlPolicyForTesting} from '../instance/SsoConfigValidation';
import {resetGlobalLimitConfigServiceForTesting} from '../limits/LimitConfigService';
@@ -32,6 +33,7 @@ export function resetServiceStateForTesting(): void {
torExitListCache.shutdown();
torExitListCache.clearForTesting();
urlBlocklistCache.resetForTesting();
resetGeoipReadersForTesting();
fileShaCache.resetForTesting();
phraseBlocklistCache.resetForTesting();
bannedAvatarHashCache.resetForTesting();
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {HttpResponse, http} from 'msw';
export function createIpInfoLookupHandler() {
return http.get('https://api.ipinfo.io/lookup/:ip', ({params}) => {
const ip = typeof params.ip === 'string' ? params.ip : '198.51.100.1';
return HttpResponse.json({
ip,
geo: {
city: 'Ashburn',
region: 'Virginia',
region_code: 'VA',
country: 'United States',
country_code: 'US',
continent: 'North America',
continent_code: 'NA',
},
as: {
asn: 'AS64500',
name: 'Test ISP',
domain: 'example.com',
type: 'isp',
},
anonymous: {},
});
});
}
@@ -64,6 +64,14 @@ interface StripeApiMockConfig {
paymentMethods?: Record<string, Partial<MockStripePaymentMethod>>;
setupIntents?: Record<string, Partial<MockStripeSetupIntent>>;
subscriptions?: Record<string, Partial<MockStripeSubscriptionState>>;
prices?: Record<string, MockStripePriceOverrides>;
}
interface MockStripePriceOverrides {
unit_amount?: number;
currency?: string;
interval?: 'month' | 'year';
product?: string;
}
interface SubscriptionScheduleParams {
@@ -302,6 +310,7 @@ interface MockStripeSubscriptionState {
customer: string;
trial_end: number | null;
price_id: string;
unit_amount: number;
currency: string;
interval: 'month' | 'year';
item_id: string;
@@ -808,6 +817,7 @@ export function createStripeApiHandlers(config: StripeApiMockConfig = {}): Strip
customer: 'cus_test_1',
trial_end: null,
price_id: 'price_test_1',
unit_amount: 2500,
currency: 'usd',
interval: 'month',
item_id: 'si_test_1',
@@ -856,7 +866,7 @@ export function createStripeApiHandlers(config: StripeApiMockConfig = {}): Strip
price: {
id: subState.price_id,
object: 'price',
unit_amount: 2500,
unit_amount: subState.unit_amount,
currency: subState.currency,
recurring: {
interval: subState.interval,
@@ -1703,28 +1713,31 @@ export function createStripeApiHandlers(config: StripeApiMockConfig = {}): Strip
http.get(`${STRIPE_API_BASE}/v1/prices/:id`, ({params}) => {
const {id} = params;
const normalizedPriceId = String(id).toLowerCase();
const overrides = config.prices?.[String(id)];
return HttpResponse.json({
id,
object: 'price',
active: true,
currency: normalizedPriceId.includes('eur')
? 'eur'
: normalizedPriceId.includes('brl')
? 'brl'
: normalizedPriceId.includes('inr')
? 'inr'
: normalizedPriceId.includes('pln')
? 'pln'
: normalizedPriceId.includes('try')
? 'try'
: 'usd',
unit_amount: normalizedPriceId.includes('year') ? 4999 : 499,
currency:
overrides?.currency ??
(normalizedPriceId.includes('eur')
? 'eur'
: normalizedPriceId.includes('brl')
? 'brl'
: normalizedPriceId.includes('inr')
? 'inr'
: normalizedPriceId.includes('pln')
? 'pln'
: normalizedPriceId.includes('try')
? 'try'
: 'usd'),
unit_amount: overrides?.unit_amount ?? (normalizedPriceId.includes('year') ? 4999 : 499),
type: 'recurring',
recurring: {
interval: normalizedPriceId.includes('year') ? 'year' : 'month',
interval: overrides?.interval ?? (normalizedPriceId.includes('year') ? 'year' : 'month'),
interval_count: 1,
},
product: 'prod_test_1',
product: overrides?.product ?? 'prod_test_1',
livemode: false,
created: Math.floor(Date.now() / 1000) - 365 * 24 * 60 * 60,
});
+2
View File
@@ -2,6 +2,7 @@
import {setupServer} from 'msw/node';
import {createBunnyEdgeHandlers} from './handlers/BunnyEdgeHandlers';
import {createIpInfoLookupHandler} from './handlers/IpInfoHandlers';
import {createNcmecHandlers} from './handlers/NcmecHandlers';
import {createOnionooDetailsHandler} from './handlers/OnionooHandlers';
import {createOpenNsfwHandlers} from './handlers/OpenNsfwHandlers';
@@ -11,6 +12,7 @@ export const server = setupServer(
...createBunnyEdgeHandlers(),
...createNcmecHandlers(),
...createOpenNsfwHandlers(),
createIpInfoLookupHandler(),
createOnionooDetailsHandler(),
createPwnedPasswordsRangeHandler(),
);
@@ -441,16 +441,25 @@ function collectGuildText(doc: SearchableGuild): Array<string | null> {
return [doc.name, doc.vanityUrlCode, doc.discoveryDescription, ...doc.discoveryTags];
}
const sortGuildsByCreatedAt = sortNumericField<SearchableGuild, GuildSearchFilters>('createdAt', 'asc');
const sortGuildsByMemberCount = sortNumericField<SearchableGuild, GuildSearchFilters>('memberCount', 'desc');
function sortGuilds(left: SearchableGuild, right: SearchableGuild, filters: GuildSearchFilters, query: string): number {
const sorter = filters.sortBy === 'memberCount' ? sortGuildsByMemberCount : sortGuildsByCreatedAt;
const delta = sorter(left, right, filters, query);
if (delta !== 0) return delta;
const leftId = BigInt(left.id);
const rightId = BigInt(right.id);
if (leftId === rightId) return 0;
return leftId > rightId ? -1 : 1;
}
class InMemoryGuildSearchService
extends InMemorySearchServiceBase<GuildSearchFilters, SearchableGuild>
implements IGuildSearchService
{
constructor() {
super(
matchesGuildFilters,
collectGuildText,
sortNumericField<SearchableGuild, GuildSearchFilters>('createdAt', 'asc'),
);
super(matchesGuildFilters, collectGuildText, sortGuilds);
}
async indexGuild(guild: Guild, discovery?: GuildDiscoveryContext): Promise<void> {
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {describe, expect, it} from 'vitest';
import {isDerivedTrait, isServerManagedTrait, resolveAssignedTraits} from './UserTraits';
describe('UserTraits', () => {
it('treats premium as derived rather than assignable', () => {
expect(isDerivedTrait('premium')).toBe(true);
expect(isDerivedTrait('beta-tester')).toBe(false);
});
it('recognises every trait shape the server manages', () => {
expect(isServerManagedTrait('sso')).toBe(true);
expect(isServerManagedTrait('sso:acme')).toBe(true);
expect(isServerManagedTrait('sso_provider:0123456789abcdef')).toBe(true);
expect(isServerManagedTrait('sso_identity:0123456789abcdef')).toBe(true);
expect(isServerManagedTrait('registration_pending_approval')).toBe(true);
expect(isServerManagedTrait('registration_rejected')).toBe(true);
expect(isServerManagedTrait('beta-tester')).toBe(false);
});
it('keeps the operator traits it was given', () => {
expect([...resolveAssignedTraits([], ['beta-tester', 'experimental'])]).toEqual(['beta-tester', 'experimental']);
});
it('drops premium because nothing reads the stored value', () => {
expect([...resolveAssignedTraits([], ['premium'])]).toEqual([]);
expect([...resolveAssignedTraits([], ['beta-tester', 'premium'])]).toEqual(['beta-tester']);
});
it('keeps a premium user premium when the operator saves other traits', () => {
expect([...resolveAssignedTraits(['premium'], ['beta-tester'])]).toEqual(['beta-tester']);
});
it('preserves server managed traits the operator did not send', () => {
const resolved = resolveAssignedTraits(
['sso', 'sso:acme', 'sso_identity:0123456789abcdef', 'beta-tester'],
['experimental'],
);
expect([...resolved].sort()).toEqual(['experimental', 'sso', 'sso:acme', 'sso_identity:0123456789abcdef']);
});
it('keeps a pending approval user pending when their traits are cleared', () => {
expect([...resolveAssignedTraits(['registration_pending_approval'], [])]).toEqual([
'registration_pending_approval',
]);
});
it('refuses to let an operator forge a server managed trait', () => {
expect([...resolveAssignedTraits([], ['sso', 'sso_identity:forged'])]).toEqual([]);
});
it('ignores empty trait names', () => {
expect([...resolveAssignedTraits([], ['', 'beta-tester'])]).toEqual(['beta-tester']);
});
});
+31
View File
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
const DERIVED_TRAITS = new Set(['premium']);
const SERVER_MANAGED_TRAITS = new Set(['sso', 'registration_pending_approval', 'registration_rejected']);
const SERVER_MANAGED_TRAIT_PREFIXES = ['sso:', 'sso_provider:', 'sso_identity:'];
export function isDerivedTrait(trait: string): boolean {
return DERIVED_TRAITS.has(trait);
}
export function isServerManagedTrait(trait: string): boolean {
return SERVER_MANAGED_TRAITS.has(trait) || SERVER_MANAGED_TRAIT_PREFIXES.some((prefix) => trait.startsWith(prefix));
}
export function resolveAssignedTraits(current: Iterable<string>, requested: Iterable<string>): Set<string> {
const next = new Set<string>();
for (const trait of requested) {
if (!trait || isDerivedTrait(trait) || isServerManagedTrait(trait)) {
continue;
}
next.add(trait);
}
for (const trait of current) {
if (isServerManagedTrait(trait)) {
next.add(trait);
}
}
return next;
}
@@ -178,7 +178,7 @@ export class EmailChangeService {
}
const hasValidDns = await emailDnsValidation.hasValidDnsRecords(trimmedEmail);
if (!hasValidDns) {
throw InputValidationError.fromCode('new_email', ValidationErrorCodes.INVALID_EMAIL_ADDRESS);
throw InputValidationError.fromCode('new_email', ValidationErrorCodes.EMAIL_DOMAIN_CANNOT_RECEIVE_MAIL);
}
const existing = await users.findByEmail(trimmedEmail.toLowerCase());
if (existing && existing.id !== user.id) {
+44
View File
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {afterEach, describe, expect, it} from 'vitest';
import {setCachedDateOfBirthCollection} from '../instance/DateOfBirthCollectionCache';
import {canUserAccessNsfwContent} from './AgeUtils';
const ADULT_DATE_OF_BIRTH = '1990-01-01';
const MINOR_DATE_OF_BIRTH = '2020-01-01';
describe('canUserAccessNsfwContent', () => {
afterEach(() => {
setCachedDateOfBirthCollection(true);
});
it('allows a bot whatever the instance collects', () => {
setCachedDateOfBirthCollection(true);
expect(canUserAccessNsfwContent({isBot: true, dateOfBirth: null})).toBe(true);
});
it('allows an adult when the instance collects a date of birth', () => {
setCachedDateOfBirthCollection(true);
expect(canUserAccessNsfwContent({isBot: false, dateOfBirth: ADULT_DATE_OF_BIRTH})).toBe(true);
});
it('blocks a minor when the instance collects a date of birth', () => {
setCachedDateOfBirthCollection(true);
expect(canUserAccessNsfwContent({isBot: false, dateOfBirth: MINOR_DATE_OF_BIRTH})).toBe(false);
});
it('blocks a missing date of birth when the instance collects one', () => {
setCachedDateOfBirthCollection(true);
expect(canUserAccessNsfwContent({isBot: false, dateOfBirth: null})).toBe(false);
});
it('allows a missing date of birth when the instance collects none', () => {
setCachedDateOfBirthCollection(false);
expect(canUserAccessNsfwContent({isBot: false, dateOfBirth: null})).toBe(true);
});
it('allows an account with a minor date of birth when the instance collects none', () => {
setCachedDateOfBirthCollection(false);
expect(canUserAccessNsfwContent({isBot: false, dateOfBirth: MINOR_DATE_OF_BIRTH})).toBe(true);
});
});
+5
View File
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {instanceCollectsDateOfBirth} from '../instance/DateOfBirthCollectionCache';
export function calculateAge(
dateOfBirth:
| {
@@ -53,5 +55,8 @@ export function canUserAccessNsfwContent(user: NsfwEligibilityUser): boolean {
if (user.isBot) {
return true;
}
if (!instanceCollectsDateOfBirth()) {
return true;
}
return isUserAdult(user.dateOfBirth);
}
@@ -102,6 +102,7 @@ async function parseWebhookMultipartMessageData(
onPayloadParsed(payload) {
parsedPayload = payload;
},
actor: 'webhook',
},
);
if (!parsedPayload) {
@@ -1,12 +1,14 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {Permissions} from '@fluxer/constants/src/ChannelConstants';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {createTestAccount} from '../../auth/tests/AuthTestUtils';
import {loadFixture} from '../../channel/tests/AttachmentTestUtils';
import {createGuild} from '../../guild/tests/GuildTestUtils';
import {createPermissionOverwrite} from '../../channel/tests/ChannelTestUtils';
import {acceptInvite, addMemberRole, createGuild, createRole} from '../../guild/tests/GuildTestUtils';
import {type ApiTestHarness, createApiTestHarness} from '../../test/ApiTestHarness';
import {HTTP_STATUS} from '../../test/TestConstants';
import {createWebhook, deleteWebhook, executeWebhookWithAttachments} from './WebhookTestUtils';
import {createChannelInvite, createWebhook, deleteWebhook, executeWebhookWithAttachments} from './WebhookTestUtils';
describe('Webhook multipart attachment uploads', () => {
let harness: ApiTestHarness;
@@ -92,6 +94,36 @@ describe('Webhook multipart attachment uploads', () => {
expect(response.status).toBe(HTTP_STATUS.BAD_REQUEST);
await deleteWebhook(harness, webhook.id, owner.token);
});
it('executes multipart webhook requests when the creator is denied attach files', async () => {
const owner = await createTestAccount(harness);
const creator = await createTestAccount(harness);
const guild = await createGuild(harness, owner.token, 'Webhook creator denied attach files guild');
const channelId = guild.system_channel_id!;
const invite = await createChannelInvite(harness, owner.token, channelId);
await acceptInvite(harness, creator.token, invite.code);
const webhookManagerRole = await createRole(harness, owner.token, guild.id, {
name: 'Webhook Manager',
permissions: Permissions.MANAGE_WEBHOOKS.toString(),
});
await addMemberRole(harness, owner.token, guild.id, creator.userId, webhookManagerRole.id);
const webhook = await createWebhook(harness, channelId, creator.token, 'Denied Creator Webhook');
await createPermissionOverwrite(harness, owner.token, channelId, creator.userId, {
type: 1,
allow: '0',
deny: Permissions.ATTACH_FILES.toString(),
});
const {response, json} = await executeWebhookWithAttachments(harness, {
webhookId: webhook.id,
webhookToken: webhook.token,
payload: {
attachments: [{id: 0, filename: 'denied_creator.png'}],
},
files: [{index: 0, filename: 'denied_creator.png', data: loadFixture('yeah.png')}],
});
expect(response.status).toBe(HTTP_STATUS.OK);
expect(json?.attachments?.length).toBe(1);
await deleteWebhook(harness, webhook.id, owner.token);
});
it('rejects multipart webhook requests when file indices do not match metadata IDs', async () => {
const owner = await createTestAccount(harness);
const guild = await createGuild(harness, owner.token, 'Webhook multipart id mismatch guild');
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';
import {DELETED_USER_ID, DELETED_USER_USERNAME} from '@fluxer/constants/src/UserConstants';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {createTestAccount} from '../../auth/tests/AuthTestUtils';
@@ -13,10 +12,6 @@ import {createWebhook, executeWebhook, executeWebhookWithAttachments, getChannel
const VANISHED_CREATOR_ID = createUserID(999999999999999997n);
function parseErrorCode(text: string): string | undefined {
return (JSON.parse(text) as {code?: string}).code;
}
describe('Webhook whose creating account cannot be resolved', () => {
let harness: ApiTestHarness;
beforeEach(async () => {
@@ -49,13 +44,13 @@ describe('Webhook whose creating account cannot be resolved', () => {
expect(listed?.user.id).toBe(VANISHED_CREATOR_ID.toString());
expect(listed?.user.username).toBe(DELETED_USER_USERNAME);
});
it('answers a multipart execution for a webhook with no creator id with an access decision', async () => {
it('executes a multipart payload for a webhook with no creator id', async () => {
const owner = await createTestAccount(harness);
const guild = await createGuild(harness, owner.token, 'Null creator multipart guild');
const channelId = guild.system_channel_id!;
const webhook = await createWebhook(harness, channelId, owner.token, 'Null Creator Multipart Webhook');
await new WebhookRepository().update(createWebhookID(BigInt(webhook.id)), {creatorId: null});
const {response, text} = await executeWebhookWithAttachments(harness, {
const {response, json} = await executeWebhookWithAttachments(harness, {
webhookId: webhook.id,
webhookToken: webhook.token,
payload: {
@@ -63,16 +58,16 @@ describe('Webhook whose creating account cannot be resolved', () => {
},
files: [{index: 0, filename: 'orphaned.txt', data: Buffer.from('uploaded by an orphaned webhook')}],
});
expect(response.status).toBe(HTTP_STATUS.FORBIDDEN);
expect(parseErrorCode(text)).toBe(APIErrorCodes.ACCESS_DENIED);
expect(response.status).toBe(HTTP_STATUS.OK);
expect(json?.attachments?.[0].filename).toBe('orphaned.txt');
});
it('answers a multipart execution for a webhook whose creator row is gone with an access decision', async () => {
it('executes a multipart payload for a webhook whose creator row is gone', async () => {
const owner = await createTestAccount(harness);
const guild = await createGuild(harness, owner.token, 'Vanished creator multipart guild');
const channelId = guild.system_channel_id!;
const webhook = await createWebhook(harness, channelId, owner.token, 'Vanished Creator Multipart Webhook');
await new WebhookRepository().update(createWebhookID(BigInt(webhook.id)), {creatorId: VANISHED_CREATOR_ID});
const {response, text} = await executeWebhookWithAttachments(harness, {
const {response, json} = await executeWebhookWithAttachments(harness, {
webhookId: webhook.id,
webhookToken: webhook.token,
payload: {
@@ -80,8 +75,8 @@ describe('Webhook whose creating account cannot be resolved', () => {
},
files: [{index: 0, filename: 'vanished.txt', data: Buffer.from('uploaded by a vanished creator')}],
});
expect(response.status).toBe(HTTP_STATUS.FORBIDDEN);
expect(parseErrorCode(text)).toBe(APIErrorCodes.ACCESS_DENIED);
expect(response.status).toBe(HTTP_STATUS.OK);
expect(json?.attachments?.[0].filename).toBe('vanished.txt');
});
it('executes a json payload for a webhook with no creator id', async () => {
const owner = await createTestAccount(harness);
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {describe, expect, it} from 'vitest';
import {type ChannelID, createChannelID, createMessageID, createUserID, type MessageID} from '../../BrandedTypes';
import {harvestMessages} from './HarvestUserData';
const AUTHOR = createUserID(1000000000000000000n);
function makeRepository(refs: Array<{channelId: ChannelID; messageId: MessageID}>, missing = new Set<string>()) {
let pages = 0;
return {
pages: () => pages,
listMessagesByAuthor: async (_userId: typeof AUTHOR, limit: number, lastMessageId?: MessageID) => {
pages++;
const start = lastMessageId ? refs.findIndex((r) => r.messageId === lastMessageId) + 1 : 0;
return refs.slice(start, start + limit);
},
getMessage: async (_channelId: ChannelID, messageId: MessageID) =>
missing.has(messageId.toString()) ? null : {content: `body ${messageId.toString()}`, attachments: undefined},
};
}
function refsAcross(channelCount: number, perChannel: number) {
const out: Array<{channelId: ChannelID; messageId: MessageID}> = [];
let id = 1500000000000000000n;
for (let c = 0; c < channelCount; c++) {
const channelId = createChannelID(2000000000000000000n + BigInt(c));
for (let m = 0; m < perChannel; m++) {
out.push({channelId, messageId: createMessageID(id)});
id += 1n;
}
}
return out;
}
describe('harvestMessages', () => {
it('reads past a single page instead of stopping at one query', async () => {
const refs = refsAcross(1, 2500);
const repo = makeRepository(refs);
const result = await harvestMessages(repo, AUTHOR, Date.now(), null);
expect(result.totalMessages).toBe(2500);
expect(repo.pages()).toBeGreaterThan(1);
});
it('groups every message under its own channel', async () => {
const repo = makeRepository(refsAcross(3, 4));
const result = await harvestMessages(repo, AUTHOR, Date.now(), null);
expect(result.channelMessagesMap.size).toBe(3);
for (const messages of result.channelMessagesMap.values()) {
expect(messages).toHaveLength(4);
}
expect(result.totalMessages).toBe(12);
});
it('leaves out a message the repository cannot return', async () => {
const refs = refsAcross(1, 5);
const repo = makeRepository(refs, new Set([refs[2].messageId.toString()]));
const result = await harvestMessages(repo, AUTHOR, Date.now(), null);
expect(result.totalMessages).toBe(4);
});
it('returns an empty map for an account with no messages', async () => {
const repo = makeRepository([]);
const result = await harvestMessages(repo, AUTHOR, Date.now(), null);
expect(result.totalMessages).toBe(0);
expect(result.channelMessagesMap.size).toBe(0);
});
});
@@ -171,6 +171,10 @@ interface ArchiveResult {
downloadUrl: string;
}
// A harvest is every message the account wrote, so the read pages to the end of
// the account rather than stopping at a count. The page size is what bounds one
// query, not what bounds the archive.
const HARVEST_MESSAGE_CHUNK_SIZE = 1000;
const CONCURRENT_MESSAGE_LIMIT = 10;
const INITIAL_PROGRESS = 5;
const MESSAGES_PROGRESS_MAX = 55;
@@ -226,11 +230,12 @@ interface HarvestMessagesFilterArgs {
findChannel: (channelId: ChannelID) => Promise<Channel | null>;
}
async function harvestMessages(
export async function harvestMessages(
channelRepository: {
listMessagesByAuthor: (
userId: UserID,
limit: number,
lastMessageId?: MessageID,
) => Promise<
Array<{
channelId: ChannelID;
@@ -250,24 +255,78 @@ async function harvestMessages(
filterArgs: HarvestMessagesFilterArgs | null,
): Promise<HarvestMessageResult> {
const channelMessagesMap = new Map<string, Array<HarvestedMessage>>();
const channelEligibility = filterArgs ? new Map<string, boolean>() : null;
Logger.debug('Fetching all user messages');
const startFetchTime = Date.now();
const messageRefs = await channelRepository.listMessagesByAuthor(userId, 100000);
Logger.debug(
{
totalMessages: messageRefs.length,
fetchElapsed: Date.now() - startFetchTime,
totalElapsed: Date.now() - startTime,
},
'All messages retrieved',
);
if (messageRefs.length === 0) {
return {channelMessagesMap, totalMessages: 0};
}
const channelEligibility = filterArgs ? new Map<string, boolean>() : null;
const filteredRefs: Array<{channelId: ChannelID; messageId: MessageID}> = [];
if (filterArgs) {
for (const ref of messageRefs) {
let lastMessageId: MessageID | undefined;
let scannedMessages = 0;
let totalMessages = 0;
const readMessage = async ({
channelId,
messageId,
}: {
channelId: ChannelID;
messageId: MessageID;
}): Promise<ChannelHarvestResult | null> => {
try {
const message = await channelRepository.getMessage(channelId, messageId);
if (!message) {
Logger.warn(
{channelId: channelId.toString(), messageId: messageId.toString()},
'Message not found during harvest',
);
return null;
}
const timestamp = snowflakeToDate(messageId);
const attachments: Array<HarvestedAttachment> = [];
if (message.attachments) {
for (const attachment of message.attachments) {
attachments.push({
attachment_id: attachment.id.toString(),
filename: attachment.filename,
size: attachment.size.toString(),
content_type: attachment.contentType,
content_hash: null,
archive_path: null,
cdn_url: makeAttachmentCdnUrl(channelId, attachment.id, attachment.filename),
width: attachment.width,
height: attachment.height,
});
}
}
return {
channelId: channelId.toString(),
messageData: {
id: messageId.toString(),
timestamp: timestamp.toISOString(),
content: message.content ?? '',
attachments,
},
};
} catch (error) {
Logger.error(
{error, channelId: channelId.toString(), messageId: messageId.toString()},
'Failed to process message during harvest',
);
return null;
}
};
while (true) {
const page = await channelRepository.listMessagesByAuthor(userId, HARVEST_MESSAGE_CHUNK_SIZE, lastMessageId);
if (page.length === 0) {
break;
}
scannedMessages += page.length;
lastMessageId = page[page.length - 1].messageId;
const pageRefs: Array<{channelId: ChannelID; messageId: MessageID}> = [];
for (const ref of page) {
if (!filterArgs) {
pageRefs.push(ref);
continue;
}
const ts = snowflakeToDate(ref.messageId).getTime();
if (!isTimestampInWindow(ts, filterArgs.filter)) {
continue;
@@ -280,76 +339,41 @@ async function harvestMessages(
channelEligibility!.set(channelIdStr, eligible);
}
if (eligible) {
filteredRefs.push(ref);
pageRefs.push(ref);
}
}
} else {
filteredRefs.push(...messageRefs);
}
if (filteredRefs.length === 0) {
return {channelMessagesMap, totalMessages: 0};
}
const messages: Array<ChannelHarvestResult> = [];
for (let i = 0; i < filteredRefs.length; i += CONCURRENT_MESSAGE_LIMIT) {
const batch = filteredRefs.slice(i, i + CONCURRENT_MESSAGE_LIMIT);
const batchPromises = batch.map(async ({channelId, messageId}): Promise<ChannelHarvestResult | null> => {
try {
const message = await channelRepository.getMessage(channelId, messageId);
if (!message) {
Logger.warn(
{channelId: channelId.toString(), messageId: messageId.toString()},
'Message not found during harvest',
);
return null;
for (let i = 0; i < pageRefs.length; i += CONCURRENT_MESSAGE_LIMIT) {
const batchResults = await Promise.all(pageRefs.slice(i, i + CONCURRENT_MESSAGE_LIMIT).map(readMessage));
for (const result of batchResults) {
if (result === null) {
continue;
}
const timestamp = snowflakeToDate(messageId);
const attachments: Array<HarvestedAttachment> = [];
if (message.attachments) {
for (const attachment of message.attachments) {
attachments.push({
attachment_id: attachment.id.toString(),
filename: attachment.filename,
size: attachment.size.toString(),
content_type: attachment.contentType,
content_hash: null,
archive_path: null,
cdn_url: makeAttachmentCdnUrl(channelId, attachment.id, attachment.filename),
width: attachment.width,
height: attachment.height,
});
}
let bucket = channelMessagesMap.get(result.channelId);
if (!bucket) {
bucket = [];
channelMessagesMap.set(result.channelId, bucket);
}
return {
channelId: channelId.toString(),
messageData: {
id: messageId.toString(),
timestamp: timestamp.toISOString(),
content: message.content ?? '',
attachments,
},
};
} catch (error) {
Logger.error(
{error, channelId: channelId.toString(), messageId: messageId.toString()},
'Failed to process message during harvest',
);
return null;
}
});
const batchResults = await Promise.all(batchPromises);
for (const result of batchResults) {
if (result !== null) {
messages.push(result);
bucket.push(result.messageData);
totalMessages++;
}
}
}
for (const {channelId, messageData} of messages) {
if (!channelMessagesMap.has(channelId)) {
channelMessagesMap.set(channelId, []);
if (page.length < HARVEST_MESSAGE_CHUNK_SIZE) {
break;
}
channelMessagesMap.get(channelId)!.push(messageData);
}
return {channelMessagesMap, totalMessages: messages.length};
Logger.debug(
{
scannedMessages,
totalMessages,
fetchElapsed: Date.now() - startFetchTime,
totalElapsed: Date.now() - startTime,
},
'All messages retrieved',
);
return {channelMessagesMap, totalMessages};
}
function buildUserDataJson(params: UserDataJsonParams) {

Some files were not shown because too many files have changed in this diff Show More