mirror of
https://github.com/usememos/memos.git
synced 2026-11-21 19:39:12 +01:00
471745b431
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.
120 lines
3.6 KiB
Go
120 lines
3.6 KiB
Go
// Package sqlite provides SQLite driver implementation with custom functions.
|
|
// Custom functions are registered globally on first use to extend SQLite's
|
|
// limited ASCII-only text operations with proper Unicode support.
|
|
package sqlite
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"errors"
|
|
"regexp"
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"golang.org/x/text/cases"
|
|
msqlite "modernc.org/sqlite"
|
|
)
|
|
|
|
var (
|
|
registerUnicodeLowerOnce sync.Once
|
|
registerUnicodeLowerErr error
|
|
// unicodeFold provides Unicode case folding for case-insensitive comparisons.
|
|
// It's safe to use concurrently and reused across all function calls.
|
|
unicodeFold = cases.Fold()
|
|
)
|
|
|
|
// ensureUnicodeLowerRegistered registers the memos_unicode_lower custom function
|
|
// with SQLite. This function provides proper Unicode case folding for case-insensitive
|
|
// text comparisons, overcoming modernc.org/sqlite's lack of ICU extension.
|
|
//
|
|
// The function is registered once globally and is safe to call multiple times.
|
|
func ensureUnicodeLowerRegistered() error {
|
|
registerUnicodeLowerOnce.Do(func() {
|
|
registerUnicodeLowerErr = msqlite.RegisterScalarFunction("memos_unicode_lower", 1, func(_ *msqlite.FunctionContext, args []driver.Value) (driver.Value, error) {
|
|
if len(args) == 0 || args[0] == nil {
|
|
return nil, nil
|
|
}
|
|
switch v := args[0].(type) {
|
|
case string:
|
|
return unicodeFold.String(v), nil
|
|
case []byte:
|
|
return unicodeFold.String(string(v)), nil
|
|
default:
|
|
return v, nil
|
|
}
|
|
})
|
|
})
|
|
return registerUnicodeLowerErr
|
|
}
|
|
|
|
var (
|
|
registerRegexpOnce sync.Once
|
|
registerRegexpErr error
|
|
// regexpCache memoizes compiled patterns; keys are pattern strings.
|
|
regexpCache sync.Map
|
|
// regexpCacheSize counts cached patterns so a stream of distinct
|
|
// attacker-chosen patterns cannot grow the cache without bound.
|
|
regexpCacheSize atomic.Int64
|
|
)
|
|
|
|
// maxRegexpCacheEntries caps the compiled-pattern cache. When it is full the
|
|
// cache is cleared rather than evicted entry by entry; recompiling a pattern
|
|
// is cheap next to letting the cache grow unbounded.
|
|
const maxRegexpCacheEntries = 256
|
|
|
|
// ensureRegexpRegistered registers a Go-backed `regexp(pattern, value)` scalar
|
|
// function so SQLite's `value REGEXP pattern` operator works (modernc.org/sqlite
|
|
// has no built-in implementation). Patterns use Go's RE2 syntax. Registered once
|
|
// globally; safe to call multiple times.
|
|
func ensureRegexpRegistered() error {
|
|
registerRegexpOnce.Do(func() {
|
|
registerRegexpErr = msqlite.RegisterScalarFunction("regexp", 2, func(_ *msqlite.FunctionContext, args []driver.Value) (driver.Value, error) {
|
|
if len(args) != 2 || args[0] == nil || args[1] == nil {
|
|
return int64(0), nil
|
|
}
|
|
pattern, ok := args[0].(string)
|
|
if !ok {
|
|
return nil, errors.New("regexp pattern must be a string")
|
|
}
|
|
var value string
|
|
switch v := args[1].(type) {
|
|
case string:
|
|
value = v
|
|
case []byte:
|
|
value = string(v)
|
|
default:
|
|
return int64(0), nil
|
|
}
|
|
re, err := compileRegexp(pattern)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if re.MatchString(value) {
|
|
return int64(1), nil
|
|
}
|
|
return int64(0), nil
|
|
})
|
|
})
|
|
return registerRegexpErr
|
|
}
|
|
|
|
// compileRegexp compiles and caches a RE2 pattern.
|
|
func compileRegexp(pattern string) (*regexp.Regexp, error) {
|
|
if cached, ok := regexpCache.Load(pattern); ok {
|
|
if re, ok := cached.(*regexp.Regexp); ok {
|
|
return re, nil
|
|
}
|
|
}
|
|
re, err := regexp.Compile(pattern)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if regexpCacheSize.Load() >= maxRegexpCacheEntries {
|
|
regexpCache.Clear()
|
|
regexpCacheSize.Store(0)
|
|
}
|
|
if _, loaded := regexpCache.LoadOrStore(pattern, re); !loaded {
|
|
regexpCacheSize.Add(1)
|
|
}
|
|
return re, nil
|
|
}
|