pre-auth Stored XSS in comments via javascript: url when administrator edits user supplied comment
A stored cross-site scripting vulnerability exists in WordPress comment handling because the function that adds rel=nofollow to links can be tricked into decoding an escaped colon, allowing a javascript: URL to survive filtering. An attacker can post a comment containing such a link, and when an administrator edits and saves that comment, the link becomes active and executes script when clicked.
Originally disclosed on HackerOne
- Triaged in 8 days
- Fixed in 2 months
- Paid in 2 months
- Disclosed after 14 months
When a comment is submitted, it is filtered via wp_rel_nofollow_callback(), which adds the rel attribute to <a> tags within the anchor:
function wp_rel_nofollow_callback( $matches ) {
$text = $matches[1];
$atts = shortcode_parse_atts( $matches[1] );
$rel = 'nofollow';
if ( ! empty( $atts['href'] ) ) {
if ( in_array( strtolower( wp_parse_url( $atts['href'], PHP_URL_SCHEME ) ), array( 'http', 'https' ), true ) ) {
if ( strtolower( wp_parse_url( $atts['href'], PHP_URL_HOST ) ) === strtolower( wp_parse_url( home_url(), PHP_URL_HOST ) ) ) {
return "<a $text>";
}
}
}
if ( ! empty( $atts['rel'] ) ) {
$parts = array_map( 'trim', explode( ' ', $atts['rel'] ) );
if ( false === array_search( 'nofollow', $parts ) ) {
$parts[] = 'nofollow';
}
$rel = implode( ' ', $parts );
unset( $atts['rel'] );
$html = '';
foreach ( $atts as $name => $value ) {
$html .= "{$name}=\"" . $value . '" ';
}
$text = trim( $html );
}
return "<a $text rel=\"" . esc_attr( $rel ) . '">';
}
if the rel attribute is already set, the <a> tag is built back together with the values returned by shortcode_parse_atts(). This is problematic, since shortcode_parse_atts() calls stripcslashes() on the attribute values, which for example allows turning \x3a into :.
Therefor the esc_url() function can be bypassed by:
- using a URL such as
javascript\x3aalert(1); - getting an admin to edit and update the comment containing the XSS payload
- done
I recommend moving away from shortcode_parse_atts() because of side effects like these. I also got close to a XSS without user interaction through the same mechanisms but it fails luckily.
PoC:
- As an unauthenticated user, create a comment with the following content:
Hi!
I really enjoy your work. We've also written a blog post about it here: http://dummysite.com/awesome-blogpost. Feel free to check it out!
<a href="javascript\x3aalert(1);">Visit my web page</a>
- create a second comment with the content:
I just noticed a typo in the URL! Could you please change it from dummysite.com to dummysite2.com? Thank you so much
- Log in as an admin, go to the comments section and edit the comment and click save
- View the comment on the post, click the "Visit my web page" URL and see the alert() box popping up.
Impact
Through the XSS, RCE can be gained. Obviously a lot of user interaction is required but yeah, it is a super easy to copy & paste payload that could be used against non technical users. The XSS could then also be triggered via clickjacking.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the use of shortcode_parse_atts() on attribute values, which calls stripcslashes() and thus decodes escape sequences like \x3a into a colon, bypassing the esc_url() check that would otherwise block javascript: URLs.
Impact
Through the XSS, RCE can be gained, though a lot of user interaction is required; the XSS could also be triggered via clickjacking.
What the attacker needed
Ability to post comments; admin edits and saves the comment; admin clicks the malicious link
The fix
The report recommends moving away from shortcode_parse_atts() because of side effects like these; the text does not state that a fix was applied.
How it was found
- As an unauthenticated user, submit a comment containing a link with an escaped colon in the href attribute.
- Submit a second comment asking to correct a typo in the URL, to prompt an admin to edit the first comment.
- Log in as an administrator, go to the comments section, edit the first comment, and save it.
- View the comment on the post and click the link to trigger the alert box.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When you leave a comment with a link, WordPress tries to make it safe by adding a 'nofollow' attribute. But a bug in how it rebuilds the link means an attacker can hide a dangerous 'javascript:' link that runs code when clicked. The attacker posts a comment with this hidden link, then tricks an administrator into editing and saving the comment, which activates the link. If the admin then clicks it, malicious script runs in their browser.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.