Skip to content

Commit 2dc528a

Browse files
committed
[Bug修复](master): 修复定时任务管理存在SQL注入漏洞问题
由于定时任务未对Bean进行过滤,导致攻击者可以从SpringContextHolder获得控制jdbcTemplate类,并使用getDeclaredMethod调用jdbcTemplate的queryForMap函数,从而执行任意sql命令。 修复后定时任务的 Bean 需要使用 @service 注解定义。
1 parent 22c3864 commit 2dc528a

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

eladmin-common/src/main/java/me/zhengjie/utils/SpringContextHolder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import org.springframework.context.ApplicationContext;
2222
import org.springframework.context.ApplicationContextAware;
2323
import org.springframework.core.env.Environment;
24+
import org.springframework.stereotype.Service;
2425
import java.util.ArrayList;
26+
import java.util.Arrays;
2527
import java.util.List;
2628

2729
/**
@@ -142,4 +144,13 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
142144
}
143145
SpringContextHolder.addCallback = false;
144146
}
147+
148+
/**
149+
* 获取 @Service 的所有 bean 名称
150+
* @return /
151+
*/
152+
public static List<String> getAllServiceBeanName() {
153+
return new ArrayList<>(Arrays.asList(applicationContext
154+
.getBeanNamesForAnnotation(Service.class)));
155+
}
145156
}

eladmin-system/src/main/java/me/zhengjie/modules/quartz/rest/QuartzJobController.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import me.zhengjie.modules.quartz.domain.QuartzJob;
2525
import me.zhengjie.modules.quartz.service.QuartzJobService;
2626
import me.zhengjie.modules.quartz.service.dto.JobQueryCriteria;
27+
import me.zhengjie.utils.SpringContextHolder;
2728
import org.springframework.data.domain.Pageable;
2829
import org.springframework.http.HttpStatus;
2930
import org.springframework.http.ResponseEntity;
@@ -84,6 +85,8 @@ public ResponseEntity<Object> createQuartzJob(@Validated @RequestBody QuartzJob
8485
if (resources.getId() != null) {
8586
throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
8687
}
88+
// 验证Bean是不是合法的,合法的定时任务 Bean 需要用 @Service 定义
89+
checkBean(resources.getBeanName());
8790
quartzJobService.create(resources);
8891
return new ResponseEntity<>(HttpStatus.CREATED);
8992
}
@@ -93,6 +96,8 @@ public ResponseEntity<Object> createQuartzJob(@Validated @RequestBody QuartzJob
9396
@PutMapping
9497
@PreAuthorize("@el.check('timing:edit')")
9598
public ResponseEntity<Object> updateQuartzJob(@Validated(QuartzJob.Update.class) @RequestBody QuartzJob resources){
99+
// 验证Bean是不是合法的,合法的定时任务 Bean 需要用 @Service 定义
100+
checkBean(resources.getBeanName());
96101
quartzJobService.update(resources);
97102
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
98103
}
@@ -123,4 +128,12 @@ public ResponseEntity<Object> deleteQuartzJob(@RequestBody Set<Long> ids){
123128
quartzJobService.delete(ids);
124129
return new ResponseEntity<>(HttpStatus.OK);
125130
}
131+
132+
private void checkBean(String beanName){
133+
// 避免调用攻击者可以从SpringContextHolder获得控制jdbcTemplate类
134+
// 并使用getDeclaredMethod调用jdbcTemplate的queryForMap函数,执行任意sql命令。
135+
if(!SpringContextHolder.getAllServiceBeanName().contains(beanName)){
136+
throw new BadRequestException("非法的 Bean,请重新输入!");
137+
}
138+
}
126139
}

eladmin-system/src/main/java/me/zhengjie/modules/quartz/task/TestTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
package me.zhengjie.modules.quartz.task;
1717

1818
import lombok.extern.slf4j.Slf4j;
19-
import org.springframework.stereotype.Component;
19+
import org.springframework.stereotype.Service;
2020

2121
/**
2222
* 测试用
2323
* @author Zheng Jie
2424
* @date 2019-01-08
2525
*/
2626
@Slf4j
27-
@Component
27+
@Service
2828
public class TestTask {
2929

3030
public void run(){

0 commit comments

Comments
 (0)