-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Ella van Durpe <4710635+ellatrix@users.noreply.github.com>
- Loading branch information
Showing
18 changed files
with
381 additions
and
48 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
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
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
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,38 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import remove from './remove'; | ||
|
||
/** | ||
* Strips scripts and on* attributes from HTML. | ||
* | ||
* @param {string} html HTML to sanitize. | ||
* | ||
* @return {string} The sanitized HTML. | ||
*/ | ||
export default function safeHTML( html ) { | ||
const { body } = document.implementation.createHTMLDocument( '' ); | ||
body.innerHTML = html; | ||
const elements = body.getElementsByTagName( '*' ); | ||
let elementIndex = elements.length; | ||
|
||
while ( elementIndex-- ) { | ||
const element = elements[ elementIndex ]; | ||
|
||
if ( element.tagName === 'SCRIPT' ) { | ||
remove( element ); | ||
} else { | ||
let attributeIndex = element.attributes.length; | ||
|
||
while ( attributeIndex-- ) { | ||
const { name: key } = element.attributes[ attributeIndex ]; | ||
|
||
if ( key.startsWith( 'on' ) ) { | ||
element.removeAttribute( key ); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return body.innerHTML; | ||
} |
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,31 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import safeHTML from '../safe-html'; | ||
|
||
describe( 'safeHTML', () => { | ||
it( 'should strip on* attributes', () => { | ||
const input = '<img src="" onerror="alert(\'1\')" onload="">'; | ||
const output = '<img src="">'; | ||
expect( safeHTML( input ) ).toBe( output ); | ||
} ); | ||
|
||
it( 'should strip on* attributes with spacing', () => { | ||
const input = '<img src="" onerror = "alert(\'1\')" onload = "">'; | ||
const output = '<img src="">'; | ||
expect( safeHTML( input ) ).toBe( output ); | ||
} ); | ||
|
||
it( 'should strip nested on* attributes', () => { | ||
const input = | ||
'<p><strong><img src="" onerror="alert(\'1\')"></strong></p>'; | ||
const output = '<p><strong><img src=""></strong></p>'; | ||
expect( safeHTML( input ) ).toBe( output ); | ||
} ); | ||
|
||
it( 'should strip script tags', () => { | ||
const input = '<script>alert("1")</script><script></script>'; | ||
const output = ''; | ||
expect( safeHTML( input ) ).toBe( output ); | ||
} ); | ||
} ); |
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,44 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createNewPost, setPostContent } from '@wordpress/e2e-test-utils'; | ||
|
||
describe( 'missing block', () => { | ||
beforeEach( async () => { | ||
await createNewPost(); | ||
} ); | ||
|
||
it( 'should strip potentially malicious on* attributes', async () => { | ||
let hasAlert = false; | ||
|
||
page.on( 'dialog', () => { | ||
hasAlert = true; | ||
} ); | ||
|
||
await setPostContent( | ||
`<!-- wp:non-existing-block-here --><img src onerror=alert(1)>` | ||
); | ||
|
||
// Give the browser time to show the alert. | ||
await page.evaluate( () => new Promise( window.requestIdleCallback ) ); | ||
|
||
expect( hasAlert ).toBe( false ); | ||
} ); | ||
|
||
it( 'hould strip potentially malicious script tags', async () => { | ||
let hasAlert = false; | ||
|
||
page.on( 'dialog', () => { | ||
hasAlert = true; | ||
} ); | ||
|
||
await setPostContent( | ||
`<!-- wp:non-existing-block-here --><script>alert("EVIL");</script>` | ||
); | ||
|
||
// Give the browser time to show the alert. | ||
await page.evaluate( () => new Promise( window.requestIdleCallback ) ); | ||
|
||
expect( hasAlert ).toBe( false ); | ||
} ); | ||
} ); |
Oops, something went wrong.