DOM Cross-Site Scripting Via postMessage in AnnounceKit
AnnounceKit's script listens for postMessage events and injects the payload's CSS into a style element using innerHTML without escaping. An attacker could send a crafted message from an untrusted origin to inject HTML into the page, potentially leading to script execution or content injection on customer domains using custom hostnames.
Published on doyensec.com
Overview This document summarizes a security issue affecting the AnnounceKit platform incidentally discovered during a larger vulnerability research activity targeting a Doyensec customer. While security testing was not meant to be comprehensive in terms of attack and code coverage for AnnounceKit, we have identified a…
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
AnnounceKit is a user communication platform that provides product updates. Customers embed it in their own web applications, and some of them serve the change-log page through a custom hostname, documented by the vendor at https://announcekit.app/docs/custom-host, which relies on a CNAME record pointing at AnnounceKit infrastructure.
The advisory covers a DOM-based cross-site scripting issue in the component updates.targetapp.com, classified as Cross Site Scripting (XSS) with a severity of Medium. The vendor is Kovan Studio, Inc. The status at the time of writing is Open, and no CVE was assigned.
The issue was found incidentally during a larger vulnerability research activity targeting a Doyensec customer. Testing was not meant to be comprehensive in terms of attack and code coverage for AnnounceKit, so the findings should be read as a sample rather than a full audit. Because a number of AnnounceKit customers serve the vulnerable code through the Custom Hostname setup, the same flaw allows a universal HTML injection on all of their origins.
Setup
The precondition is a customer web application that integrates AnnounceKit and uses a custom hostname for the change-log page, configured with a CNAME record. The vulnerable component observed during the research was updates.targetapp.com.
The relevant client-side code is served from https://cdn.announcekit.app/7a65b93555e5c78cdf5d.js. The attack itself needs no special tooling: a page hosted by the attacker, a browser, and a logged-in session on the target application. The proof of concept uses an HTML page with a form and an iframe, and drives the vulnerable listener with postMessage calls.
Finding it
The switch expression in the script creates a style element and injects the message payload content directly into the DOM using the innerHTML native function, with no HTML escaping:
window.addEventListener("message",(e=>{ var o,i;switch(e.data.event){ ... case "R2L_PUT_CSS": let f, l = document.querySelector("head"); e.data.payload.id && (f = document.getElementById(`injectedstyle-$ {e.data.payload.id}`)), f || (f = document.createElement("style"), f.id = `injectedstyle-${e.data.payload.id}`, f.type = "text/css"), f.innerHTML = e.data.payload.css, l.appendChild(f); break;
The listener accepts any message event and dispatches on e.data.event. For the R2L_PUT_CSS event it looks up an existing element by the id injectedstyle-<payload.id>, creates a style element with that id and type text/css if none exists, assigns e.data.payload.css to innerHTML, and appends the element to the document head. Nothing in the shown code checks MessageEvent.origin or the source window, so any origin that can get a reference to the window can send the message. The payload is treated as markup rather than as CSS text, which is what turns a styling message into HTML injection.
Exploitation
Step 1: host the proof-of-concept page
The attacker hosts the following HTML, for example at http://attacker.com/xss.html:
<html>
</head> <body>
<input type="submit" value="Submit request">
</form> <iframe name="framepoc" id="framepoc" src="#"></iframe> <script>
updatekitForm = document.querySelector("form[name=announcekitForm]");
updatekitForm.addEventListener('submit', function(e) {
setTimeout(function() {
let frame = window.document.getElementById("framepoc");
frame.contentWindow.postMessage(
{ "event": "READY", "payload": {} },
"*"
);
frame.contentWindow.postMessage(
{
}, "*"
"event": "R2L_PUT_CSS", "payload": { "css": "<PAYLOAD>", "id": "main" }
);
}, 2000);
});
</script> </body> </html>
The page contains a form named announcekitForm and an iframe with the id framepoc. On submit, it waits two seconds and then sends two messages to the iframe's window with a target origin of *: first a READY event with an empty payload, then an R2L_PUT_CSS event whose payload carries the attacker-controlled css value and the id main. The READY message is sent first, then the R2L_PUT_CSS message.
Step 2: log in to the target app
The victim authenticates to the target application, so that the AnnounceKit widget runs in the context of updates.targetapp.com with the victim's session.
Step 3: visit the hosted page
The victim visits the attacker-hosted HTML and the messages are delivered to the AnnounceKit frame.
Step 4: observe the injection
The payload is injected without escaping into a #injectedstyle-main style element in the context of the updates.targetapp.com domain. The element is created in the document head, which is where the impact discussion picks up.
Root cause
The message event handler trusts the content of incoming messages and writes e.data.payload.css into a style element through innerHTML. Since innerHTML parses its input as HTML, markup in the css field becomes DOM nodes rather than being confined to stylesheet text. The handler does not validate the origin of the sender, so any page that can obtain a window reference to the widget, here through an iframe, can deliver the message. The proof of concept sends a READY event before the styling message.
Impact
Because the injection occurs inside the head tag, exploitability seems limited on modern user agents. The HTML5 specification states that if a <script> tag is inserted into the page using the innerHTML property of an element, it should not be executed. That restriction can usually be bypassed by using anything other than a <script> tag, for example <svg> or <img> tags, or by injecting other tags. While arbitrary JavaScript execution may not be easily achievable on newer user agents, content injection and other attacks can still be mounted on modern user agents.
The complexity is rated Medium: the attacker must force the victim into visiting a specific URL first. Where customers use the Custom Hostname setup, the injection applies to all of their origins, which is what makes the issue universal for those deployments.
Fix
As a short-term mitigation, customers should stop using the custom host setup suggested by AnnounceKit. As a long-term mitigation, AnnounceKit should always check the origin of postMessage events against a list of expected domains, and should allow AnnounceKit customers to provide an allowlist of message senders and frame ancestors. This could be achieved by checking the MessageEvent.origin attribute, which contains the URL of the page that sent the postMessage, and by returning a CSP header containing a relevant frame-ancestors directive covering the intended customer origins.
According to the disclosure timeline, the issue was responsibly disclosed to AnnounceKit on 01/07/2021 and AnnounceKit deployed a preliminary fix on 01/18/2021. The document is dated 01/07/2022 and lists the status as Open.
Variants to test
- Enumerate other
eventvalues handled by the same switch and send each one with a crafted payload. A positive result is any branch that writes attacker-controlled data into the DOM without escaping. - Send
R2L_PUT_CSSwith differentidvalues. A positive result is a new#injectedstyle-<id>element created inheadcontaining the supplied markup. - Send the styling message without the preceding
READYmessage, and with different orderings or delays. A positive result is that the injection still lands, which shows theREADYstep is not a real precondition. - Deliver the message from an origin other than the attacker's own page, for example a sandboxed iframe or a page on a different host. A positive result is that the handler still processes it, confirming the missing origin check.
- Try markup that does not rely on
<script>, such as<svg>or<img>tags, inside thecssfield. A positive result is that the element is created and its event handlers or resource loads fire in the target origin. - Check whether a CSP
frame-ancestorsdirective is returned on the custom-hostname deployment. A positive result is its absence, meaning the widget can be framed by arbitrary origins.
What happened, in plain words
AnnounceKit is a service that companies use to show news and product updates to their own users. A company signs up with AnnounceKit, and AnnounceKit gives it a small widget (a piece of code) that the company puts on its website. When visitors come to the company's site, the widget fetches the latest updates from AnnounceKit and displays them.
Some companies do not want the updates page to live on an AnnounceKit address. They want it on their own domain, for example updates.targetapp.com. AnnounceKit supports this through a feature called a custom hostname. The company adds a DNS record (a CNAME record) that points its own subdomain at AnnounceKit's servers, so visitors see the company's address while the content is still served by AnnounceKit.
While a security team at Doyensec was testing a completely different customer's website, they noticed something odd in the AnnounceKit widget. The widget listens for messages sent by other pages in the browser. One of those message types is meant to let a page change the styling (the colours, fonts and layout) of the widget. The widget takes the styling text from the message and drops it straight into the page without checking where the message came from or cleaning up what it contains.
That means an attacker who can get a victim to open a page they control can send a message to the widget and have their own HTML (the language web pages are written in) inserted into the victim's page. This kind of flaw is called DOM-based cross-site scripting, or DOM XSS. Because many AnnounceKit customers use the custom hostname setup, the same flaw affects all of their domains, not just one.
The advisory rates the severity as Medium, the vulnerability class as Cross Site Scripting (XSS), the vendor as Kovan Studio, Inc. and the status as Open, with no CVE number assigned. A CVE is a public identifier that security researchers use to track a specific vulnerability.
The finding was incidental. The Doyensec team was not doing a full audit of AnnounceKit, so the report should be read as a sample of what they found, not a complete picture of the product's security.
Setup
To reproduce the issue you need three things:
- A customer website that uses AnnounceKit and serves the change-log page through a custom hostname, set up with a CNAME record.
- A browser with a logged-in session on that customer site.
- A page the attacker controls, hosted somewhere on the internet.
The component the researchers looked at was updates.targetapp.com. The vulnerable code is a JavaScript file served from https://cdn.announcekit.app/7a65b93555e5c78cdf5d.js. JavaScript is the programming language that runs inside web pages in your browser. A CDN (content delivery network) is a service that hosts files so they load quickly for visitors around the world.
No special tools are needed. The proof of concept is a plain HTML page with a form and an iframe. An iframe is a way to embed one web page inside another, like a small window inside a bigger window. The page uses postMessage, which is a browser feature that lets one page send a message to another page, even when they come from different websites.
Finding it
The researchers read the JavaScript file and found a piece of code that listens for incoming messages. Here it is:
window.addEventListener("message",(e=>{ var o,i;switch(e.data.event){ ... case "R2L_PUT_CSS": let f, l = document.querySelector("head"); e.data.payload.id && (f = document.getElementById(`injectedstyle-$ {e.data.payload.id}`)), f || (f = document.createElement("style"), f.id = `injectedstyle-${e.data.payload.id}`, f.type = "text/css"), f.innerHTML = e.data.payload.css, l.appendChild(f); break;
Line by line, in plain words:
window.addEventListener("message", ...)tells the browser: whenever a message arrives at this page, run the function that follows. Theeis the event object, which holds the message and information about who sent it.switch(e.data.event)looks at a field calledeventinside the message and decides what to do based on its value. Think of it like a switchboard operator routing calls to different desks.case "R2L_PUT_CSS":is the branch that handles a message asking the widget to apply some CSS. CSS is the language that describes how a page should look.let f, l = document.querySelector("head");finds the<head>section of the page. The head is the part of an HTML document that holds information about the page rather than visible content.e.data.payload.id && (f = document.getElementById(...))checks whether the message includes anidand, if so, looks for an existing element with the nameinjectedstyle-<that id>. If the element already exists, it is reused.f || (f = document.createElement("style"), ...)runs only if no such element was found. It creates a new<style>element, gives it the idinjectedstyle-<id>, and sets its type totext/css.f.innerHTML = e.data.payload.csstakes thecssfield from the message and puts it inside the style element usinginnerHTML.innerHTMLis a browser property that treats whatever you give it as HTML markup, not as plain text. This is the critical line: the message content is treated as HTML.l.appendChild(f)adds the new element to the page's head.break;ends this branch of the switch.
Nothing in this code checks MessageEvent.origin, which is the field that tells you which website sent the message. Nothing checks the source window either. So any page that can get a reference to the widget's window can send it a message. Because innerHTML parses its input as HTML, a message that was supposed to carry CSS can instead carry HTML tags, and those tags become real elements in the page. That is how a styling message turns into HTML injection.
Exploitation
Step 1: host the proof-of-concept page
The attacker puts the following HTML on a server they control, for example at http://attacker.com/xss.html:
<html>
</head> <body>
<input type="submit" value="Submit request">
</form> <iframe name="framepoc" id="framepoc" src="#"></iframe> <script>
updatekitForm = document.querySelector("form[name=announcekitForm]");
updatekitForm.addEventListener('submit', function(e) {
setTimeout(function() {
let frame = window.document.getElementById("framepoc");
frame.contentWindow.postMessage(
{ "event": "READY", "payload": {} },
"*"
);
frame.contentWindow.postMessage(
{
}, "*"
"event": "R2L_PUT_CSS", "payload": { "css": "<PAYLOAD>", "id": "main" }
);
}, 2000);
});
</script> </body> </html>
What each part does:
<input type="submit" value="Submit request">is a button the visitor can click. It is part of a form.<iframe name="framepoc" id="framepoc" src="#"></iframe>creates an empty frame on the page. Thenameandidlet the script find it later. Thesrc="#"means it starts with no real content.updatekitForm = document.querySelector("form[name=announcekitForm]");finds a form namedannouncekitFormon the page. The proof of concept looks for a form namedannouncekitFormon the page.updatekitForm.addEventListener('submit', function(e) { ... })says: when that form is submitted, run the code inside.setTimeout(function() { ... }, 2000);waits two seconds before running the inner code. The delay gives the embedded widget time to load.let frame = window.document.getElementById("framepoc");grabs the iframe element.frame.contentWindow.postMessage({ "event": "READY", "payload": {} }, "*");sends a message to the page inside the iframe. The message says the event isREADYand the payload is empty. The"*"means the sender does not restrict which origin can receive the message. TheREADYmessage is sent first, then the styling message.frame.contentWindow.postMessage({ "event": "R2L_PUT_CSS", "payload": { "css": "<PAYLOAD>", "id": "main" } }, "*");sends the second message. This one asks the widget to apply CSS. Thecssfield holds the attacker's payload, and theidismain.
Step 2: log in to the target app
The victim signs in to the customer's website as usual. This matters because the AnnounceKit widget then runs in the context of updates.targetapp.com with the victim's session. A session is the browser's way of remembering that you are logged in, usually through a cookie or a token. If the attacker can run code in that context, they can act as the victim.
Step 3: visit the hosted page
The victim opens the attacker's page, and the script waits two seconds and sends the two messages to the AnnounceKit frame.
Step 4: observe the injection
The payload ends up inside a #injectedstyle-main style element on the updates.targetapp.com domain, without any escaping. Escaping means turning special characters into harmless text so they cannot be interpreted as code. Here nothing does that, so the payload is treated as markup. The element is created inside the page's <head>, which is where the impact discussion begins.
Root cause
The message handler trusts whatever arrives. It takes e.data.payload.css and writes it into a style element through innerHTML. Because innerHTML parses its input as HTML, any markup in the css field becomes real elements in the page instead of staying as stylesheet text. The handler never checks the origin of the sender, so any page that can get a window reference to the widget, here through an iframe, can deliver the message. The proof of concept sends a READY event before the styling message.
Impact
Because the injection happens inside the <head> tag, the researchers judged that exploiting it on modern browsers is limited. The HTML5 specification says that if a <script> tag is inserted into a page using the innerHTML property, it should not execute. That restriction can usually be bypassed by using something other than a <script> tag, for example <svg> or <img> tags, or by injecting other tags. So while running arbitrary JavaScript may not be easy on newer browsers, content injection and other attacks can still be mounted on modern browsers.
The complexity is rated Medium: the attacker must first force the victim to visit a specific URL. Where customers use the custom hostname setup, the injection applies to all of their origins, which is what makes the issue universal for those deployments.
Fix
As a short-term mitigation, customers should stop using the custom host setup suggested by AnnounceKit. As a long-term mitigation, AnnounceKit should always check the origin of postMessage events against a list of expected domains, and should let customers provide an allowlist of message senders and frame ancestors. An allowlist is a list of things that are explicitly permitted, everything else is refused. This could be done by checking the MessageEvent.origin attribute, which holds the URL of the page that sent the message, and by returning a CSP header with a relevant frame-ancestors directive covering the intended customer origins. CSP stands for Content Security Policy, a set of rules a server sends to the browser to say what the page is allowed to load or be embedded by.
According to the disclosure timeline, the issue was responsibly disclosed to AnnounceKit on 01/07/2021 and AnnounceKit deployed a preliminary fix on 01/18/2021. The document is dated 01/07/2022 and lists the status as Open.
Variants to test
- List the other
eventvalues handled by the same switch and send each one with a crafted payload. A positive result is any branch that writes attacker-controlled data into the page without escaping. - Send
R2L_PUT_CSSwith differentidvalues. A positive result is a new#injectedstyle-<id>element created in the head containing the supplied markup. - Send the styling message without the preceding
READYmessage, and with different orderings or delays. A positive result is that the injection still lands, which shows theREADYstep is not a real precondition. - Deliver the message from an origin other than the attacker's own page, for example a sandboxed iframe or a page on a different host. A positive result is that the handler still processes it, confirming the missing origin check.
- Try markup that does not rely on
<script>, such as<svg>or<img>tags, inside thecssfield. A positive result is that the element is created and its event handlers or resource loads fire in the target origin. - Check whether a CSP
frame-ancestorsdirective is returned on the custom-hostname deployment. A positive result is its absence, meaning the widget can be framed by arbitrary origins.
Glossary
- AnnounceKit: a user communication platform that provides product updates, embedded by customers in their own web applications.
- API: a defined way for one piece of software to talk to another, like a menu of requests a service accepts.
- CNAME record: a DNS entry that points one domain name at another, so visitors to the first name are served content from the second.
- Cookie: a small piece of data a website stores in your browser, often used to remember that you are logged in.
- CSP (Content Security Policy): a set of rules a server sends to the browser describing what the page may load or be embedded by.
- CSS: the language that describes how a web page looks, including colours, fonts and layout.
- CVE: a public identifier used to track a specific security vulnerability.
- DOM: the browser's in-memory model of a web page, a tree of elements the page is made of.
- DOM-based XSS: cross-site scripting where the flaw is in client-side JavaScript that writes untrusted data into the page, rather than in the server's response.
- Escaping: converting special characters into harmless text so they cannot be interpreted as code.
- frame-ancestors: a CSP directive that says which origins are allowed to embed the page in a frame.
- HTML: the language web pages are written in, made of tags like
<head>,<body>and<img>. - HTML injection: inserting attacker-controlled HTML into a page so it becomes part of the page's structure.
- iframe: an element that embeds one web page inside another.
- innerHTML: a browser property that reads or writes the HTML inside an element, parsing whatever is assigned to it as markup.
- JavaScript: the programming language that runs inside web pages in the browser.
- MessageEvent.origin: the field on a message event that says which website sent the message.
- postMessage: a browser feature that lets one page send a message to another, even across different websites.
- Session: the browser's way of remembering that you are logged in, usually through a cookie or a token.
- Severity: a rating of how serious a vulnerability is, here Medium.
- Token: a piece of data used to prove identity or authorise an action, often stored in a cookie.
- Vendor: the company that makes and sells the product, here Kovan Studio, Inc.
- XSS (cross-site scripting): a class of vulnerability where an attacker gets their own code to run in another user's browser in the context of a trusted site.