| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter; |
| 4 |
|
| 5 |
use ManualLogEntry; |
| 6 |
use MediaWiki\MediaWikiServices; |
| 7 |
use MediaWiki\Title\Title; |
| 8 |
use MediaWiki\User\ActorStore; |
| 9 |
use MediaWiki\User\UserIdentity; |
| 10 |
use Psr\Log\LoggerInterface; |
| 11 |
use Wikimedia\Assert\Assert; |
| 12 |
use Wikimedia\Rdbms\DBError; |
| 13 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class ProtectedVarsAccessLogger { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public const ACTION_CHANGE_ACCESS_ENABLED = 'change-access-enable'; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public const ACTION_CHANGE_ACCESS_DISABLED = 'change-access-disable'; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public const ACTION_VIEW_PROTECTED_VARIABLE_VALUE = 'view-protected-var-value'; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public const LOG_TYPE = 'abusefilter-protected-vars'; |
| 47 |
|
| 48 |
private LoggerInterface $logger; |
| 49 |
private IConnectionProvider $lbFactory; |
| 50 |
private ActorStore $actorStore; |
| 51 |
private int $delay; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function __construct( |
| 61 |
LoggerInterface $logger, |
| 62 |
IConnectionProvider $lbFactory, |
| 63 |
ActorStore $actorStore, |
| 64 |
int $delay |
| 65 |
) { |
| 66 |
Assert::parameter( $delay > 0, 'delay', 'delay must be positive' ); |
| 67 |
|
| 68 |
$this->logger = $logger; |
| 69 |
$this->lbFactory = $lbFactory; |
| 70 |
$this->actorStore = $actorStore; |
| 71 |
$this->delay = $delay; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function logAccessEnabled( UserIdentity $performer ): void { |
| 80 |
$this->log( $performer, $performer->getName(), self::ACTION_CHANGE_ACCESS_ENABLED, false ); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function logAccessDisabled( UserIdentity $performer ): void { |
| 89 |
$this->log( $performer, $performer->getName(), self::ACTION_CHANGE_ACCESS_DISABLED, false ); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function logViewProtectedVariableValue( |
| 100 |
UserIdentity $performer, |
| 101 |
string $target, |
| 102 |
?int $timestamp = null |
| 103 |
): void { |
| 104 |
if ( !$timestamp ) { |
| 105 |
$timestamp = (int)wfTimestamp(); |
| 106 |
} |
| 107 |
$this->log( |
| 108 |
$performer, |
| 109 |
$target, |
| 110 |
self::ACTION_VIEW_PROTECTED_VARIABLE_VALUE, |
| 111 |
true, |
| 112 |
$timestamp |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
private function log( |
| 125 |
UserIdentity $performer, |
| 126 |
string $target, |
| 127 |
string $action, |
| 128 |
bool $shouldDebounce, |
| 129 |
?int $timestamp = null, |
| 130 |
?array $params = [] |
| 131 |
): void { |
| 132 |
if ( !$timestamp ) { |
| 133 |
$timestamp = (int)wfTimestamp(); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
if ( MediaWikiServices::getInstance()->getExtensionRegistry()->isLoaded( 'CheckUser' ) ) { |
| 138 |
|
| 139 |
|
| 140 |
$action = 'af-' . $action; |
| 141 |
|
| 142 |
$logger = MediaWikiServices::getInstance() |
| 143 |
->getService( 'CheckUserTemporaryAccountLoggerFactory' ) |
| 144 |
->getLogger(); |
| 145 |
$logger->logFromExternal( |
| 146 |
$performer, |
| 147 |
$target, |
| 148 |
$action, |
| 149 |
$params, |
| 150 |
$shouldDebounce, |
| 151 |
$timestamp |
| 152 |
); |
| 153 |
} else { |
| 154 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 155 |
$shouldLog = false; |
| 156 |
|
| 157 |
|
| 158 |
if ( $shouldDebounce ) { |
| 159 |
$timestampMinusDelay = $timestamp - $this->delay; |
| 160 |
$actorId = $this->actorStore->findActorId( $performer, $dbw ); |
| 161 |
if ( !$actorId ) { |
| 162 |
$shouldLog = true; |
| 163 |
} else { |
| 164 |
$logline = $dbw->newSelectQueryBuilder() |
| 165 |
->select( '*' ) |
| 166 |
->from( 'logging' ) |
| 167 |
->where( [ |
| 168 |
'log_type' => self::LOG_TYPE, |
| 169 |
'log_action' => $action, |
| 170 |
'log_actor' => $actorId, |
| 171 |
'log_namespace' => NS_USER, |
| 172 |
'log_title' => $target, |
| 173 |
$dbw->expr( 'log_timestamp', '>', $dbw->timestamp( $timestampMinusDelay ) ), |
| 174 |
] ) |
| 175 |
->caller( __METHOD__ ) |
| 176 |
->fetchRow(); |
| 177 |
|
| 178 |
if ( !$logline ) { |
| 179 |
$shouldLog = true; |
| 180 |
} |
| 181 |
} |
| 182 |
} else { |
| 183 |
|
| 184 |
$shouldLog = true; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
if ( $shouldLog ) { |
| 189 |
$logEntry = $this->createManualLogEntry( $action ); |
| 190 |
$logEntry->setPerformer( $performer ); |
| 191 |
$logEntry->setTarget( Title::makeTitle( NS_USER, $target ) ); |
| 192 |
$logEntry->setParameters( $params ); |
| 193 |
$logEntry->setTimestamp( wfTimestamp( TS_MW, $timestamp ) ); |
| 194 |
|
| 195 |
try { |
| 196 |
$logEntry->insert( $dbw ); |
| 197 |
} catch ( DBError $e ) { |
| 198 |
$this->logger->critical( |
| 199 |
'AbuseFilter proctected variable log entry was not recorded. ' . |
| 200 |
'This means access to IPs can occur without being auditable. ' . |
| 201 |
'Immediate fix required.' |
| 202 |
); |
| 203 |
|
| 204 |
throw $e; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
protected function createManualLogEntry( string $subtype ): ManualLogEntry { |
| 217 |
return new ManualLogEntry( self::LOG_TYPE, $subtype ); |
| 218 |
} |
| 219 |
} |