Skip to content

fix(loader): uncontrolled string concatenation leads to max call stack exceeded #690

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

Closed
wants to merge 2 commits into from
Closed
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
15 changes: 10 additions & 5 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ module.exports = function(content, map) {
if(query.url !== false && result.urlItems.length > 0) {
urlEscapeHelper = "var escape = require(" + loaderUtils.stringifyRequest(this, require.resolve("./url/escape.js")) + ");\n";

var concatPatternCnt = 0;
cssAsString = cssAsString.replace(result.urlItemRegExpG, function(item) {
// split concat pattern "" + ... + "" -> ("" + ... + "") + ("" + ... + "") to avoid maximum call stack exceeded error
concatPatternCnt += 1
var concatPattern = concatPatternCnt % 1000 === 0 ? ") + (" : " + "

var match = result.urlItemRegExp.exec(item);
var idx = +match[1];
var urlItem = result.urlItems[idx];
Expand All @@ -101,14 +106,14 @@ module.exports = function(content, map) {
if(idx > 0) { // idx === 0 is catched by isUrlRequest
// in cases like url('webfont.eot?#iefix')
urlRequest = url.substr(0, idx);
return "\" + escape(require(" + loaderUtils.stringifyRequest(this, urlRequest) + ")) + \"" +
return "\"" + concatPattern + "escape(require(" + loaderUtils.stringifyRequest(this, urlRequest) + ")) + \"" +
url.substr(idx);
}
urlRequest = url;
return "\" + escape(require(" + loaderUtils.stringifyRequest(this, urlRequest) + ")) + \"";
return "\"" + concatPattern + "escape(require(" + loaderUtils.stringifyRequest(this, urlRequest) + ")) + \"";
}.bind(this));
}

var exportJs = compileExports(result, importItemMatcher.bind(this), camelCaseKeys);
if (exportJs) {
exportJs = "exports.locals = " + exportJs + ";";
Expand All @@ -126,9 +131,9 @@ module.exports = function(content, map) {
}
map.file = map.file.split("!").pop().replace(/\\/g, '/');
map = JSON.stringify(map);
moduleJs = "exports.push([module.id, " + cssAsString + ", \"\", " + map + "]);";
moduleJs = "exports.push([module.id, (" + cssAsString + "), \"\", " + map + "]);";
} else {
moduleJs = "exports.push([module.id, " + cssAsString + ", \"\"]);";
moduleJs = "exports.push([module.id, (" + cssAsString + "), \"\"]);";
}

// embed runtime
Expand Down
28 changes: 28 additions & 0 deletions test/urlConcatTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*globals describe */

var helpers = require("./helpers");
var test = helpers.test;

describe("url concat", function() {
this.timeout(10000);

var actualCSS, expectedCSS, i;

actualCSS = '';
expectedCSS = '';
for (i = 0; i < 100; i++) {
actualCSS += ".class" + i + " { background-image: url(./path/to/file.png); }";
expectedCSS += ".class" + i + " { background-image: url({./path/to/file.png}); }";
}

test("should handle concat of 201 strings", actualCSS, [[1, expectedCSS, ""]]);

actualCSS = '';
expectedCSS = '';
for (i = 0; i < 10000; i++) {
actualCSS += ".class" + i + " { background-image: url(./path/to/file.png); }";
expectedCSS += ".class" + i + " { background-image: url({./path/to/file.png}); }";
}

test("should handle concat of 20001 strings", actualCSS, [[1, expectedCSS, ""]]);
});