Skip to content

[SECURITY] Post content is stored and rendered without HTML sanitization, enabling stored XSS via script injection in post bodies #995

Description

@anshul23102

Problem

Post content submitted by users is stored in the database and rendered to other users without sanitization. An attacker who creates a post containing a script tag or an event-handler attribute (e.g., <img src=x onerror=alert(document.cookie)>) can execute arbitrary JavaScript in the browser of every user who views the post. On a social platform, a self-propagating XSS payload can spread to thousands of users through the feed.

Steps to Reproduce

  1. Create a post with the body: <script>alert("XSS")</script>
  2. View the feed as another user
  3. Observe the script executes in the victim's browser

Proposed Fix

Sanitize post content on the server before storage AND before rendering:

const createDOMPurify = require("dompurify");
const { JSDOM } = require("jsdom");

const window = new JSDOM("").window;
const DOMPurify = createDOMPurify(window);

const safeContent = DOMPurify.sanitize(req.body.content, {
  ALLOWED_TAGS: ["b", "i", "em", "strong", "a", "p", "br"],
  ALLOWED_ATTR: ["href"],
});

Never trust client-side sanitization alone. Always sanitize on the server side.

Complexity: Level 3 | Program: GSSOC '26

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions