Skip to content

Commit d1aab2c

Browse files
authored
Merge pull request #25 from uacode/master
Some security fixes
2 parents 6672682 + e014ef0 commit d1aab2c

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ Add a disk config in `config/admin.php`:
3434
'extensions' => [
3535

3636
'media-manager' => [
37-
37+
3838
           // Select a local disk that you configured in `config/filesystem.php`
39-
        'disk' => 'public'
39+
        'disk' => 'public',
40+
'allowed_ext' => 'jpg,jpeg,png,pdf,doc,docx,zip'
4041
],
4142
],
4243

src/MediaController.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ public function download(Request $request)
3333

3434
$manager = new MediaManager($file);
3535

36-
return $manager->download();
36+
try {
37+
return $manager->download();
38+
} catch (\Exception $e) {
39+
return response()->json([
40+
'status' => false,
41+
'message' => $e->getMessage(),
42+
]);
43+
}
3744
}
3845

3946
public function upload(Request $request)
@@ -69,7 +76,7 @@ public function delete(Request $request)
6976
}
7077
} catch (\Exception $e) {
7178
return response()->json([
72-
'status' => true,
79+
'status' => false,
7380
'message' => $e->getMessage(),
7481
]);
7582
}
@@ -91,7 +98,7 @@ public function move(Request $request)
9198
}
9299
} catch (\Exception $e) {
93100
return response()->json([
94-
'status' => true,
101+
'status' => false,
95102
'message' => $e->getMessage(),
96103
]);
97104
}
@@ -113,7 +120,7 @@ public function newFolder(Request $request)
113120
}
114121
} catch (\Exception $e) {
115122
return response()->json([
116-
'status' => true,
123+
'status' => false,
117124
'message' => $e->getMessage(),
118125
]);
119126
}

src/MediaManager.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class MediaManager extends Extension
2626
*/
2727
protected $storage;
2828

29+
/**
30+
* List of allowed extensions.
31+
*
32+
* @var string
33+
*/
34+
protected $allowed = [];
35+
2936
/**
3037
* @var array
3138
*/
@@ -50,6 +57,10 @@ public function __construct($path = '/')
5057
{
5158
$this->path = $path;
5259

60+
if (!empty(config('admin.extensions.media-manager.allowed_ext'))) {
61+
$this->allowed = explode(',', config('admin.extensions.media-manager.allowed_ext'));
62+
}
63+
5364
$this->initStorage();
5465
}
5566

@@ -77,10 +88,10 @@ public function ls()
7788
$directories = $this->storage->directories($this->path);
7889

7990
return $this->formatDirectories($directories)
80-
->merge($this->formatFiles($files))
81-
->sort(function ($item) {
82-
return $item['name'];
83-
})->all();
91+
->merge($this->formatFiles($files))
92+
->sort(function ($item) {
93+
return $item['name'];
94+
})->all();
8495
}
8596

8697
/**
@@ -92,7 +103,12 @@ public function ls()
92103
*/
93104
protected function getFullPath($path)
94105
{
95-
return $this->storage->getDriver()->getAdapter()->applyPathPrefix($path);
106+
$path = $this->storage->getDriver()->getAdapter()->applyPathPrefix($path);
107+
if (strstr($fullPath, '..')) {
108+
throw new \Exception('Incorrect path');
109+
}
110+
111+
return $path;
96112
}
97113

98114
public function download()
@@ -125,6 +141,11 @@ public function delete($path)
125141

126142
public function move($new)
127143
{
144+
$ext = pathinfo($new, PATHINFO_EXTENSION);
145+
if ($this->allowed && !in_array($ext, $this->allowed)) {
146+
throw new \Exception('File extension '.$ext.' is not allowed');
147+
}
148+
128149
return $this->storage->move($this->path, $new);
129150
}
130151

@@ -137,6 +158,10 @@ public function move($new)
137158
public function upload($files = [])
138159
{
139160
foreach ($files as $file) {
161+
if ($this->allowed && !in_array($file->getClientOriginalExtension(), $this->allowed)) {
162+
throw new \Exception('File extension '.$file->getClientOriginalExtension().' is not allowed');
163+
}
164+
140165
$this->storage->putFileAs($this->path, $file, $file->getClientOriginalName());
141166
}
142167

0 commit comments

Comments
 (0)