-
Notifications
You must be signed in to change notification settings - Fork 1
Review fixes #2
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
Review fixes #2
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
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
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,94 @@ | ||
| import { resolve, sep } from "node:path"; | ||
| import { ALL_TOOL_NAMES, type SecurityConfig, type ToolName } from "../security.js"; | ||
| import type { ClientGrant } from "./provider.js"; | ||
|
|
||
| /** | ||
| * Narrow a global security config by a per-client grant. Grants can only | ||
| * narrow (never widen), with one exception: if the global config doesn't | ||
| * enable the sandbox, a grant CAN turn it on for that client. Commands are | ||
| * narrowed by ANDing the grant's patterns with the global ones as separate | ||
| * groups (see SecurityConfig.allowedCommands), so a broad grant pattern can't | ||
| * escape the global `--allow-command` ceiling. | ||
| * | ||
| * When `grant` is null (legacy bearer token), the global config is returned | ||
| * as-is minus the `onToolsChanged` hook — runtime toggles reach per-session | ||
| * tools via `broadcastToolToggle`, not via each session's own hook. | ||
| * | ||
| * Kept free of any HTTP/express dependency so it can be unit-tested in | ||
| * isolation (importing the express-laden HTTP controller into the test runner | ||
| * is fragile). | ||
| */ | ||
| export function buildSessionSecurity(global: SecurityConfig, grant: ClientGrant | null): SecurityConfig { | ||
| if (!grant) { | ||
| const { onToolsChanged: _drop, ...rest } = global; | ||
| return { ...rest }; | ||
| } | ||
|
|
||
| const sessionSecurity: SecurityConfig = {}; | ||
|
|
||
| // Tools: intersection of global enabled and grant-requested. | ||
| const grantTools = new Set<ToolName>(grant.tools); | ||
| const globalTools = global.allowedTools ?? new Set<ToolName>(ALL_TOOL_NAMES); | ||
| const intersected = new Set<ToolName>(); | ||
| for (const t of grantTools) { | ||
| if (globalTools.has(t)) intersected.add(t); | ||
| } | ||
| sessionSecurity.allowedTools = intersected; | ||
|
|
||
| // Paths: grant paths must be subpaths of at least one global allowed path | ||
| // (if the global config has a restriction). Otherwise the grant paths apply | ||
| // directly. Grant paths are resolved to absolute form. | ||
| if (grant.allowedPaths?.length) { | ||
| const grantResolved = grant.allowedPaths.map((p) => resolve(p)); | ||
| if (global.allowedPaths?.length) { | ||
| const globalPaths = global.allowedPaths; | ||
| sessionSecurity.allowedPaths = grantResolved.filter((g) => | ||
| globalPaths.some((allowed) => g === allowed || g.startsWith(allowed + sep)), | ||
| ); | ||
| } else { | ||
| sessionSecurity.allowedPaths = grantResolved; | ||
| } | ||
| } else if (global.allowedPaths) { | ||
| sessionSecurity.allowedPaths = global.allowedPaths; | ||
| } | ||
|
|
||
| // Commands: enforce the global ceiling AND the grant as a conjunction of | ||
| // groups (see SecurityConfig.allowedCommands / validateCommand). The global | ||
| // patterns stay as their own group(s); the grant's consent-page patterns are | ||
| // compiled into an additional group. A command must satisfy every group, so | ||
| // a grant can only narrow — never widen — the global restriction, even if the | ||
| // operator types a broad pattern like `.*` on the consent page. | ||
| const commandGroups: RegExp[][] = []; | ||
| if (global.allowedCommands?.length) { | ||
| commandGroups.push(...global.allowedCommands); | ||
| } | ||
| if (grant.allowedCommands?.length) { | ||
| const grantGroup = grant.allowedCommands.flatMap((pat) => { | ||
| try { | ||
| return [new RegExp(`^(?:${pat})$`)]; | ||
| } catch { | ||
| return []; | ||
| } | ||
| }); | ||
| if (grantGroup.length) commandGroups.push(grantGroup); | ||
| } | ||
| if (commandGroups.length) { | ||
| sessionSecurity.allowedCommands = commandGroups; | ||
| } | ||
|
|
||
| // Sandbox: the global config wins whenever it's on (clients can't weaken). | ||
| // Otherwise, if the grant opts in, use the grant's sandbox config. | ||
| if (global.sandbox?.enabled) { | ||
| sessionSecurity.sandbox = global.sandbox; | ||
| } else if (grant.sandbox?.enabled) { | ||
| sessionSecurity.sandbox = { | ||
| enabled: true, | ||
| fsMode: grant.sandbox.fsMode, | ||
| allowedUrls: grant.sandbox.allowedUrls, | ||
| }; | ||
| } else if (global.sandbox) { | ||
| sessionSecurity.sandbox = global.sandbox; | ||
| } | ||
|
|
||
| return sessionSecurity; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.