Skip to content

A free open source IT asset/license management system

Escape custom fields in custom asset report CSV export

snipef1defc5c7a02
diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php
index c8a0f2fe9d..62c63efa8d 100644
--- a/app/Http/Controllers/ReportsController.php
+++ b/app/Http/Controllers/ReportsController.php
@@ -759,7 +759,13 @@ class ReportsController extends Controller
$executionTime = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
Log::debug('Starting headers: '.$executionTime);
- fputcsv($handle, $header);
+ // Formula-escape the header before writing. Custom-field
+ // names are attacker-editable via the customfields
+ // permission and carry no character filter, so a header
+ // cell like "=cmd|'/c calc.exe'!A1" would evaluate as a
+ // formula on a reports.view user's workstation.
+ $headerFormatter = new EscapeFormula('`');
+ fputcsv($handle, $headerFormatter->escapeRecord($header));
$executionTime = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
Log::debug('Added headers: '.$executionTime);
diff --git a/tests/Feature/Reporting/Custom/CustomAssetReportCsvFormulaEscapeTest.php b/tests/Feature/Reporting/Custom/CustomAssetReportCsvFormulaEscapeTest.php
new file mode 100644
index 0000000000..fd7f81fbc0
--- /dev/null
+++ b/tests/Feature/Reporting/Custom/CustomAssetReportCsvFormulaEscapeTest.php
@@ -0,0 +1,100 @@
+<?php
+
+namespace Tests\Feature\Reporting\Custom;
+
+use App\Models\Asset;
+use App\Models\AssetModel;
+use App\Models\CustomField;
+use App\Models\CustomFieldset;
+use App\Models\User;
+use League\Csv\Reader;
+use Tests\TestCase;
+
+/**
+ * Regression coverage for the GHSA-r7wf-qq4r-q98w CSV formula
+ * injection reported by zx (@manus-pi). postCustom() was appending
+ * `$customfield->name` verbatim to the CSV header array and writing
+ * that with `fputcsv($handle, $header)` unescaped, so a user with
+ * the `customfields` permission could name a field with a leading
+ * `=` / `+` / `-` / `@` and have it evaluated as a formula on a
+ * `reports.view` operator's workstation when they open the CSV.
+ * The data rows in the same handler DID run through EscapeFormula.
+ *
+ * Fix mirrors the data-row treatment on the header row, run
+ * unconditionally rather than gated on `escape_formulas` because
+ * header protection is not optional.
+ */
+class CustomAssetReportCsvFormulaEscapeTest extends TestCase
+{
+ private function fieldsetWithPoisonedCustomField(string $name): CustomField
+ {
+ $customField = CustomField::factory()->create(['name' => $name]);
+
+ $fieldset = CustomFieldset::factory()->create();
+ $fieldset->fields()->attach($customField, ['order' => 1, 'required' => false]);
+ $model = AssetModel::factory()->create(['fieldset_id' => $fieldset->id]);
+
+ Asset::factory()->for($model, 'model')->create(['name' => 'header-escape-test-asset']);
+
+ return $customField;
+ }
+
+ public function test_custom_field_header_with_formula_prefix_is_escaped(): void
+ {
+ $poisoned = $this->fieldsetWithPoisonedCustomField('=cmd|\'/c calc.exe\'!A1');
+
+ $response = $this->actingAs(User::factory()->canViewReports()->create())
+ ->post('reports/custom', [
+ 'asset_name' => '1',
+ $poisoned->db_column_name() => '1',
+ ])
+ ->assertOk();
+
+ $header = Reader::createFromString($response->streamedContent())->fetchOne(0);
+
+ $this->assertNotContains($poisoned->name, $header, 'Unescaped attacker-controlled header cell leaked into the CSV.');
+ $this->assertContains('`'.$poisoned->name, $header, 'Header cell must be prefixed with the EscapeFormula neutralizer.');
+ }
+
+ public function test_custom_field_header_with_plus_and_at_prefixes_are_escaped(): void
+ {
+ $plusField = $this->fieldsetWithPoisonedCustomField('+cmd|/c calc');
+ $atField = $this->fieldsetWithPoisonedCustomField('@SUM(A1:A9)');
+
+ $response = $this->actingAs(User::factory()->canViewReports()->create())
+ ->post('reports/custom', [
+ 'asset_name' => '1',
+ $plusField->db_column_name() => '1',
+ $atField->db_column_name() => '1',
+ ])
+ ->assertOk();
+
+ $header = Reader::createFromString($response->streamedContent())->fetchOne(0);
+
+ $this->assertContains('`+cmd|/c calc', $header, 'Plus-prefixed header must be neutralized.');
+ $this->assertContains('`@SUM(A1:A9)', $header, 'At-prefixed header must be neutralized.');
+ }
+
+ public function test_header_escape_is_unconditional_even_when_data_row_escaping_is_disabled(): void
+ {
+ // Data rows respect config('app.escape_formulas'), but the
+ // header is unconditional because a header cell derived from
+ // a user-editable custom field name is always attacker-
+ // controlled.
+ config(['app.escape_formulas' => false]);
+
+ $poisoned = $this->fieldsetWithPoisonedCustomField('=HYPERLINK("http://attacker.test","x")');
+
+ $response = $this->actingAs(User::factory()->canViewReports()->create())
+ ->post('reports/custom', [
+ 'asset_name' => '1',
+ $poisoned->db_column_name() => '1',
+ ])
+ ->assertOk();
+
+ $header = Reader::createFromString($response->streamedContent())->fetchOne(0);
+
+ $this->assertNotContains($poisoned->name, $header, 'Header cell must be neutralized even when data-row escaping is disabled.');
+ $this->assertContains('`'.$poisoned->name, $header);
+ }
+}