自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Web安全之Content Security Policy(CSP 內(nèi)容安全策略)詳解

安全 應(yīng)用安全
CSP對于保護(hù)Web應(yīng)用程序的安全非常重要,可以幫助減少很多XSS類攻擊。需要注意的是,CSP只是一種安全策略,不能完全保證網(wǎng)站的安全性。因此,在使用CSP時(shí),還需要結(jié)合其他安全措施,如使用HTTPS、防火墻等,進(jìn)一步提高網(wǎng)站的安全性。

什么是Content Security Policy(CSP)

Content Security Policy是一種網(wǎng)頁安全策略,現(xiàn)代瀏覽器使用它來增強(qiáng)網(wǎng)頁的安全性??梢酝ㄟ^Content Security Policy來限制哪些資源(如JavaScript、CSS、圖像等)可以被加載,從哪些url加載。

CSP 本質(zhì)上是白名單機(jī)制,開發(fā)者明確告訴瀏覽器哪些外部資源可以加載和執(zhí)行,可以從哪些url加載資源。

CSP最初被設(shè)計(jì)用來減少跨站點(diǎn)腳本攻擊(XSS),該規(guī)范的后續(xù)版本還可以防止其他形式的攻擊,如點(diǎn)擊劫持。

啟用CSP的兩種方法

啟用CSP的方法有兩種,第一種是通過設(shè)置一個(gè)HTTP響應(yīng)頭(HTTP response header) “Content-Security-Policy”,第二種是通過HTML標(biāo)簽<meta>設(shè)置,例如:

<meta http-equiv="Content-Security-Policy" content="script-src 'self'; object-src 'none'">

除了Content-Security-Policy外,還有一個(gè)Content-Security-Policy-Report-Only字段,表示不執(zhí)行限制選項(xiàng),只是記錄違反限制的行為,必須與report-uri值選項(xiàng)配合使用,例如:

Content-Security-Policy-Report-Only: default-src 'self'; report-uri /some-report-uri;

CSP指令介紹

Content-Security-Policy值由一個(gè)或多個(gè)指令組成,多個(gè)指令用分號分隔。

csp資源加載項(xiàng)限制指令如下:

script-src:外部腳本
style-src:樣式文件
img-src:圖片文件
media-src:媒體文件(音頻和視頻)
font-src:字體文件
object-src:插件(比如 Flash)
child-src:框架
frame-ancestors:嵌入的外部資源(比如<frame>、<iframe><embed><applet>
connect-src:HTTP 連接(通過 XHR、WebSockets、EventSource等)
worker-src:worker腳本
manifest-src:manifest 文件
default-src:用來設(shè)置上面各個(gè)選項(xiàng)的默認(rèn)值。

上述指令對應(yīng)的值如下:

Source Value

Example

Description

*

img-src *

Wildcard, allows any URL except data: blob: filesystem: schemes.

'none'

object-src 'none'

Prevents loading resources from any source.

'self'

script-src 'self'

Allows loading resources from the same origin (same scheme, host and port).

data:

img-src 'self' data:

Allows loading resources via the data scheme (eg Base64 encoded images).

domain.example.com

img-src domain.example.com

Allows loading resources from the specified domain name.

*.example.com

img-src *.example.com

Allows loading resources from any subdomain under example.com.

??https://cdn.com??

img-src https://cdn.com

Allows loading resources only over HTTPS matching the given domain.

https:

img-src https:

Allows loading resources only over HTTPS on any domain.

'unsafe-inline'

script-src 'unsafe-inline'

Allows use of inline source elements such as style attribute, onclick, or script tag bodies (depends on the context of the source it is applied to) and javascript: URIs

'unsafe-eval'

script-src 'unsafe-eval'

Allows unsafe dynamic code evaluation such as JavaScript eval()

'sha256-'

script-src 'sha256-xyz...'

Allows an inline script or CSS to execute if its hash matches the specified hash in the header. Currently supports SHA256, SHA384 or SHA512. CSP Level 2

'nonce-'

script-src 'nonce-rAnd0m'

Allows an inline script or CSS to execute if the script (eg: <script nonce="rAnd0m">) tag contains a nonce attribute matching the nonce specifed in the CSP header. The nonce should be a secure random string, and should not be reused. CSP Level 2

'strict-dynamic'

script-src 'strict-dynamic'

Enables an allowed script to load additional scripts via non-"parser-inserted" script elements (for example document.createElement('script'); is allowed). CSP Level 3

'unsafe-hashes'

script-src 'unsafe-hashes' 'sha256-abc...'

Allows you to enable scripts in event handlers (eg onclick). Does not apply to javascript: or inline <script> CSP Level 3

CSP URL限制指令如下:

frame-ancestors:限制嵌入框架的網(wǎng)頁
base-uri:限制<base#href>
form-action:限制<form#action>

CSP其它限制指令如下:

block-all-mixed-content:HTTPS 網(wǎng)頁不得加載HTTP鏈接的資源
upgrade-insecure-requests:自動(dòng)將網(wǎng)頁加載的外部資源的鏈接由HTTP改為HTTPS
plugin-types:限制可以使用的插件格式
sandbox:瀏覽器行為的限制,比如不能有彈出窗口等。

小結(jié)

CSP對于保護(hù)Web應(yīng)用程序的安全非常重要,可以幫助減少很多XSS類攻擊。需要注意的是,CSP只是一種安全策略,不能完全保證網(wǎng)站的安全性。因此,在使用CSP時(shí),還需要結(jié)合其他安全措施,如使用HTTPS、防火墻等,進(jìn)一步提高網(wǎng)站的安全性。

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2014-04-21 10:24:06

2024-07-30 14:31:01

2012-11-14 17:18:58

2011-06-20 13:29:44

2010-09-17 14:50:06

2015-09-02 10:21:55

2020-02-02 09:23:44

軟件安全滲透測試信息安全

2024-01-10 08:03:50

數(shù)據(jù)安全網(wǎng)絡(luò)安全

2015-01-13 09:08:54

內(nèi)容安全策略CSP

2013-02-20 10:33:28

Windows安全策略

2011-03-23 10:58:52

2009-08-05 10:49:50

信息安全策略安全管理

2022-02-13 00:13:26

云安全數(shù)據(jù)安全

2017-02-07 09:28:29

云安全策略云計(jì)算

2011-08-19 14:29:52

2010-06-03 17:02:49

2010-05-05 15:38:31

Oracle安全策略

2010-01-05 11:00:54

2017-03-31 09:27:05

2012-11-09 10:55:44

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號