Repository navigation
Fix label updates being dropped for the first IP in the allow list - #419
Conversation
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".
|
@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
left a comment
There was a problem hiding this comment.
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.
|
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 |
There was a problem hiding this comment.
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 index0as 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.txtandCHANGELOG.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.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
peterwilsoncc
left a comment
There was a problem hiding this comment.
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.
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
10.5.6.1with the labelold labelcurrent IPwp eval "Restricted_Site_Access::append_ips( array( '10.5.6.1' => 'new label' ) );"Changelog Entry
Credits
Props @thisismyurl
Checklist: