Table of Contents
Foreword:
Default policy restrictions
Home Web Front-end H5 Tutorial Content Security Policy (CSP) in HTML5

Content Security Policy (CSP) in HTML5

Oct 07, 2017 am 11:40 AM
h5 html5 Strategy

Foreword:

Cordova不支持内联事件,所以点击事件必须提取到js里面.
以下是从官网摘抄下来,希望对您有所帮助
Copy after login

To alleviate a large number of potential cross-site scripting issues, Chrome's extension system has incorporated the general concept of Content Security Policy (CSP). This introduces some fairly strict policies that will make extensions more secure by default and give you the ability to create and enforce rules that govern the types of content that can be loaded and executed by extensions and applications.

Generally speaking, CSP acts as a hacking/whitelisting mechanism for resources that extensions load or execute. By defining a sensible policy for your extension, you can carefully consider the resources your extension requires and ask the browser to ensure that these are the only ones your extension can access. These policies provide security beyond the host permissions requested by your extension; they are an additional layer of protection, not a replacement.

On the web, such policies are defined through HTTP headers or elements. Neither is a suitable mechanism in Chrome's extension system. Instead, an extension's policy is defined via the extension's manifest.json file, as shown below:

{ 
   … 
   “content_security_policy”:“[POLICY STRING GOES HERE]” 
   …
}
Copy after login

For complete details on CSP syntax, see the Content Security Policy specification and the "Content Security Policy" section on HTML5Rocks Introduction" article.

Default policy restrictions

The manifest_version package is not defined and does not have a default content security policy. Those that select manifest_version 2, have the default content security policy:

script-src'self'; object-src'self'

This policy increases security by restricting extensions and applications in three ways Performance:

(1) Evaluation and related functions are disabled

The following code does not work:

alert(eval("foo.bar .baz"));

window.setTimeout(“alert(’hi’)”,10); 
 window.setInterval(“alert(’hi’)”,10); 
 new Function(“return foo.bar.baz”);
Copy after login

Evaluating such a JavaScript string is a common XSS attack vector. Instead, you should write the following code:

alert(foo && foo.bar && foo.bar.baz); 
window.setTimeout(function(){alert(’hi’);},10); 
window.setInterval(function(){alert(’hi’);},10); 
function(){return foo && foo.bar && foo.bar.baz};
Copy after login

(2) Inline JavaScript will not be executed

Inline JavaScript will not be executed. This restriction prohibits inline blocks and inline event handlers programs (such as ).

The first restriction eliminates a large number of cross-site scripting attacks by preventing you from accidentally executing scripts provided by malicious third parties. However, it requires a clean separation between what your code writes and how it behaves (which you should certainly do) right? An example might make this clearer. You might try writing a browser-action popup as a single popup.html containing:

<!doctype html> 
      My Awesome Popup! 
       function awesome(){ 
         //做某事真棒! 
       }
   function totalAwesome(){
     //做某事真棒!
   }
  函数clickHandler(element){
     setTimeout( “awesome();getherAwesome()” ,1000);
   }
   function main(){
     //初始化工作在这里。
   }
 </ SCRIPT>
Copy after login
Click for awesomeness!


Relax the default policy

(1) Inline script

Until Chrome 45, there was no relaxation of restrictions on executing inline JavaScript mechanism. In particular, setting a script policy containing 'unsafe-inline' will have no effect.

Starting with Chrome 46, it is possible to whitelist inline scripts by specifying the base64-encoded hash of the source code in the policy. The hash must be prefixed by the hashing algorithm used (sha256, sha384 or sha512).

about examples

The above is the detailed content of Content Security Policy (CSP) in HTML5. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles