Skip to content

Commit 3550374

Browse files
committed
feat($interpolate): escaped interpolation expressions
This CL enables interpolation expressions to be escaped, by prefixing each character of their start/end markers with a REVERSE SOLIDUS U+005C, and to render the escaped expression as a regular interpolation expression. Example: `<span ng-init="foo='Hello'">{{foo}}, \\{\\{World!\\}\\}</span>` would be rendered as: `<span ng-init="foo='Hello'">Hello, {{World!}}</span>` This will also work with custom interpolation markers, for example: module. config(function($interpolateProvider) { $interpolateProvider.startSymbol('\\\\'); $interpolateProvider.endSymbol('//'); }). run(function($interpolate) { // Will alert with "hello\\bar//": alert($interpolate('\\\\foo//\\\\\\\\bar\\/\\/')({foo: "hello", bar: "world"})); }); This change effectively only changes the rendering of these escaped markers, because they are not context-aware, and are incapable of preventing nested expressions within those escaped markers from being evaluated. Therefore, backends are encouraged to ensure that when escaping expressions for security reasons, every single instance of a start or end marker have each of its characters prefixed with a backslash (REVERSE SOLIDUS, U+005C) Closes angular#5601
1 parent 95cdb53 commit 3550374

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

Diff for: src/ng/interpolate.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ function $InterpolateProvider() {
8181

8282
this.$get = ['$parse', '$exceptionHandler', '$sce', function($parse, $exceptionHandler, $sce) {
8383
var startSymbolLength = startSymbol.length,
84-
endSymbolLength = endSymbol.length;
84+
endSymbolLength = endSymbol.length,
85+
escapedStartRegexp = new RegExp(startSymbol.replace(/./g, escape), 'g'),
86+
escapedEndRegexp = new RegExp(endSymbol.replace(/./g, escape), 'g');
87+
88+
function escape(ch) {
89+
return '\\\\\\' + ch;
90+
}
8591

8692
/**
8793
* @ngdoc service
@@ -126,6 +132,41 @@ function $InterpolateProvider() {
126132
*
127133
* `allOrNothing` is useful for interpolating URLs. `ngSrc` and `ngSrcset` use this behavior.
128134
*
135+
* ####Escaped Interpolation
136+
* $interpolate provides a mechanism for escaping interpolation markers. Start and end markers
137+
* can be escaped by preceding each of their characters with a REVERSE SOLIDUS U+005C (backslash).
138+
* It will be rendered as a regular start/end marker, and will not be interpreted as an expression
139+
* or binding.
140+
*
141+
* This enables web-servers to prevent script injection attacks and defacing attacks, to some
142+
* degree, while also enabling code examples to work without relying on the
143+
* {@link ng.directive:ngNonBindable ngNonBindable} directive.
144+
*
145+
* **For security purposes, it is strongly encouraged that web servers escape user-supplied data of
146+
* all angle brackets (&lt;, &gt;) with &amp;lt; and &amp;gt; respectively, and to replace all
147+
* interpolation start/end markers with their escaped counterparts, in order to prevent unwanted
148+
* execution of code.**
149+
*
150+
* Escaped interpolation markers are only replaced with the actual interpolation markers in rendered
151+
* output when the $interpolate service processes the text. And so, for HTML elements interpolated
152+
* by {@link ng.$compile $compile}, there must be a proper interpolated expression in order
153+
* to see these results.
154+
*
155+
* <example>
156+
* <file name="index.html">
157+
* <div ng-init="username='A user'">
158+
* <p ng-init="apptitle='Escaping demo'">{{apptitle}}: \{\{ username = "some jerk"; \}\}
159+
* </p>
160+
* <p><strong>{{username}}</strong> attempts to inject code which will deface the
161+
* application, but fails to accomplish their task, because the server has correctly
162+
* escaped the interpolation start/end markers with REVERSE SOLIDUS U+005C (backslash)
163+
* characters.</p>
164+
* <p>Instead, the result of the attempted script injection is visible, and can be removed
165+
* from the database by an administrator.</p>
166+
* </div>
167+
* </file>
168+
* </example>
169+
*
129170
* @param {string} text The text with markup to interpolate.
130171
* @param {boolean=} mustHaveExpression if set to true then the interpolation string must have
131172
* embedded expression in order to return an interpolation function. Strings with no
@@ -176,6 +217,12 @@ function $InterpolateProvider() {
176217
}
177218
}
178219

220+
forEach(separators, function(key, i) {
221+
separators[i] = separators[i].
222+
replace(escapedStartRegexp, startSymbol).
223+
replace(escapedEndRegexp, endSymbol);
224+
});
225+
179226
if (separators.length === expressions.length) {
180227
separators.push('');
181228
}

Diff for: test/ng/interpolateSpec.js

+60
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,66 @@ describe('$interpolate', function() {
6161
}));
6262

6363

64+
describe('interpolation escaping', function() {
65+
var obj;
66+
beforeEach(function() {
67+
obj = {foo: 'Hello', bar: 'World'};
68+
});
69+
70+
71+
it('should support escaping interpolation signs', inject(function($interpolate) {
72+
expect($interpolate('{{foo}} \\{\\{bar\\}\\}')(obj)).toBe('Hello {{bar}}');
73+
expect($interpolate('\\{\\{foo\\}\\} {{bar}}')(obj)).toBe('{{foo}} World');
74+
}));
75+
76+
77+
it('should unescape multiple expressions', inject(function($interpolate) {
78+
expect($interpolate('\\{\\{foo\\}\\}\\{\\{bar\\}\\} {{foo}}')(obj)).toBe('{{foo}}{{bar}} Hello');
79+
expect($interpolate('{{foo}}\\{\\{foo\\}\\}\\{\\{bar\\}\\}')(obj)).toBe('Hello{{foo}}{{bar}}');
80+
expect($interpolate('\\{\\{foo\\}\\}{{foo}}\\{\\{bar\\}\\}')(obj)).toBe('{{foo}}Hello{{bar}}');
81+
expect($interpolate('{{foo}}\\{\\{foo\\}\\}{{bar}}\\{\\{bar\\}\\}{{foo}}')(obj)).toBe('Hello{{foo}}World{{bar}}Hello');
82+
}));
83+
84+
85+
it('should support escaping custom interpolation start/end symbols', function() {
86+
module(function($interpolateProvider) {
87+
$interpolateProvider.startSymbol('[[');
88+
$interpolateProvider.endSymbol(']]');
89+
});
90+
inject(function($interpolate) {
91+
expect($interpolate('[[foo]] \\[\\[bar\\]\\]')(obj)).toBe('Hello [[bar]]');
92+
});
93+
});
94+
95+
96+
it('should unescape incomplete escaped expressions', inject(function($interpolate) {
97+
expect($interpolate('\\{\\{foo{{foo}}')(obj)).toBe('{{fooHello');
98+
expect($interpolate('\\}\\}foo{{foo}}')(obj)).toBe('}}fooHello');
99+
expect($interpolate('foo{{foo}}\\{\\{')(obj)).toBe('fooHello{{');
100+
expect($interpolate('foo{{foo}}\\}\\}')(obj)).toBe('fooHello}}');
101+
}));
102+
103+
104+
it('should not unescape markers within expressions', inject(function($interpolate) {
105+
expect($interpolate('{{"\\\\{\\\\{Hello, world!\\\\}\\\\}"}}')(obj)).toBe('\\{\\{Hello, world!\\}\\}');
106+
expect($interpolate('{{"\\{\\{Hello, world!\\}\\}"}}')(obj)).toBe('{{Hello, world!}}');
107+
expect(function() {
108+
$interpolate('{{\\{\\{foo\\}\\}}}')(obj);
109+
}).toThrowMinErr('$parse', 'lexerr',
110+
'Lexer Error: Unexpected next character at columns 0-0 [\\] in expression [\\{\\{foo\\}\\]');
111+
}));
112+
113+
114+
// This test demonstrates that the web-server is responsible for escaping every single instance
115+
// of interpolation start/end markers in an expression which they do not wish to evaluate,
116+
// because AngularJS will not protect them from being evaluated (due to the added complexity
117+
// and maintenance burden of context-sensitive escaping)
118+
it('should evaluate expressions between escaped start/end symbols', inject(function($interpolate) {
119+
expect($interpolate('\\{\\{Hello, {{bar}}!\\}\\}')(obj)).toBe('{{Hello, World!}}');
120+
}));
121+
});
122+
123+
64124
describe('interpolating in a trusted context', function() {
65125
var sce;
66126
beforeEach(function() {

0 commit comments

Comments
 (0)