Repository navigation
feat(storage): filesystem StorageProvider - #7
Conversation
Implement FilesystemStorageProvider over Bun's file APIs, satisfying the StorageProvider contract (store/delete/exists/stream/copy/move/getPublicUrl). Keys are confined to the storage root via a relative()-based traversal check (correct even when root is "/"); the guarantee is scoped to key-based traversal (keys are app-generated content paths, root is app-owned). getPublicUrl returns null — a private FS backend has no inherent URL; the API serves bytes via its own route. The StorageProvider interface moves to provider.ts to avoid an index<->impl import cycle. Tests: round-trip store/stream/exists/delete/copy/move + path-traversal rejection on every key-taking path (source + destination, read + delete), and the source survives a rejected move. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughAdds a standalone ChangesFilesystem StorageProvider
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
packages/storage/test/filesystem.test.ts (1)
78-103: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick winCover the remaining root-escape cases in the security matrix.
Line 100 only checks
stream()anddelete(), butexists()also resolves throughresolveKey, andpackages/storage/src/filesystem.ts:30-58has a separate guard for the storage root itself. A regression that stops rejectingexists("../x")or""/"."keys would still pass this suite today.Proposed test additions
it("rejects traversal on read and delete paths", async () => { + await expect(storage.exists("../escape.txt")).rejects.toThrow(/escape/); await expect(storage.stream("../escape.txt")).rejects.toThrow(/escape/); await expect(storage.delete("../escape.txt")).rejects.toThrow(/escape/); }); + + it("rejects the storage root itself as a key", async () => { + for (const key of ["", "."]) { + await expect(storage.store(key, bytes("x"))).rejects.toThrow(/escape/); + await expect(storage.exists(key)).rejects.toThrow(/escape/); + } + });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/storage/test/filesystem.test.ts` around lines 78 - 103, Extend the path-traversal safety test matrix in filesystem.test.ts to cover the remaining root-escape cases that also flow through resolveKey and the filesystem root guard in FilesystemStorage. Add assertions around exists() rejecting traversal inputs like "../x", and include empty or "." keys so the suite fails if the root-path validation regresses. Keep the checks alongside the existing storage.store/copy/move/stream/delete cases so all public methods in FilesystemStorage are covered consistently.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/storage/src/filesystem.ts`:
- Around line 41-45: The resolveKey helper in filesystem.ts is still allowing
absolute keys because it only validates the path after calling resolve/relative.
Update resolveKey to reject absolute inputs up front before any path
normalization, then keep the existing escape checks for the resolved relative
path. Use the resolveKey function and its root/key handling to ensure absolute
keys are rejected consistently, including when root is "/".
- Around line 79-88: Validate the source key before mutating the destination
side in `copy` and `move` within `filesystem.ts`. Resolve and validate `from`
first using `resolveKey` (and fail fast if it escapes) before calling `mkdir` on
`to`, so rejected operations do not leave empty directories behind under the
storage root. Keep the destination directory creation and file transfer only
after `from` has been successfully validated.
---
Nitpick comments:
In `@packages/storage/test/filesystem.test.ts`:
- Around line 78-103: Extend the path-traversal safety test matrix in
filesystem.test.ts to cover the remaining root-escape cases that also flow
through resolveKey and the filesystem root guard in FilesystemStorage. Add
assertions around exists() rejecting traversal inputs like "../x", and include
empty or "." keys so the suite fails if the root-path validation regresses. Keep
the checks alongside the existing storage.store/copy/move/stream/delete cases so
all public methods in FilesystemStorage are covered consistently.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 945e2e32-49c4-4dd1-86a6-821c32a7ea3d
📒 Files selected for processing (4)
packages/storage/src/filesystem.tspackages/storage/src/index.tspackages/storage/src/provider.tspackages/storage/test/filesystem.test.ts
…e before mkdir - resolveKey rejects isAbsolute(key) before relativizing, so an absolute key that points inside root (always when root === "/") no longer slips past the check. - copy/move resolve the source key before creating destination dirs — a rejected escaping source no longer leaves empty directories behind. - tests: absolute-key-inside-root rejection; no destination dirs on invalid source. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
@coderabbitai review |
✅ Action performedReview finished.
|
FilesystemStorageProvider (storage thrust, part 1)
First concrete
StorageProvider— the foundation the upload pipeline (next PR) stores bytes through. Per the Storage Rule, Core never touches the FS directly; it depends only on the interface.What
createFilesystemStorageProvider({ root })over Bun's native file APIs, implementing the full contract:store / delete / exists / stream / copy / move / getPublicUrl.rootand is validated withrelative()(correct even whenrootis/, wherestartsWith(root + sep)breaks). Rejects../traversal, absolute keys, and the root itself — on every key-taking path (source + destination of copy/move, plus read/delete).assets/ab/cd/<sha256>, root is app-owned). Attacker-planted symlinks inside root are out of scope (they'd need FS write access already); realpath hardening is noted for any future untrusted-root use.getPublicUrl→null: a private FS backend has no inherent URL; the API serves bytes via its own route (next PR).provider.tsto avoid anindex ↔ implimport cycle.Tests (13)
Round-trip
store/stream/exists/delete/copy/move(Uint8Array + ReadableStream),getPublicUrlnull, and path-traversal rejection across source/destination/read/delete — including that a rejected move leaves the source intact.Validation
typecheck 0 · 78 tests total · boundaries 0 · pre-reviewed with the CodeRabbit CLI (relative()-check, expanded traversal tests, scoped docs all applied).
🤖 Generated with Claude Code
Summary by CodeRabbit