Repository navigation
Fix #82: IpHelper validators accept an address with a trailing newline - #83
Conversation
isIpv4(), isIpv6() and isIp() matched against IPV4_REGEXP, IPV6_REGEXP and IP_REGEXP, which end in `$` without the D modifier. In PHP `$` also matches before a final newline, so "1.2.3.4\n" and "::1\n" were valid IPs, while inRange() / IpRanges::isAllowed() then failed on the same value (InvalidArgumentException for IPv4, a TypeError/AssertionError from ip2bin() for IPv6). getIpVersion() had the same gap in its IPv6 regexp fallback. Use the regexps with the D modifier in these four places. The public *_REGEXP constants are unchanged, as they are documented as JavaScript compatible and JavaScript has no D flag. Tests: trailing-newline cases for isIpv4(), isIpv6(), isIp() and getIpVersion() with validation. Signed-off-by: Iefken <[email protected]>
Reviewer's GuideAPIIntroduce engine-specific public regexp constants, deprecate the legacy constants without changing their values, and document the PHP-versus-JavaScript distinction.
ImplementationUse the strict PCRE regexps in the IP validators and IPv6 fallback, and reject trailing newlines when parsing CIDR input.
DocumentationRecord the validator fix and the new regexp constants in the 1.2.3 changelog.
TestsCover trailing-newline rejection in validators and parsing, test the PCRE and ECMA regexp constants, and preserve legacy constant values.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #83 +/- ##
=========================================
Coverage 96.82% 96.82%
Complexity 54 54
=========================================
Files 3 3
Lines 126 126
=========================================
Hits 122 122
Misses 4 4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/IpHelper.php" line_range="62" />
<code_context>
+ /**
+ * The regexps above with the `D` modifier, used by the validation methods. Without it, PHP's `$` also matches
+ * before a final newline, so `"1.2.3.4\n"` would pass. The public constants stay JavaScript compatible (no `D`).
+ */
+ private const IPV4_REGEXP_STRICT = self::IPV4_REGEXP . 'D';
</code_context>
<issue_to_address>
**Public regex accepts trailing newlines**
When a consumer validates newline-terminated input directly with one of the public regex constants, pHP `preg_match()` with the public `IPV4_REGEXP`, `IPV6_REGEXP`, or `IP_REGEXP` still treats a final newline as matching the end of the address, so consumers using these exported validators accept inputs the helper methods now reject.
Use an end-of-input assertion compatible with the documented PHP/JavaScript use in the public regex constants, and add trailing-newline tests for them.
</issue_to_address>Sourcery assessment
Approval pending. 1 finding to address first.
Blocking findings: src/IpHelper.php:62
As requested in review: make the engine distinction explicit in the
public API instead of private *_STRICT constants.
- Add PCRE_IPV4_REGEXP, PCRE_IPV6_REGEXP and PCRE_IP_REGEXP ('/^...$/D',
strict end of string) for PHP's preg_*() functions.
- Add ECMA_IPV4_REGEXP, ECMA_IPV6_REGEXP and ECMA_IP_REGEXP ('/^...$/')
for JavaScript; their docblocks say they are not strict in PHP.
- Deprecate IPV4_REGEXP, IPV6_REGEXP and IP_REGEXP; their values do not
change.
- isIpv4(), isIpv6(), isIp() and the IPv6 fallback in getIpVersion() use
the PCRE_* constants.
- getCidrBits(): add the D modifier, so "127.0.0.1\n" and
"127.0.0.1/24\n" are rejected instead of returning 32 and 24.
Tests: trailing newline, CRLF and leading newline rows for the PCRE_*
constants; ECMA_* with the regular valid/invalid rows; trailing-newline
rows for getCidrBits(); the deprecated constants keep their values.
Signed-off-by: Iefken <[email protected]>
|
Hi @vjik, thanks for the review! Reworked it the way you described, in a second commit on top of the first one:
This should also cover the Sourcery note: the strict 'PCRE_' constants are the PHP ones now, and the 'ECMA_' ones stay without 'D' on purpose. Locally (PHP 8.3, offline container): phpunit all green except the three DnsHelper tests that need network, phpstan, php-cs-fixer, rector and composer-dependency-analyser clean. Not run locally: the PHP 7.4 to 8.6 matrix, the mutation job and roave BC check, so those are up to CI. Review proposed by Claude, checked and sent by Ief (GitHub: iefken). |
isIpv4(),isIpv6(),isIp()and the IPv6 fallback ingetIpVersion()matched with regexpsending in
$withoutD, so"1.2.3.4\n"was a valid IP whileinRange()/IpRanges::isAllowed()threw on it.
src/IpHelper.php: private*_REGEXP_STRICTconstants (self::*_REGEXP . 'D') used in those fourplaces; the public constants are unchanged (documented as JavaScript compatible).
tests/IpHelperTest.php: trailing-newline cases forisIpv4(),isIpv6(),isIp()andgetIpVersion()with validation.CHANGELOG.md:Bug #82line under 1.2.3.Tests run locally on
master@ d7ed96f (PHP 8.3.37, PHPUnit 9.6):Tests: 157, Assertions: 160, Errors: 3, Failures: 5.- the 5 failures are the new trailing-newline rows (isIpv4, isIpv6, isIp x2:Failed asserting that true is identical to false; getIpVersion IPv6:Failed asserting that exception of type "InvalidArgumentException" is thrown)Tests: 157, Assertions: 160, Errors: 3.- 0 failures; the 3 errors areDnsHelperTest::testMx/testA/testAcceptsEmail(dns_get_record(): A temporary server error), which need network and error identically without the fix (our test container has no network)phpstan.neon, level max):[OK] No errorsFound 0 of 6 files that can be fixed--dry-run:[OK] Rector is done!Not run locally: the PHP 7.4-8.6 x ubuntu/windows matrix (one PHP in our image), Infection,
Roave BC check, composer-dependency-analyser (no dependency changed). Private constants and string
concatenation in constant expressions work on PHP 7.4.
GPG-signed and DCO signed-off.
Issue found and fixed by Claude, reproduced issue in an isolated container, then reviewed and confirmed by Ief (GitHub: iefken).
Summary by Sourcery
Reject IP and CIDR values with trailing newlines and clarify which regular-expression constants to use for PHP and JavaScript.
New Features:
Bug Fixes:
Enhancements:
Tests:
Chores: