Skip to content
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

Update OpenAI detector #2863

Merged
merged 2 commits into from
Jun 5, 2024
Merged

Conversation

rgmz
Copy link
Contributor

@rgmz rgmz commented May 17, 2024

Description:

User-level API keys have been deprecated, now project-level API keys (sk-proj-) are the default. They have also introduced service account API keys (sk-$name-).

https://help.openai.com/en/articles/9186755-managing-your-work-in-the-api-platform-with-projects#h_20805964da

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

@rgmz rgmz changed the title Update OpenAI detectors Update OpenAI detector May 17, 2024
@rgmz rgmz force-pushed the feat/openai-update branch 2 times, most recently from 6e49335 to 85c9275 Compare May 17, 2024 14:33
@rgmz rgmz mentioned this pull request May 21, 2024
2 tasks
Copy link
Contributor

@abmussani abmussani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rgmz for updating the open-ai detector. I've left some comments.

}()

switch res.StatusCode {
case 200:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rgmz previously verification was based on range of 2xx Http status. Is there any particular reason to change it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of the detectors are based on a template with generic conditions. I think it's prudent to only consider something verified if it matches known/documented status codes.

Unexpected API/site changes, and thus status code changes, will propagate as an error rather than creating false-positives.

Copy link
Contributor

@abmussani abmussani Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API reference of OpenAI has well defined the error codes (also In there python library). In my opinion, Its good to parse the response but It wont make any effect on verification.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, Its good to parse the response but It wont make any effect on verification.

I agree that parsing error codes doesn't make sense for OpenAI (although there are some APIs where 401 vs 403 is an importance difference).

I don't think it's a good practice to be liberal with what status codes are considered "valid" when only a specific one is expected. It's a recipe for false positives, imo.

Comment on lines +95 to +110
if err = json.NewDecoder(res.Body).Decode(&orgs); err != nil {
return false, nil, err
}

org := orgs.Data[0]
extraData := map[string]string{
"id": org.ID,
"title": org.Title,
"user": org.User,
"description": org.Description,
"role": org.Role,
"is_personal": strconv.FormatBool(org.Personal),
"is_default": strconv.FormatBool(org.Default),
"total_orgs": fmt.Sprintf("%d", len(orgs.Data)),
}
return true, extraData, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rgmz Metadata here is an additional information and the credentials are already been verified based on HTTP Status code. Base on the conversation we had on #2807 and #2808 , these are non-fatal errors and can be ignored without affecting the verification result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..these are non-fatal errors and can be ignored without affecting the verification result.

The question I posed was actually whether it makes sense to return "verified" alongside an error to indicate a potential problem with the response. Generally speaking, I think it's much safer to handle errors rather than ignore them if err == nil { ... }.

There's a larger problem of surfacing errors and giving them precedence, but that's a different topic. e.g., the GitHub detector failing due to a DNS timeout and the JDBC detector failing because of an invalid hosts shouldn't be treated equally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right that these errors should not be ignored. Unfortunately, right now, Detector result struct does not supports to hold these kind of errors (other than verification). In future, we can have separate error field(s) like @dustin-decker proposed to name it as EnrichmentError.

Copy link
Contributor Author

@rgmz rgmz Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, right now, Detector result struct does not supports to hold these kind of errors (other than verification).

I would still consider them verification errors. e.g., invalid JSON can be indicative of false-positives or the API not behaving as expected (#2099).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rgmz for sharing this case study. It is valid use case to convince me. One more case came in my mind is if an API started to redirected to valid page (with status 200) with HTML in response (may be home page), It will also be indicative of false-positives.

Copy link
Contributor

@abmussani abmussani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. LGTM

@dustin-decker dustin-decker merged commit 024b219 into trufflesecurity:main Jun 5, 2024
11 of 12 checks passed
@rgmz rgmz deleted the feat/openai-update branch June 5, 2024 15:33
rgmz added a commit to rgmz/trufflehog that referenced this pull request Jun 5, 2024
)

Co-authored-by: āh̳̕med <13666360+0x1@users.noreply.github.com>
rgmz added a commit to rgmz/trufflehog that referenced this pull request Jun 6, 2024
)

Co-authored-by: āh̳̕med <13666360+0x1@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants