-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Handle custom detector response and include in extra data #3411
Changes from all commits
fb478d4
3662976
670eb3d
6dc944a
36c6519
949426f
707598b
b9bf00c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"bytes" | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
|
@@ -101,10 +102,6 @@ func (c *CustomRegexWebhook) FromData(ctx context.Context, verify bool, data []b | |
close(resultsCh) | ||
|
||
for result := range resultsCh { | ||
// NOTE: I don't believe this is being set anywhere else, hence the map assignment. | ||
result.ExtraData = map[string]string{ | ||
"name": c.GetName(), | ||
} | ||
results = append(results, result) | ||
} | ||
|
||
|
@@ -129,6 +126,7 @@ func (c *CustomRegexWebhook) createResults(ctx context.Context, match map[string | |
DetectorType: detectorspb.DetectorType_CustomRegex, | ||
DetectorName: c.GetName(), | ||
Raw: []byte(raw), | ||
ExtraData: map[string]string{}, | ||
} | ||
|
||
if !verify { | ||
|
@@ -166,14 +164,34 @@ func (c *CustomRegexWebhook) createResults(ctx context.Context, match map[string | |
} | ||
req.Header.Add(key, strings.TrimLeft(value, "\t\n\v\f\r ")) | ||
} | ||
res, err := httpClient.Do(req) | ||
resp, err := httpClient.Do(req) | ||
if err != nil { | ||
continue | ||
} | ||
// TODO: Read response body. | ||
res.Body.Close() | ||
if res.StatusCode == http.StatusOK { | ||
defer func() { | ||
_, _ = io.Copy(io.Discard, resp.Body) | ||
_ = resp.Body.Close() | ||
}() | ||
|
||
if resp.StatusCode == http.StatusOK { | ||
// mark the result as verified | ||
result.Verified = true | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
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. @mcastorina I could not recall where we had a discussion about PII. I remember, we decided not to include PII in extraData. Will this needed to be care here as well ? 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. That's a great point. Custom detector servers are user supplied (or at least user configured), so I don't think PII would be an issue here.. @zricethezav what are your thoughts? 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. |
||
if err != nil { | ||
continue | ||
} | ||
|
||
// TODO: handle different content-type responses seperatly when implement custom detector configurations | ||
responseStr := string(body) | ||
// truncate to 200 characters if response length exceeds 200 | ||
if len(responseStr) > 200 { | ||
responseStr = responseStr[:200] | ||
} | ||
|
||
// store the processed response in ExtraData | ||
result.ExtraData["response"] = responseStr | ||
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. @kashifkhan0771 Thought: storing the entire response could lead to some large outputs. This could be annoying for users who already are using custom detectors. One option could be limit the response to 200 characters. I don't feel comfortable merging this PR as is. @kashifkhan0771 additionally, I tried spinning up a verification server and testing this myself but found that the only thing that could printed in the output was the name of the customer verifier: @CameronLonsdale what is your use case for 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. For me, I always run trufflehog with --json --only-verified. I wanted to surface data from my custom patterns that are the responses from verifying the credential against the API. So this would typically be a JSON blob which tells me info about the user which corresponds to the token I just verified. I wrap trufflehog around custom code to sync results to Jira etc so hence why JSON. But I would expect the extraData to show up in CLI output aswell, so truncating it to be readable at the terminal makes sense. 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. @zricethezav @CameronLonsdale I added the limit of 200 chars for response. I am not sure how to test the custom detector. If anyone of you can guide me how to do it. I can test locally to understand why it is not printing the extra data in output. 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. Thank you, @mcastorina, for your guidance on running the custom detector. I successfully tested it and identified the issue that was preventing the results from printing. I've fixed it now. Please review my changes. |
||
|
||
break | ||
} | ||
} | ||
|
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.
This was the reason the response result wasn't being printed! 😛 @zricethezav
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.
Let me know if the name is something we want to print. I can make the change to append it.