From 1fc05be08f42dede5f48fb9ec5cbf69aca9e7607 Mon Sep 17 00:00:00 2001 From: wuhaoyang <2507865306@qq.com> Date: Thu, 11 Jan 2024 18:14:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=87=86=E5=88=99=EF=BC=9A?= =?UTF-8?q?=E7=A6=81=E6=AD=A2=E5=9C=A8=E6=8C=81=E4=B9=85cookie=E4=B8=AD?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E6=95=8F=E6=84=9F=E4=BF=A1=E6=81=AF=EF=BC=8C?= =?UTF-8?q?=E5=A6=82=E7=94=A8=E6=88=B7=E5=90=8D=E5=8F=A3=E4=BB=A4=E3=80=81?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E8=AE=BF=E9=97=AE=E8=AE=B0=E5=BD=95=E3=80=81?= =?UTF-8?q?=E8=BA=AB=E4=BB=BD=E7=89=B9=E5=BE=81=E4=BF=A1=E6=81=AF=E7=AD=89?= =?UTF-8?q?=EF=BC=8C=E6=88=96=E8=BF=9B=E8=A1=8C=E5=8D=95=E8=BA=AB=E6=95=A3?= =?UTF-8?q?=E5=88=97=E5=8A=A0=E5=AF=86=E5=A4=84=E7=90=86=E4=BB=A5=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E4=BF=A1=E6=81=AF=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CookieSensitiveParameterCheck.java | 106 ++++++++++++++++++ .../java/CookieSensitiveParameterCheck.html | 9 ++ .../java/CookieSensitiveParameterCheck.json | 13 +++ .../files/CookieSensitiveParameterCheck.java | 16 +++ .../CookieSensitiveParameterCheckTest.java | 30 +++++ 5 files changed, 174 insertions(+) create mode 100644 sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/CookieSensitiveParameterCheck.java create mode 100644 sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/CookieSensitiveParameterCheck.html create mode 100644 sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/CookieSensitiveParameterCheck.json create mode 100644 sonar-keyware-plugins-java/src/test/files/CookieSensitiveParameterCheck.java create mode 100644 sonar-keyware-plugins-java/src/test/java/com/keyware/sonar/java/rules/checkers/CookieSensitiveParameterCheckTest.java diff --git a/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/CookieSensitiveParameterCheck.java b/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/CookieSensitiveParameterCheck.java new file mode 100644 index 0000000..5c130d4 --- /dev/null +++ b/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/CookieSensitiveParameterCheck.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved. + * 项目名称:Java 信息安全性设计准则 + * 项目描述:用于检查Java源代码的安全性设计准则的Sonarqube插件 + * 版权说明:本软件属北京关键科技股份有限公司所有,在未获得北京关键科技股份有限公司正式授权情况下,任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。 + */ +package com.keyware.sonar.java.rules.checkers; + + +import org.sonar.plugins.java.api.semantic.Symbol; +import org.sonar.plugins.java.api.tree.*; +import org.sonar.check.Rule; +import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; + + +import java.util.*; + +@Rule(key = "CookieSensitiveParameterCheck") +public class CookieSensitiveParameterCheck extends IssuableSubscriptionVisitor { + + + + private static final Set SENSITIVE_COOKIE_PARAMS = new HashSet<>(Arrays.asList("password", "token", "secret")); + + @Override + public List nodesToVisit() { + return Arrays.asList(Tree.Kind.METHOD_INVOCATION, Tree.Kind.VARIABLE, Tree.Kind.STRING_LITERAL); + } + + @Override + public void visitNode(Tree tree) { + if (tree.is(Tree.Kind.METHOD_INVOCATION)) { + visitMethodInvocation((MethodInvocationTree) tree); + } else if (tree.is(Tree.Kind.VARIABLE)) { + visitVariable((VariableTree) tree); + } else if (tree.is(Tree.Kind.STRING_LITERAL)) { + visitStringLiteral((LiteralTree) tree); + } + } + + private void visitMethodInvocation(MethodInvocationTree methodInvocation) { + if (isCookieConstructor(methodInvocation)) { + checkCookieParameters(methodInvocation); + } + } + + private boolean isCookieConstructor(MethodInvocationTree methodInvocation) { + if (methodInvocation.methodSelect().is(Tree.Kind.MEMBER_SELECT)) { + MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) methodInvocation.methodSelect(); + return memberSelect.expression().symbolType().is("javax.servlet.http.Cookie"); + } + return false; + } + + private void visitVariable(VariableTree variableTree) { + ExpressionTree initializer = variableTree.initializer(); + if (initializer != null && initializer.is(Tree.Kind.NEW_CLASS)) { + checkCookieParameters(variableTree, ((NewClassTree) initializer).arguments()); + } + } + + private void checkCookieParameters(MethodInvocationTree methodInvocation) { + List arguments = methodInvocation.arguments(); + if (arguments != null && arguments.size() == 2) { + checkSensitiveParameter((ExpressionTree) arguments.get(0), "name", (Symbol) methodInvocation); + checkSensitiveParameter((ExpressionTree) arguments.get(1), "value", (Symbol) methodInvocation); + } + } + + private void checkCookieParameters(VariableTree variableTree, List arguments) { + Symbol variableSymbol = variableTree.symbol(); + if (arguments != null && arguments.size() == 2) { + checkSensitiveParameter(arguments.get(0), "name", variableSymbol); + checkSensitiveParameter(arguments.get(1), "value", variableSymbol); + } + } + + private void checkSensitiveParameter(ExpressionTree expression, String paramName, Symbol variableSymbol) { + if (expression.is(Tree.Kind.IDENTIFIER) && paramName.equals("name")) { + String variableName = ((IdentifierTree) expression).name(); + if (variableName.equals(variableSymbol.name()) || containsSensitiveParam(variableName)) { + System.out.println("Cookie参数设置中的name属性与局部变量一致: " + variableName); + reportIssue(expression, "Cookie参数设置中包含敏感字段"); + } + } else if (expression.is(Tree.Kind.IDENTIFIER) && paramName.equals("value")) { + String variableName = ((IdentifierTree) expression).name(); + if (variableName.equals(variableSymbol.name()) || containsSensitiveParam(variableName)) { + System.out.println("Cookie参数设置中的value属性与局部变量一致: " + variableName); + reportIssue(expression, "Cookie参数设置中包含敏感字段"); + } + } + } + + private void visitStringLiteral(LiteralTree literalTree) { + String literalValue = literalTree.value(); + if (containsSensitiveParam(literalValue)) { + System.out.println("Cookie参数设置中包含敏感字段: " + literalValue); + reportIssue(literalTree, "Cookie参数设置中包含敏感字段"); + } + } + + private boolean containsSensitiveParam(String value) { + String lowerCaseValue = value.toLowerCase(); + return SENSITIVE_COOKIE_PARAMS.stream().anyMatch(lowerCaseValue::contains); + } +} diff --git a/sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/CookieSensitiveParameterCheck.html b/sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/CookieSensitiveParameterCheck.html new file mode 100644 index 0000000..f021c1f --- /dev/null +++ b/sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/CookieSensitiveParameterCheck.html @@ -0,0 +1,9 @@ +

