diff --git a/ci/check-oracle-markers.Tests.ps1 b/ci/check-oracle-markers.Tests.ps1 index fa2b10893..4294ac0fd 100644 --- a/ci/check-oracle-markers.Tests.ps1 +++ b/ci/check-oracle-markers.Tests.ps1 @@ -418,6 +418,41 @@ 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 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 = 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 { + & $Script -Root $Repo 6>$null | Out-Null + } finally { + $Env:GH_TOKEN = $SavedToken + } + + 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 + } } Describe 'Drift report' { diff --git a/ci/check-oracle-markers.ps1 b/ci/check-oracle-markers.ps1 index a3aa003aa..8e4ad763a 100644 --- a/ci/check-oracle-markers.ps1 +++ b/ci/check-oracle-markers.ps1 @@ -18,9 +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 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: 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' } @@ -28,7 +28,23 @@ param( if ($Token) { $Headers.Authorization = "Bearer $Token" } - $Response = Invoke-WebRequest -Uri "https://api.github.com/$Endpoint" -Headers $Headers -SkipHttpErrorCheck + # 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())