当前位置:网站首页>About CSP and its implementation
About CSP and its implementation
2022-06-09 11:34:00 【Johnny, me】
CSP
- Content Security Policy Content security policy
- produce XSS The reason for the attack is that the user data is executed as a program
- Escape substitution is not very formal , But it is most suitable for XSS Solutions for
- Except for escape substitution , Let's talk about CSP, It is a browser and web Standard setting
- CSP Used to specify what is executable , What is not executable
- We just need to mark the user input as non executable , There will be no harm
- CSP yes HTTP head , It specifies which sources can be restricted , such as :
- child-src connect-src default-src
- font-src frame-src img-src
- manifest-src media-src object-src
- script-src style-src worker-src
- For one of these , such as script Script , We can specify which are trusted , What's not believable , It defines many sources
- <host-source> <scheme-source> ‘self’
- ‘unsafe-inline’ ‘unsafe-eval’ ‘none’
- ‘nonce-<base64-value>’ <hash-source>
- ‘strict-dynamic’
- For details, please refer to :https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CSP
- Specific usage can be set HTTP head , Such as :
ctx.set(`Content-Security-Policy`, `script-src 'self'`) - What if we want to execute a piece of code in our page ?
- We can use nonce Parameters , On our page and HTTP A random one on the head nonce, So the attacker has nothing to do
- For example, the script in the front page template
<!-- pug template engine --> script(none='123456') var a = 1; console.log(a); - On the back-end response header
ctx.set(`Content-Security-Policy`, `script-src 'self' 'nonce-123456'`) - there 123456 You can dynamically set a random string parameter , In this case, the front and rear ends remain nonce Unified
- Each time the page is refreshed, a new random string is retrieved
- You can also use one of the following methods to deal with
// Back end encryption var content = ` The script code to be encrypted is written here `; var crypto = require('crypto'); var hash = crypto.createHash('sha256'); hash.update(content); var str = hash.digest('base64'); console.log(str); // This paragraph str The code is the encrypted script code , Is a hash value - It USES sha256 The encryption algorithm of computes the hash value
- We can set... On the response header
ctx.set(`Content-Security-Policy`, `script-src 'self' 'sha256- Fill in the above calculated str A variable's value '`) - On the page template , The code format and content should be consistent with the encryption
<!-- pug template engine --> script The script code to be encrypted is written here
边栏推荐
- 最新Camtasia 2022免费版电脑录屏工具
- [buuctf.reverse] 105_ [FlareOn6]Memecat Battlestation
- 10 个派上用场的 Flutter 小部件
- 版式设计三大原则
- 精智达冲刺科创板:年营收4.58亿 中小企业基金是股东
- 一文详解光场深度估计
- [buuctf.reverse] 114_[MRCTF2020]VirtualTree
- 第三章运输层
- 【基础知识】~ 稳压二极管、三极管、放大电路、逻辑门晶体管数量、FPGA 器件结温范围、FPGA 加载方式、施密特触发器、C 语言结构化编程、中断向量地址、寄生效应、上拉电阻的作用
- Listing of tengya precision industry on Shenzhen Stock Exchange: the market value was 2.6 billion, and the net profit decreased by 42% in the first quarter after deducting non-profit
猜你喜欢

首家BMW i品牌专属体验店开业,全面展示宝马电动产品的魅力

多引擎数据库管理工具 DataGrip 2022.1.5中文版

In modern society, people are more and more dependent on semiconductor products

Leetcode 2048. Next larger numerical balance number (interesting, solved)

百度 90 后程序员删改数据库被判刑,其称因对领导不满

Data asset management: how to manage the data assets of an enterprise?

自己建设网站需要做哪些准备

At last someone explained the law of large numbers

MOS tube from entry to mastery

Object instantiation and access
随机推荐
多引擎数据库管理工具 DataGrip 2022.1.5中文版
[buuctf.reverse] 108_[GKCTF 2021]Crash
Leetcode 2048. 下一个更大的数值平衡数(有点意思,已解决)
Lecture 4: data warehouse construction (II)
How does the IOT low code platform use operation logs?
[buuctf.reverse] 111_ [b01lers2020]chugga_ chugga
最全知识总结,初学者必看
李宁抄底Clarks
Comparison and evaluation of code on cloud - devcloud
[buuctf.reverse] 103_ [CFI-CTF 2018]powerPacked
Kaggle泰坦尼克号幸存者预测
关于XSS攻击及其防御
第二章应用层
Matlab related function knowledge points (III) -floor function + dot division operator + matrix index rules
电脑的选择1
Is it safe for CICC fortune to open an account
Possible causes of processing chain loading error -process chain loading error
本科毕设CTF平台-MarsCTF
More than observation | Alibaba cloud observable Technology Summit officially launched
自己建设网站需要做哪些准备