-
Notifications
You must be signed in to change notification settings - Fork 310
fix(security): close path traversal and shell injection paths #508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
.changeset/olive-pots-repeat.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --- | ||
| '@srcbook/api': patch | ||
| --- | ||
|
|
||
| Close path traversal and shell injection paths. | ||
|
|
||
| `DELETE /api/srcbooks/:id` could delete directories outside the srcbooks directory, and | ||
| `POST /api/file` could read any file on disk. Both are now confined to `SRCBOOKS_DIR`. | ||
|
|
||
| Cell filenames are validated when a `.src.md` is decoded rather than only on rename, so an | ||
| imported notebook can no longer introduce a filename that reaches a shell. Prettier and | ||
| depcheck are invoked with argument arrays instead of interpolated command strings. | ||
|
|
||
| `POST /api/settings` now validates its body against an allowlist. It previously wrote any | ||
| column of the config row, including `aiBaseUrl`, which controls where AI requests and the | ||
| user's API key are sent. | ||
|
|
||
| Also fixes formatting reporting failure when prettier writes a warning to stderr but exits | ||
| successfully. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
packages/api/path-utils.mts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import Path from 'node:path'; | ||
|
|
||
| /** | ||
| * Resolve `segments` against `root` and assert the result stays inside it. | ||
| * | ||
| * User-supplied path segments reach the filesystem from several directions — | ||
| * srcbook ids in URLs, cell filenames decoded from a `.src.md`, file paths that | ||
| * tsserver reports back to the client — and any of them can contain `..`. Express | ||
| * URL-decodes route params, so `..%2F..%2F` arrives as a real traversal. | ||
| * | ||
| * Returns the resolved path, or null when it would escape. | ||
| */ | ||
| export function containedPath(root: string, ...segments: string[]): string | null { | ||
| const resolvedRoot = Path.resolve(root); | ||
| const resolved = Path.resolve(resolvedRoot, ...segments); | ||
|
|
||
| if (resolved === resolvedRoot) { | ||
| return resolved; | ||
| } | ||
|
|
||
| // The separator matters: without it, `/foo/barbaz` looks like it is inside `/foo/bar`. | ||
| if (!resolved.startsWith(resolvedRoot + Path.sep)) { | ||
| return null; | ||
| } | ||
|
|
||
| return resolved; | ||
| } | ||
|
|
||
| /** | ||
| * Like `containedPath`, but throws instead of returning null. | ||
| * | ||
| * Use at trust boundaries where there is no sensible way to continue. | ||
| */ | ||
| export function requireContainedPath(root: string, ...segments: string[]): string { | ||
| const path = containedPath(root, ...segments); | ||
|
|
||
| if (path === null) { | ||
| throw new Error(`Refusing to operate on a path outside of ${root}`); | ||
| } | ||
|
|
||
| return path; | ||
| } |
31 changes: 31 additions & 0 deletions
packages/api/schemas/config.mts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import z from 'zod'; | ||
| import { AiProvider } from '@srcbook/shared'; | ||
|
|
||
| /** | ||
| * Fields a client is allowed to write via POST /api/settings. | ||
| * | ||
| * Deliberately an allowlist rather than a partial of the drizzle row type. The | ||
| * body used to be handed straight to `db.update(configs).set(...)`, which made | ||
| * every column writable — including `installId`, and `aiBaseUrl`, which decides | ||
| * where AI requests (and the user's API key) get sent. | ||
| */ | ||
| export const ConfigUpdateSchema = z | ||
| .object({ | ||
| baseDir: z.string().min(1), | ||
| defaultLanguage: z.enum(['javascript', 'typescript']), | ||
| openaiKey: z.string().nullable(), | ||
| anthropicKey: z.string().nullable(), | ||
| xaiKey: z.string().nullable(), | ||
| geminiKey: z.string().nullable(), | ||
| openrouterKey: z.string().nullable(), | ||
| customApiKey: z.string().nullable(), | ||
| aiProvider: z.enum(Object.values(AiProvider) as [string, ...string[]]), | ||
| aiModel: z.string().nullable(), | ||
| aiBaseUrl: z.string().url().nullable(), | ||
| subscriptionEmail: z.string().nullable(), | ||
| enabledAnalytics: z.boolean(), | ||
| }) | ||
| .partial() | ||
| .strict(); | ||
|
|
||
| export type ConfigUpdateType = z.infer<typeof ConfigUpdateSchema>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.