Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2017ed6

Browse filesBrowse files
Fix misleading error message when using an expired GITHUB_TOKEN (#2568)
## Summary Currently when if `GITHUB_TOKEN` is set to an expired or invalid token requests to github will fail with a 404. There is not messaging to the user (even with `DEVBOX_DEBUG=1`) to indicate the cause. This took a while to debug, opening this PR to make it easier for the next person this happens to. ## How was it tested? - make a temp devbox project and add a plugin like https://github.com/jetify-com/devbox-plugins/tree/main/mongodb - set `GITHUB_TOKEN` to a random string - see the following error that doesn't indicate the cause of the error at all (also try running with `DEVBOX_DEBUG=1` and see no extra helpful info ``` ❯ devbox install Error: failed to get plugin github:jetpack-io/devbox-plugins?dir=mongodb @ https://raw.githubusercontent.com/jetpack-io/devbox-plugins/master/mongodb/plugin.json (Status code 404). Please make sure a plugin.json file exists in plugin directory. ``` - **now using code in this PR** ``` ❯ ../dist/devbox install Error: failed to get plugin github:jetpack-io/devbox-plugins?dir=mongodb @ https://raw.githubusercontent.com/jetpack-io/devbox-plugins/master/mongodb/plugin.json (Status code 404). The auth header `token gh_1*********` was send with this request. Please make sure a plugin.json file exists in plugin directory. ``` ## Community Contribution License All community contributions in this pull request are licensed to the project maintainers under the terms of the [Apache 2 License](https://www.apache.org/licenses/LICENSE-2.0). By creating this pull request, I represent that I have the right to license the contributions to the project maintainers under the Apache 2 License as stated in the [Community Contribution License](https://github.com/jetify-com/opensource/blob/main/CONTRIBUTING.md#community-contribution-license).
1 parent cc94f70 commit 2017ed6
Copy full SHA for 2017ed6

File tree

Expand file treeCollapse file tree

2 files changed

+73
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+73
-1
lines changed

‎internal/plugin/github.go

Copy file name to clipboardExpand all lines: internal/plugin/github.go
+34-1Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"cmp"
55
"fmt"
66
"io"
7+
"log/slog"
78
"net/http"
89
"net/url"
910
"os"
@@ -104,12 +105,20 @@ func (p *githubPlugin) FileContent(subpath string) ([]byte, error) {
104105
}
105106
defer res.Body.Close()
106107
if res.StatusCode != http.StatusOK {
108+
authInfo := "No auth header was sent with this request."
109+
if req.Header.Get("Authorization") != "" {
110+
authInfo = fmt.Sprintf(
111+
"The auth header `%s` was sent with this request.",
112+
getRedactedAuthHeader(req),
113+
)
114+
}
107115
return nil, 0, usererr.New(
108-
"failed to get plugin %s @ %s (Status code %d). \nPlease make "+
116+
"failed to get plugin %s @ %s (Status code %d).\n%s\nPlease make "+
109117
"sure a plugin.json file exists in plugin directory.",
110118
p.LockfileKey(),
111119
req.URL.String(),
112120
res.StatusCode,
121+
authInfo,
113122
)
114123
}
115124
body, err := io.ReadAll(res.Body)
@@ -147,6 +156,11 @@ func (p *githubPlugin) request(contentURL string) (*http.Request, error) {
147156
if ghToken != "" {
148157
authValue := fmt.Sprintf("token %s", ghToken)
149158
req.Header.Add("Authorization", authValue)
159+
slog.Debug(
160+
"GITHUB_TOKEN env var found, adding to request's auth header",
161+
"headerValue",
162+
getRedactedAuthHeader(req),
163+
)
150164
}
151165

152166
return req, nil
@@ -155,3 +169,22 @@ func (p *githubPlugin) request(contentURL string) (*http.Request, error) {
155169
func (p *githubPlugin) LockfileKey() string {
156170
return p.ref.String()
157171
}
172+
173+
func getRedactedAuthHeader(req *http.Request) string {
174+
authHeader := req.Header.Get("Authorization")
175+
parts := strings.SplitN(authHeader, " ", 2)
176+
177+
if len(authHeader) < 10 || len(parts) < 2 {
178+
// too short to safely reveal any part
179+
return strings.Repeat("*", len(authHeader))
180+
}
181+
182+
authType, token := parts[0], parts[1]
183+
if len(token) < 10 {
184+
// second word too short to reveal any, but show first word
185+
return authType + " " + strings.Repeat("*", len(token))
186+
}
187+
188+
// show first 4 chars of token to help with debugging (will often be "ghp_")
189+
return authType + " " + token[:4] + strings.Repeat("*", len(token)-4)
190+
}

‎internal/plugin/github_test.go

Copy file name to clipboardExpand all lines: internal/plugin/github_test.go
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package plugin
22

33
import (
4+
"net/http"
45
"strings"
56
"testing"
67

@@ -134,3 +135,41 @@ func TestGithubPluginAuth(t *testing.T) {
134135
assert.Equal(t, "token gh_abcd", actual.Header.Get("Authorization"))
135136
})
136137
}
138+
139+
func TestGetRedactedAuthHeader(t *testing.T) {
140+
testCases := []struct {
141+
name string
142+
authHeader string
143+
expected string
144+
}{
145+
{
146+
"normal length token partially readable for debugging",
147+
"token ghp_61b296fb898349778e20532cb65ce38e",
148+
"token ghp_********************************",
149+
},
150+
{
151+
"short token redacted",
152+
"token ghp_61b29",
153+
"token *********",
154+
},
155+
{
156+
"short header fully redacted",
157+
"token xyz",
158+
"*********",
159+
},
160+
{
161+
"no token returns empty string",
162+
"",
163+
"",
164+
},
165+
}
166+
167+
for _, testCase := range testCases {
168+
t.Run(testCase.name, func(t *testing.T) {
169+
req, err := http.NewRequest(http.MethodGet, "https://example.com", nil)
170+
assert.NoError(t, err)
171+
req.Header.Add("Authorization", testCase.authHeader)
172+
assert.Equal(t, testCase.expected, getRedactedAuthHeader(req))
173+
})
174+
}
175+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.