From 52f926a44c43b6389dd2e3793e41429fbb3f791d Mon Sep 17 00:00:00 2001 From: Henry Jonas Date: Tue, 1 Dec 2026 14:52:22 -0400 Subject: [PATCH 1/2] FOUR-34573: Customize UI / settings upload-file allows PHP upload to public storage, leading to remote code execution (exploited in the wild) --- .../Controllers/Api/SettingController.php | 60 ++++++++++-- .../admin/settings/components/SettingFile.vue | 12 ++- tests/Feature/Api/SettingFileUploadTest.php | 96 +++++++++++++++++++ 3 files changed, 158 insertions(+), 10 deletions(-) create mode 100644 tests/Feature/Api/SettingFileUploadTest.php diff --git a/ProcessMaker/Http/Controllers/Api/SettingController.php b/ProcessMaker/Http/Controllers/Api/SettingController.php index 8bee58deac..6e9d545e2f 100644 --- a/ProcessMaker/Http/Controllers/Api/SettingController.php +++ b/ProcessMaker/Http/Controllers/Api/SettingController.php @@ -5,6 +5,7 @@ use DB; use Illuminate\Database\QueryException; use Illuminate\Http\Request; +use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; use ProcessMaker\Cache\Settings\SettingCacheFactory; @@ -346,16 +347,63 @@ public function upload(Request $request) $settingKey = $request->input('setting_key'); $setting = Setting::byKey($settingKey); - $this->uploadFile($setting->refresh(), $request, 'file', $settingKey, 'settings'); + if (!$setting || $setting->format !== 'file') { + return response()->json([ + 'message' => 'The selected setting is not a file setting.', + ], 422); + } + + $file = $request->file('file'); + if (!$file instanceof UploadedFile) { + return response()->json([ + 'message' => 'No file was provided.', + ], 422); + } + + $setting = $setting->refresh(); + $validationError = $this->validateSettingFile($setting, $file); + if ($validationError !== null) { + return response()->json(['message' => $validationError], 422); + } + + $this->uploadFile($setting, $request, 'file', $settingKey, 'settings'); + + return response()->json(['message' => 'File uploaded.']); } - private function uploadFile(Setting $setting, Request $request, $filename, $collectionName, $diskName) + private function validateSettingFile(Setting $setting, UploadedFile $file): ?string { - $data = $request->all(); + $ui = (array) ($setting->ui ?? []); + $allowedExtensions = $ui['allowed_extensions'] ?? null; + $allowedMimeTypes = $ui['mime_types_allowed'] ?? null; + + if (!is_array($allowedExtensions) || $allowedExtensions === []) { + return 'The file setting is missing allowed_extensions.'; + } - if (isset($data[$filename]) && !empty($data[$filename]) && $data[$filename] != 'null') { - $disk = $setting->ui->is_public ? 'settings' : 'private_settings'; - Storage::disk($disk)->put($collectionName, file_get_contents($request->file($filename))); + if (!is_array($allowedMimeTypes) || $allowedMimeTypes === []) { + return 'The file setting is missing mime_types_allowed.'; + } + + $extension = strtolower($file->getClientOriginalExtension()); + $allowedExtensions = array_map('strtolower', $allowedExtensions); + if (!in_array($extension, $allowedExtensions, true)) { + return 'The file extension is not allowed for this setting.'; + } + + if (!in_array($file->getMimeType(), $allowedMimeTypes, true)) { + return 'The file MIME type is not allowed for this setting.'; + } + + return null; + } + + private function uploadFile(Setting $setting, Request $request, $filename, $collectionName, $diskName) + { + if ($request->hasFile($filename)) { + $isPublic = $setting->ui->is_public ?? false; + $disk = $isPublic ? 'settings' : 'private_settings'; + Storage::disk($disk)->put($collectionName, $request->file($filename)->getContent()); if (property_exists($setting->ui, 'copy_to') && $setting->ui->copy_to) { //to use mustache replacement in destination file $mustache = app(\Mustache_Engine::class); diff --git a/resources/js/admin/settings/components/SettingFile.vue b/resources/js/admin/settings/components/SettingFile.vue index c440929eb8..27262edfcd 100644 --- a/resources/js/admin/settings/components/SettingFile.vue +++ b/resources/js/admin/settings/components/SettingFile.vue @@ -9,6 +9,9 @@

{{ $t('Please upload a settings file.') }}

+
{{ $t("File uploaded") }}
@@ -53,6 +56,7 @@ export default { changed: false, loading: false, imported: false, + errorMessage: null, input: null, showModal: false, transformed: null, @@ -106,6 +110,7 @@ export default { this.changed = false; this.loading = false; this.imported = false; + this.errorMessage = null; }, onSave() { this.showModal = false; @@ -113,6 +118,7 @@ export default { }, onImport() { this.loading = true; + this.errorMessage = null; let formData = new FormData(); formData.append('file', this.file); formData.append('setting_key', this.setting.key || 'services.ldap.certificate'); @@ -128,10 +134,8 @@ export default { this.changed = true; }) .catch((error) => { - if (error.response && error.response.data && error.response.data.error) { - message = this.$t(error.response.data.error); - ProcessMaker.alert(message, "danger"); - } + const message = error.response?.data?.message || this.$t('An error occurred while uploading the file.'); + this.errorMessage = this.$t(message); this.loading = false; }); }, diff --git a/tests/Feature/Api/SettingFileUploadTest.php b/tests/Feature/Api/SettingFileUploadTest.php new file mode 100644 index 0000000000..4b9a1ca011 --- /dev/null +++ b/tests/Feature/Api/SettingFileUploadTest.php @@ -0,0 +1,96 @@ +createFileSetting([ + 'allowed_extensions' => ['txt'], + 'mime_types_allowed' => ['text/plain'], + ]); + + $response = $this->uploadFile( + UploadedFile::fake()->create('document.txt', 1, 'text/plain') + ); + + $response->assertOk(); + Storage::disk('private_settings')->assertExists(Setting::query()->latest('id')->first()->key); + } + + public function testItRejectsAnExtensionNotAllowedByTheSetting(): void + { + $this->createFileSetting([ + 'allowed_extensions' => ['txt'], + 'mime_types_allowed' => ['text/plain'], + ]); + + $response = $this->uploadFile( + UploadedFile::fake()->create('document.pdf', 1, 'application/pdf') + ); + + $response->assertStatus(422) + ->assertJson(['message' => 'The file extension is not allowed for this setting.']); + } + + public function testItRejectsAMimeTypeNotAllowedByTheSetting(): void + { + $this->createFileSetting([ + 'allowed_extensions' => ['txt'], + 'mime_types_allowed' => ['text/plain'], + ]); + + $response = $this->uploadFile( + UploadedFile::fake()->create('document.txt', 1, 'application/json') + ); + + $response->assertStatus(422) + ->assertJson(['message' => 'The file MIME type is not allowed for this setting.']); + } + + public function testItRejectsAFileSettingWithoutValidationRules(): void + { + $this->createFileSetting(['is_public' => false]); + + $response = $this->uploadFile( + UploadedFile::fake()->create('document.txt', 1, 'text/plain') + ); + + $response->assertStatus(422) + ->assertJson(['message' => 'The file setting is missing allowed_extensions.']); + } + + private function createFileSetting(array $ui): Setting + { + return Setting::create([ + 'key' => 'tests.file.' . uniqid(), + 'name' => 'Test file', + 'group' => 'Tests', + 'format' => 'file', + 'config' => null, + 'hidden' => false, + 'readonly' => false, + 'ui' => $ui, + ]); + } + + private function uploadFile(UploadedFile $file) + { + $setting = Setting::query()->latest('id')->first(); + + return $this->actingAs($this->user, 'api')->post( + route('api.settings.upload-file'), + ['setting_key' => $setting->key, 'file' => $file], + ); + } +} From f97ca07bba61bb4f46a16d9c6308f0c007e5bac0 Mon Sep 17 00:00:00 2001 From: Henry Jonas Date: Tue, 1 Dec 2026 17:44:10 -0400 Subject: [PATCH 2/2] FOUR-34673: update cvalidations in allowed extensions to post settings --- .../Controllers/Api/SettingController.php | 41 +++++++++++++- config/files.php | 31 ++++++++++ tests/Feature/Api/SettingFileUploadTest.php | 56 ++++++++++++++++++- 3 files changed, 123 insertions(+), 5 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Api/SettingController.php b/ProcessMaker/Http/Controllers/Api/SettingController.php index 6e9d545e2f..e1e55d8d4f 100644 --- a/ProcessMaker/Http/Controllers/Api/SettingController.php +++ b/ProcessMaker/Http/Controllers/Api/SettingController.php @@ -8,6 +8,7 @@ use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; +use Illuminate\Validation\ValidationException; use ProcessMaker\Cache\Settings\SettingCacheFactory; use ProcessMaker\Events\SettingsUpdated; use ProcessMaker\Http\Controllers\Controller; @@ -276,7 +277,14 @@ public function store(Request $request) try { $this->validateWhiteListURL($request); - $setting->fill($request->json()->all()); + $payload = $request->json()->all(); + $format = $payload['format'] ?? null; + $isPublic = (bool) data_get($payload, 'ui.is_public', false); + if ($format === 'file' || $isPublic) { + $this->assertSafeStorageName((string) ($payload['key'] ?? ''), 'key'); + } + + $setting->fill($payload); $setting->saveOrFail(); return response([], 201); @@ -361,6 +369,7 @@ public function upload(Request $request) } $setting = $setting->refresh(); + $this->assertSafeStorageName($settingKey, 'setting_key'); $validationError = $this->validateSettingFile($setting, $file); if ($validationError !== null) { return response()->json(['message' => $validationError], 422); @@ -376,6 +385,16 @@ private function validateSettingFile(Setting $setting, UploadedFile $file): ?str $ui = (array) ($setting->ui ?? []); $allowedExtensions = $ui['allowed_extensions'] ?? null; $allowedMimeTypes = $ui['mime_types_allowed'] ?? null; + $extension = strtolower($file->getClientOriginalExtension()); + $mimeType = strtolower((string) $file->getMimeType()); + + if (in_array($extension, config('files.dangerous_settings_extensions', []), true)) { + return 'The file extension is not allowed for settings.'; + } + + if (in_array($mimeType, config('files.dangerous_settings_mime_types', []), true)) { + return 'The file MIME type is not allowed for settings.'; + } if (!is_array($allowedExtensions) || $allowedExtensions === []) { return 'The file setting is missing allowed_extensions.'; @@ -385,19 +404,35 @@ private function validateSettingFile(Setting $setting, UploadedFile $file): ?str return 'The file setting is missing mime_types_allowed.'; } - $extension = strtolower($file->getClientOriginalExtension()); $allowedExtensions = array_map('strtolower', $allowedExtensions); if (!in_array($extension, $allowedExtensions, true)) { return 'The file extension is not allowed for this setting.'; } - if (!in_array($file->getMimeType(), $allowedMimeTypes, true)) { + $allowedMimeTypes = array_map('strtolower', $allowedMimeTypes); + if (!in_array($mimeType, $allowedMimeTypes, true)) { return 'The file MIME type is not allowed for this setting.'; } return null; } + private function assertSafeStorageName(string $name, string $field): void + { + if ($name === '' || preg_match('/\A[A-Za-z0-9._-]+\z/D', $name) !== 1) { + throw ValidationException::withMessages([ + $field => ['The setting storage name is invalid.'], + ]); + } + + $extension = strtolower(pathinfo($name, PATHINFO_EXTENSION)); + if (in_array($extension, config('files.dangerous_settings_extensions', []), true)) { + throw ValidationException::withMessages([ + $field => ['The setting storage name uses a blocked extension.'], + ]); + } + } + private function uploadFile(Setting $setting, Request $request, $filename, $collectionName, $diskName) { if ($request->hasFile($filename)) { diff --git a/config/files.php b/config/files.php index 23f99063ea..b2713c9dca 100644 --- a/config/files.php +++ b/config/files.php @@ -33,6 +33,13 @@ 'cab', 'ar', 'iso', 'dmg', 'pkg', 'deb', 'rpm', ]; +$dangerousSettingsExtensions = [ + 'php', 'php3', 'php4', 'php5', 'php7', 'php8', + 'phtml', 'pht', 'phtm', 'phar', + 'cgi', 'pl', 'py', 'jsp', 'asp', 'aspx', + 'sh', 'bash', +]; + return [ /* |-------------------------------------------------------------------------- @@ -171,4 +178,28 @@ 'application/vnd.ms-cab-compressed', 'application/x-iso9660-image', ], + + /* + |-------------------------------------------------------------------------- + | Dangerous Settings File Types + |-------------------------------------------------------------------------- + | + | These types are always blocked for settings uploads, even when a + | setting-specific allowlist includes them. + | + */ + 'dangerous_settings_extensions' => $dangerousSettingsExtensions, + 'dangerous_settings_mime_types' => [ + 'application/x-httpd-php', + 'application/x-php', + 'text/x-php', + 'application/x-sh', + 'text/x-shellscript', + 'application/x-csh', + 'application/x-perl', + 'text/x-python', + 'application/x-python', + 'application/x-jsp', + 'application/x-asp', + ], ]; diff --git a/tests/Feature/Api/SettingFileUploadTest.php b/tests/Feature/Api/SettingFileUploadTest.php index 4b9a1ca011..33c640d4bb 100644 --- a/tests/Feature/Api/SettingFileUploadTest.php +++ b/tests/Feature/Api/SettingFileUploadTest.php @@ -58,6 +58,58 @@ public function testItRejectsAMimeTypeNotAllowedByTheSetting(): void ->assertJson(['message' => 'The file MIME type is not allowed for this setting.']); } + public function testItRejectsAGloballyDangerousExtension(): void + { + $this->createFileSetting([ + 'allowed_extensions' => ['php'], + 'mime_types_allowed' => ['text/plain'], + ]); + + $response = $this->uploadFile( + UploadedFile::fake()->create('document.php', 1, 'text/plain') + ); + + $response->assertStatus(422) + ->assertJson(['message' => 'The file extension is not allowed for settings.']); + } + + public function testItRejectsAGloballyDangerousMimeType(): void + { + $this->createFileSetting([ + 'allowed_extensions' => ['txt'], + 'mime_types_allowed' => ['application/x-php'], + ]); + + $response = $this->uploadFile( + UploadedFile::fake()->create('document.txt', 1, 'application/x-php') + ); + + $response->assertStatus(422) + ->assertJson(['message' => 'The file MIME type is not allowed for settings.']); + } + + public function testItRejectsASettingWithADangerousStorageName(): void + { + $response = $this->actingAs($this->user, 'api')->postJson( + route('api.settings.store'), + [ + 'key' => 'tests.file.php', + 'name' => 'Test file', + 'group' => 'Tests', + 'format' => 'file', + 'config' => null, + 'hidden' => false, + 'ui' => [ + 'allowed_extensions' => ['txt'], + 'mime_types_allowed' => ['text/plain'], + ], + ], + ); + + $response->assertStatus(422) + ->assertJsonValidationErrors(['key']); + } + public function testItRejectsAFileSettingWithoutValidationRules(): void { $this->createFileSetting(['is_public' => false]); @@ -70,10 +122,10 @@ public function testItRejectsAFileSettingWithoutValidationRules(): void ->assertJson(['message' => 'The file setting is missing allowed_extensions.']); } - private function createFileSetting(array $ui): Setting + private function createFileSetting(array $ui, ?string $key = null): Setting { return Setting::create([ - 'key' => 'tests.file.' . uniqid(), + 'key' => $key ?? 'tests.file.' . uniqid(), 'name' => 'Test file', 'group' => 'Tests', 'format' => 'file',