Skip to content

Commit f0cb9a4

Browse files
committed
add filter to check referer
1 parent 86d2551 commit f0cb9a4

11 files changed

Lines changed: 227 additions & 144 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- [CSRF](https://github.com/JoyChou93/java-sec-code/wiki/CSRF)
3838
- [JSONP](https://github.com/JoyChou93/java-sec-code/wiki/JSONP)
3939
- [Actuators to RCE](https://github.com/JoyChou93/java-sec-code/wiki/Actuators-to-RCE)
40+
- [URL whitelist Bypass](https://github.com/JoyChou93/java-sec-code/wiki/URL-whtielist-Bypass)
4041
- [Others](https://github.com/JoyChou93/java-sec-code/wiki/others)
4142

4243

src/main/java/org/joychou/Application.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.boot.builder.SpringApplicationBuilder;
6+
import org.springframework.boot.web.servlet.ServletComponentScan;
67
import org.springframework.boot.web.support.SpringBootServletInitializer;
78
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
89

910

11+
@ServletComponentScan
1012
@SpringBootApplication
1113
// @EnableEurekaClient // 测试Eureka请打开注释,防止控制台一直有warning
1214
public class Application extends SpringBootServletInitializer {

src/main/java/org/joychou/CsrfAccessDeniedHandler.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313

1414
public class CsrfAccessDeniedHandler implements AccessDeniedHandler {
1515

16-
16+
/**
17+
* @desc 返回自定义拦截页面
18+
*/
1719
@Override
1820
public void handle(HttpServletRequest request, HttpServletResponse response,
1921
AccessDeniedException accessDeniedException) throws IOException, ServletException {
20-
response.setContentType(MediaType.TEXT_HTML_VALUE);
21-
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
22-
response.getWriter().write("CSRF check failed by JoyChou.");
22+
response.setContentType(MediaType.TEXT_HTML_VALUE); // content-type: text/html
23+
response.setStatus(HttpServletResponse.SC_FORBIDDEN); // 403 forbidden
24+
response.getWriter().write("CSRF check failed by JoyChou."); // response
2325
}
2426

2527
}

src/main/java/org/joychou/WebSecurityConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected void configure(HttpSecurity http) throws Exception {
3737
.requireCsrfProtectionMatcher(csrfRequestMatcher)
3838
.ignoringAntMatchers("/xxe/**", "/fastjon/**") // 不进行csrf校验的uri,多个uri使用逗号分隔
3939
.csrfTokenRepository(new CookieCsrfTokenRepository());
40+
// 自定义csrf校验失败的代码,默认是返回403错误页面
4041
http.exceptionHandling().accessDeniedHandler(new CsrfAccessDeniedHandler());
4142
// http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
4243
}

src/main/java/org/joychou/controller/CORS.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.joychou.controller;
22

3-
import org.joychou.utils.Security;
3+
import org.joychou.security.SecurityUtil;
44
import org.springframework.stereotype.Controller;
55
import org.springframework.web.bind.annotation.CrossOrigin;
66
import org.springframework.web.bind.annotation.RequestMapping;
@@ -52,11 +52,10 @@ private static String vuls3(HttpServletResponse response) {
5252
@ResponseBody
5353
private static String seccode(HttpServletRequest request, HttpServletResponse response) {
5454
String origin = request.getHeader("Origin");
55-
Security sec = new Security();
5655

5756
// 如果origin不为空并且origin不在白名单内,认定为不安全。
5857
// 如果origin为空,表示是同域过来的请求或者浏览器直接发起的请求。
59-
if ( origin != null && !sec.checkSafeUrl(origin, urlwhitelist) ) {
58+
if ( origin != null && !SecurityUtil.checkURLbyEndsWith(origin, urlwhitelist) ) {
6059
return "Origin is not safe.";
6160
}
6261
response.setHeader("Access-Control-Allow-Origin", origin);

src/main/java/org/joychou/controller/FileUpload.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.springframework.web.multipart.MultipartFile;
1010
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
1111

12+
import javax.imageio.ImageIO;
13+
import java.awt.image.BufferedImage;
1214
import java.io.File;
1315
import java.io.FileOutputStream;
1416
import java.io.IOException;
@@ -17,8 +19,6 @@
1719
import java.nio.file.Paths;
1820
import java.util.UUID;
1921

20-
import static org.joychou.utils.Security.isImage;
21-
2222

2323
/**
2424
* @author: JoyChou (joychou@joychou.org)
@@ -154,4 +154,16 @@ private File convert(MultipartFile multiFile) throws Exception {
154154
fos.close();
155155
return convFile;
156156
}
157+
158+
/**
159+
* @param file
160+
* @desc 判断文件内容是否是图片
161+
*/
162+
public static boolean isImage(File file) throws IOException {
163+
BufferedImage bi = ImageIO.read(file);
164+
if (bi == null) {
165+
return false;
166+
}
167+
return true;
168+
}
157169
}

src/main/java/org/joychou/controller/JSONP.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.joychou.controller;
22

3-
import org.joychou.utils.Security;
3+
import org.joychou.security.SecurityUtil;
44
import org.springframework.stereotype.Controller;
55
import org.springframework.web.bind.annotation.*;
66
import javax.servlet.http.HttpServletRequest;
@@ -17,7 +17,7 @@
1717
public class JSONP {
1818

1919
protected static String info = "{\"name\": \"JoyChou\", \"phone\": \"18200001111\"}";
20-
protected static String[] urlwhitelist = {"joychou.com", "joychou.me"};
20+
protected static String[] urlwhitelist = {"joychou.com", "joychou.org"};
2121

2222

2323
// http://localhost:8080/jsonp/referer?callback=test
@@ -31,19 +31,19 @@ private static String referer(HttpServletRequest request, HttpServletResponse re
3131
}
3232

3333
/**
34-
* Desc: 直接访问不限制Referer,非直接访问限制Referer (开发同学喜欢这样进行JSONP测试)
35-
* URL: http://localhost:8080/jsonp/emptyReferer?callback=test
34+
* 直接访问不限制Referer,非直接访问限制Referer (开发同学喜欢这样进行JSONP测试)
35+
* http://localhost:8080/jsonp/emptyReferer?callback=test
36+
*
3637
*/
3738
@RequestMapping("/emptyReferer")
3839
@ResponseBody
3940
private static String emptyReferer(HttpServletRequest request, HttpServletResponse response) {
4041
String referer = request.getHeader("referer");
4142
response.setHeader("Access-Control-Allow-Origin", "*");
42-
Security sec = new Security();
4343

4444
// 如果referer不为空,并且referer不在安全域名白名单内,return error
4545
// 导致空referer就会绕过校验。开发同学为了方便测试,不太喜欢校验空Referer
46-
if (null != referer && !sec.checkSafeUrl(referer, urlwhitelist)) {
46+
if (null != referer && !SecurityUtil.checkURLbyEndsWith(referer, urlwhitelist)) {
4747
return "error";
4848
}
4949

@@ -58,9 +58,8 @@ private static String sec(HttpServletRequest request, HttpServletResponse respon
5858
// JSONP的跨域设置
5959
response.setHeader("Access-Control-Allow-Origin", "*");
6060
String referer = request.getHeader("referer");
61-
Security sec = new Security();
6261

63-
if (!sec.checkSafeUrl(referer, urlwhitelist)) {
62+
if (!SecurityUtil.checkURLbyEndsWith(referer, urlwhitelist)) {
6463
return "error";
6564
}
6665

0 commit comments

Comments
 (0)