Files

302 lines
11 KiB
Go

package api
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/core/usagelimit"
"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/Memo"), so reasons and resources share one name.
errorInfoDomain = "memos.api"
// reasonRateLimited marks a refusal that recovers on its own after the
// advertised delay.
reasonRateLimited = "RATE_LIMITED"
// reasonQuotaExceeded marks a refusal by a usage limit. It does not recover
// with time: the user must free space or an admin must raise the limit.
reasonQuotaExceeded = "QUOTA_EXCEEDED"
// reasonChallengeRequired marks a request that must carry a valid challenge token.
reasonChallengeRequired = "CHALLENGE_REQUIRED"
rateLimitMessage = "too many requests, try again later"
quotaExceededMessage = "quota exceeded"
// 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 := max(int(math.Ceil(decision.RetryAfter.Seconds())), 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()
}
// newQuotaExceededError builds the refusal for an action that would exceed a
// usage limit: RESOURCE_EXHAUSTED with an ErrorInfo carrying the metric, the
// limit and the usage, and a QuotaFailure naming the user. It carries no
// RetryInfo, because waiting does not help.
func newQuotaExceededError(username string, refusal *usagelimit.Refusal) error {
st := status.New(codes.ResourceExhausted, quotaExceededMessage)
detailed, err := st.WithDetails(
&errdetails.ErrorInfo{
Reason: reasonQuotaExceeded,
Domain: errorInfoDomain,
Metadata: map[string]string{
"metric": string(refusal.Metric),
"limit": strconv.FormatInt(refusal.Limit, 10),
"usage": strconv.FormatInt(refusal.Usage, 10),
},
},
&errdetails.QuotaFailure{
Violations: []*errdetails.QuotaFailure_Violation{{
Subject: BuildUserName(username),
Description: quotaExceededDescription(refusal),
}},
},
)
if err != nil {
return st.Err()
}
return detailed.Err()
}
func quotaExceededDescription(refusal *usagelimit.Refusal) string {
switch refusal.Metric {
case usagelimit.MetricMemoCount:
return "You have reached the limit of " + strconv.FormatInt(refusal.Limit, 10) + " memos."
case usagelimit.MetricStorageBytes:
return "This upload would exceed your storage limit of " + strconv.FormatInt(refusal.Limit, 10) + " bytes."
case usagelimit.MetricUploadBytes:
return "The file is larger than the limit of " + strconv.FormatInt(refusal.Limit, 10) + " bytes per file."
case usagelimit.MetricMemoContentBytes:
return "The content is larger than the limit of " + strconv.FormatInt(refusal.Limit, 10) + " bytes per memo."
default:
return "A usage limit was reached."
}
}
// isQuotaExceeded reports whether err is a usage limit refusal.
func isQuotaExceeded(err error) bool {
st, ok := status.FromError(err)
if !ok || st.Code() != codes.ResourceExhausted {
return false
}
for _, detail := range st.Details() {
if info, ok := detail.(*errdetails.ErrorInfo); ok && info.GetReason() == reasonQuotaExceeded {
return true
}
}
return false
}
// 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 *APIService) 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 *APIService) 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 *APIService) 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 *APIService) 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 *APIService) 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 *APIService
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 *APIService) 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, attempt.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
}