To report a security vulnerability in MediaWiki, follow the responsible disclosure process at https://www.mediawiki.org/wiki/Reporting_security_bugs. The security team also monitors Phabricator for reports.
Security Model Overview
MediaWiki operates with the following trust boundaries:- Anonymous users can read pages (by default) but cannot edit without an account
- Registered users can edit but are subject to permission checks on every action
- Sysops / bureaucrats have elevated privileges, but no action bypasses the permission layer
- Server-side code runs with the privileges of the web server user — the database user should have minimal rights
- User-supplied wikitext (rendered into HTML)
- Uploaded files
- URL and form parameters passed to PHP
- Extension code
SQL Injection Prevention
MediaWiki uses theWikimedia\Rdbms database abstraction layer, which provides parameterized query support. Never interpolate user input directly into SQL strings.
Use Parameterized Queries
Use addQuotes() and addIdentifierQuotes()
When you must construct query fragments, always escape values:Never Trust User Input
- Validate all inputs server-side — client-side validation is never sufficient
- Use
intval()or type casting for numeric parameters - Use
Title::newFromText()and check fornullbefore using page titles from user input
XSS Prevention
Cross-site scripting (XSS) occurs when user-supplied data is rendered in HTML without escaping. MediaWiki’s HTML output layer provides several tools:HTML Escaping
OutputPage
Always useOutputPage methods to add content to pages — do not concatenate raw HTML yourself:
Content Security Policy (CSP)
MediaWiki supports Content Security Policy headers to mitigate XSS even when output escaping fails:CSP nonce support requires that inline scripts use the
$nonce variable injected by OutputPage. Extension code that adds inline JavaScript should use OutputPage::getCSP()->getNonce().CSRF Protection
MediaWiki uses edit tokens (also called CSRF tokens) to protect state-changing actions. Every POST request that modifies data must include a valid token tied to the user’s session.Using Tokens in Extensions
API Edit Tokens
The MediaWiki API requires acsrf token for all write actions. Clients obtain a token via action=query&meta=tokens and include it in the request.
File Upload Security
Uploaded files are a significant attack vector. MediaWiki applies multiple layers of validation:- MIME type checking — verifies the actual file content, not just the extension
- File extension allowlist — only permitted extensions can be uploaded (
$wgFileExtensions) - SVG sanitization — uploaded SVGs are processed to remove
<script>tags, event handlers, andjavascript:URLs - Virus scanning — can be integrated via
$wgAntivirus
wgUpgradeKey
$wgSecretKey is the most important secret in your LocalSettings.php. It is used to:
- Sign user sessions
- Generate CSRF (edit) tokens
- Authenticate MediaWiki API bot passwords
- Sign certain URLs and cookies
Hardening Checklist
Set a strong $wgSecretKey
Generate a cryptographically random 64-character hex string and set it in
LocalSettings.php. Do not reuse a key across wikis.Restrict LocalSettings.php permissions
Ensure
LocalSettings.php is readable only by the web server user:Protect the maintenance directory
Block web access to
maintenance/ — these scripts must only be run from the CLI:Use HTTPS
Set
$wgServer to an https:// URL and configure $wgCookieSecure = true to ensure session cookies are only sent over HTTPS.Restrict file uploads
Set
$wgFileExtensions to only the types you need. Enable antivirus scanning via $wgAntivirus if possible.Enable Content Security Policy
Set
$wgCSPHeader = true to add a CSP header that mitigates XSS attacks.Limit database permissions
The database user in
LocalSettings.php should only have SELECT, INSERT, UPDATE, DELETE on the wiki database. It does not need DROP, CREATE, or ALTER — use a separate privileged user for running update.php.Keep MediaWiki and extensions updated
Subscribe to the mediawiki-announce mailing list for security release notifications. Apply security patches promptly.
Audit extension permissions
Review
$wgGroupPermissions after installing extensions. Some extensions add new rights; verify that defaults are appropriate for your wiki.