来源:自学PHP网 时间:2015-04-17 11:59 作者: 阅读:次
[导读] On SendSafely.com we make heavy use of many new JavaScript APIs introduced with HTML5. We encrypt files, calculate checksums and upload data using pure JavaScript. Moving ......
On SendSafely.com we make heavy use of many new JavaScript APIs introduced with HTML5. We encrypt files, calculate checksums and upload data using pure JavaScript. Moving logic like this down to the browser, however, makes the threat of Cross-Site Scripting (XSS) even greater than before. In order to prevent XSS vulnerabilities, our site makes liberal use of pretty aggressive client-side and server-side encoding APIs. These APIs are based on the OWASP ESAPI library, so we have context-specific encoding methods for pretty much every scenario. Even so, we recognize that it is very difficult to rule out all possible ways to inject code, including a human error on our part. For this reason we chose to also implement Content Security Policy (CSP) on SendSafely.com.
CSP is a new security mechanism supported by modern browsers. It aims to prevent XSS by white-listing URLs the browser can load and execute JavaScript from. The server can, by specifying specific CSP directives, prevent the browser from executing things like in-line JavaScript, eval(), setTimeout() or any JavaScript that comes from an untrusted URL. The policy works as a white list, only domains listed are allowed to execute, everything else will be blocked. The Content Security Policy in SendSafely Use of a strict CSP makes it significantly harder to inject executable JavaScript into application pages since the code must come from a trusted server. The typical XSS attack using un-encoded output on one of our pages won’t work when the CSP is enforced. In fact, any JavaScript embedded on our content pages (even JavaScript we put there) get blocked by the policy. Pretty cool stuff. So you may be asking yourself, does this mean XSS is nothing but a memory? Sadly, this is not the case. For starters, CSP is still fairly new and only supported by recent versions of Firefox, Safari and Chrome. Internet Explorer 10 (IE10) supports a subset of CSP options, but the ability to white list domains is unfortunately not one of them. Aside from limited browser support, data dynamically loaded into the page from JavaScript is still potentially vulnerable. A strict content security policy should therefore not be considered the end-all solution to XSS . Think of CSP more like a safety belt, which is nice to have when your car crashes. Dissecting our Policy To keep our policy as strict as possible, we use two different policies depending on what the page needs to do. The stricter policy is used for all pages except the ones that handle encryption and decryption (the reason for this will be discussed in a separate follow up post). For simplicity, the more strict policy will be explained here. X-Content-Security-Policy: default-src ‘none’; connect-src ‘self’; script-src https://static.sendsafely.com https://www.google.com https://ssl.google-analytics.com; style-src ‘self’ ‘unsafe-inline’ http: https:; img-src ‘self’ https://www.google.com https://ssl.google-analytics.com; report-uri /csp-reports; The header is divided into different sections that are each separated by a semi-colon. The “default-src” directive defines the security policy for all types of content which are not expressly called out by more specific directives. We opted to set the default-src value to ‘none’, meaning that by default we allow nothing to load. If we stopped defining directives here, the site would be completely broken, so now we need to open up the policy and allow specifically what we want to load. Now that we explicitly denied everything as the default, we need to add back the specific content policy options our site needs. On SendSafely, we have a hand full of resource categories that we need to add policy settings for. Each of these are outlined below, along with the CSP directives for each.
Final Notes Implementing CSP on our site proved to be a very interesting exercize. We’ll provide more details on some other aspects of our Content Security Policy implementation in a follow up post here on our blog |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com