Skip to content

Commit

Permalink
Handle invalid escaped quotes in tags more gracefully
Browse files Browse the repository at this point in the history
Don't break the entire parsing of the template afterwards by treating escaped quotes which aren't already in quotes as not being quotes
  • Loading branch information
jasmith-hs committed Jul 25, 2023
1 parent 69379e5 commit d7831b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/main/java/com/hubspot/jinjava/tree/parse/TokenScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,15 @@ private Token getNextToken() {
}

if (inBlock > 0) {
if (inQuote != 0) {
if (c == '\\') {
++currPost;
continue;
} else if (inQuote != 0) {
if (inQuote == c) {
inQuote = 0;
continue;
} else if (c == '\\') {
++currPost;
continue;
} else {
continue;
}
} else if (inQuote == 0 && (c == '\'' || c == '"')) {
continue;
} else if (c == '\'' || c == '"') {
inQuote = c;
continue;
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/hubspot/jinjava/tree/parse/TokenScannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,23 @@ public void testLstripBlocks() {
assertThat(tokens).isNotEmpty();
}

@Test
public void itTreatsEscapedQuotesSameWhenNotInQuotes() {
List<Token> tokens = tokens("tag-with-all-escaped-quotes");
assertThat(tokens).hasSize(8);
assertThat(tokens.stream().map(Token::getType).collect(Collectors.toList()))
.containsExactly(
symbols.getNote(),
symbols.getFixed(),
symbols.getTag(),
symbols.getFixed(),
symbols.getTag(),
symbols.getFixed(),
symbols.getTag(),
symbols.getFixed()
);
}

private List<Token> tokens(String fixture) {
TokenScanner t = fixture(fixture);
return Lists.newArrayList(t);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{# This print tag is invalid, but it should not cause the rest of the template to break #}
{% print \"foo\" %}
Start
{% if true %}
The dog says: "Woof!"
{% endif %}
End

0 comments on commit d7831b1

Please sign in to comment.