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
- Create a kernel test that dynamically enables a module which makes a non-revisionable entity type revisionable via hook_entity_type_build()
- Install the entity schema BEFORE enabling the module
- Try to load entity revisions
- 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
| Comment | File | Size | Author |
|---|
Issue fork drupal-3572144
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:
Comments
Comment #3
sam711 commentedCreated patch and tests.
Comment #4
sam711 commentedComment #5
quietone commentedHi, 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.
Comment #6
sam711 commentedComment #7
smustgrave commentedSmall comments on the MR
Comment #8
sam711 commentedI made the changes and forgot to update the issue here.
Comment #9
smustgrave commentedSmall 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!
Comment #10
sam711 commentedRebased to add the block descriptions. Replaced createMock() with createStub() to fix phpUnit test warnings.
Comment #11
smustgrave commentedBelieve feedback for this one is addressed.
Thanks!
Comment #12
catchLeft 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...
Comment #13
sam711 commentedUpdated.
I used Claude to generate the comments.
Comment #14
smustgrave commentedThink the comments are a little too verbose but not sure it’s a blocker
Comment #15
catchCouple more comments on the MR.
Comment #16
sam711 commentedUpdated.
Comment #17
smustgrave commentedSorry for the delay, looks like feedback has been addressed.
Comment #18
alexpottThis 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?
Comment #19
smustgrave commentedWhat would that update hook look like?
Comment #20
alexpott@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.
Comment #21
sam711 commented@alexpott any additional insights on this?
Comment #22
catchI 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.
Comment #23
sam711 commentedIf 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.
Comment #24
smustgrave commentedSorry for the delay but @sam711 think regardless we will need an upgrade path
Comment #25
smustgrave commentedCould this land in 12? @catch or @alexpott?
Comment #26
needs-review-queue-bot commentedThe 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.