Skip to content

Commit ef00064

Browse files
authored
Merge pull request #130 from prezha/fix-gh-fg-token
fix regex for checking github fine-grained tokens
2 parents 22a486f + c4a2377 commit ef00064

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

cmd/reviewGOOSE/security.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ var (
2626
// githubTokenRegex validates GitHub token format.
2727
// Classic tokens: 40 hex chars.
2828
// New tokens: ghp_ (personal), ghs_ (server), ghr_ (refresh), gho_ (OAuth), ghu_ (user-to-server) followed by base62 chars.
29-
// Fine-grained tokens: github_pat_ followed by base62 chars.
30-
githubTokenRegex = regexp.MustCompile(`^[a-f0-9]{40}$|^gh[psoru]_[A-Za-z0-9]{36,251}$|^github_pat_[A-Za-z0-9]{82}$`)
29+
// Fine-grained tokens: github_pat_ followed by 22 base62 chars, underscore, and 59 base62 chars.
30+
githubTokenRegex = regexp.MustCompile(`^[a-f0-9]{40}$|^gh[psoru]_[A-Za-z0-9]{36,251}$|^github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59}$`)
3131
)
3232

3333
// validateGitHubUsername validates a GitHub username.
@@ -80,7 +80,7 @@ func sanitizeForLog(s string) string {
8080
// New format tokens (ghp_, ghs_, ghr_, gho_, ghu_)
8181
s = regexp.MustCompile(`\bgh[psoru]_[A-Za-z0-9]{36,251}\b`).ReplaceAllString(s, "[REDACTED-TOKEN]")
8282
// Fine-grained personal access tokens
83-
s = regexp.MustCompile(`\bgithub_pat_[A-Za-z0-9]{82}\b`).ReplaceAllString(s, "[REDACTED-TOKEN]")
83+
s = regexp.MustCompile(`\bgithub_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59}\b`).ReplaceAllString(s, "[REDACTED-TOKEN]")
8484
// Bearer tokens in headers
8585
s = regexp.MustCompile(`Bearer [A-Za-z0-9_\-.]+`).ReplaceAllString(s, "Bearer [REDACTED]")
8686
// Authorization headers

cmd/reviewGOOSE/security_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,16 @@ func TestValidateGitHubToken(t *testing.T) {
138138
},
139139
{
140140
name: "fine-grained PAT",
141-
token: "github_pat_" + strings.Repeat("a", 82),
141+
token: "github_pat_" + strings.Repeat("a", 22) + "_" + strings.Repeat("b", 59),
142142
wantErr: false,
143143
},
144144

145145
// Invalid tokens
146+
{
147+
name: "fine-grained PAT missing underscore",
148+
token: "github_pat_" + strings.Repeat("a", 82),
149+
wantErr: true,
150+
},
146151
{
147152
name: "empty string",
148153
token: "",
@@ -223,7 +228,7 @@ func TestSanitizeForLog(t *testing.T) {
223228
},
224229
{
225230
name: "fine-grained PAT redacted",
226-
input: "token=github_pat_" + strings.Repeat("b", 82),
231+
input: "token=github_pat_" + strings.Repeat("a", 22) + "_" + strings.Repeat("b", 59),
227232
wantHide: true,
228233
},
229234
{

0 commit comments

Comments
 (0)