| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
namespace MediaWiki\SecureLinkFixer; |
| 20 |
|
| 21 |
use DateTime; |
| 22 |
use RuntimeException; |
| 23 |
use Wikimedia\StaticArrayWriter; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class ListFetcher { |
| 33 |
|
| 34 |
private $output; |
| 35 |
|
| 36 |
private const HTTP_OPTIONS = [ |
| 37 |
'http' => [ 'method' => 'GET', 'header' => [ 'User-Agent: MediaWiki SecureLinkFixer' ] ] |
| 38 |
]; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function __construct( ?callable $output = null ) { |
| 44 |
$this->output = $output; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private function output( $text ) { |
| 51 |
if ( $this->output ) { |
| 52 |
call_user_func( $this->output, $text ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function getLatestInfo(): array { |
| 62 |
|
| 63 |
$changesUrl = 'https://api.github.com/repos/mozilla-firefox/firefox/commits?sha=main&per_page=1&path=security%2Fmanager%2Fssl%2FnsSTSPreloadList.inc'; |
| 64 |
$json = json_decode( |
| 65 |
file_get_contents( |
| 66 |
$changesUrl, |
| 67 |
false, |
| 68 |
stream_context_create( self::HTTP_OPTIONS ) |
| 69 |
) |
| 70 |
); |
| 71 |
|
| 72 |
if ( !isset( $json[0]->sha ) ) { |
| 73 |
throw new RuntimeException( "Unable to parse revision id/updated date for HSTS preload list" ); |
| 74 |
} |
| 75 |
|
| 76 |
$rev = $json[0]->sha; |
| 77 |
$date = new DateTime( $json[0]->commit->author->date ); |
| 78 |
return [ $rev, $date->format( 'Y-m-d' ) ]; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function fetchList( $rev, $date ) { |
| 89 |
$this->output( "Downloading the HSTS preload list (revision $rev)..." ); |
| 90 |
|
| 91 |
$url = "https://github.com/mozilla-firefox/firefox/raw/{$rev}/security/manager/ssl/nsSTSPreloadList.inc"; |
| 92 |
$lines = explode( "\n", |
| 93 |
file_get_contents( |
| 94 |
$url, |
| 95 |
false, |
| 96 |
stream_context_create( self::HTTP_OPTIONS ) |
| 97 |
) |
| 98 |
); |
| 99 |
$this->output( "done\n" ); |
| 100 |
$inList = false; |
| 101 |
$header = <<<HEADER |
| 102 |
Generated by fetchList.php using mozilla-firefox/firefox@$rev ($date) |
| 103 |
This Source Code Form is subject to the terms of the Mozilla Public |
| 104 |
License, v. 2.0. If a copy of the MPL was not distributed with this |
| 105 |
file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 106 |
phpcs:ignoreFile |
| 107 |
HEADER; |
| 108 |
|
| 109 |
|
| 110 |
$data = []; |
| 111 |
foreach ( $lines as $line ) { |
| 112 |
if ( $line === '%%' ) { |
| 113 |
$inList = !$inList; |
| 114 |
continue; |
| 115 |
} |
| 116 |
|
| 117 |
if ( $inList ) { |
| 118 |
$exploded = explode( ', ', $line ); |
| 119 |
$data[$exploded[0]] = (int)$exploded[1]; |
| 120 |
} |
| 121 |
} |
| 122 |
$writer = new StaticArrayWriter(); |
| 123 |
return $writer->create( $data, $header ); |
| 124 |
} |
| 125 |
} |