-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent creating script tags from embedding stylesheets (#156)
* Prevent security concern from preload media option * Update yarn.lock * Validates CSS path before loading the file * Prevent creating script tags from embedding stylesheets * Remove launch.json * Add test case * Fix review comments
- Loading branch information
1 parent
2e8cbe8
commit 6a75baf
Showing
5 changed files
with
509 additions
and
260 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,69 @@ | ||
import Critters from '../src/index'; | ||
import * as cheerio from 'cheerio'; | ||
|
||
function hasEvilOnload(html) { | ||
const $ = cheerio.load(html, { scriptingEnabled: true }); | ||
return $('[onload]').attr('onload').includes(`''-alert(1)-''`); | ||
} | ||
|
||
function hasEvilScript(html) { | ||
const $ = cheerio.load(html, { scriptingEnabled: true }); | ||
const scripts = Array.from($('script')); | ||
return scripts.some((s) => s.textContent.trim() === 'alert(1)'); | ||
} | ||
|
||
describe('Critters', () => { | ||
it('should not decode entities', async () => { | ||
const critters = new Critters({}); | ||
const html = await critters.process(` | ||
<html> | ||
<body> | ||
<script>alert(1)</script> | ||
`); | ||
expect(hasEvilScript(html)).toBeFalsy(); | ||
}); | ||
it('should not create a new script tag from embedding linked stylesheets', async () => { | ||
const critters = new Critters({}); | ||
critters.readFile = () => | ||
`* { background: url('</style><script>alert(1)</script>') }`; | ||
const html = await critters.process(` | ||
<html> | ||
<head> | ||
<link rel=stylesheet href=/file.css> | ||
</head> | ||
<body> | ||
</body> | ||
`); | ||
expect(hasEvilScript(html)).toBeFalsy(); | ||
}); | ||
it('should not create a new script tag from embedding additional stylesheets', async () => { | ||
const critters = new Critters({ | ||
additionalStylesheets: ['/style.css'] | ||
}); | ||
critters.readFile = () => | ||
`* { background: url('</style><script>alert(1)</script>') }`; | ||
const html = await critters.process(` | ||
<html> | ||
<head> | ||
</head> | ||
<body> | ||
</body> | ||
`); | ||
expect(hasEvilScript(html)).toBeFalsy(); | ||
}); | ||
|
||
it('should not create a new script tag by ending </script> from href', async () => { | ||
const critters = new Critters({ preload: 'js' }); | ||
critters.readFile = () => `* { background: red }`; | ||
const html = await critters.process(` | ||
<html> | ||
<head> | ||
<link rel=stylesheet href="/abc/</script><script>alert(1)</script>/style.css"> | ||
</head> | ||
<body> | ||
</body> | ||
`); | ||
expect(hasEvilScript(html)).toBeFalsy(); | ||
}); | ||
}); |
Oops, something went wrong.