This document is provided as a supplement to Security for developers
. This is a list of common development tasks, and the security measures that need to be taken.Security checklist
If you are working with ... | have you ... |
---|---|
Browser Cookies |
# <translate nowrap><!--T:32--> Attempt to fetch the UserID cookie value.</translate>
# <translate nowrap><!--T:33--> Note: the value returned isn't trusted and is forced to be an int.</translate>
$sId = intval( $wgRequest->getCookie( 'UserID' ) );
|
Dynamic code generation |
Avoid using functions like
Sometimes you really do need these features (obviously Inline lambda functions will make it easier to make your callback inline while retaining the benefits of code that's written in native syntax instead of strings.
$str = preg_replace( "!" . preg_quote( $externalStr, '!' ) . "!", $replacement, $str );
|
External programs |
// <translate nowrap><!--T:44--> Automatically escape any naughty characters</translate>
$result = Shell::command( $cmd, '--version' )
->params( 'some', 'extra', 'parameters' )
->execute();
Note that old |
Forms |
|
GET data |
# <translate nowrap><!--T:50--> Check if the action parameter is set to '<tvar name=1>purge</tvar>'</translate>
if ( $wgRequest->getVal( 'action' ) == 'purge' ) {
...
|
Output (API, CSS, JavaScript, HTML, XML, etc.)Any content that MediaWiki generates can be a vector for XSS attacks. |
# <translate nowrap><!--T:52--> <tvar name=1>rawElement()</tvar> escapes all attribute values</translate>
# <translate nowrap><!--T:53--> (which, in this case, is provided by <tvar name=1>$myClass</tvar>)</translate>
echo Html::rawElement( 'p', [ 'class' => $myClass ] );
|
User provided CSS User provided CSS (Say for use in a |
# <translate nowrap><!--T:57--> let <tvar name=1>$CSSFromUser</tvar> be the user's CSS.</translate>
echo Html::rawElement( 'p', [ 'style' => Sanitizer::checkCss( $CSSFromUser ) ] );
|
POST data |
# <translate nowrap><!--T:61--> Check if the action parameter is set to '<tvar name=1>render</tvar>'</translate>
if ( $wgRequest->getVal( 'action' ) == 'render' ) {
...
|
Query strings |
|
Sessions |
|
Reviewer anxiety |
# <translate nowrap><!--T:63--> $wgRequest isn't yet available.</translate> <translate nowrap><!--T:64--> Forced to use <tvar name=1>$_GET</tvar> instead.</translate>
if ( $_GET['setupTestSuite'] !== null ) {
$setupTestSuiteName = $_GET['setupTestSuite'];
...
|
SQL queries |
|
Automated checking
Some of these issues can be checked with phan-taint-check-plugin, which is required for all MediaWiki code in Wikimedia production. This is of course just a tool, and it cannot detect all issue types, and may miss issues even in the issue types it can check for.
See also
- Security for developers