Skip to content
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

fix: handle exception on loading invalid base64 source maps #53

Merged
merged 1 commit into from
Sep 30, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var baseRegex = "\\s*[@#]\\s*sourceMappingURL\\s*=\\s*([^\\s]*)(?![\\S\\s]*sourc
// Matches // .... comments
regex2 = new RegExp("//"+baseRegex+"($|\n|\r\n?)"),
// Matches DataUrls
regexDataUrl = /data:[^;\n]+(?:;charset=[^;\n]+)?;base64,(.*)/;
regexDataUrl = /data:[^;\n]+(?:;charset=[^;\n]+)?;base64,([a-zA-Z0-9+/]+={0,2})/;

module.exports = function(input, inputMap) {
this.cacheable && this.cacheable();
Expand All @@ -28,7 +28,16 @@ module.exports = function(input, inputMap) {
var dataUrlMatch = regexDataUrl.exec(url);
var callback = this.async();
if(dataUrlMatch) {
processMap(JSON.parse((new Buffer(dataUrlMatch[1], "base64")).toString()), this.context, callback);
var mapBase64 = dataUrlMatch[1];
var mapStr = (new Buffer(mapBase64, "base64")).toString();
var map;
try {
map = JSON.parse(mapStr)
} catch (e) {
emitWarning("Cannot parse inline SourceMap '" + mapBase64.substr(0, 50) + "': " + e);
return untouched();
}
processMap(map, this.context, callback);
} else {
resolve(this.context, loaderUtils.urlToRequest(url), function(err, result) {
if(err) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/invalid-inline-source-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
without SourceMap
// @sourceMappingURL=data:application/source-map;base64,"something invalid"
// comment
3 changes: 3 additions & 0 deletions test/fixtures/invalid-inline-source-map2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
without SourceMap
// @sourceMappingURL=data:application/source-map;base64,invalid/base64=
// comment
22 changes: 22 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,28 @@ describe("source-map-loader", function() {
done();
});
});
it("should skip invalid base64 SourceMap", function (done) {
execLoader(path.join(__dirname, "fixtures", "invalid-inline-source-map.js"), function (err, res, map, deps, warns) {
should.equal(err, null);
warns.should.be.eql([]);
should.equal(res, "without SourceMap\n// @sourceMappingURL=data:application/source-map;base64,\"something invalid\"\n// comment");
should.equal(map, null);
deps.should.be.eql([]);
done();
});
});
it("should warn on invalid base64 SourceMap", function (done) {
execLoader(path.join(__dirname, "fixtures", "invalid-inline-source-map2.js"), function (err, res, map, deps, warns) {
should.equal(err, null);
warns.should.matchEach(
new RegExp("Cannot parse inline SourceMap 'invalid\/base64=': SyntaxError: Unexpected token")
);
should.equal(res, "without SourceMap\n// @sourceMappingURL=data:application/source-map;base64,invalid/base64=\n// comment");
should.equal(map, null);
deps.should.be.eql([]);
done();
});
});
it("should warn on missing SourceMap", function(done) {
execLoader(path.join(__dirname, "fixtures", "missing-source-map.js"), function(err, res, map, deps, warns) {
should.equal(err, null);
Expand Down