-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Lloydcoder separate detectors #4817
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
LloydCoder
wants to merge
30
commits into
trufflesecurity:main
Choose a base branch
from
LloydCoder:lloydcoder-separate-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.
+595
−100
Open
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
0a5a341
feat(detectors): add Nigerian fintech & betting credential detector
LloydCoder 8403245
fix: remove unused import common import
LloydCoder 8fa8dcb
feat: add separate Nigerian fintech detectors with verifiers
LloydCoder 9d1c9bc
fix: address Cursor Bugbot findings in Nigerian fintech detectors
LloydCoder 59281cc
fix: address remaining Cursor issues - optimize regex compilation and…
LloydCoder 777f2f9
fix: final Cursor Bugbot issues - HTTP client pooling and verificatio…
LloydCoder d29092d
fix: final logic bug and import consistency
LloydCoder 484f965
fix: final logic bug and import consistency
LloydCoder 5ead2ef
fix: correct pb.go const block closure and add missing enum entries 1…
LloydCoder 0806179
resolve merge conflicts - adjust enum values for new detectors
LloydCoder 99dd4b7
Merge branch 'main' into lloydcoder-separate-detectors
LloydCoder e47177d
fix: add word boundaries to Paystack regex and return errors from all…
LloydCoder e637259
resolve merge conflicts - adjust enum values for new detectors
LloydCoder 50af6f4
fix: capitalize Scanner type to match defaults.go and test expectations
LloydCoder 3cad318
fix: correctly patch pb.go with Remita/Interswitch/Sportybet enum ent…
LloydCoder 7032586
Merge branch 'main' into lloydcoder-separate-detectors
LloydCoder b7705ae
fix: regenerate detectors.pb.go from proto using protoc to fix rawDes…
LloydCoder 78d502d
fix: remove phantom enum entries, regenerate pb.go with Remita=1040 I…
LloydCoder 3c27606
fix: word boundaries on flutterwave, remove fragile body checks, fix …
LloydCoder e2614bb
fix: remove unused bytes and strings imports
LloydCoder bfff390
fix: add missing proto entries 1040-1043, regenerate pb.go, remove du…
LloydCoder 82cd025
fix: correct proto enum IDs (Remita=1044, Interswitch=1045, Sportybet…
LloydCoder 064104b
fix: remove duplicate interswitch keyword, drop unverifiable MAC key …
LloydCoder 0c4c49e
fix: remove misleading macKey keyword that never matches interswitch …
LloydCoder b2d48a2
test: add pattern tests for interswitch, remita, and sportybet detectors
LloydCoder f668b00
fix: add case-insensitive flag to interswitch, remita, sportybet rege…
LloydCoder f21f2f5
fix: rename shadowed receiver variable, cap regex upper bound to 64 c…
LloydCoder 78a60f8
fix: tighten regex lower bound to 40 chars to reduce false positives …
LloydCoder 17df196
fix: update test inputs to 40-char keys, clean remita regex lookahead
LloydCoder b3a92a3
fix: remove lookahead from remita regex (go-re2 does not support look…
LloydCoder 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package interswitch | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/base64" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| regexp "github.com/wasilibs/go-re2" | ||
|
|
||
| "github.com/trufflesecurity/trufflehog/v3/pkg/common" | ||
| "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
| "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
| ) | ||
|
|
||
| var ( | ||
| interswitchKeyPattern = regexp.MustCompile(`(?i)(?:interswitch|quickteller)[_-]?(?:api[_-])?(?:key|secret)["\s:=]+([0-9a-zA-Z]{32,})`) | ||
| interswitchClient = common.SaneHttpClient() | ||
| ) | ||
|
|
||
| type Scanner struct{} | ||
|
|
||
| var _ detectors.Detector = (*Scanner)(nil) | ||
|
|
||
| func (s Scanner) Keywords() []string { | ||
| return []string{"interswitch", "quickteller"} | ||
| } | ||
|
|
||
| func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | ||
| dataStr := string(data) | ||
| matches := interswitchKeyPattern.FindAllStringSubmatch(dataStr, -1) | ||
| for _, match := range matches { | ||
| if len(match) < 2 || match[1] == "" { | ||
| continue | ||
| } | ||
| key := match[1] | ||
| s := detectors.Result{ | ||
| DetectorType: detectorspb.DetectorType_Interswitch, | ||
| Raw: []byte(key), | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if verify { | ||
| verified, verifyErr := verifyInterswitchKey(ctx, key) | ||
| s.Verified = verified | ||
| if verifyErr != nil { | ||
| s.SetVerificationError(verifyErr, key) | ||
| } | ||
| } | ||
| results = append(results, s) | ||
| } | ||
| return results, nil | ||
| } | ||
|
|
||
| func verifyInterswitchKey(ctx context.Context, key string) (bool, error) { | ||
| req, err := http.NewRequestWithContext(ctx, "GET", "https://api.interswitchng.com/api/v1/merchant/profile", nil) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| auth := base64.StdEncoding.EncodeToString([]byte(key + ":")) | ||
| req.Header.Add("Authorization", "Basic "+auth) | ||
| req.Header.Add("Content-Type", "application/json") | ||
| resp, err := interswitchClient.Do(req) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| defer resp.Body.Close() | ||
| _, _ = io.Copy(io.Discard, resp.Body) | ||
| switch resp.StatusCode { | ||
| case http.StatusOK: | ||
| return true, nil | ||
| case http.StatusUnauthorized, http.StatusForbidden: | ||
| return false, nil | ||
| default: | ||
| return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func (s Scanner) Type() detectorspb.DetectorType { | ||
| return detectorspb.DetectorType_Interswitch | ||
| } | ||
|
|
||
| func (s Scanner) Description() string { | ||
| return "Detects Interswitch API keys" | ||
| } | ||
LloydCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,63 @@ | ||
| package interswitch | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
| "github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick" | ||
| ) | ||
|
|
||
| func TestInterswitch_Pattern(t *testing.T) { | ||
| d := Scanner{} | ||
| ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d}) | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| want []string | ||
| }{ | ||
| { | ||
| name: "valid pattern", | ||
| input: "interswitch_api_key=abcdefghijklmnopqrstuvwxyz123456", | ||
| want: []string{"abcdefghijklmnopqrstuvwxyz123456"}, | ||
| }, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input)) | ||
| if len(matchedDetectors) == 0 { | ||
| t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input) | ||
| return | ||
| } | ||
| results, err := d.FromData(context.Background(), false, []byte(test.input)) | ||
| if err != nil { | ||
| t.Errorf("error = %v", err) | ||
| return | ||
| } | ||
| if len(results) != len(test.want) { | ||
| if len(results) == 0 { | ||
| t.Errorf("did not receive result") | ||
| } else { | ||
| t.Errorf("expected %d results, only received %d", len(test.want), len(results)) | ||
| } | ||
| return | ||
| } | ||
| actual := make(map[string]struct{}, len(results)) | ||
| for _, r := range results { | ||
| if len(r.RawV2) > 0 { | ||
| actual[string(r.RawV2)] = struct{}{} | ||
| } else { | ||
| actual[string(r.Raw)] = struct{}{} | ||
| } | ||
| } | ||
| expected := make(map[string]struct{}, len(test.want)) | ||
| for _, v := range test.want { | ||
| expected[v] = struct{}{} | ||
| } | ||
| if diff := cmp.Diff(expected, actual); diff != "" { | ||
| t.Errorf("%s diff: (-want +got)\n%s", test.name, diff) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.