Skip to content

Commit 38da2eb

Browse files
zuohaochengjoshwiens
authored andcommitted
fix: Handle exception on loading invalid base64 source maps (#53)
1 parent e0bff76 commit 38da2eb

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

index.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var baseRegex = "\\s*[@#]\\s*sourceMappingURL\\s*=\\s*([^\\s]*)(?![\\S\\s]*sourc
1515
// Matches // .... comments
1616
regex2 = new RegExp("//"+baseRegex+"($|\n|\r\n?)"),
1717
// Matches DataUrls
18-
regexDataUrl = /data:[^;\n]+(?:;charset=[^;\n]+)?;base64,(.*)/;
18+
regexDataUrl = /data:[^;\n]+(?:;charset=[^;\n]+)?;base64,([a-zA-Z0-9+/]+={0,2})/;
1919

2020
module.exports = function(input, inputMap) {
2121
this.cacheable && this.cacheable();
@@ -28,7 +28,16 @@ module.exports = function(input, inputMap) {
2828
var dataUrlMatch = regexDataUrl.exec(url);
2929
var callback = this.async();
3030
if(dataUrlMatch) {
31-
processMap(JSON.parse((new Buffer(dataUrlMatch[1], "base64")).toString()), this.context, callback);
31+
var mapBase64 = dataUrlMatch[1];
32+
var mapStr = (new Buffer(mapBase64, "base64")).toString();
33+
var map;
34+
try {
35+
map = JSON.parse(mapStr)
36+
} catch (e) {
37+
emitWarning("Cannot parse inline SourceMap '" + mapBase64.substr(0, 50) + "': " + e);
38+
return untouched();
39+
}
40+
processMap(map, this.context, callback);
3241
} else {
3342
resolve(this.context, loaderUtils.urlToRequest(url), function(err, result) {
3443
if(err) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
without SourceMap
2+
// @sourceMappingURL=data:application/source-map;base64,"something invalid"
3+
// comment
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
without SourceMap
2+
// @sourceMappingURL=data:application/source-map;base64,invalid/base64=
3+
// comment

test/index.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,28 @@ describe("source-map-loader", function() {
127127
done();
128128
});
129129
});
130+
it("should skip invalid base64 SourceMap", function (done) {
131+
execLoader(path.join(__dirname, "fixtures", "invalid-inline-source-map.js"), function (err, res, map, deps, warns) {
132+
should.equal(err, null);
133+
warns.should.be.eql([]);
134+
should.equal(res, "without SourceMap\n// @sourceMappingURL=data:application/source-map;base64,\"something invalid\"\n// comment");
135+
should.equal(map, null);
136+
deps.should.be.eql([]);
137+
done();
138+
});
139+
});
140+
it("should warn on invalid base64 SourceMap", function (done) {
141+
execLoader(path.join(__dirname, "fixtures", "invalid-inline-source-map2.js"), function (err, res, map, deps, warns) {
142+
should.equal(err, null);
143+
warns.should.matchEach(
144+
new RegExp("Cannot parse inline SourceMap 'invalid\/base64=': SyntaxError: Unexpected token")
145+
);
146+
should.equal(res, "without SourceMap\n// @sourceMappingURL=data:application/source-map;base64,invalid/base64=\n// comment");
147+
should.equal(map, null);
148+
deps.should.be.eql([]);
149+
done();
150+
});
151+
});
130152
it("should warn on missing SourceMap", function(done) {
131153
execLoader(path.join(__dirname, "fixtures", "missing-source-map.js"), function(err, res, map, deps, warns) {
132154
should.equal(err, null);

0 commit comments

Comments
 (0)