XSS
一句话解释
XSS (Cross-Site Scripting) 是 security vulnerability 哪里 attacker injects malicious scripts 到 web page viewed 通过 other 用户.
用大白话说
XSS 是one of the most common web security vulnerabilities。 It happens when user input is displayed on a page without proper escaping, allowing an attacker to inject JavaScript that runs in other users' browsers. This can steal cookies, session tokens, or sensitive data.
There are three types: Stored XSS (malicious input saved to the database and shown to others), Reflected XSS (input embedded in the URL and reflected on the page), and DOM-based XSS (client-side JavaScript injects unescaped data into the DOM).
Prevention: never use innerHTML with user input (use textContent instead), escape all user-generated content, implement a Content Security Policy (CSP), and use HttpOnly cookies so injected scripts cannot steal them. Always treat user input as untrusted.
示例
// VULNERABLE - XSS risk:
element.innerHTML = userInput;
// SAFE - treats input as text:
element.textContent = userInput;innerHTML parses 输入 as HTML (允许 script injection). textContent treats 它 as plain 文本, preventing XSS.
相关术语
相关资源
相关课程
- Web Security 基础
学习 web security.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。