mirror of
https://github.com/usememos/memos.git
synced 2026-12-04 06:34:22 +01:00
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.
101 lines
3.6 KiB
Go
101 lines
3.6 KiB
Go
package v1
|
|
|
|
import (
|
|
"strings"
|
|
"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/identifier"
|
|
"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))
|
|
}
|
|
|
|
func TestAccountKeyBoundsLength(t *testing.T) {
|
|
require.Equal(t, "alice", accountKey("alice"))
|
|
long := strings.Repeat("a", identifier.MaxUsernameLength+1)
|
|
key := accountKey(long)
|
|
require.True(t, strings.HasPrefix(key, "sha256:"))
|
|
require.Len(t, key, len("sha256:")+64)
|
|
// Distinct submissions stay distinct; the same submission maps to one key.
|
|
require.NotEqual(t, key, accountKey(long+"b"))
|
|
require.Equal(t, key, accountKey(long))
|
|
}
|