Never bypass validation when saving a config object

Created on 26 June 2025, 2 days ago

Problem/Motivation

After πŸ“Œ Clean up unserialize() in the config system Active was Fixed, @alexpott and I discussed the issue further and agreed on this followup.

Drupal\Core\Config\Config::save() calls validateValue() (either directly or indirectly, via castValue()). That ensures that proper use of the config system protects against serializing anything other than nested arrays of scalar types. However, the validation is skipped if the optional $has_trusted_data parameter is set to TRUE:

  public function save($has_trusted_data = FALSE) {
// ...
    if (!$has_trusted_data) {
      if ($this->typedConfigManager->hasConfigSchema($this->name)) {
        // Ensure that the schema wrapper has the latest data.
        $this->schemaWrapper = NULL;
        $this->data = $this->castValue(NULL, $this->data);
      }
      else {
        foreach ($this->data as $key => $value) {
          $this->validateValue($key, $value);
        }
      }
    }

Steps to reproduce

Install Drupal with the Standard profile. Using drush php, execute the following:

$cf = Drupal::configFactory();
$ce->set('foo', (object) ['bar' => 17]);
$ce->save(TRUE);

There is a warning, but the config entity is saved and $ce->get('foo') is a stdClass object:

MariaDB [db]> SELECT * FROM config WHERE name = 'dblog.settings'\G
*************************** 1. row ***************************
collection: 
      name: dblog.settings
      data: a:3:{s:5:"_core";a:1:{s:19:"default_config_hash";s:43:"e883aGsrt1wFrsydlYU584PZONCSfRy0DtkZ9KzHb58";}s:9:"row_limit";i:1000;s:3:"foo";O:8:"stdClass":1:{s:3:"bar";i:17;}}
1 row in set (0.001 sec)

Proposed resolution

Update the code so that $has_trusted_data bypasses config validation but not basic validation: the config object should represent a nested array of scalar values.

Remaining tasks

User interface changes

None

Introduced terminology

None

API changes

None

Data model changes

None

Release notes snippet

N/A

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component

configuration system

Created by

πŸ‡ΊπŸ‡ΈUnited States benjifisher Boston area

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024