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

Populate detectionTool metadata for Sonar codemods #346

Merged
merged 17 commits into from
Apr 18, 2024

Conversation

carlosu7
Copy link
Contributor

add finding to sonar codemods and add assertion to check if FixOnlyCodeChanger has findings

Issue #337

@carlosu7 carlosu7 requested a review from nahsra April 11, 2024 14:51
@@ -133,7 +133,8 @@ public CodemodFileScanningResult visit(
if (regionNodeMatcher.matches(region, range)) {
ChangesResult changeSuccessful = onResultFound(context, cu, (T) node, result);
if (changeSuccessful.areChangesApplied()) {
final Optional<FixedFinding> optionalFixedFinding = getFixedFinding();
final Optional<FixedFinding> optionalFixedFinding =
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not the rule ID we want, but the finding ID (or "issue" ID in Sonar's parlance. Maybe this isn't clear, I would check the JavaDoc for this method and make sure it's good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the sonar issue ID is set correctly at SonarPluginJavaParserChanger.java line 85

this is for the sarif plugin

right now SemgrepSarifJavaParserChanger extends from SarifPluginFixOnlyCodeChanger that extends from SarifPluginJavaParserChanger

when runing the only rule Semgrep codemod: SemgrepOverlyPermissiveFilePermissionsCodemod, the semgrep provided rule id is: "java.lang.security.audit.overly-permissive-file-permission.overly-permissive-file-permission" which is the same as result.RuleId

Copy link
Contributor

Choose a reason for hiding this comment

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

We don' want the rule ID. We want the finding ID.

Copy link
Contributor Author

@carlosu7 carlosu7 Apr 15, 2024

Choose a reason for hiding this comment

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

@nahsra

taking the DefectDojo codemod as reference:

the finding ID is taken from the RuleFindings findings argument of the injected constructor

in the case of the semgrep codemod, i see that the argument type of the injected constructor is RuleSarif sarif. I'm not sure if the Result::guid would be the desired value.

RuleSarif sarif has a method called getResultsByLocationPath which returns a List, and the javadoc for this method says: "Get all the SARIF results with the matching path for the first location field"

And Result class has the guid field that says: "A stable, unique identifer for the result in the form of a GUID."

So what I'm trying to understand is if this Result::guid for the Sarif codemods is similar to the Finding::id for the defect dojo codemods given the fact that the Finding class javadoc says: "Represents a finding in DefectDojo."

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it's usually either guid or correlationGuid. However, the DefectDojo ID, not the underlying Semgrep ID, is the one that's valuable, because that's where we need to link the user to see the result.

This logic being here is wrong because SarifPluginJavaParserChanger isn't necessarily a FixOnlyChanger, so it has no idea where to find the finding ID, and thus, maybe it doesn't know how to make a CodemodChange for all subtypes. This inheritance isn't working, so we should favor composition. The first thought to me is to create a new interface CodemodChangeFactory, and have have the subclasses inject one into the constructor if they want a different behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nahsra should core-codemods/src/test/resources/semgrep-overly-permissive-file-permission/out.sarif file have the tool vendor's ID property ?

Copy link
Contributor

Choose a reason for hiding this comment

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

OOF -- it doesn't have one. I did not see that coming. I don't even know what to do in that scenario.

I think we have two options:

  • make our own "composite ID", maybe rule ID + location + line
  • match fingerprint ID

I feel like either is fine, because both are temporary. I'd prefer match fingerprint ID.

@@ -133,7 +133,8 @@ public CodemodFileScanningResult visit(
if (regionNodeMatcher.matches(region, range)) {
ChangesResult changeSuccessful = onResultFound(context, cu, (T) node, result);
if (changeSuccessful.areChangesApplied()) {
final Optional<FixedFinding> optionalFixedFinding = getFixedFinding();
final Optional<FixedFinding> optionalFixedFinding =
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it's usually either guid or correlationGuid. However, the DefectDojo ID, not the underlying Semgrep ID, is the one that's valuable, because that's where we need to link the user to see the result.

This logic being here is wrong because SarifPluginJavaParserChanger isn't necessarily a FixOnlyChanger, so it has no idea where to find the finding ID, and thus, maybe it doesn't know how to make a CodemodChange for all subtypes. This inheritance isn't working, so we should favor composition. The first thought to me is to create a new interface CodemodChangeFactory, and have have the subclasses inject one into the constructor if they want a different behavior.

// TODO
fixOnlyCodeChangerInformtionOptional
.get()
.buildFixedFinding(result.getFingerprints().toString())));
Copy link
Contributor Author

@carlosu7 carlosu7 Apr 16, 2024

Choose a reason for hiding this comment

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

@nahsra would this be OK since fingerprints is basically a map of strings or do you prefer the other way you suggested: make our own "composite ID", maybe rule ID + location + line ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yuck, I hate both. Let's use the fingerprint, but what you've got here doesn't look like it will be the string fingerprint value, it looks like it is a toString() of the array.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nahsra fingerprint is basically a Map<String, String> which represent a set of strings each of which individually defines a stable, unique identity for the result, do you want the string value of that map?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather just get the first value in the map (not the key, the value.)

@carlosu7 carlosu7 requested a review from nahsra April 16, 2024 07:41
Copy link
Contributor

@nahsra nahsra left a comment

Choose a reason for hiding this comment

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

Needs a lot more design love. Don't rush to get another revision. Take your time, draw it out.


import io.codemodder.codetf.DetectorRule;
import io.codemodder.codetf.FixedFinding;

Copy link
Contributor

Choose a reason for hiding this comment

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

JavaDoc


DetectorRule detectorRule();

default FixedFinding buildFixedFinding(String id) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The scope of this type is very strange. It's primarily a model, but it also looks like a Factory type -- except the Factory looks like it's just a utility that won't be very different.

I think you've now got a much better idea of what information needs to be where, but these patterns are not working for me.

// TODO
fixOnlyCodeChangerInformtionOptional
.get()
.buildFixedFinding(result.getFingerprints().toString())));
Copy link
Contributor

Choose a reason for hiding this comment

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

Yuck, I hate both. Let's use the fingerprint, but what you've got here doesn't look like it will be the string fingerprint value, it looks like it is a toString() of the array.

@carlosu7 carlosu7 requested a review from nahsra April 17, 2024 20:00
Copy link
Contributor

@nahsra nahsra left a comment

Choose a reason for hiding this comment

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

Minor nitpick -- please fix then we're good to go.

Copy link

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@carlosu7 carlosu7 merged commit 380a8a9 into main Apr 18, 2024
5 checks passed
@carlosu7 carlosu7 deleted the sonar-detectiontool-metadata branch April 18, 2024 01:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants