mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-19 06:30:54 +08:00
fix(server): confine ask_operator image_path to the task directory
image_path was opened as a raw filesystem path, so any readable png/jpg/jpeg/webp/gif on the host could be copied into the operator transcript. The path is now resolved from the task working directory and refused when it contains .. or EvalSymlinks outside that tree.
This commit is contained in:
@@ -49,6 +49,7 @@ All notable changes to this project are documented in this file. The format is b
|
||||
|
||||
### Fixed
|
||||
|
||||
- **ask_operator image_path**: the screenshot path must resolve inside the task working directory (`<data>/tasks/<id>`). Absolute paths, `..` segments, and symlinks that leave that directory are refused, so the tool cannot copy an arbitrary server image into the operator transcript.
|
||||
- **insert_assets fail-closed**: when intercept or task allow/block rules cannot be loaded, `insert_assets` refuses the whole call instead of evaluating an empty rule set (which allowed every asset). The tool result does not include the underlying database error.
|
||||
- **BlobRange length no longer wraps int64**: `offset+length > size` overflowed when length was near MaxInt64, so the cap was skipped and `make([]byte, length)` could exhaust memory. The bound is now `length > size-offset`.
|
||||
- **PostgreSQL text[] literals escape backslashes**: `marshalStringArray` quoted double quotes but not backslashes, so a value ending in `\"` could close the current array element and inject another. It now doubles backslashes before quotes, matching `marshalJSONBArray`.
|
||||
|
||||
+47
-10
@@ -37,7 +37,7 @@ func (s *Server) toolAskOperator() actool.CoreTool {
|
||||
},
|
||||
"image_path": map[string]any{
|
||||
"type": "string",
|
||||
"description": "Optional path to a screenshot (png/jpg/jpeg/webp/gif, max 10MB), e.g. the browser_take_screenshot output. It is copied into the task uploads and shown to the operator.",
|
||||
"description": "Optional path to a screenshot (png/jpg/jpeg/webp/gif, max 10MB) under this task's working directory, e.g. the browser_take_screenshot output. It is copied into the task uploads and shown to the operator.",
|
||||
},
|
||||
},
|
||||
"required": []any{"question"},
|
||||
@@ -156,6 +156,38 @@ func askOperatorActivity(text string, att []chatAttachment, derived []string) db
|
||||
return a
|
||||
}
|
||||
|
||||
// resolveAskOperatorImage maps image_path onto a regular file inside taskDir.
|
||||
// Relative paths are resolved from the task working directory (the worker CWD).
|
||||
// ".." segments, paths that EvalSymlinks outside taskDir, and non-images are refused.
|
||||
func resolveAskOperatorImage(taskDir, src string) (string, error) {
|
||||
src = strings.TrimSpace(src)
|
||||
ext := strings.ToLower(filepath.Ext(src))
|
||||
if !askOperatorImageExt[ext] {
|
||||
return "", fmt.Errorf("image_path must be a png/jpg/jpeg/webp/gif file, got %q", filepath.Base(src))
|
||||
}
|
||||
for _, el := range strings.Split(filepath.ToSlash(src), "/") {
|
||||
if el == ".." {
|
||||
return "", fmt.Errorf("image_path must stay inside the task working directory")
|
||||
}
|
||||
}
|
||||
if !filepath.IsAbs(src) {
|
||||
src = filepath.Join(taskDir, src)
|
||||
}
|
||||
realRoot, err := filepath.EvalSymlinks(taskDir)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("image not readable")
|
||||
}
|
||||
real, err := filepath.EvalSymlinks(src)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("image not readable")
|
||||
}
|
||||
rel, err := filepath.Rel(realRoot, real)
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
return "", fmt.Errorf("image_path must stay inside the task working directory")
|
||||
}
|
||||
return real, nil
|
||||
}
|
||||
|
||||
// stageAskOperatorImage copies the referenced image into the task's uploads dir
|
||||
// (the same place chat uploads land, so the transcript attachment card resolves)
|
||||
// and returns the attachment descriptor. Empty src returns no attachment.
|
||||
@@ -164,25 +196,30 @@ func stageAskOperatorImage(dataDir, taskID, src string) ([]chatAttachment, error
|
||||
if src == "" {
|
||||
return nil, nil
|
||||
}
|
||||
ext := strings.ToLower(filepath.Ext(src))
|
||||
if !askOperatorImageExt[ext] {
|
||||
return nil, fmt.Errorf("image_path must be a png/jpg/jpeg/webp/gif file, got %q", filepath.Base(src))
|
||||
}
|
||||
st, err := os.Stat(src)
|
||||
taskDir := filepath.Join(dataDir, "tasks", taskID)
|
||||
resolved, err := resolveAskOperatorImage(taskDir, src)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("image not readable: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
st, err := os.Stat(resolved)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("image not readable")
|
||||
}
|
||||
if !st.Mode().IsRegular() {
|
||||
return nil, fmt.Errorf("image_path must be a regular file")
|
||||
}
|
||||
if st.Size() > askOperatorMaxImage {
|
||||
return nil, fmt.Errorf("image too large (%s, max 10MB)", humanBytes(st.Size()))
|
||||
}
|
||||
dir := filepath.Join(dataDir, "tasks", taskID, "uploads")
|
||||
dir := filepath.Join(taskDir, "uploads")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ext := strings.ToLower(filepath.Ext(resolved))
|
||||
name := fmt.Sprintf("ask-operator-%d%s", time.Now().Unix(), ext)
|
||||
in, err := os.Open(src)
|
||||
in, err := os.Open(resolved)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("image not readable")
|
||||
}
|
||||
defer in.Close()
|
||||
out, err := os.Create(filepath.Join(dir, name))
|
||||
|
||||
@@ -12,7 +12,11 @@ import (
|
||||
|
||||
func TestStageAskOperatorImage(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
src := filepath.Join(dir, "shot.png")
|
||||
taskDir := filepath.Join(dir, "tasks", "7")
|
||||
if err := os.MkdirAll(taskDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src := filepath.Join(taskDir, "shot.png")
|
||||
if err := os.WriteFile(src, []byte("fakepng"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -23,31 +27,67 @@ func TestStageAskOperatorImage(t *testing.T) {
|
||||
t.Fatalf("empty src: att=%v err=%v", att, err)
|
||||
}
|
||||
|
||||
// valid image -> copied under the task uploads, descriptor matches the chat format
|
||||
att, err = stageAskOperatorImage(dir, "7", src)
|
||||
// relative path from the task working directory (worker CWD)
|
||||
att, err = stageAskOperatorImage(dir, "7", "shot.png")
|
||||
if err != nil || len(att) != 1 {
|
||||
t.Fatalf("valid src: att=%v err=%v", att, err)
|
||||
}
|
||||
if !strings.HasPrefix(att[0].Path, "uploads/ask-operator-") || !strings.HasSuffix(att[0].Path, ".png") {
|
||||
t.Fatalf("unexpected attachment path %q", att[0].Path)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "tasks", "7", "uploads", att[0].Name)); err != nil {
|
||||
if _, err := os.Stat(filepath.Join(taskDir, "uploads", att[0].Name)); err != nil {
|
||||
t.Fatalf("copy missing: %v", err)
|
||||
}
|
||||
|
||||
// absolute path still accepted when it stays inside the task dir
|
||||
att, err = stageAskOperatorImage(dir, "7", src)
|
||||
if err != nil || len(att) != 1 {
|
||||
t.Fatalf("absolute inside task dir: att=%v err=%v", att, err)
|
||||
}
|
||||
|
||||
// wrong extension rejected
|
||||
bad := filepath.Join(dir, "payload.exe")
|
||||
bad := filepath.Join(taskDir, "payload.exe")
|
||||
if err := os.WriteFile(bad, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := stageAskOperatorImage(dir, "7", bad); err == nil || !strings.Contains(err.Error(), "png/jpg") {
|
||||
if _, err := stageAskOperatorImage(dir, "7", "payload.exe"); err == nil || !strings.Contains(err.Error(), "png/jpg") {
|
||||
t.Fatalf("expected extension rejection, got %v", err)
|
||||
}
|
||||
|
||||
// missing file rejected
|
||||
if _, err := stageAskOperatorImage(dir, "7", filepath.Join(dir, "nope.png")); err == nil {
|
||||
if _, err := stageAskOperatorImage(dir, "7", "nope.png"); err == nil {
|
||||
t.Fatal("expected missing-file rejection")
|
||||
}
|
||||
|
||||
outside := filepath.Join(dir, "outside.png")
|
||||
if err := os.WriteFile(outside, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := stageAskOperatorImage(dir, "7", filepath.Join("..", "..", "outside.png")); err == nil {
|
||||
t.Fatal("expected .. rejection")
|
||||
}
|
||||
if _, err := stageAskOperatorImage(dir, "7", outside); err == nil {
|
||||
t.Fatal("expected outside-path rejection")
|
||||
}
|
||||
|
||||
otherDir := filepath.Join(dir, "tasks", "8")
|
||||
if err := os.MkdirAll(otherDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := filepath.Join(otherDir, "shot.png")
|
||||
if err := os.WriteFile(other, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := stageAskOperatorImage(dir, "7", other); err == nil {
|
||||
t.Fatal("expected other-task rejection")
|
||||
}
|
||||
|
||||
link := filepath.Join(taskDir, "link.png")
|
||||
if err := os.Symlink(outside, link); err != nil {
|
||||
t.Logf("symlink test skipped: %v", err)
|
||||
} else if _, err := stageAskOperatorImage(dir, "7", "link.png"); err == nil {
|
||||
t.Fatal("expected symlink-out rejection")
|
||||
}
|
||||
}
|
||||
|
||||
// The emitted activity must carry the {text, attachments} Detail shape the
|
||||
|
||||
Reference in New Issue
Block a user