Cookie settings

Choose which cookies Topscan can use. Essential cookies are always on. You can change your choice anytime in Cookie settings at the bottom of the page. Read our Cookie Policy.

Essential

Required for Topscan to work: sign-in, security and remembering your cookie choice

Analytics

Help us understand how visitors use Topscan

Advertising

Measure our ad campaigns and show relevant ads

Web Application Security

What Is Cross-Site Scripting (XSS)?

11 min read

Cross-site scripting lets malicious code run in another user's browser through a vulnerable web application. Learn where the flaw starts, how to assess its impact, and which defenses to apply.

Aleksandr Melnichuk

Aleksandr Melnichuk

CPO, Project Director

Cross-Site Scripting

Cross-site scripting (XSS) is a web application vulnerability that lets an attacker run code, typically JavaScript, in another user's browser under the affected site's origin. It occurs when a page or its client-side code handles untrusted data in a way that lets the browser execute it. The script can then interact with the page and use whatever access the browser has in that session.

How Cross-Site Scripting Works

A site might display a search term beside its results, load a saved comment, or render a label from an API. Each value starts as data. XSS becomes possible when the application puts that data somewhere the browser can interpret it as markup or code. In a server-rendered page, the error can occur while generating the response. In a browser-rendered page, it can occur later, when client-side JavaScript updates the page.

A three-stage diagram shows data reaching the page, unsafe rendering, and possible code execution under the affected site's origin
An XSS flaw can turn untrusted data into code that runs in the browser under the affected site's origin.

The CWE-79 entry for cross-site scripting describes this failure to make untrusted input safe for a web page. The affected browser origin, which combines scheme, host, and port, determines what page data and browser resources the script can access, subject to browser and application controls. That is why the same rendering flaw can have different consequences on a public help page and in a signed-in support console.

The abbreviation XSS keeps cross-site scripting distinct from Cascading Style Sheets (CSS). The OWASP Top 10:2025 lists CWE-79 under A05: Injection. Other types of injection follow a similar pattern at a different point in the application, such as when a database interprets data as part of a query.

Reflected, Stored, and DOM-Based XSS

Reflected and stored XSS describe how untrusted data reaches the affected page. DOM-based XSS identifies where client-side code creates the unsafe rendering. OWASP's XSS classification explains why these labels can overlap. A saved value, for example, can cause a browser-side flaw when an application fetches and renders it later.

Reflected XSS

Reflected XSS can occur when an application puts request data into its immediate response in an unsafe context. Think of a search results page that displays the submitted phrase above the results. If that label is rendered as page markup, the browser may interpret part of the phrase as active content. The fix belongs in the response template or component that displays the phrase.

Stored XSS

Stored XSS starts with data the application keeps and displays in a later visit. A customer may enter a ticket subject that later appears in a support queue. If the queue treats the saved subject as markup, the view can expose each staff member who opens it. Editing the ticket form leaves existing records in place, so the team needs to fix the rendering and check other views of the same data.

DOM-Based XSS

DOM-based XSS occurs when browser-side code uses untrusted data to update the Document Object Model (DOM) in a way that can execute code. A dashboard might read a filter label from its URL and show it after the page has loaded. When the label is plain text, the component should use a text-only operation such as textContent. The original HTML response may look fine because the error happens during the later update.

Two columns compare reflected and stored delivery with unsafe DOM rendering. A saved value can trigger both stored and DOM-based XSS
Reflected and stored describe how data reaches the page. DOM-based identifies unsafe rendering in client-side code, which can occur with stored data.

Common Places Where XSS Appears

Follow each value to the point where a user sees it. A name may be safe on a profile page and unsafe in a separate admin preview because the two screens use different rendering components. During a review, group these paths by how the application handles the data.

  • Request data. Search terms, form values, and URL parameters often reappear in page labels and error messages. Cookies and HTTP headers also count as untrusted when the application displays them, including in diagnostics viewed by staff.
  • Saved or imported content. Comments, reviews, profile fields, support records, and data returned by an API may appear in several views. Review the public page, preview, and moderation screen separately when they use different components.
  • Custom rendering. Legacy templates, rich-text editors, and third-party widgets deserve review when they turn data into HTML or write to the DOM directly. Review third-party scripts as dependencies too. A compromised script that the site already loads has a separate trust path from an XSS flaw in the widget's rendering code.

Impact of XSS on Users and Organizations

