如何解决如何在 SAST Checkmarx 之后解决 API 控制器中的 XSRF 跨站请求伪造 (CSRF)
我已经使用 Checkmarx 工具完成了我的项目 java spring boot 的扫描。 该工具发现了大约 23 个中等严重性的 XSRF 出现。
发现的问题已在 @RequestBody List<String> lineups
上的 Rest API 方法 POST 上标记
附上描述结果的截图:
@RequestMapping(value = "/rules/lineup/{ruleMode}",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> getRulesByRuleModeAndLineup(@PathVariable Integer ruleMode,@RequestBody List<String> lineups) throws Exception {
LOGGER.info("[getRulesByRuleModeAndLineup] ENTER type: " + ruleMode + " lineup: " + lineups);
ResponseEntity<Object> output = null;
List<Rule> rules = new ArrayList<Rule>();
try {
for (String lineup : lineups) {
String lineupSanitized = HtmlUtils.htmlEscape(lineup);
rules.addAll(uiService.getRulesByRuleModeAndLineup(ruleMode,lineupSanitized));
}
output = new ResponseEntity<>(rules,HttpStatus.OK);
} catch (Exception e) {
LOGGER.error(e,e);
output = new ResponseEntity<>("An error occurred: " + e.getMessage() + "'",HttpStatus.INTERNAL_SERVER_ERROR);
}
return output;
}
是否有解决问题的示例修复程序?
解决方法
您可以选择对该字段进行验证。 你有两个选择:
- 使用已经内置的框架,我推荐 spring 为了这: https://docs.spring.io/spring-security/site/docs/3.2.0.CI-SNAPSHOT/reference/html/csrf.html
- 尝试将您希望在 api 上接受的字符串列入白名单,如果您 可以这样做。
第二个意味着您有责任使您的列表保持最新。
,我试图实现你在回答中提到的第一点,就像后面的代码
后端:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private static final Logger LOGGER = LogManager.getLogger(SecurityConfig.class);
@SuppressWarnings("javadoc")
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").permitAll().anyRequest().authenticated();
http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
http.headers().httpStrictTransportSecurity();
}
}
前端侧
我在下面的代码中实现了一个 xhrRequestHandler 文件名 js:
XMLHttpRequest.prototype.origOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function () {
this.origOpen.apply(this,arguments);
if (document.cookie) {
var csrfToken = document.cookie.match(new RegExp(`XSRF-TOKEN=([^;]+)`))[1];
if (csrfToken) {
this.setRequestHeader("X-XSRF-TOKEN",csrfToken);
}
}
if (arguments[1].includes("socket/info")) {
this.setRequestHeader('X-Requested-With','XMLHttpRequest');
}
};
并在 index.jsp 上
我使用以下代码导入了上面定义的文件js:
<script src="<c:url value="/resources/js/xhrRequestHandler.js"/>"></script>
并添加了元名称行:
<meta name="_csrf" content="${_csrf.token}"/>
<meta name="_csrf_header" content="${_csrf.headerName}"/>
但我没有解决问题
有什么问题吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。