Files
memos/server/api/v1/ratelimit.go
T
bluedbird 471745b431 fix: harden attachment, auth, and import paths for public deployment
Bound work an anonymous or low-privilege caller can force:

- Bound thumbnail decoding by declared pixel count before decoding and
  remember images that cannot be thumbnailed (over the bound or not
  decodable), so a small image declaring billions of pixels is served
  as-is instead of decoded on every request. Transient storage read
  errors are not remembered. The bound lives in internal/imagelimit and
  is shared with EXIF stripping.
- Cap request bodies at 16 MiB for procedures that carry no file bytes;
  only inline attachment creation, transcription, and chunked uploads
  keep their larger caps.
- Digest over-long submitted usernames before using them as sign-in
  limiter keys, add header and idle timeouts to the HTTP server, limit
  filter expression size, and cap the SQLite regexp cache.

Close correctness gaps:

- Delete the thumbnail the file server actually writes (.v2.jpeg), its
  failure marker, and the legacy name when an attachment is removed.
- Decide EXIF stripping from the sniffed content as well as the declared
  type, so a mislabeled JPEG keeps no metadata.
- Revoke every other session when a password changes; an admin reset
  revokes all of the target's sessions. Prune expired refresh tokens.
- Enforce the 2 MiB avatar limit the web client already applies.
- Keep S3 object keys unique when the path template has no {uuid}.
- Reject negative attachment page tokens.
- Charge memo archive imports against the write and upload budgets,
  charge repeated validate-only plans, discard only attachments an
  import actually stored, and refuse archives that declare far more
  bytes than they hold.
- Load database-stored attachment bytes only after authorization.
- Remove the unused, unguarded linkmeta.GetImage fetcher.

Frontend: send no referrer when loading external images in memos and
link previews, and drop picture/source from the sanitizer so every
remote image goes through the https-only src rule.

Dependencies: grpc 1.83.1 and x/crypto 0.56.0 for reported advisories.
2026-09-17 08:47:44 +08:00

242 lines
8.4 KiB
Go

package v1
import (
"context"
"crypto/sha256"
"encoding/hex"
"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/identifier"
"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))
}
// accountKey is the limiter key for a submitted account name. Names longer
// than any valid username are replaced by a digest, so the limiter stores a
// bounded key for an attacker-chosen string while still counting each
// distinct submission separately.
func accountKey(username string) string {
if len(username) <= identifier.MaxUsernameLength {
return username
}
sum := sha256.Sum256([]byte(username))
return "sha256:" + hex.EncodeToString(sum[:])
}
// 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: accountKey(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
}