The impact of an XSS attack depends on what the affected page exposes and which actions the current account can take. Within those limits, script execution can expose data displayed on the page, change a form into a phishing prompt, or carry out requests as the signed-in user. Session hijacking becomes possible if the script can obtain a reusable session secret. Some applications expose enough data and actions for serious harm even when the session cookie is protected from direct script access.

Prioritize a verified flaw in a view that handles personal data or supports powerful actions, such as issuing refunds or changing account settings. A stored issue can reach many users if they share the same view. A reflected flaw that reaches an administrator can also be urgent. If evidence points to actual exposure of customer data, assess the scope with the incident response team and involve the privacy or compliance owner as appropriate. The finding itself establishes a flaw. Evidence about affected sessions, data access, and account activity guides the incident response.

Roundcube Webmail and the Risk of Reprocessing HTML

In August 2024, Roundcube's security release for versions 1.6.8 and 1.5.8 fixed CVE-2024-42009, an XSS flaw in the way the webmail application processed HTML after sanitizing it. The flaw could let an attacker read or send mail using an affected account. In June 2025, NHS England's Roundcube advisory reported that the vulnerability had been exploited in the wild.

The case gives teams a concrete review question. Does another step rewrite formatted content after the sanitizer runs? If a preview, link formatter, or mail viewer changes cleaned HTML before displaying it, include the final rendered view in the regression test. The same review applies when several components transform the content in sequence.

Detecting Cross-Site Scripting

Use code review to trace the data and browser checks to observe how the application renders it. Start with shared rendering components and views restricted to signed-in users. A scanner finding, a reflected string, and evidence of script execution are different observations. Give each finding a status that reflects the evidence behind it.

  • Static application security testing (SAST) analyzes code for unsafe data flows and rendering calls. Run it during code review and check whether the result reaches a real display path. Framework wrappers and custom sanitizers may require a developer to inspect the flow.
  • Dynamic application security testing (DAST) examines a running application. Coverage depends on the URLs, states, account roles, and browser interactions the scanner can reach. Before relying on a result for a staff feature, check that the scan reached the signed-in view. The web application vulnerability scanning guide explains how to review scan scope. Our web application security testing includes opt-in deep scans that check for XSS in a running application.
  • Interactive application security testing (IAST) observes an instrumented application while tests exercise it. The OWASP IAST guidance describes the role of sensors in the running application. Include the workflows and roles you want covered in the test suite, including views reached only after saving a record.

In a regression test for your own support queue, use an ordinary label containing angle brackets and an ampersand, such as R&D <draft>, as a text fixture. Assert that the browser displays the exact value in the ticket subject and that it remains text in the rendered page. Run the same check in the customer view and staff preview if they use separate components. A passing test protects the specific display behavior it exercises.

For production monitoring, correlate unusual input-validation failures, WAF alerts, or Content Security Policy (CSP) reports with the affected route, release, and user role where appropriate. A deployment can cause CSP reports when legitimate scripts are blocked, so use the signal to decide what to investigate. Follow OWASP's logging guidance on data to exclude when recording request details. Session secrets and sensitive customer content belong outside those logs.

Preventing Cross-Site Scripting

The primary defense is to keep untrusted values out of executable browser contexts. Render plain values as text, sanitize allowed HTML, and keep user data away from inline scripts and event handlers. Choose the control based on what the feature actually needs to display.

What the Feature Displays

Primary Control

What to Check in Review

Plain text, such as a ticket title

Keep the template's escaping enabled or use a text-only DOM API.

Check that every display view shows the value literally.

A user-supplied link

Validate the URL against the allowed schemes and apply the encoding required for the output context.

Verify which destinations the feature permits and how it handles invalid links.

Formatted content, such as a message preview

Use a maintained HTML sanitizer with rules that match the allowed markup.

Verify the content at the final rendering step, including any transformations after sanitization.

Output Encoding, Input Validation, and Framework Defaults

OWASP's XSS prevention guidance explains why HTML text, attributes, and URLs have different encoding rules. Use the framework's context-aware tools and redesign components that put variable data in executable contexts. Input allow-lists still have a role. A field that expects an order number can accept only its defined format. Free-text fields need to accept ordinary punctuation, so protect them at each point of display.

Keep framework safeguards in place during ordinary rendering. React and Vue, for example, escape text rendered through their usual template mechanisms. Review any call that explicitly inserts HTML. React's guidance on rendering raw HTML describes where that responsibility shifts to the developer. For a feature that needs bold text and lists, define the allowed markup and use a maintained sanitizer. Put those rules in a shared component so each view applies them consistently.

