Files
orca/cloud/apps/sandbox-control/test/github-graphql-pin.test.ts
T
Neil 730334b4f5 fix(cloud): GH-AS-MEMBER PRs, comments and pushes from a cloud workspace show as the member, not the bot
stablyai/orca#42960 (Neil, dev) was authored by orca-cloud-dev[bot]. Neil had connected GitHub; member
identities only reached git: gh ran on the workspace placeholder (the App's installation token), and
the Worker's member route on api.github.com allowed only REST under the repo while gh pr create is
GraphQL.

- VM: a gh wrapper runs gh with the attested sender's own placeholder (orcad attests, as for git);
  plain gh otherwise. Behind GH_AS_MEMBER=on (dev on).
- Worker, api.github.com with a member placeholder: the member's own token only for REST under the
  workspace repo and for allowlisted authorship mutations whose every target resolves to it
  (repositoryId equals its node id; other ids looked up with the installation token, cached per repo,
  failures not cached). graphql-js parse; variable defaults, owner-level projectIds, duplicate
  top-level keys and anything unexpected are not member requests. Everything else goes out on the
  installation token as before; an out-of-repo pinned mutation is refused with a GraphQL error.
- Shared workspaces never approve or merge as a member (GraphQL and REST): those go out on the
  installation token, so branch protection still applies.
- Identity rule: the only person who can use a workspace always acts; in a shared one the latest
  sender acts until another member sends, for at most MEMBER_GIT_SHARED_MAX_MINUTES (default 480).
- A GitHub connect or disconnect mid-session reaches the running VM's identities now.
- Tokens never enter the VM.
2026-11-15 21:18:00 -08:00

68 lines
4.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// GH-AS-MEMBER: which GraphQL requests may carry a member's own token (parsed, never regex).
import { describe, expect, it } from 'vitest'
import { classifyGraphqlRequest, repositoryIdOfNode } from '../src/github-graphql-pin'
const req = (query: string, variables?: unknown, operationName?: string) => JSON.stringify({ query, ...(variables ? { variables } : {}), ...(operationName ? { operationName } : {}) })
describe('classifyGraphqlRequest', () => {
it('gh pr create (variables): a member request naming the repository', () => {
const body = req(
'mutation PullRequestCreate($input: CreatePullRequestInput!) { createPullRequest(input: $input) { pullRequest { id url } } }',
{ input: { repositoryId: 'R_repo', baseRefName: 'main', headRefName: 'f', title: 't' } }
)
expect(classifyGraphqlRequest(body)).toMatchObject({ member: true, targets: [{ kind: 'repository', id: 'R_repo' }] })
})
it('inline literals, aliases and fragments on Mutation are all read', () => {
const body = req(`
mutation { x: addComment(input: { subjectId: "PR_1", body: "hi" }) { clientMutationId } ...More }
fragment More on Mutation { ... on Mutation { y: updatePullRequest(input: { pullRequestId: "PR_2" }) { clientMutationId } } }`)
expect(classifyGraphqlRequest(body)).toMatchObject({ member: true, targets: [{ kind: 'node', id: 'PR_1' }, { kind: 'node', id: 'PR_2' }] })
})
it('anything else stays on the installation token', () => {
const cases: [string, string][] = [
[req('query { viewer { login } }'), 'not_a_mutation'],
[req('mutation { createPullRequest(input: {repositoryId: "R"}) { clientMutationId } deleteRepository(input: {repositoryId: "R"}) { clientMutationId } }'), 'mutation_not_allowlisted'],
[req('mutation { addComment(input: {subjectId: "PR_1"}) @include(if: true) { clientMutationId } }'), 'directives'],
[req('mutation A { addComment(input: {subjectId: "X"}) { clientMutationId } } mutation B { addComment(input: {subjectId: "Y"}) { clientMutationId } }'), 'ambiguous_operation'],
[req('mutation { addComment(input: {body: "no target"}) { clientMutationId } }'), 'no_target'],
[req('mutation ($i: AddCommentInput!) { addComment(input: $i) { clientMutationId } }'), 'unresolved_input'],
[req('mutation { addComment(input: {subjectId: "PR_1"}) { ...F } } fragment F on Query { viewer { login } }'), 'member'],
[req('mutation { ... on Query { addComment(input: {subjectId: "PR_1"}) { clientMutationId } } }'), 'fragment_type'],
[req('this is { not graphql'), 'unparsable'],
['not json', 'unreadable_body'],
// Review M1: an omitted variable with a default would be invisible here but applied by GitHub.
[req('mutation ($pr: ID = "PR_other") { addPullRequestReviewThread(input: { pullRequestReviewId: "PRR_in", pullRequestId: $pr }) { clientMutationId } }'), 'variable_defaults'],
// Review L3: owner-level Projects reach outside the repository.
[req('mutation { updatePullRequest(input: { pullRequestId: "PR_1", projectIds: ["P"] }) { clientMutationId } }'), 'outside_repo_input'],
// Review L4: repeated top-level keys.
['{"query":"mutation { addComment(input: {subjectId: \\"PR_1\\"}) { clientMutationId } }","query":"query { viewer { login } }"}', 'duplicate_keys']
]
for (const [body, reason] of cases) {
const verdict = classifyGraphqlRequest(body)
if (reason === 'member') {
// A fragment inside the result selection is the response shape, not a root field: still one target.
expect(verdict).toMatchObject({ member: true, targets: [{ kind: 'node', id: 'PR_1' }] })
} else {
expect(verdict).toEqual({ member: false, reason })
}
}
})
it('with several operations, the named one is the one read', () => {
const body = req('query Q { viewer { login } } mutation M($id: ID!) { markPullRequestReadyForReview(input: {pullRequestId: $id}) { clientMutationId } }', { id: 'PR_9' }, 'M')
expect(classifyGraphqlRequest(body)).toMatchObject({ member: true, targets: [{ kind: 'node', id: 'PR_9' }] })
expect(classifyGraphqlRequest(req('query Q { viewer { login } } mutation M { x }', undefined, 'Q'))).toEqual({ member: false, reason: 'not_a_mutation' })
})
})
describe('repositoryIdOfNode', () => {
it('reads a PR/issue/comment repository, or a review’s pull request’s', () => {
expect(repositoryIdOfNode({ data: { node: { __typename: 'PullRequest', repository: { id: 'R' } } } })).toBe('R')
expect(repositoryIdOfNode({ data: { node: { __typename: 'PullRequestReview', pullRequest: { repository: { id: 'R2' } } } } })).toBe('R2')
expect(repositoryIdOfNode({ data: { node: null } })).toBeNull()
expect(repositoryIdOfNode({ errors: [{}] })).toBeNull()
})
})