优化:优化误报问题

master
RenFengJiang 8 months ago
parent be9b9f2dfb
commit 867f67b964
  1. 2
      sonar-keyware-plugins-ConfigurationDetection/src/main/java/com/keyware/sonar/Configuration/ConfigurationFileLanguage.java
  2. 12
      sonar-keyware-plugins-ConfigurationDetection/src/main/java/com/keyware/sonar/Configuration/rules/checkers/SessionDateChecker.java
  3. 14
      sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/OptionsVerifyChecker.java
  4. 2
      sonar-keyware-plugins-java/src/test/files/options/OptionsVerifyOneRule.java

@ -21,7 +21,7 @@ public class ConfigurationFileLanguage extends AbstractLanguage {
public static final String NAME = "Configuration"; public static final String NAME = "Configuration";
public static final String KEY = "cfg"; public static final String KEY = "cfg";
public static final String FILE_SUFFIXES_KEY = "sonar.disposition.file.suffixes"; public static final String FILE_SUFFIXES_KEY = "sonar.disposition.file.suffixes";
public static final String FILE_SUFFIXES_DEFAULT_VALUE = ".properties,.ini,.conf,.xml,.yml,.json"; public static final String FILE_SUFFIXES_DEFAULT_VALUE = ".properties,.ini,.conf,.yml,.json";
private final Configuration config; private final Configuration config;

@ -29,9 +29,9 @@ import java.util.Scanner;
@Rule(key = "SessionDateChecker") @Rule(key = "SessionDateChecker")
public class SessionDateChecker implements ConfigCheck { public class SessionDateChecker implements ConfigCheck {
private boolean boo = true; private boolean boo = false;
public void execute(SensorContext context, InputFile inputFile, RuleKey ruleKey){ public void execute(SensorContext context, InputFile inputFile, RuleKey ruleKey){
if(boo){ if(!boo){
//文件名称 //文件名称
String filename = inputFile.filename(); String filename = inputFile.filename();
//校验文件后缀 //校验文件后缀
@ -41,8 +41,8 @@ public class SessionDateChecker implements ConfigCheck {
try (Scanner scanner = new Scanner(file)) { try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) { while (scanner.hasNextLine()) {
String line = scanner.nextLine(); String line = scanner.nextLine();
if (line.contains("server.servlet.session.timeout")) { if (!line.contains("server.servlet.session.timeout")) {
boo = false; boo = true;
break; break;
} }
} }
@ -62,8 +62,8 @@ public class SessionDateChecker implements ConfigCheck {
Map<String, Object> obj = yaml.load(fis); Map<String, Object> obj = yaml.load(fis);
if (obj != null){ if (obj != null){
String sessionTimeout = searchForSessionTimeout(obj, "server", "servlet", "session", "timeout"); String sessionTimeout = searchForSessionTimeout(obj, "server", "servlet", "session", "timeout");
if (sessionTimeout != null) { if (sessionTimeout == null) {
boo = false; boo = true;
} }
} }
} catch (IOException e) { } catch (IOException e) {

@ -27,7 +27,7 @@ import java.util.List;
@Rule(key = "OptionsVerifyChecker") @Rule(key = "OptionsVerifyChecker")
public class OptionsVerifyChecker extends IssuableSubscriptionVisitor implements EndOfAnalysis { public class OptionsVerifyChecker extends IssuableSubscriptionVisitor implements EndOfAnalysis {
private boolean boo = true; private boolean boo = false;
@Override @Override
public List<Tree.Kind> nodesToVisit() { public List<Tree.Kind> nodesToVisit() {
@ -36,7 +36,7 @@ public class OptionsVerifyChecker extends IssuableSubscriptionVisitor implements
@Override @Override
public void visitNode(Tree tree) { public void visitNode(Tree tree) {
if(boo) { if(!boo) {
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree; MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
ExpressionTree expressionTree = methodInvocationTree.methodSelect(); ExpressionTree expressionTree = methodInvocationTree.methodSelect();
if (expressionTree.is(Tree.Kind.MEMBER_SELECT)) { if (expressionTree.is(Tree.Kind.MEMBER_SELECT)) {
@ -44,19 +44,21 @@ public class OptionsVerifyChecker extends IssuableSubscriptionVisitor implements
if("addHeader".equals(selectExpressionTree.identifier().name()) || "setHeader".equals(selectExpressionTree.identifier().name())) { if("addHeader".equals(selectExpressionTree.identifier().name()) || "setHeader".equals(selectExpressionTree.identifier().name())) {
Arguments arguments = methodInvocationTree.arguments(); Arguments arguments = methodInvocationTree.arguments();
boolean one = false; boolean one = false;
boolean two = false; boolean two = true;
for (ExpressionTree argument : arguments) { for (ExpressionTree argument : arguments) {
if(argument.is(Tree.Kind.STRING_LITERAL)){ if(argument.is(Tree.Kind.STRING_LITERAL)){
String literalValue = ((LiteralTree) argument).value(); String literalValue = ((LiteralTree) argument).value();
if ("\"X-Frame-Options\"".equals(literalValue)) { if ("\"X-Frame-Options\"".equals(literalValue)) {
one = true; one = true;
} else if("\"DENY\"".equals(literalValue)){ } else if("\"DENY\"".equals(literalValue)){
two = true; two = false;
} }
} }
} }
if(one && two){ if(one){
boo = false; if(two){
boo = true;
}
} }
} }
} }

@ -11,7 +11,7 @@ public class OptionsVerifyOneRule extends OncePerRequestFilter {
@Override @Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
// response.setHeader("X-Frame-Options", "DENY"); // 或者使用SAMEORIGIN,ALLOW-FROM等其他策略 response.setHeader("X-Frame-Options", "SAMEORIGIN"); // 或者使用SAMEORIGIN,ALLOW-FROM等其他策略
filterChain.doFilter(request, response); filterChain.doFilter(request, response);
} }
} }

Loading…
Cancel
Save