import React from 'react';
import { render } from '../test-utils';
import { Markup } from './Markup';
/**
* Proof of Concept: Markup Injection via Attacker-Controlled Data
*
* These tests demonstrate Laszlo's concern: when untrusted data (e.g. a payee name,
* company name, or any string from an external source) is interpolated into a markup
* template string, the attacker can break out of their structural context and inject
* arbitrary allowed markup — links, emphasis, etc.
*
* This is NOT about XSS (script execution is blocked). It's about content spoofing
* and phishing within trusted UI.
*/
describe('Markup injection via attacker-controlled data', () => {
describe('the vulnerability: structural injection', () => {
it('default allowedHrefs blocks injected external links', () => {
const attackerControlledName =
'Acme Corp' +
'' +
'ACTION REQUIRED: Verify your identity' +
'' +
'';
const template = `Transfer £35.00 to ${attackerControlledName}`;
const { container } = render({template});
// The default allowlist blocks external domains — the link is NOT rendered
const injectedLink = container.querySelector('a[href="https://evil.example.com"]');
expect(injectedLink).not.toBeInTheDocument();
// But the structural injection still works (emphasis renders)
const injectedEmphasis = container.querySelector('.wds-markup-emphasis--important');
expect(injectedEmphasis).toBeInTheDocument();
expect(injectedEmphasis).toHaveTextContent('ACTION REQUIRED: Verify your identity');
});
it('injected external links render when allowLinks="all"', () => {
const attackerControlledName =
'Acme Corp' +
'' +
'ACTION REQUIRED: Verify your identity' +
'' +
'';
const template = `Transfer £35.00 to ${attackerControlledName}`;
const { container } = render({template});
// With allowLinks="all", the link renders — demonstrating the structural injection
const injectedLink = container.querySelector('a[href="https://evil.example.com"]');
expect(injectedLink).toBeInTheDocument();
});
it('default allowedHrefs blocks phishing links in paragraph injection', () => {
const attackerNote =
'Thanks!
' +
'
Your account has been compromised. ' +
'Contact support at ' +
'this secure link
' +
'
';
const template = `
Note from sender: ${attackerNote}
`;
const { container } = render({template});
const phishingLink = container.querySelector('a[href="https://phishing.example.com"]');
expect(phishingLink).not.toBeInTheDocument();
});
it('attacker can inject misleading accessibility labels', () => {
const attackerValue =
'normal text' +
'' +
'here' +
'' +
'';
const template = `Your balance: ${attackerValue}`;
const { container } = render({template});
const misleadingElement = container.querySelector(
'[aria-label="Security alert: click to verify"]',
);
expect(misleadingElement).toBeInTheDocument();
});
});
describe('what IS blocked (XSS / script execution)', () => {
it('cannot inject arbitrary HTML elements', () => {
const xssAttempt = '';
const template = `Hello ${xssAttempt}`;
const { container } = render({template});
// is not in the allow-list, so it's rendered as text
expect(container.querySelector('img')).not.toBeInTheDocument();
expect(container).toHaveTextContent(//);
});
it('cannot inject script tags', () => {
const xssAttempt = '';
const template = `Hello ${xssAttempt}`;
const { container } = render({template});
expect(container.querySelector('script')).not.toBeInTheDocument();
});
it('blocks javascript: URLs in links', () => {
// Even if attacker injects a link, javascript: URLs are blocked
const attack = 'clickme';
const template = `${attack}`;
const { container } = render({template});
// The link renders but without the dangerous href (falls back to Fragment)
expect(container.querySelector('a[href*="javascript"]')).not.toBeInTheDocument();
});
});
describe('the general nature of this problem', () => {
it('is identical to HTML injection via innerHTML', () => {
// This is the same class of bug — mixing code and data in a string
const attackerName = 'URGENT';
const div = document.createElement('div');
div.innerHTML = `Hello ${attackerName}`;
// The attacker's data became structure
expect(div.querySelector('b')).not.toBeNull();
expect(div.querySelector('b')!).toHaveTextContent('URGENT');
});
it('react JSX prevents this by design', () => {
// In contrast, React treats variables as pure data — never as structure
const attackerName = 'URGENT';
const { container } = render(Hello {attackerName});
// React escapes the string — it renders as visible text, not as a tag
expect(container.querySelector('strong')).not.toBeInTheDocument();
expect(container).toHaveTextContent(/URGENT<\/strong>/);
});
it('demonstrates why Markup reintroduces the problem React solved', () => {
// The same attacker string that React handles safely...
const attackerName = 'URGENT: Call +44 123 456 789 NOW';
// ...becomes dangerous when passed through Markup's string parser
const { container } = render({`Hello ${attackerName}`});
// The attacker's data has been promoted to structure
const injected = container.querySelector('strong');
expect(injected).toBeInTheDocument();
expect(injected).toHaveTextContent('URGENT: Call +44 123 456 789 NOW');
});
});
});