Files
memos/server/server.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

201 lines
5.8 KiB
Go

package server
import (
"context"
"fmt"
"log/slog"
"net"
"net/http"
"os"
"time"
"uuid"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
"github.com/pkg/errors"
"github.com/usememos/memos/internal/clientip"
"github.com/usememos/memos/internal/profile"
storepb "github.com/usememos/memos/proto/gen/store"
apiv1 "github.com/usememos/memos/server/api/v1"
"github.com/usememos/memos/server/fileserver"
"github.com/usememos/memos/server/frontend"
"github.com/usememos/memos/server/mcp"
"github.com/usememos/memos/store"
)
const (
shutdownTimeout = 10 * time.Second
// readHeaderTimeout bounds how long a client may take to send request
// headers, so idle or slow connections cannot pin a worker forever. Bodies
// and responses are not bounded here: uploads and SSE streams are
// legitimately long, and the request context still ends on disconnect.
readHeaderTimeout = 15 * time.Second
// idleTimeout closes keep-alive connections that send nothing.
idleTimeout = 2 * time.Minute
)
type Server struct {
Secret string
Profile *profile.Profile
Store *store.Store
echoServer *echo.Echo
httpServer *http.Server
apiV1Service *apiv1.APIV1Service
}
func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store) (*Server, error) {
s := &Server{
Store: store,
Profile: profile,
}
echoServer := echo.New()
echoServer.Use(middleware.Recover())
echoServer.Use(newCORSMiddleware(profile))
// Resolve the client address once per request, before anything that keys
// on it: rate limits, session records, and the file server.
clientIPResolver, err := clientip.ParseTrustedProxies(profile.TrustedProxies)
if err != nil {
return nil, errors.Wrap(err, "invalid trusted proxies")
}
echoServer.Use(clientip.Middleware(clientIPResolver))
s.echoServer = echoServer
instanceBasicSetting, err := s.getOrUpsertInstanceBasicSetting(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to get instance basic setting")
}
secret := "usememos"
if !profile.Demo {
secret = instanceBasicSetting.SecretKey
}
s.Secret = secret
// Register healthz endpoint.
echoServer.GET("/healthz", func(c *echo.Context) error {
return c.String(http.StatusOK, "Service ready.")
})
// Serve frontend static files.
frontend.NewFrontendService(profile, store).Serve(ctx, echoServer)
apiV1Service := apiv1.NewAPIV1Service(s.Secret, profile, store)
s.apiV1Service = apiV1Service
// Register HTTP file server routes BEFORE gRPC-Gateway to ensure proper range request handling for Safari.
// This uses native HTTP serving (http.ServeContent) instead of gRPC for video/audio files.
fileServerService := fileserver.NewFileServerService(s.Profile, s.Store, s.Secret)
fileServerService.RegisterRoutes(echoServer)
// Register gRPC gateway as api v1 (includes SSE endpoint on CORS-enabled group).
if err := apiV1Service.RegisterGateway(ctx, echoServer); err != nil {
return nil, errors.Wrap(err, "failed to register gRPC gateway")
}
mcpService, err := mcp.NewMCPService(profile, echoServer)
if err != nil {
return nil, errors.Wrap(err, "failed to create MCP service")
}
mcpService.RegisterRoutes(echoServer)
return s, nil
}
func (s *Server) Start() error {
var address, network string
if len(s.Profile.UNIXSock) == 0 {
address = fmt.Sprintf("%s:%d", s.Profile.Addr, s.Profile.Port)
network = "tcp"
} else {
address = s.Profile.UNIXSock
network = "unix"
}
listener, err := net.Listen(network, address)
if err != nil {
return errors.Wrap(err, "failed to listen")
}
if network == "unix" {
if err := os.Chmod(address, 0660); err != nil {
_ = listener.Close()
return errors.Wrap(err, "failed to chmod socket")
}
}
// Start Echo server directly (no cmux needed - all traffic is HTTP).
s.httpServer = &http.Server{
Handler: s.echoServer,
ReadHeaderTimeout: readHeaderTimeout,
IdleTimeout: idleTimeout,
}
go func() {
if err := s.httpServer.Serve(listener); err != nil && err != http.ErrServerClosed {
slog.Error("failed to start echo server", "error", err)
}
}()
return nil
}
func (s *Server) Shutdown(ctx context.Context) {
ctx, cancel := context.WithTimeout(ctx, shutdownTimeout)
defer cancel()
slog.Info("server shutting down")
s.closeLongLivedConnections()
s.shutdownHTTPServer(ctx)
s.apiV1Service.CloseUploads()
// Close database connection.
if err := s.Store.Close(); err != nil {
slog.Error("failed to close database", slog.String("error", err.Error()))
}
slog.Info("memos stopped properly")
}
func (s *Server) closeLongLivedConnections() {
// Long-lived SSE requests do not finish on their own during http.Server.Shutdown.
s.apiV1Service.SSEHub.Close()
}
func (s *Server) shutdownHTTPServer(ctx context.Context) {
if s.httpServer == nil {
return
}
if err := s.httpServer.Shutdown(ctx); err != nil {
slog.Error("failed to shutdown server", slog.String("error", err.Error()))
if closeErr := s.httpServer.Close(); closeErr != nil && closeErr != http.ErrServerClosed {
slog.Error("failed to close server", slog.String("error", closeErr.Error()))
}
}
}
func (s *Server) getOrUpsertInstanceBasicSetting(ctx context.Context) (*storepb.InstanceBasicSetting, error) {
instanceBasicSetting, err := s.Store.GetInstanceBasicSetting(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to get instance basic setting")
}
modified := false
if instanceBasicSetting.SecretKey == "" {
instanceBasicSetting.SecretKey = uuid.NewV4().String()
modified = true
}
if modified {
instanceSetting, err := s.Store.UpsertInstanceSetting(ctx, &storepb.InstanceSetting{
Key: storepb.InstanceSettingKey_BASIC,
Value: &storepb.InstanceSetting_BasicSetting{BasicSetting: instanceBasicSetting},
})
if err != nil {
return nil, errors.Wrap(err, "failed to upsert instance setting")
}
instanceBasicSetting = instanceSetting.GetBasicSetting()
}
return instanceBasicSetting, nil
}