Skip to content

Commit

Permalink
fix: Handle exception on loading invalid base64 source maps (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
zuohaocheng authored and joshwiens committed Sep 30, 2017
1 parent e0bff76 commit 38da2eb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
13 changes: 11 additions & 2 deletions index.js
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
@@ -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
@@ -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
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

0 comments on commit 38da2eb

Please sign in to comment.