Skip to content
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

[Stable13] Scss hardening #8156

Merged
merged 7 commits into from Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions apps/theming/css/theming.scss
Expand Up @@ -91,18 +91,20 @@
}

/* override styles for login screen in guest.css */
#header .logo {
background-image: url(#{$image-logo});
@if $theming-logo-mime != '' {
@if variable_exists('theming-logo-mime') {
#header .logo {
background-image: url(#{$image-logo});
background-size: contain;
}
}

#body-login,
#firstrunwizard .firstrunwizard-header,
#theming-preview {
background-image: url(#{$image-login-background});
background-color: $color-primary;
@if variable_exists('theming-background-mime') {
#body-login,
#firstrunwizard .firstrunwizard-header,
#theming-preview {
background-image: url(#{$image-login-background});
background-color: $color-primary;
}
}

input.primary,
Expand Down
4 changes: 2 additions & 2 deletions apps/theming/lib/ThemingDefaults.php
Expand Up @@ -242,8 +242,8 @@ public function getScssVariables() {
'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime', '') . "'"
];

$variables['image-logo'] = "'".$this->urlGenerator->getAbsoluteURL($this->getLogo())."'";
$variables['image-login-background'] = "'".$this->urlGenerator->getAbsoluteURL($this->getBackground())."'";
$variables['image-logo'] = "'".$this->getLogo()."'";
$variables['image-login-background'] = "'".$this->getBackground()."'";
$variables['image-login-plain'] = 'false';

if ($this->config->getAppValue('theming', 'color', null) !== null) {
Expand Down
10 changes: 2 additions & 8 deletions apps/theming/tests/ThemingDefaultsTest.php
Expand Up @@ -517,18 +517,12 @@ public function testGetScssVariables() {
['theming.Theming.getLoginBackground', [], 'custom-background'],
]);

$this->urlGenerator->expects($this->exactly(2))
->method('getAbsoluteURL')
->willReturnCallback(function ($path) {
return 'absolute-' . $path;
});

$expected = [
'theming-cachebuster' => '\'0\'',
'theming-logo-mime' => '\'jpeg\'',
'theming-background-mime' => '\'jpeg\'',
'image-logo' => "'absolute-custom-logo?v=0'",
'image-login-background' => "'absolute-custom-background?v=0'",
'image-logo' => "'custom-logo?v=0'",
'image-login-background' => "'custom-background?v=0'",
'color-primary' => $this->defaults->getColorPrimary(),
'color-primary-text' => '#ffffff',
'image-login-plain' => 'false',
Expand Down
21 changes: 19 additions & 2 deletions lib/private/Template/SCSSCacher.php
Expand Up @@ -63,6 +63,9 @@ class SCSSCacher {
/** @var ICache */
protected $depsCache;

/** @var null|string */
protected $injectedVariables = null;

/**
* @param ILogger $logger
* @param Factory $appDataFactory
Expand Down Expand Up @@ -153,8 +156,9 @@ private function isCached($fileNameCSS, ISimpleFolder $folder) {
return false;
}
}
return true;
}
return true;
return false;
} catch(NotFoundException $e) {
return false;
}
Expand Down Expand Up @@ -250,6 +254,7 @@ private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder
* We need to regenerate all files when variables change
*/
private function resetCache() {
$this->injectedVariables = null;
$appDirectory = $this->appData->getDirectoryListing();
if(empty($appDirectory)){
return;
Expand All @@ -267,10 +272,22 @@ private function resetCache() {
* @return string SCSS code for variables from OC_Defaults
*/
private function getInjectedVariables() {
if ($this->injectedVariables !== null)
return $this->injectedVariables;
$variables = '';
foreach ($this->defaults->getScssVariables() as $key => $value) {
$variables .= '$' . $key . ': ' . $value . ';';
}

// check for valid variables / otherwise fall back to defaults
try {
$scss = new Compiler();
$scss->compile($variables);
$this->injectedVariables = $variables;
} catch (ParserException $e) {
$this->logger->error($e, ['app' => 'core']);
}

return $variables;
}

Expand All @@ -281,7 +298,7 @@ private function getInjectedVariables() {
* @return string
*/
private function rebaseUrls($css, $webDir) {
$re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x';
$re = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x';
$subst = 'url(\''.$webDir.'/$1\')';
return preg_replace($re, $subst, $css);
}
Expand Down
18 changes: 14 additions & 4 deletions tests/lib/Template/SCSSCacherTest.php
Expand Up @@ -351,11 +351,21 @@ public function testCacheFailure() {
$this->assertFalse($actual);
}

public function testRebaseUrls() {
public function dataRebaseUrls() {
return [
['#id { background-image: url(\'../img/image.jpg\'); }','#id { background-image: url(\'/apps/files/css/../img/image.jpg\'); }'],
['#id { background-image: url("../img/image.jpg"); }','#id { background-image: url(\'/apps/files/css/../img/image.jpg\'); }'],
['#id { background-image: url(\'/img/image.jpg\'); }','#id { background-image: url(\'/img/image.jpg\'); }'],
['#id { background-image: url("http://example.com/test.jpg"); }','#id { background-image: url("http://example.com/test.jpg"); }'],
];
}

/**
* @dataProvider dataRebaseUrls
*/
public function testRebaseUrls($scss, $expected) {
$webDir = '/apps/files/css';
$css = '#id { background-image: url(\'../img/image.jpg\'); }';
$actual = self::invokePrivate($this->scssCacher, 'rebaseUrls', [$css, $webDir]);
$expected = '#id { background-image: url(\'/apps/files/css/../img/image.jpg\'); }';
$actual = self::invokePrivate($this->scssCacher, 'rebaseUrls', [$scss, $webDir]);
$this->assertEquals($expected, $actual);
}

Expand Down