Skip to content

Forums: message bodies and titles are output without escaping (only safe because bind() encodes input) #533

Closed

Description

@caseysoftware

Summary

The forum views send message bodies and titles to the page without escaping them on output. Today this is safe only because BaseObject::bind() runs htmlentities() on every string before a message is saved. Any row that reaches forum_messages without going through bind() is rendered as live HTML, including <script>. In practice that limits it to a few uncommon cases, listed below, but the output should not depend on how the input was saved.

Severity: low. Normal posting through the UI is not exploitable on 5.0.

Where

On dev-5.0.0 (0ad29d2):

  • classes/w2p/Core/BaseObject.class.php:235 runs htmlentities($v) on every string in bind(). This was added in 3.2 (7b72c15, May 2014). It is the only thing protecting the views.
  • includes/cleanup_functions.php:4883-4884 and 4979-4981 run the body through $bbparser->qparse() and append it to the page as-is.
  • includes/cleanup_functions.php:4881, 4918 and 4978 append message_title without escaping.
  • modules/forums/post_message.php:147-149 handles the "Previous Message" body the same way (its title at line 141 already uses w2PformSafe()).

HTML_BBCodeParser does not escape HTML. It only converts BBCode tags and passes everything else through. With a raw row, <script>alert(1)</script> and <img src=x onerror=...> come out unchanged.

When it is exploitable

Only when a raw (not entity-encoded) body or title is in the database:

  1. Installs upgraded from before 3.2. Messages saved by 1.x, 2.x, 3.0 and 3.1 were stored raw. No upgrade script re-encodes them (006_special_characters.sql only changes the character set). Any HTML or script in those old posts runs today for everyone who opens the thread. Forum BBCode isn't rendered on PHP 8: HTML_BBCodeParser constructor never runs #531's reporter, for example, upgraded from 2.3.1.
  2. Data that bypasses bind(). This includes the dotProject conversion (dp_to_w2p*.sql copies forum_messages as-is), manual SQL imports or restores from another system, and add-on modules or scripts that insert into forum_messages directly.
  3. Future regressions. If a new write path skips bind(), or if the htmlentities() call in bind() is removed or changed, every new post becomes a stored XSS for every reader of the forum.

Case 1 needs malicious content to have been posted under an old version, and case 2 needs database or code-level access, so the realistic risk is low. Case 3 is the main reason to fix it.

Recommended fix

Escape when displaying, in one place. Because existing rows are a mix of entity-encoded (3.2+) and raw (pre-3.2) text, decode first and then escape once. That makes both kinds safe and avoids double-encoding &amp; and friends:

// includes/main_functions.php (or a small helper class)
function w2p_forum_render($text, $bbparser)
{
    $plain = html_entity_decode((string) $text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
    return nl2br($bbparser->qparse(htmlspecialchars($plain, ENT_QUOTES, 'UTF-8')));
}

Then:

  • Use it for the body in __extract_from_view_messages1(), __extract_from_view_messages4() and post_message.php. __extract_from_view_messages1() doesn't currently call nl2br(), so drop nl2br() from the helper there or add a parameter for it.
  • Escape titles the same way (htmlspecialchars(html_entity_decode($title, ENT_QUOTES | ENT_HTML5, 'UTF-8'), ENT_QUOTES, 'UTF-8')) at lines 4881, 4918 and 4978.
  • Optionally, add a unit test that renders a raw body and an htmlentities()-encoded body, and asserts that both produce the same output with no <script>/<img> and with BBCode still converted.

I tested this with the PHP 8 HTML_BBCodeParser fix (#531) on PHP 8.3. Raw input and bind()-encoded input produced identical output. [b], [url] (including query strings with &) and [list] still render. <script> and <img onerror> show as text. Café & co shows correctly, with no double-encoding.

One side effect to expect: pre-3.2 posts that deliberately used HTML for formatting will now show the tags as text. This is the intended trade-off.

A longer-term option is to escape on output everywhere and stop encoding input in bind(). That is a larger change, because every view currently relies on bind(). See #482. Related: #401 (stored XSS through URL fields).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions