-
Notifications
You must be signed in to change notification settings - Fork 84
Fixed filter failure on unescaped urls #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,7 @@ public function setBasePath($basePath) | |
| public function absolutizeUrl($url, $quote, $cssFile) | ||
| { | ||
| // is already absolute | ||
| if (preg_match('/^([a-z]+:\/)?\//', $url)) { | ||
| if (preg_match('/^([a-z]+:|\/)/i', $url)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you think you could add some more tests for this method, so it's obviouse what you were tryting to solve?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What exactly would you want? Paths from all urls are passed into this match, if it is https? or data uri, it is returned as is, if not, the path is then parsed further.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like some unit tests for this method only.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added 2 assertions into |
||
| return $url; | ||
| } | ||
|
|
||
|
|
@@ -71,7 +71,7 @@ public function absolutizeUrl($url, $quote, $cssFile) | |
|
|
||
| $path = $this->cannonicalizePath($path); | ||
|
|
||
| return $quote === '"' ? addslashes($path) : $path; | ||
| return !$quote ? addslashes($path) : $path; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -107,30 +107,22 @@ public function cannonicalizePath($path) | |
| */ | ||
| public function __invoke($code, \WebLoader\Compiler $loader, $file = null) | ||
| { | ||
| // thanks to kravco | ||
| $regexp = '~ | ||
| (?<![a-z]) | ||
| url\( ## url( | ||
| \s* ## optional whitespace | ||
| ([\'"])? ## optional single/double quote | ||
| (?!data:) ## keep data URIs | ||
| ( (?: (?:\\\\.)+ ## escape sequences | ||
| | [^\'"\\\\,()\s]+ ## safe characters | ||
| | (?(1) (?!\1)[\'"\\\\,() \t] ## allowed special characters | ||
| | ^ ## (none, if not quoted) | ||
| ) | ||
| )* ## (greedy match) | ||
| ) | ||
| (?(1)\1) ## optional single/double quote | ||
| \s* ## optional whitespace | ||
| \) ## ) | ||
| ~xs'; | ||
|
|
||
| $self = $this; | ||
|
|
||
| return preg_replace_callback($regexp, function ($matches) use ($self, $file) | ||
| $regexp = '/ | ||
| url\( ## url( | ||
| \s* ## optional space | ||
| ([\'"])? ## optional quote | ||
| ([^\)]+) ## anything up to ")" character | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO this can be simplified, but you've simplified it too much. It must match anything, including escaping characters, except the end of the quotation and ending bracked. something like this should work
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well well well, this should pass in ok, I've added it into style.css (so it is tested) but looks like I've discovered another problem, return $quote === '"' ? addslashes($path) : $path; |
||
| (?(1)\1) ## optional quote | ||
| \s* ## optional space | ||
| \) ## ) | ||
| /ix'; | ||
|
|
||
| return preg_replace_callback($regexp, function ($matches) use ($file) | ||
| { | ||
| return "url('" . $self->absolutizeUrl($matches[2], $matches[1], $file) . "')"; | ||
| $path = trim($matches[2]); // Remove new lines, spaces, etc | ||
| $path = trim($path, '\'"'); // Remove quotes on each end (if any) | ||
|
|
||
| return "url('" . $this->absolutizeUrl($path, $matches[1], $file) . "')"; | ||
| }, $code); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| @import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic); | ||
|
|
||
| body { | ||
| background: url(img/boxed-bg.jpg); | ||
| } | ||
|
|
||
| .greedy-match { | ||
| background: url("img/\"layer1.jpg"), url('img\'/layer2.jpg'); | ||
| } | ||
|
|
||
| .addslashes-if-unquoted { | ||
| background: url(img/'layer1.jpg); | ||
| } | ||
|
|
||
| .quote-mismatch { | ||
| background: url("img/layer2.jpg'); | ||
| } | ||
|
|
||
| ul { | ||
| list-style-image: url( | ||
| data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg== | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| @import url('https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic'); | ||
|
|
||
| body { | ||
| background: url('/fixtures/img/boxed-bg.jpg'); | ||
| } | ||
|
|
||
| .greedy-match { | ||
| background: url('/fixtures/img/\"layer1.jpg'), url('/fixtures/img\'/layer2.jpg'); | ||
| } | ||
|
|
||
| .addslashes-if-unquoted { | ||
| background: url('/fixtures/img/\'layer1.jpg'); | ||
| } | ||
|
|
||
| .quote-mismatch { | ||
| background: url('/fixtures/img/layer2.jpg'); | ||
| } | ||
|
|
||
| ul { | ||
| list-style-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems like a separate issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just extension, I removed data uri filtering from the regexp so I filter it here.