Repository navigation
feat(cli): Add a permission class and an MCP exposure to every command #1916
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
8 commits
Select commit
Hold shift + click to select a range
e2d4fba
feat(cli): Add a permission class and an MCP exposure to every command
sentry-junior[bot] 8f45cf4
Merge remote-tracking branch 'origin/main' into feat/command-access-m…
sentry-junior[bot] 60447be
fix(cli): Reject permission "method" on commands with fixed non-API w…
sentry-junior[bot] 93ecd6f
Merge remote-tracking branch 'origin/main' into feat/command-access-m…
sentry-junior[bot] 2725cf0
Merge remote-tracking branch 'origin/main' into feat/command-access-m…
sentry-junior[bot] 3d88d89
Merge remote-tracking branch 'origin/main' into feat/command-access-m…
sentry-junior[bot] 465dc1c
Merge remote-tracking branch 'origin/main' into feat/command-access-m…
sentry-junior[bot] c53ef8d
fix(cli): Keep the command-api worker off the generated schema
sentry-junior[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
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,54 @@ | ||
| #!/usr/bin/env tsx | ||
| /** | ||
| * Verify every command declares an exposure and that its permission class | ||
| * agrees with the requests it sends (`script/lib/command-access.ts`). Uses the | ||
| * same source analysis as `generate:command-api`. | ||
| */ | ||
|
|
||
| import { resolve } from "node:path"; | ||
| import { pathToFileURL } from "node:url"; | ||
| import type { Command, RouteMap } from "../src/lib/introspect.js"; | ||
| import { | ||
| allCommands, | ||
| type CommandRequests, | ||
| commandAccessProblems, | ||
| } from "./lib/command-access.js"; | ||
| import { analyzeCliSources } from "./lib/source-api-usage.js"; | ||
|
|
||
| const { analysis } = await analyzeCliSources(); | ||
|
|
||
| // A command re-exported under another name has one entry per export. | ||
| const requests = new Map<Command, CommandRequests>(); | ||
| for (const { file, name } of analysis.commands) { | ||
| const usage = analysis.exports.get(file)?.get(name); | ||
| const module = await import(pathToFileURL(resolve(file)).href); | ||
| const command = module[name] as Command | undefined; | ||
| if (!(usage && command)) continue; | ||
| const previous = requests.get(command); | ||
| requests.set(command, { | ||
| methods: [...(previous?.methods ?? []), ...usage.methods], | ||
| apiMethods: [...(previous?.apiMethods ?? []), ...usage.apiMethods], | ||
| }); | ||
| } | ||
|
|
||
| const { routes } = await import("../src/app.js"); | ||
| const commands = allCommands(routes as unknown as RouteMap); | ||
| const problems = [...commands].flatMap(([command, path]) => | ||
| commandAccessProblems(command, requests.get(command)).map( | ||
| (problem) => `${path} ${problem}`, | ||
| ), | ||
| ); | ||
|
|
||
| if (problems.length === 0) { | ||
| console.log( | ||
| `✓ All ${commands.size} commands declare an exposure and a permission class that matches their requests`, | ||
| ); | ||
| } else { | ||
| console.error( | ||
| `✗ ${problems.length} command access problem(s):\n\n${problems.map((problem) => ` ${problem}`).join("\n")}`, | ||
| ); | ||
| console.error( | ||
| "\n See script/lib/command-access.ts for when a command declares `permission`.", | ||
| ); | ||
| process.exitCode = 1; | ||
| } |
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,142 @@ | ||
| /** | ||
| * Rules for `check:command-access`: every command declares where agents may | ||
| * run it, and its permission class agrees with the requests it sends. | ||
| * | ||
| * A command's class comes from its Sentry API requests (`apiAccess`), so a | ||
| * declaration is accepted only where those can't settle it: | ||
| * - it also sends a write method outside the Sentry API (DSN ingest, the docs | ||
| * service), and must say whether that writes; | ||
| * - the analysis can't follow its source, or can't read a request's method; | ||
| * - `method`, for a command without fixed requests whose class each call's | ||
| * HTTP method sets (`sentry api`). | ||
| * Hidden commands never run for agents, so they need no declaration, but a | ||
| * declaration they make is still checked. | ||
| */ | ||
|
|
||
| import { | ||
| apiAccess, | ||
| COMMAND_EXPOSURES, | ||
| isWriteMethod, | ||
| } from "../../src/lib/command-access.js"; | ||
| import { | ||
| type Command, | ||
| isCommand, | ||
| isRouteMap, | ||
| type RouteMap, | ||
| } from "../../src/lib/introspect.js"; | ||
|
|
||
| /** Requests the source analysis found for one command. */ | ||
| export type CommandRequests = { | ||
| /** Every method found, including requests outside the Sentry API. */ | ||
| methods: readonly string[]; | ||
| /** Methods of its Sentry API requests; `undefined` when unknown. */ | ||
| apiMethods: readonly (string | undefined)[]; | ||
| }; | ||
|
|
||
| /** Every command in the tree, hidden routes included, with its first path. */ | ||
| export function allCommands( | ||
| routeMap: RouteMap, | ||
| prefix: readonly string[] = [], | ||
| found = new Map<Command, string>(), | ||
| ): Map<Command, string> { | ||
| for (const entry of routeMap.getAllEntries()) { | ||
| const path = [...prefix, entry.name.original]; | ||
| if (isRouteMap(entry.target)) { | ||
| allCommands(entry.target, path, found); | ||
| } else if (isCommand(entry.target) && !found.has(entry.target)) { | ||
| found.set(entry.target, `sentry ${path.join(" ")}`); | ||
| } | ||
| } | ||
| return found; | ||
| } | ||
|
|
||
| /** | ||
| * Problems with one command's exposure and permission. `requests` is | ||
| * undefined when the analysis found no source export for the command. | ||
| */ | ||
| export function commandAccessProblems( | ||
| command: Command, | ||
| requests: CommandRequests | undefined, | ||
| ): string[] { | ||
| const problems: string[] = []; | ||
| const exposure = command.__exposure; | ||
| if (!(exposure && COMMAND_EXPOSURES.includes(exposure))) { | ||
| problems.push( | ||
| `declares no exposure (${COMMAND_EXPOSURES.join(", ")}); pass \`exposure\` to its builder`, | ||
| ); | ||
| } | ||
| const declared = command.__permission; | ||
| if (command.__destructive) { | ||
| if (declared) { | ||
| problems.push( | ||
| `is destructive, which sets its class; drop \`permission: "${declared}"\``, | ||
| ); | ||
| } | ||
| return problems; | ||
| } | ||
| const required = exposure !== "hidden"; | ||
| if (!requests) { | ||
| if (!declared && required) { | ||
| problems.push( | ||
| "has no source export the API analysis can follow; declare `permission`", | ||
| ); | ||
| } | ||
| return problems; | ||
| } | ||
|
|
||
| const api = apiAccess(requests.apiMethods); | ||
| const apiWrites = unique( | ||
| requests.apiMethods.filter( | ||
| (method): method is string => | ||
| method !== undefined && isWriteMethod(method), | ||
| ), | ||
| ); | ||
| const otherWrites = unique( | ||
| requests.methods.filter( | ||
| (method) => | ||
| isWriteMethod(method) && !requests.apiMethods.includes(method), | ||
| ), | ||
| ); | ||
| if (api === "write") { | ||
| if (declared) { | ||
| problems.push( | ||
| declared === "write" | ||
| ? 'declares `permission: "write"`, which its API requests already give; drop it' | ||
| : `declares \`permission: "${declared}"\`, but it sends ${apiWrites.join("/")} Sentry API requests`, | ||
| ); | ||
| } | ||
| } else if (api === "unknown") { | ||
| if (!declared && required) { | ||
| problems.push( | ||
| "sends a Sentry API request whose method the analysis can't read; declare `permission`", | ||
| ); | ||
| } | ||
| } else if (declared === "method") { | ||
| if (api === "read") { | ||
| problems.push( | ||
| 'declares `permission: "method"`, but its Sentry API requests are read-only', | ||
| ); | ||
| } else if (otherWrites.length > 0) { | ||
| problems.push( | ||
| `declares \`permission: "method"\`, but it sends ${otherWrites.join("/")} requests outside the Sentry API; declare \`permission: "read"\` or \`"write"\``, | ||
| ); | ||
| } | ||
| } else if (otherWrites.length > 0) { | ||
| if (!declared && required) { | ||
| problems.push( | ||
| `sends ${otherWrites.join("/")} requests outside the Sentry API; declare \`permission: "read"\` or \`"write"\``, | ||
| ); | ||
| } | ||
| } else if (declared) { | ||
| problems.push( | ||
| declared === "write" | ||
| ? `declares \`permission: "write"\`, but ${api === "read" ? "its Sentry API requests are read-only" : "it sends no request"}` | ||
| : 'declares `permission: "read"`, which its requests already give; drop it', | ||
| ); | ||
| } | ||
| return problems; | ||
| } | ||
|
|
||
| function unique(values: readonly string[]): string[] { | ||
| return [...new Set(values)].sort(); | ||
| } | ||
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.