Page MenuHomePhorge

No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None
diff --git a/includes/block/BlockTargetFactory.php b/includes/block/BlockTargetFactory.php
index 30af23fa6e1..d4ca942c2f2 100644
--- a/includes/block/BlockTargetFactory.php
+++ b/includes/block/BlockTargetFactory.php
@@ -112,6 +112,19 @@ class BlockTargetFactory implements WikiAwareEntity {
return null;
}
+ /**
+ * Try to create a block target from a single IP address
+ *
+ * @param string $ip
+ * @return AnonIpBlockTarget|null
+ */
+ public function newFromIp( string $ip ): ?AnonIpBlockTarget {
+ if ( IPUtils::isValid( $ip ) ) {
+ return new AnonIpBlockTarget( IPUtils::sanitizeIP( $ip ), $this->wikiId );
+ }
+ return null;
+ }
+
/**
* Create a BlockTarget from a UserIdentity, which may refer to a
* registered user, an IP address or range.
@@ -128,7 +141,7 @@ class BlockTargetFactory implements WikiAwareEntity {
} elseif ( IPUtils::isValidRange( $name ) ) {
return $this->newRangeBlockTarget( IPUtils::sanitizeRange( $name ) );
} elseif ( IPUtils::isValid( $name ) ) {
- return $this->newAnonIpBlockTarget( $name );
+ return $this->newAnonIpBlockTarget( IPUtils::sanitizeIP( $name ) );
} else {
return new UserBlockTarget( $user );
}
@@ -144,7 +157,7 @@ class BlockTargetFactory implements WikiAwareEntity {
public function newFromLegacyUnion( $union ): ?BlockTarget {
if ( $union instanceof UserIdentity ) {
if ( IPUtils::isValid( $union->getName() ) ) {
- return new AnonIpBlockTarget( $union->getName(), $this->wikiId );
+ return new AnonIpBlockTarget( IPUtils::sanitizeIP( $union->getName() ), $this->wikiId );
} else {
return new UserBlockTarget( $union );
}
@@ -251,7 +264,8 @@ class BlockTargetFactory implements WikiAwareEntity {
/**
* Create an IP block target
*
- * A simple constructor proxy for pre-validated input.
+ * A simple constructor proxy for pre-validated input. Use newFromIP() to
+ * apply normalization, for example stripping leading zeroes.
*
* @param string $ip
* @return AnonIpBlockTarget
diff --git a/includes/block/DatabaseBlockStore.php b/includes/block/DatabaseBlockStore.php
index 91568f2d7a3..cc037ba5268 100644
--- a/includes/block/DatabaseBlockStore.php
+++ b/includes/block/DatabaseBlockStore.php
@@ -1525,11 +1525,11 @@ class DatabaseBlockStore {
}
$parentBlock->assertWiki( $this->wikiId );
- if ( !IPUtils::isValid( $autoblockIP ) ) {
+ $target = $this->blockTargetFactory->newFromIp( $autoblockIP );
+ if ( !$target ) {
$this->logger->debug( "Invalid autoblock IP" );
return false;
}
- $target = $this->blockTargetFactory->newAnonIpBlockTarget( $autoblockIP );
// Check if autoblock exempt.
if ( $this->autoblockExemptionList->isExempt( $autoblockIP ) ) {
diff --git a/tests/phpunit/integration/includes/block/BlockTargetFactoryTest.php b/tests/phpunit/integration/includes/block/BlockTargetFactoryTest.php
index 6eded3e7625..2224feb0c42 100644
--- a/tests/phpunit/integration/includes/block/BlockTargetFactoryTest.php
+++ b/tests/phpunit/integration/includes/block/BlockTargetFactoryTest.php
@@ -112,13 +112,38 @@ class BlockTargetFactoryTest extends \MediaWikiIntegrationTestCase {
$status->getMessages()[0]->getParams()[0]->dump() );
}
+ public static function provideNewFromIp() {
+ return [
+ [ '', null ],
+ [ '127.0.0.1', '127.0.0.1' ],
+ [ '::0', '0:0:0:0:0:0:0:0' ],
+ [ '127.0.0.001', '127.0.0.1' ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideNewFromIp
+ * @param string $input
+ * @param string|null $expected
+ */
+ public function testNewFromIp( $input, $expected ) {
+ $result = $this->getBlockTargetFactory()->newFromIp( $input );
+ if ( $expected === null ) {
+ $this->assertNull( $result );
+ } else {
+ $this->assertInstanceOf( AnonIpBlockTarget::class, $result );
+ $this->assertSame( $expected, $result->toString() );
+ }
+ }
+
public static function provideNewFromUser() {
return [
- [ 1, 'Alice', UserBlockTarget::class ],
- [ 5, 'Exists', UserBlockTarget::class ],
- [ 0, 'Nonexistent', UserBlockTarget::class ],
- [ 0, '127.0.0.1', AnonIpBlockTarget::class ],
- [ 0, '1.2.3.0/24', RangeBlockTarget::class ],
+ [ 1, 'Alice', 'Alice', UserBlockTarget::class ],
+ [ 5, 'Exists', 'Exists', UserBlockTarget::class ],
+ [ 0, 'Nonexistent', 'Nonexistent', UserBlockTarget::class ],
+ [ 0, '127.0.0.1', '127.0.0.1', AnonIpBlockTarget::class ],
+ [ 0, '127.0.0.001', '127.0.0.1', AnonIpBlockTarget::class ],
+ [ 0, '1.2.3.0/24', '1.2.3.0/24', RangeBlockTarget::class ],
];
}
@@ -126,13 +151,14 @@ class BlockTargetFactoryTest extends \MediaWikiIntegrationTestCase {
* @dataProvider provideNewFromUser
* @param int $id
* @param string $name
+ * @param string|null $expectedName
* @param string $class
*/
- public function testNewFromUser( $id, $name, $class ) {
+ public function testNewFromUser( $id, $name, $expectedName, $class ) {
$user = new UserIdentityValue( $id, $name );
$target = $this->getBlockTargetFactory()->newFromUser( $user );
$this->assertInstanceOf( $class, $target );
- $this->assertSame( $name, $target->toString() );
+ $this->assertSame( $expectedName, $target->toString() );
}
public static function provideNewFromRow() {

File Metadata

Mime Type
text/x-diff
Expires
Sat, Jul 5, 5:31 AM (11 h, 48 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
227434
Default Alt Text
(5 KB)

Event Timeline