Cookie参数设置中包含敏感字段

+

Cookie参数设置中包含敏感字段

+
+
+
+

合规解决方案

+
+
+
diff --git a/sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/CookieSensitiveParameterCheck.json b/sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/CookieSensitiveParameterCheck.json new file mode 100644 index 0000000..b938bbc --- /dev/null +++ b/sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/CookieSensitiveParameterCheck.json @@ -0,0 +1,13 @@ +{ + "title": "Cookie参数设置中包含敏感字段", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "28suo" + ], + "defaultSeverity": "Minor" +} \ No newline at end of file diff --git a/sonar-keyware-plugins-java/src/test/files/CookieSensitiveParameterCheck.java b/sonar-keyware-plugins-java/src/test/files/CookieSensitiveParameterCheck.java new file mode 100644 index 0000000..2d69613 --- /dev/null +++ b/sonar-keyware-plugins-java/src/test/files/CookieSensitiveParameterCheck.java @@ -0,0 +1,16 @@ + +import javax.servlet.http.Cookie; + +class CookieSensitiveParameterCheck{ + + public void func1(){ + + String password = ""; + + Cookie invalidCookie1 = new Cookie("password", "1321"); // Noncompliant {{Cookie参数设置中包含敏感字段}} + Cookie invalidCookie2 = new Cookie(password, 1); // Noncompliant {{Cookie参数设置中包含敏感字段}} + Cookie invalidCookie3 = new Cookie("213", password); // Noncompliant {{Cookie参数设置中包含敏感字段}} + } + + +} \ No newline at end of file diff --git a/sonar-keyware-plugins-java/src/test/java/com/keyware/sonar/java/rules/checkers/CookieSensitiveParameterCheckTest.java b/sonar-keyware-plugins-java/src/test/java/com/keyware/sonar/java/rules/checkers/CookieSensitiveParameterCheckTest.java new file mode 100644 index 0000000..42700ad --- /dev/null +++ b/sonar-keyware-plugins-java/src/test/java/com/keyware/sonar/java/rules/checkers/CookieSensitiveParameterCheckTest.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved. + * 项目名称:Java 信息安全性设计准则 + * 项目描述:用于检查Java源代码的安全性设计准则的Sonarqube插件 + * 版权说明:本软件属北京关键科技股份有限公司所有,在未获得北京关键科技股份有限公司正式授权情况下,任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。 + */ +package com.keyware.sonar.java.rules.checkers; + +import com.keyware.sonar.java.utils.FilesUtils; +import org.junit.jupiter.api.Test; +import org.sonar.java.checks.verifier.CheckVerifier; + +/** + * TODO ABCVarNameCheckerTest + * + * @author GuoXin + * @date 2024/1/6 + */ +public class CookieSensitiveParameterCheckTest { + + @Test + void detected() { + + CheckVerifier.newVerifier() + .onFile("src/test/files/CookieSensitiveParameterCheck.java") + .withCheck(new CookieSensitiveParameterCheck()) + .withClassPath(FilesUtils.getClassPath("target/test-jars")) + .verifyIssues(); + } +}