后端中的跨站脚本攻击(XSS)
了解 XSS 如何源于后端漏洞,并探索正确进行输出编码和验证的策略。
后端中的跨站脚本攻击(XSS) 是 CoddyKit 上的免费 Secure Coding & OWASP Top 10 for Backend 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Secure Coding & OWASP Top 10 for Backend 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Secure Coding & OWASP Top 10 for Backend 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
XSS from a Backend Perspective
Cross-Site Scripting (XSS) is a type of security vulnerability that allows attackers to inject malicious scripts into web pages viewed by other users.
While XSS attacks execute in the user's browser (client-side), the root cause often lies in how the backend application handles, stores, and outputs user-supplied data.
Backend's Role in XSS
Your backend application is responsible for managing user data. This includes:
- Receiving input from users.
- Storing that input (e.g., in a database).
- Retrieving and sending that input back to browsers for display.
If the backend fails to properly process or 'sanitize' this data before sending it to the browser, it creates an XSS vulnerability.
Reflected XSS via Backend
Reflected XSS occurs when a backend application immediately returns user input in its response without proper encoding, and a browser then renders it.
Think of a search page where your search term is echoed back in the results. If the search term contains malicious script, and the backend doesn't handle it, the script runs.
Stored XSS via Backend
Stored XSS is often more severe. Here, malicious user input is:
- Received by the backend.
- Persisted (e.g., saved in a database, file system).
- Later retrieved and displayed to other users or even administrators.
Examples include vulnerable comment sections, forum posts, or user profile fields where data is saved and then rendered without proper protection.
Vulnerable Backend Output
Consider this simplified Java example. It takes user input and directly embeds it into the HTML response. Try running it with some malicious input!
public class VulnerableOutput {
public static void main(String[] args) {
// Imagine this is user input from a web request
String userInput = "<script>alert('XSS Attack!');</script>";
System.out.println("<html><body>");
System.out.println("<h1>Welcome, " + userInput + "!</h1>"); // Direct output
System.out.println("</body></html>");
}
}The Problem: Code Execution
When the backend directly outputs user input like in the previous example, the browser interprets it as part of the HTML structure.
If the userInput contained <script>alert('XSS Attack!');</script>, the browser would execute the JavaScript code within the script tags.
This allows attackers to:
- Steal cookies (session hijacking).
- Deface websites.
- Redirect users to malicious sites.
- Execute arbitrary actions on behalf of the user.
Defending with Output Encoding
The primary defense against XSS, especially for data originating from the backend, is output encoding.
Output encoding converts special characters (like <, >, &, ", ') into their safe HTML entity equivalents (e.g., <, >).
This ensures the browser treats the input as plain text, not executable code.
Secure Backend with Encoding
Here's how you can implement a basic HTML encoding function in Java to prevent XSS. Many web frameworks provide built-in, more robust encoding utilities.
public class SecureOutput {
// A simplified HTML encoder
public static String htmlEncode(String input) {
if (input == null) return "";
return input
.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace("\"", """)
.replace("'", "'")
.replace("/", "/");
}
public static void main(String[] args) {
String userInput = "<script>alert('XSS Attack!');</script>"; // Malicious input
String encodedInput = htmlEncode(userInput); // Apply encoding!
System.out.println("<html><body>");
System.out.println("<h1>Welcome, " + encodedInput + "!</h1>"); // Safe output
System.out.println("</body></html>");
}
}Input Validation vs. Encoding
It's important to distinguish between:
- Input Validation: Checks if data is valid and safe *before* processing or storing (e.g., ensuring an email is in correct format, limiting length). This helps with overall data integrity and other attack types.
- Output Encoding: Makes data safe for display *after* retrieval from the backend. This is the direct and crucial defense against XSS.
Both are vital for a secure application, but output encoding is your final safeguard against XSS when rendering user-controlled content.
XSS Defense Check
A social media platform's backend stores user posts in a database. When another user views a post, the backend retrieves and displays it. Which is the most effective measure to prevent XSS?
Recap: Guarding Against XSS
In this lesson, we learned that:
- XSS vulnerabilities often originate from backend applications that improperly handle user-supplied data.
- Both Reflected and Stored XSS rely on the backend sending unencoded malicious input to the browser.
- The most critical defense is output encoding, which converts special characters into safe HTML entities before any user-controlled data is rendered.
- Combining robust input validation with consistent output encoding provides the best protection against XSS.
用 AI 导师学习 Secure Coding & OWASP Top 10 for Backend — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「后端中的跨站脚本攻击(XSS)」课时是免费的吗?
是的 — 「后端中的跨站脚本攻击(XSS)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Secure Coding & OWASP Top 10 for Backend 课程的其余内容,请升级到 CoddyKit PRO。 Secure Coding & OWASP Top 10 for Backend 课程共包含 4 节课。
「后端中的跨站脚本攻击(XSS)」这节课中我会学到什么?
了解 XSS 如何源于后端漏洞,并探索正确进行输出编码和验证的策略。 你通过在浏览器中直接运行的动手代码来练习 Secure Coding & OWASP Top 10 for Backend,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Secure Coding & OWASP Top 10 for Backend 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Secure Coding & OWASP Top 10 for Backend 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「后端中的跨站脚本攻击(XSS)」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Secure Coding & OWASP Top 10 for Backend 课中编写并运行代码吗?
能。每节 Secure Coding & OWASP Top 10 for Backend 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 防止 SQL 注入
- 命令与代码注入
- 后端中的跨站脚本攻击(XSS)
- 防止 XML 与 LDAP 注入