Skip to content

SONARJAVA-5909 FN on S3752 when @RequestMapping in class#5412

Merged
asya-vorobeva merged 3 commits intomasterfrom
asya/s3752-fn
Jan 29, 2026
Merged

SONARJAVA-5909 FN on S3752 when @RequestMapping in class#5412
asya-vorobeva merged 3 commits intomasterfrom
asya/s3752-fn

Conversation

@asya-vorobeva
Copy link
Contributor

@asya-vorobeva asya-vorobeva commented Jan 29, 2026

Fixed FN in case when we mix safe and unsafe methods on class and method. See SONARJAVA-5909.

@hashicorp-vault-sonar-prod
Copy link

hashicorp-vault-sonar-prod bot commented Jan 29, 2026

SONARJAVA-5909

}

private static boolean mixSafeAndUnsafeMethods(ExpressionTree requestMethodsAssignment) {
private boolean mixSafeAndUnsafeMethods(ExpressionTree requestMethodsAssignment) {

Choose a reason for hiding this comment

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

Nitpick: Could we rename the method to have a more interrogative name such as mixesSafeAndUnsafeMethods or isMixingSafeAndUnsafeMethods?

private boolean mixSafeAndUnsafeMethods(ExpressionTree requestMethodsAssignment) {
HttpMethodVisitor visitor = new HttpMethodVisitor();
requestMethodsAssignment.accept(visitor);
if (isClassVisited) {

Choose a reason for hiding this comment

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

We introduced the isClassVisited flag so that visiting the method=<VERBS> on a class or a method behaves slightly differently. Could we get rid of the field and use 2 separate methods instead?

Copy link
Contributor Author

@asya-vorobeva asya-vorobeva Jan 29, 2026

Choose a reason for hiding this comment

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

We can of course, but it will duplicate a code. I try to minimize such things. But if you think it will be better from maintainability view, we can. Another approach is to extract

if (isClassVisited) {
      classHasSafeMethods = visitor.hasSafeMethods;
      classHasUnsafeMethods = visitor.hasUnsafeMethods;
    } else {
      methodHasSafeMethods = visitor.hasSafeMethods;
      methodHasUnsafeMethods = visitor.hasUnsafeMethods;
    }

to a separate method.

Choose a reason for hiding this comment

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

In the spirit of minimizing state of the check, I would prefer that the isClassVisited flag be passed as a parameter or replaced by 2 methods. I have no particular opinion over which is best but it make maintanability of the rule a little easier

private static final String REQUEST_METHOD = "method";
public static final String MESSAGE = "Make sure allowing safe and unsafe HTTP methods is safe here.";

private boolean classHasSafeMethods = false;

Choose a reason for hiding this comment

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

We initialize these 4 new fields to false when an instance of the check is created (which should be once per module visited). But it looks like we don't reset them when moving from one file/class to another. Could we ensure to reset them when visiting a new class or entering a new file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For each new class (as well as for each new method) we recalculate these fields. So it shouldn't be a problem. (and as we see, corresponding sample works pretty well)

Choose a reason for hiding this comment

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

Indeed, it looks like we properly reset he values everytime in the current implementation. However, we have had issues in the past where after rework some checks no longer reset their state properly (we no longer passed through the reset path in some cases or the check would fail with a NPE on a file and then proceed to the next file with a invalid state). So we generally would explicitly reset the state on entry of the check. If you don't mind, I would prefer that reseting the state when entering visitNode to ease debugging

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank your for the changes, I added some more comments about patterns in managing the state of checks

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the quick turn aroudn, the PR LGTM!

@asya-vorobeva asya-vorobeva enabled auto-merge (squash) January 29, 2026 16:24
@asya-vorobeva asya-vorobeva merged commit fdb79da into master Jan 29, 2026
16 of 17 checks passed
@asya-vorobeva asya-vorobeva deleted the asya/s3752-fn branch January 29, 2026 16:30
@sonarqube-next
Copy link

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