@@ -13,6 +13,15 @@ class AccessControlProvider
1313{
1414 public $ settings ;
1515
16+ private $ maliciousPatterns = [
17+ '/<script.*?>.*?<\/script>/is ' ,
18+ '/onload=[" \'].*?[" \']/is ' ,
19+ '/<.*?javascript:.*?>/is ' ,
20+ '/<.*?on\w+=[^>]+>/is ' ,
21+ '/\/S \/JavaScript \/JS /is ' ,
22+ ];
23+ private $ scannedResult = [];
24+
1625 public function __construct ()
1726 {
1827 $ this ->settings = Plugin::instance ()->preferences ();
@@ -165,54 +174,78 @@ public function scanFile($command, $args)
165174 if (!\in_array ($ command , ['put ' , 'upload ' ]) || \in_array ('javascript ' , Plugin::instance ()->permissions ()->getEnabledFileType ())) {
166175 return ;
167176 }
168- $ content = '' ;
169177
170- if ($ command === 'upload ' && isset ($ args [0 ]['FILES ' ]['upload ' ]['tmp_name ' ])) {
178+ if (isset ($ args [0 ]['chunk ' ]) && !empty ($ args [0 ]['chunk ' ])) {
179+ return ;
180+ }
181+
182+ if (
183+ $ command === 'upload ' &&
184+ !empty ($ args [0 ]['FILES ' ]['upload ' ]['tmp_name ' ]) &&
185+ is_array ($ args [0 ]['FILES ' ]['upload ' ]['tmp_name ' ])
186+ ) {
171187 $ filePath = '' ;
172188 $ 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 , $ args [0 ]['FILES ' ]['upload ' ]['type ' ][0 ]);
176-
177- if (
178- isset ($ fileTypeAndExt ['ext ' ], $ fileTypeAndExt ['type ' ])
179- && (strpos ($ fileTypeAndExt ['type ' ], 'text ' ) !== false || strpos ($ fileTypeAndExt ['type ' ], 'pdf ' ) !== false )
180- || current_user_can ('administrator ' )
181- ) {
182- $ content = file_get_contents ($ filePath );
183- } else {
184- throw new PreCommandException (__ ('Failed to process the file ' , 'file-manager ' ));
189+ $ uploadedFiles = $ args [0 ]['FILES ' ]['upload ' ]['tmp_name ' ];
190+ error_log (print_r ($ args [0 ]['FILES ' ]['upload ' ], true ));
191+ foreach ($ uploadedFiles as $ index => $ tmpName ) {
192+ $ content = '' ;
193+ $ filePath = $ args [0 ]['FILES ' ]['upload ' ]['tmp_name ' ][$ index ];
194+ $ fileName = $ args [0 ]['FILES ' ]['upload ' ]['name ' ][$ index ];
195+ if (empty ($ filePath )) {
196+ continue ;
197+ }
198+ $ fileTypeAndExt = wp_check_filetype_and_ext ($ filePath , $ fileName );
199+ error_log (print_r ($ fileTypeAndExt , true ));
200+ if (!empty ($ fileTypeAndExt ['type ' ])) {
201+ if (stripos ($ fileTypeAndExt ['type ' ], 'javascript ' ) !== false ) {
202+ $ this ->scannedResult [] = sprintf (__ ('This file %s type is not allowed ' , 'file-manager ' ), $ fileName );
203+ }
204+ if (
205+ stripos ($ fileTypeAndExt ['type ' ], 'text ' ) !== false ||
206+ stripos ($ fileTypeAndExt ['type ' ], 'pdf ' ) !== false
207+ ) {
208+ $ content = file_get_contents ($ filePath );
209+ }
210+ } else {
211+ try {
212+ $ content = file_get_contents ($ filePath );
213+ } catch (\Exception $ e ) {
214+ $ this ->scannedResult [] = sprintf (__ ('Failed to process this file %s ' , 'file-manager ' ), $ fileName );
215+ }
216+ }
217+
218+ if (!empty ($ content )) {
219+ $ this ->scanForPattern ($ content , $ fileName );
220+ }
185221 }
186222 } elseif (isset ($ _REQUEST ['content ' ])) {
187- $ content = $ _REQUEST ['content ' ];
223+ $ this -> scanForPattern ( $ _REQUEST ['content ' ], '' ) ;
188224 }
189- if (empty ($ content )) {
190- return ;
225+
226+ if (count ($ this ->scannedResult ) > 0 ) {
227+ throw new PreCommandException (
228+ implode ('. >> ' , $ this ->scannedResult )
229+ );
191230 }
192231
193- $ containsJs = false ;
232+ }
194233
195- $ maliciousPatterns = [
196- '/<script.*?>.*?<\/script>/is ' ,
197- '/onload=[" \'].*?[" \']/is ' ,
198- '/<.*?javascript:.*?>/is ' ,
199- '/<.*?on\w+=[^>]+>/is ' ,
200- '/\/S \/JavaScript \/JS /is ' ,
201- ];
202-
203- foreach ($ maliciousPatterns as $ pattern ) {
234+ private function scanForPattern ($ content , $ fileName )
235+ {
236+ $ containsJs = false ;
237+ foreach ($ this ->maliciousPatterns as $ pattern ) {
204238 if (preg_match ($ pattern , $ content )) {
205239 $ containsJs = true ;
206240
207241 break ;
208242 }
209243 }
210244
211-
212-
213245 if ($ containsJs ) {
214-
215- throw new PreCommandException (__ ('The file contains JS code. Please remove the code and try again. Or allow js mimetype ' , 'file-manager ' ));
246+ $ this ->scannedResult [] = sprintf (__ ('This file %s contains JS code. Please remove the code and try again. Or allow js mimetype ' , 'file-manager ' ), $ fileName );
216247 }
217248 }
218249}
250+
251+
0 commit comments