feat(content-gate): expose institutional IP allowlist endpoint#4685
Open
rbcorrales wants to merge 3 commits intotrunkfrom
Open
feat(content-gate): expose institutional IP allowlist endpoint#4685rbcorrales wants to merge 3 commits intotrunkfrom
rbcorrales wants to merge 3 commits intotrunkfrom
Conversation
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an admin-gated REST API endpoint to expose the institutional IPv4/CIDR allowlist so external systems can mirror it, and tightens CIDR parsing to prevent malformed prefixes from matching all IPs.
Changes:
- Added
GET /newspack/v1/institutional-access/ip-allowlist(gated bymanage_options) returning{ id, name, ip_ranges }entries and applyingnewspack_content_gate_ip_allowlistfilter. - Centralized IPv4/CIDR parsing via
parse_ip_ranges()and reused it for both allowlist output and matching. - Hardened CIDR validation to prevent non-numeric prefixes (e.g.
/foo,/) from being treated as/0.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
includes/content-gate/class-ip-access-rule.php |
Registers new allowlist route, implements allowlist callback + schema, and introduces stricter IP/CIDR parsing used by matching. |
tests/unit-tests/content-gate/class-ip-access-rule.php |
Adds unit tests for malformed CIDR behavior and comprehensive endpoint behavior/shape tests for the new allowlist route. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
All Submissions:
Changes proposed in this Pull Request:
Adds a
GET /newspack/v1/institutional-access/ip-allowlistREST endpoint, gated bymanage_options, that returns the institutional IP allowlist configured for the site. Designed for external systems to mirror the list locally and decide gating without hitting the origin per request.The endpoint exposes only IP rules. Email domain and reader data rules are not surfaced. Entries that fail IPv4 or CIDR validation are dropped before being returned. A
newspack_content_gate_ip_allowlistfilter is provided so other plugins can augment the list.This PR also includes a security fix for a bug in
IP_Access_Rule::ip_matches_ranges()(introduced in #4574): the(int) $bitscast silently coerced any value that wasn't numeric to0, causing typo'd entries like10.0.0.5/fooor10.0.0.5/to match every IP. Validation is now tightened withctype_digit()before the cast. Caused by admin input only, not externally exploitable, but operationally risky.Closes NPPM-2770.
How to test the changes in this Pull Request:
192.168.1.0/24,10.0.0.5) under Audience > Access Control > Institutions.curl -u admin:app_password https://your-site.com/wp-json/newspack/v1/institutional-access/ip-allowlist. Confirm the response is a JSON array of{ id, name, ip_ranges }entries, sorted byidascending.10.0.0.1,not-an-ip,10.0.0.5/foo,192.168.1.0/24,10.0.0.5/40). Confirm only the valid ones (10.0.0.1,192.168.1.0/24) appear./checkendpoint (POST and GET) still works unchanged.10.0.0.0/fooas its IP range. An unrelated visitor IP like203.0.113.5must NOT pass the institutional IP gate. Previously it would have, due to the loose validation.Other information: