Debian Codegraph @

extensions/AbuseFilter/includes/ProtectedVarsAccessLogger.php

raw symbols SCIP
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 * Defines the API for the component responsible for logging the following interactions:
17 *
18 * - A user enables protected variable viewing
19 * - A user disables protected variable viewing
20 */
21 class ProtectedVarsAccessLogger {
22 /**
23 * Represents a user enabling their own access to view protected variables
24 *
25 * @var string
26 */
27 public const ACTION_CHANGE_ACCESS_ENABLED = 'change-access-enable';
28
29 /**
30 * Represents a user disabling their own access to view protected variables
31 *
32 * @var string
33 */
34 public const ACTION_CHANGE_ACCESS_DISABLED = 'change-access-disable';
35
36 /**
37 * Represents a user viewing the value of a protected variable
38 *
39 * @var string
40 */
41 public const ACTION_VIEW_PROTECTED_VARIABLE_VALUE = 'view-protected-var-value';
42
43 /**
44 * @var string
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 * @param LoggerInterface $logger
55 * @param IConnectionProvider $lbFactory
56 * @param ActorStore $actorStore
57 * @param int $delay The number of seconds after which a duplicate log entry can be
58 * created for a debounced log
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 * Log when the user enables their own access
76 *
77 * @param UserIdentity $performer
78 */
79 public function logAccessEnabled( UserIdentity $performer ): void {
80 $this->log( $performer, $performer->getName(), self::ACTION_CHANGE_ACCESS_ENABLED, false );
81 }
82
83 /**
84 * Log when the user disables their own access
85 *
86 * @param UserIdentity $performer
87 */
88 public function logAccessDisabled( UserIdentity $performer ): void {
89 $this->log( $performer, $performer->getName(), self::ACTION_CHANGE_ACCESS_DISABLED, false );
90 }
91
92 /**
93 * Log when the user views the values of protected variables
94 *
95 * @param UserIdentity $performer
96 * @param string $target
97 * @param int|null $timestamp
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 * @param UserIdentity $performer
118 * @param string $target
119 * @param string $action
120 * @param bool $shouldDebounce
121 * @param int|null $timestamp
122 * @param array|null $params
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 // Log to CheckUser's temporary accounts log if CU is installed
137 if ( MediaWikiServices::getInstance()->getExtensionRegistry()->isLoaded( 'CheckUser' ) ) {
138 // Add the extension name to the action so that CheckUser has a clearer
139 // reference to the source in the message key
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 // If the log is debounced, check against the logging table before logging
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 // If the log isn't debounced then it should always be logged
184 $shouldLog = true;
185 }
186
187 // Actually write to logging table
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 * @internal
212 *
213 * @param string $subtype
214 * @return ManualLogEntry
215 */
216 protected function createManualLogEntry( string $subtype ): ManualLogEntry {
217 return new ManualLogEntry( self::LOG_TYPE, $subtype );
218 }
219 }