From 780e0b1ffbc6c725d3f19bce70fc7ad1f4cdfcc7 Mon Sep 17 00:00:00 2001 From: Said MOHAMEDALI Date: Wed, 5 Apr 2023 16:06:55 +0200 Subject: [PATCH 01/14] adding test file for Cookie without ExpirationCheck --- .../test/files/CookieWithoutExpirationCheck.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 java-plugin/src/test/files/CookieWithoutExpirationCheck.java diff --git a/java-plugin/src/test/files/CookieWithoutExpirationCheck.java b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java new file mode 100644 index 000000000..de5c91cbb --- /dev/null +++ b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java @@ -0,0 +1,16 @@ +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + +class CustomerDataCopyCheck { + + public void NOK_CookieCreation() { + // create objet cookie  + Cookie C = new Cookie("id","674684641"); + // set the validity + C.setMaxAge(24*3600); + } + + + +} \ No newline at end of file From 5983df471068be0c384a1951298fc5994492b007 Mon Sep 17 00:00:00 2001 From: Said MOHAMEDALI Date: Wed, 5 Apr 2023 16:15:27 +0200 Subject: [PATCH 02/14] adding CookieWithoutExpirationRule --- .../checks/CookieWithoutExpirationRule.java | 102 ++++++++++++++++++ .../files/CookieWithoutExpirationCheck.java | 3 - 2 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java new file mode 100644 index 000000000..61d75d1b9 --- /dev/null +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java @@ -0,0 +1,102 @@ +package fr.greencodeinitiative.java.checks; + + +import org.sonar.check.Priority; +import org.sonar.check.Rule; +import org.sonar.java.model.expression.NewArrayTreeImpl; +import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; +import org.sonar.plugins.java.api.tree.*; + + +import java.util.Collections; +import java.util.List; + + +@Rule( + key = "CRJVM207-JAVA", + name = "Developpement", + description = CookieWithoutExpirationRule.MESSAGERULE, + priority = Priority.MINOR, + tags = {"bug"}) +public class CookieWithoutExpirationRule extends IssuableSubscriptionVisitor { + + protected static final String MESSAGERULE = "Customer data must have end-of-life information"; + + private static final String COOKIE_CLASS_NAME = "javax.servlet.http.Cookie"; + private static final String SET_MAX_AGE_METHOD_NAME = "setMaxAge"; + + @Override + public List nodesToVisit() { + return Collections.singletonList(Tree.Kind.CLASS); + } + + @Override + public void visitNode(Tree tree) { + ClassTree classTree = (ClassTree) tree; + + + + for (Tree member : classTree.members()) { + if (member.is(Tree.Kind.METHOD)) { + MethodInvocationTree methodInvocation = getMethodInvocation(member); + if (methodInvocation != null && isCookieConstructor(methodInvocation)) { + checkCookieExpiration(methodInvocation); + } + } + } + } + + + + private MethodInvocationTree getMethodInvocation(Tree tree) { + + //LOGGER.debug("--------------------_____-----_____----- AvoidGettingSizeCollectionInLoop.visitNode METHOD - BEGIN"); + if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) { + + ExpressionTree children = ((AssignmentExpressionTree)tree).variable(); + if(children.is(Tree.Kind.IDENTIFIER)) + { + IdentifierTree current = (IdentifierTree) children; + current.name().equals("Cookie"); + return (MethodInvocationTree) tree; + } + } + return null; + } + + + + private boolean isCookieConstructor(MethodInvocationTree methodInvocation) { + ExpressionTree methodSelect = methodInvocation.methodSelect(); + if (methodSelect.is(Tree.Kind.IDENTIFIER)) { + IdentifierTree identifier = (IdentifierTree) methodSelect; + return identifier.name().equals("Cookie"); + } + return false; + } + + + + private void checkCookieExpiration(MethodInvocationTree methodInvocation) { + List arguments = methodInvocation.arguments(); + for (ExpressionTree argument : arguments) { + if (argument.symbolType().fullyQualifiedName().equals(COOKIE_CLASS_NAME)) { + checkCookieExpirationArgument(argument); + } + } + } + + + + private void checkCookieExpirationArgument(ExpressionTree argument) { + boolean hasExpiration = false; + + + + + + if (!hasExpiration) { + reportIssue(argument, "Le cookie est créé sans date d'expiration."); + } + } +} \ No newline at end of file diff --git a/java-plugin/src/test/files/CookieWithoutExpirationCheck.java b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java index de5c91cbb..8c715e0ef 100644 --- a/java-plugin/src/test/files/CookieWithoutExpirationCheck.java +++ b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java @@ -10,7 +10,4 @@ public void NOK_CookieCreation() { // set the validity C.setMaxAge(24*3600); } - - - } \ No newline at end of file From 80260e35d339e921f325b5ecad456b8f3bce6253 Mon Sep 17 00:00:00 2001 From: Said MOHAMEDALI Date: Wed, 5 Apr 2023 16:33:18 +0200 Subject: [PATCH 03/14] adding CookieWithoutExpiration Test case --- .../files/CookieWithoutExpirationCheck.java | 4 ++-- .../checks/CookieWithExpirationCheckTest.java | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 java-plugin/src/test/java/fr/greencodeinitiative/java/checks/CookieWithExpirationCheckTest.java diff --git a/java-plugin/src/test/files/CookieWithoutExpirationCheck.java b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java index 8c715e0ef..8adf94040 100644 --- a/java-plugin/src/test/files/CookieWithoutExpirationCheck.java +++ b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java @@ -2,10 +2,10 @@ import java.util.Collection; import java.util.Collections; -class CustomerDataCopyCheck { +class TestClass { public void NOK_CookieCreation() { - // create objet cookie  + // create objet cookie Cookie C = new Cookie("id","674684641"); // set the validity C.setMaxAge(24*3600); diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/CookieWithExpirationCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/CookieWithExpirationCheckTest.java new file mode 100644 index 000000000..648f5927f --- /dev/null +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/CookieWithExpirationCheckTest.java @@ -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(); + } + +} \ No newline at end of file From e599e50180e63b8cc62a9059e26b1c78fc89abf8 Mon Sep 17 00:00:00 2001 From: Said MOHAMEDALI Date: Wed, 5 Apr 2023 16:57:31 +0200 Subject: [PATCH 04/14] adding MethodTree Exploration --- .../java/checks/CookieWithoutExpirationRule.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java index 61d75d1b9..fe4059d37 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java @@ -3,6 +3,7 @@ import org.sonar.check.Priority; import org.sonar.check.Rule; +import org.sonar.java.model.declaration.MethodTreeImpl; import org.sonar.java.model.expression.NewArrayTreeImpl; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.tree.*; @@ -50,8 +51,14 @@ public void visitNode(Tree tree) { private MethodInvocationTree getMethodInvocation(Tree tree) { - //LOGGER.debug("--------------------_____-----_____----- AvoidGettingSizeCollectionInLoop.visitNode METHOD - BEGIN"); - if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) { + List children = ((MethodTreeImpl)tree).children(); + for (Tree child : children) { + if (child instanceof MethodInvocationTree) { + return (MethodInvocationTree) child; + } + } + + /*if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) { ExpressionTree children = ((AssignmentExpressionTree)tree).variable(); if(children.is(Tree.Kind.IDENTIFIER)) @@ -60,7 +67,7 @@ private MethodInvocationTree getMethodInvocation(Tree tree) { current.name().equals("Cookie"); return (MethodInvocationTree) tree; } - } + }*/ return null; } From 824fa6bd8f283fa38059cd1a11245b33849764a0 Mon Sep 17 00:00:00 2001 From: Tisserand Benoit Date: Thu, 6 Apr 2023 09:18:54 +0200 Subject: [PATCH 05/14] parcours New Classe et SetMaxDate --- .../checks/CookieWithoutExpirationRule.java | 120 ++++++++---------- .../files/CookieWithoutExpirationCheck.java | 11 +- 2 files changed, 65 insertions(+), 66 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java index fe4059d37..f87ab8a2b 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java @@ -1,18 +1,18 @@ package fr.greencodeinitiative.java.checks; +import java.util.*; import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.java.model.declaration.MethodTreeImpl; +import org.sonar.java.model.declaration.VariableTreeImpl; +import org.sonar.java.model.expression.MemberSelectExpressionTreeImpl; import org.sonar.java.model.expression.NewArrayTreeImpl; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; +import org.sonar.plugins.java.api.semantic.MethodMatchers; import org.sonar.plugins.java.api.tree.*; -import java.util.Collections; -import java.util.List; - - @Rule( key = "CRJVM207-JAVA", name = "Developpement", @@ -26,84 +26,76 @@ public class CookieWithoutExpirationRule extends IssuableSubscriptionVisitor { private static final String COOKIE_CLASS_NAME = "javax.servlet.http.Cookie"; private static final String SET_MAX_AGE_METHOD_NAME = "setMaxAge"; + //TODO pour trouver les extend Cookie + private static final MethodMatchers REPOSITORY_METHOD = + MethodMatchers.create().ofSubTypes("SPRING_REPOSITORY").anyName().withAnyParameters() + .build(); + @Override public List nodesToVisit() { return Collections.singletonList(Tree.Kind.CLASS); } + private final CookieWithoutExpirationRuleCheckVisitor visitorInFile = new CookieWithoutExpirationRuleCheckVisitor(); + @Override public void visitNode(Tree tree) { - ClassTree classTree = (ClassTree) tree; - - - for (Tree member : classTree.members()) { - if (member.is(Tree.Kind.METHOD)) { - MethodInvocationTree methodInvocation = getMethodInvocation(member); - if (methodInvocation != null && isCookieConstructor(methodInvocation)) { - checkCookieExpiration(methodInvocation); - } - } + tree.accept(visitorInFile); + if (visitorInFile.hasANewCookieWithoutMaxDate()) + { + reportIssue(tree, "RULE_MESSAGE"); } } - - - private MethodInvocationTree getMethodInvocation(Tree tree) { + private class CookieWithoutExpirationRuleCheckVisitor extends BaseTreeVisitor { - List children = ((MethodTreeImpl)tree).children(); - for (Tree child : children) { - if (child instanceof MethodInvocationTree) { - return (MethodInvocationTree) child; + @Override + public void visitReturnStatement(ReturnStatementTree tree) { + this.scan((Tree)tree.expression()); } - } - /*if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) { - - ExpressionTree children = ((AssignmentExpressionTree)tree).variable(); - if(children.is(Tree.Kind.IDENTIFIER)) + @Override + public void visitNewClass(NewClassTree tree) { + if (tree.identifier().toString().equals("Cookie")) { - IdentifierTree current = (IdentifierTree) children; - current.name().equals("Cookie"); - return (MethodInvocationTree) tree; + System.out.println(tree.toString()); } - }*/ - return null; - } - - - - private boolean isCookieConstructor(MethodInvocationTree methodInvocation) { - ExpressionTree methodSelect = methodInvocation.methodSelect(); - if (methodSelect.is(Tree.Kind.IDENTIFIER)) { - IdentifierTree identifier = (IdentifierTree) methodSelect; - return identifier.name().equals("Cookie"); - } - return false; - } - - - - private void checkCookieExpiration(MethodInvocationTree methodInvocation) { - List arguments = methodInvocation.arguments(); - for (ExpressionTree argument : arguments) { - if (argument.symbolType().fullyQualifiedName().equals(COOKIE_CLASS_NAME)) { - checkCookieExpirationArgument(argument); - } - } - } - - - - private void checkCookieExpirationArgument(ExpressionTree argument) { - boolean hasExpiration = false; + } + @Override + public void visitVariable(VariableTree tree) { + for (Tree children : ((VariableTreeImpl) tree).children()) + { + if (children.is(Tree.Kind.NEW_CLASS) + && ((IdentifierTree)((NewClassTree)children).identifier()).toString().equals("Cookie")) + { + this.newCookieVariableName.add(tree.simpleName().toString()); + } + } + //todo appel à super(); + } + private ArrayList hasSetMaxAgeForCookiesVariableName = new ArrayList<>(); + private ArrayList newCookieVariableName = new ArrayList<>(); + public boolean hasANewCookieWithoutMaxDate() + { + for (String variableName : newCookieVariableName ) + { + if (!hasSetMaxAgeForCookiesVariableName.contains(variableName)) + return true; + } + return false; + } + @Override + public void visitMethodInvocation(MethodInvocationTree tree) { + System.out.println(((MemberSelectExpressionTree)tree.methodSelect()).identifier().name()); + if (((MemberSelectExpressionTree)tree.methodSelect()).identifier().name().equals("setMaxAge")) + { + hasSetMaxAgeForCookiesVariableName.add(((MemberSelectExpressionTree)tree.methodSelect()).expression().toString()); + } + } - - if (!hasExpiration) { - reportIssue(argument, "Le cookie est créé sans date d'expiration."); - } - } + } } \ No newline at end of file diff --git a/java-plugin/src/test/files/CookieWithoutExpirationCheck.java b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java index 8adf94040..68cf7bd60 100644 --- a/java-plugin/src/test/files/CookieWithoutExpirationCheck.java +++ b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java @@ -6,8 +6,15 @@ class TestClass { public void NOK_CookieCreation() { // create objet cookie - Cookie C = new Cookie("id","674684641"); + Cookie A = new Cookie("id","674684641"); + } + + public void OK_CookieCreation() { + // create objet cookie + Cookie B = new Cookie("id","674684641"); // set the validity - C.setMaxAge(24*3600); + B.setMaxAge(24*3600); + } + } \ No newline at end of file From 6a86ad08be10d020221eeafb4313d9918aa3b38e Mon Sep 17 00:00:00 2001 From: Tisserand Benoit Date: Thu, 6 Apr 2023 11:27:55 +0200 Subject: [PATCH 06/14] corrections --- .../java/checks/CookieWithoutExpirationRule.java | 2 +- java-plugin/src/test/files/CookieWithoutExpirationCheck.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java index f87ab8a2b..db0eb1039 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java @@ -44,7 +44,7 @@ public void visitNode(Tree tree) { tree.accept(visitorInFile); if (visitorInFile.hasANewCookieWithoutMaxDate()) { - reportIssue(tree, "RULE_MESSAGE"); + reportIssue(tree, "Avoid not setting MaxAge"); } } diff --git a/java-plugin/src/test/files/CookieWithoutExpirationCheck.java b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java index 68cf7bd60..9ad43459b 100644 --- a/java-plugin/src/test/files/CookieWithoutExpirationCheck.java +++ b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java @@ -2,7 +2,7 @@ import java.util.Collection; import java.util.Collections; -class TestClass { +class TestClass {// Noncompliant {{Avoid not setting MaxAge}} public void NOK_CookieCreation() { // create objet cookie From 246bf4fd288a8566b897920a9bcd5d05e9e2d497 Mon Sep 17 00:00:00 2001 From: Tisserand Benoit Date: Thu, 6 Apr 2023 11:32:04 +0200 Subject: [PATCH 07/14] ajout d'un test --- .../src/test/files/CookieWithoutExpirationCheck.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/java-plugin/src/test/files/CookieWithoutExpirationCheck.java b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java index 9ad43459b..bb1bd9dbf 100644 --- a/java-plugin/src/test/files/CookieWithoutExpirationCheck.java +++ b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java @@ -17,4 +17,13 @@ public void OK_CookieCreation() { } + public void OK_CookieCreation2() { + // create objet cookie + Cookie C; + C = new Cookie("id","674684641"); + // set the validity + C.setMaxAge(24*3600); + + } + } \ No newline at end of file From 5364c386c1c560e6ae9cadbe621546cd50a788a9 Mon Sep 17 00:00:00 2001 From: j12t Date: Thu, 6 Apr 2023 11:49:44 +0200 Subject: [PATCH 08/14] cast before ! --- .../java/checks/CookieWithoutExpirationRule.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java index db0eb1039..55309b141 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java @@ -11,6 +11,7 @@ import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.semantic.MethodMatchers; import org.sonar.plugins.java.api.tree.*; +import org.sonar.plugins.java.api.tree.Tree.Kind; @Rule( @@ -89,11 +90,16 @@ public boolean hasANewCookieWithoutMaxDate() } @Override public void visitMethodInvocation(MethodInvocationTree tree) { - System.out.println(((MemberSelectExpressionTree)tree.methodSelect()).identifier().name()); - if (((MemberSelectExpressionTree)tree.methodSelect()).identifier().name().equals("setMaxAge")) - { - hasSetMaxAgeForCookiesVariableName.add(((MemberSelectExpressionTree)tree.methodSelect()).expression().toString()); - } + //System.out.println(((MemberSelectExpressionTree)tree.methodSelect()).identifier().name()); + + if (tree.methodSelect().is(Kind.MEMBER_SELECT)) { + MemberSelectExpressionTree member = (MemberSelectExpressionTree)tree.methodSelect(); + if (member.identifier().name().equals("setMaxAge")) + { + hasSetMaxAgeForCookiesVariableName.add(((MemberSelectExpressionTree)tree.methodSelect()).expression().toString()); + } + + } } From f43ffee48b9ef2f9b80166ebacfb6bb96d0685b6 Mon Sep 17 00:00:00 2001 From: Tisserand Benoit Date: Thu, 6 Apr 2023 11:56:07 +0200 Subject: [PATCH 09/14] =?UTF-8?q?am=C3=A9lioration=20et=20commentaire=20co?= =?UTF-8?q?de?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../checks/CookieWithoutExpirationRule.java | 52 +++++++++++-------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java index 55309b141..db2766251 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java @@ -19,10 +19,11 @@ name = "Developpement", description = CookieWithoutExpirationRule.MESSAGERULE, priority = Priority.MINOR, - tags = {"bug"}) + tags = {"smell"}) + public class CookieWithoutExpirationRule extends IssuableSubscriptionVisitor { - protected static final String MESSAGERULE = "Customer data must have end-of-life information"; + protected static final String MESSAGERULE = "Customer data must have end-of-life information, so cookies must have a maxAge"; private static final String COOKIE_CLASS_NAME = "javax.servlet.http.Cookie"; private static final String SET_MAX_AGE_METHOD_NAME = "setMaxAge"; @@ -43,63 +44,68 @@ public List nodesToVisit() { 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 hasSetMaxAgeForCookiesVariableName = new ArrayList<>(); + // storage of variable name for New Cookies + private ArrayList newCookieVariableName = new ArrayList<>(); @Override public void visitReturnStatement(ReturnStatementTree tree) { this.scan((Tree)tree.expression()); } - @Override - public void visitNewClass(NewClassTree tree) { - if (tree.identifier().toString().equals("Cookie")) - { - System.out.println(tree.toString()); - } - } @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); } - //todo appel à super(); } - private ArrayList hasSetMaxAgeForCookiesVariableName = new ArrayList<>(); - private ArrayList newCookieVariableName = new ArrayList<>(); + 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) { - //System.out.println(((MemberSelectExpressionTree)tree.methodSelect()).identifier().name()); - - if (tree.methodSelect().is(Kind.MEMBER_SELECT)) { - MemberSelectExpressionTree member = (MemberSelectExpressionTree)tree.methodSelect(); - if (member.identifier().name().equals("setMaxAge")) - { - hasSetMaxAgeForCookiesVariableName.add(((MemberSelectExpressionTree)tree.methodSelect()).expression().toString()); - } - - } + //lors de la visite d'une méthode + if (tree.methodSelect().is(Kind.MEMBER_SELECT)) { + + if (((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()); + } + } + } From bd4c67607520aa7ee70315948e907de81d034aa7 Mon Sep 17 00:00:00 2001 From: Said MOHAMEDALI Date: Tue, 11 Apr 2023 17:10:43 +0200 Subject: [PATCH 10/14] Removing unused import --- .../java/checks/CookieWithoutExpirationRule.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java index db2766251..a90564662 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java @@ -4,10 +4,7 @@ import org.sonar.check.Priority; import org.sonar.check.Rule; -import org.sonar.java.model.declaration.MethodTreeImpl; import org.sonar.java.model.declaration.VariableTreeImpl; -import org.sonar.java.model.expression.MemberSelectExpressionTreeImpl; -import org.sonar.java.model.expression.NewArrayTreeImpl; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.semantic.MethodMatchers; import org.sonar.plugins.java.api.tree.*; @@ -25,9 +22,7 @@ public class CookieWithoutExpirationRule extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Customer data must have end-of-life information, so cookies must have a maxAge"; - private static final String COOKIE_CLASS_NAME = "javax.servlet.http.Cookie"; - private static final String SET_MAX_AGE_METHOD_NAME = "setMaxAge"; - + //TODO pour trouver les extend Cookie private static final MethodMatchers REPOSITORY_METHOD = MethodMatchers.create().ofSubTypes("SPRING_REPOSITORY").anyName().withAnyParameters() From f5af5413656d0f066fee0753d33c6f477fe58b05 Mon Sep 17 00:00:00 2001 From: Said MOHAMEDALI Date: Tue, 11 Apr 2023 17:12:13 +0200 Subject: [PATCH 11/14] TODO --- .../java/checks/CookieWithoutExpirationRule.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java index a90564662..6d63ab96b 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java @@ -22,8 +22,6 @@ public class CookieWithoutExpirationRule extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Customer data must have end-of-life information, so cookies must have a maxAge"; - - //TODO pour trouver les extend Cookie private static final MethodMatchers REPOSITORY_METHOD = MethodMatchers.create().ofSubTypes("SPRING_REPOSITORY").anyName().withAnyParameters() .build(); From abf6b0e4554926242893f3b0252724db3013dd6a Mon Sep 17 00:00:00 2001 From: smohamedali Date: Tue, 11 Apr 2023 17:32:25 +0200 Subject: [PATCH 12/14] correcting code smell --- .../java/checks/CookieWithoutExpirationRule.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java index 6d63ab96b..f769cee85 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java @@ -22,9 +22,6 @@ public class CookieWithoutExpirationRule extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Customer data must have end-of-life information, so cookies must have a maxAge"; - private static final MethodMatchers REPOSITORY_METHOD = - MethodMatchers.create().ofSubTypes("SPRING_REPOSITORY").anyName().withAnyParameters() - .build(); @Override public List nodesToVisit() { @@ -54,7 +51,7 @@ private class CookieWithoutExpirationRuleCheckVisitor extends BaseTreeVisitor { private ArrayList newCookieVariableName = new ArrayList<>(); @Override public void visitReturnStatement(ReturnStatementTree tree) { - this.scan((Tree)tree.expression()); + this.scan(tree.expression()); } @Override @@ -89,14 +86,12 @@ public boolean hasANewCookieWithoutMaxDate() @Override public void visitMethodInvocation(MethodInvocationTree tree) { //lors de la visite d'une méthode - if (tree.methodSelect().is(Kind.MEMBER_SELECT)) { - - if (((MemberSelectExpressionTree)tree.methodSelect()).identifier().name().equals("setMaxAge")) - { - + 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()); - } } } From 547937dd0119de2ba1e202cb9abf1d233a9497a2 Mon Sep 17 00:00:00 2001 From: smohamedali Date: Tue, 11 Apr 2023 17:38:21 +0200 Subject: [PATCH 13/14] correcting code smell --- .../java/checks/CookieWithoutExpirationRule.java | 1 - 1 file changed, 1 deletion(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java index f769cee85..2a769f2c4 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/CookieWithoutExpirationRule.java @@ -6,7 +6,6 @@ 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.semantic.MethodMatchers; import org.sonar.plugins.java.api.tree.*; import org.sonar.plugins.java.api.tree.Tree.Kind; From fe872fef1d460d4257f54c7a076bd04f4bb811ae Mon Sep 17 00:00:00 2001 From: smohamedali Date: Tue, 11 Apr 2023 17:52:04 +0200 Subject: [PATCH 14/14] correcting code coverage --- java-plugin/src/test/files/CookieWithoutExpirationCheck.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/java-plugin/src/test/files/CookieWithoutExpirationCheck.java b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java index bb1bd9dbf..e4af1f3c3 100644 --- a/java-plugin/src/test/files/CookieWithoutExpirationCheck.java +++ b/java-plugin/src/test/files/CookieWithoutExpirationCheck.java @@ -9,6 +9,11 @@ public void NOK_CookieCreation() { 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");