fix(webserver): add htmlspecialchars + addslashes builtins and escape remote-controlled strings in the WebUI (XSS) - #106
Merged
Conversation
The template mini-PHP had no way to escape HTML, forcing each page to duplicate an html_escape()/html_join() helper built on split() (include is unusable, it resolves against the process cwd). Add a proper builtin that escapes & < > " ' (matching PHP >=8.1 htmlspecialchars with ENT_QUOTES), so templates can call it directly.
Escapes a string for safe inclusion in a double-quoted JavaScript string literal inside an HTML <script> block. On top of the classic addslashes set (\ " ') it also escapes < as \x3C so an embedded </script> cannot terminate the <script> element (the HTML parser scans raw-text script content for that literal sequence regardless of JS syntax), and CR/LF as \r/\n so a multi-line value can't break the literal. Every escape decodes back to the original character in JS, so a value assigned to a form field is displayed verbatim -- unlike htmlspecialchars, whose HTML entities are not decoded inside a raw-text <script>.
Audit every echo of non-constant data across all .php templates and wrap the dangerous ones with the appropriate escaping builtin: - file names / user names (dload, shared, search), server name/desc/addr (servers), raw log/serverinfo (log.php): echoed verbatim -> stored XSS from a shared file or server named '<script>...</script>'. - connected server name/address in the status bar (stats.php): the name is self-declared by the eD2K server and the bar loads in an iframe on every page. - stats tree leaf labels (stats_tree.php): client version / OS strings are reported as free text by remote clients. - category names in <option> lists (dload, search, footer). These all land in HTML text or quoted-attribute context, so they use htmlspecialchars, where the parser decodes the entities for display. prefs.php is different: the nickname (settable via ?nick=) and option values are emitted into double-quoted JS string literals inside a <script> block. There they are escaped with addslashes (JS-string context), not htmlspecialchars -- HTML entities are not decoded in a raw-text <script>, so htmlspecialchars would display the literal entities in the form fields and round-trip them back on save. Verified end to end with the standalone interpreter under ASan/UBSan and a real-JS (node) round-trip: every page parses and runs clean, crafted payloads render inert, and escaped values decode back to the original.
|
Great job on the testing side! 👏 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes stored/reflected XSS in the amuleweb
defaulttemplate. Adds twoescaping builtins to the embedded PHP interpreter and uses them to escape
every echo of remote-controlled data in the right context.
Why
Several pages echoed attacker-influenced strings into HTML without escaping, so
a shared file or server named
<script>…</script>(or a malicious eD2K servername, or a remote client's reported version/OS string) executed JavaScript in
the admin's authenticated session.
The bundled mini-PHP interpreter had no escaping builtins (
htmlspecialchars,str_replace, …), andinclude()is unusable (it resolves against the processcwd), so there was previously no clean way to escape from a template.
Changes (3 commits)
1.
htmlspecialcharsbuiltin (php_core_lib.cpp)& < > " '(matching PHP ≥8.1htmlspecialcharswithENT_QUOTES).std::string— UTF-8 safe (metacharacters are ASCII andnever appear inside multibyte sequences), no manual buffer sizing.
2.
addslashesbuiltin (php_core_lib.cpp)<script>block. On top of the classic\ " 'set, it escapes<as\x3C(so an embedded</script>cannot terminate the script element) andCR/LF as
\r/\n.a form field is displayed verbatim — unlike
htmlspecialchars, whose HTMLentities are not decoded inside a raw-text
<script>.3. Audit + escape across all
.phptemplateshtmlspecialchars: file names, user names(dload, shared, search), server name/desc/addr (servers), raw log/serverinfo
(
log.php), connected server name/address in the iframe status bar(
stats.php), stats-tree leaf labels (stats_tree.php), category names in<option>lists (dload, search, footer).addslashes: the nickname (settable via?nick=) andoption values emitted into the
<script> initvalsblock inprefs.php.amuleweb-main-search.php(thesortparamstays whitelisted — stricter than escaping for that reflected value).
Testing
amulewebbuilds clean (no new warnings).for both builtins:
nested calls, UTF-8, missing/extra args).
(allocation count independent of iteration count), no overflow.
exact match.
addslashes: real-JS (node) round-trip — every payload (incl.</script>, quotes, backslash, newlines, unicode) decodes back to theoriginal and never produces a literal
</script>that closes the block.display correctly.