Home php教程 PHP开发 Methods to prevent XSS cross-site attacks in Laravel5

Methods to prevent XSS cross-site attacks in Laravel5

Dec 20, 2016 pm 03:54 PM
xss

本文实例讲述了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
Copy after login

安装完成后,在配置文件config/app.php的providers中注册HTMLPurifier服务提供者:

'providers' => [
 // ...
 Mews\Purifier\PurifierServiceProvider::class,
]
然后在aliases中注册Purifier门面:
'aliases' => [
 // ...
 'Purifier' => Mews\Purifier\Facades\Purifier::class,
]
Copy after login

2、配置

要使用自定义的配置,发布配置文件到config目录:

php artisan vendor:publish
Copy after login

这样会在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/)%",
  ],
 ],
];
Copy after login

3、使用示例

可以使用辅助函数clean:

clean(Input::get('inputname'));
Copy after login

或者使用Purifier门面提供的clean方法:

Purifier::clean(Input::get('inputname'));
Copy after login

还可以在应用中进行动态配置:

clean('This is my H1 title', 'titles');
clean('This is my H1 title', array('Attr.EnableID' => true));
Copy after login

或者你也可以使用Purifier门面提供的方法:

Purifier::clean('This is my H1 title', 'titles');
Purifier::clean('This is my H1 title', array('Attr.EnableID' => true));
Copy after login

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 (&#39;"&#39;, "\\", "&#39;", "/", "..", "../", "./", "//" ), &#39;&#39;, $string );
 $no = &#39;/%0[0-8bcef]/&#39;;
 $string = preg_replace ( $no, &#39;&#39;, $string );
 $no = &#39;/%1[0-9a-f]/&#39;;
 $string = preg_replace ( $no, &#39;&#39;, $string );
 $no = &#39;/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S&#39;;
 $string = preg_replace ( $no, &#39;&#39;, $string );
 return True;
 }
 $keys = array_keys ( $string );
 foreach ( $keys as $key )
 {
 clean_xss ( $string [$key] );
 }
}
//just a test
$str = &#39;jb51.net<meta http-equiv="refresh" content="0;">&#39;;
clean_xss($str); //如果你把这个注释掉,你就知道xss攻击的厉害了
echo $str;
?>
Copy after login

   

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

更多Laravel5中防止XSS跨站攻击的方法相关文章请关注PHP中文网!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Cross-site scripting (XSS) and cross-site request forgery (CSRF) protection in Laravel Cross-site scripting (XSS) and cross-site request forgery (CSRF) protection in Laravel Aug 13, 2023 pm 04:43 PM

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 defend against XSS and remote code execution attacks in PHP How to defend against XSS and remote code execution attacks in PHP Jun 30, 2023 am 08:04 AM

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.

Analysis of secure XSS filtering technology in PHP Analysis of secure XSS filtering technology in PHP Jun 29, 2023 am 09:49 AM

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

Security Best Practices for PHP and Vue.js Development: Preventing XSS Attacks Security Best Practices for PHP and Vue.js Development: Preventing XSS Attacks Jul 06, 2023 pm 01:37 PM

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.

How to analyze reflected XSS How to analyze reflected XSS Jun 03, 2023 pm 12:09 PM

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

XSS attacks in PHP XSS attacks in PHP May 23, 2023 am 09:10 AM

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

How to analyze reflected XSS How to analyze reflected XSS May 13, 2023 pm 08:13 PM

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.

A Beginner's Guide to PHP: Cross-Site Scripting Attacks (XSS) A Beginner's Guide to PHP: Cross-Site Scripting Attacks (XSS) May 21, 2023 am 10:51 AM

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

See all articles