mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/SecureLinkFixer
synced 2024-11-30 18:34:16 +00:00
Switch to new hook handler format
* Turn HSTSPreloadLookup into a real service, per @todo * Convert Hooks into a non-static class, take lookup as an argument * Update tests for new calling style == Test plan == * CI/tests pass * Use Special:ExpandTemplates with a preloaded domain and localhost, observe preloaded link changes, localhost stays http Bug: T271027 Change-Id: I0196350e85c09eedfba4dbf1dab81f9a757b2a12
This commit is contained in:
parent
62898355d3
commit
0509d34225
|
@ -16,8 +16,19 @@
|
|||
"MessagesDirs": {
|
||||
"SecureLinkFixer": "i18n"
|
||||
},
|
||||
"Hooks": {
|
||||
"LinkerMakeExternalLink": "MediaWiki\\SecureLinkFixer\\Hooks::onLinkerMakeExternalLink"
|
||||
"HookHandlers": {
|
||||
"main": {
|
||||
"class": "MediaWiki\\SecureLinkFixer\\Hooks",
|
||||
"services": [
|
||||
"HSTSPreloadLookup"
|
||||
]
|
||||
}
|
||||
},
|
||||
"Hooks": {
|
||||
"LinkerMakeExternalLink": "main"
|
||||
},
|
||||
"ServiceWiringFiles": [
|
||||
"includes/ServiceWiring.php"
|
||||
],
|
||||
"manifest_version": 2
|
||||
}
|
||||
|
|
|
@ -25,20 +25,6 @@ class HSTSPreloadLookup {
|
|||
*/
|
||||
private $domains;
|
||||
|
||||
/**
|
||||
* @todo turn into proper MWServices thing
|
||||
* @codeCoverageIgnore
|
||||
* @return HSTSPreloadLookup
|
||||
*/
|
||||
public static function getInstance() {
|
||||
static $instance;
|
||||
if ( !$instance ) {
|
||||
$instance = new self( require __DIR__ . '/../domains.php' );
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $domains
|
||||
*/
|
||||
|
|
|
@ -18,16 +18,35 @@
|
|||
|
||||
namespace MediaWiki\SecureLinkFixer;
|
||||
|
||||
class Hooks {
|
||||
use MediaWiki\Hook\LinkerMakeExternalLinkHook;
|
||||
|
||||
class Hooks implements LinkerMakeExternalLinkHook {
|
||||
|
||||
/** @var HSTSPreloadLookup */
|
||||
private $lookup;
|
||||
|
||||
/**
|
||||
* @param HSTSPreloadLookup $lookup
|
||||
*/
|
||||
public function __construct( HSTSPreloadLookup $lookup ) {
|
||||
$this->lookup = $lookup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook: LinkerMakeExternalLink
|
||||
*
|
||||
* Changes the scheme of the URL to HTTPS if necessary
|
||||
*
|
||||
* @param string &$url
|
||||
* @param string &$url Link URL
|
||||
* @param string &$text Link text
|
||||
* @param string &$link New link HTML (if returning false)
|
||||
* @param string[] &$attribs Attributes to be applied
|
||||
* @param string $linkType External link type
|
||||
* @return bool|void True or no return value to continue or false to abort
|
||||
*/
|
||||
public static function onLinkerMakeExternalLink( &$url ) {
|
||||
public function onLinkerMakeExternalLink(
|
||||
&$url, &$text, &$link, &$attribs, $linkType
|
||||
) {
|
||||
if ( strpos( $url, 'https://' ) === 0 ) {
|
||||
// Already HTTPS
|
||||
return;
|
||||
|
@ -43,7 +62,7 @@ class Hooks {
|
|||
return;
|
||||
}
|
||||
|
||||
if ( HSTSPreloadLookup::getInstance()->isPreloaded( $parsed['host'] ) ) {
|
||||
if ( $this->lookup->isPreloaded( $parsed['host'] ) ) {
|
||||
$parsed['scheme'] = 'https';
|
||||
$parsed['delimiter'] = '://';
|
||||
$url = wfAssembleUrl( $parsed );
|
||||
|
|
11
includes/ServiceWiring.php
Normal file
11
includes/ServiceWiring.php
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// Copyright (C) 2022 Kunal Mehta <legoktm@debian.org>
|
||||
|
||||
namespace MediaWiki\SecureLinkFixer;
|
||||
|
||||
return [
|
||||
'HSTSPreloadLookup' => static function () {
|
||||
return new HSTSPreloadLookup( require __DIR__ . '/../domains.php' );
|
||||
}
|
||||
];
|
|
@ -19,6 +19,7 @@
|
|||
namespace MediaWiki\SecureLinkFixer;
|
||||
|
||||
use Benchmarker;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use const RUN_MAINTENANCE_IF_MAIN;
|
||||
|
||||
$IP = getenv( 'MW_INSTALL_PATH' );
|
||||
|
@ -40,7 +41,7 @@ class BenchLookup extends Benchmarker {
|
|||
}
|
||||
|
||||
public function execute() {
|
||||
$lookup = HSTSPreloadLookup::getInstance();
|
||||
$lookup = MediaWikiServices::getInstance()->getService( 'HSTSPreloadLookup' );
|
||||
$domains = [
|
||||
// Need to traverse up one domain to find it
|
||||
'foobar.dev',
|
||||
|
|
|
@ -30,7 +30,10 @@ class HooksTest extends MediaWikiIntegrationTestCase {
|
|||
* @dataProvider provideOnLinkerMakeExternalLink
|
||||
*/
|
||||
public function testOnLinkerMakeExternalLink( $input, $expected ) {
|
||||
Hooks::onLinkerMakeExternalLink( $input );
|
||||
$hooks = new Hooks( $this->getServiceContainer()->getService( 'HSTSPreloadLookup' ) );
|
||||
$dummy = '';
|
||||
$dummy2 = [];
|
||||
$hooks->onLinkerMakeExternalLink( $input, $dummy, $dummy, $dummy2, $dummy );
|
||||
$this->assertSame( $expected, $input );
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue