-
Notifications
You must be signed in to change notification settings - Fork 237
fix(db): prevent prototype pollution via select() alias paths (#1584) #1595
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
kevin-dp
wants to merge
6
commits into
TanStack:main
Choose a base branch
from
kevin-dp:fix/1584-select-alias-prototype-pollution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+94
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0dda114
test: reproduce prototype pollution via select() alias (#1584)
kevin-dp 8bfe0be
fix(db): reject unsafe alias path segments in select() compiler
kevin-dp ad93509
ci: apply automated fixes
autofix-ci[bot] 0d6fba3
test: address CodeRabbit review on prototype-pollution tests
kevin-dp 6cc63e3
chore: add changeset for #1584 fix
kevin-dp 5f3fe93
ci: apply automated fixes
autofix-ci[bot] 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
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,5 @@ | ||
| --- | ||
| '@tanstack/db': patch | ||
| --- | ||
|
|
||
| Fix prototype pollution via `select()` alias paths. Aliases were split on `.` and walked into the result object without sanitization, so a query like `select(() => ({ ['__proto__.polluted']: ... }))` (or any segment matching `__proto__`, `prototype`, or `constructor`) could mutate `Object.prototype`. The select compiler now rejects unsafe alias path segments with a new `UnsafeAliasPathError`. Fixes #1584. |
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
60 changes: 60 additions & 0 deletions
60
packages/db/tests/query/select-prototype-pollution.test.ts
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,60 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { createCollection } from '../../src/collection/index.js' | ||
| import { queryOnce } from '../../src/query/index.js' | ||
| import { UnsafeAliasPathError } from '../../src/errors.js' | ||
| import { mockSyncCollectionOptions } from '../utils.js' | ||
|
|
||
| type User = { id: number; name: string } | ||
|
|
||
| const sampleUsers: Array<User> = [ | ||
| { id: 1, name: `Alice` }, | ||
| { id: 2, name: `Bob` }, | ||
| ] | ||
|
|
||
| function makeCollection() { | ||
| return createCollection( | ||
| mockSyncCollectionOptions<User>({ | ||
| id: `proto-pollution-users`, | ||
| getKey: (u) => u.id, | ||
| initialData: sampleUsers, | ||
| }), | ||
| ) | ||
| } | ||
|
|
||
| function prototypeHasOwn(prop: string): boolean { | ||
| return Object.prototype.hasOwnProperty.call(Object.prototype, prop) | ||
| } | ||
|
|
||
| describe(`select() alias prototype pollution (issue #1584)`, () => { | ||
| it(`should reject __proto__ in alias path and not pollute Object.prototype`, async () => { | ||
| const users = makeCollection() | ||
| const hadBefore = prototypeHasOwn(`polluted`) | ||
|
|
||
| await expect( | ||
| queryOnce((q) => | ||
| q.from({ user: users }).select(({ user }) => ({ | ||
| [`__proto__.polluted`]: user.name, | ||
| })), | ||
| ), | ||
| ).rejects.toThrow(UnsafeAliasPathError) | ||
|
|
||
| expect(prototypeHasOwn(`polluted`)).toBe(hadBefore) | ||
| expect(prototypeHasOwn(`polluted`)).toBe(false) | ||
| }) | ||
|
|
||
| it(`should reject constructor in alias path and not pollute Object.prototype`, async () => { | ||
| const users = makeCollection() | ||
| const hadBefore = prototypeHasOwn(`polluted`) | ||
|
|
||
| await expect( | ||
| queryOnce((q) => | ||
| q.from({ user: users }).select(({ user }) => ({ | ||
| [`constructor.prototype.polluted`]: user.name, | ||
| })), | ||
| ), | ||
| ).rejects.toThrow(UnsafeAliasPathError) | ||
|
|
||
| expect(prototypeHasOwn(`polluted`)).toBe(hadBefore) | ||
| expect(prototypeHasOwn(`polluted`)).toBe(false) | ||
| }) | ||
| }) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.