-
Notifications
You must be signed in to change notification settings - Fork 78
fix(update): authenticate GitHub API requests to fix 403 Forbidden #504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PatrickNoFilter
wants to merge
6
commits into
Gitlawb:main
Choose a base branch
from
PatrickNoFilter:fix/update-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
afa7bed
fix(update): authenticate GitHub API requests to fix 403 Forbidden
PatrickNoFilter e930918
fix(update): require HTTPS before sending token, add tests and docs
PatrickNoFilter 292d02e
fix(pr): auth test isolation + case-insensitive hostname
PatrickNoFilter aba1a47
chore: gitignore zero-linux-sandbox
PatrickNoFilter 6230a58
chore: gitignore zero-linux-sandbox
PatrickNoFilter 5e61b59
style: gofmt internal/update/auth_test.go (CI fix)
PatrickNoFilter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package update | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // authTransport intercepts HTTP requests and records the Authorization header | ||
| // so tests can verify whether fetchRelease sent credentials. | ||
| type authTransport struct { | ||
| t *testing.T | ||
| receivedAuth string | ||
| responseBody string | ||
| responseCode int | ||
| } | ||
|
|
||
| func (at *authTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| at.receivedAuth = req.Header.Get("Authorization") | ||
| at.t.Logf("authTransport: %s %s → Authorization: %q", req.Method, req.URL.String(), at.receivedAuth) | ||
| if at.responseBody == "" { | ||
| at.responseBody = `{"tag_name":"v0.2.0","html_url":"https://example.test/release","assets":[{"name":"zero-v0.2.0-linux-x64.tar.gz","browser_download_url":"https://example.test/zero-v0.2.0-linux-x64.tar.gz"},{"name":"zero-v0.2.0-linux-x64.tar.gz.sha256","browser_download_url":"https://example.test/zero-v0.2.0-linux-x64.tar.gz.sha256"}]}` | ||
| } | ||
| if at.responseCode == 0 { | ||
| at.responseCode = 200 | ||
| } | ||
| return &http.Response{ | ||
| StatusCode: at.responseCode, | ||
| Body: io.NopCloser(strings.NewReader(at.responseBody)), | ||
| Header: make(http.Header), | ||
| }, nil | ||
| } | ||
|
|
||
| func TestFetchReleaseSendsAuthToHttpsGithub(t *testing.T) { | ||
| // ZERO_GITHUB_TOKEN → sent to https://api.github.com | ||
| at := &authTransport{t: t} | ||
| oldClient := http.DefaultClient | ||
| http.DefaultClient = &http.Client{Transport: at} | ||
| t.Cleanup(func() { http.DefaultClient = oldClient }) | ||
|
|
||
| t.Setenv("ZERO_GITHUB_TOKEN", "zero_token") | ||
| t.Setenv("GITHUB_TOKEN", "github_fallback") | ||
|
|
||
| _, err := fetchRelease(context.Background(), "https://api.github.com/repos/Gitlawb/zero/releases/latest") | ||
| if err != nil { | ||
| t.Logf("fetchRelease returned error (expected for fake response): %v", err) | ||
| } | ||
| if at.receivedAuth != "Bearer zero_token" { | ||
| t.Fatalf("ZERO_GITHUB_TOKEN should take precedence: got Authorization %q, want %q", at.receivedAuth, "Bearer zero_token") | ||
| } | ||
| } | ||
|
|
||
| func TestFetchReleaseFallsBackToGithubToken(t *testing.T) { | ||
| // Only GITHUB_TOKEN set → sent to https://api.github.com | ||
| at := &authTransport{t: t} | ||
| oldClient := http.DefaultClient | ||
| http.DefaultClient = &http.Client{Transport: at} | ||
| t.Cleanup(func() { http.DefaultClient = oldClient }) | ||
|
|
||
| t.Setenv("GITHUB_TOKEN", "fallback_token") | ||
| t.Setenv("ZERO_GITHUB_TOKEN", "") // clear ambient precedence var | ||
|
|
||
| _, err := fetchRelease(context.Background(), "https://api.github.com/repos/Gitlawb/zero/releases/latest") | ||
| if err != nil { | ||
| t.Logf("fetchRelease returned error (expected for fake response): %v", err) | ||
| } | ||
| if at.receivedAuth != "Bearer fallback_token" { | ||
| t.Fatalf("GITHUB_TOKEN should be used as fallback: got Authorization %q, want %q", at.receivedAuth, "Bearer fallback_token") | ||
| } | ||
| } | ||
|
|
||
| func TestFetchReleaseNoAuthToCustomEndpoint(t *testing.T) { | ||
| // Token set but endpoint is custom host → no auth sent | ||
| at := &authTransport{t: t} | ||
| oldClient := http.DefaultClient | ||
| http.DefaultClient = &http.Client{Transport: at} | ||
| t.Cleanup(func() { http.DefaultClient = oldClient }) | ||
|
|
||
| t.Setenv("ZERO_GITHUB_TOKEN", "secret") | ||
| t.Setenv("GITHUB_TOKEN", "fallback") | ||
|
|
||
| _, err := fetchRelease(context.Background(), "https://internal.mirror.example.com/releases/latest") | ||
| if err != nil { | ||
| t.Logf("fetchRelease returned error (expected for fake response): %v", err) | ||
| } | ||
| if at.receivedAuth != "" { | ||
| t.Fatalf("auth should not be sent to custom endpoint: got Authorization %q", at.receivedAuth) | ||
| } | ||
| } | ||
|
|
||
| func TestFetchReleaseNoAuthToHttpGithub(t *testing.T) { | ||
| // Token set but scheme is HTTP → no auth sent even to api.github.com | ||
| at := &authTransport{t: t} | ||
| oldClient := http.DefaultClient | ||
| http.DefaultClient = &http.Client{Transport: at} | ||
| t.Cleanup(func() { http.DefaultClient = oldClient }) | ||
|
|
||
| t.Setenv("ZERO_GITHUB_TOKEN", "secret") | ||
|
|
||
| _, err := fetchRelease(context.Background(), "http://api.github.com/repos/Gitlawb/zero/releases/latest") | ||
| if err != nil { | ||
| t.Logf("fetchRelease returned error (expected for fake response): %v", err) | ||
| } | ||
| if at.receivedAuth != "" { | ||
| t.Fatalf("auth should not be sent over HTTP: got Authorization %q", at.receivedAuth) | ||
| } | ||
| } | ||
|
|
||
| func TestFetchReleaseNoAuthWhenTokensNotSet(t *testing.T) { | ||
| // No env vars set → no auth sent (authenticated fallback) | ||
| at := &authTransport{t: t} | ||
| oldClient := http.DefaultClient | ||
| http.DefaultClient = &http.Client{Transport: at} | ||
| t.Cleanup(func() { http.DefaultClient = oldClient }) | ||
|
|
||
| t.Setenv("ZERO_GITHUB_TOKEN", "") | ||
| t.Setenv("GITHUB_TOKEN", "") | ||
|
|
||
| _, err := fetchRelease(context.Background(), "https://api.github.com/repos/Gitlawb/zero/releases/latest") | ||
| if err != nil { | ||
| t.Logf("fetchRelease returned error (expected for fake response): %v", err) | ||
| } | ||
| if at.receivedAuth != "" { | ||
| t.Fatalf("no auth should be sent when no tokens set: got Authorization %q", at.receivedAuth) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.