当前位置:网站首页>XSS prevention
XSS prevention
2022-07-02 03:52:00 【Doc_ ACwhite】
TP6 Packaging usage steps :
① Use composer Carry out orders , install ezyang/htmlpurifier Extended class library
Under project directory
composer require ezyang/htmlpurifier
② stay app/common.php In the definition of remove_xss function
if (!function_exists('remove_xss')) {
// Use htmlpurifier To guard against xss attack
function remove_xss($string){
// relative index.php Entrance file , introduce HTMLPurifier.auto.php Core documents
//require_once './plugins/htmlpurifier/HTMLPurifier.auto.php';
// Generate configuration object
$cfg = HTMLPurifier_Config::createDefault();
// Here is the configuration :
$cfg -> set('Core.Encoding', 'UTF-8');
// Set the allowed HTML label
$cfg -> set('HTML.Allowed','div,b,strong,i,em,a[href|title],ul,ol,li,br,p[style],span[style],img[width|height|alt|src]');
// Set the allowed CSS Style attribute
$cfg -> set('CSS.AllowedProperties', 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align');
// Set up a Is it allowed to use... On the label target="_blank"
$cfg -> set('HTML.TargetBlank', TRUE);
// Use configuration to generate objects for filtering
$obj = new HTMLPurifier($cfg);
// Filter strings
return $obj -> purify($string);
}
}
explain :htmlpurifier plug-in unit , It will filter out script The label and what the label contains js Code .
Middleware processes data
class Safe
{
/**
* Processing requests
*
* @param \think\Request $request
* @param \Closure $next
* @return Response
*/
public function handle($request, \Closure $next)
{
try {
//
# verification token 、 It can be added directly after the route
# Verification timestamp
$this->checkTime();
# Verify the signature
$this->checkSign();
return $next($request);
}catch (Exception $exception) {
return json($exception->getMessage());
}
}
/**
* Verification timestamp
* @throws Exception
*/
public function checkTime(){
$client_time = request()->get('timestamp') ?: request()->post('timestamp');
if (!is_numeric($client_time)) {
throw new Exception(' Incorrect timestamp format ');
}
if (time() - $client_time > 120) {
throw new Exception(' request timeout ');
}
}
/**
* Check the signature
*/
public function checkSign(){
$client_sign = request()->get('sign') ?: request()->post('sign');
# Determine whether there is a signature
if (!$client_sign) {
throw new Exception(' Incorrect signature ');
}
# Determine if the signature is correct
$server_sign = $this->getSign();
if ($client_sign != $server_sign) {
throw new Exception(' Incorrect signature ');
}
}
/**
* Get server signature
* @return string
*/
public function getSign(){
# Get the parameters of all requests
$params = request()->all();
# Signature rules
# First step The parameters participating in the signature do not include the signature itself 、 barring token
unset($params['sign']);
unset($params['token']);
# The second step according to ASCII Sort
ksort($params);
$wait_sign = '';
foreach ($params as $key=> $value) {
$wait_sign .= $key.'='.$value.'&';
}
# Remove the superfluous & Symbol
$wait_sign = rtrim($wait_sign,'&');
return md5($wait_sign);
}
}
Front end generation sign
<script src="../javaScript-MD5/js/md5.js"></script>
<script>
// # Get the parameters of all requests
// # Signature rules
// # First step The parameters participating in the signature do not include the signature itself 、 barring token
// # The second step according to ASCII Sort
// # Remove the superfluous & Symbol
var params = new Array();
params['id'] = 1;
params['name'] = ' Zhang San ';
var sign = createSign(params);
params['sign'] = sign;
var url = 'http://pyg.com/list?'
for (var i in params) {
url += i + '=' + params[i] + '&';
}
$.ajax({
url:url,
dataType:'json',
success:function (result) {
console.log(result);
}
})
function createSign(params) {
var timestamp = Math.ceil((new Date()).getTime()/1000);
params['timestamp'] = timestamp;
params.sort();
var wait_sign = ''
for (var i in params) {
wait_sign += i + '=' + params[i] + '&';
}
wait_sign = wait_sign.substr(0,wait_sign.length-1);
console.log(wait_sign)
sign = hex_md5(wait_sign)
return sign;
}
</script>The second kind : Encapsulate global filtering method
Set the global filtering method to htmlspecialchars
[ This method will make the payment function of the initial version unusable ]
By default, the framework does not set any global filtering rules , You can app\Request Object filter Global filter properties :
namespace app;
class Request extends \think\Request
{
protected $filter = ['htmlspecialchars'];
}边栏推荐
- How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
- 蓝桥杯单片机省赛第十届
- The 6th Blue Bridge Cup single chip microcomputer provincial competition
- First acquaintance with string+ simple usage (II)
- Kotlin basic learning 17
- 【小技巧】使用matlab GUI以对话框模式读取文件
- 蓝桥杯单片机省赛第八届
- Wpviewpdf Delphi and Net PDF viewing component
- 蓝桥杯单片机省赛第十二届第二场
- Welcome the winter vacation multi school league game 2 partial solution (B, C, D, F, G, H)
猜你喜欢
![[untitled] basic operation of raspberry pie (2)](/img/b4/cac22c1691181c1b09fe9d98963dbf.jpg)
[untitled] basic operation of raspberry pie (2)

树莓派GPIO引脚控制红绿灯与轰鸣器

Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs

Sorted out an ECS summer money saving secret, this time @ old users come and take it away

Jetpack之LiveData扩展MediatorLiveData

跳出舒适区,5年点工转型自动化测试工程师,我只用了3个月时间

Fourier series

Getting started with MQ

Basic syntax of unity script (6) - specific folder

【小技巧】使用matlab GUI以对话框模式读取文件
随机推荐
近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
Interface debugging tool simulates post upload file - apipost
Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map
Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
蓝桥杯单片机省赛第十二届第二场
It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
The page in H5 shows hidden execution events
集成底座方案演示说明
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
【DesignMode】原型模式(prototype pattern)
Vite: scaffold assembly
A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
Basic operations of MySQL database (based on tables)
Getting started with MQ
Unity脚本的基础语法(8)-协同程序与销毁方法
2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有
QT designer plug-in implementation of QT plug-in
Monkey测试