Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit e3f78c1

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 #5601 Closes #7517
1 parent e994259 commit e3f78c1

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

src/ng/interpolate.js

+49-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,42 @@ 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,
146+
* replacing angle brackets (&lt;, &gt;) with &amp;lt; and &amp;gt; respectively, and replacing all
147+
* interpolation start/end markers with their escaped counterparts.**
148+
*
149+
* Escaped interpolation markers are only replaced with the actual interpolation markers in rendered
150+
* output when the $interpolate service processes the text. So, for HTML elements interpolated
151+
* by {@link ng.$compile $compile}, or otherwise interpolated with the `mustHaveExpression` parameter
152+
* set to `true`, the interpolated text must contain an unescaped interpolation expression. As such,
153+
* this is typically useful only when user-data is used in rendering a template from the server, or
154+
* when otherwise untrusted data is used by a directive.
155+
*
156+
* <example>
157+
* <file name="index.html">
158+
* <div ng-init="username='A user'">
159+
* <p ng-init="apptitle='Escaping demo'">{{apptitle}}: \{\{ username = "some jerk"; \}\}
160+
* </p>
161+
* <p><strong>{{username}}</strong> attempts to inject code which will deface the
162+
* application, but fails to accomplish their task, because the server has correctly
163+
* escaped the interpolation start/end markers with REVERSE SOLIDUS U+005C (backslash)
164+
* characters.</p>
165+
* <p>Instead, the result of the attempted script injection is visible, and can be removed
166+
* from the database by an administrator.</p>
167+
* </div>
168+
* </file>
169+
* </example>
170+
*
129171
* @param {string} text The text with markup to interpolate.
130172
* @param {boolean=} mustHaveExpression if set to true then the interpolation string must have
131173
* embedded expression in order to return an interpolation function. Strings with no
@@ -176,6 +218,12 @@ function $InterpolateProvider() {
176218
}
177219
}
178220

221+
forEach(separators, function(key, i) {
222+
separators[i] = separators[i].
223+
replace(escapedStartRegexp, startSymbol).
224+
replace(escapedEndRegexp, endSymbol);
225+
});
226+
179227
if (separators.length === expressions.length) {
180228
separators.push('');
181229
}

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)