Page MenuHomePhabricator

Consider adding more CSP directives to MediaWIki core
Open, Needs TriagePublicFeature

Description

The current directives included in the MediaWiki core is very limited. Often time wikis and extensions would have to fallback to default-src to load certain resources (e.g. fonts, frame, etc.), making the policy less strict thus less secured. It is also the perquisite to set default-src: none in the future.

Current support

directive?notes
default-srcConfigurable since MW 1.35 through $wgCSPHeader and MediaWiki\Request\ContentSecurityPolicy::addDefaultSrc()
script-srcConfigurable since MW 1.35 through $wgCSPHeader and MediaWiki\Request\ContentSecurityPolicy::addScriptSrc()
style-srcConfigurable since MW 1.35 through $wgCSPHeader and MediaWiki\Request\ContentSecurityPolicy::addStyleSrc()
image-srcConfigurable since MW 1.35 through $wgEnableImageWhitelist and $wgAllowExternalImagesFrom, no direct PHP method to add src.
object-srcConfigurable since MW 1.35 through $wgCSPHeader, no PHP method to add src.

Potential additions

directive?notes
CSP Level 1
connect-srcApplies to XMLHttpRequest (AJAX), WebSocket, fetch(), <a ping> or EventSource. Useful for extensions that make API requests, such as UploadWizard (T278472).
font-srcDefines valid sources of font resources (loaded via @font-face). Useful for wikis that use custom webfonts.
media-srcDefines valid sources of audio and video, eg HTML5 <audio>, <video> elements. Useful for extensions such as TimedMediaHandler.
frame-srcDefines valid sources for loading frames. Useful for extensions such as EmbedVideo. Note that it was once deprecated in favor of the child-src directive at CSP level 2, but undeprecated again in level 3.
sandboxEnables a sandbox for the requested resource similar to the iframe sandbox attribute.
CSP Level 2
base-uriDefines valid sources of <base> element
child-srcSimilar to frame-src.
frame-ancestorsSpecify what parent source may embed a page. A more granular version of X-Frame-Options HTTP header.
CSP Level 3
manifest-srcFor webapp manifest. Useful for PWA such as MobileFrontend and potentially T282500
worker-srcFor service worker. Same as above

I might be able to integrate some of these directives into core. However, I am not sure about the current status of CSP and what are the requirements. Please feel free to edit the list and comment below!

See also

Related: T135963

Event Timeline

Change 1007458 had a related patch set uploaded (by Alistair3149; author: Alistair3149):

[mediawiki/core@master] Set the equivalent frame-ancestors in CSP when X-Frame-Options is set

https://gerrit.wikimedia.org/r/1007458

object-src is probably not too useful in modern browsers now that <object> is just a glorified iframe. It probably makes sense to just always have that set to 'none'.

object-src is probably not too useful in modern browsers now that <object> is just a glorified iframe. It probably makes sense to just always have that set to 'none'.

The current implementation in MW core is to set to none by default, override if manually defined in $wgCSPHeader.