Skip to content

Autoescaping improvements #577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/twig.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,8 @@ module.exports = function (Twig) {
for (i = 0; i < len; i++) {
str = output[i];

if (str && (str.twig_markup !== true && str.twig_markup != strategy)) {
if (str && (str.twig_markup !== true && str.twig_markup !== strategy)
&& !(strategy === 'html' && str.twig_markup === 'html_attr')) {
str = Twig.filters.escape(str, [ strategy ]);
}

Expand Down Expand Up @@ -1294,13 +1295,13 @@ module.exports = function (Twig) {
}

if (!params) {
return output;
return output.valueOf();
} else if (params.output == 'blocks') {
return that.blocks;
} else if (params.output == 'macros') {
return that.macros;
} else {
return output;
return output.valueOf();
}
});
});
Expand Down
21 changes: 21 additions & 0 deletions test/test.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,27 @@ describe("Twig.js Core ->", function() {
}).should.equal('\\x3Ctest\\x3E\\x26\\x3C\\x2Ftest\\x3E');
});

it("should not auto escape html_attr within the html strategy", function() {
twig({
autoescape: 'html',
data: "{{ value|escape('html_attr') }}"
}).render({
value: '" onclick="alert(\\"html_attr\\")"'
}).should.equal('&quot;&#x20;onclick&#x3D;&quot;alert&#x28;&#x5C;&quot;html_attr&#x5C;&quot;&#x29;&quot;');
});

it("should return a usable string after autoescaping", function() {
var result = twig({
autoescape: true,
data: '{{ value }}'
}).render({
value: '<test>&</test>'
});

(typeof result).should.equal('string');
result.valueOf().should.equal(result);
});

it("should autoescape parent() output correctly", function() {
twig({id: 'parent1', data: '{% block body %}<p>{{ value }}</p>{% endblock body %}'});
twig({
Expand Down