The silent page awaits the waking dawn.
<input value='<?php echo htmlspecialchars($name); ?>'><script>var name = "<?php echo htmlspecialchars($name, ENT_QUOTES); ?>";</script><a href="?page=<?php echo htmlspecialchars($page); ?>">link</a><?php
// 显示文章内容
$id = $_GET['id'];
$article = getArticleById($id);
?>
<div class="post">
<h1><?php echo $article['title']; ?></h1>
<div class="content">
<?php echo $article['content']; ?>
</div>
<div class="meta">
作者:<?php echo $article['author']; ?>,
发布时间:<?php echo $article['time']; ?>
</div>
<input type="button" value="收藏" onclick="addFavorite(<?php echo $article['id']; ?>)">
</div>
Code Audit — PHP XSS
1. XSS Vulnerability Basics
Cross-Site Scripting (XSS) is an attack where malicious scripts are injected into a trusted website. When other users visit the site, the script executes in their browser, enabling cookie theft, session hijacking, phishing, and more.
XSS is categorized by storage location:
2. Common XSS Trigger Points in PHP (Output Contexts)
When PHP code outputs data into different positions within HTML, each context requires its own escaping approach. Missing any one of them can lead to XSS.
Output Location
Dangerous Functions / Statements
Example
**HTML Body Content**
`echo`, `print`, `print_r`, `var_dump`, `<?= ?>`
`echo $_GET['name'];`
**HTML Tag Attribute Value**
Same as above, but output inside an attribute value
`<input value="<?php echo $val; ?>">`
**JavaScript String**
Output into a `<script>` tag or event handler
`var msg = "<?php echo $msg; ?>";`
**CSS Style**
Output into a `<style>` block or `style` attribute
`background: "<?php echo $color; ?>"`
**URL Parameter**
Output into `href` or `src`
`<a href="?q=<?php echo $q; ?>">`
3. Code Audit Methodology: Input Tracing and Output Inspection
The core approach to auditing XSS is "data-flow tracing":
The four-step audit method:
4. PHP Security Function Analysis
htmlspecialchars()
htmlentities()
strip_tags()
filter_var() combined with FILTER_SANITIZE_STRING
Custom filtering (regex, blacklists) is extremely dangerous—bypasses are almost always possible.
5. Classic Techniques for Bypassing Security Functions
Even when htmlspecialchars is used, improper parameter configuration or incorrect context can still leave it exploitable.
If $name = "' onclick='alert(1)", the output becomes <input value='' onclick='alert(1)'>, and the attack succeeds.
If $name = "alert(1)\";//", the output becomes var name = "alert(1)\";//";, which prematurely closes the string and injects code.
If $page = "javascript:alert(1)", although : and alert will not be escaped, the href attribute supports the javascript: protocol and can execute directly.
6. Escaping Requirements for Different Contexts
Context
Recommended Escaping Method
HTML Body
`htmlspecialchars($data, ENT_QUOTES
HTML Attribute (plain)
Same as above, but attribute values must be wrapped in double quotes (recommended).
HTML Attribute (URL)
First use `filter_var($url, FILTER_SANITIZE_URL)` to check the protocol, then output with `htmlspecialchars`.
JavaScript String
Use `json_encode()` (it automatically escapes Unicode and special characters), then output. E.g., `var msg = <?= json_encode($msg); ?>;`
CSS String
Use `addcslashes` to escape `\n, \r,`,`,``, etc., or use `json_encode` and place it inside JSON. \
URL Parameter
Use `urlencode()` to encode the parameter value (note: parameter names are not controllable).
7. Stored XSS Audit Focus
Stored XSS data comes from the database, so during an audit you must trace not only request input but also output after reading from the database.
Common scenarios:
Audit tips:
8. Audit Tools and Auxiliary Techniques
9. Complete Audit Case Study (Step-by-Step Analysis)
Suppose we have the following PHP code for a blog system:
Audit steps:
Remediation suggestions:
10. Remediation Plan and Secure Coding Standards