Skip to content

A free open source IT asset/license management system

Fixed encrypted custom fields escaping

snipe39ff553b3e14
diff --git a/app/Http/Transformers/ActionlogsTransformer.php b/app/Http/Transformers/ActionlogsTransformer.php
index b7adbadb22..8190156056 100644
--- a/app/Http/Transformers/ActionlogsTransformer.php
+++ b/app/Http/Transformers/ActionlogsTransformer.php
@@ -118,8 +118,8 @@ class ActionlogsTransformer
// Display the changes if the user is an admin or superadmin
if (Gate::allows('admin')) {
- $clean_meta[$fieldname]['old'] = ($enc_old) ? unserialize($enc_old, ['allowed_classes' => false]) : '';
- $clean_meta[$fieldname]['new'] = ($enc_new) ? unserialize($enc_new, ['allowed_classes' => false]) : '';
+ $clean_meta[$fieldname]['old'] = ($enc_old) ? e(unserialize($enc_old, ['allowed_classes' => false])) : '';
+ $clean_meta[$fieldname]['new'] = ($enc_new) ? e(unserialize($enc_new, ['allowed_classes' => false])) : '';
}
}
diff --git a/tests/Feature/Assets/Api/AssetHistoryTest.php b/tests/Feature/Assets/Api/AssetHistoryTest.php
new file mode 100644
index 0000000000..4f61934405
--- /dev/null
+++ b/tests/Feature/Assets/Api/AssetHistoryTest.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace Tests\Feature\Assets\Api;
+
+use App\Models\Asset;
+use App\Models\CustomField;
+use App\Models\User;
+use Illuminate\Support\Facades\Crypt;
+use Tests\TestCase;
+
+class AssetHistoryTest extends TestCase
+{
+ public function test_encrypted_custom_field_values_are_html_encoded_in_history_for_admins()
+ {
+ $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL');
+
+ $field = CustomField::factory()->testEncrypted()->create();
+ $asset = Asset::factory()->hasEncryptedCustomField($field)->create([
+ $field->db_column => Crypt::encrypt('safe initial value'),
+ ]);
+ $superuser = User::factory()->superuser()->create();
+
+ $this->actingAsForApi($superuser)
+ ->patchJson(route('api.assets.update', $asset->id), [
+ $field->db_column_name() => '<img src=x onerror=alert(1)>',
+ ])
+ ->assertOk();
+
+ $response = $this->actingAsForApi($superuser)
+ ->getJson(route('api.assets.history', ['asset' => $asset->id, 'action_type' => 'update']))
+ ->assertOk()
+ ->assertJsonPath('total', 1);
+
+ $logMeta = $response->json('rows.0.log_meta');
+ $this->assertNotNull($logMeta, 'log_meta should be present in history');
+
+ $fieldEntry = $logMeta[$field->db_column] ?? null;
+ $this->assertNotNull($fieldEntry, 'Encrypted field should appear in log_meta');
+
+ $newValue = $fieldEntry['new'];
+ $this->assertStringNotContainsString('<img', $newValue, 'Raw HTML tag must not appear in history log_meta');
+ $this->assertStringNotContainsString('onerror', $newValue, 'Raw event handler must not appear in history log_meta');
+ $this->assertStringContainsString('&lt;', $newValue, 'Value should be HTML-encoded in history log_meta');
+ }
+
+ public function test_encrypted_custom_field_values_are_masked_for_non_admins()
+ {
+ $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL');
+
+ $field = CustomField::factory()->testEncrypted()->create();
+ $asset = Asset::factory()->hasEncryptedCustomField($field)->create([
+ $field->db_column => Crypt::encrypt('safe initial value'),
+ ]);
+ $superuser = User::factory()->superuser()->create();
+
+ $this->actingAsForApi($superuser)
+ ->patchJson(route('api.assets.update', $asset->id), [
+ $field->db_column_name() => '<img src=x onerror=alert(1)>',
+ ])
+ ->assertOk();
+
+ $viewer = User::factory()->viewAssets()->viewAssetHistory()->create();
+
+ $response = $this->actingAsForApi($viewer)
+ ->getJson(route('api.assets.history', $asset->id))
+ ->assertOk()
+ ->assertJsonPath('total', 1);
+
+ $logMeta = $response->json('rows.0.log_meta');
+ $fieldEntry = $logMeta[$field->db_column] ?? null;
+
+ if ($fieldEntry !== null) {
+ $this->assertEquals('************', $fieldEntry['new'], 'Non-admin should see masked value for encrypted field changes');
+ $this->assertStringNotContainsString('<img', $fieldEntry['new']);
+ }
+ }
+}