diff --git a/lib/middleware/karma.js b/lib/middleware/karma.js index a36a53617..2e603b382 100644 --- a/lib/middleware/karma.js +++ b/lib/middleware/karma.js @@ -151,6 +151,9 @@ var createKarmaMiddleware = function (filesPromise, serveStaticFile, serveFile, var mappings = files.served.map(function (file) { // Windows paths contain backslashes and generate bad IDs if not escaped var filePath = filePathToUrlPath(file.path, basePath, urlRoot).replace(/\\/g, '\\\\') + // Escape single quotes that might be in the filename - + // double quotes should not be allowed! + filePath = filePath.replace(/'/g, '\\\'') return util.format(" '%s': '%s'", filePath, file.sha) }) diff --git a/test/unit/middleware/karma.spec.js b/test/unit/middleware/karma.spec.js index c7e905792..534a33453 100644 --- a/test/unit/middleware/karma.spec.js +++ b/test/unit/middleware/karma.spec.js @@ -282,6 +282,21 @@ describe('middleware.karma', () => { callHandlerWith('/__karma__/context.html') }) + it('should escape quotes in mappings with all served files', (done) => { + fsMock._touchFile('/karma/static/context.html', 0, '%MAPPINGS%') + servedFiles([ + new MockFile("/some/abc/a'b.js", 'sha_a'), + new MockFile('/base/path/ba.js', 'sha_b') + ]) + + response.once('end', () => { + expect(response).to.beServedAs(200, 'window.__karma__.files = {\n \'/__karma__/absolute/some/abc/a\\\'b.js\': \'sha_a\',\n \'/__karma__/base/ba.js\': \'sha_b\'\n};\n') + done() + }) + + callHandlerWith('/__karma__/context.html') + }) + it('should serve debug.html with replaced script tags without timestamps', (done) => { includedFiles([ new MockFile('/first.js'),