Skip to content

Fix label updates being dropped for the first IP in the allow list - #419

Merged
peterwilsoncc merged 11 commits into
10up:developfrom
thisismyurl:fix/append-ips-falsy-zero-index
Jul 2, 2026
Merged

peterwilsoncc merged 11 commits into
10up:developfrom
thisismyurl:fix/append-ips-falsy-zero-index

Conversation

@thisismyurl

@thisismyurl thisismyurl commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Description of the Change

`append_ips()` looks up an IP address in the allow list with `array_search( $ip_address, $allowed_ips, true )`, which returns an integer index on a hit or `false` when the IP is not found. For the IP stored at index `0`, `array_search()` returns `0`. The update guard reads `if ( $found_index && ... )` — `0` is falsy in PHP, so the branch is never entered for the first IP in the list. The `elseif ( false === $found_index )` also skips it, since `0 !== false` under strict comparison. The label change falls through both branches and is silently discarded.

Changing the guard to `if ( false !== $found_index && ... )` explicitly tests for not-found, which is the idiomatic PHP idiom for `array_search()` when index `0` is a valid result. The `elseif` branch is unchanged — it still owns the not-found path.

There is only one `array_search()` call in the entire file; no other location has the same pattern.

Three new tests cover: (1) label update for index-0 IP with a precondition assertion that the IP is at index 0 before the call; (2) label update for the sole IP in a single-element list; (3) append-when-not-in-list to confirm the not-found path is unaffected.

Tests reviewed by inspection; not executed locally (no integration harness in my environment) — relying on CI for the first run.

Closes #418

(full disclosure: AI helped me identify the issue and verify my work)

How to test the Change

  1. Add two IP addresses with labels to the allow list:
    • Set the first entry to 10.5.6.1 with the label old label
    • click Add my current IP address and give it the label current IP
    • Save settings
  2. In WP-CLI run wp eval "Restricted_Site_Access::append_ips( array( '10.5.6.1' => 'new label' ) );"
  3. Reload the settings page
  4. Ensure the new label is applied to the first entry

Changelog Entry

Fixed - Allow programatic changes to first allow-listed IP address label.

Credits

Props @thisismyurl

Checklist:

array_search() returns 0 (not false) when the IP is the first element of the
allowlist. The previous guard if ( $found_index && ... ) treats 0 as falsy,
silently dropping label updates for any IP at index 0. The elseif branch also
misses it because 0 !== false under strict comparison.

Changing the guard to false !== $found_index correctly distinguishes
"not found" from "found at index zero".
@jeffpaul jeffpaul added this to the 7.7.0 milestone Jun 24, 2026
@jeffpaul
jeffpaul requested a review from peterwilsoncc June 24, 2026 15:20
@peterwilsoncc

Copy link
Copy Markdown
Collaborator

@thisismyurl thank you for the pull request.

I've taken the liberty of reformatting your PR description using the repository template. Are you able to review the test details I've provided to ensure you are happy with them.

We also require contributors to review and agree to the repos code of conduct, are you able to read through them and check the checkbox "I agree to follow this project's Code of Conduct." in the PR's description if you are happy with them.

Once I hear back, we'll be able to merge the pull request.

I'll do a review in the meantime.

@peterwilsoncc peterwilsoncc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for including unit tests, it's greatly appreciated.

I've added a few minor notes inline for the test suite. The source code changes look good and test well.

I think this will be good to merge pending my COC comment above.

Comment thread tests/php/test-ip-management.php Outdated
Comment thread tests/php/test-ip-management.php
Comment thread CHANGELOG.md Outdated
@thisismyurl

Copy link
Copy Markdown
Contributor Author

Thanks for the kind words and for reformatting the PR description — the test details look accurate to me. I've checked the Code of Conduct box. The Validate check failure appears to be the repo automation hitting a permission error when trying to add labels/assignees, not a code issue — all the PHP, Cypress, and compatibility tests are green. Happy to rebase onto the latest base branch if that would make merging smoother.

Copilot AI review requested due to automatic review settings July 2, 2026 03:16
peterwilsoncc and others added 3 commits July 2, 2026 13:17
Co-authored-by: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a bug in Restricted_Site_Access::append_ips() where label updates were silently skipped when the target IP existed at index 0 in the allow list (due to PHP’s falsy 0 from array_search()).

Changes:

  • Update the append_ips() guard to treat index 0 as a valid “found” result (false !== $found_index).
  • Add PHPUnit coverage for updating labels at index 0, in a single-element allow list, and for appending a new IP.
  • Document the fix in both readme.txt and CHANGELOG.md.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
restricted_site_access.php Fixes the array_search() index-0 falsy guard so label updates are applied correctly.
tests/php/test-ip-management.php Adds regression tests covering index-0 label updates and ensuring the not-found append path still works.
readme.txt Adds an Unreleased changelog entry for the fix.
CHANGELOG.md Adds an Unreleased “Fixed” entry for the fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/php/test-ip-management.php
Comment thread tests/php/test-ip-management.php Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

@peterwilsoncc peterwilsoncc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me and is testing well.

I took the liberty of pushing some of the changes I suggested to this branch prior to merge.

@peterwilsoncc
peterwilsoncc merged commit 15c0686 into 10up:develop Jul 2, 2026
24 checks passed
@peterwilsoncc peterwilsoncc mentioned this pull request Aug 25, 2026
17 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

append_ips() drops label updates for the IP at index 0 of the allow list

4 participants