Skip to content

Commit ddcfca9

Browse files
committed
fix: prevent file upload with js included
1 parent a15191c commit ddcfca9

File tree

5 files changed

+59
-9
lines changed

5 files changed

+59
-9
lines changed

backend/app/Http/Controllers/FileManagerController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function getFinderOptions()
4545
);
4646

4747
$finderOptions->setBind(
48-
'get.pre file.pre archive.pre back.pre chmod.pre colwidth.pre copy.pre cut.pre duplicate.pre editor.pre put.pre
48+
'get.pre file.pre archive.pre back.pre chmod.pre colwidth.pre copy.pre cut.pre duplicate.pre editor.pre
4949
extract.pre forward.pre fullscreen.pre getfile.pre help.pre home.pre info.pre mkdir.pre mkfile.pre
5050
netmount.pre netunmount.pre open.pre opendir.pre paste.pre places.pre quicklook.pre reload.pre
5151
rename.pre resize.pre restore.pre rm.pre search.pre sort.pre up.pre upload.pre view.pre zipdl.pre

backend/app/Providers/AccessControlProvider.php

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,13 @@ public function checkPermission($command, ...$args)
9595
$error = '';
9696
}
9797

98-
if (!empty($error)) {
99-
try {
98+
try {
99+
if (!empty($error)) {
100100
throw new PreCommandException($error);
101-
} catch (PreCommandException $th) {
102-
return $th->getError();
103101
}
102+
$this->scanFile($command, $args);
103+
} catch (PreCommandException $th) {
104+
return $th->getError();
104105
}
105106
}
106107

@@ -158,4 +159,52 @@ private function isFileAllowedToOpen($args)
158159

159160
return false;
160161
}
162+
163+
public function scanFile($command, $args)
164+
{
165+
if (!\in_array($command, ['put', 'upload']) || \in_array('javascript', Plugin::instance()->permissions()->getEnabledFileType())) {
166+
return;
167+
}
168+
$content = '';
169+
170+
if ($command === 'upload' && isset($args[0]['FILES']['upload']['tmp_name'])) {
171+
$filePath = '';
172+
$fileName = '';
173+
$filePath = $args[0]['FILES']['upload']['tmp_name'][0];
174+
$fileName = $args[0]['FILES']['upload']['name'][0];
175+
$fileTypeAndExt = wp_check_filetype_and_ext($filePath, $fileName);
176+
if (isset($fileTypeAndExt['ext'], $fileTypeAndExt['type']) && (strpos($fileTypeAndExt['type'], 'text') !== false || strpos($fileTypeAndExt['type'], 'pdf') !== false)) {
177+
$content = file_get_contents($filePath);
178+
}
179+
} elseif (isset($_REQUEST['content'])) {
180+
$content = $_REQUEST['content'];
181+
}
182+
183+
if (empty($content)) {
184+
return;
185+
}
186+
187+
$containsJs = false;
188+
189+
$maliciousPatterns = [
190+
'/<script.*?>.*?<\/script>/is',
191+
'/onload=["\'].*?["\']/is',
192+
'/<.*?javascript:.*?>/is',
193+
'/<.*?on\w+=[^>]+>/is',
194+
'/\/S \/JavaScript \/JS /is',
195+
];
196+
197+
foreach ($maliciousPatterns as $pattern) {
198+
if (preg_match($pattern, $content)) {
199+
$containsJs = true;
200+
201+
break;
202+
}
203+
}
204+
205+
if ($containsJs) {
206+
207+
throw new PreCommandException(__('The file contains JS code. Please remove the code and try again. Or allow js mimetype', 'file-manager'));
208+
}
209+
}
161210
}

backend/app/Providers/FileEditValidator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function validate($cmd, &$args, $elfinder, $volume)
1313
{
1414
try {
1515
$this->checkPermission();
16+
Plugin::instance()->accessControl()->scanFile($cmd, $args);
1617
} catch (PreCommandException $th) {
1718
return $th->getError();
1819
}

frontend/src/pages/Layout/ui/Navigation/TopNavigation/static/MenuItems.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ export const items: Array<ProductDetail> = [
4444
'Customer Support Button with SMS Call Button, Click to Chat Messenger, Live Chat Support Chat Button'
4545
},
4646
{
47-
key: 'bit-pi',
47+
key: 'bit-flows',
4848
label: (
49-
<a href="https://bitapps.pro" target="_blank" rel="noreferrer">
50-
Bit Pi
49+
<a href="https://bit-flows.com" target="_blank" rel="noreferrer">
50+
Bit Flows
5151
</a>
5252
),
5353
title: 'An advanced integration plugin for your WordPress website.'

frontend/src/pages/root/Root.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default function Root() {
7070
setIsOpening(true)
7171
})
7272

73-
finder.bind('opendone reload', () => {
73+
finder.bind('opendone reload sync', () => {
7474
setIsOpening(false)
7575
})
7676

0 commit comments

Comments
 (0)