Methods to prevent XSS cross-site attacks in Laravel5
本文实例讲述了Laravel5中防止XSS跨站攻击的方法。分享给大家供大家参考,具体如下:
Laravel 5本身没有这个能力来防止xss跨站攻击了,但是这它可以使用Purifier 扩展包集成 HTMLPurifier 防止 XSS 跨站攻击。
1、安装
HTMLPurifier 是基于 PHP 编写的富文本 HTML 过滤器,通常我们可以使用它来防止 XSS 跨站攻击,更多关于 HTMLPurifier的详情请参考其官网:http://htmlpurifier.org/。Purifier 是在 Laravel 5 中集成 HTMLPurifier 的扩展包,我们可以通过 Composer 来安装这个扩展包:
composer require mews/purifier
安装完成后,在配置文件config/app.php的providers中注册HTMLPurifier服务提供者:
'providers' => [ // ... Mews\Purifier\PurifierServiceProvider::class, ] 然后在aliases中注册Purifier门面: 'aliases' => [ // ... 'Purifier' => Mews\Purifier\Facades\Purifier::class, ]
2、配置
要使用自定义的配置,发布配置文件到config目录:
php artisan vendor:publish
这样会在config目录下生成一个purifier.php文件:
return [ 'encoding' => 'UTF-8', 'finalize' => true, 'preload' => false, 'cachePath' => null, 'settings' => [ 'default' => [ 'HTML.Doctype' => 'XHTML 1.0 Strict', 'HTML.Allowed' => 'div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]', 'CSS.AllowedProperties' => 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align', 'AutoFormat.AutoParagraph' => true, 'AutoFormat.RemoveEmpty' => true ], 'test' => [ 'Attr.EnableID' => true ], "youtube" => [ "HTML.SafeIframe" => 'true', "URI.SafeIframeRegexp" => "%^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/)%", ], ], ];
3、使用示例
可以使用辅助函数clean:
clean(Input::get('inputname'));
或者使用Purifier门面提供的clean方法:
Purifier::clean(Input::get('inputname'));
还可以在应用中进行动态配置:
clean('This is my H1 title', 'titles'); clean('This is my H1 title', array('Attr.EnableID' => true));
或者你也可以使用Purifier门面提供的方法:
Purifier::clean('This is my H1 title', 'titles'); Purifier::clean('This is my H1 title', array('Attr.EnableID' => true));
php防止xss攻击
<?PHP function clean_xss(&$string, $low = False) { if (! is_array ( $string )) { $string = trim ( $string ); $string = strip_tags ( $string ); $string = htmlspecialchars ( $string ); if ($low) { return True; } $string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $string ); $no = '/%0[0-8bcef]/'; $string = preg_replace ( $no, '', $string ); $no = '/%1[0-9a-f]/'; $string = preg_replace ( $no, '', $string ); $no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; $string = preg_replace ( $no, '', $string ); return True; } $keys = array_keys ( $string ); foreach ( $keys as $key ) { clean_xss ( $string [$key] ); } } //just a test $str = 'jb51.net<meta http-equiv="refresh" content="0;">'; clean_xss($str); //如果你把这个注释掉,你就知道xss攻击的厉害了 echo $str; ?>
希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。
更多Laravel5中防止XSS跨站攻击的方法相关文章请关注PHP中文网!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Cross-site scripting (XSS) and cross-site request forgery (CSRF) protection in Laravel With the development of the Internet, network security issues have become more and more serious. Among them, Cross-SiteScripting (XSS) and Cross-SiteRequestForgery (CSRF) are one of the most common attack methods. Laravel, as a popular PHP development framework, provides users with a variety of security mechanisms

How to use PHP to defend against cross-site scripting (XSS) and remote code execution attacks Introduction: In today's Internet world, security has become a vital issue. XSS (cross-site scripting) and remote code execution attacks are two of the most common security vulnerabilities. This article will explore how to use the PHP language to defend against these two attacks and provide several methods and techniques to protect your website from these attacks. 1. Understand XSS attacks XSS attacks refer to attackers obtaining users’ personal information by injecting malicious scripts on websites.

PHP is a programming language widely used in website development, but when using PHP to develop websites, security issues often cause people to worry. One of them is Cross-SiteScripting (XSS), which is a common network security vulnerability. To solve this problem, PHP provides some secure XSS filtering technologies. This article will introduce the principles and usage of secure XSS filtering technology in PHP. First, we need to understand what an XSS attack is. XSS attack

Best Practices for PHP and Vue.js Development Security: Preventing XSS Attacks With the rapid development of the Internet, network security issues are becoming more and more important. Among them, XSS (cross-site scripting attack) is a very common type of network attack that aims to exploit the security vulnerabilities of the website to inject malicious code into users or tamper with web page content. In PHP and Vue.js development, it is very important to adopt some security best practices to prevent XSS attacks. This article will introduce some commonly used methods to prevent XSS attacks and provide corresponding codes.

1 Test environment introduction The test environment is the DVWA module in the OWASP environment 2 Test description XSS is also called CSS (CrossSiteScript), a cross-site scripting attack. It refers to a malicious attacker inserting malicious HTML code into a Web page. When a user browses the page, the HTML code embedded in the Web will be executed, thereby achieving the special purpose of maliciously attacking the user, such as obtaining the user's cookie. Navigate to malicious websites, carry attacks and more. This vulnerability could be exploited by an attacker to hijack the session of an authenticated user. After hijacking an authenticated session, the virus originator has all the permissions of that authorized user. 3. Test step: Enter the javascript script code in the input box: al

In recent years, with the rapid development of Internet information technology, our lives are increasingly inseparable from the Internet. The interaction between the network and our daily lives is inseparable from a large amount of code writing, transmission and processing. And these codes need us to protect their security, otherwise malicious attackers will use them to launch various attacks. One of these attacks is XSS attack. In this article, we will focus on XSS attacks in PHP and give corresponding defense methods. 1. Overview of XSS attacks XSS attacks, also known as cross-site scripting attacks, are usually

1. Reflected XSS Reflected XSS means that the application obtains untrustworthy data through Web requests and transmits it to Web users without checking whether the data contains malicious code. Reflected XSS is generally constructed by the attacker with malicious code parameters in the URL. When the URL address is opened, the unique malicious code parameters are parsed and executed by HTML. It is characterized by non-persistence and requires the user to click on a link with specific parameters. can cause. The editor takes the JAVA language source code as an example to analyze CWEID80:ImproperNeutralizationofScript-RelatedHTMLTagsinaWebPage(BasicXSS)2.

PHP is a popular server-side programming language used for developing dynamic web pages and web applications. However, due to its widespread use and easy-to-learn nature, it is often targeted by hackers to compromise websites. This article will introduce cross-site scripting attacks (XSS) and provide some preventive measures. What is a cross-site scripting attack? Cross-site scripting (XSS) is an attack that exploits vulnerabilities in web applications. Attackers take control of websites by injecting malicious code and then sending deceptive links to users or inserting malicious code into
