-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMediaWikiSecurityCheckPlugin.php
318 lines (300 loc) · 11.5 KB
/
MediaWikiSecurityCheckPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/**
* Static analysis tool for MediaWiki extensions.
*
* To use, add this file to your phan plugins list.
*
* Copyright (C) 2017 Brian Wolff <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
use ast\Node;
use Phan\AST\UnionTypeVisitor;
use Phan\CodeBase;
use Phan\Exception\CodeBaseException;
use Phan\Language\Context;
use Phan\Language\Element\FunctionInterface;
use Phan\Language\Element\Method;
use Phan\Language\FQSEN\FullyQualifiedClassName;
use Phan\Language\Type\GenericArrayType;
use SecurityCheckPlugin\CausedByLines;
use SecurityCheckPlugin\FunctionTaintedness;
use SecurityCheckPlugin\MWPreVisitor;
use SecurityCheckPlugin\MWVisitor;
use SecurityCheckPlugin\SecurityCheckPlugin;
use SecurityCheckPlugin\Taintedness;
use SecurityCheckPlugin\TaintednessVisitor;
class MediaWikiSecurityCheckPlugin extends SecurityCheckPlugin {
/**
* @inheritDoc
*/
public static function getPostAnalyzeNodeVisitorClassName(): string {
return MWVisitor::class;
}
/**
* @inheritDoc
*/
public static function getPreAnalyzeNodeVisitorClassName(): string {
return MWPreVisitor::class;
}
/**
* @inheritDoc
*/
protected function getCustomFuncTaints(): array {
$shellCommandOutput = [
// This is a bit unclear. Most of the time
// you should probably be escaping the results
// of a shell command, but not all the time.
'overall' => self::YES_TAINT
];
$sqlExecTaint = new Taintedness( self::SQL_EXEC_TAINT );
$insertTaint = FunctionTaintedness::emptySingleton();
// table name
$insertTaint = $insertTaint->withParamSinkTaint( 0, $sqlExecTaint, self::NO_OVERRIDE );
// Insert values. The keys names are unsafe. The argument can be either a single row or an array of rows.
// Note, here we are assuming the single row case. The multiple rows case is handled in modifyParamSinkTaint.
$sqlExecKeysTaint = Taintedness::safeSingleton()->withAddedKeysTaintedness( self::SQL_EXEC_TAINT );
$insertTaint = $insertTaint->withParamSinkTaint( 1, $sqlExecKeysTaint, self::NO_OVERRIDE );
// method name
$insertTaint = $insertTaint->withParamSinkTaint( 2, $sqlExecTaint, self::NO_OVERRIDE );
// options. They are not escaped
$insertTaint = $insertTaint->withParamSinkTaint( 3, $sqlExecTaint, self::NO_OVERRIDE );
$insertQBRowTaint = FunctionTaintedness::emptySingleton();
$insertQBRowTaint = $insertQBRowTaint->withParamSinkTaint( 0, clone $sqlExecKeysTaint, self::NO_OVERRIDE );
$insertQBRowsTaint = FunctionTaintedness::emptySingleton();
$multiRowsTaint = Taintedness::safeSingleton()->withAddedOffsetTaintedness( null, clone $sqlExecKeysTaint );
$insertQBRowsTaint = $insertQBRowsTaint->withParamSinkTaint( 0, $multiRowsTaint, self::NO_OVERRIDE );
return [
// Note, at the moment, this checks where the function
// is implemented, so you can't use IDatabase.
'\Wikimedia\Rdbms\IDatabase::insert' => $insertTaint,
'\Wikimedia\Rdbms\IMaintainableDatabase::insert' => $insertTaint,
'\Wikimedia\Rdbms\Database::insert' => $insertTaint,
'\Wikimedia\Rdbms\DBConnRef::insert' => $insertTaint,
'\Wikimedia\Rdbms\InsertQueryBuilder::row' => $insertQBRowTaint,
'\Wikimedia\Rdbms\InsertQueryBuilder::rows' => $insertQBRowsTaint,
// FIXME Doesn't handle array args right.
'\wfShellExec' => [
self::SHELL_EXEC_TAINT | self::ARRAY_OK,
'overall' => self::YES_TAINT
],
'\wfShellExecWithStderr' => [
self::SHELL_EXEC_TAINT | self::ARRAY_OK,
'overall' => self::YES_TAINT
],
'\wfEscapeShellArg' => [
( self::YES_TAINT & ~self::SHELL_TAINT ) | self::VARIADIC_PARAM,
'overall' => self::NO_TAINT
],
'\MediaWiki\Shell\Shell::escape' => [
( self::YES_TAINT & ~self::SHELL_TAINT ) | self::VARIADIC_PARAM,
'overall' => self::NO_TAINT
],
'\MediaWiki\Shell\Command::unsafeParams' => [
self::SHELL_EXEC_TAINT | self::VARIADIC_PARAM,
'overall' => self::NO_TAINT
],
'\MediaWiki\Shell\Result::getStdout' => $shellCommandOutput,
'\MediaWiki\Shell\Result::getStderr' => $shellCommandOutput,
// Methods from wikimedia/Shellbox
'\Shellbox\Shellbox::escape' => [
( self::YES_TAINT & ~self::SHELL_TAINT ) | self::VARIADIC_PARAM,
'overall' => self::NO_TAINT
],
'\Shellbox\Command\Command::unsafeParams' => [
self::SHELL_EXEC_TAINT | self::VARIADIC_PARAM,
'overall' => self::NO_TAINT
],
'\Shellbox\Command\UnboxedResult::getStdout' => $shellCommandOutput,
'\Shellbox\Command\UnboxedResult::getStderr' => $shellCommandOutput,
// The value of a status object can be pretty much anything, with any degree of taintedness
// and escaping. Since it's a widely used class, it will accumulate a lot of links and taintedness
// offset, resulting in huge objects (the short string representation of those Taintedness objects
// can reach lengths in the order of tens of millions).
// Since the plugin cannot keep track the taintedness of a property per-instance (as it assumes that
// every property will be used with the same escaping level), we just annotate the methods as safe.
'\StatusValue::newGood' => [
self::NO_TAINT,
'overall' => self::NO_TAINT
],
'\Status::newGood' => [
self::NO_TAINT,
'overall' => self::NO_TAINT
],
'\StatusValue::getValue' => [
'overall' => self::NO_TAINT
],
'\Status::getValue' => [
'overall' => self::NO_TAINT
],
'\StatusValue::setResult' => [
self::NO_TAINT,
self::NO_TAINT,
'overall' => self::NO_TAINT
],
'\Status::setResult' => [
self::NO_TAINT,
self::NO_TAINT,
'overall' => self::NO_TAINT
],
];
}
/**
* Mark XSS's that happen in a Maintenance subclass as false a positive
*
* @inheritDoc
*/
public function isFalsePositive(
int $combinedTaint,
string &$msg,
Context $context,
CodeBase $code_base
): bool {
if ( $combinedTaint === self::HTML_TAINT ) {
$path = str_replace( '\\', '/', $context->getFile() );
if (
strpos( $path, 'maintenance/' ) === 0 ||
strpos( $path, '/maintenance/' ) !== false
) {
// For classes not using Maintenance subclasses
$msg .= ' [Likely false positive because in maintenance subdirectory, thus probably CLI]';
return true;
}
if ( !$context->isInClassScope() ) {
return false;
}
$maintFQSEN = FullyQualifiedClassName::fromFullyQualifiedString(
'\\Maintenance'
);
if ( !$code_base->hasClassWithFQSEN( $maintFQSEN ) ) {
return false;
}
$classFQSEN = $context->getClassFQSEN();
$isMaint = TaintednessVisitor::isSubclassOf( $classFQSEN, $maintFQSEN, $code_base );
if ( $isMaint ) {
$msg .= ' [Likely false positive because in a subclass of Maintenance, thus probably CLI]';
return true;
}
}
return false;
}
/**
* Special-case the $rows argument to Database::insert (T290563)
* @inheritDoc
* @suppress PhanUnusedPublicMethodParameter
*/
public function modifyParamSinkTaint(
Taintedness $paramSinkTaint,
Taintedness $curArgTaintedness,
Node $argument,
int $argIndex,
FunctionInterface $func,
FunctionTaintedness $funcTaint,
CausedByLines $paramSinkError,
Context $context,
CodeBase $code_base
): array {
if ( !$func instanceof Method || $argIndex !== 1 || $func->getName() !== 'insert' ) {
return [ $paramSinkTaint, $paramSinkError ];
}
$classFQSEN = $func->getClassFQSEN();
if ( $classFQSEN->__toString() !== '\\Wikimedia\\Rdbms\\Database' ) {
$idbFQSEN = FullyQualifiedClassName::fromFullyQualifiedString( '\\Wikimedia\\Rdbms\\IDatabase' );
$isDBSubclass = $classFQSEN->asType()->asExpandedTypes( $code_base )->hasType( $idbFQSEN->asType() );
if ( !$isDBSubclass ) {
return [ $paramSinkTaint, $paramSinkError ];
}
}
$argType = UnionTypeVisitor::unionTypeFromNode( $code_base, $context, $argument );
$keyType = GenericArrayType::keyUnionTypeFromTypeSetStrict( $argType->getTypeSet() );
if ( $keyType !== GenericArrayType::KEY_INT ) {
// Note, it might still be an array of rows, but it's too hard for us to tell.
return [ $paramSinkTaint, $paramSinkError ];
}
// Definitely a list of rows, so remove taintedness from the outer array keys, and instead add it to the
// keys of inner arrays.
$sqlExecKeysTaint = Taintedness::safeSingleton()->withAddedKeysTaintedness( self::SQL_EXEC_TAINT );
$adjustedTaint = Taintedness::safeSingleton()->withAddedOffsetTaintedness( null, $sqlExecKeysTaint );
$curErrorLines = $paramSinkError->toLinesArray();
assert( count( $curErrorLines ) === 1 && str_starts_with( $curErrorLines[0], 'Builtin' ) );
$adjustedError = CausedByLines::emptySingleton()
->withAddedLines( $curErrorLines, $adjustedTaint->asExecToYesTaint() );
return [ $adjustedTaint, $adjustedError ];
}
/**
* Disable double escape checking for messages with polymorphic methods
*
* A common cause of false positives for double escaping is that some
* methods take a string|Message, and this confuses the tool given
* the __toString() behaviour of Message. So disable double escape
* checking for that.
*
* This is quite hacky. Ideally the tool would treat methods taking
* multiple types as separate for each type, and also be able to
* reason out simple conditions of the form if ( $arg instanceof Message ).
* However that's much more complicated due to dependence on phan.
*
* @inheritDoc
* @suppress PhanUnusedPublicMethodParameter
*/
public function modifyArgTaint(
Taintedness $curArgTaintedness,
Node $argument,
int $argIndex,
FunctionInterface $func,
FunctionTaintedness $funcTaint,
Context $context,
CodeBase $code_base
): Taintedness {
if ( $curArgTaintedness->has( self::ESCAPED_TAINT ) ) {
$argumentIsMaybeAMsg = false;
/** @var \Phan\Language\Element\Clazz[] $classes */
$classes = UnionTypeVisitor::unionTypeFromNode( $code_base, $context, $argument )
->asClassList( $code_base, $context );
try {
foreach ( $classes as $cl ) {
if ( $cl->getFQSEN()->__toString() === '\Message' ) {
$argumentIsMaybeAMsg = true;
break;
}
}
} catch ( CodeBaseException $_ ) {
// A class that doesn't exist, don't crash.
return $curArgTaintedness;
}
$param = $func->getParameterForCaller( $argIndex );
if ( !$argumentIsMaybeAMsg || !$param || !$param->getUnionType()->hasStringType() ) {
return $curArgTaintedness;
}
/** @var \Phan\Language\Element\Clazz[] $classesParam */
$classesParam = $param->getUnionType()->asClassList( $code_base, $context );
try {
foreach ( $classesParam as $cl ) {
if ( $cl->getFQSEN()->__toString() === '\Message' ) {
// So we are here. Input is a Message, and func expects either a Message or string
// (or something else). So disable double escape check.
return $curArgTaintedness->without( self::ESCAPED_TAINT );
}
}
} catch ( CodeBaseException $_ ) {
// A class that doesn't exist, don't crash.
return $curArgTaintedness;
}
}
return $curArgTaintedness;
}
}
return new MediaWikiSecurityCheckPlugin;