77import org .springframework .web .bind .annotation .ResponseBody ;
88
99import javax .servlet .http .HttpServletRequest ;
10+ import java .net .URI ;
1011import java .net .URL ;
1112import java .util .regex .Matcher ;
1213import java .util .regex .Pattern ;
@@ -89,17 +90,21 @@ public String indexOf(HttpServletRequest request) throws Exception{
8990 }
9091 }
9192
92- // 安全代码
93- @ RequestMapping ("/seccode" )
93+ // URL类getHost方法被绕过造成的安全问题
94+ // 绕过姿势:http://localhost:8080/url/seccode?url=http://www.taobao.com%[email protected] /, URL类getHost为joychou.com 95+ // 直接访问http://www.taobao.com#@joychou.com/,浏览器请求的是www.taobao.com
96+ @ RequestMapping ("/url" )
9497 @ ResponseBody
95- public String seccode (HttpServletRequest request ) throws Exception {
98+ public String urlVul (HttpServletRequest request ) throws Exception {
9699 String url = request .getParameter ("url" );
100+ System .out .println ("url: " + url );
97101 URL u = new URL (url );
98102 // 判断是否是http(s)协议
99103 if (!u .getProtocol ().startsWith ("http" ) && !u .getProtocol ().startsWith ("https" )) {
100104 return "URL is not http or https" ;
101105 }
102106 String host = u .getHost ().toLowerCase ();
107+ System .out .println ("host: " + host );
103108 // 如果非顶级域名后缀会报错
104109 String rootDomain = InternetDomainName .from (host ).topPrivateDomain ().toString ();
105110
@@ -111,4 +116,29 @@ public String seccode(HttpServletRequest request) throws Exception{
111116 }
112117
113118
119+ // 安全代码
120+ @ RequestMapping ("/seccode" )
121+ @ ResponseBody
122+ public String seccode (HttpServletRequest request ) throws Exception {
123+ String url = request .getParameter ("url" );
124+ System .out .println ("url: " + url );
125+ URI uri = new URI (url );
126+ URL u = new URL (url );
127+ // 判断是否是http(s)协议
128+ if (!u .getProtocol ().startsWith ("http" ) && !u .getProtocol ().startsWith ("https" )) {
129+ return "URL is not http or https" ;
130+ }
131+ // 使用uri获取host
132+ String host = uri .getHost ().toLowerCase ();
133+ System .out .println ("host: " + host );
134+
135+ // 如果非顶级域名后缀会报错
136+ String rootDomain = InternetDomainName .from (host ).topPrivateDomain ().toString ();
137+
138+ if (rootDomain .equals (urlwhitelist )) {
139+ return "URL is legal" ;
140+ } else {
141+ return "URL is illegal" ;
142+ }
143+ }
114144}
0 commit comments