Compare commits

..

3 Commits

  1. 2
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/CxxLanguage.java
  2. 2
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/CxxPlugin.java
  3. 48
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/CxxSonarWayProfile.java
  4. 976
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/CxxSquidSensor.java
  5. 9
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/SubscriptionAstVisitor.java
  6. 2
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/SecurityDesignRuleRepository.java
  7. 4
      sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/CxxLanguageTest.java
  8. 24
      sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/CxxPluginTest.java
  9. 2
      sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/JavaSecurityDesignWayProfile.java

@ -40,7 +40,7 @@ public class CxxLanguage extends AbstractLanguage {
/** /**
* Default cxx files knows suffixes * Default cxx files knows suffixes
*/ */
public static final String DEFAULT_FILE_SUFFIXES = "-"; public static final String DEFAULT_FILE_SUFFIXES = ".cxx,.cpp,.cc,.c,.hxx,.hpp,.hh,.h";
/** /**
* Settings of the plugin. * Settings of the plugin.

@ -64,9 +64,9 @@ public final class CxxPlugin implements Plugin {
// plugin elements // plugin elements
l.add(CxxLanguage.class); l.add(CxxLanguage.class);
l.add(CxxSonarWayProfile.class); l.add(CxxSonarWayProfile.class);
l.add(SecurityDesignWayProfile.class);
l.add(CxxRuleRepository.class); l.add(CxxRuleRepository.class);
l.add(SecurityDesignRuleRepository.class); l.add(SecurityDesignRuleRepository.class);
l.add(SecurityDesignWayProfile.class);
// reusable elements // reusable elements
l.addAll(getSensorsImpl()); l.addAll(getSensorsImpl());

@ -8,6 +8,7 @@ package com.keyware.sonar.cxx;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.keyware.sonar.cxx.rules.SecurityDesignRuleRepository;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition; import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
import org.sonar.cxx.checks.CheckList; import org.sonar.cxx.checks.CheckList;
import org.sonarsource.api.sonarlint.SonarLintSide; import org.sonarsource.api.sonarlint.SonarLintSide;
@ -23,34 +24,33 @@ import java.util.List;
@SonarLintSide @SonarLintSide
public class CxxSonarWayProfile implements BuiltInQualityProfilesDefinition { public class CxxSonarWayProfile implements BuiltInQualityProfilesDefinition {
private static String readResource(URL resource) { private static String readResource(URL resource) {
try { try {
return Resources.toString(resource, StandardCharsets.UTF_8); return Resources.toString(resource, StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException("Failed to read: " + resource, e); throw new IllegalStateException("Failed to read: " + resource, e);
}
} }
}
static Profile readProfile() { static Profile readProfile() {
URL resource = CxxSonarWayProfile.class.getResource("/org/sonar/l10n/cxx/rules/cxx/Sonar_way_profile.json"); URL resource = CxxSonarWayProfile.class.getResource("/org/sonar/l10n/cxx/rules/cxx/Sonar_way_profile.json");
return new Gson().fromJson(readResource(resource), Profile.class); return new Gson().fromJson(readResource(resource), Profile.class);
} }
@Override
public void define(Context context) {
var sonarWay = context.createBuiltInQualityProfile("Sonar way", CxxLanguage.KEY);
Profile jsonProfile = readProfile();
jsonProfile.ruleKeys.forEach((key) -> {
sonarWay.activateRule(CheckList.REPOSITORY_KEY, key);
});
sonarWay.done(); @Override
} public void define(Context context) {
var sonarWay = context.createBuiltInQualityProfile("Sonar way", CxxLanguage.KEY);
Profile jsonProfile = readProfile();
jsonProfile.ruleKeys.forEach((key) -> {
sonarWay.activateRule(CheckList.REPOSITORY_KEY, key);
});
sonarWay.done();
}
static class Profile { static class Profile {
public String name; public String name;
public List<String> ruleKeys; public List<String> ruleKeys;
} }
} }

@ -9,6 +9,7 @@ package com.keyware.sonar.cxx;
import com.sonar.cxx.sslr.api.AstNode; import com.sonar.cxx.sslr.api.AstNode;
import com.sonar.cxx.sslr.api.AstNodeType; import com.sonar.cxx.sslr.api.AstNodeType;
import com.sonar.cxx.sslr.api.Grammar; import com.sonar.cxx.sslr.api.Grammar;
import org.sonar.cxx.squidbridge.SquidAstVisitor;
import org.sonar.cxx.squidbridge.checks.SquidCheck; import org.sonar.cxx.squidbridge.checks.SquidCheck;
import java.util.List; import java.util.List;
@ -19,13 +20,15 @@ import java.util.List;
* @author GuoXin * @author GuoXin
* @date 2024/1/13 * @date 2024/1/13
*/ */
public abstract class SubscriptionAstVisitor extends SquidCheck<Grammar> { public abstract class SubscriptionAstVisitor extends SquidAstVisitor<Grammar> {
private SquidCheck<Grammar> checker;
/** /**
* 构造函数需要传入初代访问器 * 构造函数需要传入初代访问器
* *
* @param checker 初代规则检查器 * @param checker 初代规则检查器
*/ */
public SubscriptionAstVisitor(SquidCheck<Grammar> checker) { public SubscriptionAstVisitor(SquidCheck<Grammar> checker) {
this.checker = checker;
setContext(checker.getContext()); setContext(checker.getContext());
visitNodeTypes().forEach(this::subscribeTo); visitNodeTypes().forEach(this::subscribeTo);
} }
@ -95,7 +98,7 @@ public abstract class SubscriptionAstVisitor extends SquidCheck<Grammar> {
* @param messageParameters 可选消息参数请参阅 java.text.MessageFormat * @param messageParameters 可选消息参数请参阅 java.text.MessageFormat
*/ */
protected void reportIssue(String message, Object... messageParameters) { protected void reportIssue(String message, Object... messageParameters) {
getContext().createFileViolation(this, message, messageParameters); getContext().createFileViolation(checker, message, messageParameters);
} }
/** /**
@ -106,7 +109,7 @@ public abstract class SubscriptionAstVisitor extends SquidCheck<Grammar> {
* @param messageParameters 可选消息参数请参阅 java.text.MessageFormat * @param messageParameters 可选消息参数请参阅 java.text.MessageFormat
*/ */
protected void reportIssue(AstNode node, String message, Object... messageParameters) { protected void reportIssue(AstNode node, String message, Object... messageParameters) {
getContext().createLineViolation(this, message, node, messageParameters); getContext().createLineViolation(checker, message, node, messageParameters);
} }
} }

@ -25,7 +25,7 @@ public class SecurityDesignRuleRepository implements RulesDefinition {
public final static String REPOSITORY_NAME = "C++信息安全性设计准则"; public final static String REPOSITORY_NAME = "C++信息安全性设计准则";
// 规则检查器的集合,当有新的规则开发完毕后,需要添加到下面的集合中 // 规则检查器的集合,当有新的规则开发完毕后,需要添加到下面的集合中
private final List<Class> RULE_CHECKERS = Arrays.asList(ABCVarNameChecker.class); public final static List<Class> RULE_CHECKERS = Arrays.asList(ABCVarNameChecker.class);
@Override @Override
public void define(Context context) { public void define(Context context) {

@ -32,7 +32,7 @@ class CxxLanguageTest {
@Test @Test
void shouldReturnDefaultFileSuffixes1() { void shouldReturnDefaultFileSuffixes1() {
var cxx = new CxxLanguage(settings.asConfig()); var cxx = new CxxLanguage(settings.asConfig());
String[] expected = {"disabled"}; String[] expected = {".cxx", ".cpp", ".cc", ".c", ".hxx", ".hpp", ".hh", ".h"};
assertThat(cxx.getFileSuffixes()).contains(expected); assertThat(cxx.getFileSuffixes()).contains(expected);
} }
@ -40,7 +40,7 @@ class CxxLanguageTest {
void shouldReturnDefaultFileSuffixes2() { void shouldReturnDefaultFileSuffixes2() {
settings.setProperty(CxxLanguage.FILE_SUFFIXES_KEY, ""); settings.setProperty(CxxLanguage.FILE_SUFFIXES_KEY, "");
var cxx = new CxxLanguage(settings.asConfig()); var cxx = new CxxLanguage(settings.asConfig());
String[] expected = {"disabled"}; String[] expected = {".cxx", ".cpp", ".cc", ".c", ".hxx", ".hpp", ".hh", ".h"};
assertThat(cxx.getFileSuffixes()).contains(expected); assertThat(cxx.getFileSuffixes()).contains(expected);
} }

@ -18,17 +18,17 @@ import static org.assertj.core.api.Assertions.assertThat;
class CxxPluginTest { class CxxPluginTest {
@Test @Test
void testGetExtensions() throws Exception { void testGetExtensions() throws Exception {
SonarRuntime runtime = SonarRuntimeImpl.forSonarQube( SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(
Version.create(8, 6), Version.create(8, 6),
SonarQubeSide.SCANNER, SonarQubeSide.SCANNER,
SonarEdition.COMMUNITY SonarEdition.COMMUNITY
); );
var context = new Plugin.Context(runtime); var context = new Plugin.Context(runtime);
var plugin = new CxxPlugin(); var plugin = new CxxPlugin();
plugin.define(context); plugin.define(context);
assertThat(context.getExtensions()).hasSize(84); assertThat(context.getExtensions()).hasSize(85);
} }
} }

@ -21,7 +21,7 @@ public class JavaSecurityDesignWayProfile implements BuiltInQualityProfilesDefin
@Override @Override
public void define(Context context) { public void define(Context context) {
var way = context.createBuiltInQualityProfile("Java信息安全设计准则", "java"); var way = context.createBuiltInQualityProfile("Java信息安全设计准则", "java");
way.activateRule(JavaSecurityDesignRulesRepository.REPOSITORY_KEY, "ABCVarNameChecker"); way.activateRule(JavaSecurityDesignRulesRepository.REPOSITORY_KEY, "ABCVarNameChecker");
way.activateRule(JavaSecurityDesignRulesRepository.REPOSITORY_KEY, "AbsolutePathDetectorChecker"); way.activateRule(JavaSecurityDesignRulesRepository.REPOSITORY_KEY, "AbsolutePathDetectorChecker");
way.activateRule(JavaSecurityDesignRulesRepository.REPOSITORY_KEY, "DynamicCodeChecker"); way.activateRule(JavaSecurityDesignRulesRepository.REPOSITORY_KEY, "DynamicCodeChecker");

Loading…
Cancel
Save