File tree Expand file tree Collapse file tree 3 files changed +30
-4
lines changed Expand file tree Collapse file tree 3 files changed +30
-4
lines changed Original file line number Diff line number Diff line change 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 .
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 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 - z A - Z 0 - 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
You can’t perform that action at this time.
0 commit comments