{"id":87525,"date":"2020-05-08T13:42:05","date_gmt":"2020-05-08T05:42:05","guid":{"rendered":"http:\/\/4563.org\/?p=87525"},"modified":"2020-05-08T13:42:05","modified_gmt":"2020-05-08T05:42:05","slug":"springmvc-%e5%a2%9e%e5%8a%a0%e4%ba%86%e4%b8%80%e4%b8%aa-xss-%e8%bf%87%e6%bb%a4%e5%99%a8%ef%bc%8c%e5%af%bc%e8%87%b4-controller-%e4%b8%8a%e4%bc%a0%e7%9a%84%e6%96%87%e4%bb%b6%e4%b8%ba%e7%a9%ba","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=87525","title":{"rendered":"SpringMVC \u589e\u52a0\u4e86\u4e00\u4e2a xss \u8fc7\u6ee4\u5668\uff0c\u5bfc\u81f4 Controller \u4e0a\u4f20\u7684\u6587\u4ef6\u4e3a\u7a7a"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  SpringMVC \u589e\u52a0\u4e86\u4e00\u4e2a xss \u8fc7\u6ee4\u5668\uff0c\u5bfc\u81f4 Controller \u4e0a\u4f20\u7684\u6587\u4ef6\u4e3a\u7a7a               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : liugp5201314 <\/span>  <span><i><\/i> 10<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6700\u8fd1\u505a\u7684\u4e00\u4e2a\u9879\u76ee\u8fdb\u884c\u5b89\u5168\u6d4b\u8bd5\u65f6\u6d4b\u51fa\u4e86 SQL \u6ce8\u5165\u95ee\u9898\uff0c\u4e25\u91cd\u7ea7\u522b\u4e3a\u9ad8\u5371\uff0c\u600e\u4e48\u529e\u5462\uff1f\u6211\u8fd8\u662f\u4e2a\u96cf\uff0c\u8fd8\u6ca1\u5b66\u4f1a\u98de\u5462\uff0c\u6320\u6320\u5934\uff0c\u786c\u4e0a\u5427\uff0c\u7136\u540e\u628a\u4e4b\u524d\u9879\u76ee\u91cc\u7684 xss \u90fd\u5f04\u8fc7\u6765\u4fee\u4fee\u6539\u6539\uff0c\u7136\u540e\u8dd1\u8d77\u6765\uff0c\u9707\u60ca\u4e86\uff0c\u7adf\u7136\u5168\u90fd\u8fc7\u6ee4\u4e86\uff0c\u662f\u7684\uff0c\u5168\u90fd\u8fc7\u6ee4\u4e86\uff0c\u8fde\u4e0a\u4f20\u7684\u6587\u4ef6\u90fd\u7ed9\u6211\u8fc7\u6ee4\u4e86\uff0c\u548b\u529e\uff1f\u518d\u767e\u5ea6\uff0c\u7ed3\u679c\u5168\u662f\u5343\u7bc7\u4e00\u5f8b\u7684\u6284\u88ad\uff0c\u6ca1\u4e00\u4e2a\u80fd\u7528\u7684\uff0c\u8fd8\u662f\u53d1\u4e2a\u5e16\u5b50\u5927\u5bb6\u5e2e\u6211\u7785\u7785\uff0c\u770b\u770b\u600e\u4e48\u89e3\u51b3\u4e00\u4e0b\uff0c\u5934\u53d1\u90fd\u6320\u6389\u4e00\u5927\u628a\u4e86\uff0c\u542c\u8bf4\u690d\u53d1\u4e00\u6839\u4e8c\u5341\u5757\uff0c\u542c\u7740\u90fd\u5413\u4eba\u3002<\/p>\n<p>\u8fd9\u662f\u8c03\u7528\u8fc7\u6ee4\u5668\uff1a<\/p>\n<pre><code> ```java  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)     throws IOException, ServletException{ HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; String enctype = request.getContentType(); if (StringUtils.isNotBlank(enctype) &amp;&amp; enctype.contains(\"multipart\/form-data\")) {    final MultipartResolver multipartResolver = SpringUtil.getBean(\"multipartResolver\");    final MultipartHttpServletRequest multipartHttpServletRequest =        multipartResolver.resolveMultipart((HttpServletRequest) request);    chain.doFilter(new XssHttpServletRequestWrapper(multipartHttpServletRequest),   response); } else {      chain.doFilter(new XssHttpServletRequestWrapper((HttpServletRequest) request), response); } } <\/code><\/pre>\n<pre><code> \u8fd9\u662f\u91cd\u5199\u7684\u65b9\u6cd5\uff1a  ```java public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {     \/**      * @param request      *\/     public XssHttpServletRequestWrapper(HttpServletRequest request)     {         super(request);     }      \/**      * \u8986\u76d6 getHeader \u65b9\u6cd5\uff0c\u5c06\u53c2\u6570\u540d\u548c\u53c2\u6570\u503c\u90fd\u505a xss \u8fc7\u6ee4\u3002      * \u5982\u679c\u9700\u8981\u83b7\u5f97\u539f\u59cb\u7684\u503c\uff0c\u5219\u901a\u8fc7 super.getHeaders(name)\u6765\u83b7\u53d6      * getHeaderNames \u4e5f\u53ef\u80fd\u9700\u8981\u8986\u76d6      *\/     @Override     public String getHeader(String name) {          String value = super.getHeader(EscapeUtil.escape(name));         if (value != null) {             value = EscapeUtil.escape(value);         }         return value;     }      @Override     public String getParameter(String name){         String value = super.getParameter(name);         if (value != null)         {         String escapseValue = EscapeUtil.escape(value.trim());         return escapseValue;         }         return super.getParameter(name);     }      @Override     public String[] getParameterValues(String name)     {         String[] values = super.getParameterValues(name);         if (values != null)         {             int length = values.length;             String[] escapseValues = new String[length];             for (int i = 0; i &lt; length; i++)             {                 \/\/ \u9632 xss \u653b\u51fb\u548c\u8fc7\u6ee4\u524d\u540e\u7a7a\u683c                 escapseValues[i] = EscapeUtil.escape(values[i]).trim();             }             return escapseValues;         }         return super.getParameterValues(name);     }  } <\/code><\/pre>\n<p>\u8fd9\u662f\u8fc7\u6ee4\u89c4\u5219\uff1a<\/p>\n<pre><code>public class EscapeUtil {     public static final String RE_HTML_MARK = \"(&lt;[^&lt;]*?&gt;)|(&lt;[\\s]*?\/[^&lt;]*?&gt;)|(&lt;[^&lt;]*?\/[\\s]*?&gt;)\";      private static final char[][] TEXT = new char[64][];      static     {         for (int i = 0; i &lt; 64; i++)         {             TEXT[i] = new char[] { (char) i };         }          \/\/ special HTML characters         TEXT['''] = \"&amp;#039;\".toCharArray(); \/\/ \u5355\u5f15\u53f7         TEXT['\"'] = \"&amp;#34;\".toCharArray(); \/\/ \u5355\u5f15\u53f7         TEXT['&amp;'] = \"&amp;#38;\".toCharArray(); \/\/ &amp;\u7b26         TEXT['&lt;'] = \"&amp;#60;\".toCharArray(); \/\/ \u5c0f\u4e8e\u53f7         TEXT['&gt;'] = \"&amp;#62;\".toCharArray(); \/\/ \u5927\u4e8e\u53f7     }      \/**      * \u8f6c\u4e49\u6587\u672c\u4e2d\u7684 HTML \u5b57\u7b26\u4e3a\u5b89\u5168\u7684\u5b57\u7b26      *       * @param text \u88ab\u8f6c\u4e49\u7684\u6587\u672c      * @return \u8f6c\u4e49\u540e\u7684\u6587\u672c      *\/     public static String escape(String text)     {         return encode(text);     }      \/**      * \u8fd8\u539f\u88ab\u8f6c\u4e49\u7684 HTML \u7279\u6b8a\u5b57\u7b26      *       * @param content \u5305\u542b\u8f6c\u4e49\u7b26\u7684 HTML \u5185\u5bb9      * @return \u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32      *\/     public static String unescape(String content)     {         return decode(content);     }      \/**      * \u6e05\u9664\u6240\u6709 HTML \u6807\u7b7e\uff0c\u4f46\u662f\u4e0d\u5220\u9664\u6807\u7b7e\u5185\u7684\u5185\u5bb9      *       * @param content \u6587\u672c      * @return \u6e05\u9664\u6807\u7b7e\u540e\u7684\u6587\u672c      *\/     public static String clean(String content)     {         return new HTMLFilter().filter(content);     }      \/**      * Escape \u7f16\u7801      *       * @param text \u88ab\u7f16\u7801\u7684\u6587\u672c      * @return \u7f16\u7801\u540e\u7684\u5b57\u7b26      *\/     private static String encode(String text)     {         int len;         if ((text == null) || ((len = text.length()) == 0))         {             return StringUtils.EMPTY;         }         StringBuilder buffer = new StringBuilder(len + (len &gt;&gt; 2));         char c;         for (int i = 0; i &lt; len; i++)         {             c = text.charAt(i);             if (c &lt; 64)             {                 buffer.append(TEXT[c]);             }             else             {                 buffer.append(c);             }         }         return buffer.toString();     }      \/**      * Escape \u89e3\u7801      *       * @param content \u88ab\u8f6c\u4e49\u7684\u5185\u5bb9      * @return \u89e3\u7801\u540e\u7684\u5b57\u7b26\u4e32      *\/     public static String decode(String content)     {         if (StringUtils.isEmpty(content))         {             return content;         }          StringBuilder tmp = new StringBuilder(content.length());         int lastPos = 0, pos = 0;         char ch;         while (lastPos &lt; content.length())         {             pos = content.indexOf(\"%\", lastPos);             if (pos == lastPos)             {                 if (content.charAt(pos + 1) == 'u')                 {                     ch = (char) Integer.parseInt(content.substring(pos + 2, pos + 6), 16);                     tmp.append(ch);                     lastPos = pos + 6;                 }                 else                 {                     ch = (char) Integer.parseInt(content.substring(pos + 1, pos + 3), 16);                     tmp.append(ch);                     lastPos = pos + 3;                 }             }             else             {                 if (pos == -1)                 {                     tmp.append(content.substring(lastPos));                     lastPos = content.length();                 }                 else                 {                     tmp.append(content.substring(lastPos, pos));                     lastPos = pos;                 }             }         }         return tmp.toString();     }      public static void main(String[] args)     {         String html = \"&lt;script&gt;alert(1);&lt;\/script&gt;\";         \/\/ String html = \"&lt;scr&lt;script&gt;ipt&gt;alert(\"XSS\")&lt;\/scr&lt;script&gt;ipt&gt;\";         \/\/ String html = \"&lt;123\";         System.out.println(EscapeUtil.clean(html));         System.out.println(EscapeUtil.escape(html));         System.out.println(EscapeUtil.unescape(html));     } } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>11<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1443297\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : waa <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5728 controller \u65b9\u6cd5\u53c2\u6570\u4e2d\u76f4\u63a5\u6dfb\u52a0 MultipartHttpServletRequest \u5f62\u5f0f\u53c2\u6570\uff0c\u901a\u8fc7 multipartHttpservletRequest \u5bf9\u8c61\u83b7\u53d6 MultipartFile \u548c\u5176\u4f59\u7684\u8bf7\u6c42\u53c2\u6570\u3002\u6211\u7684\u662f\u8fd9\u6837\u89e3\u51b3\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1443298\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jzmws <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             forty \u6d4b\u8bd5\u7684 ??                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1443299\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : FreeEx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. sql \u6ce8\u5165\u4f60\u52a0 xss \u8fc7\u6ee4\u5668\u6709\u5565\u4f5c\u7528\uff1f<br \/>2. \u5224\u65ad ContentType \u4e4b\u540e\u7684\u5904\u7406\u6709\u95ee\u9898\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1443300\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sagaxu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u8fd9\u4e2a\u601d\u8def\u5f88 php                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1443301\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : richard1122 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u60f3\u8d77\u4e86\u65e0\u6570\u5e74\u524d PHP \u5f00\u7684 magic quote                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1443302\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chendy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5efa\u8bae\u76f4\u63a5\u5199 servlet \u7b97\u4e86\uff0c\u8fd9 springmvc \u7528\u7684\u4e0d\u5982\u4e0d\u7528                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1443303\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : liugp5201314 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @FreeEx \u7b2c\u4e00\u6b21\u5f04\uff0c\u6211\u4e5f\u662f\u767e\u5ea6\u7684\uff0c\u8bf4 xss \u662f\u9632 sql \u6ce8\u5165\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1443304\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : liugp5201314 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @waa \u662f\u5427\u539f\u6765 controller \u91cc\u7684 HttpServletRequest \u8fd9\u4e2a\u53c2\u6570\u66ff\u6362\u4e3a MultipartHttpServletRequest \u8fd9\u4e2a\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1443305\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : MrMario <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ce8\u5165\u95ee\u9898\u6839\u6e90\u662f sql \u7684\u5904\u7406\uff0c\u9884\u7f16\u8bd1+\u53c2\u6570\u7ed1\u5b9a\uff0c\u6216\u8005\u4f7f\u7528 ORM \u6846\u67b6\u5c31\u597d\uff08\u4e2a\u522b\u6846\u67b6\u4f7f\u7528\u9700\u6ce8\u610f\uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1443306\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : liugp5201314 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @MrMario \u6846\u67b6\u5df2\u7ecf\u8ba2\u597d\u4e86\uff0c\u8fd9\u662f\u4e2a\u7ef4\u62a4\u7684\u9879\u76ee\uff0c\u6211\u53ea\u80fd\u6539\u529f\u80fd\uff0c\u4e0d\u80fd\u6539\u6846\u67b6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1443307\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ice2neet <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             sql \u6ce8\u5165\u4e0d\u662f\u5e94\u8be5\u5904\u7406 sql \u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>SpringMVC \u589e\u52a0\u4e86\u4e00\u4e2a x&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/87525"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=87525"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/87525\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=87525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=87525"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=87525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}