fix: distinguish outside-workspace errors from not-found in fs-safe#29715
fix: distinguish outside-workspace errors from not-found in fs-safe#29715obviyus merged 4 commits intoopenclaw:mainfrom
Conversation
Greptile SummaryAdded new Key changes:
Additional files with SafeOpenError handling (not updated, use generic error handling):
Confidence Score: 5/5
Last reviewed commit: a6f9988 |
Additional Comments (1)
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/media/store.ts
Line: 228-249
Comment:
add case for `"outside-workspace"` to provide more specific error message
```suggestion
function toSaveMediaSourceError(err: SafeOpenError): SaveMediaSourceError {
switch (err.code) {
case "symlink":
return new SaveMediaSourceError("invalid-path", "Media path must not be a symlink", {
cause: err,
});
case "not-file":
return new SaveMediaSourceError("not-file", "Media path is not a file", { cause: err });
case "path-mismatch":
return new SaveMediaSourceError("path-mismatch", "Media path changed during read", {
cause: err,
});
case "too-large":
return new SaveMediaSourceError("too-large", "Media exceeds 5MB limit", { cause: err });
case "not-found":
return new SaveMediaSourceError("not-found", "Media path does not exist", { cause: err });
case "outside-workspace":
return new SaveMediaSourceError("invalid-path", "Media path is outside workspace root", {
cause: err,
});
case "invalid-path":
default:
return new SaveMediaSourceError("invalid-path", "Media path is not safe to read", {
cause: err,
});
}
}
```
How can I resolve this? If you propose a fix, please make it concise. |
|
Addressed Greptile's suggestion: added |
|
Hi. Thank you for maintaining openclaw. This PR is ready for review. @obviyus |
When editing a file outside the workspace root, SafeOpenError previously
used the "invalid-path" code with the message "path escapes root". This
was indistinguishable from other invalid-path errors (hardlinks, symlinks,
non-files) and consumers often fell back to a generic "not found" message,
which was misleading.
Add a new "outside-workspace" error code with the message "file is outside
workspace root" so consumers can surface a clear, accurate error message.
- fs-safe.ts: add "outside-workspace" to SafeOpenErrorCode, use it for
all path-escapes-root checks in openFileWithinRoot/writeFileWithinRoot
- pi-tools.read.ts: map "outside-workspace" to EACCES instead of rethrowing
- browser/paths.ts: return specific "File is outside {scopeLabel}" message
- media/server.ts: return 400 with descriptive message for outside-workspace
- fs-safe.test.ts: update traversal test expectations
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Address Greptile review: add explicit "outside-workspace" case to toSaveMediaSourceError so it returns "Media path is outside workspace root" instead of the generic "Media path is not safe to read". Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
8048e28 to
f70ee96
Compare
Summary
Describe the problem and fix in 2–5 bullets:
SafeOpenErrorthrows with code"invalid-path"and message"path escapes root". Consumers cannot distinguish this from other invalid-path cases (hardlinks, symlinks, non-files) and often fall back to a misleading"File not found"message."outside-workspace"error code toSafeOpenErrorCode. All path-escapes-root checks inopenFileWithinRoot/writeFileWithinRootnow use this code. Consumers (pi-tools.read.ts,browser/paths.ts,media/server.ts) handle it with specific, accurate messages."invalid-path"uses (hardlinks, symlinks, non-files) remain unchanged.readLocalFileSafelyis unaffected (no root boundary check).Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
User-visible / Behavior Changes
"file is outside workspace root"or"File is outside {scopeLabel}"instead of"File not found"or"Invalid path"when targeting a file outside the workspace.400 "file is outside workspace root"instead of400 "invalid path"for outside-workspace requests.EACCESinstead of rethrowing a rawSafeOpenError.Security Impact (required)
NoNoNoNoNoRepro + Verification
Environment
Steps
openFileWithinRootwith a relative path that escapes the root (e.g.../outside/file.txt)Expected
SafeOpenErrorwith code"outside-workspace"and message"file is outside workspace root"Actual
SafeOpenErrorwith code"invalid-path"and message"path escapes root"Evidence
pnpm vitest run src/infra/fs-safe.test.ts— all 11 tests pass, including updated traversal expectations ("outside-workspace").Human Verification (required)
What you personally verified (not just CI), and how:
pnpm build,pnpm tsgo,pnpm lint,pnpm vitest run src/infra/fs-safe.test.tsall passCompatibility / Migration
No— code that matcheserr.code === "invalid-path"for path-escapes-root will no longer trigger; they need to also check"outside-workspace".NoNo— consumers in this repo are updated in this PR. External consumers of the plugin-sdkSafeOpenErrorCodetype will get a compile error if they have exhaustive switches, which is the desired behavior.Failure Recovery (if this breaks)
"outside-workspace"back to"invalid-path"with"path escapes root"messagesrc/infra/fs-safe.ts,src/agents/pi-tools.read.ts,src/browser/paths.ts,src/media/server.ts,src/infra/fs-safe.test.ts"invalid-path"for workspace boundary errors would silently miss the new code and fall through to a generic handlerRisks and Mitigations
SafeOpenErrorCodeswitches get a compile error.d.tsis auto-generated from source.