strings contain keyword check, add collection name to keywords (#2602)

This commit is contained in:
Zachary Rice 2024-03-21 09:35:38 -05:00 committed by GitHub
parent 3f47dbccfb
commit 1216fa23c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 1 deletions

View file

@ -60,8 +60,18 @@ func (s *Source) addKeywords(keywords []string) {
}
func (s *Source) addKeyword(keyword string) {
// fast check
if _, ok := s.DetectorKeywords[keyword]; ok {
s.keywords[keyword] = struct{}{}
return
}
// slow check. This is to handle the case where the keyword is a substring of a detector keyword
// e.g. "datadog-token" is a variable key in postman, but "datadog" is a detector keyword
for k := range s.DetectorKeywords {
if strings.Contains(keyword, k) {
s.keywords[k] = struct{}{}
}
}
}
@ -325,6 +335,7 @@ func (s *Source) scanCollection(ctx context.Context, chunksChan chan *sources.Ch
ctx.Logger().V(2).Info("starting scanning collection", collection.Info.Name, "uuid", collection.Info.UID)
metadata.CollectionInfo = collection.Info
metadata.Type = COLLECTION_TYPE
s.addKeyword(collection.Info.Name)
if !metadata.fromLocal {
metadata.FullID = metadata.CollectionInfo.UID

View file

@ -145,7 +145,6 @@ func TestSource_FormatAndInjectKeywords(t *testing.T) {
expected := strings.Split(tc.expected, "\n")
sort.Strings(got)
sort.Strings(expected)
// CHATGPT CHECK HERE
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected result: %q, got: %q", tc.expected, result)