AdvancedCustomFields / AdvancedCustomFields/acf

potential infinite loop in acf_get_field_ancestors()

Open
#898 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
945
Forks
197
PR merge metrics
No merged PRs in 30d

Description

acf_get_field_ancestors($field) (acf-field-functions.php) can cause an infinite loop if the field setup is broken and includes a recursive reference.

To Reproduce
Set up a field as {"name": "my_field", "parent": "my_field"}
call acf_get_field_ancestors($field)

Expected behavior
Returns the fields found until recursion is encountered (or throws an exception upon recursion).

// Loop over parents.
while ( $field['parent'] && $field = acf_get_field( $field['parent'] ) ) {
        $ancestors[] = $field['ID'] ? $field['ID'] : $field['key'];
} 

This part would need a check whether the ID/key is already included, i.e.

// Loop over parents.
while ( $field['parent'] && $field = acf_get_field( $field['parent'] ) ) {
    $idOrKey = $field['ID'] ? $field['ID'] : $field['key'];
    if(in_array($idOrKey, $ancestors, true)) {
        break;
    }
    $ancestors[] = $idOrKey;
} 

Version Information:

  • WP Version 6.4.3
  • PHP 8.0.18
  • ACF PRO 6.2.5 (problem still exists in current version)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in acf-field-functions.php at acf_get_field_ancestors($field), then reproduce the issue with a field whose parent points to itself, such as {"name":"my_field","parent":"my_field"}. Done means recursive references no longer loop indefinitely and the function either returns the fields found before recursion or throws an exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.