Skip to content

Commit 087edeb

Browse files
Stricter checks for not importing absolute paths
Fixes #100
1 parent cd15113 commit 087edeb

File tree

1 file changed

+29
-44
lines changed

1 file changed

+29
-44
lines changed

src/CSS.php

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,7 @@ protected function combineImports($source, $content, $parents)
120120
(?P<quotes>["\']?)
121121
122122
# fetch path
123-
(?P<path>
124-
125-
# do not fetch data uris, external sources or absolute paths
126-
(?!(
127-
["\']?
128-
(data:|https?:\\/\\/|\\/)
129-
))
130-
131-
.+?
132-
)
123+
(?P<path>.+?)
133124
134125
# (optional) close path enclosure
135126
(?P=quotes)
@@ -164,16 +155,7 @@ protected function combineImports($source, $content, $parents)
164155
(?P<quotes>["\'])
165156
166157
# fetch path
167-
(?P<path>
168-
169-
# do not fetch data uris, external sources or absolute paths
170-
(?!(
171-
["\']?
172-
(data:|https?:\\/\\/|\\/)
173-
))
174-
175-
.+?
176-
)
158+
(?P<path>.+?)
177159
178160
# close path enclosure
179161
(?P=quotes)
@@ -211,33 +193,33 @@ protected function combineImports($source, $content, $parents)
211193

212194
// only replace the import with the content if we can grab the
213195
// content of the file
214-
if ($this->canImportFile($importPath)) {
215-
// check if current file was not imported previously in the same
216-
// import chain.
217-
if (in_array($importPath, $parents)) {
218-
throw new FileImportException('Failed to import file "'.$importPath.'": circular reference detected.');
219-
}
196+
if (!$this->canImportByPath($match['path']) || !$this->canImportFile($importPath)) {
197+
continue;
198+
}
220199

221-
// grab referenced file & minify it (which may include importing
222-
// yet other @import statements recursively)
223-
$minifier = new static($importPath);
224-
$importContent = $minifier->execute($source, $parents);
200+
// check if current file was not imported previously in the same
201+
// import chain.
202+
if (in_array($importPath, $parents)) {
203+
throw new FileImportException('Failed to import file "'.$importPath.'": circular reference detected.');
204+
}
225205

226-
// check if this is only valid for certain media
227-
if (!empty($match['media'])) {
228-
$importContent = '@media '.$match['media'].'{'.$importContent.'}';
229-
}
206+
// grab referenced file & minify it (which may include importing
207+
// yet other @import statements recursively)
208+
$minifier = new static($importPath);
209+
$importContent = $minifier->execute($source, $parents);
230210

231-
// add to replacement array
232-
$search[] = $match[0];
233-
$replace[] = $importContent;
211+
// check if this is only valid for certain media
212+
if (!empty($match['media'])) {
213+
$importContent = '@media '.$match['media'].'{'.$importContent.'}';
234214
}
215+
216+
// add to replacement array
217+
$search[] = $match[0];
218+
$replace[] = $importContent;
235219
}
236220

237221
// replace the import statements
238-
$content = str_replace($search, $replace, $content);
239-
240-
return $content;
222+
return str_replace($search, $replace, $content);
241223
}
242224

243225
/**
@@ -253,18 +235,21 @@ protected function combineImports($source, $content, $parents)
253235
*/
254236
protected function importFiles($source, $content)
255237
{
256-
$extensions = array_keys($this->importExtensions);
257-
$regex = '/url\((["\']?)((?!["\']?data:).*?\.('.implode('|', $extensions).'))\\1\)/i';
258-
if ($extensions && preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) {
238+
$regex = '/url\((["\']?)(.+?)\\1\)/i';
239+
if ($this->importExtensions && preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) {
259240
$search = array();
260241
$replace = array();
261242

262243
// loop the matches
263244
foreach ($matches as $match) {
245+
$extension = substr(strrchr($match[2], '.'), 1);
246+
if ($extension && !array_key_exists($extension, $this->importExtensions)) {
247+
continue;
248+
}
249+
264250
// get the path for the file that will be imported
265251
$path = $match[2];
266252
$path = dirname($source).'/'.$path;
267-
$extension = $match[3];
268253

269254
// only replace the import with the content if we're able to get
270255
// the content of the file, and it's relatively small

0 commit comments

Comments
 (0)