From 2c1340437171778961ba333fd7ccd311c84377a8 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Wed, 16 Nov 2016 04:26:49 -0500 Subject: [PATCH] fix(reporter): do not allow URL domains to span new lines This was causing some excruciating error logs with the default format of cross-domain iframe errors: ``` Error: Blocked a frame with origin "http://localhost:9876" from accessing a cross-origin frame. at Error (native) at Function.method.restore (absolute/var/folders/8m/vg2kf8h50bj9gy5r9gwn20dw00_y0z/T/fa96dc8102e4c5416f4cd38061107a08.browserify?f4458f57b50ae627f718945da619dae7c682b4a0:60376:28) at each (absolute/var/folders/8m/vg2kf8h50bj9gy5r9gwn20dw00_y0z/T/fa96dc8102e4c5416f4cd38061107a08.browserify?f4458f57b50ae627f718945da619dae7c682b4a0:57875:33) at Object.restore (absolute/var/folders/8m/vg2kf8h50bj9gy5r9gwn20dw00_y0z/T/fa96dc8102e4c5416f4cd38061107a08.browserify?f4458f57b50ae627f718945da619dae7c682b4a0:57895:17) at Object.restore (absolute/var/folders/8m/vg2kf8h50bj9gy5r9gwn20dw00_y0z/T/fa96dc8102e4c5416f4cd38061107a08.browserify?f4458f57b50ae627f718945da619dae7c682b4a0:59163:42) at Context. (absolute/var/folders/8m/vg2kf8h50bj9gy5r9gwn20dw00_y0z/T/fa96dc8102e4c5416f4cd38061107a08.browserify?f4458f57b50ae627f718945da619dae7c682b4a0:109551:13) ``` The old regex would find that `https://` then include everything that wasn't a slash, which included the next two lines into the `/absolute`. The output would be similar to: ``` Error: Blocked a frame with origin "/var/folders/8m/vg2kf8h50bj9gy5r9gwn20dw00_y0z/T/afde9a0a28f71236ac3340462d6ca94e.browserify:60376:28 <- node_modules/sinon/lib/sinon/util/core.js:154:0) at each (/var/folders/8m/vg2kf8h50bj9gy5r9gwn20dw00_y0z/T/afde9a0a28f71236ac3340462d6ca94e.browserify:57875:33 <- node_modules/sinon/lib/sinon/collection.js:34:0) ... ``` With this, we prevent the domain from spanning the newline character, and now the output is: ``` Error: Blocked a frame with origin "http://localhost:9876" from accessing a cross-origin frame. at Error (native) at Function.method.restore (/var/folders/8m/vg2kf8h50bj9gy5r9gwn20dw00_y0z/T/6c039bf5162f620e4d2667e56f143a02.browserify:60376:28 <- node_modules/sinon/lib/sinon/util/core.js:154:0) at each (/var/folders/8m/vg2kf8h50bj9gy5r9gwn20dw00_y0z/T/6c039bf5162f620e4d2667e56f143a02.browserify:57875:33 <- node_modules/sinon/lib/sinon/collection.js:34:0) ... ``` --- lib/reporter.js | 2 +- test/unit/reporter.spec.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/reporter.js b/lib/reporter.js index d44c8788f..fcde0ad3b 100644 --- a/lib/reporter.js +++ b/lib/reporter.js @@ -25,7 +25,7 @@ var createErrorFormatter = function (config, emitter, SourceMapConsumer) { return null } - var URL_REGEXP = new RegExp('(?:https?:\\/\\/[^\\/]*)?\\/?' + + var URL_REGEXP = new RegExp('(?:https?:\\/\\/[^\\/\\s]*)?\\/?' + '(base/|absolute)' + // prefix, including slash for base/ to create relative paths. '((?:[A-z]\\:)?[^\\?\\s\\:]*)' + // path '(\\?\\w*)?' + // sha diff --git a/test/unit/reporter.spec.js b/test/unit/reporter.spec.js index ab55cd312..e7069b1bd 100644 --- a/test/unit/reporter.spec.js +++ b/test/unit/reporter.spec.js @@ -245,6 +245,21 @@ describe('reporter', () => { }) }) + it('should not try to match domains with spaces', (done) => { + formatError = m.createErrorFormatter({ basePath: '/some/base' }, emitter, MockSourceMapConsumer) + var servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')] + servedFiles[0].sourceMap = {content: 'SOURCE MAP a.js'} + servedFiles[1].sourceMap = {content: 'SOURCE MAP b.js'} + + emitter.emit('file_list_modified', {served: servedFiles}) + + _.defer(() => { + var ERROR = '"http://localhost:9876"\n at /base/b.js:2:6' + expect(formatError(ERROR)).to.equal('"http://localhost:9876"\n at /original/b.js:4:8 <- b.js:2:6\n') + done() + }) + }) + describe('Windows', () => { formatError = null var servedFiles = null