-
-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(markdown): prevent script execution (#2040)
- Loading branch information
Showing
4 changed files
with
128 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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 isSafeAttribute = (attribute: string, value: string) => { | ||
if (attribute.startsWith('on')) { | ||
console.warn(`[@nuxt/content] removing unsafe attribute: ${attribute}="${value}"`) | ||
return false | ||
} | ||
|
||
if (attribute === 'href' || attribute === 'src') { | ||
return !unsafeLinkPrefix.some(prefix => value.toLowerCase().startsWith(prefix)) | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { expect, test, describe } from 'vitest' | ||
import { transformContent } from '../src/runtime/transformers' | ||
import { isSafeAttribute } from '../src/runtime/markdown-parser/utils/attrs' | ||
|
||
describe('XSS', () => { | ||
test('anchor', async () => { | ||
const { body } = await transformContent( | ||
'content:index.md', | ||
[ | ||
'[a](javascript://www.google.com%0Aprompt(1))', | ||
'[a](JaVaScRiPt:alert(1))', | ||
'[XSS](vbscript:alert(document.domain))', | ||
'[x](y \'<style>\')', | ||
'<javascript:prompt(document.cookie)>' | ||
].join('\n\n') | ||
) | ||
|
||
for (let child of body.children) { | ||
if (child.tag === 'p') { | ||
child = child.children[0] | ||
} | ||
expect(child.tag).toBe('a') | ||
expect(Object.entries(child.props as Record<string, any>).every(([k, v]) => isSafeAttribute(k, v))).toBeTruthy() | ||
} | ||
}) | ||
test('image', async () => { | ||
const { body } = await transformContent( | ||
'content:index.md', | ||
[ | ||
'![](x){onerror=alert(1) onload="alert(\'XSS\')" }', | ||
'![a]("onerror="alert(1))', | ||
'![](contenteditable/autofocus/onfocus=confirm(\'qwq\')//)">', | ||
'![XSS](data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K)', | ||
'<img src=x onerror=alert(1) onload="alert(\'XSS\')" />', | ||
'<img src=x onerror=alert(1)>">yep</a>', | ||
'![XSS]("onerror="alert(\'XSS\'))', | ||
'![XSS](https://www.example.com/image.png"onload="alert(\'XSS\'))', | ||
'![onload](https://www.example.com/image.png"onload="alert(\'ImageOnLoad\'))', | ||
'![onerror]("onerror="alert(\'ImageOnError\'))' | ||
].join('\n\n') | ||
) | ||
|
||
for (let child of body.children) { | ||
if (child.tag === 'p') { | ||
child = child.children[0] | ||
} | ||
expect(child.tag).toBe('img') | ||
expect(Object.entries(child.props as Record<string, any>).every(([k, v]) => isSafeAttribute(k, v))).toBeTruthy() | ||
} | ||
}) | ||
|
||
test('iframe', async () => { | ||
const { body } = await transformContent( | ||
'content:index.md', | ||
[ | ||
':iframe{src=x onerror=alert(1) onload="alert(\'XSS\')" }', | ||
'<iframe src=x onerror=alert(1) onload="alert(\'XSS\')" />' | ||
].join('\n\n') | ||
) | ||
|
||
for (let child of body.children) { | ||
if (child.tag === 'p') { | ||
child = child.children[0] | ||
} | ||
expect(child.tag).toBe('iframe') | ||
expect(Object.entries(child.props as Record<string, any>).every(([k, v]) => isSafeAttribute(k, v))).toBeTruthy() | ||
} | ||
}) | ||
}) |