Skip to content

Commit 261971c

Browse files
pxpmStyleCIBot
andauthored
Add encrypt_mimes config option (#88)
* add option to disabled mimes encryption * Apply fixes from StyleCI * bc --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 0ec4d88 commit 261971c

5 files changed

Lines changed: 40 additions & 14 deletions

File tree

config/elfinder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,17 @@
9090
'root_options' => [
9191

9292
],
93+
94+
/*
95+
|--------------------------------------------------------------------------
96+
| Encrypt MIME Types
97+
|--------------------------------------------------------------------------
98+
|
99+
| When enabled, MIME types configured on browse/browse_multiple fields are
100+
| encrypted before being passed to the frontend, preventing CASUAL URL
101+
| tampering. Note that this is a UI-level convenience only — NOT A SECURITY MEASURE.
102+
| Determined users can always bypass client-side restrictions.
103+
|
104+
*/
105+
'encrypt_mimes' => true,
93106
];

resources/views/fields/browse.blade.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{{-- browse server input --}}
22
@php
33
$field['attributes']['data-elfinder-trigger-url'] = $field['attributes']['data-elfinder-trigger-url'] ?? url(config('elfinder.route.prefix').'/popup/'.$field['name']);
4-
$field['attributes']['data-elfinder-trigger-url'] .= '?mimes='.urlencode(Crypt::encrypt($field['mime_types'] ?? ''));
4+
$mimeTypes = $field['mime_types'] ?? '';
5+
$field['attributes']['data-elfinder-trigger-url'] .= '?mimes='.urlencode(
6+
config('elfinder.encrypt_mimes', true) ? Crypt::encrypt($mimeTypes) : json_encode($mimeTypes, JSON_UNESCAPED_SLASHES)
7+
);
58
@endphp
69
@include('crud::fields.inc.wrapper_start')
710
<label>{!! $field['label'] !!}</label>

resources/views/fields/browse_multiple.blade.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
$field['wrapper']['data-init-function'] = $field['wrapper']['data-init-function'] ?? 'bpFieldInitBrowseMultipleElement';
1212
$field['wrapper']['data-elfinder-trigger-url'] = $field['wrapper']['data-elfinder-trigger-url'] ?? url(config('elfinder.route.prefix').'/popup/'.$field['name'].'?multiple=1');
1313
14-
$field['wrapper']['data-elfinder-trigger-url'] .= '&mimes='.urlencode(Crypt::encrypt($field['mime_types'] ?? ''));
14+
$mimeTypes = $field['mime_types'] ?? '';
15+
$field['wrapper']['data-elfinder-trigger-url'] .= '&mimes='.urlencode(
16+
config('elfinder.encrypt_mimes', true) ? Crypt::encrypt($mimeTypes) : json_encode($mimeTypes, JSON_UNESCAPED_SLASHES)
17+
);
1518
1619
if ($multiple) {
1720
$field['wrapper']['data-multiple'] = "true";

resources/views/standalonepopup.blade.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
<script type="text/javascript">
3232
$(document).ready(function () {
33+
3334
let elfinderConfig = {
3435
cssAutoLoad : false,
3536
speed: 100,
@@ -46,7 +47,7 @@
4647
url: '{{ route("elfinder.connector") }}', // connector URL
4748
soundPath: '{{ Basset::getUrl(base_path("vendor/studio-42/elfinder/sounds")) }}',
4849
resizable: false,
49-
onlyMimes: @json(urldecode(json_decode(request('mimes'))), JSON_UNESCAPED_SLASHES),
50+
onlyMimes: @json(json_decode(urldecode(request('mimes'))), JSON_UNESCAPED_SLASHES),
5051
commandsOptions: {
5152
getfile: {
5253
multiple: {{ request('multiple') ? 'true' : 'false' }},
@@ -66,8 +67,10 @@
6667
},
6768
};
6869
let elfinderOptions = window.parent.elfinderOptions ?? {};
70+
@if(config('elfinder.encrypt_mimes', true))
71+
delete elfinderOptions.onlyMimes;
72+
@endif
6973
var elf = $('#elfinder').elfinder({...elfinderConfig, ...elfinderOptions}).elfinder('instance');
70-
7174
document.getElementById('elfinder').style.opacity = 1;
7275
});
7376
</script>

src/BackpackElfinderController.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@ public function showPopup($input_id)
1212
{
1313
$mimes = request('mimes');
1414

15-
if (! isset($mimes)) {
16-
Log::error('Someone attempted to tamper with mime types in elfinder popup. The attempt was blocked.');
17-
abort(403, 'Unauthorized action.');
18-
}
19-
20-
try {
21-
$mimes = Crypt::decrypt(urldecode(request('mimes')));
22-
} catch (\Illuminate\Contracts\Encryption\DecryptException $e) {
23-
Log::error('Someone attempted to tamper with mime types in elfinder popup. The attempt was blocked.');
24-
abort(403, 'Unauthorized action.');
15+
if (config('elfinder.encrypt_mimes', true)) {
16+
if (! isset($mimes)) {
17+
Log::error('Someone attempted to tamper with mime types in elfinder popup. The attempt was blocked.');
18+
abort(403, 'Unauthorized action.');
19+
}
20+
21+
try {
22+
$mimes = Crypt::decrypt(urldecode($mimes));
23+
} catch (\Illuminate\Contracts\Encryption\DecryptException $e) {
24+
Log::error('Someone attempted to tamper with mime types in elfinder popup. The attempt was blocked.');
25+
abort(403, 'Unauthorized action.');
26+
}
27+
} else {
28+
$mimes = $mimes ? json_decode(urldecode($mimes), true) : '';
2529
}
2630

2731
if (! empty($mimes)) {

0 commit comments

Comments
 (0)