fix(for tag): Potential ‘Index out of range’ crash

Fixes #136
This commit is contained in:
Jan Berkel
2017-09-01 14:21:41 +02:00
committed by Kyle Fuller
parent 0444f45d2b
commit f1fc747897
3 changed files with 18 additions and 1 deletions

View File

@@ -1,5 +1,13 @@
# Stencil Changelog # Stencil Changelog
## Master
### Bug Fixes
- Fixes a potential crash when using the `{% for %}` template tag with the
incorrect amount of arguments.
## 0.9.0 ## 0.9.0
### Enhancements ### Enhancements

View File

@@ -10,7 +10,7 @@ class ForNode : NodeType {
class func parse(_ parser:TokenParser, token:Token) throws -> NodeType { class func parse(_ parser:TokenParser, token:Token) throws -> NodeType {
let components = token.components() let components = token.components()
guard components.count >= 2 && components[2] == "in" && guard components.count >= 3 && components[2] == "in" &&
(components.count == 4 || (components.count >= 6 && components[4] == "where")) else { (components.count == 4 || (components.count >= 6 && components[4] == "where")) else {
throw TemplateSyntaxError("'for' statements should use the following 'for x in y where condition' `\(token.contents)`.") throw TemplateSyntaxError("'for' statements should use the following 'for x in y where condition' `\(token.contents)`.")
} }

View File

@@ -134,6 +134,15 @@ func testForNode() {
let node = ForNode(resolvable: Variable("dict"), loopVariables: ["key", "value"], nodes: nodes, emptyNodes: emptyNodes, where: nil) let node = ForNode(resolvable: Variable("dict"), loopVariables: ["key", "value"], nodes: nodes, emptyNodes: emptyNodes, where: nil)
try expect(try node.render(context)) == "oneItwoII" try expect(try node.render(context)) == "oneItwoII"
} }
$0.it("handles invalid input") {
let tokens: [Token] = [
.block(value: "for i"),
]
let parser = TokenParser(tokens: tokens, environment: Environment())
let error = TemplateSyntaxError("'for' statements should use the following 'for x in y where condition' `for i`.")
try expect(try parser.parse()).toThrow(error)
}
} }
} }