From 46f66d17fef8b43e7644c05a717f1ddb23994c32 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Mon, 12 Oct 2015 08:28:07 +0100 Subject: [PATCH] Fixes #679 - wrong rebasing of remote URLs. There was an edge case when a remote stylesheet pointed to a URL in a different domain. Introduced in #667. --- History.md | 5 +++++ lib/urls/rewrite.js | 8 ++++++++ test/protocol-imports-test.js | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/History.md b/History.md index fd6f0dd00..1793283dc 100644 --- a/History.md +++ b/History.md @@ -3,6 +3,11 @@ * Requires Node.js 4.0+ to run. +[3.4.6 / 2015-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.5...3.4) +================== + +* Fixed issue [#679](https://github.com/jakubpawlowicz/clean-css/issues/679) - wrong rebasing of remote URLs. + [3.4.5 / 2015-09-28](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.4...v3.4.5) ================== diff --git a/lib/urls/rewrite.js b/lib/urls/rewrite.js index f2f1152d0..13a57e22e 100644 --- a/lib/urls/rewrite.js +++ b/lib/urls/rewrite.js @@ -25,6 +25,11 @@ function isRemote(uri) { return /^[^:]+?:\/\//.test(uri) || uri.indexOf('//') === 0; } +function isSameOrigin(uri1, uri2) { + return url.parse(uri1).protocol == url.parse(uri2).protocol && + url.parse(uri1).host == url.parse(uri2).host; +} + function isImport(uri) { return uri.lastIndexOf('.css') === uri.length - 4; } @@ -63,6 +68,9 @@ function rebase(uri, options) { if (isRemote(uri) && !isRemote(options.toBase)) return uri; + if (isRemote(uri) && !isSameOrigin(uri, options.toBase)) + return uri; + if (!isRemote(uri) && isRemote(options.toBase)) return url.resolve(options.toBase, uri); diff --git a/test/protocol-imports-test.js b/test/protocol-imports-test.js index 502a4c5da..35fa86ac0 100644 --- a/test/protocol-imports-test.js +++ b/test/protocol-imports-test.js @@ -239,6 +239,25 @@ vows.describe('protocol imports').addBatch({ nock.cleanAll(); } }, + 'of an existing file with absolute URLs in different domain': { + topic: function () { + this.reqMocks = nock('http://127.0.0.1') + .get('/base.css') + .reply(200, 'a{background:url(http://example.com/deeply/images/test.png)}'); + + new CleanCSS().minify('@import url(http://127.0.0.1/base.css);', this.callback); + }, + 'should not raise errors': function (errors, minified) { + assert.isNull(errors); + }, + 'should process @import': function (errors, minified) { + assert.equal(minified.styles, 'a{background:url(http://example.com/deeply/images/test.png)}'); + }, + teardown: function () { + assert.isTrue(this.reqMocks.isDone()); + nock.cleanAll(); + } + }, 'of an unreachable domain': { topic: function () { new CleanCSS().minify('@import url(http://0.0.0.0/custom.css);a{color:red}', this.callback);