From aafeaf2b123e81e395346a76311f04739074b03e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20CORTIER?= Date: Wed, 2 Dec 2026 01:13:12 +0900 Subject: [PATCH 1/3] ci: keep the token across GitHub API redirects The oracle marker check intermittently failed with "API rate limit exceeded for " although it sends a token. GitHub answers a request for a renamed repository, such as Devolutions/now-proto, with a redirect, and PowerShell drops the Authorization header when following it, so the request counted against the unauthenticated limit that GitHub-hosted runners share, and a private renamed repository would have been reported unreadable instead of checked. The token is now kept on redirects, which stay on api.github.com. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- ci/check-oracle-markers.ps1 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ci/check-oracle-markers.ps1 b/ci/check-oracle-markers.ps1 index a3aa003aae..9a20b74174 100644 --- a/ci/check-oracle-markers.ps1 +++ b/ci/check-oracle-markers.ps1 @@ -18,9 +18,10 @@ param( [switch] $DriftReport, # Calls the GitHub REST API: takes an endpoint and an Accept header, returns its status and body. - # UNTESTED: the tests replace this block, so nothing checks that it sends the token and returns - # an error status instead of throwing; after changing it, run a pinned marker against a public repository - # with a valid token, an invalid one, and none. + # UNTESTED: the tests replace this block, so nothing checks that it sends the token, keeps it across the + # redirect GitHub answers for a renamed repository, and returns an error status instead of throwing; after + # changing it, run a pinned marker against a public repository with a valid token, an invalid one, and none, + # and against a renamed one such as Devolutions/now-proto with a valid token. [scriptblock] $GitHubApi = { param([string] $Endpoint, [string] $Accept) $Headers = @{ Accept = $Accept; 'X-GitHub-Api-Version' = '2022-11-28' } @@ -28,7 +29,9 @@ param( if ($Token) { $Headers.Authorization = "Bearer $Token" } - $Response = Invoke-WebRequest -Uri "https://api.github.com/$Endpoint" -Headers $Headers -SkipHttpErrorCheck + # A redirect stays on api.github.com, and without the token it would count against the runner's shared + # unauthenticated rate limit and could not read a private repository. + $Response = Invoke-WebRequest -Uri "https://api.github.com/$Endpoint" -Headers $Headers -SkipHttpErrorCheck -PreserveAuthorizationOnRedirect [pscustomobject]@{ Status = [int] $Response.StatusCode Body = [System.Text.Encoding]::UTF8.GetString($Response.RawContentStream.ToArray()) From 50ffe922465e7ad37de9b9ebe41a19047a8380de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20CORTIER?= Date: Wed, 2 Dec 2026 04:51:17 +0900 Subject: [PATCH 2/3] ci: test that GitHub API calls keep the token on redirects Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- ci/check-oracle-markers.Tests.ps1 | 19 +++++++++++++++++++ ci/check-oracle-markers.ps1 | 7 +++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ci/check-oracle-markers.Tests.ps1 b/ci/check-oracle-markers.Tests.ps1 index fa2b108931..6d76efc194 100644 --- a/ci/check-oracle-markers.Tests.ps1 +++ b/ci/check-oracle-markers.Tests.ps1 @@ -418,6 +418,25 @@ Describe 'Pinned references to other repositories' { $Calls["repos/Org/specs/contents/docs/CONTRACT.specs.md?ref=$PinA"] | Should -Be 1 } + + It 'Calls GitHub with the token, keeps it across redirects, and returns error statuses' { # oracle: regression #2234 + Set-RepoFile $Repo 'src/lib.rs' "fn f() {} // oracle: contract Org/specs@43958682a1b2:docs/CONTRACT.specs.md §6`n" + Invoke-Git $Repo add -A + Mock Invoke-WebRequest { + [pscustomobject]@{ StatusCode = 404; RawContentStream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes('{"message":"Not Found"}')) } + } + $SavedToken = $Env:GH_TOKEN + $Env:GH_TOKEN = 'token' + try { + & $Script -Root $Repo 6>$null | Out-Null + } finally { + $Env:GH_TOKEN = $SavedToken + } + + Should -Invoke Invoke-WebRequest -Times 1 -Exactly -ParameterFilter { + $Uri -eq 'https://api.github.com/repos/Org/specs' -and $Headers.Authorization -eq 'Bearer token' -and $PreserveAuthorizationOnRedirect -and $SkipHttpErrorCheck + } + } } Describe 'Drift report' { diff --git a/ci/check-oracle-markers.ps1 b/ci/check-oracle-markers.ps1 index 9a20b74174..0b587c2906 100644 --- a/ci/check-oracle-markers.ps1 +++ b/ci/check-oracle-markers.ps1 @@ -18,10 +18,9 @@ param( [switch] $DriftReport, # Calls the GitHub REST API: takes an endpoint and an Accept header, returns its status and body. - # UNTESTED: the tests replace this block, so nothing checks that it sends the token, keeps it across the - # redirect GitHub answers for a renamed repository, and returns an error status instead of throwing; after - # changing it, run a pinned marker against a public repository with a valid token, an invalid one, and none, - # and against a renamed one such as Devolutions/now-proto with a valid token. + # UNTESTED: a test mocks Invoke-WebRequest, so nothing checks that GitHub accepts these headers or that the + # switches behave as named; after changing it, run a pinned marker against a public repository with a valid + # token, an invalid one, and none, and against a renamed one such as Devolutions/now-proto with a valid token. [scriptblock] $GitHubApi = { param([string] $Endpoint, [string] $Accept) $Headers = @{ Accept = $Accept; 'X-GitHub-Api-Version' = '2022-11-28' } From 0e8d556965623da668c0d2ca12e4714e8d975ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20CORTIER?= Date: Wed, 2 Dec 2026 07:34:34 +0900 Subject: [PATCH 3/3] ci: follow GitHub API redirects only within api.github.com Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- ci/check-oracle-markers.Tests.ps1 | 26 +++++++++++++++++++++----- ci/check-oracle-markers.ps1 | 20 +++++++++++++++++--- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/ci/check-oracle-markers.Tests.ps1 b/ci/check-oracle-markers.Tests.ps1 index 6d76efc194..4294ac0fd1 100644 --- a/ci/check-oracle-markers.Tests.ps1 +++ b/ci/check-oracle-markers.Tests.ps1 @@ -419,12 +419,15 @@ Describe 'Pinned references to other repositories' { $Calls["repos/Org/specs/contents/docs/CONTRACT.specs.md?ref=$PinA"] | Should -Be 1 } - It 'Calls GitHub with the token, keeps it across redirects, and returns error statuses' { # oracle: regression #2234 + It 'Calls GitHub with the token and keeps it on a redirect within api.github.com' { # oracle: regression #2234 Set-RepoFile $Repo 'src/lib.rs' "fn f() {} // oracle: contract Org/specs@43958682a1b2:docs/CONTRACT.specs.md §6`n" Invoke-Git $Repo add -A Mock Invoke-WebRequest { - [pscustomobject]@{ StatusCode = 404; RawContentStream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes('{"message":"Not Found"}')) } - } + [pscustomobject]@{ StatusCode = 301; Headers = @{ Location = @('https://api.github.com/repositories/1') }; RawContentStream = [System.IO.MemoryStream]::new() } + } -ParameterFilter { "$Uri" -eq 'https://api.github.com/repos/Org/specs' } + Mock Invoke-WebRequest { + [pscustomobject]@{ StatusCode = 404; Headers = @{}; RawContentStream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes('{"message":"Not Found"}')) } + } -ParameterFilter { "$Uri" -eq 'https://api.github.com/repositories/1' } $SavedToken = $Env:GH_TOKEN $Env:GH_TOKEN = 'token' try { @@ -433,9 +436,22 @@ Describe 'Pinned references to other repositories' { $Env:GH_TOKEN = $SavedToken } - Should -Invoke Invoke-WebRequest -Times 1 -Exactly -ParameterFilter { - $Uri -eq 'https://api.github.com/repos/Org/specs' -and $Headers.Authorization -eq 'Bearer token' -and $PreserveAuthorizationOnRedirect -and $SkipHttpErrorCheck + foreach ($Target in 'https://api.github.com/repos/Org/specs', 'https://api.github.com/repositories/1') { + Should -Invoke Invoke-WebRequest -Times 1 -Exactly -ParameterFilter { + "$Uri" -eq $Target -and $Headers.Authorization -eq 'Bearer token' -and $MaximumRedirection -eq 0 -and $SkipHttpErrorCheck + } + } + } + + It 'Stops on a redirect away from api.github.com without following it' { # oracle: regression #2234 + Set-RepoFile $Repo 'src/lib.rs' "fn f() {} // oracle: contract Org/specs@43958682a1b2:docs/CONTRACT.specs.md §6`n" + Invoke-Git $Repo add -A + Mock Invoke-WebRequest { + [pscustomobject]@{ StatusCode = 302; Headers = @{ Location = @('https://codeload.github.com/Org/specs') }; RawContentStream = [System.IO.MemoryStream]::new() } } + + { & $Script -Root $Repo 6>$null } | Should -Throw '*returned 302*' + Should -Invoke Invoke-WebRequest -Times 1 -Exactly } } diff --git a/ci/check-oracle-markers.ps1 b/ci/check-oracle-markers.ps1 index 0b587c2906..8e4ad763a3 100644 --- a/ci/check-oracle-markers.ps1 +++ b/ci/check-oracle-markers.ps1 @@ -28,9 +28,23 @@ param( if ($Token) { $Headers.Authorization = "Bearer $Token" } - # A redirect stays on api.github.com, and without the token it would count against the runner's shared - # unauthenticated rate limit and could not read a private repository. - $Response = Invoke-WebRequest -Uri "https://api.github.com/$Endpoint" -Headers $Headers -SkipHttpErrorCheck -PreserveAuthorizationOnRedirect + # GitHub redirects a request for a renamed repository to its new API path. + # Without the token, it would count against the runner's shared unauthenticated rate limit and could not read a + # private repository, so redirects are followed here, and only within api.github.com, the one host given the token. + # A redirect anywhere else is returned as is, and its status then stops the check. + $Uri = [uri] "https://api.github.com/$Endpoint" + for ($Redirects = 0; $true; $Redirects++) { + $Response = Invoke-WebRequest -Uri $Uri -Headers $Headers -SkipHttpErrorCheck -MaximumRedirection 0 -ErrorAction SilentlyContinue + $Location = @($Response.Headers['Location'])[0] + if ([int] $Response.StatusCode -notin 301, 302, 307, 308 -or -not $Location -or $Redirects -ge 5) { + break + } + $Next = [uri]::new($Uri, $Location) + if ($Next.Scheme -ne 'https' -or $Next.Host -ne 'api.github.com') { + break + } + $Uri = $Next + } [pscustomobject]@{ Status = [int] $Response.StatusCode Body = [System.Text.Encoding]::UTF8.GetString($Response.RawContentStream.ToArray())