From c3d5743ea6b5ba36be81361b11ad986b9cfaf831 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Thu, 12 Nov 2026 16:10:21 -0500 Subject: [PATCH] Authorize gh api endpoints written with a leading slash gh strips a leading slash from an api endpoint, and GitHub's REST docs print endpoints that way, but the gh toolkit only recognized the bare form. A call such as 'gh api -X POST /repos/OWNER/NAME/forks' therefore named no repository and was refused outright, with no approval a person could grant. The slashed and bare spellings now resolve to the same repository; an endpoint that names none still fails closed. --- toolkits/gh.yaml | 19 ++++++++++++++----- toolkits/gh_bypass_test.go | 17 ++++++++++++++++- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/toolkits/gh.yaml b/toolkits/gh.yaml index 9a9677f9..992bfe9f 100644 --- a/toolkits/gh.yaml +++ b/toolkits/gh.yaml @@ -1147,11 +1147,20 @@ subcommands: # The FALLBACK is the dangerous reading, and that ordering is the design: # variants below name what is SAFE, so a method or endpoint nobody # anticipated lands here rather than slipping past an allowlist. + # + # Every expression here and in the variants reads the endpoint with ONE + # leading slash stripped. gh does the same before it builds the request, + # so `/repos/o/r/forks` and `repos/o/r/forks` are one call — and the + # slashed form is the one GitHub's own REST docs print. Reading only the + # bare form left the slashed one naming no repository: the fallback + # resolved an empty id and the call was refused with no approval a human + # could have granted. Exactly one slash, as gh strips: `//repos/...` still + # names nothing and still fails closed. stateImpact: external check: resourceType: github_repo_url permission: write - resourceIDExpr: 'has(args.endpoint) && args.endpoint.startsWith("repos/") && args.endpoint.split("/").size() >= 3 ? args.endpoint.split("/")[1] + "/" + args.endpoint.split("/")[2] : ""' + resourceIDExpr: 'has(args.endpoint) && (args.endpoint.startsWith("/") ? args.endpoint.substring(1) : args.endpoint).startsWith("repos/") && (args.endpoint.startsWith("/") ? args.endpoint.substring(1) : args.endpoint).split("/").size() >= 3 ? (args.endpoint.startsWith("/") ? args.endpoint.substring(1) : args.endpoint).split("/")[1] + "/" + (args.endpoint.startsWith("/") ? args.endpoint.substring(1) : args.endpoint).split("/")[2] : ""' resourceIDTransforms: [github_repo_url_id] resourceIDHint: "for a write, call a repo-scoped endpoint (repos/OWNER/NAME/...) — an endpoint naming no repository cannot be authorized" permissionVariants: @@ -1175,13 +1184,13 @@ subcommands: # A repo-scoped GET is a read of that repository. `gh pr view` is # readonly#read on the same object; routing the identical read through a # human just because it went via `api` was over-restriction, not security. - - when: '(!has(args.method) || args.method == "" || args.method == "GET") && !("field" in args) && !("raw-field" in args) && !("hostname" in args) && has(args.endpoint) && args.endpoint.startsWith("repos/") && args.endpoint.split("/").size() >= 3' + - when: '(!has(args.method) || args.method == "" || args.method == "GET") && !("field" in args) && !("raw-field" in args) && !("hostname" in args) && has(args.endpoint) && (args.endpoint.startsWith("/") ? args.endpoint.substring(1) : args.endpoint).startsWith("repos/") && (args.endpoint.startsWith("/") ? args.endpoint.substring(1) : args.endpoint).split("/").size() >= 3' check: stateImpact: readonly check: resourceType: github_repo_url permission: read - resourceIDExpr: 'args.endpoint.split("/")[1] + "/" + args.endpoint.split("/")[2]' + resourceIDExpr: '(args.endpoint.startsWith("/") ? args.endpoint.substring(1) : args.endpoint).split("/")[1] + "/" + (args.endpoint.startsWith("/") ? args.endpoint.substring(1) : args.endpoint).split("/")[2]' # Same canonical id as every other repository reference here: the # endpoint yields OWNER/NAME, and github_repo_url_id folds and # encodes it exactly as `gh pr view` does, so `gh api repos/o/r/...` @@ -1191,7 +1200,7 @@ subcommands: # flow opens with it to derive the committer, and it names no repository, # so it keys on the `self` sentinel: grantable and revocable on its own, # rather than either ungated or impossible. - - when: '(!has(args.method) || args.method == "" || args.method == "GET") && !("field" in args) && !("raw-field" in args) && !("hostname" in args) && has(args.endpoint) && (args.endpoint == "user" || args.endpoint.startsWith("user/"))' + - when: '(!has(args.method) || args.method == "" || args.method == "GET") && !("field" in args) && !("raw-field" in args) && !("hostname" in args) && has(args.endpoint) && ((args.endpoint.startsWith("/") ? args.endpoint.substring(1) : args.endpoint) == "user" || (args.endpoint.startsWith("/") ? args.endpoint.substring(1) : args.endpoint).startsWith("user/"))' check: stateImpact: readonly check: @@ -1205,7 +1214,7 @@ subcommands: # grant stays written on the literal `self`, exactly as before. resourceIDTransforms: [github_repo_url_id] # Cross-repo search, same sentinel the `search` subcommands use. - - when: '(!has(args.method) || args.method == "" || args.method == "GET") && !("field" in args) && !("raw-field" in args) && !("hostname" in args) && has(args.endpoint) && args.endpoint.startsWith("search/")' + - when: '(!has(args.method) || args.method == "" || args.method == "GET") && !("field" in args) && !("raw-field" in args) && !("hostname" in args) && has(args.endpoint) && (args.endpoint.startsWith("/") ? args.endpoint.substring(1) : args.endpoint).startsWith("search/")' check: stateImpact: readonly check: diff --git a/toolkits/gh_bypass_test.go b/toolkits/gh_bypass_test.go index 5b51b9c3..fcaa277e 100644 --- a/toolkits/gh_bypass_test.go +++ b/toolkits/gh_bypass_test.go @@ -97,7 +97,15 @@ func TestGhToolkit_apiFailsClosedWhenTheEndpointNamesNoRepo(t *testing.T) { assert.Equal(t, "aHR0cHM6Ly9naXRodWIuY29tL2RlbW8tb3JnL2RlbW8tcmVwbw", got, "the base64url-encoded github_repo_url id is the object the grant is written on") - for _, endpoint := range []string{"user", "graphql", "search/issues", "repos", "repos/only-owner"} { + // gh strips one leading slash before it builds the request, and GitHub's REST + // docs print the slashed form, so it has to resolve to the SAME object. Read + // as naming no repository, a fork request spelled `/repos/o/r/forks` was + // refused with no approval anyone could have granted. + got, err = authz.ResolveResourceID(*p.Check, map[string]any{"endpoint": "/repos/demo-org/demo-repo/forks"}) + require.NoError(t, err, "a leading slash must not hide the repository") + assert.Equal(t, "aHR0cHM6Ly9naXRodWIuY29tL2RlbW8tb3JnL2RlbW8tcmVwbw", got) + + for _, endpoint := range []string{"user", "/user", "graphql", "search/issues", "repos", "repos/only-owner", "/repos/only-owner", "//repos/demo-org/demo-repo"} { got, err := authz.ResolveResourceID(*p.Check, map[string]any{"endpoint": endpoint}) assert.True(t, got == "" || err != nil, "endpoint %q names no repository; it must not resolve to an authorizable id, got %q", @@ -156,6 +164,12 @@ func TestGhToolkit_apiChargesReadsAsReads(t *testing.T) { // run one chain without moving a sentinel's grant. See e1bc38836 and // the apiFailsClosed test. impact: authz.Readonly, id: "aHR0cHM6Ly9naXRodWIuY29tL2RlbW8tb3JnL2RlbW8tcmVwbw"}, + {name: "GET a repo endpoint with a leading slash: the same read on the same repo", + args: map[string]any{"endpoint": "/repos/demo-org/demo-repo/pulls"}, + impact: authz.Readonly, id: "aHR0cHM6Ly9naXRodWIuY29tL2RlbW8tb3JnL2RlbW8tcmVwbw"}, + {name: "gh api /user: the slashed spelling keys on the self sentinel too", + args: map[string]any{"endpoint": "/user"}, + impact: authz.Readonly, id: "self"}, {name: "gh api user: readonly on the self sentinel, not on any repo", args: map[string]any{"endpoint": "user"}, impact: authz.Readonly, id: "self"}, @@ -184,6 +198,7 @@ func TestGhToolkit_apiFallsBackToWriteForEverythingElse(t *testing.T) { for _, args := range []map[string]any{ {"endpoint": "repos/demo-org/demo-repo/pulls", "method": "POST"}, {"endpoint": "repos/demo-org/demo-repo", "method": "DELETE"}, + {"endpoint": "/repos/demo-org/demo-repo/forks", "method": "POST"}, {"endpoint": "graphql"}, {"endpoint": "user", "method": "PATCH"}, {"endpoint": "orgs/demo-org/members"},