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

Create rule S6985 : Usage of "torch.load" can lead to untrusted code execution #3976

Merged
merged 5 commits into from
Sep 17, 2024

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Jun 5, 2024

You can preview this rule here (updated a few minutes after each push).

Review

A dedicated reviewer checked the rule description successfully for:

  • logical errors and incorrect information
  • information gaps and missing content
  • text style and tone
  • PR summary and labels follow the guidelines

Copy link
Contributor

@joke1196 joke1196 left a comment

Choose a reason for hiding this comment

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

Looks good, just a minor suggestion. And don't forget to add a title to the PR.

If the model comes from an untrusted source, an attacker could inject a malicious payload which would be executed during the deserialization.

== How to fix it
Use a safer alternative to load the model, such as `safetensors.torch.load_model`.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you will need to add a new line before the title. === Code examples


== Why is this an issue?

Under the hood, `torch.load` uses the `pickle` library to load the model and the weights.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we could add just a small introduction line. In PyTorch it is common practice to load a serialized model or something like that.

@ghislainpiot ghislainpiot changed the title Create rule S6985 Create rule S6985 : Usage of "torch.load" can lead to untrusted code execution Jun 10, 2024
@ghislainpiot ghislainpiot requested a review from joke1196 June 10, 2024 09:17
Copy link

Quality Gate passed Quality Gate passed for 'rspec-frontend'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Copy link

Quality Gate passed Quality Gate passed for 'rspec-tools'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Copy link
Contributor

@joke1196 joke1196 left a comment

Choose a reason for hiding this comment

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

LGTM!

@Seppli11 Seppli11 requested a review from joke1196 September 11, 2024 07:00
Copy link
Contributor

@joke1196 joke1196 left a comment

Choose a reason for hiding this comment

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

I have added a small comment, otherwise it looks good!

import safetensors

model = MyModel()
safetensors.torch.load_model(model, 'model.pth')
Copy link
Contributor

Choose a reason for hiding this comment

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

Here I would only provide the code that would fix the issue in the Noncompliant section. So I would remove the line 36, from this example. And if you want to showcase it, I would create a different code example, but frankly I do not think it is worth it here.

@Seppli11 Seppli11 force-pushed the rule/add-RSPEC-S6985 branch from 479a4be to b9481e5 Compare September 17, 2024 12:49
@Seppli11 Seppli11 marked this pull request as ready for review September 17, 2024 12:50
Copy link

Quality Gate passed Quality Gate passed for 'rspec-tools'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Copy link

Quality Gate passed Quality Gate passed for 'rspec-frontend'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

@Seppli11 Seppli11 merged commit 7f75840 into master Sep 17, 2024
9 of 10 checks passed
@Seppli11 Seppli11 deleted the rule/add-RSPEC-S6985 branch September 17, 2024 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants