-
Notifications
You must be signed in to change notification settings - Fork 0
feat(detector): detect GitHub stateless (JWT-format) ghs_ installation tokens #15
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
cemililik
wants to merge
3
commits into
main
Choose a base branch
from
feat/github-stateless-ghs-token
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
3 commits
Select commit
Hold shift + click to select a range
524a172
fix(detector): detect GitHub stateless (JWT-format) ghs_ installation…
cemililik a3bc752
fix(jwt): suppress stateless ghs_ bodies glued to a preceding token char
cemililik 8a4fbeb
test(detector): guard generated detectors.js and pin github 403 verify
cemililik 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
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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| package jwt | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "regexp" | ||
|
|
||
|
|
@@ -11,6 +12,12 @@ import ( | |
|
|
||
| var jwtPattern = regexp.MustCompile(`eyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}`) | ||
|
|
||
| // ghsPrefix marks a GitHub stateless installation token (ghs_APPID_<jwt>). The | ||
| // embedded JWT also matches jwtPattern, but it is already reported in full by | ||
| // the github-oauth-token detector, so this detector suppresses it to avoid | ||
| // splitting one secret into two findings (see isGitHubStatelessBody). | ||
| var ghsPrefix = []byte("ghs_") | ||
|
|
||
| // JWT detects JSON Web Tokens. | ||
| type JWT struct{} | ||
|
|
||
|
|
@@ -28,13 +35,20 @@ func (d *JWT) Severity() finding.Severity { return finding.SeverityHigh } | |
|
|
||
| // Scan scans the given data for JSON Web Token patterns. | ||
| func (d *JWT) Scan(_ context.Context, data []byte) []detector.RawFinding { | ||
| matches := jwtPattern.FindAll(data, -1) | ||
| if len(matches) == 0 { | ||
| locs := jwtPattern.FindAllIndex(data, -1) | ||
| if len(locs) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| findings := make([]detector.RawFinding, 0, len(matches)) | ||
| for _, match := range matches { | ||
| findings := make([]detector.RawFinding, 0, len(locs)) | ||
| for _, loc := range locs { | ||
| start, end := loc[0], loc[1] | ||
| // Skip JWTs that are the body of a GitHub stateless installation token | ||
| // (ghs_APPID_<jwt>); those are reported in full by github-oauth-token. | ||
| if isGitHubStatelessBody(data, start) { | ||
| continue | ||
| } | ||
| match := data[start:end] | ||
| // Reveal only the trailing characters to avoid exposing the JWT | ||
| // header, payload, or signature. | ||
| findings = append(findings, detector.RawFinding{ | ||
|
|
@@ -43,9 +57,47 @@ func (d *JWT) Scan(_ context.Context, data []byte) []detector.RawFinding { | |
| Redacted: detector.RedactBytes(match), | ||
| }) | ||
| } | ||
| if len(findings) == 0 { | ||
| return nil | ||
| } | ||
| return findings | ||
| } | ||
|
|
||
| // isGitHubStatelessBody reports whether the JWT beginning at start is the body | ||
| // of a GitHub stateless installation token (ghs_APPID_<jwt>). RE2 has no | ||
| // lookbehind, so it walks back over the contiguous token run (base64url plus the | ||
| // ghs_/app-ID separators) immediately preceding the match and checks whether | ||
| // that run contains the literal "ghs_". | ||
| // | ||
| // Contains rather than HasPrefix: the run may carry leading base64url bytes with | ||
| // no delimiter (e.g. "xghs_APPID_"). Wherever "ghs_" appears in the run, the run | ||
| // has no dots (dots are not token bytes) so it is glued straight onto this JWT, | ||
| // forming a "ghs_...eyJ.eyJ.sig" shape that the github-oauth-token detector | ||
| // captures in full — its per-segment floors ({8,}) are at or below this | ||
| // detector's ({10,}). Suppressing here therefore only removes a duplicate of a | ||
| // secret the github detector already reports; it can never drop one. (This | ||
| // assumes the github-oauth-token detector is active, which it is by default.) | ||
| func isGitHubStatelessBody(data []byte, start int) bool { | ||
| i := start | ||
| for i > 0 && isTokenByte(data[i-1]) { | ||
| i-- | ||
| } | ||
| return bytes.Contains(data[i:start], ghsPrefix) | ||
| } | ||
|
|
||
| // isTokenByte reports whether b is part of a contiguous token run: a base64url | ||
| // character or one of the separators ('_', '-') that appear in a ghs_ token. | ||
| func isTokenByte(b byte) bool { | ||
| switch { | ||
| case b >= 'a' && b <= 'z', b >= 'A' && b <= 'Z', b >= '0' && b <= '9': | ||
| return true | ||
| case b == '_', b == '-': | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
Comment on lines
+66
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Check test coverage for the jwt detector package
cd internal/detector/jwt
go test -cover -coverprofile=coverage.out
go tool cover -func=coverage.out | grep totalRepository: HodeTech/Leakwatch Length of output: 267 🏁 Script executed: #!/bin/bash
set -euo pipefail
# List all detector packages
packages=$(go list ./internal/detector/... | tr '\n' ' ')
echo "Detector packages:"
echo "$packages"
for pkg in $(go list ./internal/detector/...); do
echo "== $pkg =="
# Create a temp coverprofile per package
tmpfile=$(mktemp)
# Run tests with coverage
go test -cover -coverprofile="$tmpfile" "$pkg" >/dev/null
# Extract total statement coverage
total=$(go tool cover -func="$tmpfile" | awk '/total:/ {print $3}')
echo "statement_coverage: $total"
rm -f "$tmpfile"
doneRepository: HodeTech/Leakwatch Length of output: 9124 Coverage gate: detector packages are below the 95% minimum
🤖 Prompt for AI Agents |
||
|
|
||
| func init() { | ||
| detector.Register(&JWT{}) | ||
| } | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: HodeTech/Leakwatch
Length of output: 474
🏁 Script executed:
Repository: HodeTech/Leakwatch
Length of output: 9032
Test coverage gate for detector packages fails (95% not met overall)
internal/detector/githubis at 100%, but multiple detector packages are below the 95% minimum (e.g.,internal/detector81.2%,gcp88.7%,generic83.3%,custom92.9%,privatekey93.8%,snowflake92.3%,stripe**92.3%, andtestutil` 0.0%), so the detector coverage requirement is not satisfied.🤖 Prompt for AI Agents