@@ -897,9 +897,7 @@ <h1>template.js</h1>
897897
898898< span class ="hljs-function "> < span class ="hljs-keyword "> function</ span > < span class ="hljs-title "> escapeChar</ span > (< span class ="hljs-params "> match</ span > ) </ span > {
899899 < span class ="hljs-keyword "> return</ span > < span class ="hljs-string "> '\\'</ span > + escapes[match];
900- }
901-
902- < span class ="hljs-keyword "> var</ span > bareIdentifier = < span class ="hljs-regexp "> /^\s*(\w|\$)+\s*$/</ span > ;</ pre > </ div > </ div >
900+ }</ pre > </ div > </ div >
903901
904902 </ li >
905903
@@ -910,6 +908,25 @@ <h1>template.js</h1>
910908 < div class ="pilwrap ">
911909 < a class ="pilcrow " href ="#section-4 "> ¶</ a >
912910 </ div >
911+ < p > In order to prevent third-party code injection through
912+ < code > _.templateSettings.variable</ code > , we test it against the following regular
913+ expression. It is intentionally a bit more liberal than just matching valid
914+ identifiers, but still prevents possible loopholes through defaults or
915+ destructuring assignment.</ p >
916+
917+ </ div >
918+
919+ < div class ="content "> < div class ='highlight '> < pre > < span class ="hljs-keyword "> var</ span > bareIdentifier = < span class ="hljs-regexp "> /^\s*(\w|\$)+\s*$/</ span > ;</ pre > </ div > </ div >
920+
921+ </ li >
922+
923+
924+ < li id ="section-5 ">
925+ < div class ="annotation ">
926+
927+ < div class ="pilwrap ">
928+ < a class ="pilcrow " href ="#section-5 "> ¶</ a >
929+ </ div >
913930 < p > JavaScript micro-templating, similar to John Resig’s implementation.
914931Underscore templating handles arbitrary delimiters, preserves whitespace,
915932and correctly escapes quotes within interpolated code.
@@ -924,11 +941,11 @@ <h1>template.js</h1>
924941 </ li >
925942
926943
927- < li id ="section-5 ">
944+ < li id ="section-6 ">
928945 < div class ="annotation ">
929946
930947 < div class ="pilwrap ">
931- < a class ="pilcrow " href ="#section-5 "> ¶</ a >
948+ < a class ="pilcrow " href ="#section-6 "> ¶</ a >
932949 </ div >
933950 < p > Combine delimiters into one regular expression via alternation.</ p >
934951
@@ -943,11 +960,11 @@ <h1>template.js</h1>
943960 </ li >
944961
945962
946- < li id ="section-6 ">
963+ < li id ="section-7 ">
947964 < div class ="annotation ">
948965
949966 < div class ="pilwrap ">
950- < a class ="pilcrow " href ="#section-6 "> ¶</ a >
967+ < a class ="pilcrow " href ="#section-7 "> ¶</ a >
951968 </ div >
952969 < p > Compile the template source, escaping string literals appropriately.</ p >
953970
@@ -970,11 +987,11 @@ <h1>template.js</h1>
970987 </ li >
971988
972989
973- < li id ="section-7 ">
990+ < li id ="section-8 ">
974991 < div class ="annotation ">
975992
976993 < div class ="pilwrap ">
977- < a class ="pilcrow " href ="#section-7 "> ¶</ a >
994+ < a class ="pilcrow " href ="#section-8 "> ¶</ a >
978995 </ div >
979996 < p > Adobe VMs need the match returned to produce the correct offset.</ p >
980997
@@ -985,18 +1002,34 @@ <h1>template.js</h1>
9851002 source += < span class ="hljs-string "> "';\n"</ span > ;
9861003
9871004 < span class ="hljs-keyword "> var</ span > argument = settings.variable;
988- < span class ="hljs-keyword "> if</ span > (argument) {
989- < span class ="hljs-keyword "> if</ span > (!bareIdentifier.test(argument)) < span class ="hljs-keyword "> throw</ span > < span class ="hljs-keyword "> new</ span > < span class ="hljs-built_in "> Error</ span > (argument);
1005+ < span class ="hljs-keyword "> if</ span > (argument) {</ pre > </ div > </ div >
1006+
1007+ </ li >
1008+
1009+
1010+ < li id ="section-9 ">
1011+ < div class ="annotation ">
1012+
1013+ < div class ="pilwrap ">
1014+ < a class ="pilcrow " href ="#section-9 "> ¶</ a >
1015+ </ div >
1016+ < p > Insure against third-party code injection.</ p >
1017+
1018+ </ div >
1019+
1020+ < div class ="content "> < div class ='highlight '> < pre > < span class ="hljs-keyword "> if</ span > (!bareIdentifier.test(argument)) < span class ="hljs-keyword "> throw</ span > < span class ="hljs-keyword "> new</ span > < span class ="hljs-built_in "> Error</ span > (
1021+ < span class ="hljs-string "> 'variable is not a bare identifier: '</ span > + argument
1022+ );
9901023 } < span class ="hljs-keyword "> else</ span > {</ pre > </ div > </ div >
9911024
9921025 </ li >
9931026
9941027
995- < li id ="section-8 ">
1028+ < li id ="section-10 ">
9961029 < div class ="annotation ">
9971030
9981031 < div class ="pilwrap ">
999- < a class ="pilcrow " href ="#section-8 "> ¶</ a >
1032+ < a class ="pilcrow " href ="#section-10 "> ¶</ a >
10001033 </ div >
10011034 < p > If a variable is not specified, place data values in local scope.</ p >
10021035
@@ -1025,11 +1058,11 @@ <h1>template.js</h1>
10251058 </ li >
10261059
10271060
1028- < li id ="section-9 ">
1061+ < li id ="section-11 ">
10291062 < div class ="annotation ">
10301063
10311064 < div class ="pilwrap ">
1032- < a class ="pilcrow " href ="#section-9 "> ¶</ a >
1065+ < a class ="pilcrow " href ="#section-11 "> ¶</ a >
10331066 </ div >
10341067 < p > Provide the compiled source as a convenience for precompilation.</ p >
10351068
0 commit comments