mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/ConfirmEdit
synced 2024-11-27 09:30:06 +00:00
Add Content Security Policy handling for ReCaptcha and hCaptcha
Bug: T250544 Change-Id: I280054a8252c991cca04ec74bdb41e079c885d99
This commit is contained in:
parent
4cd8e7664f
commit
9aba484800
|
@ -41,6 +41,8 @@ class HTMLReCaptchaNoCaptchaField extends HTMLFormField {
|
|||
'g-recaptchascript',
|
||||
"<script src=\"https://www.recaptcha.net/recaptcha/api.js?hl={$lang}\" async defer></script>"
|
||||
);
|
||||
ReCaptchaNoCaptcha::addCSPSources( $out->getCSP() );
|
||||
|
||||
$output = Html::element( 'div', [
|
||||
'class' => [
|
||||
'g-recaptcha',
|
||||
|
|
|
@ -62,6 +62,13 @@ HTML;
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getCSPUrls() {
|
||||
return [ 'https://www.recaptcha.net/recaptcha/api.js' ];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Status|array|string $info
|
||||
*/
|
||||
|
|
|
@ -154,6 +154,27 @@ class SimpleCaptcha {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getCSPUrls() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the CSP policies necessary for the captcha module to work in a CSP enforced
|
||||
* setup.
|
||||
*
|
||||
* @param ContentSecurityPolicy $csp The CSP instance to add the policies to, usually
|
||||
* obtained from {@link OutputPage::getCSP()}
|
||||
*/
|
||||
public static function addCSPSources( ContentSecurityPolicy $csp ) {
|
||||
foreach ( static::getCSPUrls() as $src ) {
|
||||
$csp->addScriptSrc( $src );
|
||||
$csp->addStyleSrc( $src );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses getFormInformation() to get the CAPTCHA form and adds it to the given
|
||||
* OutputPage object.
|
||||
|
@ -173,6 +194,8 @@ class SimpleCaptcha {
|
|||
* @param array $formInformation
|
||||
*/
|
||||
public function addFormInformationToOutput( OutputPage $out, array $formInformation ) {
|
||||
static::addCSPSources( $out->getCSP() );
|
||||
|
||||
if ( !$formInformation ) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -46,6 +46,13 @@ class HCaptcha extends SimpleCaptcha {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getCSPUrls() {
|
||||
return [ 'https://hcaptcha.com', 'https://*.hcaptcha.com' ];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Status|array|string $info
|
||||
*/
|
||||
|
|
|
@ -38,6 +38,7 @@ class HTMLHCaptchaField extends HTMLFormField {
|
|||
'h-captcha',
|
||||
"<script src=\"https://hcaptcha.com/1/api.js\" async defer></script>"
|
||||
);
|
||||
HCaptcha::addCSPSources( $out->getCSP() );
|
||||
return Html::element( 'div', [
|
||||
'class' => [
|
||||
'h-captcha',
|
||||
|
|
Loading…
Reference in a new issue