Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions toolkits/gh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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/...`
Expand All @@ -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:
Expand All @@ -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:
Expand Down
17 changes: 16 additions & 1 deletion toolkits/gh_bypass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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"},
Expand Down
Loading