Skip to content

Commit 6478ef4

Browse files
committed
Fixed a security issue allowing to execute aritrary JavaScript code via a specially prepared function name of a typed function
1 parent 9a8cac3 commit 6478ef4

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# History
22

33

4+
## not yet released, version 0.10.6
5+
6+
- Fixed a security issue allowing to execute aritrary JavaScript code via a
7+
specially prepared function name of a typed function. Thanks Masato Kinugawa.
8+
9+
410
## 2016-11-18, version 0.10.5
511

612
- Fixed the use of multi-layered use of `any` type. See #8.

test/security.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var assert = require('assert');
2+
var typed = require('../typed-function');
3+
4+
describe('security', function () {
5+
6+
it ('should not allow bad code in the function name', function () {
7+
// simple example:
8+
// var fn = typed("(){}+console.log('hacked...');function a", {
9+
// "": function () {}
10+
// });
11+
12+
// example resulting in throwing an error
13+
var fn = typed("(){}+(function(){throw new Error('Hacked... should not have executed this function!!!')})();function a", {
14+
"": function () {}
15+
});
16+
})
17+
})

typed-function.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,12 +1096,15 @@
10961096
//console.log(util.inspect(node, { depth: null }));
10971097

10981098
// generate code for the typed function
1099+
// safeName is a conservative replacement of characters
1100+
// to prevend being able to inject JS code at the place of the function name
1101+
// the name is useful for stack trackes therefore we want have it there
10991102
var code = [];
1100-
var _name = name || '';
1101-
var _args = getArgs(maxParams(_signatures));
1102-
code.push('function ' + _name + '(' + _args.join(', ') + ') {');
1103+
var safeName = (name || '').replace(/[^a-zA-Z0-9_$]/g, '_')
1104+
var args = getArgs(maxParams(_signatures));
1105+
code.push('function ' + safeName + '(' + args.join(', ') + ') {');
11031106
code.push(' "use strict";');
1104-
code.push(' var name = \'' + _name + '\';');
1107+
code.push(' var name = ' + JSON.stringify(name || '') + ';');
11051108
code.push(node.toCode(refs, ' ', false));
11061109
code.push('}');
11071110

0 commit comments

Comments
 (0)