Our recommendation for a legacy screen with several custom renderers is to convert fields that only show labels to plain text first. That reduces the number of views that need HTML sanitization. Check the feature requirements before changing what users can display. Keep an explicit sanitizer and a browser regression test for any view that still renders user-supplied HTML.

Content Security Policy and Trusted Types

A restrictive Content Security Policy can limit which scripts the browser runs and reduce the consequences of some injection flaws. If you are rolling a policy out on an existing site, use report-only mode to see which legitimate features it would affect, then move to enforcement after review. Report-only mode records violations without blocking them. Fix the underlying unsafe rendering as part of the same work.

On supporting browsers, Trusted Types can enforce the use of approved policies for certain DOM operations that interpret strings as HTML or code. The policy is yours to implement and review. Check your browser support requirements and make sure each policy applies the intended transformation, such as sanitizing allowed HTML.

Session Cookies and Request Filtering

The session cookie attributes serve different purposes. HttpOnly prevents JavaScript from reading the cookie directly. Secure restricts transmission to secure connections, with a localhost exception. SameSite controls when the cookie accompanies a cross-site request. A script already running under the affected origin may still make requests using the active session, so keep the rendering fix at the top of the list.

A web application firewall (WAF), including one within a web application and API protection (WAAP) service, can flag or block some suspicious requests. Its visibility depends on the traffic it inspects. A browser-only data flow may never reach it. Use the WAF as an additional control while engineers correct the component and verify the result.

Fix the Rendering Path and Verify the Repair

Set rules for text, URLs, and formatted HTML before teams build new components. The OWASP Application Security Verification Standard gives reviewers a common basis for requirements. During code review, look for places that bypass normal rendering, then use static checks in continuous integration (CI) and run defined browser checks against a test deployment. Add examples from resolved findings to the team's review checklist.

For a confirmed issue, record which component and roles are affected, assign an owner, and set a remediation deadline that reflects the consequences. Fix the shared renderer where possible. Retest the original view, the roles that use it, and any sibling views that consume the same data. Add a regression test and rescan the relevant application scope.

A checklist calls for retesting affected views and roles, checking other views that render the same data, and adding a regression test and rescan
Retest affected views and roles, add a regression test, and rescan the affected scope before closing the XSS finding.

Our guide to website vulnerability testing covers how to verify a finding and record a retest. Close the issue when the evidence shows that the affected rendering behavior has changed.

FAQ

How is XSS different from CSRF?
-

Cross-site request forgery (CSRF) causes a user's browser to submit an unwanted request to a site where the user already has a session. XSS lets injected code run under the site's origin. Because that code can interact with the application like other scripts on the page, it can undermine some CSRF defenses. Follow OWASP's CSRF prevention guidance and track both issues separately when reviewing web application vulnerabilities.

Can a JSON API contribute to XSS?
+

Yes. A JSON API can supply a value that a web client later inserts into an unsafe part of a page. The browser-facing application determines how the value is rendered. For example, a profile API can safely return a display name as JSON while a separate admin client handles the name unsafely. Serve JSON with the intended media type and test the web client that displays the data.

Can XSS affect mobile apps?
+

Mobile apps that display web content in an embedded browser can be affected when that content includes an XSS flaw. Review which content the embedded browser can load and whether it can call native app functions through a bridge. The Android guidance on cross-app scripting recommends restricting WebView capabilities to what the feature requires. Include the embedded view in regression testing when it renders user-generated content.

Is HTML injection the same as XSS?
+

HTML injection means untrusted data can add or modify markup in a page. XSS involves a path to script execution. An injected heading or link can still mislead a user if the browser treats it only as markup. Document what you observed and record the two outcomes separately during triage. For a field intended to show plain text, verify that the fix prevents the value from becoming markup at all.

What should we do with data saved before an XSS fix?
+

Start by checking whether old records still reach other renderers, including moderation queues, archives, and exports that open in a browser. Fix each affected display path and verify the change using representative historical formats in a test environment. If a cleanup is needed, plan it separately with the data owner. A rendering change can protect existing records from unsafe display while preserving legitimate customer text and formatting.

5.0

based on 1 rating

Related articles
What Is SQL Injection
Aleksandr Melnichuk・13 min read
Web Application Security
Injection Attack Types
Aleksandr Melnichuk・10 min read
Web Application Security