XSS in WordPress via open embed auto discovery
A postMessage handler in WordPress's embed auto-discovery feature allowed an attacker-controlled embed page to send a message that set the top window's location to a javascript: URL. In Safari, the host check was bypassed because Safari returns a non-empty host for javascript: URLs, enabling XSS in the victim's WordPress site.
Published on research.securitum.com
Users often assume that known software is free of security flaws because it has been checked by a sufficient number of tools and security testers. However, this is not an assumption that a pentester or bug hunter can afford to make.
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
WordPress ships an embed feature: when you paste a URL into a post, the editor turns it into an embedded representation of the remote page. The embed is rendered inside an iframe, and the embedded page communicates back to the parent window with postMessage(), for example to report its height or to ask the parent to navigate to a link the user clicked inside the embed.
The handler that receives those messages lives in WordPress core, in the wp.receiveEmbedMessage function. The researcher's target was this handler, and the bug is a cross-site scripting issue that depends on how one particular browser parses URLs.
What postMessage() is, and why it matters here
postMessage() is part of the Web API. It allows cross-origin communication between Window objects: one window can send a message to another regardless of their origins. Used correctly it is safe. Used carelessly it becomes an attack surface, because the receiving handler has to decide for itself whether the sender and the payload can be trusted. That is exactly the class of mistake found here.
Setup
The write-up describes two sides:
- An attacker-controlled WordPress instance, used to host a malicious embed page.
- A victim WordPress site, viewed in Safari, where a post embeds a post from the attacker's site.
The precondition that makes the attack work is the browser: the behaviour exploited is specific to Safari. The report does not give WordPress versions beyond the fix references at the end, which point to the WordPress 6.2.1 maintenance and security release.
Finding it
The researcher started from the core postMessage handler and read it line by line. The handler, as quoted in the report:
if (c.wp.receiveEmbedMessage = function(e) {
var t = e.data;
if (t)
if (t.secret || t.message || t.value)
if (!/[^a-zA-Z0-9]/.test(t.secret)) {
for (var r, a, i, s = d.querySelectorAll('iframe[data-secret="' + t.secret + '"]'), n = d.querySelectorAll('blockquote[data-secret="' + t.secret + '"]'), o = 0; o < n.length; o++)
n[o].style.display = "none";
for (o = 0; o < s.length; o++)
if (r = s[o],
e.source === r.contentWindow) {
if (r.removeAttribute("style"),
"height" === t.message) {
if (1e3 < (i = parseInt(t.value, 10)))
i = 1e3;
else if (~~i < 200)
i = 200;
r.height = i
}
if ("link" === t.message)
if (a = d.createElement("a"),
i = d.createElement("a"),
a.href = r.getAttribute("src"),
i.href = t.value,
i.host === a.host)
if (d.activeElement === r)
c.top.location.href = t.value
}
}
}
Three observations stood out:
- The
secretmust be known, but it is provided as thelocation.hashof the embed webpage, so it is not a real obstacle. - Only the content window of the matching iframe can send the message, which is fine for the attacker, because the attacker controls that iframe's page.
- When the message attribute is
link, the handler does something far more interesting: it navigates the top window.
The dangerous line is c.top.location.href = t.value, where t is the postMessage data controlled by the attacker. Assigning attacker-controlled data to top.location.href obviously leads to XSS if the attacker uses javascript:alert(document.domain) as t.value. Before that happens, however, an important check is made:
if (a = d.createElement("a"),
i = d.createElement("a"),
a.href = r.getAttribute("src"),
i.href = t.value,
i.host === a.host)
This code checks whether the hostname in t.value is the same as the hostname of the embed page. It creates an <a> element, sets t.value as its href, and then reads the host attribute of the resulting URL. That approach is much better than regular-expression matching, but there is a browser-specific behaviour in Safari:
> var a = document.createElement("a")
> a.href="javascript://google.com/%0aalert(document.domain);//"
> console.log(a.host)
< google.com
All other browsers return an empty string when the javascript: scheme is used, but Safari does not. That difference lets an attacker use the javascript: scheme and still pass the host comparison, and then execute JavaScript in the top window, which is the victim's blog.
Exploitation
Step 1: Prepare an attacker-controlled WordPress instance
The attacker needs a WordPress site they control, so that they can serve a page that will be embedded on the victim site and that will send the malicious postMessage.
Step 2: Add the malicious script to the embed template
On the attacker's instance, edit wordpress/wp-includes/theme-compat/embed.php and add custom HTML:
<script>
if(document.location.hash.indexOf("secret") != -1) {
secret = document.location.hash.split("=")[1];
window.top.postMessage({"secret":secret,"message":"link","value":"javascript://"+document.location.host+"/%0aalert(document.domain);//"},"*");
}
</script>
The script reads the secret from the URL fragment of the embed page, which is how WordPress passes it to the embedded document. It then sends a message to the top window with three fields: the secret, message set to link, and value set to a javascript: URL whose host part is the attacker's own host, followed by %0a and the payload alert(document.domain). The %0a is a newline, which separates the host-looking part from the JavaScript that will actually run. The target origin is *, which is acceptable here because the attacker wants the message delivered to the parent window.
Step 3: Publish a post on the attacker blog
Create any post on the attacker blog, publish it, and note its URL. This is the URL that will be embedded on the victim site.
Step 4: Embed the attacker post on the victim site in Safari
On the victim WordPress site, in Safari, add a new post that embeds the post from the attacker's WordPress. The embed iframe loads the attacker's page, the page reads the secret from its fragment, and sends the link message to the parent.
Step 5: The handler navigates the top window
The parent handler receives the message. The secret matches the iframe's data-secret, and e.source is the iframe's contentWindow, so the sender check passes. Because message is link, the handler builds the two anchor elements and compares i.host with a.host. In Safari, i.host for the javascript://... URL is the attacker's host, so the comparison succeeds. The handler then assigns t.value to c.top.location.href, and the JavaScript runs in the top window. The alert executes.
Root cause
The handler trusts a URL's host property as a proxy for "this is a safe, same-host link". That assumption holds for ordinary schemes, but Safari parses javascript: URLs in a way that yields a non-empty host, so the check can be satisfied by a javascript: URL. The javascript: scheme is then assigned to top.location.href, which executes it in the origin of the top window, the victim's blog.
The other defences in the handler do not help: the secret is disclosed to the embed page by design, and the e.source === r.contentWindow check only proves that the message came from the iframe, which the attacker controls. The d.activeElement === r condition is part of the same branch.
Impact
An attacker who can get a victim site to embed an attacker-controlled post, and whose victim views that page in Safari, can execute JavaScript in the origin of the victim's WordPress site. In the proof of concept this is demonstrated with alert(document.domain). The report does not go further than that demonstration, and the attack depends on the Safari-specific URL parsing behaviour.
Fix
The report links to the WordPress 6.2.1 maintenance and security release and to the corresponding WPScan entry. The report does not describe the code change itself.
Variants to test
- Look for other
postMessagehandlers that validate a URL by reading.hostor.hostnamefrom an anchor element. A positive result is ajavascript:URL that yields a non-empty host in Safari and passes the comparison. - Test handlers that navigate
top.locationorwindow.locationfrom message data. A positive result is a navigation to ajavascript:URL that executes in the top window. - Check whether the
secretor nonce used to bind an embed to its iframe is exposed in the URL fragment or another readable location. A positive result is that an attacker-controlled embed page can read it and produce a message the handler accepts. - Test the same handler in different browsers. A positive result is a behaviour difference in URL parsing between browsers, as seen here between Safari and the others.
- Look for other places where a URL scheme is not checked explicitly before assignment to a navigation sink. A positive result is a
javascript:,data:or similar scheme reaching the sink.
What happened, in plain words
A security researcher found a way to run his own JavaScript code inside someone else's WordPress blog. WordPress is a very popular program for building websites: you install it, and it gives you an editor where you write posts. One of its features is embeds: if you paste a link to another page into a post, WordPress shows a small live copy of that page inside your post instead of a plain link.
That live copy is displayed in an iframe. An iframe is a little window inside a web page that loads a different page, like a picture frame hanging on a wall: the frame belongs to your page, but the picture inside comes from somewhere else. The embedded page and the page around it are two separate windows, and they need to talk to each other. For example, the embedded page needs to tell the outer page how tall it is, so the frame can be sized correctly. Browsers provide a way for windows to talk: postMessage().
The bug is that WordPress's code which listens for those messages could be tricked into running a javascript: link in the outer page. A javascript: link is a URL whose "address" is actually a piece of code; when a browser navigates to it, it runs the code. Running attacker code inside someone else's site is called cross-site scripting, or XSS. The trick only works in the Safari browser, because Safari reads one kind of URL differently from every other browser.
What postMessage() is, and why it matters here
An API is a set of rules that lets one program ask another program to do something, like a waiter taking your order to the kitchen: you do not go into the kitchen yourself, you use the agreed channel. The Web API is the collection of such channels that browsers offer to web pages.
postMessage() is one of them. It lets one window send a message to another window even when the two pages come from different websites. Normally a browser stops a page from one site from reading or changing a page from another site; that rule is called the same-origin policy, and it is a core safety wall of the web. postMessage() is a controlled door in that wall.
A door is only safe if the person receiving the message checks who sent it and what it contains. The receiving code has to decide for itself whether the sender and the payload can be trusted. That is exactly where the mistake was found: the WordPress handler trusted a value it should not have trusted.
Setup
The write-up describes two sides:
- An attacker-controlled WordPress instance, used to host a malicious embed page.
- A victim WordPress site, viewed in Safari, where a post embeds a post from the attacker's site.
The precondition that makes the attack work is the browser: the behaviour exploited is specific to Safari. The report does not give WordPress versions beyond the fix references at the end, which point to the WordPress 6.2.1 maintenance and security release.
Finding it
The researcher started from the core postMessage handler and read it line by line. The handler, as quoted in the report:
if (c.wp.receiveEmbedMessage = function(e) {
var t = e.data;
if (t)
if (t.secret || t.message || t.value)
if (!/[^a-zA-Z0-9]/.test(t.secret)) {
for (var r, a, i, s = d.querySelectorAll('iframe[data-secret="' + t.secret + '"]'), n = d.querySelectorAll('blockquote[data-secret="' + t.secret + '"]'), o = 0; o < n.length; o++)
n[o].style.display = "none";
for (o = 0; o < s.length; o++)
if (r = s[o],
e.source === r.contentWindow) {
if (r.removeAttribute("style"),
"height" === t.message) {
if (1e3 < (i = parseInt(t.value, 10)))
i = 1e3;
else if (~~i < 200)
i = 200;
r.height = i
}
if ("link" === t.message)
if (a = d.createElement("a"),
i = d.createElement("a"),
a.href = r.getAttribute("src"),
i.href = t.value,
i.host === a.host)
if (d.activeElement === r)
c.top.location.href = t.value
}
}
}
What this code does, in plain words:
c.wp.receiveEmbedMessage = function(e) {defines a function that will be called every time a message arrives. The browser hands it an event object, here namede.var t = e.data;takes the content of the message. Everything the sender put in the message is int.if (t)andif (t.secret || t.message || t.value)ignore empty messages and messages that have none of the three expected fields:secret,messageandvalue.if (!/[^a-zA-Z0-9]/.test(t.secret))requires thesecretto contain only letters and digits. A regular expression (regex) is a pattern for matching text; this one rejects anything with a character outsidea-z,A-Zand0-9.- The two
querySelectorAllcalls find, on the page, everyiframeand everyblockquotewhosedata-secretattribute equals the secret from the message. Adata-attribute is just a custom label a page can attach to an element to store a value. - The first loop hides all matching
blockquoteelements (style.display = "none"). - The second loop walks through the matching iframes.
e.source === r.contentWindowchecks that the message really came from that iframe's inner window.contentWindowis the window object of the page loaded inside the iframe. - If the sender matches, the iframe's inline style is removed, and if
messageis"height", the code readsvalueas a number, keeps it between 200 and 1000, and sets the iframe height to it. This is the normal, harmless use of the feature. - If
messageis"link", the code creates two link elements (<a>), puts the iframe'ssrc(its address) in the first and the message'svaluein the second, and compares theirhostvalues. Thehostof a URL is the domain name part, for exampleexample.com. - If the hosts match and the iframe is the element the user is currently focused on (
d.activeElement === r), the code setsc.top.location.href = t.value.topmeans the outermost window, the whole page;location.hrefis the address bar. So this line navigates the whole page to the URL in the message.
Three observations stood out:
- The
secretmust be known, but it is provided as thelocation.hashof the embed webpage, so it is not a real obstacle. Thehashis the part of a URL after the#sign. It is readable by the page itself, so the embedded page can simply read its own secret. - Only the content window of the matching iframe can send the message, which is fine for the attacker, because the attacker controls that iframe's page.
- When the message attribute is
link, the handler does something far more interesting: it navigates the top window.
The dangerous line is c.top.location.href = t.value, where t is the postMessage data controlled by the attacker. Assigning attacker-controlled data to top.location.href obviously leads to XSS if the attacker uses javascript:alert(document.domain) as t.value. Before that happens, however, an important check is made:
if (a = d.createElement("a"),
i = d.createElement("a"),
a.href = r.getAttribute("src"),
i.href = t.value,
i.host === a.host)
Line by line:
a = d.createElement("a")creates an empty link element. A link element is not just a clickable thing; the browser also uses it as a URL parser. If you set itshref, the browser fills in the parts of the URL: scheme, host, path and so on.i = d.createElement("a")creates a second one.a.href = r.getAttribute("src")puts the iframe's address into the first link.i.href = t.valueputs the attacker's value into the second link.i.host === a.hostcompares the two host names. The idea is: only allow a navigation if the link points to the same site as the embed.
This code checks whether the hostname in t.value is the same as the hostname of the embed page. It creates an <a> element, sets t.value as its href, and then reads the host attribute of the resulting URL. That approach is much better than regular-expression matching, but there is a browser-specific behaviour in Safari:
> var a = document.createElement("a")
> a.href="javascript://google.com/%0aalert(document.domain);//"
> console.log(a.host)
< google.com
This is a browser console session. The console is a built-in tool in every browser where you can type JavaScript and see the result immediately; the > lines are what was typed and the < line is the answer.
var a = document.createElement("a")makes an empty link element.a.href="javascript://google.com/%0aalert(document.domain);//"sets its address to ajavascript:URL. The%0ais an encoded newline character. Everything after it is the code that would run.console.log(a.host)asks the browser what host name it sees in that URL.< google.comis Safari's answer: it reportsgoogle.comas the host.
All other browsers return an empty string when the javascript: scheme is used, but Safari does not. That difference lets an attacker use the javascript: scheme and still pass the host comparison, and then execute JavaScript in the top window, which is the victim's blog.
Exploitation
Step 1: Prepare an attacker-controlled WordPress instance
The attacker needs a WordPress site they control, so that they can serve a page that will be embedded on the victim site and that will send the malicious postMessage. Without control of the embedded page, the attacker cannot send the message at all.
Step 2: Add the malicious script to the embed template
On the attacker's instance, edit wordpress/wp-includes/theme-compat/embed.php and add custom HTML:
<script>
if(document.location.hash.indexOf("secret") != -1) {
secret = document.location.hash.split("=")[1];
window.top.postMessage({"secret":secret,"message":"link","value":"javascript://"+document.location.host+"/%0aalert(document.domain);//"},"*");
}
</script>
Line by line:
<script>starts a block of JavaScript that the browser will run when the page loads.if(document.location.hash.indexOf("secret") != -1) {checks whether the URL fragment (the part after#) contains the wordsecret. If it does not, nothing happens.secret = document.location.hash.split("=")[1];takes the fragment, splits it at the=sign, and keeps the second piece. That is the secret value WordPress put there.window.top.postMessage(...)sends a message to the outermost window, the page that contains the iframe.- The message has three fields:
secret(the value just read),messageset to"link"(so the handler takes the link branch), andvalueset to ajavascript:URL. - The
valueis built by joining"javascript://", the attacker's own host name (document.location.host),"/%0a", andalert(document.domain). The%0anewline separates the host-looking part from the code that will actually run, andalert(document.domain)is the proof: it pops up a box showing which site's address the code is running under. ,"*"is the target origin.*means "deliver to any origin". That is acceptable here because the attacker wants the message delivered to the parent window.
The script reads the secret from the URL fragment of the embed page, which is how WordPress passes it to the embedded document. It then sends a message to the top window with three fields: the secret, message set to link, and value set to a javascript: URL whose host part is the attacker's own host, followed by %0a and the payload alert(document.domain). The %0a is a newline, which separates the host-looking part from the JavaScript that will actually run. The target origin is *, which is acceptable here because the attacker wants the message delivered to the parent window.
Step 3: Publish a post on the attacker blog
Create any post on the attacker blog, publish it, and note its URL. This is the URL that will be embedded on the victim site.
Step 4: Embed the attacker post on the victim site in Safari
On the victim WordPress site, in Safari, add a new post that embeds the post from the attacker's WordPress. The embed iframe loads the attacker's page, the page reads the secret from its fragment, and sends the link message to the parent.
Step 5: The handler navigates the top window
The parent handler receives the message. The secret matches the iframe's data-secret, and e.source is the iframe's contentWindow, so the sender check passes. Because message is link, the handler builds the two anchor elements and compares i.host with a.host. In Safari, i.host for the javascript://... URL is the attacker's host, so the comparison succeeds. The handler then assigns t.value to c.top.location.href, and the JavaScript runs in the top window. The alert executes.
Root cause
The handler trusts a URL's host property as a proxy for "this is a safe, same-host link". That assumption holds for ordinary schemes, but Safari parses javascript: URLs in a way that yields a non-empty host, so the check can be satisfied by a javascript: URL. The javascript: scheme is then assigned to top.location.href, which executes it in the origin of the top window, the victim's blog.
The other defences in the handler do not help: the secret is disclosed to the embed page by design, and the e.source === r.contentWindow check only proves that the message came from the iframe, which the attacker controls. The d.activeElement === r condition is part of the same branch.
Impact
An attacker who can get a victim site to embed an attacker-controlled post, and whose victim views that page in Safari, can execute JavaScript in the origin of the victim's WordPress site. In the proof of concept this is demonstrated with alert(document.domain). The report does not go further than that demonstration, and the attack depends on the Safari-specific URL parsing behaviour.
Fix
The issue has been fixed. The report links to the WordPress 6.2.1 maintenance and security release and to the corresponding WPScan entry. The report does not describe the code change itself.
Variants to test
- Look for other
postMessagehandlers that validate a URL by reading.hostor.hostnamefrom an anchor element. A positive result is ajavascript:URL that yields a non-empty host in Safari and passes the comparison. - Test handlers that navigate
top.locationorwindow.locationfrom message data. A positive result is a navigation to ajavascript:URL that executes in the top window. - Check whether the
secretor nonce used to bind an embed to its iframe is exposed in the URL fragment or another readable location. A positive result is that an attacker-controlled embed page can read it and produce a message the handler accepts. - Test the same handler in different browsers. A positive result is a behaviour difference in URL parsing between browsers, as seen here between Safari and the others.
- Look for other places where a URL scheme is not checked explicitly before assignment to a navigation sink. A positive result is a
javascript:,data:or similar scheme reaching the sink.
Glossary
- API: a set of rules that lets one program use another program's features without knowing how it works inside.
- Web API: the APIs that a browser makes available to web pages, such as
postMessage(). - postMessage(): a browser function that sends a message from one window to another, even across different websites.
- Cross-origin: belonging to a different website (different scheme, host or port) than the page you are on.
- Same-origin policy: the browser rule that stops a page from one site from reading or changing a page from another site.
- Origin: the combination of scheme, host and port that identifies a website, for example
https://example.com. - Handler: the piece of code that runs when an event, such as an incoming message, happens.
- Payload: the part of an attack that does the actual work, here the JavaScript that runs.
- XSS (cross-site scripting): a bug where an attacker gets their own code to run inside someone else's website.
- iframe: a small window inside a web page that loads another page.
- contentWindow: the window object of the page loaded inside an iframe.
- top: the outermost window of a page, the one the user sees in the address bar.
- location.href: the address of a window; setting it navigates the window to a new URL.
- location.hash: the part of a URL after the
#sign, readable by the page itself. - URL scheme: the first part of a URL, such as
https:,javascript:ordata:, which tells the browser how to handle it. - javascript: URL: a URL whose scheme is
javascript:, meaning the browser runs the rest as code instead of loading a page. - host: the domain name part of a URL, for example
example.com. - Regular expression (regex): a pattern used to match or test text.
- data- attribute: a custom label a page can attach to an HTML element to store a value.
- DOM element: an object in the page's structure, such as a link or an iframe, that code can create and change.
- Anchor element (
<a>): an HTML link element; browsers also use it to parse URLs. - Browser console: a built-in tool in browsers where you can type JavaScript and see the result.
- Nonce: a one-time random value used to prove that something is fresh or authorised.
- Navigation sink: a place in code where a URL is used to navigate the page, such as
location.href. - WPScan: a public database of WordPress vulnerabilities.