Files
memos/store/test/user_setting_refresh_prune_test.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

59 lines
1.9 KiB
Go

package test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
storepb "github.com/usememos/memos/proto/gen/store"
)
func TestUserSettingRefreshTokensPruneExpiredOnAdd(t *testing.T) {
t.Parallel()
ctx := context.Background()
ts := NewTestingStore(ctx, t)
defer ts.Close()
user, err := createTestingHostUser(ctx, ts)
require.NoError(t, err)
expired := &storepb.RefreshTokensUserSetting_RefreshToken{TokenId: "expired", ExpiresAt: timestamppb.New(time.Now().Add(-time.Hour))}
live := &storepb.RefreshTokensUserSetting_RefreshToken{TokenId: "live", ExpiresAt: timestamppb.New(time.Now().Add(time.Hour))}
require.NoError(t, ts.AddUserRefreshToken(ctx, user.ID, expired))
require.NoError(t, ts.AddUserRefreshToken(ctx, user.ID, live))
require.NoError(t, ts.AddUserRefreshToken(ctx, user.ID, &storepb.RefreshTokensUserSetting_RefreshToken{TokenId: "newest"}))
tokens, err := ts.GetUserRefreshTokens(ctx, user.ID)
require.NoError(t, err)
ids := []string{}
for _, token := range tokens {
ids = append(ids, token.TokenId)
}
require.Equal(t, []string{"live", "newest"}, ids)
}
func TestUserSettingRemoveRefreshTokensExcept(t *testing.T) {
t.Parallel()
ctx := context.Background()
ts := NewTestingStore(ctx, t)
defer ts.Close()
user, err := createTestingHostUser(ctx, ts)
require.NoError(t, err)
for _, id := range []string{"a", "b", "c"} {
require.NoError(t, ts.AddUserRefreshToken(ctx, user.ID, &storepb.RefreshTokensUserSetting_RefreshToken{TokenId: id}))
}
require.NoError(t, ts.RemoveUserRefreshTokensExcept(ctx, user.ID, "b"))
tokens, err := ts.GetUserRefreshTokens(ctx, user.ID)
require.NoError(t, err)
require.Len(t, tokens, 1)
require.Equal(t, "b", tokens[0].TokenId)
require.NoError(t, ts.RemoveUserRefreshTokensExcept(ctx, user.ID, ""))
tokens, err = ts.GetUserRefreshTokens(ctx, user.ID)
require.NoError(t, err)
require.Empty(t, tokens)
}