Skip to content

Commit ff8e64c

Browse files
security fix
1 parent 707b728 commit ff8e64c

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

stores/aws/store.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,16 @@ func (s *s3Store) Create(ctx context.Context, document *core.Document) (string,
7979
}
8080

8181
// CanvasStore implementation for user-owned canvases
82-
func (s *s3Store) getCanvasKey(userID, canvasID string) string {
83-
return path.Join(userID, canvasID)
82+
func (s *s3Store) getCanvasKey(userID, canvasID string) (string, error) {
83+
// Sanitize canvasID to prevent path traversal attacks.
84+
// It should be a simple name, not a path.
85+
if path.Base(canvasID) != canvasID {
86+
return "", fmt.Errorf("invalid canvas id: must not be a path")
87+
}
88+
if canvasID == "" || canvasID == "." || canvasID == ".." {
89+
return "", fmt.Errorf("invalid canvas id: must not be empty or a dot directory")
90+
}
91+
return path.Join(userID, canvasID), nil
8492
}
8593

8694
func (s *s3Store) List(ctx context.Context, userID string) ([]*core.Canvas, error) {
@@ -125,7 +133,10 @@ func (s *s3Store) List(ctx context.Context, userID string) ([]*core.Canvas, erro
125133
}
126134

127135
func (s *s3Store) Get(ctx context.Context, userID, id string) (*core.Canvas, error) {
128-
key := s.getCanvasKey(userID, id)
136+
key, err := s.getCanvasKey(userID, id)
137+
if err != nil {
138+
return nil, err
139+
}
129140
resp, err := s.s3Client.GetObject(ctx, &s3.GetObjectInput{
130141
Bucket: aws.String(s.bucket),
131142
Key: aws.String(key),
@@ -154,7 +165,10 @@ func (s *s3Store) Get(ctx context.Context, userID, id string) (*core.Canvas, err
154165
}
155166

156167
func (s *s3Store) Save(ctx context.Context, canvas *core.Canvas) error {
157-
key := s.getCanvasKey(canvas.UserID, canvas.ID)
168+
key, err := s.getCanvasKey(canvas.UserID, canvas.ID)
169+
if err != nil {
170+
return err
171+
}
158172

159173
// Preserve CreatedAt on update
160174
if canvas.CreatedAt.IsZero() {
@@ -184,8 +198,11 @@ func (s *s3Store) Save(ctx context.Context, canvas *core.Canvas) error {
184198
}
185199

186200
func (s *s3Store) Delete(ctx context.Context, userID, id string) error {
187-
key := s.getCanvasKey(userID, id)
188-
_, err := s.s3Client.DeleteObject(ctx, &s3.DeleteObjectInput{
201+
key, err := s.getCanvasKey(userID, id)
202+
if err != nil {
203+
return err
204+
}
205+
_, err = s.s3Client.DeleteObject(ctx, &s3.DeleteObjectInput{
189206
Bucket: aws.String(s.bucket),
190207
Key: aws.String(key),
191208
})

stores/filesystem/store.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"log"
1010
"os"
1111
"path/filepath"
12+
"strings"
1213
"time"
1314

1415
"github.com/oklog/ulid/v2"
@@ -127,7 +128,22 @@ func (s *fsStore) Get(ctx context.Context, userID, id string) (*core.Canvas, err
127128
filePath := filepath.Join(userPath, id)
128129
log := logrus.WithFields(logrus.Fields{"user_id": userID, "canvas_id": id, "path": filePath})
129130

130-
data, err := os.ReadFile(filePath)
131+
// 关键修复:验证路径合法性
132+
absUserPath, err := filepath.Abs(userPath)
133+
if err != nil {
134+
return nil, err // or handle error appropriately
135+
}
136+
absFilePath, err := filepath.Abs(filePath)
137+
if err != nil {
138+
return nil, err // or handle error appropriately
139+
}
140+
141+
if !strings.HasPrefix(absFilePath, absUserPath) {
142+
return nil, fmt.Errorf("invalid path: access denied")
143+
}
144+
// 修复结束
145+
146+
data, err := os.ReadFile(absFilePath) // 使用清理过的路径
131147
if err != nil {
132148
if os.IsNotExist(err) {
133149
log.Warn("Canvas file not found")

0 commit comments

Comments
 (0)