新增准则:要求用户使用具有足够复杂度的口令,口令长度不少于10位,且至少包含特殊字符、大写、小写、数字中的3种。

wuhaoyang
wuhaoyang 11 months ago
parent 336c00f8ba
commit a95fb68045
  1. 93
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/ValidatePasswordCheck.java
  2. 28
      sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/rules/checkers/ValidatePasswordCheckTest.java
  3. 7
      sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/ValidatePasswordCheck.cc

@ -0,0 +1,93 @@
/*
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved.
* 项目名称C++ 信息安全性设计准则
* 项目描述用于检查C++源代码的安全性设计准则的Sonarqube插件
* 版权说明本软件属北京关键科技股份有限公司所有在未获得北京关键科技股份有限公司正式授权情况下任何企业和个人不能获取阅读安装传播本软件涉及的任何受知识产权保护的内容
*/
package com.keyware.sonar.cxx.rules.checkers;
import com.sonar.cxx.sslr.api.AstNode;
import com.sonar.cxx.sslr.api.Grammar;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.cxx.squidbridge.checks.SquidCheck;
import java.util.List;
/**
* 检查口令长度不少于10位且至少包含特殊字符大写小写数字中的3种
*
* @author WuHaoYang
* @date 2024/1/13
*/
@Rule(key = "ValidatePasswordCheck", name = "口令不匹配足够复杂度", description = "口令不匹配足够复杂度", priority = Priority.INFO, tags = {"28suo"})
@ActivatedByDefault
@SqaleConstantRemediation("5min")
public class ValidatePasswordCheck extends SquidCheck<Grammar> {
private static final String passwordRegex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@#$%^&+=!]).{10,}$";
@Override
public void init() {
subscribeTo(CxxGrammarImpl.functionDefinition);
}
@Override
public void visitNode(AstNode astNode) {
AstNode functionNameNode = astNode.getFirstDescendant(CxxGrammarImpl.declaratorId);
String functionName = functionNameNode.getTokenOriginalValue();
//检查函数是否为main
if ("main".equals(functionName)) {
checkPasswordValidationInMain(astNode);
}
}
private void checkPasswordValidationInMain(AstNode mainFunctionNode) {
AstNode compoundStatement = mainFunctionNode.getFirstDescendant(CxxGrammarImpl.compoundStatement);
if (compoundStatement != null) {
List<AstNode> declarations = compoundStatement.getDescendants(CxxGrammarImpl.initDeclarator);
for (AstNode initDeclarator : declarations) {
AstNode declaratorId = initDeclarator.getFirstDescendant(CxxGrammarImpl.declaratorId);
if (declaratorId != null) {
String variableName = declaratorId.getTokenOriginalValue();
// 检查变量是否命名为password
if ("password".equalsIgnoreCase(variableName)) {
checkPasswordInitialization(initDeclarator);
}
}
}
}
}
private void checkPasswordInitialization(AstNode declaration) {
AstNode initializer = declaration.getFirstDescendant(CxxGrammarImpl.initializer);
if (initializer != null) {
// 获取初始化值的子节点
AstNode initializationValue = initializer.getLastChild();
if (initializationValue != null) {
String passwordValue = initializationValue.getTokenOriginalValue();
if (!passwordValue.matches(passwordRegex)) {
System.out.println("未通过正则校验的口令:"+passwordValue);
getContext().createLineViolation(this, "口令不匹配足够复杂度", initializationValue);
}
}
}
}
}

@ -0,0 +1,28 @@
/*
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved.
* 项目名称C++ 信息安全性设计准则
* 项目描述用于检查C++源代码的安全性设计准则的Sonarqube插件
* 版权说明本软件属北京关键科技股份有限公司所有在未获得北京关键科技股份有限公司正式授权情况下任何企业和个人不能获取阅读安装传播本软件涉及的任何受知识产权保护的内容
*/
package com.keyware.sonar.cxx.rules.checkers;
import com.keyware.sonar.cxx.CxxFileTesterHelper;
import org.junit.jupiter.api.Test;
import org.sonar.cxx.CxxAstScanner;
import org.sonar.cxx.squidbridge.api.SourceFile;
import org.sonar.cxx.squidbridge.checks.CheckMessagesVerifier;
import java.io.IOException;
public class ValidatePasswordCheckTest {
@Test
public void checkTest() throws IOException {
var checker = new ValidatePasswordCheck();
var tester = CxxFileTesterHelper.create("ValidatePasswordCheck.cc");
SourceFile file = CxxAstScanner.scanSingleInputFile(tester.asInputFile(), checker);
CheckMessagesVerifier.verify(file.getCheckMessages())
.next().atLine(5).withMessage("口令不匹配足够复杂度")
.noMore();
}
}

@ -0,0 +1,7 @@
#include <string>
using namespace std;
int main() {
string password = "1111111111"; // error
return 0;
}
Loading…
Cancel
Save