2023年6月26日发(作者:)
SonarQubeJava⾃定义规则编写分享背景当今的⼤部分公司都有对⾃⼰的业务代码进⾏安全性审计的需求,来保证业务代码的安全性,同时代码审计作为SDL中重要的⼀环,可有效保证业务的CIA。但是⼈⼯审计存在严重的性能瓶颈,单纯的使⽤代码扫描器效果也不尽如意,误报问题较多。⽬前较好的⽅法:结合业务,⾃定义规则,结合两者优势。但是⽹上关于这⽅⾯的介绍较少,希望本⽂章能帮助到有需求的同学。选择的扫描为SonarQube,这款扫描器是开源扫描器中较为出⾊的⼀款,有丰富的图像化界⾯和强⼤的语法解析能⼒。准备⼯作1. 下载并运⾏SonarQube,具体步骤请参考官⽹教程。2. 下载sonar-java插件源代码,这也是Java扫描规则集,我们会基于这个规则集编写我们⾃⼰的规则,下载地址:/SonarSource/sonar-javasonar-java插件关键结构java-checks模块:该模块包含最重要的JAVA扫描规则集java-frontend模块:该模块提供JAVA语法解析类,是该插件的基础⼀条规则的必要构成1. java-check中添加⼀条规则2. java-check test模块中添加测试⽤例3. java-check resource模块中添加规则描述,包括⼀个html和⼀个json⽂件4. 在ist中注册规则⽰例解析我们先使⽤java-check中的⼀条扫描规则作为⽰例,先了解下如何编写和注册规则,规则路径如下:tMappingMethodPublicCheck先看规则本体:package ;import ;import tions;import ;import ;import leSubscriptionVisitor;import ;import Tree;import ;@Rule(key = "S3751")public class RequestMappingMethodPublicCheck extends IssuableSubscriptionVisitor { @Override public List<> nodesToVisit() { return tonList(); } private static final List A method with a So marking a sensitive method In addition to @RequestMapping
annotation part of a class annotated with @Controller
(directly or indirectly through ameta annotation - @RestController
from Spring Boot is a good example) will be called to handle matching web requests. That will happeneven if the method is private
, because Spring invokes such methods via reflection, without checking visibility. private
may seem like a good way to control how such code is called. Unfortunately, not all Springframeworks ignore visibility in this way. For instance, if you've tried to control web access to your sensitive, private
,@RequestMapping
method by marking it @Secured
... it will still be called, whether or not the user is authorized to accessit. That's because AOP proxies are not applied to non-public methods.@RequestMapping
, this rule also considers the annotations introduced in Spring Framework 4.3: @GetMapping
,@PostMapping
, @PutMapping
, @DeleteMapping
, @PatchMapping
.Noncompliant Code Example
@RequestMapping("/greet", method = GET)private String greet(String greetee) { // Noncompliant
Compliant Solution
@RequestMapping("/greet", method = GET)public String greet(String greetee) {
See
这两个⽂件都位于sonar-java/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/⽬录下,我们⾃定义的规则json和html⽂件也要放在该⽬录下,⽂件名为KEY_、KEY_。KEY在规则中使⽤@Rule注解定义。测试⽂件编写⼀条好的规则往往需要很多次测试,通过TDD(测试驱动开发)的⽅式来帮助我们写出⼀条好的规则。该⽰例规则的测试⽂件:tMappingMethodPublicCheckTestpackage ;import ;import eckVerifier;public class RequestMappingMethodPublicCheckTest { @Test public void test() { ("src/test/files/checks/spring/", new RequestMappingMethodPublicCheck()); NoIssueWithoutSemantic("src/test/files/checks/spring/", new RequestMappingMethodPublicCheck()); }}通过JavaCheckVerifier类中提供的⽅法,来启动我们的规则扫描⽂件。注册规则在ist中进⾏注册,使⽤add⽅法添加需要注册的规则⾃定义⽰例分享Struts2 S2-057检查规则说明:扫描项⽬中是否使⽤包含S2-057漏洞版本的struts2依赖package ;import ependencyCollector;import ck;import bleList;import ;import Utils;import ty;import ;import ckContext;import dAttribute;import ency;import le;import ;@Rule(key = "Struts2_S2_057Check")public class Struts2_S2_057Check implements PomCheck { @Override public void scanFile(PomCheckContext context) { List
@Rule(key = "Struts2_S2_057Check")该注解声明本条规则的Key2.
implements PomCheckpublic void scanFile(PomCheckContext context)本条规则实现PomCheck类,重写scanFile,这样插件和扫描器会⾃动解析⽂件,并将解析完后的pom⽂件语法树传递进来3.
strutsVerCompare ⽤来定义哪些版本的Struts2依赖存在漏洞之后在sonar-java/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/中新建Struts2_S2_057Check_和Struts2_S2_057Check_两个⽂件,⼤家可以参考其他规则来编写。最后在CheckList中进⾏注册。插件编译mvn clean package -==true 跳过签名检查可能遇到的问题编译时提⽰找不到maven2相关类,在IDE中将sonar-java/java-maven-model/target/generated-sources⽬录设置为Generated SourcesRoot2020年3⽉更新:有同学在使⽤⾃⼰插件时,sonarqube会报如下错误:lStateException: Name of rule [repository=squid, key=${YourRuleName}] is empty解决⽅法:将 java-checks/src/main/resources/org/sonar/l10n/java/rules/squid ⽬录下和⾃⼰规则对应的html以及json⽂件更名为${YourRuleName}_和${YourRuleName}_
发布者:admin,转转请注明出处:http://www.yc00.com/news/1687776463a43258.html
评论列表(0条)