Skip to content

Commit d190a84

Browse files
committed
feat(snippet): Added a simple security snippet
1 parent d2f5eed commit d190a84

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ Note: code snippets do NOT have access to the full console API, for example no a
3131

3232
## Snippets
3333

34+
### Security
35+
36+
* [test-script-injection.js](test-script-injection.js) - tries to create a new
37+
inline script tag to test if page allows it.
38+
3439
### DOM and CPU generic performance
3540

3641
* [boilerplate.js](boilerplate.js) - boilerplate for loading and running a remote code script

test-script-injection.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
This code snippet checks if the page allows creating
3+
and executing new inline scripts (script-injection attacks)
4+
See https://github.com/bahmutov/disable-inline-javascript-tutorial
5+
*/
6+
(function testScriptInjection() {
7+
var el = document.createElement('script');
8+
el.innerText = 'alert("hi there")';
9+
document.body.appendChild(el); // runs the code by default
10+
}());

0 commit comments

Comments
 (0)