Skip to content

Find, verify, and analyze leaked credentials

[analyze] Fix GitHub token expiration parsing (#3205)

Miccahc381e901cc1a
diff --git a/pkg/analyzer/analyzers/github/common/github.go b/pkg/analyzer/analyzers/github/common/github.go
index ac50386db..298e921b7 100644
--- a/pkg/analyzer/analyzers/github/common/github.go
+++ b/pkg/analyzer/analyzers/github/common/github.go
@@ -81,7 +81,7 @@ func GetTokenMetadata(token string, client *gh.Client) (*TokenMetadata, error) {
return nil, err
}
- expiration, _ := time.Parse("2006-01-02 15:04:05 MST", resp.Header.Get("github-authentication-token-expiration"))
+ expiration, _ := time.Parse("2006-01-02 15:04:05 -0700", resp.Header.Get("github-authentication-token-expiration"))
var oauthScopes []analyzers.Permission
for _, scope := range resp.Header.Values("X-OAuth-Scopes") {
diff --git a/pkg/analyzer/analyzers/github/github.go b/pkg/analyzer/analyzers/github/github.go
index f09f509c7..8bf385e99 100644
--- a/pkg/analyzer/analyzers/github/github.go
+++ b/pkg/analyzer/analyzers/github/github.go
@@ -154,7 +154,7 @@ func AnalyzeAndPrintPermissions(cfg *config.Config, key string) {
color.Red("[i] Token Expiration: does not expire")
} else {
timeRemaining := time.Until(expiry)
- color.Yellow("[i] Token Expiration: %v (%v remaining)", expiry, timeRemaining)
+ color.Yellow("[i] Token Expiration: %v (%s remaining)", expiry, roughHumanReadableDuration(timeRemaining))
}
color.Yellow("[i] Token Type: %s\n\n", info.Metadata.Type)
@@ -164,3 +164,39 @@ func AnalyzeAndPrintPermissions(cfg *config.Config, key string) {
}
classic.PrintClassicToken(cfg, info)
}
+
+// roughHumanReadableDuration converts a duration into a rough estimate for
+// human consumption. The larger the duration, the larger granularity is
+// returned.
+func roughHumanReadableDuration(d time.Duration) string {
+ var gran time.Duration
+ var unit string
+ switch {
+ case d < 1*time.Minute:
+ gran = time.Second
+ unit = "second"
+ case d < 1*time.Hour:
+ gran = time.Minute
+ unit = "minute"
+ case d < 24*time.Hour:
+ gran = time.Hour
+ unit = "hour"
+ case d < 4*7*24*time.Hour:
+ gran = 24 * time.Hour
+ unit = "day"
+ case d < 3*4*7*24*time.Hour:
+ gran = 7 * 24 * time.Hour
+ unit = "week"
+ case d < 5*365*24*time.Hour:
+ gran = 365 * 24 * time.Hour
+ unit = "month"
+ default:
+ gran = 365 * 24 * time.Hour
+ unit = "year"
+ }
+ num := d.Round(gran) / gran
+ if num != 1 {
+ unit += "s"
+ }
+ return fmt.Sprintf("%d %s", num, unit)
+}
diff --git a/pkg/analyzer/analyzers/github/github_test.go b/pkg/analyzer/analyzers/github/github_test.go
index 3f1fb3c43..7d2f49a6a 100644
--- a/pkg/analyzer/analyzers/github/github_test.go
+++ b/pkg/analyzer/analyzers/github/github_test.go
@@ -229,7 +229,7 @@ func TestAnalyzer_Analyze(t *testing.T) {
],
"UnboundedResources": null,
"Metadata": {
- "expiration": "0001-01-01T00:00:00Z",
+ "expiration": "2025-08-05T00:00:00-07:00",
"type": "Fine-Grained GitHub Personal Access Token"
}
}`,