-
Notifications
You must be signed in to change notification settings - Fork 10
fix(security): honor the SQL permission denial processAST computes #2202
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
Merged
Merged
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,42 @@ | ||
| import { AsyncLocalStorage } from 'node:async_hooks'; | ||
|
|
||
| const operationAuthorizationState = new AsyncLocalStorage<boolean>(); | ||
| interface OperationAuthorizationState { | ||
| bypassAuth: boolean; | ||
| apiOperation?: string; | ||
| } | ||
|
|
||
| const operationAuthorizationState = new AsyncLocalStorage<OperationAuthorizationState>(); | ||
|
|
||
| // Shared, for the common case of no carrier to preserve. | ||
| const BYPASSED: OperationAuthorizationState = Object.freeze({ bypassAuth: true }); | ||
| const ENFORCED: OperationAuthorizationState = Object.freeze({ bypassAuth: false }); | ||
|
|
||
| export function runWithOperationAuthorizationBypass<T>(bypassAuth: boolean, callback: () => T): T { | ||
| return operationAuthorizationState.run(bypassAuth === true, callback); | ||
| // An existing carrier survives: the enforced branch is not a bypass, and a job handler dispatching | ||
| // a nested authorized operation must not lose its own operation identity. | ||
| const apiOperation = operationAuthorizationState.getStore()?.apiOperation; | ||
| if (apiOperation === undefined) { | ||
| return operationAuthorizationState.run(bypassAuth === true ? BYPASSED : ENFORCED, callback); | ||
| } | ||
| return operationAuthorizationState.run(Object.freeze({ bypassAuth: bypassAuth === true, apiOperation }), callback); | ||
| } | ||
|
|
||
| /** | ||
| * `apiOperation` MUST come from the same value that selected the code now running, so it cannot name | ||
| * an operation other than the one executing. That is the entire basis for trusting it: a request | ||
| * property would be forgeable, and on the direct-SQL path this check is the only gate. | ||
| */ | ||
| export function runWithDispatchedOperation<T>(apiOperation: string, callback: () => T): T { | ||
| return operationAuthorizationState.run( | ||
| Object.freeze({ bypassAuth: operationAuthorizationState.getStore()?.bypassAuth === true, apiOperation }), | ||
| callback | ||
| ); | ||
| } | ||
|
|
||
| export function getOperationAuthorizationState(): OperationAuthorizationState | undefined { | ||
| return operationAuthorizationState.getStore(); | ||
| } | ||
|
|
||
| export function isOperationAuthorizationBypassed(): boolean { | ||
| return operationAuthorizationState.getStore() === true; | ||
| return operationAuthorizationState.getStore()?.bypassAuth === true; | ||
| } |
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,15 @@ | ||
| /** | ||
| * `evaluateSQL` honors a supplied `parsed_sql_object` verbatim and skips parsing, so one carrying | ||
| * `permissions_checked: true` executes an AST that no authorization check ever saw. Neither position | ||
| * is ever legitimately client-supplied: dispatch parses the statement itself, and the job worker | ||
| * re-parses from the `sql` string dispatch authorized. | ||
| * | ||
| * Both positions, because the two entry points differ: `chooseOperation` overwrites the top-level | ||
| * object with its own parse, but only inside its SQL branch, so a non-SQL job (`export_local` with a | ||
| * `search_by_value` search) can still carry a client-supplied one through to the persisted row. | ||
| */ | ||
| export function stripSuppliedParsedSqlObject(request: any): void { | ||
| if (!request) return; | ||
| delete request.parsed_sql_object; | ||
| delete request.search_operation?.parsed_sql_object; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.