-
Notifications
You must be signed in to change notification settings - Fork 27.5k
feat($interpolate): escaped interpolation expressions #7517
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,66 @@ describe('$interpolate', function() { | |
})); | ||
|
||
|
||
describe('interpolation escaping', function() { | ||
var obj; | ||
beforeEach(function() { | ||
obj = {foo: 'Hello', bar: 'World'}; | ||
}); | ||
|
||
|
||
it('should support escaping interpolation signs', inject(function($interpolate) { | ||
expect($interpolate('{{foo}} \\{\\{bar\\}\\}')(obj)).toBe('Hello {{bar}}'); | ||
expect($interpolate('\\{\\{foo\\}\\} {{bar}}')(obj)).toBe('{{foo}} World'); | ||
})); | ||
|
||
|
||
it('should unescape multiple expressions', inject(function($interpolate) { | ||
expect($interpolate('\\{\\{foo\\}\\}\\{\\{bar\\}\\} {{foo}}')(obj)).toBe('{{foo}}{{bar}} Hello'); | ||
expect($interpolate('{{foo}}\\{\\{foo\\}\\}\\{\\{bar\\}\\}')(obj)).toBe('Hello{{foo}}{{bar}}'); | ||
expect($interpolate('\\{\\{foo\\}\\}{{foo}}\\{\\{bar\\}\\}')(obj)).toBe('{{foo}}Hello{{bar}}'); | ||
expect($interpolate('{{foo}}\\{\\{foo\\}\\}{{bar}}\\{\\{bar\\}\\}{{foo}}')(obj)).toBe('Hello{{foo}}World{{bar}}Hello'); | ||
})); | ||
|
||
|
||
it('should support escaping custom interpolation start/end symbols', function() { | ||
module(function($interpolateProvider) { | ||
$interpolateProvider.startSymbol('[['); | ||
$interpolateProvider.endSymbol(']]'); | ||
}); | ||
inject(function($interpolate) { | ||
expect($interpolate('[[foo]] \\[\\[bar\\]\\]')(obj)).toBe('Hello [[bar]]'); | ||
}); | ||
}); | ||
|
||
|
||
it('should unescape incomplete escaped expressions', inject(function($interpolate) { | ||
expect($interpolate('\\{\\{foo{{foo}}')(obj)).toBe('{{fooHello'); | ||
expect($interpolate('\\}\\}foo{{foo}}')(obj)).toBe('}}fooHello'); | ||
expect($interpolate('foo{{foo}}\\{\\{')(obj)).toBe('fooHello{{'); | ||
expect($interpolate('foo{{foo}}\\}\\}')(obj)).toBe('fooHello}}'); | ||
})); | ||
|
||
|
||
it('should not unescape markers within expressions', inject(function($interpolate) { | ||
expect($interpolate('{{"\\\\{\\\\{Hello, world!\\\\}\\\\}"}}')(obj)).toBe('\\{\\{Hello, world!\\}\\}'); | ||
expect($interpolate('{{"\\{\\{Hello, world!\\}\\}"}}')(obj)).toBe('{{Hello, world!}}'); | ||
expect(function() { | ||
$interpolate('{{\\{\\{foo\\}\\}}}')(obj); | ||
}).toThrowMinErr('$parse', 'lexerr', | ||
'Lexer Error: Unexpected next character at columns 0-0 [\\] in expression [\\{\\{foo\\}\\]'); | ||
})); | ||
|
||
|
||
// This test demonstrates that the web-server is responsible for escaping every single instance | ||
// of interpolation start/end markers in an expression which they do not wish to evaluate, | ||
// because AngularJS will not protect them from being evaluated (due to the added complexity | ||
// and maintenance burden of context-sensitive escaping) | ||
it('should evaluate expressions between escaped start/end symbols', inject(function($interpolate) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what I really don't like about this version. (There are other things too, but this is the most problematic). If people are ok with this (and certainly many will be), then I'm all for it. But I see this as bad. In my view, escaped expression markers should be fully protected from having their contents evaluated in any fashion There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a comment for this test that states that the server is responsible for ensuring that all bindings are properly escaped, if that doesn't happen, we won't go out of our way to ignore bindings in improperly escaped string. |
||
expect($interpolate('\\{\\{Hello, {{bar}}!\\}\\}')(obj)).toBe('{{Hello, World!}}'); | ||
})); | ||
}); | ||
|
||
|
||
describe('interpolating in a trusted context', function() { | ||
var sce; | ||
beforeEach(function() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You end up with really ugly looking escape markers this way, which is fine for computers, but it's not very pleasant to look at. The search/replace style escaping can't have custom escape markers for aesthetic niceness, because they would be potentially exploitable.