Problem/Motivation

When using a entity reference and the user uses a large integer value or other wrong values, then PostgreSQL will throw an exception.

Steps to reproduce

Enter "example (test)" value to any entity autocomplete field.

The following exception will be thrown.

The website encountered an unexpected error. Please try again later.

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type bigint: "test" LINE 5: ...('tags')) AND ("taxonomy_term_field_data"."tid" IN ('test')) ^: SELECT "base_table"."revision_id" AS "revision_id", "base_table"."tid" AS "tid" FROM "taxonomy_term_data" "base_table" INNER JOIN "taxonomy_term_field_data" "taxonomy_term_field_data" ON "taxonomy_term_field_data"."tid" = "base_table"."tid" WHERE ("taxonomy_term_field_data"."vid" IN (:db_condition_placeholder_0)) AND ("taxonomy_term_field_data"."tid" IN (:db_condition_placeholder_1)); Array ( [:db_condition_placeholder_0] => tags [:db_condition_placeholder_1] => test ) in Drupal\Core\Entity\Query\Sql\Query->result() (line 273 of core/lib/Drupal/Core/Entity/Query/Sql/Query.php).

Details

Drupal 10.1.x, PHP 8.2, PostgreSQL 15

Proposed resolution

Sanitize the user input for integer fields with entity reference selectors

Remaining tasks

TBD

User interface changes

None

Introduced terminology

None

API changes

None

Data model changes

None

Release notes snippet

None

For the committer

The changes to the .gitlab-ci.yml file need to be removed before merging!

Issue fork drupal-3372068

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:

Comments

Chi created an issue. See original summary.

chi’s picture

Status: Active » Needs review
StatusFileSize
new1.37 KB

Status: Needs review » Needs work

The last submitted patch, 2: postgres-invalid-input-syntax_3372068_2.patch, failed testing. View results

chi’s picture

The alternative solution could be casting field entity ID to text in SQL query.

joseph.olstad’s picture

Version: 10.1.x-dev » 11.x-dev
Status: Needs work » Active

Hello @Chi, thank you for this fix, it works for me also. No longer a WSOD running psql with this situation .
I'm not quite sure on steps to reproduce in my case so I'll leave yours as it seems easier to reproduce.
This works on both psql 12 and psql 13

Fix applies to D9.5.x also.

niklan’s picture

Patch for Drupal 10.3+

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

liam morland’s picture

The patch in #6 would stop the error messages, but may end up hiding an underlying issue with entities that have non-integer IDs.

liam morland’s picture

Issue tags: +PostgreSQL

ishani patel made their first commit to this issue’s fork.

daffie made their first commit to this issue’s fork.

daffie’s picture

Added fix by filtering out wrong values for integer fields.

daffie’s picture

Issue summary: View changes
Status: Active » Needs review

I have updated the CR.

Disclosure: I have used AI for the PR.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new98 bytes

The Needs Review Queue Bot tested this issue. The merge request has merge conflicts and cannot be merged. 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.

daffie’s picture

Status: Needs work » Needs review

Rebased the PR.
CI Pipeline is green again.
Back to NR.

joseph.olstad’s picture

Status: Needs review » Reviewed & tested by the community
  1. solution is needed
  2. has tests
  3. passes tests

longwave made their first commit to this issue’s fork.

longwave’s picture

Status: Reviewed & tested by the community » Needs review

Not sure this is the best place for this method, this feels like it could be useful for more subsystems and either at the field or database level.

joseph.olstad’s picture

joseph.olstad’s picture

Not sure if the fails are related to the changes. @longwave, are you able to have a look?

joseph.olstad’s picture

ok so the new branch I created wasn't using the latest head of main. kind of surprised by that but ya thanks @lkmorlan