-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[INS-455] Unify common logic in Atlassian Data Center detectors #4907
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
mustansir14
wants to merge
3
commits into
main
Choose a base branch
from
INS-455-Unify-common-code-in-Atlassian-Data-Center-detectors
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 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
File renamed without changes.
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,117 @@ | ||
| package atlassiandatacenter | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| regexp "github.com/wasilibs/go-re2" | ||
|
|
||
| "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
| ) | ||
|
|
||
| // GetDCTokenPat returns a compiled regex that matches Atlassian Data Center PATs | ||
| // (Jira DC and Confluence DC style) scoped to the given keyword prefixes. | ||
| // | ||
| // PATs are 44-char base64 strings decoding to "<numeric-id>:<random-bytes>". | ||
| // The first character is always M, N, or O because the numeric ID begins with | ||
| // an ASCII digit (0x30–0x39). The trailing boundary prevents matching substrings | ||
| // of longer base64 strings or base64-padded tokens. | ||
| // | ||
| // This does not apply to Bitbucket DC tokens, which use a BBDC- prefix format. | ||
| func GetDCTokenPat(prefixes []string) *regexp.Regexp { | ||
| return regexp.MustCompile( | ||
| detectors.PrefixRegex(prefixes) + `\b([MNO][A-Za-z0-9+/]{43})(?:[^A-Za-z0-9+/=]|\z)`, | ||
| ) | ||
| } | ||
|
|
||
| // FindEndpoints extracts all URLs from data that are near the given keywords, | ||
| // passes them through the resolve function (typically s.Endpoints), deduplicates | ||
| // the results, and returns them as a slice with trailing slashes stripped. | ||
| func FindEndpoints(data string, keywords []string, resolve func(...string) []string) []string { | ||
| urlPat := regexp.MustCompile(detectors.PrefixRegex(keywords) + `(https?://[a-zA-Z0-9][a-zA-Z0-9.\-]*(?::\d{1,5})?)`) | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
| seen := make(map[string]struct{}) | ||
| for _, m := range urlPat.FindAllStringSubmatch(data, -1) { | ||
| seen[m[1]] = struct{}{} | ||
| } | ||
|
|
||
| raw := make([]string, 0, len(seen)) | ||
| for u := range seen { | ||
| raw = append(raw, u) | ||
| } | ||
|
|
||
| resolved := make(map[string]struct{}) | ||
| for _, u := range resolve(raw...) { | ||
| resolved[strings.TrimRight(u, "/")] = struct{}{} | ||
| } | ||
|
|
||
| result := make([]string, 0, len(resolved)) | ||
| for u := range resolved { | ||
| result = append(result, u) | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| // IsStructuralPAT decodes a candidate base64 string and checks that it matches | ||
| // the "<numeric id>:<random bytes>" structure used by Jira and Confluence DC PATs: | ||
| // one or more ASCII digits, a colon, then at least one more byte. | ||
| func IsStructuralPAT(candidate string) bool { | ||
| raw, err := base64.StdEncoding.DecodeString(candidate) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| colon := bytes.IndexByte(raw, ':') | ||
| if colon <= 0 || colon == len(raw)-1 { | ||
| return false | ||
| } | ||
| for _, b := range raw[:colon] { | ||
| if b < '0' || b > '9' { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // MakeVerifyRequest sends a Bearer-authenticated GET request to fullURL and | ||
| // interprets the response: | ||
| // - 200: returns (true, decoded JSON body as map or nil if unparseable, nil) | ||
| // - 401: returns (false, nil, nil) | ||
| // - other: returns (false, nil, error describing the unexpected status) | ||
| // | ||
| // A non-nil error is also returned for network failures. | ||
| // Callers that need fields from the response body (e.g. display name, email) | ||
| // can read them from the returned map; callers that don't need the body can | ||
| // ignore it. | ||
| func MakeVerifyRequest(ctx context.Context, client *http.Client, fullURL, token string) (bool, map[string]any, error) { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, fullURL, http.NoBody) | ||
| if err != nil { | ||
| return false, nil, err | ||
| } | ||
| req.Header.Set("Accept", "application/json") | ||
| req.Header.Set("Authorization", "Bearer "+token) | ||
|
|
||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return false, nil, err | ||
| } | ||
| defer func() { | ||
| _, _ = io.Copy(io.Discard, resp.Body) | ||
| _ = resp.Body.Close() | ||
| }() | ||
|
|
||
| switch resp.StatusCode { | ||
| case http.StatusOK: | ||
| var body map[string]any | ||
| _ = json.NewDecoder(resp.Body).Decode(&body) | ||
| return true, body, nil | ||
| case http.StatusUnauthorized: | ||
| return false, nil, nil | ||
| default: | ||
| return false, nil, fmt.Errorf("unexpected HTTP response status %d", resp.StatusCode) | ||
| } | ||
| } | ||
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.
We can get rid of all this and do
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.
I was trying to make as minimal changes as possible to the detectors' specific logic. Also we generally prefer to use
net/urlto work with URLs and paths. This is valid for your other comment as well.