Skip to content

A free open source IT asset/license management system

Labels: Fixed FD-57822 / - qg6w-r9jm - protect against memory exhaustion in label preview

snipeb8d2fced6d61
diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php
index e28e795615..3a1e8ce714 100755
--- a/app/Http/Controllers/LabelsController.php
+++ b/app/Http/Controllers/LabelsController.php
@@ -81,11 +81,21 @@ class LabelsController extends Controller
}
});
- $settings = Setting::getSettings();
- if (request()->has('settings')) {
- $overrides = request()->input('settings');
+ // Clone to isolate preview overrides from the Setting::getSettings()
+ // singleton. Without the clone, one caller's `?settings[key]=value`
+ // mutates the static cache for the lifetime of the PHP-FPM worker,
+ // poisoning settings reads for every subsequent request on the same
+ // worker. Allowlist to the label2_ prefix matches the keys the
+ // Settings → Labels preview UI actually sends from
+ // resources/views/partials/label2-preview.blade.php, so an attacker
+ // cannot drive arbitrary settings keys from this endpoint.
+ $settings = clone Setting::getSettings();
+ $overrides = request()->input('settings');
+ if (is_array($overrides)) {
foreach ($overrides as $key => $value) {
- $settings->$key = $value;
+ if (is_string($key) && str_starts_with($key, 'label2_')) {
+ $settings->$key = $value;
+ }
}
}
diff --git a/app/View/Label.php b/app/View/Label.php
index 717efae189..486c5c445b 100644
--- a/app/View/Label.php
+++ b/app/View/Label.php
@@ -217,7 +217,12 @@ class Label implements View
return $toAdd ? $myFields->push($toAdd) : $myFields;
}, new Collection);
- $emptyRowsCount = $settings->label2_empty_row_count;
+ // Stick to the template's physical capacity because the
+ // ->take() below drops everything past it anyway. This
+ // also handles unbounded or negative values reaching
+ // range(), which otherwise materializes an N-element array
+ // before the ->take() can throw it away
+ $emptyRowsCount = min(max((int) $settings->label2_empty_row_count, 0), $template->getSupportFields());
if ($emptyRowsCount) {
// Create empty rows
$emptyRows = collect(range(1, $emptyRowsCount))->map(function () {
diff --git a/tests/Feature/Labels/LabelPreviewRequestOverrideTest.php b/tests/Feature/Labels/LabelPreviewRequestOverrideTest.php
new file mode 100644
index 0000000000..08e34c3740
--- /dev/null
+++ b/tests/Feature/Labels/LabelPreviewRequestOverrideTest.php
@@ -0,0 +1,125 @@
+<?php
+
+namespace Tests\Feature\Labels;
+
+use App\Models\Setting;
+use App\Models\User;
+use PHPUnit\Framework\Attributes\Test;
+use Tests\TestCase;
+
+/**
+ * Regression tests for two defects in the label-preview endpoint:
+ *
+ * 1. The `settings[key]=value` override loop used to mutate the live
+ * `Setting::getSettings()` singleton, poisoning the static cache for
+ * every subsequent request on the same PHP-FPM worker. A clone keeps
+ * preview overrides request-scoped. An allowlist on the label2_ prefix
+ * bounds the attack surface to the keys the Settings UI actually sends.
+ *
+ * 2. The label2 renderer called `range(1, $settings->label2_empty_row_count)`
+ * and materialized the full result before `->take()` truncated it to
+ * the template's physical capacity. An unbounded request value therefore
+ * scaled CPU and memory with user input (CWE-400 / CWE-1284).
+ */
+class LabelPreviewRequestOverrideTest extends TestCase
+{
+ #[Test]
+ public function non_label2_override_does_not_mutate_the_setting_singleton(): void
+ {
+ $this->settings->set(['per_page' => 20]);
+
+ $this->actingAs(User::factory()->create())
+ ->get(route('labels.show', ['labelName' => 'DefaultLabel']).'?settings[per_page]=1');
+
+ $this->assertSame(
+ 20,
+ (int) Setting::getSettings()->per_page,
+ 'A non-allowlisted override must not reach Setting::getSettings().'
+ );
+ }
+
+ #[Test]
+ public function label2_override_does_not_mutate_the_setting_singleton(): void
+ {
+ $this->settings->set(['label2_title' => 'ORIGINAL']);
+
+ $this->actingAs(User::factory()->create())
+ ->get(route('labels.show', ['labelName' => 'DefaultLabel']).'?settings[label2_title]=HACKED');
+
+ $this->assertSame(
+ 'ORIGINAL',
+ Setting::getSettings()->label2_title,
+ 'Even an allowlisted override must stay scoped to the clone, not the singleton.'
+ );
+ }
+
+ #[Test]
+ public function preview_does_not_amplify_memory_when_label2_empty_row_count_is_a_large_integer(): void
+ {
+ // The clamp inside Label::materializeFieldsForAsset bounds the request
+ // value to the template's physical capacity before range() runs. The
+ // bench compares a baseline request (empty_row_count=1) against the
+ // attacker scenario (25000). If the clamp works, peak memory does not
+ // grow between them because both runs materialize at most
+ // DefaultLabel::getSupportFields() rows.
+ $this->settings->set([
+ 'label2_enable' => 1,
+ 'label2_template' => 'DefaultLabel',
+ 'label2_fields' => 'Asset Tag=asset_tag',
+ ]);
+
+ // TCPDF echoes bytes to the output stream, so wrap each request in
+ // an output buffer to isolate the two renders from each other.
+ ob_start();
+ $this->actingAs(User::factory()->create())
+ ->get(route('labels.show', ['labelName' => 'DefaultLabel']).'?settings[label2_empty_row_count]=1')
+ ->assertOk();
+ ob_end_clean();
+ $baselinePeak = memory_get_peak_usage();
+
+ ob_start();
+ $this->actingAs(User::factory()->create())
+ ->get(route('labels.show', ['labelName' => 'DefaultLabel']).'?settings[label2_empty_row_count]=25000')
+ ->assertOk();
+ ob_end_clean();
+ $amplifiedPeak = memory_get_peak_usage();
+
+ $delta = $amplifiedPeak - $baselinePeak;
+ $this->assertLessThan(
+ 1 * 1024 * 1024,
+ $delta,
+ 'Clamp failed. Attacker scenario grew peak memory by '.$delta.' bytes over the baseline.'
+ );
+ }
+
+ #[Test]
+ public function preview_renders_a_pdf_when_label2_empty_row_count_is_negative(): void
+ {
+ // range(1, -1) throws a ValueError in PHP 8, so a negative override
+ // used to blow the request. The clamp drops anything below 0 to 0.
+ $this->settings->set([
+ 'label2_enable' => 1,
+ 'label2_template' => 'DefaultLabel',
+ 'label2_fields' => 'Asset Tag=asset_tag',
+ ]);
+
+ $this->actingAs(User::factory()->create())
+ ->get(route('labels.show', ['labelName' => 'DefaultLabel']).'?settings[label2_empty_row_count]=-5')
+ ->assertOk();
+ }
+
+ #[Test]
+ public function preview_renders_a_pdf_when_label2_empty_row_count_is_non_numeric(): void
+ {
+ // `(int) 'banana'` is 0 in PHP, so the clamp also handles junk input.
+ $this->settings->set([
+ 'label2_enable' => 1,
+ 'label2_template' => 'DefaultLabel',
+ 'label2_fields' => 'Asset Tag=asset_tag',
+ ]);
+
+ $this->actingAs(User::factory()->create())
+ ->get(route('labels.show', ['labelName' => 'DefaultLabel']).'?settings[label2_empty_row_count]=banana')
+ ->assertOk();
+ }
+}