Fix gh CLI GHES host pinning and rate-limit scope leaks

- Pin `--host` on every gh call site so a process-level GH_HOST can't
  silently redirect requests, and qualify `-R`/`-R=` repo shorthand
  alongside the existing `--repo=` handling.
- Check the target scope for an active rate-limit block before each
  WSL/native or host fallback retry, not just on the initial attempt,
  so a blocked scope can't be hit again through a fallback path.
- Compute idempotency once per call instead of re-deriving it after
  fallback reassigns args.
This commit is contained in:
Jinjing
2026-07-19 18:03:48 -07:00
parent 58034d2f74
commit 8ad7686fe3
10 changed files with 220 additions and 45 deletions
+5 -1
View File
@@ -75,7 +75,10 @@ describe('setPRFileViewed', () => {
expect(args.find((arg: string) => arg.startsWith('query='))).toContain('markFileAsViewed')
expect(args).toContain('pullRequestId=PR_kwDO123')
expect(args).toContain('path=src/app.ts')
expect(ghExecFileAsyncMock.mock.calls[0][1]).toEqual({ cwd: '/repo-root' })
expect(ghExecFileAsyncMock.mock.calls[0][1]).toEqual({
cwd: '/repo-root',
host: 'github.com'
})
expect(releaseMock).toHaveBeenCalledTimes(1)
})
@@ -111,6 +114,7 @@ describe('setPRFileViewed', () => {
expect(ghExecFileAsyncMock.mock.calls[0][1]).toEqual({
cwd: '/repo-root',
host: 'github.com',
wslDistro: 'Ubuntu'
})
})
@@ -132,17 +132,17 @@ describe('getPRCheckDetails', () => {
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
1,
['api', 'repos/acme/widgets/check-runs/88'],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
2,
['api', 'repos/acme/widgets/check-runs/88/annotations?per_page=20'],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
3,
['api', 'repos/acme/widgets/actions/runs/77/jobs?per_page=100'],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
})
@@ -209,7 +209,7 @@ describe('getPRCheckDetails', () => {
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
4,
['api', 'repos/acme/widgets/actions/jobs/8801/logs'],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
expect(ghExecFileAsyncMock).toHaveBeenCalledTimes(4)
})
+9 -5
View File
@@ -422,12 +422,12 @@ describe('getPRChecks', () => {
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
2,
['api', '--cache', '60s', 'repos/acme/widgets/commits/head-oid/check-runs?per_page=100'],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
3,
['api', '--cache', '60s', 'repos/acme/widgets/commits/head-oid/status?per_page=100'],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
expect(checks).toEqual([
{
@@ -505,7 +505,7 @@ describe('getPRChecks', () => {
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
3,
['pr', 'checks', '42', '--json', 'name,state,link', '--repo', 'acme/widgets'],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
expect(checks).toEqual([
{
@@ -542,7 +542,11 @@ describe('getPRChecks', () => {
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
2,
['api', '-X', 'POST', 'repos/acme/widgets/actions/runs/77/rerun-failed-jobs'],
{ cwd: '/repo-root', env: { ...process.env, GH_PROMPT_DISABLED: '1' } }
{
cwd: '/repo-root',
env: { ...process.env, GH_PROMPT_DISABLED: '1' },
host: 'github.com'
}
)
})
@@ -611,7 +615,7 @@ describe('getPRChecks', () => {
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
2,
['pr', 'checks', '42', '--json', 'name,state,link', '--repo', 'acme/widgets'],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
})
+14 -11
View File
@@ -3702,7 +3702,7 @@ describe('updatePRState', () => {
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(
['pr', 'reopen', '3977', '--repo', 'stablyai/orca'],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
expect(acquireMock).toHaveBeenCalledTimes(1)
expect(releaseMock).toHaveBeenCalledTimes(1)
@@ -3718,7 +3718,7 @@ describe('updatePRState', () => {
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(
['pr', 'close', '3977', '--repo', 'stablyai/orca'],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
})
@@ -3734,7 +3734,7 @@ describe('updatePRState', () => {
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(
['pr', 'reopen', '3977', '--repo', 'stablyai/orca'],
{}
{ host: 'github.com' }
)
})
})
@@ -3823,12 +3823,12 @@ describe('GitHub GraphQL rate-limit guard', () => {
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
1,
['api', '--cache', '60s', 'repos/stablyai/orca/issues/7/comments?per_page=100'],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
2,
['api', '--cache', '60s', 'repos/stablyai/orca/pulls/7/reviews?per_page=100'],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
})
@@ -3878,20 +3878,21 @@ describe('GitHub GraphQL rate-limit guard', () => {
'--json',
'number,title,state,url,statusCheckRollup,updatedAt,isDraft,mergeable,reviewDecision,mergeStateStatus,autoMergeRequest,baseRefName,headRefName,baseRefOid,headRefOid'
],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
2,
['pr', 'merge', '7', '--squash', '--repo', 'stablyai/orca'],
expect.objectContaining({
cwd: '/repo-root',
env: expect.objectContaining({ GH_PROMPT_DISABLED: '1' })
env: expect.objectContaining({ GH_PROMPT_DISABLED: '1' }),
host: 'github.com'
})
)
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
3,
['pr', 'edit', '7', '--title', 'New title', '--repo', 'stablyai/orca'],
{ cwd: '/repo-root' }
{ cwd: '/repo-root', host: 'github.com' }
)
})
@@ -3920,7 +3921,7 @@ describe('GitHub GraphQL rate-limit guard', () => {
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
1,
['pr', 'view', '7', '--json', 'id,headRefOid,baseRefName', '--repo', 'stablyai/orca'],
{}
{ host: 'github.com' }
)
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
2,
@@ -3935,14 +3936,16 @@ describe('GitHub GraphQL rate-limit guard', () => {
'expectedHeadOid=head-oid'
]),
expect.objectContaining({
env: expect.objectContaining({ GH_PROMPT_DISABLED: '1' })
env: expect.objectContaining({ GH_PROMPT_DISABLED: '1' }),
host: 'github.com'
})
)
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
3,
['pr', 'merge', '7', '--disable-auto', '--repo', 'stablyai/orca'],
expect.objectContaining({
env: expect.objectContaining({ GH_PROMPT_DISABLED: '1' })
env: expect.objectContaining({ GH_PROMPT_DISABLED: '1' }),
host: 'github.com'
})
)
expect(ghExecFileAsyncMock.mock.calls[0]?.[1]).not.toHaveProperty('cwd')
@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest'
import { githubHostExecOptions } from './github-api-repository'
describe('githubHostExecOptions', () => {
it('pins every known repository host', () => {
expect(githubHostExecOptions({ owner: 'acme', repo: 'widgets', host: 'github.com' })).toEqual({
host: 'github.com'
})
expect(
githubHostExecOptions({ owner: 'acme', repo: 'widgets', host: 'github.acme-corp.com' })
).toEqual({ host: 'github.acme-corp.com' })
})
it('does not invent a host when repository identity is unavailable or legacy', () => {
expect(githubHostExecOptions({ owner: 'acme', repo: 'widgets' })).toEqual({})
expect(githubHostExecOptions(null)).toEqual({})
})
})
+4 -5
View File
@@ -49,14 +49,13 @@ export function isGitHubDotComRepository(repository: GitHubApiRepository): boole
return !repository.host || repository.host.toLowerCase() === 'github.com'
}
// Why: the gh runner host-qualifies argv (`--hostname`, `HOST/OWNER/REPO`)
// from `options.host`, so merging this into a call's exec options is the one
// step a call site needs to target GHES. github.com stays unset to keep gh's
// default-host resolution (and existing argv) unchanged.
// Why: the gh runner host-qualifies argv from `options.host`, so every known
// host must be carried through. Pinning github.com prevents a process-level
// GH_HOST from silently redirecting an otherwise unambiguous API request.
export function githubHostExecOptions(repository: GitHubApiRepository | null | undefined): {
host?: string
} {
return repository?.host && !isGitHubDotComRepository(repository) ? { host: repository.host } : {}
return repository?.host ? { host: repository.host } : {}
}
export function githubRepositoryWebHost(repository: GitHubApiRepository): string {