Skip to content
Merged
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
35 changes: 35 additions & 0 deletions ci/check-oracle-markers.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down
24 changes: 20 additions & 4 deletions ci/check-oracle-markers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,33 @@ 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' }
$Token = if ($Env:GH_TOKEN) { $Env:GH_TOKEN } else { $Env:GITHUB_TOKEN }
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.
Comment on lines +31 to +33
# 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())
Expand Down
Loading