Problem/Motivation

When using an autocompletion element in an entity reference field widget, there is a feature to create entities automatically, in case the typed in labels of the entity aren't found. This is most commonly used to allow users to create new taxonomy terms if no matching ones are found.

While this makes sense for terms, it's a bit awkward for other entity types that can be created using only a label. Since many sites likely rely on this behavior, the fix should take into account existing field widgets and not require sites to grand wide sweeping permissions to what are now "low" or even anonymous uers.

Steps to reproduce

Give a user access to an autocomplete entity reference field widget that references an entity type / bundle they do not have access to, and make sure to enable the autocreate feature. They should be able to create entities automatically by using the widget since the "create" access check isn't being performed.

Proposed resolution

Perform the "create" access check before creating new entities during entity reference autocomplete, without breaking existing sites.

Remaining tasks

Discuss impact on existing sites, write a patch.

User interface changes

"Respect create access, only users with the necessary permission can create new entities" checkbox appears on the field's Selection handler settings when "Create referenced entities if they don't already exist" is enabled. Enabled by default on new fields; left off on upgrades.

API changes

  • \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection: new configuration key auto_create_check_access (default TRUE) returned by defaultConfiguration() and surfaced in buildConfigurationForm().
  • DefaultSelection::createNewEntity() now throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException when auto_create_check_access is TRUE and the current user lacks create access on the target bundle. The access check in the entity_reference_autocomplete widget was already client-side; this closes the path for callers that bypass the widget.

Data model changes

  • New boolean auto_create_check_access key in handler_settings of entity_reference fields using the default selection handler (config schema: field.plugin.entity_reference_selection.default).
  • system_post_update_set_auto_create_check_access() sets the key to FALSE on existing fields that already have auto_create but no stored auto_create_check_access, preserving current runtime behavior on upgrade.

Release notes snippet

Entity reference fields with auto-create now check that the current user has create permission on the referenced bundle before autocreating. The new auto_create_check_access setting (per field, default TRUE) controls this. Upgraded sites keep their existing behavior because system_post_update_set_auto_create_check_access() sets the flag to FALSE on fields that already had auto-create enabled; site admins can opt in per field from the field's Selection handler settings.

Issue fork drupal-3372919

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

samuel.mortenson created an issue. See original summary.

berdir’s picture

> While this makes sense for terms, it's a bit awkward for other entity types that can be created using only a label.

I think that's neither an issue nor relevant to this. Allowing auto-create is already an option on the field, if you don't need it or it doesn't make sense for the given entity type, then the setting can just be not enabled, it's off by default.

That's why I'm unsure what to do about this and if it's even really a bug as it is. A bug would require a "fix", which would definitely cause issues on many sites.

The only idea I have atm is to make it a feature, with a "[ ] Respect create access, only users with the necessary permission can create new entities", then have that off on existing fields and on by default on new ones?

berdir’s picture

Also, should this have a security tag?

catch’s picture

The only idea I have atm is to make it a feature, with a "[ ] Respect create access, only users with the necessary permission can create new entities", then have that off on existing fields and on by default on new ones?

This also seems like the only way to handle both cases, but I can't really think of a use-case where you'd want this particular UX. You could have curated entity refs with autocomplete (which we already allow for), but then would you want admins using the autocomplete UI to create new ones? And if you really needed that, a custom/contrib widget might be an option rather than adding clutter to the UI here.

Another thing would be adding to the description of the existing setting to note that it doesn't require 'create' entity access - i.e. just document the existing behaviour a bit more explicitly.

james.williams’s picture

I've just created https://www.drupal.org/project/autocreate_access to fill this gap, so that site admins can configure access for different roles, so that some can create referenced items on the fly, whilst others cannot.

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.

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

penyaskito’s picture

Status: Active » Needs review
Issue tags: +AI-accelerated

Created MR. Separated concerns in 4 commits:

  • e3ecc7c844e Auto-create access checks. This is extra-protective. Checks permissions on both the owner of the entity + the current user, which might be not expected. We might want to relax and don't check the owner of the entity?
  • 0d5dfec250d Include the flag in shipped config, defaulting to true. This is a must, assuming we want to avoid an optional flag for this.
  • ff758a4e945 Upgrade path + upgrade path tests. This is a must, assuming we want to avoid an optional flag for this.
  • 510dddca75 Extra hardening: this check is also added to \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::createNewEntity in case some child selection plugin is calling it directly. Which apparently even core does, as I had to amend some tests.

Opening my own threads on the MR to highlight these.

Disclaimer: Used AI (Claude:claude-opus-4.7). It initially implemented the checks for the current user, but I assumed we wanted the assigned owner too. The shipped config it's all LLM-generated, but I verified it. Same for upgrade path + tests, which I had to clean up but was technically correct. Extra hardening it's completely mine, as I assumed that if we want to protect on both of the owner and current user access, we would need to be extra-cautious here too.

penyaskito’s picture

Assigned: Unassigned » penyaskito
Status: Needs review » Needs work

Test feedback + @berdir feedback agree on not needing to check access for the owner, just the current user.

penyaskito’s picture

Issue summary: View changes
Status: Needs work » Needs review

Checking access for the current user (deliberately not checking create access for the owner of the newly created entity)

Updated IS.

penyaskito’s picture

Assigned: penyaskito » Unassigned
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record

This one appears to need a rebase and most likely would need a CR please

Thanks.

penyaskito’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record

Rebased + draft change record.

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.