feat(server): rate limit sign-in, signup, fetches, uploads, and writes (#6315)

This commit is contained in:
boojack
2026-09-13 11:58:26 +08:00
committed by GitHub
parent 751005bc19
commit c506f67d22
43 changed files with 2801 additions and 148 deletions
+16 -10
View File
@@ -66,6 +66,8 @@ func init() {
rootCmd.Flags().Bool("allow-private-webhooks", false, "allow webhooks to access any private/reserved IP address")
rootCmd.Flags().StringSlice("webhook-private-network-allowlist", nil, "private webhook destinations to allow (exact hostname, IP, or CIDR)")
rootCmd.Flags().String("log-level", "info", "log verbosity level (debug, info, warn, error)")
rootCmd.Flags().Bool("rate-limit", true, "enable request rate limiting")
rootCmd.Flags().StringSlice("trusted-proxies", []string{"private"}, "proxies whose forwarding headers identify the client: CIDRs, IPs, \"private\" (default; the loopback and private ranges), or \"none\" when the instance is reached without a header-rewriting proxy")
if err := rootCmd.Flags().MarkDeprecated("allow-private-webhooks", "use --webhook-private-network-allowlist to allow only required destinations"); err != nil {
panic(err)
@@ -82,6 +84,8 @@ func init() {
"allow-private-webhooks",
"webhook-private-network-allowlist",
"log-level",
"rate-limit",
"trusted-proxies",
} {
if err := viper.BindPFlag(key, rootCmd.Flags().Lookup(key)); err != nil {
panic(err)
@@ -97,16 +101,18 @@ func init() {
func runServer() error {
instanceProfile := &profile.Profile{
Demo: viper.GetBool("demo"),
Addr: viper.GetString("addr"),
Port: viper.GetInt("port"),
UNIXSock: viper.GetString("unix-sock"),
Data: viper.GetString("data"),
Driver: viper.GetString("driver"),
DSN: viper.GetString("dsn"),
InstanceURL: viper.GetString("instance-url"),
Version: version.GetCurrentVersion(),
Commit: version.Commit,
Demo: viper.GetBool("demo"),
Addr: viper.GetString("addr"),
Port: viper.GetInt("port"),
UNIXSock: viper.GetString("unix-sock"),
Data: viper.GetString("data"),
Driver: viper.GetString("driver"),
DSN: viper.GetString("dsn"),
InstanceURL: viper.GetString("instance-url"),
RateLimit: viper.GetBool("rate-limit"),
TrustedProxies: viper.GetStringSlice("trusted-proxies"),
Version: version.GetCurrentVersion(),
Commit: version.Commit,
}
allowPrivateWebhooks := viper.GetBool("allow-private-webhooks")
+219
View File
@@ -0,0 +1,219 @@
# API Abuse Controls Design
Status: Accepted
Date: 2026-09-12
Existing domain language: [Memos context](../../CONTEXT.md)
Related: [Unique Email Design](unique-email.md), [Multi-Spaces Design](multi-spaces.md)
## Summary
Memos has no rate limiting anywhere. A single client can guess passwords, create accounts, make the server fetch arbitrary URLs, or flood writes as fast as the server answers. On a private self-hosted instance the exposure is brute force against the admin account; on a public instance it is an open door.
This design adds a small in-process limiter driven by a declarative policy table, enforced in two places: catch-all request budgets in the shared authorizer that every transport passes through, and tight per-flow limits inside the handlers that need request fields. It defines one error contract for "slow down" that follows Google's API Improvement Proposals and the IETF rate-limit headers, and two seams a deployment fills: a challenge verifier and a signup policy. Everything ships in the open-source binary with conservative defaults, one switch to turn it off, and one setting that says which proxies to trust when reading the client address.
## Goals
- Bound failed sign-in attempts per client address and per submitted account name, so password guessing is slow and the server does not spend bcrypt time on floods.
- Bound account creation, link-metadata fetches, uploads, AI transcription, and content writes, each per the identity that best describes the caller.
- Give anonymous callers on a public instance one overall request budget, and authenticated callers another, so no endpoint needs its own rule to be protected at all.
- Count batch requests by their item count, so batching cannot bypass a limit.
- Reserve named limits for password reset so that flow plugs in without a new design.
- Derive the client address in one place from a configurable set of trusted proxies, so the limiter cannot be bypassed with a forged header and the sessions page stops showing forged addresses.
- Return one error contract that generic clients, the web app, and the future metering work all understand.
- Expose a challenge seam and a signup-policy seam that a deployment fills without changing the open-source code paths.
## Non-goals
- A distributed or persistent limiter. cloud.usememos.com is one node; self-host is one node. The limiter interface allows another implementation later.
- A captcha implementation, provider SDK, or admin UI for choosing one.
- Bans, IP reputation, disposable-domain lists, or moderation tooling.
- Byte-based upload, storage, or AI quotas. Those are metering, designed separately; this design only reserves the error reason they will use.
- Content-quality spam detection. A write budget bounds volume, not intent.
- Limits on file and avatar serving. That is bandwidth, best handled by a cache in front of the instance.
- Enumeration resistance beyond what sign-in already does. Signup with a taken email still returns `AlreadyExists`; email verification is the design that changes that.
## Current state
Facts verified in the code on 2026-09-12:
- No rate limiter, lockout, attempt counter, or captcha exists in `server/`, `internal/`, or `store/`.
- `SignIn` looks the user up, runs bcrypt, and only then checks instance settings. Every request pays the hash cost.
- Both transports share one authorizer: the Connect interceptor and the gRPC-gateway middleware each call `Authenticate` and then `CheckAccess` with the procedure name. The MCP adapter forwards a bearer token through the same HTTP API, so it passes the same code. This is the one place that sees every request with its procedure and identity.
- The client address is read in `extractClientInfo` for session records: the first entry of `X-Forwarded-For`, else `X-Real-Ip`, with no notion of which proxies are trusted. Any client can choose the address that gets recorded.
- `GetLinkMetadata` and `BatchGetLinkMetadata` are anonymous-allowed and fetch arbitrary URLs. They have private-address guards, a five-second timeout, a cache, and a batch cap of ten, but no per-caller limit.
- `Transcribe` is authenticated and caps audio at 25 MiB. Memo, comment, reaction, and share creation are authenticated with no ceiling.
- Batch endpoints already cap their item counts: one hundred usernames, ten URLs. Pages are capped at one thousand items.
- Request bodies are capped at the gateway per procedure, with a larger cap for the upload procedure.
## Research
Research completed on 2026-09-12 compared the API Improvement Proposals, Google's service-configuration quota model, the IETF rate-limit header draft, and OWASP API Security Top 10.
| Source | What it prescribes | What Memos takes from it |
| --- | --- | --- |
| AIP-193 Errors | Every error is a `google.rpc.Status` with a canonical code and a mandatory `ErrorInfo` detail carrying a stable `reason`, a `domain`, and `metadata` for the dynamic parts. | One code, `RESOURCE_EXHAUSTED`, with the reason distinguishing a throttle from a quota. |
| AIP-194 Automatic retry | `RESOURCE_EXHAUSTED` is generally non-retryable because it usually means quota, unless the server signals a short recovery. | Throttles must carry `RetryInfo`; quota refusals must not. |
| `google.api.Quota` service config | Limits are declared as a table of metrics, per-method costs, and per-consumer limits per window. Enforcement is one allocate call; infrastructure failure fails open. | A policy table with per-method costs and one consumer key per caller kind; fail-open as a stated invariant. |
| IETF `draft-ietf-httpapi-ratelimit-headers-11` (May 2026) | `RateLimit` and `RateLimit-Policy` structured header fields; `Retry-After` takes precedence on a 429. | Emit both on every refusal so generic HTTP clients behave. |
| OWASP API4:2023 | Rate limits per client, user, and address; per-operation throttles on login, one-time codes, and password recovery; caps on payload size and on operations per request; spending limits on third-party calls. | The tight per-flow scopes, batch-as-item-count, and the link-metadata and transcription limits. |
There is no AIP that prescribes a rate-limiting policy. The AIPs fix the error shape; Google's actual enforcement lives in service configuration outside the service code. The declarative-table idea transfers; the centralized allocate service does not, since Memos is one process.
## Proposed design
### Client identity
One resolver in a new `internal/clientip` package turns a request into a client address:
1. Take the peer address of the TCP connection.
2. If the peer is not in the trusted-proxy set, the client address is the peer. Forwarding headers are ignored.
3. If the peer is trusted, walk `X-Forwarded-For` from the right, skipping entries that are themselves trusted proxies. The first untrusted entry is the client. If every entry is trusted or the header is absent, fall back to `X-Real-Ip` only if the peer is trusted, else to the peer.
The trusted-proxy set is a profile option, `trusted-proxies`, set the same way as the other profile options. It accepts a list of CIDRs and two keywords: `private` expands to loopback, RFC 1918, link-local, and unique-local ranges; `none` trusts nothing. The default is `private`. That matches the common self-host layout, a reverse proxy on the same host or Docker network, without trusting the public internet. An operator whose proxy sits on a public address lists it explicitly.
The default has a known residual risk: any peer on the private network is believed when it forwards an address. On a network where other hosts are hostile, or where the instance is reached directly through a NAT hop that presents a private peer address without rewriting headers, a client can forge `X-Forwarded-For` and obtain a fresh per-address bucket. Such an operator sets `none` or lists the real proxy. The alternative default, `none`, was rejected because it would break the common layout worse: every user behind the proxy would share one bucket, so a busy public instance behind nginx would throttle everyone together. The per-account sign-in limit does not depend on the address and holds either way.
The resolver runs once, in an Echo middleware ahead of every route, and stores the result in the request context. Both transports derive their handler context from that request, so the authorizer and every handler read the address from the context and never from a header. `extractClientInfo` switches to it, so session records stop trusting forged headers as a side effect.
### Limiter
A new `internal/ratelimit` package provides a sliding-window counter:
```text
Consume(scope, key, cost) Decision{Allowed, Rule, Remaining, RetryAfter}
Refund(scope, key, cost)
Allowed(scope, key, cost) Decision
Hit(scope, key, cost)
```
`Consume` admits and records in one critical section, so concurrent requests cannot all be admitted on the strength of the same remaining unit; it is what every-attempt-counts callers use. Sign-in counts only failures, but it still consumes up front and calls `Refund` once the credential is proven, so a burst of concurrent guesses cannot slip past the per-account limit while the first bcrypt is still running. A refused `Consume` charges nothing. `Allowed` and `Hit` remain for callers that need neither.
The advertised delay is the first whole second at which a retry would be admitted, computed by inverting the estimate with the same integer truncation. It is not the window boundary, where the previous window still counts in full.
State lives in memory, keyed by `scope + key`, with a capacity cap. At capacity, a new key inspects a bounded sample of entries and evicts the expired ones, so the work per request stays constant under the lock rather than scanning the whole table; a table that has been full for a while is mostly expired entries, so the sample frees room in practice. When the sample holds nothing expired, the limiter allows the request and logs a warning once per minute. Failing open is deliberate and has precedent in Google's own enforcement guidance: a limiter that fails closed under memory pressure is a self-inflicted outage, and the capacity cap only matters to an attacker who controls that many real source addresses, since forged addresses are no longer counted.
A restart clears all state. That is acceptable for abuse control on one node.
### Policy table
A `ratelimit.Policy` is a table. Each row names a scope, the kind of key it is counted against, a limit, a window, and which procedures feed it with what cost. The open-source binary ships a default table; the server options seam planned for the cloud module accepts a replacement table and a replacement limiter, so a deployment tightens numbers or backs the limiter with shared storage without touching the handlers.
| Scope | Key | Feeds it | Counts | Default |
| --- | --- | --- | --- | --- |
| `anonymous` | client address | every procedure reached without an identity | each request; batch requests cost their item count | 300 per minute |
| `authenticated` | user id | every procedure reached with an identity | each request; batch requests cost their item count | 600 per minute |
| `signin_ip` | client address | `SignIn` | failed password and SSO sign-ins | 30 per 15 minutes |
| `signin_account` | submitted username, as submitted | `SignIn` with password credentials | failed sign-ins | 10 per 15 minutes |
| `signup_ip` | client address | `CreateUser` by a non-admin caller, not `validate_only` | every attempt | 10 per hour |
| `validate_ip` | client address | `CreateUser` with `validate_only` | every attempt | 60 per minute |
| `password_reset_ip` | client address | reserved for the reset flow | every attempt | 10 per hour |
| `password_reset_email` | canonical email | reserved for the reset flow | every attempt | 3 per hour |
| `link_metadata` | client address, or user id when authenticated | `GetLinkMetadata`, `BatchGetLinkMetadata` | each URL | 60 per minute |
| `upload_user` | user id | upload start, `CreateAttachment` | every start | 120 per minute |
| `transcribe_user` | user id | `Transcribe` | every call | 20 per hour |
| `write_user` | user id | memo, comment, reaction, share, memo-view, and webhook creation | every call | 120 per minute |
The two catch-all scopes are what make the table complete: a procedure nobody thought to list is still bounded. The specific scopes sit on top with tighter numbers. A request can therefore be refused by either its catch-all or its specific scope, and the first refusal wins.
The numbers are starting points chosen so that a household or small office behind one address is never throttled in ordinary use, while a password guesser is held to a few hundred attempts a day per address and an anonymous fetcher cannot turn the instance into a proxy. They are not tuned against measurements and are expected to change.
`signin_account` is keyed on the submitted username whether or not such a user exists. If it counted only real accounts, hitting the limit would reveal that the account exists.
A failed sign-in is a wrong password, an unknown username, or a rejected SSO exchange. A sign-in refused because the user is archived or password auth is disabled has already proven the credential and is not counted. Admin-authenticated `CreateUser` feeds only the `authenticated` budget. Sessions refreshed from a refresh token feed nothing; the token is the proof.
### Enforcement points
The catch-all budgets are enforced once, in the shared authorizer, immediately after `CheckAccess` succeeds. At that point the identity is known on either transport, and the MCP adapter passes the same code. The authorizer counts one unit per request; it cannot see item counts because the gateway has not parsed the body yet. The batch handlers add the rest, `items - 1`, to the caller's catch-all budget once they have validated the request, so a batch of n items costs n in total.
The specific scopes are enforced in the service methods, because they need request fields:
- `SignIn`, before the user lookup and before bcrypt: consume one unit of `signin_ip` and, for password credentials, of `signin_account`. Once the credential is proven, whether the sign-in then succeeds or is refused for an archived user or disabled password auth, refund both. A wrong password, an unknown username, or a rejected SSO exchange keeps the units.
- `CreateUser`: `signup_ip` or `validate_ip` after the admin check and before any store call.
- `GetLinkMetadata` and `BatchGetLinkMetadata`: `link_metadata`, costing the number of URLs, before any fetch. Cached results still cost, since the point is to bound how often a caller can ask.
- Upload start and `CreateAttachment`: `upload_user` after authentication.
- `Transcribe`: `transcribe_user` before the model call.
- The write procedures: `write_user` before the store call.
- The reset scopes are defined and tested against the limiter now; the reset RPC calls them when it exists.
### Error contract
A refused request returns `RESOURCE_EXHAUSTED` with two details:
- `google.rpc.ErrorInfo` with `reason` `RATE_LIMITED`, `domain` `memos.api.v1`, and `metadata` carrying `scope`, `retry_after_seconds`, `limit`, `window_seconds`, and `remaining`.
- `google.rpc.RetryInfo` carrying the same delay, so a client following AIP-194 knows this refusal recovers in seconds rather than hours.
Connect and the gRPC gateway both map the code to HTTP 429. Both error writers also set `Retry-After` from the delay and a `RateLimit` structured header with the remaining count and the window, following the IETF draft. `RateLimit-Policy` is emitted alongside so a client can learn the rule that tripped.
The metering work will use the same code with `reason` `QUOTA_EXCEEDED`, a `QuotaFailure` detail, and no `RetryInfo`. The web app's error handler reads the reason, so one handler covers both: a throttle shows a wait message, a quota refusal shows an upgrade link when the instance profile carries one. The api protos never import `google.rpc`, so the web app decodes the three-field `ErrorInfo` by hand rather than generating a schema for it.
`Status.message` is generic: "too many requests, try again later". It does not say which scope tripped; the scope is in the structured metadata for clients that want it, and the log line has it for the operator.
### Challenge seam
`ratelimit.Challenge` is an interface with `Verify(ctx, scope, token, clientIP) error` plus `Provider()` and `SiteKey()`, which feed the instance profile. The default is nil, meaning no challenge is configured. A deployment supplies one by setting the field on the API service.
When a challenge is configured, `CreateUser` for non-admin callers and `SignIn` with password credentials require a token. The token travels in the `Challenge-Token` request header; the Connect metadata interceptor forwards it, and the gateway's incoming-header matcher is extended to do the same. A missing or failed token returns `FAILED_PRECONDITION` with `ErrorInfo.reason` `CHALLENGE_REQUIRED`, distinct from the rate-limit code so the web app renders the widget rather than a wait message.
The instance profile gains one optional message, `challenge`, with `provider` and `site_key` strings, absent on self-host. When present, the web app renders the provider's widget on the signup and sign-in forms and attaches the token to those requests. The web app knows the Cloudflare Turnstile and hCaptcha widgets, which share one explicit-render API; adding a provider is a web change plus a private verifier. Rendering is unconditional when configured; adaptive challenges that appear only after failures are deferred. First-user setup renders no widget and the server asks for no token there. The open-source binary never imports a provider SDK.
### Signup policy seam
`ratelimit.SignupPolicy` is an interface with one method, `AllowSignup(ctx, canonicalEmail, clientIP) error`. The default allows everything. `CreateUser` calls it for non-admin callers after validation and before the store write. A returned error surfaces as `PERMISSION_DENIED` with the policy's message. This is where a deployment attaches domain blocklists, disposable-address checks, or geographic rules. Like the challenge, a deployment supplies it by setting the field on the API service; the functional-options constructor planned for the cloud module wraps those fields when it arrives.
### Configuration
Two profile options, set like the existing ones:
- `rate-limit`: boolean, default on. Off disables every scope. There are no per-scope knobs in the open-source binary; tuning goes through the policy seam.
- `trusted-proxies`: described above, default `private`.
Enabling the limiter by default is a behavior change for self-hosters, and the cloud plan's rule was that no precondition changes behavior when unconfigured. This is the one deliberate exception. Brute-force protection is a recurring self-host request, the defaults are sized to be invisible in ordinary use, and shipping it off by default would leave most instances with the exposure the feature exists to close. The release notes call out the switch.
### Observability
Every refusal logs one info line with the scope and the key. The client address is logged as-is; the account key is logged for `signin_account` because the operator needs to see which account is under attack. No metrics system exists; nothing beyond logs is added.
### Security invariants
- The client address used for any limit or session record comes from the resolver and never directly from a request header.
- Every request that passes authorization is counted against a catch-all budget; there is no procedure the table does not bound.
- Batch procedures cost their item count, never one.
- Rate-limit checks run before password hashing, before any outbound fetch, and before any store write on the paths they guard.
- A limit keyed on an account name behaves the same for accounts that exist and accounts that do not.
- Rate-limit refusals carry no free-text information about which limit tripped or whether an account exists.
- Challenge tokens are verified server-side on every use; the open-source code never treats the presence of a token as success.
- The limiter fails open under its own failure; it never turns an infrastructure problem into an outage.
- Turning the limiter off restores today's behavior exactly, and an unset challenge or signup policy adds no request, header, or field.
### Testing
- Limiter unit tests with a fake clock: window boundaries, `Allowed` without `Hit`, costs, eviction order, fail-open at capacity with the once-per-minute warning.
- Resolver table tests: untrusted peer with a forged `X-Forwarded-For`, trusted peer with a chain of trusted and untrusted entries, `none`, explicit CIDRs, IPv6, and a missing header.
- Authorizer tests: an anonymous burst trips `anonymous` on both transports; an authenticated burst trips `authenticated`; a batch of one hundred usernames costs one hundred; MCP calls with a bearer token are counted against the user.
- Handler tests: after the limit, `SignIn` returns 429 without calling the store or bcrypt; a successful sign-in does not count; `signin_account` trips identically for a real and a made-up username; admin `CreateUser` feeds only the catch-all; `validate_only` and real signup use separate scopes; link metadata costs per URL and refuses before fetching.
- Transport tests: both Connect and the gateway return HTTP 429 with `Retry-After`, `RateLimit`, `RateLimit-Policy`, an `ErrorInfo` with reason `RATE_LIMITED`, and a `RetryInfo`; both forward `Challenge-Token`; both strip a client-supplied `x-memos-client-ip`.
- A test challenge and a test signup policy wired through the server options, asserting the `FAILED_PRECONDITION` and `PERMISSION_DENIED` paths.
## Alternatives considered
| Alternative | Decision |
| --- | --- |
| Trust `X-Forwarded-For` as today and rate-limit on it | Rejected; a forged header gives every request a fresh bucket, and the limiter would protect nothing. |
| Trust no proxy by default | Rejected; most self-host installs sit behind a reverse proxy on the same host, and every user would share one bucket. `private` covers that layout without trusting the public internet. |
| Enforce every limit in handlers | Rejected; the catch-all budgets need only the procedure and identity, which the shared authorizer already has on both transports, and enforcing them there is the only way to guarantee no procedure is left unbounded. The per-flow limits stay in handlers because they need request fields. |
| Enforce every limit in an Echo middleware by path | Rejected; the sign-in check needs the submitted username and the signup check needs the admin decision, both known only inside the handler. |
| Distinguish throttle from quota by detail type alone | Rejected; AIP-193 makes `ErrorInfo.reason` the discriminator and mandates the detail. Detail types remain as supplements. |
| Token bucket from `golang.org/x/time/rate` | Rejected; it cannot count only failures without a refund step. A window counter is smaller and expresses the rule directly. |
| Lock an account after N failures | Rejected; a lockout lets an attacker deny service to any account by name. A short window per submitted name slows guessing without a lockout state to manage. |
| Require authentication for link metadata instead of limiting it | Rejected; anonymous readers of a public instance need previews for public memos. A per-address limit keeps the endpoint usable while closing the proxy abuse. |
| Put the challenge token in the request protos | Rejected; the challenge is a transport concern of two forms. A header keeps the proto free of a field that is meaningless on self-host and to MCP clients. |
| Ship the limiter off by default | Rejected; see Configuration. |
| Per-scope tuning through instance settings and an admin UI | Rejected for the initial version; operators can turn it off, and deployments tune through the policy seam. Knobs can be added when a self-host need appears. |
| Persist limiter state in the database | Rejected; write amplification on every request for state that is fine to lose on restart. |
## Deferred design
Adaptive challenges shown only after failures, per-scope tuning in the admin UI, a shared limiter for more than one node, bans and IP reputation, disposable-domain lists in the open-source binary, content-quality spam detection, enumeration-safe signup, metrics export, byte-based quotas, and limits on file and avatar serving remain deferred. Later work must preserve the single client-address resolver, the two-layer enforcement of catch-all budgets in the authorizer and per-flow limits in handlers, check-before-work ordering, the AIP-193 error shape, and identical behavior for existing and non-existing account names.
+1 -1
View File
@@ -42,6 +42,7 @@ require (
google.golang.org/genai v1.68.0
google.golang.org/genproto v0.0.0-20260810153831-ec0a7760b754
google.golang.org/genproto/googleapis/api v0.0.0-20260810153831-ec0a7760b754
google.golang.org/genproto/googleapis/rpc v0.0.0-20260810153831-ec0a7760b754
google.golang.org/grpc v1.83.0
google.golang.org/protobuf v1.36.12
gopkg.in/yaml.v3 v3.0.1
@@ -147,7 +148,6 @@ require (
golang.org/x/time v0.15.0 // indirect
golang.org/x/tools v0.49.0 // indirect
google.golang.org/api v0.293.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260810153831-ec0a7760b754 // indirect
modernc.org/libc v1.75.3 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.12.0 // indirect
+183
View File
@@ -0,0 +1,183 @@
// Package clientip resolves the client address of a request from the
// connection peer and, only when that peer is a trusted proxy, from the
// forwarding headers it set. Every rate limit and session record keys on the
// value this package produces, so a client can never choose its own address
// by sending a header.
package clientip
import (
"context"
"net"
"net/http"
"net/netip"
"strings"
"github.com/labstack/echo/v5"
"github.com/pkg/errors"
)
// KeywordPrivate expands to the loopback, RFC 1918, link-local, and
// unique-local ranges, which covers a reverse proxy on the same host or on a
// container network. It is the default because that is how most instances
// are deployed, and a default of "none" would put every user behind such a
// proxy into one shared bucket. The trade is that any peer on the private
// network is believed when it forwards an address; an operator whose
// instance is reached directly through a NAT hop that presents a private
// peer address without rewriting headers should set "none" or list the
// real proxy explicitly.
const KeywordPrivate = "private"
// KeywordNone trusts no proxy at all: the peer address is always the client.
const KeywordNone = "none"
var privatePrefixes = []netip.Prefix{
netip.MustParsePrefix("127.0.0.0/8"),
netip.MustParsePrefix("::1/128"),
netip.MustParsePrefix("10.0.0.0/8"),
netip.MustParsePrefix("172.16.0.0/12"),
netip.MustParsePrefix("192.168.0.0/16"),
netip.MustParsePrefix("169.254.0.0/16"),
netip.MustParsePrefix("fc00::/7"),
netip.MustParsePrefix("fe80::/10"),
}
// Resolver decides which address identifies the client of a request.
type Resolver struct {
trusted []netip.Prefix
}
// ParseTrustedProxies turns the configured list into a resolver. Each entry
// is a CIDR, a single address, or one of the keywords. An empty list means
// "private". Entries may themselves be comma-separated.
func ParseTrustedProxies(values []string) (*Resolver, error) {
entries := []string{}
for _, value := range values {
for entry := range strings.SplitSeq(value, ",") {
if entry = strings.TrimSpace(entry); entry != "" {
entries = append(entries, entry)
}
}
}
if len(entries) == 0 {
entries = []string{KeywordPrivate}
}
resolver := &Resolver{}
for _, entry := range entries {
switch strings.ToLower(entry) {
case KeywordPrivate:
resolver.trusted = append(resolver.trusted, privatePrefixes...)
case KeywordNone:
// Trust nothing; other entries still apply if listed.
default:
if prefix, err := netip.ParsePrefix(entry); err == nil {
resolver.trusted = append(resolver.trusted, prefix)
continue
}
addr, err := netip.ParseAddr(entry)
if err != nil {
return nil, errors.Errorf("invalid trusted proxy %q: expected a CIDR, an IP address, %q, or %q", entry, KeywordPrivate, KeywordNone)
}
resolver.trusted = append(resolver.trusted, netip.PrefixFrom(addr, addr.BitLen()))
}
}
return resolver, nil
}
// Trusts reports whether addr is a configured proxy.
func (r *Resolver) Trusts(addr netip.Addr) bool {
addr = addr.Unmap()
for _, prefix := range r.trusted {
if prefix.Contains(addr) {
return true
}
}
return false
}
// Resolve returns the client address for a request with the given peer
// address (host:port or bare host) and headers. It always returns a usable
// string: an unparseable peer is returned as-is so that the limiter still
// has a key.
func (r *Resolver) Resolve(remoteAddr string, header http.Header) string {
peer, ok := parseAddr(remoteAddr)
if !ok {
return strings.TrimSpace(remoteAddr)
}
if !r.Trusts(peer) {
return peer.String()
}
// Walk X-Forwarded-For from the right: the rightmost entry was appended by
// the nearest proxy. Skip entries that are themselves trusted proxies; the
// first untrusted one is the client.
entries := []string{}
for _, value := range header.Values("X-Forwarded-For") {
for entry := range strings.SplitSeq(value, ",") {
if entry = strings.TrimSpace(entry); entry != "" {
entries = append(entries, entry)
}
}
}
for i := len(entries) - 1; i >= 0; i-- {
candidate, ok := parseAddr(entries[i])
if !ok {
// A malformed entry ends the walk; nothing to its left can be trusted.
break
}
if r.Trusts(candidate) {
continue
}
return candidate.String()
}
if realIP, ok := parseAddr(header.Get("X-Real-Ip")); ok && !r.Trusts(realIP) {
return realIP.String()
}
return peer.String()
}
func parseAddr(value string) (netip.Addr, bool) {
value = strings.TrimSpace(value)
if value == "" {
return netip.Addr{}, false
}
if host, _, err := net.SplitHostPort(value); err == nil {
value = host
}
value = strings.Trim(value, "[]")
addr, err := netip.ParseAddr(value)
if err != nil {
return netip.Addr{}, false
}
return addr.Unmap(), true
}
type contextKey struct{}
// WithClientIP stores the resolved client address in ctx.
func WithClientIP(ctx context.Context, ip string) context.Context {
return context.WithValue(ctx, contextKey{}, ip)
}
// FromContext returns the client address stored by the middleware, or "" when
// the request did not pass through it.
func FromContext(ctx context.Context) string {
if value, ok := ctx.Value(contextKey{}).(string); ok {
return value
}
return ""
}
// Middleware resolves the client address once per request and stores it in
// the request context, where both API transports and the file server read it.
func Middleware(resolver *Resolver) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c *echo.Context) error {
request := c.Request()
ip := resolver.Resolve(request.RemoteAddr, request.Header)
c.SetRequest(request.WithContext(WithClientIP(request.Context(), ip)))
return next(c)
}
}
}
+59
View File
@@ -0,0 +1,59 @@
package clientip
import (
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func headers(pairs ...string) http.Header {
header := http.Header{}
for i := 0; i+1 < len(pairs); i += 2 {
header.Add(pairs[i], pairs[i+1])
}
return header
}
func TestResolve(t *testing.T) {
tests := []struct {
name string
trusted []string
remote string
header http.Header
want string
}{
{name: "untrusted peer ignores forwarded header", trusted: []string{"none"}, remote: "203.0.113.5:4321", header: headers("X-Forwarded-For", "198.51.100.1"), want: "203.0.113.5"},
{name: "untrusted peer ignores real ip", trusted: []string{"none"}, remote: "203.0.113.5:4321", header: headers("X-Real-Ip", "198.51.100.1"), want: "203.0.113.5"},
{name: "default trusts a private peer's forwarded header", trusted: nil, remote: "172.18.0.2:80", header: headers("X-Forwarded-For", "198.51.100.1"), want: "198.51.100.1"},
{name: "explicit private keyword", trusted: []string{"private"}, remote: "192.168.1.50:80", header: headers("X-Forwarded-For", "198.51.100.1"), want: "198.51.100.1"},
{name: "none ignores a private peer's forwarded header", trusted: []string{"none"}, remote: "192.168.1.50:80", header: headers("X-Forwarded-For", "198.51.100.1"), want: "192.168.1.50"},
{name: "none ignores a loopback peer's forwarded header", trusted: []string{"none"}, remote: "127.0.0.1:80", header: headers("X-Forwarded-For", "198.51.100.1", "X-Real-Ip", "198.51.100.1"), want: "127.0.0.1"},
{name: "rightmost untrusted entry wins", trusted: nil, remote: "127.0.0.1:80", header: headers("X-Forwarded-For", "10.0.0.9, 198.51.100.1, 192.168.1.1"), want: "198.51.100.1"},
{name: "client-supplied left entries are ignored", trusted: nil, remote: "127.0.0.1:80", header: headers("X-Forwarded-For", "1.2.3.4, 198.51.100.1"), want: "198.51.100.1"},
{name: "multiple header values are joined", trusted: nil, remote: "127.0.0.1:80", header: headers("X-Forwarded-For", "1.2.3.4", "X-Forwarded-For", "198.51.100.1"), want: "198.51.100.1"},
{name: "all entries trusted falls back to real ip", trusted: nil, remote: "127.0.0.1:80", header: headers("X-Forwarded-For", "10.0.0.1", "X-Real-Ip", "198.51.100.7"), want: "198.51.100.7"},
{name: "all entries trusted and no real ip falls back to peer", trusted: nil, remote: "127.0.0.1:80", header: headers("X-Forwarded-For", "10.0.0.1"), want: "127.0.0.1"},
{name: "malformed entry stops the walk", trusted: nil, remote: "127.0.0.1:80", header: headers("X-Forwarded-For", "198.51.100.1, not-an-ip"), want: "127.0.0.1"},
{name: "explicit cidr", trusted: []string{"203.0.113.0/24"}, remote: "203.0.113.9:1", header: headers("X-Forwarded-For", "198.51.100.1"), want: "198.51.100.1"},
{name: "explicit single address", trusted: []string{"203.0.113.9"}, remote: "203.0.113.9:1", header: headers("X-Forwarded-For", "198.51.100.1"), want: "198.51.100.1"},
{name: "explicit cidr does not include private", trusted: []string{"203.0.113.0/24"}, remote: "127.0.0.1:1", header: headers("X-Forwarded-For", "198.51.100.1"), want: "127.0.0.1"},
{name: "ipv6 peer and client", trusted: nil, remote: "[::1]:8080", header: headers("X-Forwarded-For", "2001:db8::1"), want: "2001:db8::1"},
{name: "ipv4-mapped ipv6 is unmapped", trusted: []string{"none"}, remote: "[::ffff:203.0.113.5]:1", header: nil, want: "203.0.113.5"},
{name: "bare host without port", trusted: []string{"none"}, remote: "203.0.113.5", header: nil, want: "203.0.113.5"},
{name: "unparseable peer is returned as is", trusted: nil, remote: "unix", header: headers("X-Forwarded-For", "198.51.100.1"), want: "unix"},
{name: "comma separated config", trusted: []string{"203.0.113.0/24, none"}, remote: "203.0.113.9:1", header: headers("X-Forwarded-For", "198.51.100.1"), want: "198.51.100.1"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
resolver, err := ParseTrustedProxies(test.trusted)
require.NoError(t, err)
require.Equal(t, test.want, resolver.Resolve(test.remote, test.header))
})
}
}
func TestParseTrustedProxiesRejectsGarbage(t *testing.T) {
_, err := ParseTrustedProxies([]string{"proxy.internal"})
require.Error(t, err)
}
+5
View File
@@ -35,6 +35,11 @@ type Profile struct {
Commit string
// InstanceURL is the canonical external URL of the Memos instance.
InstanceURL string
// RateLimit enables the request rate limiter. Off restores unbounded behavior.
RateLimit bool
// TrustedProxies lists the proxies whose forwarding headers identify the
// client: CIDRs, addresses, or the keywords "private" and "none".
TrustedProxies []string
}
func checkDataDir(dataDir string) (string, error) {
+391
View File
@@ -0,0 +1,391 @@
// Package ratelimit bounds how often a caller may do something. It provides a
// declarative policy table, an in-memory sliding-window limiter, and the
// seams a deployment fills to add a challenge or a signup policy.
package ratelimit
import (
"context"
"log/slog"
"math"
"sync"
"time"
)
// Scope names one counted activity. Scopes are the rows of the policy table.
type Scope string
// Scopes shipped by the open-source binary. See docs/design/api-abuse-controls.md.
const (
// ScopeAnonymous is the catch-all budget for requests without an identity, per client address.
ScopeAnonymous Scope = "anonymous"
// ScopeAuthenticated is the catch-all budget for requests with an identity, per user.
ScopeAuthenticated Scope = "authenticated"
// ScopeSignInIP counts failed sign-ins per client address.
ScopeSignInIP Scope = "signin_ip"
// ScopeSignInAccount counts failed password sign-ins per submitted username.
ScopeSignInAccount Scope = "signin_account"
// ScopeSignupIP counts self-service registrations per client address.
ScopeSignupIP Scope = "signup_ip"
// ScopeValidateIP counts validate-only registration probes per client address.
ScopeValidateIP Scope = "validate_ip"
// ScopePasswordResetIP is reserved for the password reset flow, per client address.
ScopePasswordResetIP Scope = "password_reset_ip"
// ScopePasswordResetEmail is reserved for the password reset flow, per canonical email.
ScopePasswordResetEmail Scope = "password_reset_email"
// ScopeLinkMetadata counts link preview fetches per URL requested.
ScopeLinkMetadata Scope = "link_metadata"
// ScopeUploadUser counts upload starts per user.
ScopeUploadUser Scope = "upload_user"
// ScopeTranscribeUser counts transcription calls per user.
ScopeTranscribeUser Scope = "transcribe_user"
// ScopeWriteUser counts content creation per user.
ScopeWriteUser Scope = "write_user"
)
// Rule is the limit for one scope: at most Limit units within Window.
type Rule struct {
Limit int
Window time.Duration
}
// Policy maps scopes to rules. A scope missing from the policy is unlimited.
type Policy map[Scope]Rule
// Rule returns the rule for scope and whether one exists.
func (p Policy) Rule(scope Scope) (Rule, bool) {
rule, ok := p[scope]
return rule, ok
}
// DefaultPolicy is the table shipped by the open-source binary. The numbers
// are starting points sized so that a household or small office behind one
// address is never throttled in ordinary use.
func DefaultPolicy() Policy {
return Policy{
ScopeAnonymous: {Limit: 300, Window: time.Minute},
ScopeAuthenticated: {Limit: 600, Window: time.Minute},
ScopeSignInIP: {Limit: 30, Window: 15 * time.Minute},
ScopeSignInAccount: {Limit: 10, Window: 15 * time.Minute},
ScopeSignupIP: {Limit: 10, Window: time.Hour},
ScopeValidateIP: {Limit: 60, Window: time.Minute},
ScopePasswordResetIP: {Limit: 10, Window: time.Hour},
ScopePasswordResetEmail: {Limit: 3, Window: time.Hour},
ScopeLinkMetadata: {Limit: 60, Window: time.Minute},
ScopeUploadUser: {Limit: 120, Window: time.Minute},
ScopeTranscribeUser: {Limit: 20, Window: time.Hour},
ScopeWriteUser: {Limit: 120, Window: time.Minute},
}
}
// Decision is the answer to an Allowed call.
type Decision struct {
// Allowed is true when the request may proceed.
Allowed bool
// Rule is the rule that was consulted; zero when the scope is unlimited.
Rule Rule
// Remaining is how many units are left in the window before the call.
Remaining int
// RetryAfter is how long the caller should wait when refused.
RetryAfter time.Duration
}
// Limiter answers whether an activity may proceed and records that it did.
// Consume does both atomically and is what every-attempt-counts callers use.
// A caller that only counts some outcomes, such as a failed sign-in, still
// consumes up front so concurrent attempts cannot share one remaining unit,
// and calls Refund when the outcome turns out not to count. Allowed is a
// read and Hit a write for callers that need neither.
type Limiter interface {
Consume(scope Scope, key string, cost int) Decision
Refund(scope Scope, key string, cost int)
Allowed(scope Scope, key string, cost int) Decision
Hit(scope Scope, key string, cost int)
}
// Challenge verifies a proof-of-humanity token. Provider and SiteKey are
// exposed through the instance profile so the web app can render the
// matching widget. A nil Challenge means none is configured.
type Challenge interface {
Provider() string
SiteKey() string
Verify(ctx context.Context, scope Scope, token string, clientIP string) error
}
// SignupPolicy decides whether a self-service registration may proceed. A
// nil SignupPolicy allows every registration.
type SignupPolicy interface {
AllowSignup(ctx context.Context, canonicalEmail string, clientIP string) error
}
// MemoryLimiter is a sliding-window counter kept in process memory. Each key
// holds the count for the current fixed window and the previous one; the
// estimate weights the previous window by how much of it still overlaps the
// sliding window. Memory is constant per key.
type MemoryLimiter struct {
policy Policy
now func() time.Time
capacity int
mu sync.Mutex
entries map[entryKey]*entry
lastFailOpen time.Time
}
type entryKey struct {
scope Scope
key string
}
type entry struct {
windowStart time.Time
current int
previous int
}
// DefaultCapacity bounds how many keys the limiter tracks before it starts
// failing open.
const DefaultCapacity = 100_000
// MemoryOption tunes a MemoryLimiter.
type MemoryOption func(*MemoryLimiter)
// WithClock replaces the time source, for tests.
func WithClock(now func() time.Time) MemoryOption {
return func(l *MemoryLimiter) { l.now = now }
}
// WithCapacity replaces the key cap.
func WithCapacity(capacity int) MemoryOption {
return func(l *MemoryLimiter) { l.capacity = capacity }
}
// NewMemoryLimiter creates a limiter over the given policy.
func NewMemoryLimiter(policy Policy, options ...MemoryOption) *MemoryLimiter {
limiter := &MemoryLimiter{
policy: policy,
now: time.Now,
capacity: DefaultCapacity,
entries: map[entryKey]*entry{},
}
for _, option := range options {
option(limiter)
}
return limiter
}
// Allowed reports whether cost more units fit in the window for scope and key.
func (l *MemoryLimiter) Allowed(scope Scope, key string, cost int) Decision {
return l.decide(scope, key, cost, false)
}
// Consume admits and records cost units in one step, so concurrent callers
// cannot all be admitted on the strength of the same remaining budget.
func (l *MemoryLimiter) Consume(scope Scope, key string, cost int) Decision {
return l.decide(scope, key, cost, true)
}
// Refund gives back cost units consumed earlier in the same window. It never
// drives the count below zero, so a refund after the window rolled is harmless.
func (l *MemoryLimiter) Refund(scope Scope, key string, cost int) {
rule, ok := l.policy.Rule(scope)
if !ok {
return
}
if cost < 1 {
cost = 1
}
now := l.now()
l.mu.Lock()
defer l.mu.Unlock()
current, ok := l.lookup(entryKey{scope, key}, rule, now)
if !ok {
return
}
current.current = max(current.current-cost, 0)
}
// Hit records cost units against scope and key.
func (l *MemoryLimiter) Hit(scope Scope, key string, cost int) {
rule, ok := l.policy.Rule(scope)
if !ok {
return
}
if cost < 1 {
cost = 1
}
now := l.now()
l.mu.Lock()
defer l.mu.Unlock()
l.record(entryKey{scope, key}, rule, now, cost)
}
// decide evaluates the budget under one lock and, when charge is set,
// records the cost in the same critical section.
func (l *MemoryLimiter) decide(scope Scope, key string, cost int, charge bool) Decision {
rule, ok := l.policy.Rule(scope)
if !ok {
return Decision{Allowed: true}
}
if cost < 1 {
cost = 1
}
now := l.now()
l.mu.Lock()
defer l.mu.Unlock()
id := entryKey{scope, key}
var state entry
if current, ok := l.lookup(id, rule, now); ok {
state = *current
} else {
state = entry{windowStart: now.Truncate(rule.Window)}
}
used := state.estimate(rule, now)
remaining := max(rule.Limit-used, 0)
if used+cost <= rule.Limit {
if charge {
l.record(id, rule, now, cost)
}
return Decision{Allowed: true, Rule: rule, Remaining: remaining}
}
return Decision{Allowed: false, Rule: rule, Remaining: remaining, RetryAfter: state.retryAfter(rule, now, cost)}
}
// record adds cost to the entry for id, creating it when there is room. The
// caller holds the lock.
func (l *MemoryLimiter) record(id entryKey, rule Rule, now time.Time, cost int) {
current, ok := l.lookup(id, rule, now)
if !ok {
if !l.makeRoom(now) {
return
}
current = &entry{windowStart: now.Truncate(rule.Window)}
l.entries[id] = current
}
current.current += cost
}
// lookup returns the live entry for id, rolling its windows forward first.
// The caller holds the lock.
func (l *MemoryLimiter) lookup(id entryKey, rule Rule, now time.Time) (*entry, bool) {
current, ok := l.entries[id]
if !ok {
return nil, false
}
current.roll(rule, now)
return current, true
}
// evictionSample bounds the work done per call at capacity. Go map iteration
// starts at a random bucket, so inspecting a fixed number of entries samples
// the table; expired entries dominate a table that has been full for a
// while, so a small sample frees room in amortized constant time instead of
// scanning every entry under the lock.
const evictionSample = 32
// makeRoom evicts expired entries when the cap is reached, inspecting at most
// evictionSample entries. It returns false when the sample held nothing
// expired, in which case the caller fails open. The caller holds the lock.
func (l *MemoryLimiter) makeRoom(now time.Time) bool {
if len(l.entries) < l.capacity {
return true
}
inspected := 0
for id, current := range l.entries {
if inspected >= evictionSample {
break
}
inspected++
rule, ok := l.policy.Rule(id.scope)
if !ok || now.Sub(current.windowStart) >= 2*rule.Window {
delete(l.entries, id)
}
}
if len(l.entries) < l.capacity {
return true
}
if now.Sub(l.lastFailOpen) >= time.Minute {
l.lastFailOpen = now
slog.Warn("rate limiter is at capacity and failing open", slog.Int("capacity", l.capacity))
}
return false
}
// roll advances the entry so that windowStart is the fixed window containing
// now, carrying the previous window's count when it is adjacent and dropping
// it otherwise.
func (e *entry) roll(rule Rule, now time.Time) {
windowStart := now.Truncate(rule.Window)
switch {
case windowStart.Equal(e.windowStart):
case windowStart.Equal(e.windowStart.Add(rule.Window)):
e.previous = e.current
e.current = 0
e.windowStart = windowStart
default:
e.previous = 0
e.current = 0
e.windowStart = windowStart
}
}
// estimate is the sliding-window count: the current window plus the share of
// the previous window that still overlaps the last Window of time.
func (e *entry) estimate(rule Rule, now time.Time) int {
elapsed := now.Sub(e.windowStart)
overlap := 1 - float64(elapsed)/float64(rule.Window)
if overlap < 0 {
overlap = 0
}
return e.current + int(float64(e.previous)*overlap)
}
// retryAfter is the delay until a request of cost would be admitted, in whole
// seconds and never less than one. It inverts estimate with the same integer
// truncation: the previous window's share decays through the current window,
// and at the boundary the current count becomes the previous one and decays
// in turn. The result is the first whole second strictly after the instant
// the estimate drops far enough.
func (e *entry) retryAfter(rule Rule, now time.Time, cost int) time.Duration {
budget := rule.Limit - cost
if budget < 0 {
// A single request larger than the limit never fits; advertise one window.
return rule.Window
}
windowEnd := e.windowStart.Add(rule.Window)
var ready time.Time
switch {
case e.current <= budget && e.previous > 0:
// Admitted once floor(previous * overlap) <= budget - current, that is
// once overlap < (budget - current + 1) / previous.
ready = e.windowStart.Add(decayTime(rule.Window, e.previous, budget-e.current+1))
case e.current <= budget:
ready = now
default:
// Only the next window can admit it, once the current count, then the
// previous one, has decayed: overlap < (budget + 1) / current.
ready = windowEnd.Add(decayTime(rule.Window, e.current, budget+1))
}
wait := ready.Sub(now)
if wait < 0 {
wait = 0
}
// Floor plus one second lands strictly after the boundary even when the
// wait is a whole number of seconds, and is never less than one.
return time.Duration(math.Floor(wait.Seconds()))*time.Second + time.Second
}
// decayTime is how far into a window a count of total must decay before its
// weighted share, total * (1 - t/window), falls below allowance. It is
// computed in integer nanoseconds with ceiling division so the boundary is
// never placed a rounding error too early.
func decayTime(window time.Duration, total, allowance int) time.Duration {
if allowance >= total {
return 0
}
numerator := int64(window) * int64(total-allowance)
denominator := int64(total)
return time.Duration((numerator + denominator - 1) / denominator)
}
+232
View File
@@ -0,0 +1,232 @@
package ratelimit
import (
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type clock struct{ now time.Time }
func (c *clock) Now() time.Time { return c.now }
func (c *clock) Advance(d time.Duration) { c.now = c.now.Add(d) }
func newClock() *clock { return &clock{now: time.Date(2026, 9, 12, 10, 0, 0, 0, time.UTC)} }
func policy(limit int, window time.Duration) Policy {
return Policy{"test": {Limit: limit, Window: window}}
}
func TestAllowedDoesNotConsume(t *testing.T) {
c := newClock()
l := NewMemoryLimiter(policy(2, time.Minute), WithClock(c.Now))
for range 10 {
require.True(t, l.Allowed("test", "k", 1).Allowed)
}
require.Equal(t, 2, l.Allowed("test", "k", 1).Remaining)
}
func TestHitThenRefuse(t *testing.T) {
c := newClock()
l := NewMemoryLimiter(policy(3, time.Minute), WithClock(c.Now))
l.Hit("test", "k", 1)
l.Hit("test", "k", 1)
d := l.Allowed("test", "k", 1)
require.True(t, d.Allowed)
require.Equal(t, 1, d.Remaining)
l.Hit("test", "k", 1)
d = l.Allowed("test", "k", 1)
require.False(t, d.Allowed)
require.Equal(t, 0, d.Remaining)
// At the boundary the three hits still count in full; one second later
// their weighted share has dropped to two.
require.Equal(t, 61*time.Second, d.RetryAfter)
require.Equal(t, 3, d.Rule.Limit)
c.Advance(60 * time.Second)
require.False(t, l.Allowed("test", "k", 1).Allowed)
c.Advance(time.Second)
require.True(t, l.Allowed("test", "k", 1).Allowed)
// Another key is independent.
require.True(t, l.Allowed("test", "other", 1).Allowed)
// An unknown scope is unlimited.
require.True(t, l.Allowed("unknown", "k", 1).Allowed)
}
func TestCost(t *testing.T) {
c := newClock()
l := NewMemoryLimiter(policy(10, time.Minute), WithClock(c.Now))
require.True(t, l.Allowed("test", "k", 10).Allowed)
require.False(t, l.Allowed("test", "k", 11).Allowed)
l.Hit("test", "k", 7)
require.True(t, l.Allowed("test", "k", 3).Allowed)
require.False(t, l.Allowed("test", "k", 4).Allowed)
}
func TestSlidingWindow(t *testing.T) {
c := newClock()
l := NewMemoryLimiter(policy(10, time.Minute), WithClock(c.Now))
l.Hit("test", "k", 10)
require.False(t, l.Allowed("test", "k", 1).Allowed)
// Half a window later, half of the previous window still counts.
c.Advance(90 * time.Second)
d := l.Allowed("test", "k", 1)
require.True(t, d.Allowed)
require.Equal(t, 5, d.Remaining)
// Two windows later nothing counts.
c.Advance(2 * time.Minute)
require.Equal(t, 10, l.Allowed("test", "k", 1).Remaining)
}
func TestRetryAfterIsWhenTheRequestWouldSucceed(t *testing.T) {
c := newClock()
l := NewMemoryLimiter(policy(1, time.Minute), WithClock(c.Now))
c.Advance(20 * time.Second)
l.Hit("test", "k", 1)
// The hit becomes the previous window at 10:01:00, where it still counts in
// full; one second later its weighted share truncates to zero.
d := l.Allowed("test", "k", 1)
require.False(t, d.Allowed)
require.Equal(t, 41*time.Second, d.RetryAfter)
// One second short of the advertised delay is refused; the delay itself is enough.
c.Advance(d.RetryAfter - time.Second)
require.False(t, l.Allowed("test", "k", 1).Allowed)
c.Advance(time.Second)
require.True(t, l.Allowed("test", "k", 1).Allowed)
}
func TestRetryAfterWithinTheCurrentWindow(t *testing.T) {
c := newClock()
l := NewMemoryLimiter(policy(10, time.Minute), WithClock(c.Now))
l.Hit("test", "k", 10)
c.Advance(time.Minute) // the ten hits are now the previous window
l.Hit("test", "k", 2) // current 2, previous 10: estimate 12 at the boundary
// Admitted once floor(10 * (1 - t/60s)) <= 7, which first holds just after
// t = 12s, so the advertised delay is 13s.
d := l.Allowed("test", "k", 1)
require.False(t, d.Allowed)
require.Equal(t, 13*time.Second, d.RetryAfter)
c.Advance(12 * time.Second)
require.False(t, l.Allowed("test", "k", 1).Allowed)
c.Advance(time.Second)
require.True(t, l.Allowed("test", "k", 1).Allowed)
}
func TestRetryAfterNeverBelowOneSecond(t *testing.T) {
c := newClock()
l := NewMemoryLimiter(policy(60, time.Minute), WithClock(c.Now))
l.Hit("test", "k", 60)
// Exactly at the boundary the previous window still counts in full.
c.Advance(time.Minute)
d := l.Allowed("test", "k", 1)
require.False(t, d.Allowed)
require.Equal(t, time.Second, d.RetryAfter)
c.Advance(time.Second)
require.True(t, l.Allowed("test", "k", 1).Allowed)
}
func TestRetryAfterForOversizedCost(t *testing.T) {
c := newClock()
l := NewMemoryLimiter(policy(2, time.Minute), WithClock(c.Now))
d := l.Allowed("test", "k", 3)
require.False(t, d.Allowed)
require.Equal(t, time.Minute, d.RetryAfter)
}
func TestConsumeIsAtomicUnderConcurrency(t *testing.T) {
c := newClock()
l := NewMemoryLimiter(policy(1, time.Minute), WithClock(c.Now))
const racers = 64
var wg sync.WaitGroup
start := make(chan struct{})
var admitted atomic.Int32
for range racers {
wg.Go(func() {
<-start
if l.Consume("test", "k", 1).Allowed {
admitted.Add(1)
}
})
}
close(start)
wg.Wait()
require.Equal(t, int32(1), admitted.Load(), "exactly one concurrent consume may succeed")
// Consume records the cost it admits and refuses once spent.
l2 := NewMemoryLimiter(policy(2, time.Minute), WithClock(c.Now))
require.True(t, l2.Consume("test", "k", 1).Allowed)
require.Equal(t, 1, l2.Consume("test", "k", 1).Remaining)
require.False(t, l2.Consume("test", "k", 1).Allowed)
// A refused consume charges nothing.
require.Equal(t, 0, l2.Allowed("test", "k", 1).Remaining)
require.True(t, l2.Consume("unknown", "k", 1).Allowed)
}
func TestCapacityEvictsExpiredThenFailsOpen(t *testing.T) {
c := newClock()
l := NewMemoryLimiter(policy(1, time.Minute), WithClock(c.Now), WithCapacity(2))
l.Hit("test", "a", 1)
l.Hit("test", "b", 1)
require.False(t, l.Allowed("test", "a", 1).Allowed)
// At capacity with nothing expired: the new key is not tracked, so it stays allowed.
l.Hit("test", "c", 1)
require.True(t, l.Allowed("test", "c", 1).Allowed, "fails open at capacity")
// Once the old entries expire they are evicted and new keys are tracked again.
c.Advance(3 * time.Minute)
l.Hit("test", "c", 1)
require.False(t, l.Allowed("test", "c", 1).Allowed)
}
func TestRefundGivesBackWhatWasConsumed(t *testing.T) {
c := newClock()
l := NewMemoryLimiter(policy(2, time.Minute), WithClock(c.Now))
require.True(t, l.Consume("test", "k", 1).Allowed)
require.True(t, l.Consume("test", "k", 1).Allowed)
require.False(t, l.Consume("test", "k", 1).Allowed)
l.Refund("test", "k", 1)
require.Equal(t, 1, l.Allowed("test", "k", 1).Remaining)
// A refund never drives the count negative, and an unknown key is a no-op.
l.Refund("test", "k", 5)
require.Equal(t, 2, l.Allowed("test", "k", 1).Remaining)
l.Refund("test", "never-seen", 1)
require.Equal(t, 2, l.Allowed("test", "never-seen", 1).Remaining)
}
func TestCapacityEvictionIsBoundedPerCall(t *testing.T) {
c := newClock()
const capacity = 4 * evictionSample
l := NewMemoryLimiter(policy(1, time.Minute), WithClock(c.Now), WithCapacity(capacity))
for i := range capacity {
l.Hit("test", "k"+string(rune('a'+i%26))+string(rune('a'+i/26)), 1)
}
require.Len(t, l.entries, capacity)
c.Advance(3 * time.Minute) // everything has expired
// Each new key at capacity inspects only a sample, so one call frees at
// most evictionSample entries rather than sweeping the whole table.
l.Hit("test", "fresh", 1)
require.Len(t, l.entries, capacity-evictionSample+1)
require.False(t, l.Allowed("test", "fresh", 1).Allowed, "the fresh key was tracked")
}
func TestDefaultPolicyCoversEveryScope(t *testing.T) {
p := DefaultPolicy()
for _, scope := range []Scope{
ScopeAnonymous, ScopeAuthenticated, ScopeSignInIP, ScopeSignInAccount, ScopeSignupIP, ScopeValidateIP,
ScopePasswordResetIP, ScopePasswordResetEmail, ScopeLinkMetadata, ScopeUploadUser, ScopeTranscribeUser, ScopeWriteUser,
} {
rule, ok := p.Rule(scope)
require.True(t, ok, scope)
require.Positive(t, rule.Limit, scope)
require.Positive(t, rule.Window, scope)
}
}
+11
View File
@@ -91,6 +91,17 @@ message InstanceProfile {
// Access mode controls whether unauthenticated users may access instance content.
InstanceAccessMode access_mode = 10;
// A proof-of-humanity challenge the deployment requires on signup and
// password sign-in. Absent when none is configured, which is the case on
// every self-hosted instance.
message Challenge {
// The challenge provider, which selects the widget the web app renders.
string provider = 1;
// The provider's public site key for the widget.
string site_key = 2;
}
Challenge challenge = 11;
}
// Request for instance profile.
+176 -105
View File
@@ -330,7 +330,8 @@ type InstanceProfile struct {
// mistaken for a fresh install.
NeedsSetup bool `protobuf:"varint,9,opt,name=needs_setup,json=needsSetup,proto3" json:"needs_setup,omitempty"`
// Access mode controls whether unauthenticated users may access instance content.
AccessMode InstanceAccessMode `protobuf:"varint,10,opt,name=access_mode,json=accessMode,proto3,enum=memos.api.v1.InstanceAccessMode" json:"access_mode,omitempty"`
AccessMode InstanceAccessMode `protobuf:"varint,10,opt,name=access_mode,json=accessMode,proto3,enum=memos.api.v1.InstanceAccessMode" json:"access_mode,omitempty"`
Challenge *InstanceProfile_Challenge `protobuf:"bytes,11,opt,name=challenge,proto3" json:"challenge,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -414,6 +415,13 @@ func (x *InstanceProfile) GetAccessMode() InstanceAccessMode {
return InstanceAccessMode_INSTANCE_ACCESS_MODE_UNSPECIFIED
}
func (x *InstanceProfile) GetChallenge() *InstanceProfile_Challenge {
if x != nil {
return x.Challenge
}
return nil
}
// Request for instance profile.
type GetInstanceProfileRequest struct {
state protoimpl.MessageState `protogen:"open.v1"`
@@ -974,6 +982,63 @@ func (x *InstanceStats) GetGeneratedTime() *timestamppb.Timestamp {
return nil
}
// A proof-of-humanity challenge the deployment requires on signup and
// password sign-in. Absent when none is configured, which is the case on
// every self-hosted instance.
type InstanceProfile_Challenge struct {
state protoimpl.MessageState `protogen:"open.v1"`
// The challenge provider, which selects the widget the web app renders.
Provider string `protobuf:"bytes,1,opt,name=provider,proto3" json:"provider,omitempty"`
// The provider's public site key for the widget.
SiteKey string `protobuf:"bytes,2,opt,name=site_key,json=siteKey,proto3" json:"site_key,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *InstanceProfile_Challenge) Reset() {
*x = InstanceProfile_Challenge{}
mi := &file_api_v1_instance_service_proto_msgTypes[10]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *InstanceProfile_Challenge) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*InstanceProfile_Challenge) ProtoMessage() {}
func (x *InstanceProfile_Challenge) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[10]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use InstanceProfile_Challenge.ProtoReflect.Descriptor instead.
func (*InstanceProfile_Challenge) Descriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{0, 0}
}
func (x *InstanceProfile_Challenge) GetProvider() string {
if x != nil {
return x.Provider
}
return ""
}
func (x *InstanceProfile_Challenge) GetSiteKey() string {
if x != nil {
return x.SiteKey
}
return ""
}
// General instance settings configuration.
type InstanceSetting_GeneralSetting struct {
state protoimpl.MessageState `protogen:"open.v1"`
@@ -1001,7 +1066,7 @@ type InstanceSetting_GeneralSetting struct {
func (x *InstanceSetting_GeneralSetting) Reset() {
*x = InstanceSetting_GeneralSetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[10]
mi := &file_api_v1_instance_service_proto_msgTypes[11]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1013,7 +1078,7 @@ func (x *InstanceSetting_GeneralSetting) String() string {
func (*InstanceSetting_GeneralSetting) ProtoMessage() {}
func (x *InstanceSetting_GeneralSetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[10]
mi := &file_api_v1_instance_service_proto_msgTypes[11]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -1103,7 +1168,7 @@ type InstanceSetting_Storage struct {
func (x *InstanceSetting_Storage) Reset() {
*x = InstanceSetting_Storage{}
mi := &file_api_v1_instance_service_proto_msgTypes[11]
mi := &file_api_v1_instance_service_proto_msgTypes[12]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1115,7 +1180,7 @@ func (x *InstanceSetting_Storage) String() string {
func (*InstanceSetting_Storage) ProtoMessage() {}
func (x *InstanceSetting_Storage) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[11]
mi := &file_api_v1_instance_service_proto_msgTypes[12]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -1200,7 +1265,7 @@ type InstanceSetting_StorageSetting struct {
func (x *InstanceSetting_StorageSetting) Reset() {
*x = InstanceSetting_StorageSetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[12]
mi := &file_api_v1_instance_service_proto_msgTypes[13]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1212,7 +1277,7 @@ func (x *InstanceSetting_StorageSetting) String() string {
func (*InstanceSetting_StorageSetting) ProtoMessage() {}
func (x *InstanceSetting_StorageSetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[12]
mi := &file_api_v1_instance_service_proto_msgTypes[13]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -1285,7 +1350,7 @@ type InstanceSetting_MemoRelatedSetting struct {
func (x *InstanceSetting_MemoRelatedSetting) Reset() {
*x = InstanceSetting_MemoRelatedSetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[13]
mi := &file_api_v1_instance_service_proto_msgTypes[14]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1297,7 +1362,7 @@ func (x *InstanceSetting_MemoRelatedSetting) String() string {
func (*InstanceSetting_MemoRelatedSetting) ProtoMessage() {}
func (x *InstanceSetting_MemoRelatedSetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[13]
mi := &file_api_v1_instance_service_proto_msgTypes[14]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -1348,7 +1413,7 @@ type InstanceSetting_TagMetadata struct {
func (x *InstanceSetting_TagMetadata) Reset() {
*x = InstanceSetting_TagMetadata{}
mi := &file_api_v1_instance_service_proto_msgTypes[14]
mi := &file_api_v1_instance_service_proto_msgTypes[15]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1360,7 +1425,7 @@ func (x *InstanceSetting_TagMetadata) String() string {
func (*InstanceSetting_TagMetadata) ProtoMessage() {}
func (x *InstanceSetting_TagMetadata) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[14]
mi := &file_api_v1_instance_service_proto_msgTypes[15]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -1406,7 +1471,7 @@ type InstanceSetting_TagsSetting struct {
func (x *InstanceSetting_TagsSetting) Reset() {
*x = InstanceSetting_TagsSetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[15]
mi := &file_api_v1_instance_service_proto_msgTypes[16]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1418,7 +1483,7 @@ func (x *InstanceSetting_TagsSetting) String() string {
func (*InstanceSetting_TagsSetting) ProtoMessage() {}
func (x *InstanceSetting_TagsSetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[15]
mi := &file_api_v1_instance_service_proto_msgTypes[16]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -1451,7 +1516,7 @@ type InstanceSetting_NotificationSetting struct {
func (x *InstanceSetting_NotificationSetting) Reset() {
*x = InstanceSetting_NotificationSetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[16]
mi := &file_api_v1_instance_service_proto_msgTypes[17]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1463,7 +1528,7 @@ func (x *InstanceSetting_NotificationSetting) String() string {
func (*InstanceSetting_NotificationSetting) ProtoMessage() {}
func (x *InstanceSetting_NotificationSetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[16]
mi := &file_api_v1_instance_service_proto_msgTypes[17]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -1500,7 +1565,7 @@ type InstanceSetting_AISetting struct {
func (x *InstanceSetting_AISetting) Reset() {
*x = InstanceSetting_AISetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[17]
mi := &file_api_v1_instance_service_proto_msgTypes[18]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1512,7 +1577,7 @@ func (x *InstanceSetting_AISetting) String() string {
func (*InstanceSetting_AISetting) ProtoMessage() {}
func (x *InstanceSetting_AISetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[17]
mi := &file_api_v1_instance_service_proto_msgTypes[18]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -1561,7 +1626,7 @@ type InstanceSetting_AIProviderConfig struct {
func (x *InstanceSetting_AIProviderConfig) Reset() {
*x = InstanceSetting_AIProviderConfig{}
mi := &file_api_v1_instance_service_proto_msgTypes[18]
mi := &file_api_v1_instance_service_proto_msgTypes[19]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1573,7 +1638,7 @@ func (x *InstanceSetting_AIProviderConfig) String() string {
func (*InstanceSetting_AIProviderConfig) ProtoMessage() {}
func (x *InstanceSetting_AIProviderConfig) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[18]
mi := &file_api_v1_instance_service_proto_msgTypes[19]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -1659,7 +1724,7 @@ type InstanceSetting_TranscriptionConfig struct {
func (x *InstanceSetting_TranscriptionConfig) Reset() {
*x = InstanceSetting_TranscriptionConfig{}
mi := &file_api_v1_instance_service_proto_msgTypes[19]
mi := &file_api_v1_instance_service_proto_msgTypes[20]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1671,7 +1736,7 @@ func (x *InstanceSetting_TranscriptionConfig) String() string {
func (*InstanceSetting_TranscriptionConfig) ProtoMessage() {}
func (x *InstanceSetting_TranscriptionConfig) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[19]
mi := &file_api_v1_instance_service_proto_msgTypes[20]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -1725,7 +1790,7 @@ type InstanceSetting_AccessSetting struct {
func (x *InstanceSetting_AccessSetting) Reset() {
*x = InstanceSetting_AccessSetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[20]
mi := &file_api_v1_instance_service_proto_msgTypes[21]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1737,7 +1802,7 @@ func (x *InstanceSetting_AccessSetting) String() string {
func (*InstanceSetting_AccessSetting) ProtoMessage() {}
func (x *InstanceSetting_AccessSetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[20]
mi := &file_api_v1_instance_service_proto_msgTypes[21]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -1772,7 +1837,7 @@ type InstanceSetting_GeneralSetting_CustomProfile struct {
func (x *InstanceSetting_GeneralSetting_CustomProfile) Reset() {
*x = InstanceSetting_GeneralSetting_CustomProfile{}
mi := &file_api_v1_instance_service_proto_msgTypes[21]
mi := &file_api_v1_instance_service_proto_msgTypes[22]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1784,7 +1849,7 @@ func (x *InstanceSetting_GeneralSetting_CustomProfile) String() string {
func (*InstanceSetting_GeneralSetting_CustomProfile) ProtoMessage() {}
func (x *InstanceSetting_GeneralSetting_CustomProfile) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[21]
mi := &file_api_v1_instance_service_proto_msgTypes[22]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -1841,7 +1906,7 @@ type InstanceSetting_Storage_S3Config struct {
func (x *InstanceSetting_Storage_S3Config) Reset() {
*x = InstanceSetting_Storage_S3Config{}
mi := &file_api_v1_instance_service_proto_msgTypes[22]
mi := &file_api_v1_instance_service_proto_msgTypes[23]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1853,7 +1918,7 @@ func (x *InstanceSetting_Storage_S3Config) String() string {
func (*InstanceSetting_Storage_S3Config) ProtoMessage() {}
func (x *InstanceSetting_Storage_S3Config) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[22]
mi := &file_api_v1_instance_service_proto_msgTypes[23]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -1938,7 +2003,7 @@ type InstanceSetting_StorageSetting_S3Config struct {
func (x *InstanceSetting_StorageSetting_S3Config) Reset() {
*x = InstanceSetting_StorageSetting_S3Config{}
mi := &file_api_v1_instance_service_proto_msgTypes[23]
mi := &file_api_v1_instance_service_proto_msgTypes[24]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -1950,7 +2015,7 @@ func (x *InstanceSetting_StorageSetting_S3Config) String() string {
func (*InstanceSetting_StorageSetting_S3Config) ProtoMessage() {}
func (x *InstanceSetting_StorageSetting_S3Config) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[23]
mi := &file_api_v1_instance_service_proto_msgTypes[24]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -2034,7 +2099,7 @@ type InstanceSetting_NotificationSetting_EmailSetting struct {
func (x *InstanceSetting_NotificationSetting_EmailSetting) Reset() {
*x = InstanceSetting_NotificationSetting_EmailSetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[25]
mi := &file_api_v1_instance_service_proto_msgTypes[26]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -2046,7 +2111,7 @@ func (x *InstanceSetting_NotificationSetting_EmailSetting) String() string {
func (*InstanceSetting_NotificationSetting_EmailSetting) ProtoMessage() {}
func (x *InstanceSetting_NotificationSetting_EmailSetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[25]
mi := &file_api_v1_instance_service_proto_msgTypes[26]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -2145,7 +2210,7 @@ type InstanceStats_DatabaseStats struct {
func (x *InstanceStats_DatabaseStats) Reset() {
*x = InstanceStats_DatabaseStats{}
mi := &file_api_v1_instance_service_proto_msgTypes[26]
mi := &file_api_v1_instance_service_proto_msgTypes[27]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -2157,7 +2222,7 @@ func (x *InstanceStats_DatabaseStats) String() string {
func (*InstanceStats_DatabaseStats) ProtoMessage() {}
func (x *InstanceStats_DatabaseStats) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[26]
mi := &file_api_v1_instance_service_proto_msgTypes[27]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -2191,7 +2256,7 @@ var File_api_v1_instance_service_proto protoreflect.FileDescriptor
const file_api_v1_instance_service_proto_rawDesc = "" +
"\n" +
"\x1dapi/v1/instance_service.proto\x12\fmemos.api.v1\x1a\x19api/v1/user_service.proto\x1a\x1cgoogle/api/annotations.proto\x1a\x17google/api/client.proto\x1a\x1fgoogle/api/field_behavior.proto\x1a\x19google/api/resource.proto\x1a\x1bgoogle/protobuf/empty.proto\x1a google/protobuf/field_mask.proto\x1a\x1fgoogle/protobuf/timestamp.proto\x1a\x17google/type/color.proto\"\x88\x02\n" +
"\x1dapi/v1/instance_service.proto\x12\fmemos.api.v1\x1a\x19api/v1/user_service.proto\x1a\x1cgoogle/api/annotations.proto\x1a\x17google/api/client.proto\x1a\x1fgoogle/api/field_behavior.proto\x1a\x19google/api/resource.proto\x1a\x1bgoogle/protobuf/empty.proto\x1a google/protobuf/field_mask.proto\x1a\x1fgoogle/protobuf/timestamp.proto\x1a\x17google/type/color.proto\"\x93\x03\n" +
"\x0fInstanceProfile\x12\x18\n" +
"\aversion\x18\x02 \x01(\tR\aversion\x12\x12\n" +
"\x04demo\x18\x03 \x01(\bR\x04demo\x12!\n" +
@@ -2202,7 +2267,11 @@ const file_api_v1_instance_service_proto_rawDesc = "" +
"needsSetup\x12A\n" +
"\vaccess_mode\x18\n" +
" \x01(\x0e2 .memos.api.v1.InstanceAccessModeR\n" +
"accessMode\"\x1b\n" +
"accessMode\x12E\n" +
"\tchallenge\x18\v \x01(\v2'.memos.api.v1.InstanceProfile.ChallengeR\tchallenge\x1aB\n" +
"\tChallenge\x12\x1a\n" +
"\bprovider\x18\x01 \x01(\tR\bprovider\x12\x19\n" +
"\bsite_key\x18\x02 \x01(\tR\asiteKey\"\x1b\n" +
"\x19GetInstanceProfileRequest\"\xd0\"\n" +
"\x0fInstanceSetting\x12\x17\n" +
"\x04name\x18\x01 \x01(\tB\x03\xe0A\bR\x04name\x12W\n" +
@@ -2383,7 +2452,7 @@ func file_api_v1_instance_service_proto_rawDescGZIP() []byte {
}
var file_api_v1_instance_service_proto_enumTypes = make([]protoimpl.EnumInfo, 5)
var file_api_v1_instance_service_proto_msgTypes = make([]protoimpl.MessageInfo, 27)
var file_api_v1_instance_service_proto_msgTypes = make([]protoimpl.MessageInfo, 28)
var file_api_v1_instance_service_proto_goTypes = []any{
(InstanceAccessMode)(0), // 0: memos.api.v1.InstanceAccessMode
(InstanceSetting_Key)(0), // 1: memos.api.v1.InstanceSetting.Key
@@ -2400,76 +2469,78 @@ var file_api_v1_instance_service_proto_goTypes = []any{
(*TestInstanceEmailSettingRequest)(nil), // 12: memos.api.v1.TestInstanceEmailSettingRequest
(*GetInstanceStatsRequest)(nil), // 13: memos.api.v1.GetInstanceStatsRequest
(*InstanceStats)(nil), // 14: memos.api.v1.InstanceStats
(*InstanceSetting_GeneralSetting)(nil), // 15: memos.api.v1.InstanceSetting.GeneralSetting
(*InstanceSetting_Storage)(nil), // 16: memos.api.v1.InstanceSetting.Storage
(*InstanceSetting_StorageSetting)(nil), // 17: memos.api.v1.InstanceSetting.StorageSetting
(*InstanceSetting_MemoRelatedSetting)(nil), // 18: memos.api.v1.InstanceSetting.MemoRelatedSetting
(*InstanceSetting_TagMetadata)(nil), // 19: memos.api.v1.InstanceSetting.TagMetadata
(*InstanceSetting_TagsSetting)(nil), // 20: memos.api.v1.InstanceSetting.TagsSetting
(*InstanceSetting_NotificationSetting)(nil), // 21: memos.api.v1.InstanceSetting.NotificationSetting
(*InstanceSetting_AISetting)(nil), // 22: memos.api.v1.InstanceSetting.AISetting
(*InstanceSetting_AIProviderConfig)(nil), // 23: memos.api.v1.InstanceSetting.AIProviderConfig
(*InstanceSetting_TranscriptionConfig)(nil), // 24: memos.api.v1.InstanceSetting.TranscriptionConfig
(*InstanceSetting_AccessSetting)(nil), // 25: memos.api.v1.InstanceSetting.AccessSetting
(*InstanceSetting_GeneralSetting_CustomProfile)(nil), // 26: memos.api.v1.InstanceSetting.GeneralSetting.CustomProfile
(*InstanceSetting_Storage_S3Config)(nil), // 27: memos.api.v1.InstanceSetting.Storage.S3Config
(*InstanceSetting_StorageSetting_S3Config)(nil), // 28: memos.api.v1.InstanceSetting.StorageSetting.S3Config
nil, // 29: memos.api.v1.InstanceSetting.TagsSetting.TagsEntry
(*InstanceSetting_NotificationSetting_EmailSetting)(nil), // 30: memos.api.v1.InstanceSetting.NotificationSetting.EmailSetting
(*InstanceStats_DatabaseStats)(nil), // 31: memos.api.v1.InstanceStats.DatabaseStats
(*User)(nil), // 32: memos.api.v1.User
(*fieldmaskpb.FieldMask)(nil), // 33: google.protobuf.FieldMask
(*timestamppb.Timestamp)(nil), // 34: google.protobuf.Timestamp
(*color.Color)(nil), // 35: google.type.Color
(*emptypb.Empty)(nil), // 36: google.protobuf.Empty
(*InstanceProfile_Challenge)(nil), // 15: memos.api.v1.InstanceProfile.Challenge
(*InstanceSetting_GeneralSetting)(nil), // 16: memos.api.v1.InstanceSetting.GeneralSetting
(*InstanceSetting_Storage)(nil), // 17: memos.api.v1.InstanceSetting.Storage
(*InstanceSetting_StorageSetting)(nil), // 18: memos.api.v1.InstanceSetting.StorageSetting
(*InstanceSetting_MemoRelatedSetting)(nil), // 19: memos.api.v1.InstanceSetting.MemoRelatedSetting
(*InstanceSetting_TagMetadata)(nil), // 20: memos.api.v1.InstanceSetting.TagMetadata
(*InstanceSetting_TagsSetting)(nil), // 21: memos.api.v1.InstanceSetting.TagsSetting
(*InstanceSetting_NotificationSetting)(nil), // 22: memos.api.v1.InstanceSetting.NotificationSetting
(*InstanceSetting_AISetting)(nil), // 23: memos.api.v1.InstanceSetting.AISetting
(*InstanceSetting_AIProviderConfig)(nil), // 24: memos.api.v1.InstanceSetting.AIProviderConfig
(*InstanceSetting_TranscriptionConfig)(nil), // 25: memos.api.v1.InstanceSetting.TranscriptionConfig
(*InstanceSetting_AccessSetting)(nil), // 26: memos.api.v1.InstanceSetting.AccessSetting
(*InstanceSetting_GeneralSetting_CustomProfile)(nil), // 27: memos.api.v1.InstanceSetting.GeneralSetting.CustomProfile
(*InstanceSetting_Storage_S3Config)(nil), // 28: memos.api.v1.InstanceSetting.Storage.S3Config
(*InstanceSetting_StorageSetting_S3Config)(nil), // 29: memos.api.v1.InstanceSetting.StorageSetting.S3Config
nil, // 30: memos.api.v1.InstanceSetting.TagsSetting.TagsEntry
(*InstanceSetting_NotificationSetting_EmailSetting)(nil), // 31: memos.api.v1.InstanceSetting.NotificationSetting.EmailSetting
(*InstanceStats_DatabaseStats)(nil), // 32: memos.api.v1.InstanceStats.DatabaseStats
(*User)(nil), // 33: memos.api.v1.User
(*fieldmaskpb.FieldMask)(nil), // 34: google.protobuf.FieldMask
(*timestamppb.Timestamp)(nil), // 35: google.protobuf.Timestamp
(*color.Color)(nil), // 36: google.type.Color
(*emptypb.Empty)(nil), // 37: google.protobuf.Empty
}
var file_api_v1_instance_service_proto_depIdxs = []int32{
32, // 0: memos.api.v1.InstanceProfile.admin:type_name -> memos.api.v1.User
33, // 0: memos.api.v1.InstanceProfile.admin:type_name -> memos.api.v1.User
0, // 1: memos.api.v1.InstanceProfile.access_mode:type_name -> memos.api.v1.InstanceAccessMode
15, // 2: memos.api.v1.InstanceSetting.general_setting:type_name -> memos.api.v1.InstanceSetting.GeneralSetting
17, // 3: memos.api.v1.InstanceSetting.storage_setting:type_name -> memos.api.v1.InstanceSetting.StorageSetting
18, // 4: memos.api.v1.InstanceSetting.memo_related_setting:type_name -> memos.api.v1.InstanceSetting.MemoRelatedSetting
20, // 5: memos.api.v1.InstanceSetting.tags_setting:type_name -> memos.api.v1.InstanceSetting.TagsSetting
21, // 6: memos.api.v1.InstanceSetting.notification_setting:type_name -> memos.api.v1.InstanceSetting.NotificationSetting
22, // 7: memos.api.v1.InstanceSetting.ai_setting:type_name -> memos.api.v1.InstanceSetting.AISetting
25, // 8: memos.api.v1.InstanceSetting.access_setting:type_name -> memos.api.v1.InstanceSetting.AccessSetting
7, // 9: memos.api.v1.BatchGetInstanceSettingsResponse.settings:type_name -> memos.api.v1.InstanceSetting
7, // 10: memos.api.v1.UpdateInstanceSettingRequest.setting:type_name -> memos.api.v1.InstanceSetting
33, // 11: memos.api.v1.UpdateInstanceSettingRequest.update_mask:type_name -> google.protobuf.FieldMask
30, // 12: memos.api.v1.TestInstanceEmailSettingRequest.email:type_name -> memos.api.v1.InstanceSetting.NotificationSetting.EmailSetting
31, // 13: memos.api.v1.InstanceStats.database:type_name -> memos.api.v1.InstanceStats.DatabaseStats
34, // 14: memos.api.v1.InstanceStats.generated_time:type_name -> google.protobuf.Timestamp
26, // 15: memos.api.v1.InstanceSetting.GeneralSetting.custom_profile:type_name -> memos.api.v1.InstanceSetting.GeneralSetting.CustomProfile
2, // 16: memos.api.v1.InstanceSetting.Storage.type:type_name -> memos.api.v1.InstanceSetting.StorageType
27, // 17: memos.api.v1.InstanceSetting.Storage.s3_config:type_name -> memos.api.v1.InstanceSetting.Storage.S3Config
4, // 18: memos.api.v1.InstanceSetting.StorageSetting.storage_type:type_name -> memos.api.v1.InstanceSetting.StorageSetting.StorageType
28, // 19: memos.api.v1.InstanceSetting.StorageSetting.s3_config:type_name -> memos.api.v1.InstanceSetting.StorageSetting.S3Config
16, // 20: memos.api.v1.InstanceSetting.StorageSetting.storages:type_name -> memos.api.v1.InstanceSetting.Storage
35, // 21: memos.api.v1.InstanceSetting.TagMetadata.background_color:type_name -> google.type.Color
29, // 22: memos.api.v1.InstanceSetting.TagsSetting.tags:type_name -> memos.api.v1.InstanceSetting.TagsSetting.TagsEntry
30, // 23: memos.api.v1.InstanceSetting.NotificationSetting.email:type_name -> memos.api.v1.InstanceSetting.NotificationSetting.EmailSetting
23, // 24: memos.api.v1.InstanceSetting.AISetting.providers:type_name -> memos.api.v1.InstanceSetting.AIProviderConfig
24, // 25: memos.api.v1.InstanceSetting.AISetting.transcription:type_name -> memos.api.v1.InstanceSetting.TranscriptionConfig
3, // 26: memos.api.v1.InstanceSetting.AIProviderConfig.type:type_name -> memos.api.v1.InstanceSetting.AIProviderType
0, // 27: memos.api.v1.InstanceSetting.AccessSetting.access_mode:type_name -> memos.api.v1.InstanceAccessMode
19, // 28: memos.api.v1.InstanceSetting.TagsSetting.TagsEntry.value:type_name -> memos.api.v1.InstanceSetting.TagMetadata
6, // 29: memos.api.v1.InstanceService.GetInstanceProfile:input_type -> memos.api.v1.GetInstanceProfileRequest
8, // 30: memos.api.v1.InstanceService.GetInstanceSetting:input_type -> memos.api.v1.GetInstanceSettingRequest
9, // 31: memos.api.v1.InstanceService.BatchGetInstanceSettings:input_type -> memos.api.v1.BatchGetInstanceSettingsRequest
11, // 32: memos.api.v1.InstanceService.UpdateInstanceSetting:input_type -> memos.api.v1.UpdateInstanceSettingRequest
12, // 33: memos.api.v1.InstanceService.TestInstanceEmailSetting:input_type -> memos.api.v1.TestInstanceEmailSettingRequest
13, // 34: memos.api.v1.InstanceService.GetInstanceStats:input_type -> memos.api.v1.GetInstanceStatsRequest
5, // 35: memos.api.v1.InstanceService.GetInstanceProfile:output_type -> memos.api.v1.InstanceProfile
7, // 36: memos.api.v1.InstanceService.GetInstanceSetting:output_type -> memos.api.v1.InstanceSetting
10, // 37: memos.api.v1.InstanceService.BatchGetInstanceSettings:output_type -> memos.api.v1.BatchGetInstanceSettingsResponse
7, // 38: memos.api.v1.InstanceService.UpdateInstanceSetting:output_type -> memos.api.v1.InstanceSetting
36, // 39: memos.api.v1.InstanceService.TestInstanceEmailSetting:output_type -> google.protobuf.Empty
14, // 40: memos.api.v1.InstanceService.GetInstanceStats:output_type -> memos.api.v1.InstanceStats
35, // [35:41] is the sub-list for method output_type
29, // [29:35] is the sub-list for method input_type
29, // [29:29] is the sub-list for extension type_name
29, // [29:29] is the sub-list for extension extendee
0, // [0:29] is the sub-list for field type_name
15, // 2: memos.api.v1.InstanceProfile.challenge:type_name -> memos.api.v1.InstanceProfile.Challenge
16, // 3: memos.api.v1.InstanceSetting.general_setting:type_name -> memos.api.v1.InstanceSetting.GeneralSetting
18, // 4: memos.api.v1.InstanceSetting.storage_setting:type_name -> memos.api.v1.InstanceSetting.StorageSetting
19, // 5: memos.api.v1.InstanceSetting.memo_related_setting:type_name -> memos.api.v1.InstanceSetting.MemoRelatedSetting
21, // 6: memos.api.v1.InstanceSetting.tags_setting:type_name -> memos.api.v1.InstanceSetting.TagsSetting
22, // 7: memos.api.v1.InstanceSetting.notification_setting:type_name -> memos.api.v1.InstanceSetting.NotificationSetting
23, // 8: memos.api.v1.InstanceSetting.ai_setting:type_name -> memos.api.v1.InstanceSetting.AISetting
26, // 9: memos.api.v1.InstanceSetting.access_setting:type_name -> memos.api.v1.InstanceSetting.AccessSetting
7, // 10: memos.api.v1.BatchGetInstanceSettingsResponse.settings:type_name -> memos.api.v1.InstanceSetting
7, // 11: memos.api.v1.UpdateInstanceSettingRequest.setting:type_name -> memos.api.v1.InstanceSetting
34, // 12: memos.api.v1.UpdateInstanceSettingRequest.update_mask:type_name -> google.protobuf.FieldMask
31, // 13: memos.api.v1.TestInstanceEmailSettingRequest.email:type_name -> memos.api.v1.InstanceSetting.NotificationSetting.EmailSetting
32, // 14: memos.api.v1.InstanceStats.database:type_name -> memos.api.v1.InstanceStats.DatabaseStats
35, // 15: memos.api.v1.InstanceStats.generated_time:type_name -> google.protobuf.Timestamp
27, // 16: memos.api.v1.InstanceSetting.GeneralSetting.custom_profile:type_name -> memos.api.v1.InstanceSetting.GeneralSetting.CustomProfile
2, // 17: memos.api.v1.InstanceSetting.Storage.type:type_name -> memos.api.v1.InstanceSetting.StorageType
28, // 18: memos.api.v1.InstanceSetting.Storage.s3_config:type_name -> memos.api.v1.InstanceSetting.Storage.S3Config
4, // 19: memos.api.v1.InstanceSetting.StorageSetting.storage_type:type_name -> memos.api.v1.InstanceSetting.StorageSetting.StorageType
29, // 20: memos.api.v1.InstanceSetting.StorageSetting.s3_config:type_name -> memos.api.v1.InstanceSetting.StorageSetting.S3Config
17, // 21: memos.api.v1.InstanceSetting.StorageSetting.storages:type_name -> memos.api.v1.InstanceSetting.Storage
36, // 22: memos.api.v1.InstanceSetting.TagMetadata.background_color:type_name -> google.type.Color
30, // 23: memos.api.v1.InstanceSetting.TagsSetting.tags:type_name -> memos.api.v1.InstanceSetting.TagsSetting.TagsEntry
31, // 24: memos.api.v1.InstanceSetting.NotificationSetting.email:type_name -> memos.api.v1.InstanceSetting.NotificationSetting.EmailSetting
24, // 25: memos.api.v1.InstanceSetting.AISetting.providers:type_name -> memos.api.v1.InstanceSetting.AIProviderConfig
25, // 26: memos.api.v1.InstanceSetting.AISetting.transcription:type_name -> memos.api.v1.InstanceSetting.TranscriptionConfig
3, // 27: memos.api.v1.InstanceSetting.AIProviderConfig.type:type_name -> memos.api.v1.InstanceSetting.AIProviderType
0, // 28: memos.api.v1.InstanceSetting.AccessSetting.access_mode:type_name -> memos.api.v1.InstanceAccessMode
20, // 29: memos.api.v1.InstanceSetting.TagsSetting.TagsEntry.value:type_name -> memos.api.v1.InstanceSetting.TagMetadata
6, // 30: memos.api.v1.InstanceService.GetInstanceProfile:input_type -> memos.api.v1.GetInstanceProfileRequest
8, // 31: memos.api.v1.InstanceService.GetInstanceSetting:input_type -> memos.api.v1.GetInstanceSettingRequest
9, // 32: memos.api.v1.InstanceService.BatchGetInstanceSettings:input_type -> memos.api.v1.BatchGetInstanceSettingsRequest
11, // 33: memos.api.v1.InstanceService.UpdateInstanceSetting:input_type -> memos.api.v1.UpdateInstanceSettingRequest
12, // 34: memos.api.v1.InstanceService.TestInstanceEmailSetting:input_type -> memos.api.v1.TestInstanceEmailSettingRequest
13, // 35: memos.api.v1.InstanceService.GetInstanceStats:input_type -> memos.api.v1.GetInstanceStatsRequest
5, // 36: memos.api.v1.InstanceService.GetInstanceProfile:output_type -> memos.api.v1.InstanceProfile
7, // 37: memos.api.v1.InstanceService.GetInstanceSetting:output_type -> memos.api.v1.InstanceSetting
10, // 38: memos.api.v1.InstanceService.BatchGetInstanceSettings:output_type -> memos.api.v1.BatchGetInstanceSettingsResponse
7, // 39: memos.api.v1.InstanceService.UpdateInstanceSetting:output_type -> memos.api.v1.InstanceSetting
37, // 40: memos.api.v1.InstanceService.TestInstanceEmailSetting:output_type -> google.protobuf.Empty
14, // 41: memos.api.v1.InstanceService.GetInstanceStats:output_type -> memos.api.v1.InstanceStats
36, // [36:42] is the sub-list for method output_type
30, // [30:36] is the sub-list for method input_type
30, // [30:30] is the sub-list for extension type_name
30, // [30:30] is the sub-list for extension extendee
0, // [0:30] is the sub-list for field type_name
}
func init() { file_api_v1_instance_service_proto_init() }
@@ -2487,7 +2558,7 @@ func file_api_v1_instance_service_proto_init() {
(*InstanceSetting_AiSetting)(nil),
(*InstanceSetting_AccessSetting_)(nil),
}
file_api_v1_instance_service_proto_msgTypes[11].OneofWrappers = []any{
file_api_v1_instance_service_proto_msgTypes[12].OneofWrappers = []any{
(*InstanceSetting_Storage_S3Config_)(nil),
}
type x struct{}
@@ -2496,7 +2567,7 @@ func file_api_v1_instance_service_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: unsafe.Slice(unsafe.StringData(file_api_v1_instance_service_proto_rawDesc), len(file_api_v1_instance_service_proto_rawDesc)),
NumEnums: 5,
NumMessages: 27,
NumMessages: 28,
NumExtensions: 0,
NumServices: 1,
},
+15
View File
@@ -3394,7 +3394,22 @@ components:
type: string
description: Access mode controls whether unauthenticated users may access instance content.
format: enum
challenge:
$ref: '#/components/schemas/InstanceProfile_Challenge'
description: Instance profile message containing basic instance information.
InstanceProfile_Challenge:
type: object
properties:
provider:
type: string
description: The challenge provider, which selects the widget the web app renders.
siteKey:
type: string
description: The provider's public site key for the widget.
description: |-
A proof-of-humanity challenge the deployment requires on signup and
password sign-in. Absent when none is configured, which is the case on
every self-hosted instance.
InstanceSetting:
type: object
properties:
+4
View File
@@ -16,6 +16,7 @@ import (
audiollmgemini "github.com/usememos/memos/internal/ai/audiollm/gemini"
"github.com/usememos/memos/internal/ai/stt"
sttopenai "github.com/usememos/memos/internal/ai/stt/openai"
"github.com/usememos/memos/internal/ratelimit"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
storepb "github.com/usememos/memos/proto/gen/store"
)
@@ -53,6 +54,9 @@ func (s *APIV1Service) Transcribe(ctx context.Context, request *v1pb.TranscribeR
if user == nil {
return nil, status.Errorf(codes.Unauthenticated, "user not authenticated")
}
if err := s.throttleAndCharge(ratelimit.ScopeTranscribeUser, userKey(user.ID), 1); err != nil {
return nil, err
}
if request.Audio == nil {
return nil, status.Errorf(codes.InvalidArgument, "audio is required")
@@ -18,6 +18,7 @@ import (
"google.golang.org/protobuf/types/known/emptypb"
"github.com/usememos/memos/internal/motionphoto"
"github.com/usememos/memos/internal/ratelimit"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/store"
@@ -167,6 +168,9 @@ func (s *APIV1Service) CreateAttachment(ctx context.Context, request *v1pb.Creat
if err != nil {
return nil, err
}
if err := s.throttleAndCharge(ratelimit.ScopeUploadUser, userKey(create.CreatorID), 1); err != nil {
return nil, err
}
instanceStorageSetting, err := s.Store.GetInstanceStorageSetting(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get instance storage setting: %v", err)
@@ -11,6 +11,7 @@ import (
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"github.com/usememos/memos/internal/ratelimit"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
"github.com/usememos/memos/store"
)
@@ -30,6 +31,9 @@ func (s *APIV1Service) UploadAttachment(ctx context.Context, request *v1pb.Uploa
var upload *attachmentUpload
switch u := request.Upload.(type) {
case *v1pb.UploadAttachmentRequest_Spec:
if err := s.throttleAndCharge(ratelimit.ScopeUploadUser, userKey(user.ID), 1); err != nil {
return nil, err
}
id, upload, err = s.startAttachmentUpload(ctx, request, u.Spec, user.ID)
if err != nil {
return nil, err
+27
View File
@@ -8,6 +8,8 @@ import (
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/usememos/memos/internal/clientip"
"github.com/usememos/memos/internal/ratelimit"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
"github.com/usememos/memos/store"
)
@@ -51,12 +53,28 @@ func (s *APIV1Service) GetCurrentUser(ctx context.Context, _ *v1pb.GetCurrentUse
func (s *APIV1Service) SignIn(ctx context.Context, request *v1pb.SignInRequest) (*v1pb.SignInResponse, error) {
var existingUser *store.User
// Rate limits run before any lookup or hashing so a flood costs nothing.
// Each attempt reserves its units up front, so concurrent attempts cannot
// share one remaining unit; a successful attempt gives them back below.
// The account key is the submitted name whether or not it exists, so the
// limit cannot be used to learn which accounts are real.
clientIP := clientip.FromContext(ctx)
attempt, err := s.reserveSignIn(clientIP, request.GetPasswordCredentials().GetUsername())
if err != nil {
return nil, err
}
// Authentication Method 1: Password-based authentication
if passwordCredentials := request.GetPasswordCredentials(); passwordCredentials != nil {
if err := s.requireChallenge(ctx, ratelimit.ScopeSignInIP); err != nil {
attempt.succeeded()
return nil, err
}
user, err := s.Store.GetUser(ctx, &store.FindUser{
Username: &passwordCredentials.Username,
})
if err != nil {
attempt.succeeded()
return nil, status.Errorf(codes.Internal, "failed to get user, error: %v", err)
}
if user == nil {
@@ -66,6 +84,8 @@ func (s *APIV1Service) SignIn(ctx context.Context, request *v1pb.SignInRequest)
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(passwordCredentials.Password)); err != nil {
return nil, status.Errorf(codes.InvalidArgument, unmatchedUsernameAndPasswordError)
}
// The password matched: from here on the attempt is not a guess.
attempt.succeeded()
instanceGeneralSetting, err := s.Store.GetInstanceGeneralSetting(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get instance general setting, error: %v", err)
@@ -79,18 +99,25 @@ func (s *APIV1Service) SignIn(ctx context.Context, request *v1pb.SignInRequest)
// Authentication Method 2: SSO (OAuth2) authentication
identityProvider, userInfo, err := s.resolveSSOIdentity(ctx, ssoCredentials.IdpName, ssoCredentials.Code, ssoCredentials.RedirectUri, ssoCredentials.CodeVerifier)
if err != nil {
// A rejected exchange is a credential failure and stays counted.
return nil, err
}
user, err := s.resolveSSOUser(ctx, nil, identityProvider, userInfo)
if err != nil {
// The credential was valid; whatever refused provisioning is not a guess.
attempt.succeeded()
return nil, err
}
existingUser = user
}
if existingUser == nil {
// No credential was presented at all; nothing to guess against.
attempt.succeeded()
return nil, status.Errorf(codes.InvalidArgument, "invalid credentials")
}
// The credential has been proven from here on; the attempt does not count.
attempt.succeeded()
if existingUser.RowStatus == store.Archived {
return nil, status.Errorf(codes.PermissionDenied, "user has been archived with username %s", existingUser.Username)
}
@@ -6,6 +6,8 @@ import (
"testing"
"time"
"github.com/usememos/memos/internal/clientip"
"google.golang.org/grpc/metadata"
storepb "github.com/usememos/memos/proto/gen/store"
@@ -93,14 +95,15 @@ func TestParseUserAgent(t *testing.T) {
func TestExtractClientInfo(t *testing.T) {
service := &APIV1Service{}
// Test with metadata containing user agent and IP
// The user agent comes from metadata; the address comes only from the
// trusted-proxy resolver, so forwarding headers in metadata are ignored.
md := metadata.New(map[string]string{
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
"x-forwarded-for": "203.0.113.1, 198.51.100.1",
"x-real-ip": "203.0.113.1",
"x-forwarded-for": "198.51.100.1",
"x-real-ip": "198.51.100.1",
})
ctx := metadata.NewIncomingContext(context.Background(), md)
ctx := clientip.WithClientIP(metadata.NewIncomingContext(context.Background(), md), "203.0.113.1")
clientInfo := service.extractClientInfo(ctx)
+5 -8
View File
@@ -14,6 +14,7 @@ import (
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/usememos/memos/internal/clientip"
"github.com/usememos/memos/internal/util"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
storepb "github.com/usememos/memos/proto/gen/store"
@@ -271,7 +272,7 @@ func isActiveCurrentUser(user *store.User) bool {
//
// This function parses metadata from the gRPC context to extract:
// - User Agent: Raw user agent string for detailed parsing
// - IP Address: Client IP from X-Forwarded-For or X-Real-IP headers
// - IP Address: Client IP as resolved through the trusted proxies
// - Device Type: "mobile", "tablet", or "desktop" (parsed from user agent)
// - Operating System: OS name and version (e.g., "iOS 17.1", "Windows 10/11")
// - Browser: Browser name and version (e.g., "Chrome 120.0.0.0")
@@ -292,14 +293,10 @@ func (s *APIV1Service) extractClientInfo(ctx context.Context) *storepb.RefreshTo
// Parse user agent to extract device type, OS, browser info
s.parseUserAgent(userAgent, clientInfo)
}
if forwardedFor := md.Get("x-forwarded-for"); len(forwardedFor) > 0 {
ipAddress := strings.Split(forwardedFor[0], ",")[0] // Get the first IP in case of multiple
ipAddress = strings.TrimSpace(ipAddress)
clientInfo.IpAddress = ipAddress
} else if realIP := md.Get("x-real-ip"); len(realIP) > 0 {
clientInfo.IpAddress = realIP[0]
}
}
// The address comes from the trusted-proxy resolver, never from a header
// the client chose.
clientInfo.IpAddress = clientip.FromContext(ctx)
return clientInfo
}
+40
View File
@@ -2,9 +2,12 @@ package v1
import (
"context"
"log/slog"
"github.com/pkg/errors"
"github.com/usememos/memos/internal/clientip"
"github.com/usememos/memos/internal/ratelimit"
"github.com/usememos/memos/server/auth"
"github.com/usememos/memos/store"
)
@@ -26,6 +29,7 @@ var ErrUnauthenticated = errors.New("authentication required")
type Authorizer struct {
authenticator *auth.Authenticator
accessStore anonymousAccessStore
limiter ratelimit.Limiter
}
type anonymousAccessStore interface {
@@ -40,6 +44,42 @@ func NewAuthorizer(store *store.Store, secret string) *Authorizer {
}
}
// WithRateLimiter enables the catch-all request budgets. A nil limiter leaves
// every request unbounded.
func (a *Authorizer) WithRateLimiter(limiter ratelimit.Limiter) *Authorizer {
a.limiter = limiter
return a
}
// Throttle enforces the catch-all budget for a request that CheckAccess has
// already permitted: the authenticated budget keyed by user, or the anonymous
// budget keyed by client address. Every request costs one unit; batch
// handlers add their item count afterwards. It returns a RESOURCE_EXHAUSTED
// status when the budget is spent.
func (a *Authorizer) Throttle(ctx context.Context, _ string, result *auth.AuthResult) error {
if a.limiter == nil {
return nil
}
scope, key := ratelimit.ScopeAnonymous, clientip.FromContext(ctx)
switch {
case result == nil:
case result.Claims != nil:
scope, key = ratelimit.ScopeAuthenticated, userKey(result.Claims.UserID)
case result.User != nil:
scope, key = ratelimit.ScopeAuthenticated, userKey(result.User.ID)
default:
}
if key == "" {
return nil
}
decision := a.limiter.Consume(scope, key, 1)
if !decision.Allowed {
slog.Info("rate limit refused request", slog.String("scope", string(scope)), slog.String("key", key))
return newRateLimitError(scope, decision)
}
return nil
}
// Authenticate resolves the caller from the Authorization header, returning nil for
// an anonymous request. It never enforces policy — pair it with CheckAccess.
func (a *Authorizer) Authenticate(ctx context.Context, authHeader string) *auth.AuthResult {
+22 -5
View File
@@ -61,16 +61,33 @@ func wrap(path string, handler http.Handler) struct {
}{path, handler}
}
// convertGRPCError converts gRPC status errors to Connect errors.
// This preserves the error code semantics between the two protocols.
// convertGRPCError converts gRPC status errors to Connect errors, carrying
// the status details across and, for rate-limit refusals, the HTTP header
// fields a generic client reads.
func convertGRPCError(err error) error {
if err == nil {
return nil
}
if st, ok := status.FromError(err); ok {
return connect.NewError(grpcCodeToConnectCode(st.Code()), errors.New(st.Message()))
st, ok := status.FromError(err)
if !ok {
return connect.NewError(connect.CodeInternal, err)
}
return connect.NewError(connect.CodeInternal, err)
connectErr := connect.NewError(grpcCodeToConnectCode(st.Code()), errors.New(st.Message()))
for _, detail := range st.Proto().GetDetails() {
message, unmarshalErr := detail.UnmarshalNew()
if unmarshalErr != nil {
continue
}
if connectDetail, detailErr := connect.NewErrorDetail(message); detailErr == nil {
connectErr.AddDetail(connectDetail)
}
}
for key, values := range rateLimitHTTPHeaders(st) {
for _, value := range values {
connectErr.Meta().Add(key, value)
}
}
return connectErr
}
// grpcCodeToConnectCode converts gRPC status codes to Connect error codes.
@@ -55,6 +55,9 @@ func (*MetadataInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc
if cookie := header.Get("Cookie"); cookie != "" {
md.Set("cookie", cookie)
}
if token := header.Get(challengeTokenHeader); token != "" {
md.Set(challengeTokenMetadataKey, token)
}
// Set metadata in context so services can use metadata.FromIncomingContext()
ctx = metadata.NewIncomingContext(ctx, md)
@@ -221,6 +224,9 @@ func (in *AuthInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
if err := in.authorizer.CheckAccess(ctx, req.Spec().Procedure, result); err != nil {
return nil, newAuthorizationConnectError(err)
}
if err := in.authorizer.Throttle(ctx, req.Spec().Procedure, result); err != nil {
return nil, convertGRPCError(err)
}
ctx = auth.ApplyToContext(ctx, result)
+13
View File
@@ -70,6 +70,7 @@ func (s *APIV1Service) GetInstanceProfile(ctx context.Context, _ *v1pb.GetInstan
Commit: s.Profile.Commit,
NeedsSetup: len(users) == 0,
AccessMode: convertInstanceAccessModeFromStore(accessSetting.AccessMode),
Challenge: s.challengeProfile(),
}
return instanceProfile, nil
}
@@ -268,6 +269,18 @@ func (s *APIV1Service) UpdateInstanceSetting(ctx context.Context, request *v1pb.
return convertInstanceSettingFromStore(instanceSetting), nil
}
// challengeProfile describes the configured challenge so the web app can
// render the matching widget, or nil when none is configured.
func (s *APIV1Service) challengeProfile() *v1pb.InstanceProfile_Challenge {
if s.Challenge == nil {
return nil
}
return &v1pb.InstanceProfile_Challenge{
Provider: s.Challenge.Provider(),
SiteKey: s.Challenge.SiteKey(),
}
}
func (s *APIV1Service) TestInstanceEmailSetting(ctx context.Context, request *v1pb.TestInstanceEmailSettingRequest) (*emptypb.Empty, error) {
user, err := s.fetchCurrentUser(ctx)
if err != nil {
+4
View File
@@ -12,6 +12,7 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/usememos/memos/internal/ratelimit"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/server/runner/memopayload"
@@ -28,6 +29,9 @@ func (s *APIV1Service) CreateMemo(ctx context.Context, request *v1pb.CreateMemoR
if user == nil {
return nil, status.Errorf(codes.Unauthenticated, "user not authenticated")
}
if err := s.throttleAndCharge(ratelimit.ScopeWriteUser, userKey(user.ID), 1); err != nil {
return nil, err
}
if request.Memo == nil {
return nil, status.Errorf(codes.InvalidArgument, "memo is required")
}
@@ -9,6 +9,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/usememos/memos/internal/ratelimit"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/store"
@@ -36,6 +37,9 @@ func (s *APIV1Service) CreateMemoComment(ctx context.Context, request *v1pb.Crea
if user == nil {
return nil, status.Errorf(codes.Unauthenticated, "user not authenticated")
}
if err := s.throttleAndCharge(ratelimit.ScopeWriteUser, userKey(user.ID), 1); err != nil {
return nil, err
}
if err := s.checkMemoReadAccess(ctx, relatedMemo); err != nil {
return nil, err
}
@@ -8,6 +8,7 @@ import (
"google.golang.org/grpc/status"
"github.com/usememos/memos/internal/httpgetter"
"github.com/usememos/memos/internal/ratelimit"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
)
@@ -17,9 +18,19 @@ type linkMetadataFetcher interface {
// GetLinkMetadata gets metadata for a link.
func (s *APIV1Service) GetLinkMetadata(ctx context.Context, request *v1pb.GetLinkMetadataRequest) (*v1pb.LinkMetadata, error) {
if err := s.throttleLinkMetadata(ctx, 1); err != nil {
return nil, err
}
return s.buildLinkMetadata(ctx, request.GetUrl())
}
// throttleLinkMetadata bounds outbound fetches per caller. Cached results
// still cost, since the point is to bound how often a caller can ask.
func (s *APIV1Service) throttleLinkMetadata(ctx context.Context, urls int) error {
_, key := callerBudget(ctx)
return s.throttleAndCharge(ratelimit.ScopeLinkMetadata, key, urls)
}
// BatchGetLinkMetadata gets metadata for links.
func (s *APIV1Service) BatchGetLinkMetadata(ctx context.Context, request *v1pb.BatchGetLinkMetadataRequest) (*v1pb.BatchGetLinkMetadataResponse, error) {
if len(request.Urls) == 0 {
@@ -28,6 +39,10 @@ func (s *APIV1Service) BatchGetLinkMetadata(ctx context.Context, request *v1pb.B
if len(request.Urls) > maxBatchGetLinkMetadata {
return nil, status.Errorf(codes.InvalidArgument, "too many urls (max %d)", maxBatchGetLinkMetadata)
}
if err := s.throttleLinkMetadata(ctx, len(request.Urls)); err != nil {
return nil, err
}
s.chargeBatch(ctx, len(request.Urls))
linkMetadata := make([]*v1pb.LinkMetadata, 0, len(request.Urls))
for _, url := range request.Urls {
@@ -14,6 +14,7 @@ import (
"github.com/lithammer/shortuuid/v4"
"github.com/pkg/errors"
"github.com/usememos/memos/internal/ratelimit"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
"github.com/usememos/memos/server/access"
"github.com/usememos/memos/store"
@@ -29,6 +30,9 @@ func (s *APIV1Service) CreateMemoShare(ctx context.Context, request *v1pb.Create
if user == nil {
return nil, status.Errorf(codes.Unauthenticated, "user not authenticated")
}
if err := s.throttleAndCharge(ratelimit.ScopeWriteUser, userKey(user.ID), 1); err != nil {
return nil, err
}
memoUID, err := ExtractMemoUIDFromName(request.Parent)
if err != nil {
+226
View File
@@ -0,0 +1,226 @@
package v1
import (
"context"
"log/slog"
"math"
"net/http"
"strconv"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/durationpb"
"github.com/usememos/memos/internal/clientip"
"github.com/usememos/memos/internal/ratelimit"
"github.com/usememos/memos/server/auth"
)
const (
// errorInfoDomain is the google.rpc.ErrorInfo domain for every reason this
// service emits. AIP-193 asks for a stable, globally unique namespace, not a
// host: this is the service name the API already uses in every resource
// type ("memos.api.v1/Memo"), so reasons and resources share one name.
errorInfoDomain = "memos.api.v1"
// reasonRateLimited marks a refusal that recovers on its own after the
// advertised delay. Metering will use QUOTA_EXCEEDED for refusals that do not.
reasonRateLimited = "RATE_LIMITED"
// reasonChallengeRequired marks a request that must carry a valid challenge token.
reasonChallengeRequired = "CHALLENGE_REQUIRED"
rateLimitMessage = "too many requests, try again later"
// challengeTokenHeader carries the proof-of-humanity token on the wire; both
// transports forward it to metadata under challengeTokenMetadataKey.
challengeTokenHeader = "Challenge-Token"
challengeTokenMetadataKey = "challenge-token"
)
// newRateLimitError builds the refusal described in docs/design/api-abuse-controls.md:
// RESOURCE_EXHAUSTED with an ErrorInfo naming the reason and scope and a RetryInfo
// carrying the delay, so an AIP-194 client knows this recovers in seconds.
func newRateLimitError(scope ratelimit.Scope, decision ratelimit.Decision) error {
retryAfter := int(math.Ceil(decision.RetryAfter.Seconds()))
if retryAfter < 1 {
retryAfter = 1
}
st := status.New(codes.ResourceExhausted, rateLimitMessage)
detailed, err := st.WithDetails(
&errdetails.ErrorInfo{
Reason: reasonRateLimited,
Domain: errorInfoDomain,
Metadata: map[string]string{
"scope": string(scope),
"retry_after_seconds": strconv.Itoa(retryAfter),
"limit": strconv.Itoa(decision.Rule.Limit),
"window_seconds": strconv.Itoa(int(decision.Rule.Window.Seconds())),
"remaining": strconv.Itoa(decision.Remaining),
},
},
&errdetails.RetryInfo{RetryDelay: durationpb.New(decision.RetryAfter)},
)
if err != nil {
return st.Err()
}
return detailed.Err()
}
// newChallengeRequiredError builds the refusal for a missing or failed challenge
// token. It uses a different code from rate limiting so a client renders the
// widget instead of waiting.
func newChallengeRequiredError(message string) error {
st := status.New(codes.FailedPrecondition, message)
detailed, err := st.WithDetails(&errdetails.ErrorInfo{Reason: reasonChallengeRequired, Domain: errorInfoDomain})
if err != nil {
return st.Err()
}
return detailed.Err()
}
// rateLimitErrorInfo returns the ErrorInfo of a rate-limit refusal, or nil.
func rateLimitErrorInfo(st *status.Status) *errdetails.ErrorInfo {
if st == nil || st.Code() != codes.ResourceExhausted {
return nil
}
for _, detail := range st.Details() {
if info, ok := detail.(*errdetails.ErrorInfo); ok && info.GetReason() == reasonRateLimited {
return info
}
}
return nil
}
// rateLimitHTTPHeaders derives the HTTP header fields for a rate-limit refusal:
// Retry-After, and the RateLimit and RateLimit-Policy structured fields from
// draft-ietf-httpapi-ratelimit-headers. It returns nil for any other error.
func rateLimitHTTPHeaders(st *status.Status) http.Header {
info := rateLimitErrorInfo(st)
if info == nil {
return nil
}
meta := info.GetMetadata()
header := http.Header{}
header.Set("Retry-After", meta["retry_after_seconds"])
header.Set("RateLimit", `"`+meta["scope"]+`";r=`+meta["remaining"]+";t="+meta["retry_after_seconds"])
header.Set("RateLimit-Policy", `"`+meta["scope"]+`";q=`+meta["limit"]+";w="+meta["window_seconds"])
return header
}
// callerBudget names the catch-all scope and key for the caller of ctx: the
// user id when authenticated, the client address otherwise.
func callerBudget(ctx context.Context) (ratelimit.Scope, string) {
if userID := auth.GetUserID(ctx); userID != 0 {
return ratelimit.ScopeAuthenticated, userKey(userID)
}
return ratelimit.ScopeAnonymous, clientip.FromContext(ctx)
}
// userKey is the limiter key for a per-user scope.
func userKey(userID int32) string {
return strconv.Itoa(int(userID))
}
// charge records cost units against scope and key.
func (s *APIV1Service) charge(scope ratelimit.Scope, key string, cost int) {
if s.RateLimiter == nil || key == "" {
return
}
s.RateLimiter.Hit(scope, key, cost)
}
// throttleAndCharge admits and records in one atomic step, for activities
// where every attempt counts.
func (s *APIV1Service) throttleAndCharge(scope ratelimit.Scope, key string, cost int) error {
if s.RateLimiter == nil || key == "" {
return nil
}
decision := s.RateLimiter.Consume(scope, key, cost)
if decision.Allowed {
return nil
}
slog.Info("rate limit refused request", slog.String("scope", string(scope)), slog.String("key", key))
return newRateLimitError(scope, decision)
}
// chargeBatch adds the extra cost of a batch request to the caller's catch-all
// budget. The authorizer already counted the request once, so a batch of n
// items costs n in total.
func (s *APIV1Service) chargeBatch(ctx context.Context, items int) {
if items <= 1 {
return
}
scope, key := callerBudget(ctx)
s.charge(scope, key, items-1)
}
// requireChallenge verifies the challenge token on the request when a
// challenge is configured. With no challenge configured it is a no-op.
func (s *APIV1Service) requireChallenge(ctx context.Context, scope ratelimit.Scope) error {
if s.Challenge == nil {
return nil
}
token := ""
if md, ok := metadata.FromIncomingContext(ctx); ok {
if values := md.Get(challengeTokenMetadataKey); len(values) > 0 {
token = values[0]
}
}
if token == "" {
return newChallengeRequiredError("challenge token is required")
}
if err := s.Challenge.Verify(ctx, scope, token, clientip.FromContext(ctx)); err != nil {
slog.Info("challenge verification failed", slog.String("scope", string(scope)), slog.String("error", err.Error()))
return newChallengeRequiredError("challenge verification failed")
}
return nil
}
// refund gives back units consumed for an attempt that turned out not to count.
func (s *APIV1Service) refund(scope ratelimit.Scope, key string, cost int) {
if s.RateLimiter == nil || key == "" {
return
}
s.RateLimiter.Refund(scope, key, cost)
}
// signInAttempt reserves one unit in each sign-in scope before the credential
// check, so concurrent attempts cannot share one remaining unit, and gives
// the units back when the attempt succeeds. Only failures end up counted.
type signInAttempt struct {
service *APIV1Service
clientIP string
username string
reserved []ratelimit.Scope
}
// reserveSignIn consumes the sign-in budgets for an attempt. It returns the
// rate-limit error when either budget is spent; a refusal charges nothing.
func (s *APIV1Service) reserveSignIn(clientIP, username string) (*signInAttempt, error) {
attempt := &signInAttempt{service: s, clientIP: clientIP, username: username}
if err := s.throttleAndCharge(ratelimit.ScopeSignInIP, clientIP, 1); err != nil {
return nil, err
}
attempt.reserved = append(attempt.reserved, ratelimit.ScopeSignInIP)
if username != "" {
if err := s.throttleAndCharge(ratelimit.ScopeSignInAccount, username, 1); err != nil {
attempt.succeeded()
return nil, err
}
attempt.reserved = append(attempt.reserved, ratelimit.ScopeSignInAccount)
}
return attempt, nil
}
// succeeded refunds the reservation: a successful sign-in does not count.
func (a *signInAttempt) succeeded() {
for _, scope := range a.reserved {
key := a.clientIP
if scope == ratelimit.ScopeSignInAccount {
key = a.username
}
a.service.refund(scope, key, 1)
}
a.reserved = nil
}
+87
View File
@@ -0,0 +1,87 @@
package v1
import (
"testing"
"time"
"connectrpc.com/connect"
"github.com/stretchr/testify/require"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/usememos/memos/internal/ratelimit"
)
func refusedDecision() ratelimit.Decision {
return ratelimit.Decision{
Allowed: false,
Rule: ratelimit.Rule{Limit: 10, Window: time.Minute},
Remaining: 0,
RetryAfter: 42 * time.Second,
}
}
func TestNewRateLimitErrorFollowsAIP193(t *testing.T) {
err := newRateLimitError(ratelimit.ScopeSignInIP, refusedDecision())
st := status.Convert(err)
require.Equal(t, codes.ResourceExhausted, st.Code())
require.Equal(t, rateLimitMessage, st.Message())
var info *errdetails.ErrorInfo
var retry *errdetails.RetryInfo
for _, detail := range st.Details() {
switch d := detail.(type) {
case *errdetails.ErrorInfo:
info = d
case *errdetails.RetryInfo:
retry = d
default:
}
}
require.NotNil(t, info, "ErrorInfo is mandatory")
require.Equal(t, reasonRateLimited, info.Reason)
require.Equal(t, errorInfoDomain, info.Domain)
require.Equal(t, "signin_ip", info.Metadata["scope"])
require.Equal(t, "42", info.Metadata["retry_after_seconds"])
require.Equal(t, "10", info.Metadata["limit"])
require.Equal(t, "60", info.Metadata["window_seconds"])
require.Equal(t, "0", info.Metadata["remaining"])
require.NotNil(t, retry, "a throttle carries RetryInfo so AIP-194 clients know it recovers")
require.Equal(t, 42*time.Second, retry.RetryDelay.AsDuration())
}
func TestRateLimitHTTPHeaders(t *testing.T) {
header := rateLimitHTTPHeaders(status.Convert(newRateLimitError(ratelimit.ScopeAnonymous, refusedDecision())))
require.Equal(t, "42", header.Get("Retry-After"))
require.Equal(t, `"anonymous";r=0;t=42`, header.Get("RateLimit"))
require.Equal(t, `"anonymous";q=10;w=60`, header.Get("RateLimit-Policy"))
require.Nil(t, rateLimitHTTPHeaders(status.New(codes.ResourceExhausted, "quota")), "a refusal without the reason carries no rate-limit fields")
require.Nil(t, rateLimitHTTPHeaders(status.New(codes.InvalidArgument, "x")))
}
func TestConvertGRPCErrorCarriesDetailsAndHeaders(t *testing.T) {
err := convertGRPCError(newRateLimitError(ratelimit.ScopeAnonymous, refusedDecision()))
connectErr, ok := err.(*connect.Error)
require.True(t, ok)
require.Equal(t, connect.CodeResourceExhausted, connectErr.Code())
require.Equal(t, "42", connectErr.Meta().Get("Retry-After"))
require.Equal(t, `"anonymous";r=0;t=42`, connectErr.Meta().Get("RateLimit"))
types := []string{}
for _, detail := range connectErr.Details() {
types = append(types, detail.Type())
}
require.ElementsMatch(t, []string{"google.rpc.ErrorInfo", "google.rpc.RetryInfo"}, types)
}
func TestNewChallengeRequiredError(t *testing.T) {
st := status.Convert(newChallengeRequiredError("challenge token is required"))
require.Equal(t, codes.FailedPrecondition, st.Code())
require.Len(t, st.Details(), 1)
info, ok := st.Details()[0].(*errdetails.ErrorInfo)
require.True(t, ok)
require.Equal(t, reasonChallengeRequired, info.Reason)
require.Nil(t, rateLimitHTTPHeaders(st))
}
+4
View File
@@ -9,6 +9,7 @@ import (
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/usememos/memos/internal/ratelimit"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
"github.com/usememos/memos/store"
)
@@ -56,6 +57,9 @@ func (s *APIV1Service) UpsertMemoReaction(ctx context.Context, request *v1pb.Ups
if user == nil {
return nil, status.Errorf(codes.Unauthenticated, "user not authenticated")
}
if err := s.throttleAndCharge(ratelimit.ScopeWriteUser, userKey(user.ID), 1); err != nil {
return nil, err
}
if request.GetReaction() == nil {
return nil, status.Error(codes.InvalidArgument, "reaction is required")
}
+434
View File
@@ -0,0 +1,434 @@
package test
import (
"context"
"errors"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/bcrypt"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"github.com/usememos/memos/internal/clientip"
"github.com/usememos/memos/internal/ratelimit"
apiv1 "github.com/usememos/memos/proto/gen/api/v1"
"github.com/usememos/memos/server/auth"
apiv1server "github.com/usememos/memos/server/router/api/v1"
"github.com/usememos/memos/store"
)
// limitedPolicy returns the default table with one scope tightened.
func limitedPolicy(scope ratelimit.Scope, limit int) ratelimit.Policy {
policy := ratelimit.DefaultPolicy()
policy[scope] = ratelimit.Rule{Limit: limit, Window: time.Hour}
return policy
}
func withClientIP(ctx context.Context, ip string) context.Context {
return clientip.WithClientIP(ctx, ip)
}
func requireRateLimited(t *testing.T, err error, scope ratelimit.Scope) {
t.Helper()
st := status.Convert(err)
require.Equal(t, codes.ResourceExhausted, st.Code(), "got %v", err)
for _, detail := range st.Details() {
if info, ok := detail.(*errdetails.ErrorInfo); ok {
require.Equal(t, "RATE_LIMITED", info.Reason)
require.Equal(t, string(scope), info.Metadata["scope"])
retryAfter, err := strconv.Atoi(info.Metadata["retry_after_seconds"])
require.NoError(t, err)
require.Positive(t, retryAfter)
return
}
}
t.Fatalf("no ErrorInfo detail on %v", err)
}
func passwordSignIn(ctx context.Context, ts *TestService, username, password string) error {
_, err := ts.Service.SignIn(apiv1server.WithHeaderCarrier(ctx), &apiv1.SignInRequest{
Credentials: &apiv1.SignInRequest_PasswordCredentials_{
PasswordCredentials: &apiv1.SignInRequest_PasswordCredentials{Username: username, Password: password},
},
})
return err
}
func TestSignInRateLimits(t *testing.T) {
ctx := withClientIP(context.Background(), "203.0.113.10")
t.Run("failures count and the check precedes the credential check", func(t *testing.T) {
ts := NewTestService(t)
defer ts.Cleanup()
ts.Service.RateLimiter = ratelimit.NewMemoryLimiter(limitedPolicy(ratelimit.ScopeSignInAccount, 2))
createPasswordUser(ctx, t, ts, "alice", "correct-password")
require.Equal(t, codes.InvalidArgument, status.Code(passwordSignIn(ctx, ts, "alice", "wrong")))
require.Equal(t, codes.InvalidArgument, status.Code(passwordSignIn(ctx, ts, "alice", "wrong")))
// The third attempt is refused even with the right password: the limit
// is checked before the password is looked at.
requireRateLimited(t, passwordSignIn(ctx, ts, "alice", "correct-password"), ratelimit.ScopeSignInAccount)
// Another account is unaffected.
createPasswordUser(ctx, t, ts, "bob", "bob-password")
require.NoError(t, passwordSignIn(ctx, ts, "bob", "bob-password"))
})
t.Run("successes do not count", func(t *testing.T) {
ts := NewTestService(t)
defer ts.Cleanup()
ts.Service.RateLimiter = ratelimit.NewMemoryLimiter(limitedPolicy(ratelimit.ScopeSignInAccount, 1))
createPasswordUser(ctx, t, ts, "alice", "correct-password")
for range 5 {
require.NoError(t, passwordSignIn(ctx, ts, "alice", "correct-password"))
}
})
t.Run("the account limit does not reveal whether the account exists", func(t *testing.T) {
ts := NewTestService(t)
defer ts.Cleanup()
ts.Service.RateLimiter = ratelimit.NewMemoryLimiter(limitedPolicy(ratelimit.ScopeSignInAccount, 1))
createPasswordUser(ctx, t, ts, "alice", "correct-password")
require.Equal(t, codes.InvalidArgument, status.Code(passwordSignIn(ctx, ts, "alice", "wrong")))
require.Equal(t, codes.InvalidArgument, status.Code(passwordSignIn(ctx, ts, "nobody", "wrong")))
requireRateLimited(t, passwordSignIn(ctx, ts, "alice", "wrong"), ratelimit.ScopeSignInAccount)
requireRateLimited(t, passwordSignIn(ctx, ts, "nobody", "wrong"), ratelimit.ScopeSignInAccount)
})
t.Run("the address limit is keyed by resolved address", func(t *testing.T) {
ts := NewTestService(t)
defer ts.Cleanup()
ts.Service.RateLimiter = ratelimit.NewMemoryLimiter(limitedPolicy(ratelimit.ScopeSignInIP, 1))
require.Equal(t, codes.InvalidArgument, status.Code(passwordSignIn(ctx, ts, "nobody", "wrong")))
requireRateLimited(t, passwordSignIn(ctx, ts, "someone-else", "wrong"), ratelimit.ScopeSignInIP)
other := withClientIP(context.Background(), "203.0.113.11")
require.Equal(t, codes.InvalidArgument, status.Code(passwordSignIn(other, ts, "nobody", "wrong")))
})
t.Run("concurrent attempts cannot share one remaining unit", func(t *testing.T) {
ts := NewTestService(t)
defer ts.Cleanup()
ts.Service.RateLimiter = ratelimit.NewMemoryLimiter(limitedPolicy(ratelimit.ScopeSignInAccount, 2))
createPasswordUser(ctx, t, ts, "alice", "correct-password")
const racers = 16
results := make([]codes.Code, racers)
start := make(chan struct{})
var wg sync.WaitGroup
for i := range racers {
wg.Go(func() {
<-start
results[i] = status.Code(passwordSignIn(ctx, ts, "alice", "wrong"))
})
}
close(start)
wg.Wait()
guesses := 0
for _, code := range results {
switch code {
case codes.InvalidArgument:
guesses++
case codes.ResourceExhausted:
default:
t.Fatalf("unexpected code %v", code)
}
}
require.Equal(t, 2, guesses, "each attempt reserves its unit before the credential check")
})
t.Run("a nil limiter leaves sign-in unbounded", func(t *testing.T) {
ts := NewTestService(t)
defer ts.Cleanup()
for range 20 {
require.Equal(t, codes.InvalidArgument, status.Code(passwordSignIn(ctx, ts, "nobody", "wrong")))
}
})
}
func TestCreateUserRateLimits(t *testing.T) {
ctx := withClientIP(context.Background(), "203.0.113.20")
signup := func(ts *TestService, ctx context.Context, username string, validateOnly bool) error {
_, err := ts.Service.CreateUser(ctx, &apiv1.CreateUserRequest{
User: &apiv1.User{Username: username, Password: "password123"},
ValidateOnly: validateOnly,
})
return err
}
t.Run("self-service signup is bounded per address", func(t *testing.T) {
ts := NewTestService(t)
defer ts.Cleanup()
_, err := ts.CreateHostUser(ctx, "admin")
require.NoError(t, err)
ts.Service.RateLimiter = ratelimit.NewMemoryLimiter(limitedPolicy(ratelimit.ScopeSignupIP, 1))
require.NoError(t, signup(ts, ctx, "first", false))
requireRateLimited(t, signup(ts, ctx, "second", false), ratelimit.ScopeSignupIP)
// validate_only has its own scope and is still allowed.
require.NoError(t, signup(ts, ctx, "third", true))
// Another address is unaffected.
require.NoError(t, signup(ts, withClientIP(context.Background(), "203.0.113.21"), "fourth", false))
})
t.Run("admin creation is exempt", func(t *testing.T) {
ts := NewTestService(t)
defer ts.Cleanup()
admin, err := ts.CreateHostUser(ctx, "admin")
require.NoError(t, err)
ts.Service.RateLimiter = ratelimit.NewMemoryLimiter(limitedPolicy(ratelimit.ScopeSignupIP, 1))
adminCtx := ts.CreateUserContext(ctx, admin.ID)
for i := range 3 {
require.NoError(t, signup(ts, adminCtx, "staff"+strconv.Itoa(i), false))
}
})
t.Run("first user setup is exempt", func(t *testing.T) {
ts := NewTestService(t)
defer ts.Cleanup()
limiter := ratelimit.NewMemoryLimiter(limitedPolicy(ratelimit.ScopeSignupIP, 1))
ts.Service.RateLimiter = limiter
require.NoError(t, signup(ts, ctx, "owner", false))
require.Equal(t, 1, limiter.Allowed(ratelimit.ScopeSignupIP, "203.0.113.20", 1).Remaining, "setup did not spend the signup budget")
})
}
type fakeChallenge struct {
accept string
seen []string
}
func (*fakeChallenge) Provider() string { return "fake" }
func (*fakeChallenge) SiteKey() string { return "site-key" }
func (c *fakeChallenge) Verify(_ context.Context, _ ratelimit.Scope, token string, clientIP string) error {
c.seen = append(c.seen, clientIP)
if token != c.accept {
return errors.New("bad token")
}
return nil
}
func withChallengeToken(ctx context.Context, token string) context.Context {
return metadata.NewIncomingContext(ctx, metadata.Pairs("challenge-token", token))
}
func requireChallengeRequired(t *testing.T, err error) {
t.Helper()
st := status.Convert(err)
require.Equal(t, codes.FailedPrecondition, st.Code(), "got %v", err)
require.Len(t, st.Details(), 1)
info, ok := st.Details()[0].(*errdetails.ErrorInfo)
require.True(t, ok)
require.Equal(t, "CHALLENGE_REQUIRED", info.Reason)
}
func TestChallengeSeam(t *testing.T) {
ctx := withClientIP(context.Background(), "203.0.113.30")
t.Run("signup requires a valid token when configured", func(t *testing.T) {
ts := NewTestService(t)
defer ts.Cleanup()
_, err := ts.CreateHostUser(ctx, "admin")
require.NoError(t, err)
challenge := &fakeChallenge{accept: "good"}
ts.Service.Challenge = challenge
request := func(ctx context.Context) error {
_, err := ts.Service.CreateUser(ctx, &apiv1.CreateUserRequest{User: &apiv1.User{Username: "newcomer", Password: "password123"}})
return err
}
requireChallengeRequired(t, request(ctx))
requireChallengeRequired(t, request(withChallengeToken(ctx, "bad")))
require.NoError(t, request(withChallengeToken(ctx, "good")))
require.Equal(t, []string{"203.0.113.30", "203.0.113.30"}, challenge.seen, "the resolver's address is handed to the verifier")
profile, err := ts.Service.GetInstanceProfile(ctx, &apiv1.GetInstanceProfileRequest{})
require.NoError(t, err)
require.Equal(t, "fake", profile.GetChallenge().GetProvider())
require.Equal(t, "site-key", profile.GetChallenge().GetSiteKey())
})
t.Run("password sign-in requires a valid token, SSO does not", func(t *testing.T) {
ts := NewTestService(t)
defer ts.Cleanup()
createPasswordUser(ctx, t, ts, "alice", "correct-password")
ts.Service.Challenge = &fakeChallenge{accept: "good"}
requireChallengeRequired(t, passwordSignIn(ctx, ts, "alice", "correct-password"))
require.NoError(t, passwordSignIn(withChallengeToken(ctx, "good"), ts, "alice", "correct-password"))
})
t.Run("admin creation skips the challenge", func(t *testing.T) {
ts := NewTestService(t)
defer ts.Cleanup()
admin, err := ts.CreateHostUser(ctx, "admin")
require.NoError(t, err)
ts.Service.Challenge = &fakeChallenge{accept: "good"}
_, err = ts.Service.CreateUser(ts.CreateUserContext(ctx, admin.ID), &apiv1.CreateUserRequest{User: &apiv1.User{Username: "staff", Password: "password123"}})
require.NoError(t, err)
})
t.Run("no challenge configured means no profile entry and no requirement", func(t *testing.T) {
ts := NewTestService(t)
defer ts.Cleanup()
profile, err := ts.Service.GetInstanceProfile(ctx, &apiv1.GetInstanceProfileRequest{})
require.NoError(t, err)
require.Nil(t, profile.GetChallenge())
})
}
type denyDomainPolicy struct{ domain string }
func (p denyDomainPolicy) AllowSignup(_ context.Context, email string, _ string) error {
if len(email) > len(p.domain) && email[len(email)-len(p.domain):] == p.domain {
return errors.New("addresses at " + p.domain + " are not accepted")
}
return nil
}
func TestSignupPolicySeam(t *testing.T) {
ctx := withClientIP(context.Background(), "203.0.113.40")
ts := NewTestService(t)
defer ts.Cleanup()
admin, err := ts.CreateHostUser(ctx, "admin")
require.NoError(t, err)
ts.Service.SignupPolicy = denyDomainPolicy{domain: "@blocked.example"}
_, err = ts.Service.CreateUser(ctx, &apiv1.CreateUserRequest{User: &apiv1.User{Username: "spammer", Email: "Spam@Blocked.Example", Password: "password123"}})
require.Equal(t, codes.PermissionDenied, status.Code(err))
require.Contains(t, status.Convert(err).Message(), "blocked.example")
_, err = ts.Service.CreateUser(ctx, &apiv1.CreateUserRequest{User: &apiv1.User{Username: "welcome", Email: "hi@allowed.example", Password: "password123"}})
require.NoError(t, err)
// The policy is consulted for self-service signups only.
_, err = ts.Service.CreateUser(ts.CreateUserContext(ctx, admin.ID), &apiv1.CreateUserRequest{User: &apiv1.User{Username: "staff", Email: "staff@blocked.example", Password: "password123"}})
require.NoError(t, err)
}
func TestLinkMetadataRateLimit(t *testing.T) {
ctx := withClientIP(context.Background(), "203.0.113.50")
ts := NewTestService(t)
defer ts.Cleanup()
limiter := ratelimit.NewMemoryLimiter(limitedPolicy(ratelimit.ScopeLinkMetadata, 2))
ts.Service.RateLimiter = limiter
// Three URLs exceed the budget of two, so the batch is refused before any
// fetch: these hosts do not exist and would otherwise fail differently.
_, err := ts.Service.BatchGetLinkMetadata(ctx, &apiv1.BatchGetLinkMetadataRequest{Urls: []string{
"http://a.invalid/", "http://b.invalid/", "http://c.invalid/",
}})
requireRateLimited(t, err, ratelimit.ScopeLinkMetadata)
require.Equal(t, 2, limiter.Allowed(ratelimit.ScopeLinkMetadata, "203.0.113.50", 1).Remaining, "a refused batch costs nothing")
// An authenticated caller is keyed by user, not address: spending the
// user's budget elsewhere refuses them while the address stays untouched.
user, err := ts.CreateRegularUser(ctx, "reader")
require.NoError(t, err)
userCtx := ts.CreateUserContext(ctx, user.ID)
limiter.Hit(ratelimit.ScopeLinkMetadata, strconv.Itoa(int(user.ID)), 2)
_, err = ts.Service.GetLinkMetadata(userCtx, &apiv1.GetLinkMetadataRequest{Url: "http://a.invalid/"})
requireRateLimited(t, err, ratelimit.ScopeLinkMetadata)
require.Equal(t, 2, limiter.Allowed(ratelimit.ScopeLinkMetadata, "203.0.113.50", 1).Remaining)
}
func TestWriteAndUploadBudgets(t *testing.T) {
ctx := withClientIP(context.Background(), "203.0.113.60")
ts := NewTestService(t)
defer ts.Cleanup()
ts.Service.RateLimiter = ratelimit.NewMemoryLimiter(limitedPolicy(ratelimit.ScopeWriteUser, 2))
user, err := ts.CreateRegularUser(ctx, "writer")
require.NoError(t, err)
userCtx := ts.CreateUserContext(ctx, user.ID)
createMemo := func(content string) error {
_, err := ts.Service.CreateMemo(userCtx, &apiv1.CreateMemoRequest{Memo: &apiv1.Memo{Content: content, Visibility: apiv1.Visibility_PRIVATE}})
return err
}
require.NoError(t, createMemo("one"))
require.NoError(t, createMemo("two"))
requireRateLimited(t, createMemo("three"), ratelimit.ScopeWriteUser)
// Another user has their own budget.
other, err := ts.CreateRegularUser(ctx, "other")
require.NoError(t, err)
_, err = ts.Service.CreateMemo(ts.CreateUserContext(ctx, other.ID), &apiv1.CreateMemoRequest{Memo: &apiv1.Memo{Content: "hi", Visibility: apiv1.Visibility_PRIVATE}})
require.NoError(t, err)
}
func TestAuthorizerBudgets(t *testing.T) {
ctx := withClientIP(context.Background(), "203.0.113.70")
ts := NewTestService(t)
defer ts.Cleanup()
policy := ratelimit.DefaultPolicy()
policy[ratelimit.ScopeAnonymous] = ratelimit.Rule{Limit: 2, Window: time.Hour}
policy[ratelimit.ScopeAuthenticated] = ratelimit.Rule{Limit: 3, Window: time.Hour}
limiter := ratelimit.NewMemoryLimiter(policy)
authorizer := apiv1server.NewAuthorizer(ts.Store, ts.Secret).WithRateLimiter(limiter)
const procedure = "/memos.api.v1.MemoService/ListMemos"
// Anonymous callers share a budget per address.
require.NoError(t, authorizer.Throttle(ctx, procedure, nil))
require.NoError(t, authorizer.Throttle(ctx, procedure, nil))
requireRateLimited(t, authorizer.Throttle(ctx, procedure, nil), ratelimit.ScopeAnonymous)
require.NoError(t, authorizer.Throttle(withClientIP(context.Background(), "203.0.113.71"), procedure, nil))
// Authenticated callers are keyed by user, whether the identity came from
// access-token claims or from a personal access token.
claims := &auth.AuthResult{Claims: &auth.UserClaims{UserID: 7}}
pat := &auth.AuthResult{User: &store.User{ID: 7}}
require.NoError(t, authorizer.Throttle(ctx, procedure, claims))
require.NoError(t, authorizer.Throttle(ctx, procedure, pat))
require.NoError(t, authorizer.Throttle(ctx, procedure, claims))
requireRateLimited(t, authorizer.Throttle(ctx, procedure, pat), ratelimit.ScopeAuthenticated)
require.NoError(t, authorizer.Throttle(ctx, procedure, &auth.AuthResult{Claims: &auth.UserClaims{UserID: 8}}))
// A request with no resolvable address is not counted rather than lumped together.
require.NoError(t, authorizer.Throttle(context.Background(), procedure, nil))
require.NoError(t, authorizer.Throttle(context.Background(), procedure, nil))
require.NoError(t, authorizer.Throttle(context.Background(), procedure, nil))
// Without a limiter nothing is bounded.
unlimited := apiv1server.NewAuthorizer(ts.Store, ts.Secret)
for range 10 {
require.NoError(t, unlimited.Throttle(ctx, procedure, nil))
}
}
func TestBatchRequestsCostTheirItemCount(t *testing.T) {
ctx := withClientIP(context.Background(), "203.0.113.80")
ts := NewTestService(t)
defer ts.Cleanup()
limiter := ratelimit.NewMemoryLimiter(ratelimit.DefaultPolicy())
ts.Service.RateLimiter = limiter
user, err := ts.CreateRegularUser(ctx, "batcher")
require.NoError(t, err)
userCtx := ts.CreateUserContext(ctx, user.ID)
before := limiter.Allowed(ratelimit.ScopeAuthenticated, strconv.Itoa(int(user.ID)), 1).Remaining
_, err = ts.Service.BatchGetUsers(userCtx, &apiv1.BatchGetUsersRequest{Usernames: []string{"a", "b", "c", "d"}})
require.NoError(t, err)
after := limiter.Allowed(ratelimit.ScopeAuthenticated, strconv.Itoa(int(user.ID)), 1).Remaining
require.Equal(t, 3, before-after, "the authorizer counts one; the handler adds the other three")
}
// createPasswordUser creates a user who can sign in with the given password.
func createPasswordUser(ctx context.Context, t *testing.T, ts *TestService, username, password string) *store.User {
t.Helper()
passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
require.NoError(t, err)
user, err := ts.Store.CreateUser(ctx, &store.User{
Username: username,
Role: store.RoleUser,
Email: testEmailForUsername(username),
PasswordHash: string(passwordHash),
})
require.NoError(t, err)
return user
}
+27
View File
@@ -16,6 +16,8 @@ import (
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/usememos/memos/internal/clientip"
"github.com/usememos/memos/internal/ratelimit"
"github.com/usememos/memos/internal/util"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
"github.com/usememos/memos/store"
@@ -144,6 +146,7 @@ func (s *APIV1Service) BatchGetUsers(ctx context.Context, request *v1pb.BatchGet
if nonEmptyUsernameCount > maxBatchGetUsers {
return nil, status.Errorf(codes.InvalidArgument, "too many usernames (max %d)", maxBatchGetUsers)
}
s.chargeBatch(ctx, nonEmptyUsernameCount)
if len(uniqueUsernames) == 0 {
return &v1pb.BatchGetUsersResponse{Users: []*v1pb.User{}}, nil
@@ -252,6 +255,30 @@ func (s *APIV1Service) CreateUser(ctx context.Context, request *v1pb.CreateUserR
}
}
// Self-service registration is bounded per address, may require a challenge,
// and may be vetoed by the deployment's signup policy. Admin creation and
// first-user setup are not.
if currentUser == nil || currentUser.Role != store.RoleAdmin {
clientIP := clientip.FromContext(ctx)
scope := ratelimit.ScopeSignupIP
if request.ValidateOnly {
scope = ratelimit.ScopeValidateIP
}
if err := s.throttleAndCharge(scope, clientIP, 1); err != nil {
return nil, err
}
if !request.ValidateOnly {
if err := s.requireChallenge(ctx, ratelimit.ScopeSignupIP); err != nil {
return nil, err
}
if s.SignupPolicy != nil {
if err := s.SignupPolicy.AllowSignup(ctx, email, clientIP); err != nil {
return nil, status.Error(codes.PermissionDenied, err.Error())
}
}
}
}
// If validate_only is true, just validate without creating
if request.ValidateOnly {
// Perform validation checks without actually creating the user. The
@@ -11,9 +11,11 @@ import (
"google.golang.org/protobuf/types/known/emptypb"
"github.com/usememos/memos/internal/filter"
"github.com/usememos/memos/internal/ratelimit"
"github.com/usememos/memos/internal/util"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/server/auth"
"github.com/usememos/memos/store"
)
@@ -134,6 +136,9 @@ func (s *APIV1Service) CreateMemoView(ctx context.Context, request *v1pb.CreateM
if err := s.authorizeMemoViewAccess(ctx, user); err != nil {
return nil, err
}
if err := s.throttleAndCharge(ratelimit.ScopeWriteUser, userKey(auth.GetUserID(ctx)), 1); err != nil {
return nil, err
}
newMemoView := &storepb.MemoViewsUserSetting_MemoView{
Id: util.GenUUID(),
@@ -11,6 +11,7 @@ import (
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/usememos/memos/internal/ratelimit"
"github.com/usememos/memos/internal/webhook"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
storepb "github.com/usememos/memos/proto/gen/store"
@@ -64,6 +65,9 @@ func (s *APIV1Service) CreateUserWebhook(ctx context.Context, request *v1pb.Crea
if currentUser == nil {
return nil, status.Errorf(codes.Unauthenticated, "user not authenticated")
}
if err := s.throttleAndCharge(ratelimit.ScopeWriteUser, userKey(currentUser.ID), 1); err != nil {
return nil, err
}
if currentUser.ID != userID {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
+71 -1
View File
@@ -5,6 +5,9 @@ import (
"log/slog"
"net/http"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"connectrpc.com/connect"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/labstack/echo/v5"
@@ -14,6 +17,7 @@ import (
"github.com/usememos/memos/internal/httpgetter"
"github.com/usememos/memos/internal/markdown"
"github.com/usememos/memos/internal/profile"
"github.com/usememos/memos/internal/ratelimit"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
"github.com/usememos/memos/server/auth"
"github.com/usememos/memos/server/notification"
@@ -51,6 +55,15 @@ type APIV1Service struct {
SSEHub *SSEHub
NotificationEmailSender notification.EmailSender
// RateLimiter bounds request rates; nil disables every limit.
RateLimiter ratelimit.Limiter
// Challenge verifies proof-of-humanity tokens on signup and password
// sign-in; nil means no challenge is configured.
Challenge ratelimit.Challenge
// SignupPolicy decides whether a self-service registration may proceed;
// nil allows every registration.
SignupPolicy ratelimit.SignupPolicy
// thumbnailSemaphore limits concurrent thumbnail generation to prevent memory exhaustion
thumbnailSemaphore *semaphore.Weighted
imageProcessingSemaphore *semaphore.Weighted
@@ -75,6 +88,7 @@ func NewAPIV1Service(secret string, profile *profile.Profile, store *store.Store
MarkdownService: markdownService,
SSEHub: NewSSEHub(),
NotificationEmailSender: nil,
RateLimiter: newProfileRateLimiter(profile),
thumbnailSemaphore: semaphore.NewWeighted(3), // Limit to 3 concurrent thumbnail generations
imageProcessingSemaphore: semaphore.NewWeighted(2),
}
@@ -105,7 +119,7 @@ func newGatewayMarshaler() *runtime.HTTPBodyMarshaler {
func (s *APIV1Service) RegisterGateway(ctx context.Context, echoServer *echo.Echo) error {
// Shared authorizer: one source of truth for authentication and anonymous-access
// policy, used by both the gRPC-Gateway middleware and the Connect interceptor.
authorizer := NewAuthorizer(s.Store, s.Secret)
authorizer := NewAuthorizer(s.Store, s.Secret).WithRateLimiter(s.RateLimiter)
// grpc-gateway does not hand the matched procedure to middleware:
// runtime.RPCMethod is only populated by the generated handler, which runs
@@ -136,6 +150,10 @@ func (s *APIV1Service) RegisterGateway(ctx context.Context, echoServer *echo.Ech
writeGatewayAuthorizationError(w, err)
return
}
if err := authorizer.Throttle(ctx, procedure, result); err != nil {
writeGatewayStatusError(w, err)
return
}
// Apply the identity to the context (no-op for permitted anonymous requests).
if result != nil {
@@ -151,6 +169,8 @@ func (s *APIV1Service) RegisterGateway(ctx context.Context, echoServer *echo.Ech
gwMux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, newGatewayMarshaler()),
runtime.WithMiddlewares(gatewayAuthMiddleware),
runtime.WithErrorHandler(gatewayErrorHandler),
runtime.WithIncomingHeaderMatcher(gatewayIncomingHeaderMatcher),
)
if err := v1pb.RegisterInstanceServiceHandlerServer(ctx, gwMux, s); err != nil {
return err
@@ -215,6 +235,56 @@ func setAPIResponseNoStoreHeaders(header http.Header) {
header.Set("Expires", "0")
}
// newProfileRateLimiter builds the default limiter, or nil when the profile
// turns rate limiting off.
func newProfileRateLimiter(profile *profile.Profile) ratelimit.Limiter {
if !profile.RateLimit {
return nil
}
return ratelimit.NewMemoryLimiter(ratelimit.DefaultPolicy())
}
// gatewayIncomingHeaderMatcher forwards the challenge token to metadata on top
// of the gateway's default set.
func gatewayIncomingHeaderMatcher(key string) (string, bool) {
if http.CanonicalHeaderKey(key) == challengeTokenHeader {
return challengeTokenMetadataKey, true
}
return runtime.DefaultHeaderMatcher(key)
}
// gatewayErrorHandler adds the rate-limit header fields to a refusal before
// the default handler writes the status body.
func gatewayErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) {
if st, ok := status.FromError(err); ok {
for key, values := range rateLimitHTTPHeaders(st) {
for _, value := range values {
w.Header().Add(key, value)
}
}
}
runtime.DefaultHTTPErrorHandler(ctx, mux, marshaler, w, r, err)
}
// writeGatewayStatusError writes a gRPC status as the gateway's JSON error body,
// including its details and any rate-limit header fields.
func writeGatewayStatusError(w http.ResponseWriter, err error) {
st := status.Convert(err)
for key, values := range rateLimitHTTPHeaders(st) {
for _, value := range values {
w.Header().Add(key, value)
}
}
body, marshalErr := protojson.Marshal(st.Proto())
if marshalErr != nil {
http.Error(w, `{"code": 13, "message": "failed to encode error"}`, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(runtime.HTTPStatusFromCode(st.Code()))
_, _ = w.Write(body)
}
func writeGatewayAuthorizationError(w http.ResponseWriter, err error) {
if errors.Is(err, ErrUnauthenticated) {
http.Error(w, `{"code": 16, "message": "authentication required"}`, http.StatusUnauthorized)
+8
View File
@@ -14,6 +14,7 @@ import (
"github.com/labstack/echo/v5/middleware"
"github.com/pkg/errors"
"github.com/usememos/memos/internal/clientip"
"github.com/usememos/memos/internal/profile"
storepb "github.com/usememos/memos/proto/gen/store"
apiv1 "github.com/usememos/memos/server/router/api/v1"
@@ -44,6 +45,13 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
echoServer := echo.New()
echoServer.Use(middleware.Recover())
echoServer.Use(newCORSMiddleware(profile))
// Resolve the client address once per request, before anything that keys
// on it: rate limits, session records, and the file server.
clientIPResolver, err := clientip.ParseTrustedProxies(profile.TrustedProxies)
if err != nil {
return nil, errors.Wrap(err, "invalid trusted proxies")
}
echoServer.Use(clientip.Middleware(clientIPResolver))
s.echoServer = echoServer
instanceBasicSetting, err := s.getOrUpsertInstanceBasicSetting(ctx)
+109
View File
@@ -0,0 +1,109 @@
package test
import (
"bytes"
"context"
"encoding/json"
"net/http"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
// TestStartupRateLimitOnBothTransports proves the refusal contract on the
// wire, which only a booted server can: HTTP 429, Retry-After and the IETF
// RateLimit fields, and an ErrorInfo detail with reason RATE_LIMITED, from
// both the gRPC gateway and the Connect handler. The sign-in address budget
// counts failures for an unknown user, so no password hashing is involved.
func TestStartupRateLimitOnBothTransports(t *testing.T) {
ctx := context.Background()
inst := bootInstance(ctx, t, instanceOptions{instanceURL: "http://127.0.0.1", rateLimit: true})
inst.createAdmin(t)
post := func(path string, body map[string]any, headers map[string]string) *http.Response {
t.Helper()
encoded, err := json.Marshal(body)
require.NoError(t, err)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, inst.baseURL+path, bytes.NewReader(encoded))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
for key, value := range headers {
req.Header.Set(key, value)
}
resp, err := inst.client.Do(req)
require.NoError(t, err)
return resp
}
// Each attempt names a different account so the per-account limit never
// trips and the refusal under test is the per-address one.
attempt := 0
nextBody := func() map[string]any {
attempt++
return map[string]any{"passwordCredentials": map[string]any{"username": "nobody-" + strconv.Itoa(attempt), "password": "wrong"}}
}
// Exhaust the address budget over the gateway. The default is 30 failures
// per 15 minutes, so the refusal arrives well within the loop bound.
var refused *http.Response
for range 40 {
resp := post("/api/v1/auth/signin", nextBody(), nil)
if resp.StatusCode == http.StatusTooManyRequests {
refused = resp
break
}
require.Equal(t, http.StatusBadRequest, resp.StatusCode, "a wrong credential is a plain failure until the budget is spent")
resp.Body.Close()
}
require.NotNil(t, refused, "the gateway never refused")
defer refused.Body.Close()
require.NotEmpty(t, refused.Header.Get("Retry-After"))
require.Contains(t, refused.Header.Get("RateLimit"), `"signin_ip";r=0;t=`)
require.Contains(t, refused.Header.Get("RateLimit-Policy"), `"signin_ip";q=30;w=900`)
var gatewayError struct {
Code int `json:"code"`
Message string `json:"message"`
Details []struct {
Type string `json:"@type"`
Reason string `json:"reason"`
Domain string `json:"domain"`
} `json:"details"`
}
require.NoError(t, json.NewDecoder(refused.Body).Decode(&gatewayError))
require.Equal(t, 8, gatewayError.Code, "RESOURCE_EXHAUSTED")
types := map[string]string{}
for _, detail := range gatewayError.Details {
types[detail.Type] = detail.Reason
}
require.Equal(t, "RATE_LIMITED", types["type.googleapis.com/google.rpc.ErrorInfo"])
require.Contains(t, types, "type.googleapis.com/google.rpc.RetryInfo")
// The Connect transport shares the same budget and speaks the same contract.
connectResp := post("/memos.api.v1.AuthService/SignIn", nextBody(), map[string]string{"Connect-Protocol-Version": "1"})
defer connectResp.Body.Close()
require.Equal(t, http.StatusTooManyRequests, connectResp.StatusCode)
require.NotEmpty(t, connectResp.Header.Get("Retry-After"))
require.Contains(t, connectResp.Header.Get("RateLimit"), `"signin_ip";r=0;t=`)
var connectError struct {
Code string `json:"code"`
Details []struct {
Type string `json:"type"`
} `json:"details"`
}
require.NoError(t, json.NewDecoder(connectResp.Body).Decode(&connectError))
require.Equal(t, "resource_exhausted", connectError.Code)
connectTypes := []string{}
for _, detail := range connectError.Details {
connectTypes = append(connectTypes, detail.Type)
}
require.ElementsMatch(t, []string{"google.rpc.ErrorInfo", "google.rpc.RetryInfo"}, connectTypes)
// A forged forwarding header from an untrusted peer does not open a fresh
// budget. The test client connects from loopback, which the default
// trusted-proxy set includes, so the forged header must name a trusted
// address to be walked past and still land on the peer.
forged := post("/api/v1/auth/signin", nextBody(), map[string]string{"X-Forwarded-For": "10.0.0.9"})
defer forged.Body.Close()
require.Equal(t, http.StatusTooManyRequests, forged.StatusCode)
}
+4
View File
@@ -45,6 +45,9 @@ type instanceOptions struct {
// a database without ACCESS, it is also the legacy compatibility input used
// to initialize that dedicated setting exactly once.
instanceURL string
// rateLimit enables the request rate limiter, which the other tests leave
// off so that their request volume never trips a budget.
rateLimit bool
// dataDir reuses an existing data directory instead of a fresh one, which
// is how a restart against an already-migrated database is simulated.
dataDir string
@@ -79,6 +82,7 @@ func bootInstance(ctx context.Context, t *testing.T, opts instanceOptions) *inst
Data: dataDir,
Driver: "sqlite",
InstanceURL: opts.instanceURL,
RateLimit: opts.rateLimit,
Version: version.GetCurrentVersion(),
Commit: version.Commit,
}
+128
View File
@@ -0,0 +1,128 @@
import { useEffect, useRef } from "react";
import { useInstance } from "@/contexts/InstanceContext";
/** Header carrying the challenge token; the server forwards it to the verifier. */
export const CHALLENGE_TOKEN_HEADER = "Challenge-Token";
interface Props {
/** Receives the token when the user passes, and null when it expires or resets. */
onToken: (token: string | null) => void;
/** Bump to reset the widget, for instance after the server rejected a token. */
resetKey?: number;
}
interface ProviderScript {
src: string;
global: "turnstile" | "hcaptcha";
}
// Both providers expose the same explicit-render API shape.
const PROVIDERS: Record<string, ProviderScript> = {
turnstile: { src: "https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit", global: "turnstile" },
hcaptcha: { src: "https://js.hcaptcha.com/1/api.js?render=explicit", global: "hcaptcha" },
};
interface WidgetApi {
render: (container: HTMLElement, options: Record<string, unknown>) => string;
reset: (widgetId?: string) => void;
remove?: (widgetId: string) => void;
}
const scriptLoads = new Map<string, Promise<WidgetApi>>();
function loadProvider(provider: ProviderScript): Promise<WidgetApi> {
const existing = scriptLoads.get(provider.src);
if (existing) {
return existing;
}
const script = document.createElement("script");
const load = new Promise<WidgetApi>((resolve, reject) => {
const ready = () => {
const api = (window as unknown as Record<string, WidgetApi | undefined>)[provider.global];
if (api) resolve(api);
else reject(new Error(`challenge provider ${provider.global} did not initialize`));
};
script.src = provider.src;
script.async = true;
script.onload = ready;
script.onerror = () => reject(new Error(`failed to load challenge provider ${provider.global}`));
document.head.appendChild(script);
});
// A transient failure must not be cached, or every later mount and reset
// would inherit it and sign-in would stay blocked until a page reload.
load.catch(() => {
scriptLoads.delete(provider.src);
script.remove();
});
scriptLoads.set(provider.src, load);
return load;
}
/**
* Renders the proof-of-humanity widget the instance profile asks for, or
* nothing when the instance has none. The open-source app carries the widget
* code for the providers it knows; verification happens on the server.
*/
function ChallengeWidget({ onToken, resetKey = 0 }: Props) {
const { profile } = useInstance();
const challenge = profile.challenge;
const containerRef = useRef<HTMLDivElement>(null);
const widgetRef = useRef<{ api: WidgetApi; id: string } | null>(null);
const onTokenRef = useRef(onToken);
onTokenRef.current = onToken;
const provider = challenge ? PROVIDERS[challenge.provider] : undefined;
const siteKey = challenge?.siteKey ?? "";
useEffect(() => {
if (!provider || !siteKey || !containerRef.current) {
return;
}
let cancelled = false;
const container = containerRef.current;
loadProvider(provider)
.then((api) => {
if (cancelled) return;
const id = api.render(container, {
sitekey: siteKey,
callback: (token: string) => onTokenRef.current(token),
"expired-callback": () => onTokenRef.current(null),
"error-callback": () => onTokenRef.current(null),
});
widgetRef.current = { api, id };
})
.catch((error: unknown) => {
console.error(error);
onTokenRef.current(null);
});
return () => {
cancelled = true;
const widget = widgetRef.current;
widgetRef.current = null;
if (widget) {
try {
widget.api.remove?.(widget.id);
} catch {
// The provider script may already be gone; nothing to clean up.
}
}
container.replaceChildren();
};
}, [provider, siteKey]);
useEffect(() => {
if (resetKey === 0) return;
const widget = widgetRef.current;
if (widget) {
widget.api.reset(widget.id);
onTokenRef.current(null);
}
}, [resetKey]);
if (!provider || !siteKey) {
return null;
}
return <div ref={containerRef} className="flex justify-center" />;
}
export default ChallengeWidget;
+16 -6
View File
@@ -3,13 +3,14 @@ import { LoaderIcon } from "lucide-react";
import { useState } from "react";
import { toast } from "react-hot-toast";
import { setAccessToken } from "@/auth-state";
import ChallengeWidget, { CHALLENGE_TOKEN_HEADER } from "@/components/ChallengeWidget";
import CredentialFields from "@/components/CredentialFields";
import { Button } from "@/components/ui/button";
import { authServiceClient } from "@/connect";
import { useAuth } from "@/contexts/AuthContext";
import useLoading from "@/hooks/useLoading";
import useNavigateTo from "@/hooks/useNavigateTo";
import { handleError } from "@/lib/error";
import { ERROR_REASON_CHALLENGE_REQUIRED, handleError, hasErrorReason } from "@/lib/error";
import { ROUTES } from "@/router/routes";
import { useTranslate } from "@/utils/i18n";
@@ -24,6 +25,8 @@ function PasswordSignInForm({ redirectPath }: PasswordSignInFormProps) {
const actionBtnLoadingState = useLoading(false);
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [challengeToken, setChallengeToken] = useState<string | null>(null);
const [challengeResetKey, setChallengeResetKey] = useState(0);
const handleFormSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
@@ -38,12 +41,15 @@ function PasswordSignInForm({ redirectPath }: PasswordSignInFormProps) {
try {
actionBtnLoadingState.setLoading();
const response = await authServiceClient.signIn({
credentials: {
case: "passwordCredentials",
value: { username, password },
const response = await authServiceClient.signIn(
{
credentials: {
case: "passwordCredentials",
value: { username, password },
},
},
});
challengeToken ? { headers: { [CHALLENGE_TOKEN_HEADER]: challengeToken } } : undefined,
);
// Store access token from login response
if (response.accessToken) {
setAccessToken(response.accessToken, response.accessTokenExpiresAt ? timestampDate(response.accessTokenExpiresAt) : undefined);
@@ -51,6 +57,9 @@ function PasswordSignInForm({ redirectPath }: PasswordSignInFormProps) {
await initialize();
navigateTo(redirectPath || ROUTES.HOME, { replace: true });
} catch (error: unknown) {
if (hasErrorReason(error, ERROR_REASON_CHALLENGE_REQUIRED)) {
setChallengeResetKey((key) => key + 1);
}
handleError(error, toast.error, {
fallbackMessage: "Failed to sign in.",
});
@@ -69,6 +78,7 @@ function PasswordSignInForm({ redirectPath }: PasswordSignInFormProps) {
onUsernameChange={setUsername}
onPasswordChange={setPassword}
/>
<ChallengeWidget onToken={setChallengeToken} resetKey={challengeResetKey} />
<Button type="submit" disabled={actionBtnLoadingState.isLoading}>
{t("common.sign-in")}
{actionBtnLoadingState.isLoading && <LoaderIcon className="ml-1 h-4 w-auto animate-spin opacity-60" />}
+91
View File
@@ -1,11 +1,102 @@
import { BinaryReader } from "@bufbuild/protobuf/wire";
import { type Code, ConnectError } from "@connectrpc/connect";
/** google.rpc.ErrorInfo, the mandatory detail on every server error (AIP-193). */
export interface ErrorInfo {
reason: string;
domain: string;
metadata: Record<string, string>;
}
export const ERROR_REASON_RATE_LIMITED = "RATE_LIMITED";
export const ERROR_REASON_CHALLENGE_REQUIRED = "CHALLENGE_REQUIRED";
const ERROR_INFO_TYPE = "google.rpc.ErrorInfo";
// The api protos never reference google.rpc, so its TypeScript schema is not
// generated; ErrorInfo is three fields, decoded here by hand.
function decodeErrorInfo(bytes: Uint8Array): ErrorInfo {
const info: ErrorInfo = { reason: "", domain: "", metadata: {} };
const reader = new BinaryReader(bytes);
while (reader.pos < reader.len) {
const [field, wireType] = reader.tag();
switch (field) {
case 1:
info.reason = reader.string();
break;
case 2:
info.domain = reader.string();
break;
case 3: {
const entry = new BinaryReader(reader.bytes());
let key = "";
let value = "";
while (entry.pos < entry.len) {
const [entryField, entryWireType] = entry.tag();
if (entryField === 1) key = entry.string();
else if (entryField === 2) value = entry.string();
else entry.skip(entryWireType, entryField);
}
info.metadata[key] = value;
break;
}
default:
reader.skip(wireType, field);
}
}
return info;
}
/** Returns the ErrorInfo detail carried by a Connect error, if any. */
export function getErrorInfo(error: unknown): ErrorInfo | undefined {
if (!(error instanceof ConnectError)) {
return undefined;
}
for (const detail of error.details) {
if ("type" in detail && detail.type === ERROR_INFO_TYPE && "value" in detail && detail.value instanceof Uint8Array) {
try {
return decodeErrorInfo(detail.value);
} catch {
return undefined;
}
}
}
return undefined;
}
export function hasErrorReason(error: unknown, reason: string): boolean {
return getErrorInfo(error)?.reason === reason;
}
/** Seconds to wait before retrying a rate-limited request, or undefined. */
export function getRetryAfterSeconds(error: unknown): number | undefined {
const info = getErrorInfo(error);
if (info?.reason !== ERROR_REASON_RATE_LIMITED) {
return undefined;
}
const seconds = Number.parseInt(info.metadata.retry_after_seconds ?? "", 10);
return Number.isFinite(seconds) && seconds > 0 ? seconds : undefined;
}
function formatWait(seconds: number): string {
if (seconds < 60) return `${seconds}s`;
const minutes = Math.ceil(seconds / 60);
return minutes < 60 ? `${minutes} min` : `${Math.ceil(minutes / 60)} h`;
}
export function hasConnectCode(error: unknown, ...codes: Code[]): error is ConnectError {
return error instanceof ConnectError && codes.includes(error.code);
}
export function getErrorMessage(error: unknown, fallback = "Unknown error"): string {
if (error instanceof ConnectError) {
const retryAfter = getRetryAfterSeconds(error);
if (retryAfter !== undefined) {
return `Too many requests. Try again in ${formatWait(retryAfter)}.`;
}
if (hasErrorReason(error, ERROR_REASON_CHALLENGE_REQUIRED)) {
return "Complete the verification challenge and try again.";
}
return error.rawMessage || fallback;
}
+20 -7
View File
@@ -6,6 +6,7 @@ import { toast } from "react-hot-toast";
import { useSearchParams } from "react-router-dom";
import { setAccessToken } from "@/auth-state";
import AuthPageLayout, { AuthChip, AuthEmptyState, AuthLinkPrompt, AuthOptionsLoading } from "@/components/AuthPageLayout";
import ChallengeWidget, { CHALLENGE_TOKEN_HEADER } from "@/components/ChallengeWidget";
import CredentialFields from "@/components/CredentialFields";
import IdentityProviderButtons from "@/components/IdentityProviderButtons";
import { Button } from "@/components/ui/button";
@@ -15,7 +16,7 @@ import { useInstance } from "@/contexts/InstanceContext";
import { useIdentityProviderList } from "@/hooks/useIdentityProviderQueries";
import useLoading from "@/hooks/useLoading";
import useNavigateTo from "@/hooks/useNavigateTo";
import { handleError } from "@/lib/error";
import { ERROR_REASON_CHALLENGE_REQUIRED, handleError, hasErrorReason } from "@/lib/error";
import { ROUTES } from "@/router/routes";
import { User_Role, UserSchema } from "@/types/proto/api/v1/user_service_pb";
import { AUTH_REDIRECT_PARAM, appendSearchParams, getSafeRedirectPath } from "@/utils/auth-redirect";
@@ -27,6 +28,8 @@ const SignUp = () => {
const actionBtnLoadingState = useLoading(false);
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [challengeToken, setChallengeToken] = useState<string | null>(null);
const [challengeResetKey, setChallengeResetKey] = useState(0);
const { initialize: initAuth } = useAuth();
const { generalSetting: instanceGeneralSetting, profile, initialize: initInstance } = useInstance();
const [searchParams] = useSearchParams();
@@ -60,13 +63,19 @@ const SignUp = () => {
password,
role: User_Role.USER,
});
await userServiceClient.createUser({ user });
const response = await authServiceClient.signIn({
credentials: {
case: "passwordCredentials",
value: { username, password },
// The token is spent by createUser; the sign-in that follows is not a
// credential guess, and the server does not ask for a second one there.
const callOptions = challengeToken ? { headers: { [CHALLENGE_TOKEN_HEADER]: challengeToken } } : undefined;
await userServiceClient.createUser({ user }, callOptions);
const response = await authServiceClient.signIn(
{
credentials: {
case: "passwordCredentials",
value: { username, password },
},
},
});
callOptions,
);
// Store access token from login response
if (response.accessToken) {
setAccessToken(response.accessToken, response.accessTokenExpiresAt ? timestampDate(response.accessTokenExpiresAt) : undefined);
@@ -77,6 +86,9 @@ const SignUp = () => {
await initInstance();
navigateTo(redirectTarget || ROUTES.HOME, { replace: true });
} catch (error: unknown) {
if (hasErrorReason(error, ERROR_REASON_CHALLENGE_REQUIRED)) {
setChallengeResetKey((key) => key + 1);
}
handleError(error, toast.error, {
fallbackMessage: "Sign up failed",
});
@@ -95,6 +107,7 @@ const SignUp = () => {
onUsernameChange={setUsername}
onPasswordChange={setPassword}
/>
{!needsSetup && <ChallengeWidget onToken={setChallengeToken} resetKey={challengeResetKey} />}
<Button type="submit" disabled={actionBtnLoadingState.isLoading}>
{needsSetup ? t("auth.create-admin-account") : t("common.sign-up")}
{actionBtnLoadingState.isLoading && <LoaderIcon className="ml-1 h-4 w-auto animate-spin opacity-60" />}
File diff suppressed because one or more lines are too long
+59
View File
@@ -0,0 +1,59 @@
import { BinaryWriter } from "@bufbuild/protobuf/wire";
import { Code, ConnectError } from "@connectrpc/connect";
import { describe, expect, it } from "vitest";
import { ERROR_REASON_RATE_LIMITED, getErrorInfo, getErrorMessage, getRetryAfterSeconds, hasErrorReason } from "@/lib/error";
function encodeErrorInfo(reason: string, domain: string, metadata: Record<string, string>): Uint8Array {
const writer = new BinaryWriter();
writer.tag(1, 2).string(reason);
writer.tag(2, 2).string(domain);
for (const [key, value] of Object.entries(metadata)) {
const entry = new BinaryWriter();
entry.tag(1, 2).string(key);
entry.tag(2, 2).string(value);
writer.tag(3, 2).bytes(entry.finish());
}
return writer.finish();
}
function rateLimitError(retryAfterSeconds: string): ConnectError {
// Incoming details arrive as raw type-url and bytes pairs; mirror that shape.
const error = new ConnectError("too many requests, try again later", Code.ResourceExhausted);
error.details.push(
{
type: "google.rpc.ErrorInfo",
value: encodeErrorInfo(ERROR_REASON_RATE_LIMITED, "memos.api.v1", {
scope: "signin_ip",
retry_after_seconds: retryAfterSeconds,
}),
},
{ type: "google.rpc.RetryInfo", value: new Uint8Array([10, 2, 8, 42]) },
);
return error;
}
describe("ErrorInfo decoding", () => {
it("reads reason, domain and metadata from the detail", () => {
const info = getErrorInfo(rateLimitError("42"));
expect(info).toEqual({
reason: ERROR_REASON_RATE_LIMITED,
domain: "memos.api.v1",
metadata: { scope: "signin_ip", retry_after_seconds: "42" },
});
expect(hasErrorReason(rateLimitError("42"), ERROR_REASON_RATE_LIMITED)).toBe(true);
});
it("turns a rate-limit refusal into a wait message", () => {
expect(getRetryAfterSeconds(rateLimitError("42"))).toBe(42);
expect(getErrorMessage(rateLimitError("42"))).toBe("Too many requests. Try again in 42s.");
expect(getErrorMessage(rateLimitError("300"))).toBe("Too many requests. Try again in 5 min.");
});
it("leaves other errors alone", () => {
const plain = new ConnectError("nope", Code.InvalidArgument);
expect(getErrorInfo(plain)).toBeUndefined();
expect(getRetryAfterSeconds(plain)).toBeUndefined();
expect(getErrorMessage(plain)).toBe("nope");
expect(getErrorInfo(new Error("x"))).toBeUndefined();
});
});
+5
View File
@@ -12,6 +12,11 @@ vi.mock("@/contexts/AuthContext", () => ({
useAuth: () => ({ initialize: vi.fn() }),
}));
// The challenge widget reads the instance profile; no challenge is configured here.
vi.mock("@/contexts/InstanceContext", () => ({
useInstance: () => ({ profile: { challenge: undefined } }),
}));
vi.mock("@/hooks/useNavigateTo", () => ({
default: () => vi.fn(),
}));