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

[JAVA] CRJVM207 - 10 MILLIARDS - Customer data must have end-of-life information #169

Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package fr.greencodeinitiative.java.checks;
import java.util.*;


import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.java.model.declaration.VariableTreeImpl;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.*;
import org.sonar.plugins.java.api.tree.Tree.Kind;


@Rule(
key = "CRJVM207-JAVA",
name = "Developpement",
description = CookieWithoutExpirationRule.MESSAGERULE,
priority = Priority.MINOR,
tags = {"smell"})

public class CookieWithoutExpirationRule extends IssuableSubscriptionVisitor {

protected static final String MESSAGERULE = "Customer data must have end-of-life information, so cookies must have a maxAge";


@Override
public List<Tree.Kind> nodesToVisit() {
return Collections.singletonList(Tree.Kind.CLASS);
}
private final CookieWithoutExpirationRuleCheckVisitor visitorInFile = new CookieWithoutExpirationRuleCheckVisitor();


@Override
public void visitNode(Tree tree) {

tree.accept(visitorInFile);
//Class visitor
if (visitorInFile.hasANewCookieWithoutMaxDate())
{
//if we found a cookie that maxDate is not initialized, we report issue
reportIssue(tree, "Avoid not setting MaxAge");
}
}

private class CookieWithoutExpirationRuleCheckVisitor extends BaseTreeVisitor {


// storage of variable name for setMaxAge
private ArrayList<String> hasSetMaxAgeForCookiesVariableName = new ArrayList<>();
// storage of variable name for New Cookies
private ArrayList<String> newCookieVariableName = new ArrayList<>();
@Override
public void visitReturnStatement(ReturnStatementTree tree) {
this.scan(tree.expression());
}

@Override
public void visitVariable(VariableTree tree) {
//when we visit variable affectation
for (Tree children : ((VariableTreeImpl) tree).children())
{
//if we found an affectation
if (children.is(Tree.Kind.NEW_CLASS)
&& ((IdentifierTree)((NewClassTree)children).identifier()).toString().equals("Cookie"))
{
//if this is a New Cookie affectation, we store the name of the variable
this.newCookieVariableName.add(tree.simpleName().toString());
}
else
super.visitVariable(tree);
}
}


public boolean hasANewCookieWithoutMaxDate()
{
//parcours des variables pour lesquelles on a fait un new Cookie
for (String variableName : newCookieVariableName )
{
if (!hasSetMaxAgeForCookiesVariableName.contains(variableName))
//si on n'a pas fait setMaxAge pour ces variables
return true;
}
return false;
}
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
//lors de la visite d'une méthode
if (tree.methodSelect().is(Kind.MEMBER_SELECT) &&
(((MemberSelectExpressionTree)tree.methodSelect()).identifier().name().equals("setMaxAge"))
)
{
//si on est sur un setMaxAge, on enregistre la variable qui est affectée
hasSetMaxAgeForCookiesVariableName.add(((MemberSelectExpressionTree)tree.methodSelect()).expression().toString());
}

}


}
}
34 changes: 34 additions & 0 deletions java-plugin/src/test/files/CookieWithoutExpirationCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

class TestClass {// Noncompliant {{Avoid not setting MaxAge}}

public void NOK_CookieCreation() {
// create objet cookie
Cookie A = new Cookie("id","674684641");
}

public Cookie NOK_ReturnCookieCreation() {
// create objet cookie
Cookie A = new Cookie("id","674684641");
return A;
}
public void OK_CookieCreation() {
// create objet cookie
Cookie B = new Cookie("id","674684641");
// set the validity
B.setMaxAge(24*3600);

}

public void OK_CookieCreation2() {
// create objet cookie
Cookie C;
C = new Cookie("id","674684641");
// set the validity
C.setMaxAge(24*3600);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fr.greencodeinitiative.java.checks;

import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
import org.sonar.plugins.java.api.JavaFileScanner;

class CookieWithExpirationCheckTest {

/**
* @formatter:off
*/
@Test
void test() {
CheckVerifier.newVerifier()
.onFile("src/test/files/CookieWithoutExpirationCheck.java")
.withCheck(new CookieWithoutExpirationRule())
.verifyIssues();
}

}