Problem/Motivation

DefaultTableMapping::__construct() has operator precedence bugs that cause malformed SQL queries when an entity type's revision/data tables are not configured (return NULL or empty string).

This issue was discovered in the 11.3.2 version, but it's present in all 11.x and 10.x.

The issue is in these lines:

// Line 135
$this->baseTable = $this->prefix . $entity_type->getBaseTable() ?: $entity_type->id();

// Line 137
$this->revisionTable = $this->prefix . $entity_type->getRevisionTable() ?: $entity_type->id() . '_revision';

// Line 140
$this->dataTable = $this->prefix . $entity_type->getDataTable() ?: $entity_type->id() . '_field_data';

// Line 143
$this->revisionDataTable = $this->prefix . $entity_type->getRevisionDataTable() ?: $entity_type->id() . '_field_revision';

Due to PHP operator precedence, . (concatenation) binds tighter than ?: (null coalescing), so these are evaluated as:

$this->revisionTable = ($this->prefix . $entity_type->getRevisionTable()) ?: ($entity_type->id() . '_revision');

When $entity_type->getRevisionTable() returns NULL:

  • $this->prefix . NULL = "test12345" (the prefix only)
  • "test12345" ?: "entity_id_revision" = "test12345" (non-empty = true, fallback NOT used)
  • Result: Table name is just the database prefix with no actual table name

This produces malformed SQL like:
INNER JOIN "test12345" "revision" ON ...

Steps to reproduce

  1. Create a kernel test that dynamically enables a module which makes a non-revisionable entity type revisionable via hook_entity_type_build()
  2. Install the entity schema BEFORE enabling the module
  3. Try to load entity revisions
  4. Observe malformed SQL with incomplete table names

Proposed resolution

Add parentheses to ensure correct operator precedence:

// Line 135
$this->baseTable = $this->prefix . ($entity_type->getBaseTable() ?: $entity_type->id());

// Line 137
$this->revisionTable = $this->prefix . ($entity_type->getRevisionTable() ?: $entity_type->id() . '_revision');

// Line 140
$this->dataTable = $this->prefix . ($entity_type->getDataTable() ?: $entity_type->id() . '_field_data');

// Line 143
$this->revisionDataTable = $this->prefix . ($entity_type->getRevisionDataTable() ?: $entity_type->id() . '_field_revision');

Remaining tasks

  • Review

Issue fork drupal-3572144

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

  • main Comparechanges, plain diff MR !14680
  • issue-3572144 Comparecompare

Comments

sam711 created an issue. See original summary.

sam711’s picture

Issue summary: View changes
Status: Active » Needs review

Created patch and tests.

sam711’s picture

Issue summary: View changes
quietone’s picture

Version: 11.x-dev » main

Hi, Issues for Drupal core should be targeted to the 'main' branch, our primary development branch. Changes are made on the main branch first, and are then back ported as needed according to the Core change policies. The version the problem was discovered on should be stated in the issue summary Problem/Motivation section. Thanks.

sam711’s picture

Issue summary: View changes
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Bug Smash Initiative

Small comments on the MR

sam711’s picture

Status: Needs work » Needs review

I made the changes and forgot to update the issue here.

smustgrave’s picture

Status: Needs review » Needs work

Small change and kinda surprised we don’t have a rule for it.

But the tests are missing doc block descriptions

If you are another contributor eager to jump in, please allow the previous poster(s) at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!

sam711’s picture

Status: Needs work » Needs review

Rebased to add the block descriptions. Replaced createMock() with createStub() to fix phpUnit test warnings.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Believe feedback for this one is addressed.

Thanks!

catch’s picture

Status: Reviewed & tested by the community » Needs work

Left a couple of comments on the test.

Also aspects of the issue summary and the test coverage look to me like they might have been generated with Claude or another LLM, could you confirm this if so? This should always be disclosed per https://www.drupal.org/docs/develop/issues/issue-procedures-and-etiquett...

sam711’s picture

Status: Needs work » Needs review

Updated.

I used Claude to generate the comments.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Think the comments are a little too verbose but not sure it’s a blocker

catch’s picture

Status: Reviewed & tested by the community » Needs work

Couple more comments on the MR.

sam711’s picture

Status: Needs work » Needs review

Updated.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Sorry for the delay, looks like feedback has been addressed.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

This is an interesting bug... I wonder though - do we not need an upgrade path here? What happens if someone has generated a schema based on the incorrect names?

smustgrave’s picture

What would that update hook look like?

alexpott’s picture

@smustgrave complex - renaming tables is no fun - I wonder if it is necessary I can't work it out without doing a deeper dive - but I need to ask the question.

sam711’s picture

@alexpott any additional insights on this?

catch’s picture

I think to trigger the bug in real life, you'd need to explicitly specify the base table in the entity type definition but set it as an empty string or null. And then these queries would immediately be failing. So to me this seems like something you can only run into in a test, or would have broken your site anyway.

sam711’s picture

If we agree this is a bug, I think the question is why we shouldn't fix it. Comment #20 doesn't really clarify that. If fixing it would break backward compatibility, then I think it should target the next major version, provide an upgrade path or both.

smustgrave’s picture

Sorry for the delay but @sam711 think regardless we will need an upgrade path

smustgrave’s picture

Could this land in 12? @catch or @alexpott?

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.