Security checklist for developers

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
{| class="wikitable" | ! If you are working with ... ! have you ...


 * valign="top" |
 * valign="top" |

Cookies

 * valign="top" |
 * reduced reviewer anxiety by using $wgRequest instead of ?
 * fetched cookies using ?
 * set cookies using ?
 * valign="top" |
 * valign="top" |

Dynamic code generation
Avoid using functions like  and , as well as the   pattern modifier for. While powerful and convenient, these features are inherently insecure:
 * valign="top" |


 * it's easier to put arbitrary strings into text processed by a regular expressions, which – when combined with the  pattern modifier – can lead to code injection attacks.
 * it is harder to read and maintain code that is part of a string.
 * static analysis tools won't catch warnings and errors in the code.
 * opcode caches (like APC) can't cache code mixed into strings.
 * sometimes has garbage-collection issues.
 * A loop which has a  inside will create a new function on each iteration.

Sometimes you really do need these features (obviously  needs to run   ;) but in most cases, we'd rather see the function broken out and referred as a callback.

For future code that runs only under PHP 5.3 and later, note that 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.

""
 * Anything external that is used in part of regex should be escaped with preg_quote( $externalStr, $delimiter ). It puts a backslash in front of every character that is part of the regular expression syntax, and escapes also the delimiter given as second parameter:
 * valign="top" |
 * valign="top" |

External programs

 * valign="top" |
 * executed the program via  in   in
 * valign="top" |
 * valign="top" |

Forms

 * valign="top" |
 * used  to  implement anti-CSRF measures?
 * used  when checking the token to avoid timing attacks?
 * reduced reviewer anxiety by using or extending MediaWiki's existing form functionality?


 * valign="top" |
 * valign="top" |

GET data
""
 * valign="top" |
 * reduced reviewer anxiety by using  instead of  ?
 * valign="top" |
 * valign="top" |

Global variables
""
 * valign="top" |
 * written your code to defend against register globals-based variable injection?
 * valign="top" |
 * valign="top" |

Output (API, CSS, JavaScript, HTML, XML, etc.)
Any content that MediaWiki generates can be a vector for XSS attacks. ""
 * valign="top" |
 * used the  and   helper classes?
 * reduced reviewer anxiety by using ResourceLoader to deliver CSS and JavaScript resources?
 * valign="top" |
 * valign="top" |

User provided CSS
User provided CSS (Say for use in a style attribute) needs to be sanitized to prevent XSS, as well as to disallow insertion of tracking images (via background-image), etc ""
 * valign="top" |
 * Use the Sanitizer::checkCss method for any css received from a user, possibly along with the Html class.
 * For CSS provided by the extension (and not the user), this is not needed (and will remove some valid things like background-image:). However, extension provided CSS should go in stylesheets loaded by ResourceLoader, and not in style attributes.
 * valign="top" |
 * valign="top" |

POST data
""
 * valign="top" |
 * reduced reviewer anxiety by using  instead of
 * Always validate that any POST data received is what you expect it to be
 * valign="top" |
 * valign="top" |

Query strings

 * valign="top" |
 * See above


 * valign="top" |
 * valign="top" |

Sessions

 * valign="top" |


 * valign="top" |
 * valign="top" |

Any user input: no isset!
Any  is frowned upon. Use strong comparison like  or. An example from SimpleAntiSpam: ""
 * valign="top" |

See also Register globals and Manual:Coding conventions/PHP.


 * valign="top" |
 * valign="top" |

Reviewer anxiety
""
 * valign="top" |
 * Clearly added comments to explain unexpected or odd parts of your code?
 * valign="top" |
 * valign="top" |

SQL queries

 * valign="top" |
 * used MediaWiki's database wrappers?


 * }