Skip to content

Commit 8d8a92f

Browse files
committed
verify the path in the autoloader
1 parent ffff156 commit 8d8a92f

File tree

5 files changed

+81
-42
lines changed

5 files changed

+81
-42
lines changed

lib/autoloader.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,28 @@ class Autoloader {
3434

3535
private $classPaths = array();
3636

37+
private $validRoots = [];
38+
3739
/**
3840
* Optional low-latency memory cache for class to path mapping.
41+
*
3942
* @var \OC\Memcache\Cache
4043
*/
4144
protected $memoryCache;
4245

46+
/**
47+
* Autoloader constructor.
48+
*
49+
* @param string[] $validRoots
50+
*/
51+
public function __construct(array $validRoots) {
52+
$this->validRoots = $validRoots;
53+
}
54+
55+
public function addValidRoot($root) {
56+
$this->validRoots[] = $root;
57+
}
58+
4359
/**
4460
* disable the usage of the global classpath \OC::$CLASSPATH
4561
*/
@@ -102,6 +118,15 @@ public function findClass($class) {
102118
return $paths;
103119
}
104120

121+
protected function isValidPath($fullPath) {
122+
foreach ($this->validRoots as $root) {
123+
if (substr($fullPath, 0, strlen($root) + 1) === $root . '/') {
124+
return true;
125+
}
126+
}
127+
throw new \Exception('Path nor allowed');
128+
}
129+
105130
/**
106131
* Load the specified class
107132
*
@@ -119,7 +144,7 @@ public function load($class) {
119144
$pathsToRequire = array();
120145
foreach ($this->findClass($class) as $path) {
121146
$fullPath = stream_resolve_include_path($path);
122-
if ($fullPath) {
147+
if ($fullPath && $this->isValidPath($fullPath)) {
123148
$pathsToRequire[] = $fullPath;
124149
}
125150
}
@@ -138,6 +163,7 @@ public function load($class) {
138163

139164
/**
140165
* Sets the optional low-latency cache for class to path mapping.
166+
*
141167
* @param \OC\Memcache\Cache $memoryCache Instance of memory cache.
142168
*/
143169
public function setMemoryCache(\OC\Memcache\Cache $memoryCache = null) {

lib/base.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ class OC {
115115
* the app path list is empty or contains an invalid path
116116
*/
117117
public static function initPaths() {
118-
// calculate the root directories
119-
OC::$SERVERROOT = str_replace("\\", '/', substr(__DIR__, 0, -4));
120-
121118
// ensure we can find OC_Config
122119
set_include_path(
123120
OC::$SERVERROOT . '/lib' . PATH_SEPARATOR .
@@ -513,10 +510,20 @@ public static function setRequiredIniValues() {
513510
}
514511

515512
public static function init() {
513+
// calculate the root directories
514+
OC::$SERVERROOT = str_replace("\\", '/', substr(__DIR__, 0, -4));
515+
516516
// register autoloader
517517
$loaderStart = microtime(true);
518518
require_once __DIR__ . '/autoloader.php';
519-
self::$loader = new \OC\Autoloader();
519+
self::$loader = new \OC\Autoloader([
520+
OC::$SERVERROOT . '/lib',
521+
OC::$SERVERROOT . '/core',
522+
OC::$SERVERROOT . '/settings',
523+
OC::$SERVERROOT . '/ocs',
524+
OC::$SERVERROOT . '/ocs-provider',
525+
OC::$SERVERROOT . '/3rdparty'
526+
]);
520527
spl_autoload_register(array(self::$loader, 'load'));
521528
$loaderEnd = microtime(true);
522529

@@ -539,6 +546,10 @@ public static function init() {
539546
exit();
540547
}
541548

549+
foreach(OC::$APPSROOTS as $appRoot) {
550+
self::$loader->addValidRoot($appRoot['path']);
551+
}
552+
542553
// setup the basic server
543554
self::$server = new \OC\Server(\OC::$WEBROOT);
544555
\OC::$server->getEventLogger()->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd);

tests/bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
require_once __DIR__ . '/../lib/base.php';
1010

11+
\OC::$loader->addValidRoot(OC::$SERVERROOT . '/tests');
12+
1113
// load minimum set of apps
1214
OC_App::loadApps(array('authentication'));
1315
OC_App::loadApps(array('filesystem', 'logging'));

tests/lib/autoloader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AutoLoader extends TestCase {
1616

1717
protected function setUp() {
1818
parent::setUp();
19-
$this->loader = new \OC\AutoLoader();
19+
$this->loader = new \OC\AutoLoader([]);
2020
}
2121

2222
public function testLeadingSlashOnClassName() {

tests/lib/template.php

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
<?php
2-
/**
3-
* ownCloud
4-
*
5-
* @author Bernhard Posselt
6-
* @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
7-
*
8-
* This library is free software; you can redistribute it and/or
9-
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10-
* License as published by the Free Software Foundation; either
11-
* version 3 of the License, or any later version.
12-
*
13-
* This library is distributed in the hope that it will be useful,
14-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16-
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17-
*
18-
* You should have received a copy of the GNU Affero General Public
19-
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
20-
*
21-
*/
222

3+
/**
4+
* ownCloud
5+
*
6+
* @author Bernhard Posselt
7+
* @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
8+
*
9+
* This library is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11+
* License as published by the Free Software Foundation; either
12+
* version 3 of the License, or any later version.
13+
*
14+
* This library is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public
20+
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
2323
class Test_TemplateFunctions extends \Test\TestCase {
2424

2525
protected function setUp() {
2626
parent::setUp();
2727

28-
$loader = new \OC\Autoloader();
28+
$loader = new \OC\Autoloader([OC::$SERVERROOT . '/lib']);
2929
$loader->load('OC_Template');
3030
}
3131

@@ -60,7 +60,7 @@ public function testPrintUnescapedNormalString() {
6060
// ---------------------------------------------------------------------------
6161
// Test relative_modified_date with dates only
6262
// ---------------------------------------------------------------------------
63-
public function testRelativeDateToday(){
63+
public function testRelativeDateToday() {
6464
$currentTime = 1380703592;
6565
$elementTime = $currentTime;
6666
$result = (string)relative_modified_date($elementTime, $currentTime, true);
@@ -74,7 +74,7 @@ public function testRelativeDateToday(){
7474
$this->assertEquals('today', $result);
7575
}
7676

77-
public function testRelativeDateYesterday(){
77+
public function testRelativeDateYesterday() {
7878
$currentTime = 1380703592;
7979
$elementTime = $currentTime - 24 * 3600;
8080
$result = (string)relative_modified_date($elementTime, $currentTime, true);
@@ -88,7 +88,7 @@ public function testRelativeDateYesterday(){
8888
$this->assertEquals('yesterday', $result);
8989
}
9090

91-
public function testRelativeDate2DaysAgo(){
91+
public function testRelativeDate2DaysAgo() {
9292
$currentTime = 1380703592;
9393
$elementTime = $currentTime - 48 * 3600;
9494
$result = (string)relative_modified_date($elementTime, $currentTime, true);
@@ -102,7 +102,7 @@ public function testRelativeDate2DaysAgo(){
102102
$this->assertEquals('2 days ago', $result);
103103
}
104104

105-
public function testRelativeDateLastMonth(){
105+
public function testRelativeDateLastMonth() {
106106
$currentTime = 1380703592;
107107
$elementTime = $currentTime - 86400 * 31;
108108
$result = (string)relative_modified_date($elementTime, $currentTime, true);
@@ -115,7 +115,7 @@ public function testRelativeDateLastMonth(){
115115
$this->assertEquals('last month', $result);
116116
}
117117

118-
public function testRelativeDateMonthsAgo(){
118+
public function testRelativeDateMonthsAgo() {
119119
$currentTime = 1380703592;
120120
$elementTime = $currentTime - 86400 * 65;
121121
$result = (string)relative_modified_date($elementTime, $currentTime, true);
@@ -128,7 +128,7 @@ public function testRelativeDateMonthsAgo(){
128128
$this->assertEquals('4 months ago', $result);
129129
}
130130

131-
public function testRelativeDateLastYear(){
131+
public function testRelativeDateLastYear() {
132132
$currentTime = 1380703592;
133133
$elementTime = $currentTime - 86400 * 365;
134134
$result = (string)relative_modified_date($elementTime, $currentTime, true);
@@ -141,7 +141,7 @@ public function testRelativeDateLastYear(){
141141
$this->assertEquals('last year', $result);
142142
}
143143

144-
public function testRelativeDateYearsAgo(){
144+
public function testRelativeDateYearsAgo() {
145145
$currentTime = 1380703592;
146146
$elementTime = $currentTime - 86400 * 365.25 * 2;
147147
$result = (string)relative_modified_date($elementTime, $currentTime, true);
@@ -158,31 +158,31 @@ public function testRelativeDateYearsAgo(){
158158
// Test relative_modified_date with timestamps only (date + time value)
159159
// ---------------------------------------------------------------------------
160160

161-
public function testRelativeTimeSecondsAgo(){
161+
public function testRelativeTimeSecondsAgo() {
162162
$currentTime = 1380703592;
163163
$elementTime = $currentTime - 5;
164164
$result = (string)relative_modified_date($elementTime, $currentTime, false);
165165

166166
$this->assertEquals('seconds ago', $result);
167167
}
168168

169-
public function testRelativeTimeMinutesAgo(){
169+
public function testRelativeTimeMinutesAgo() {
170170
$currentTime = 1380703592;
171171
$elementTime = $currentTime - 190;
172172
$result = (string)relative_modified_date($elementTime, $currentTime, false);
173173

174174
$this->assertEquals('3 minutes ago', $result);
175175
}
176176

177-
public function testRelativeTimeHoursAgo(){
177+
public function testRelativeTimeHoursAgo() {
178178
$currentTime = 1380703592;
179179
$elementTime = $currentTime - 7500;
180180
$result = (string)relative_modified_date($elementTime, $currentTime, false);
181181

182182
$this->assertEquals('2 hours ago', $result);
183183
}
184184

185-
public function testRelativeTime2DaysAgo(){
185+
public function testRelativeTime2DaysAgo() {
186186
$currentTime = 1380703592;
187187
$elementTime = $currentTime - 48 * 3600;
188188
$result = (string)relative_modified_date($elementTime, $currentTime, false);
@@ -196,7 +196,7 @@ public function testRelativeTime2DaysAgo(){
196196
$this->assertEquals('2 days ago', $result);
197197
}
198198

199-
public function testRelativeTimeLastMonth(){
199+
public function testRelativeTimeLastMonth() {
200200
$currentTime = 1380703592;
201201
$elementTime = $currentTime - 86400 * 31;
202202
$result = (string)relative_modified_date($elementTime, $currentTime, false);
@@ -209,7 +209,7 @@ public function testRelativeTimeLastMonth(){
209209
$this->assertEquals('last month', $result);
210210
}
211211

212-
public function testRelativeTimeMonthsAgo(){
212+
public function testRelativeTimeMonthsAgo() {
213213
$currentTime = 1380703592;
214214
$elementTime = $currentTime - 86400 * 65;
215215
$result = (string)relative_modified_date($elementTime, $currentTime, false);
@@ -222,7 +222,7 @@ public function testRelativeTimeMonthsAgo(){
222222
$this->assertEquals('4 months ago', $result);
223223
}
224224

225-
public function testRelativeTimeLastYear(){
225+
public function testRelativeTimeLastYear() {
226226
$currentTime = 1380703592;
227227
$elementTime = $currentTime - 86400 * 365;
228228
$result = (string)relative_modified_date($elementTime, $currentTime, false);
@@ -235,7 +235,7 @@ public function testRelativeTimeLastYear(){
235235
$this->assertEquals('last year', $result);
236236
}
237237

238-
public function testRelativeTimeYearsAgo(){
238+
public function testRelativeTimeYearsAgo() {
239239
$currentTime = 1380703592;
240240
$elementTime = $currentTime - 86400 * 365.25 * 2;
241241
$result = (string)relative_modified_date($elementTime, $currentTime, false);

0 commit comments

Comments
 (0)