Skip to content

Commit

Permalink
Merge pull request #49039 from nextcloud/jtr/fix-ipv6-zone-ids-link-l…
Browse files Browse the repository at this point in the history
…ocal
  • Loading branch information
skjnldsv authored Nov 6, 2024
2 parents 452e4be + e885e4f commit 3c42906
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/private/Net/IpAddressClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class IpAddressClassifier {
public function isLocalAddress(string $ip): bool {
$parsedIp = Factory::parseAddressString(
$ip,
ParseStringFlag::IPV4_MAYBE_NON_DECIMAL | ParseStringFlag::IPV4ADDRESS_MAYBE_NON_QUAD_DOTTED
ParseStringFlag::IPV4_MAYBE_NON_DECIMAL | ParseStringFlag::IPV4ADDRESS_MAYBE_NON_QUAD_DOTTED | ParseStringFlag::MAY_INCLUDE_ZONEID
);
if ($parsedIp === null) {
/* Not an IP */
Expand Down
5 changes: 3 additions & 2 deletions lib/private/Security/Ip/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use InvalidArgumentException;
use IPLib\Address\AddressInterface;
use IPLib\Factory;
use IPLib\ParseStringFlag;
use OCP\Security\Ip\IAddress;
use OCP\Security\Ip\IRange;

Expand All @@ -21,15 +22,15 @@ class Address implements IAddress {
private readonly AddressInterface $ip;

public function __construct(string $ip) {
$ip = Factory::parseAddressString($ip);
$ip = Factory::parseAddressString($ip, ParseStringFlag::MAY_INCLUDE_ZONEID);
if ($ip === null) {
throw new InvalidArgumentException('Given IP address can’t be parsed');
}
$this->ip = $ip;
}

public static function isValid(string $ip): bool {
return Factory::parseAddressString($ip) !== null;
return Factory::parseAddressString($ip, ParseStringFlag::MAY_INCLUDE_ZONEID) !== null;
}

public function matches(IRange ... $ranges): bool {
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Security/Ip/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use InvalidArgumentException;
use IPLib\Factory;
use IPLib\ParseStringFlag;
use IPLib\Range\RangeInterface;
use OCP\Security\Ip\IAddress;
use OCP\Security\Ip\IRange;
Expand All @@ -30,7 +31,7 @@ public static function isValid(string $range): bool {
}

public function contains(IAddress $address): bool {
return $this->range->contains(Factory::parseAddressString((string)$address));
return $this->range->contains(Factory::parseAddressString((string)$address, ParseStringFlag::MAY_INCLUDE_ZONEID));
}

public function __toString(): string {
Expand Down
1 change: 1 addition & 0 deletions tests/lib/Net/IpAddressClassifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function localIpAddressData(): array {
return [
['192.168.0.1'],
['fe80::200:5aee:feaa:20a2'],
['fe80::1fc4:15d8:78db:2319%enp4s0'], // v6 zone ID
['0:0:0:0:0:ffff:10.0.0.1'],
['0:0:0:0:0:ffff:127.0.0.0'],
['10.0.0.1'],
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/Security/Ip/RemoteAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function dataProvider(): array {
// No configuration
['1.2.3.4', false, true],
['1234:4567:8910::', false, true],
// v6 Zone ID
['fe80::1fc4:15d8:78db:2319%enp4s0', false, true],
// Empty configuration
['1.2.3.4', [], true],
['1234:4567:8910::', [], true],
Expand Down

0 comments on commit 3c42906

Please sign in to comment.