Skip to content

Commit caabd39

Browse files
authored
fix(api): detect invalid UTF-8 in memo content before gRPC marshal
Add defensive UTF-8 validation in convertMemoFromStore to prevent gRPC marshal failures when memo content or derived snippet contains invalid UTF-8 data. This fails fast with a clear INTERNAL error instead of crashing the Listen response. Signed-off-by: Manak Raj <[email protected]>
1 parent f66c750 commit caabd39

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

server/router/api/v1/memo_service_converter.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import (
44
"context"
55
"fmt"
66
"time"
7+
"unicode/utf8"
78

89
"github.com/pkg/errors"
910
"google.golang.org/protobuf/types/known/timestamppb"
11+
"google.golang.org/grpc/codes"
12+
"google.golang.org/grpc/status"
1013

1114
v1pb "github.com/usememos/memos/proto/gen/api/v1"
1215
storepb "github.com/usememos/memos/proto/gen/store"
@@ -25,6 +28,13 @@ func (s *APIV1Service) convertMemoFromStore(ctx context.Context, memo *store.Mem
2528

2629
name := fmt.Sprintf("%s%s", MemoNamePrefix, memo.UID)
2730
memoMessage := &v1pb.Memo{
31+
if !utf8.ValidString(memo.Content) {
32+
return nil, status.Errorf(
33+
codes.Internal,
34+
"memo %s contains invalid UTF-8 content",
35+
memo.UID,
36+
)
37+
}
2838
Name: name,
2939
State: convertStateFromStore(memo.RowStatus),
3040
Creator: fmt.Sprintf("%s%d", UserNamePrefix, memo.CreatorID),
@@ -67,6 +77,13 @@ func (s *APIV1Service) convertMemoFromStore(ctx context.Context, memo *store.Mem
6777
}
6878

6979
snippet, err := s.getMemoContentSnippet(memo.Content)
80+
if !utf8.ValidString(snippet) {
81+
return nil, status.Errorf(
82+
codes.Internal,
83+
"memo %s contains invalid UTF-8 snippet",
84+
memo.UID,
85+
)
86+
}
7087
if err != nil {
7188
return nil, errors.Wrap(err, "failed to get memo content snippet")
7289
}

0 commit comments

Comments
 (0)