mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2024-10-01 01:26:10 -04:00
break unit tests if mismatch between JS files and SRI configuration array is detected
This commit is contained in:
parent
118c919663
commit
6261c94fc9
@ -1,5 +1,7 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
use Exception;
|
||||
use GlobIterator;
|
||||
use Google\Cloud\Core\Exception\BadRequestException;
|
||||
use Google\Cloud\Core\Exception\NotFoundException;
|
||||
use Google\Cloud\Storage\Bucket;
|
||||
@ -371,31 +373,34 @@ class Helper
|
||||
*/
|
||||
public static function updateSubresourceIntegrity()
|
||||
{
|
||||
$dir = dir(PATH . 'js');
|
||||
while (false !== ($file = $dir->read())) {
|
||||
if (substr($file, -3) === '.js') {
|
||||
self::$hashes[$file] = base64_encode(
|
||||
hash('sha512', file_get_contents(
|
||||
PATH . 'js' . DIRECTORY_SEPARATOR . $file
|
||||
), true)
|
||||
foreach (new GlobIterator(PATH . 'js' . DIRECTORY_SEPARATOR . '*.js') as $file) {
|
||||
if ($file->getBasename() == 'common.js') {
|
||||
continue; // ignore JS unit test bootstrap
|
||||
}
|
||||
self::$hashes[$file->getBasename()] = base64_encode(
|
||||
hash('sha512', file_get_contents($file->getPathname()), true)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$counter = 0;
|
||||
$file = PATH . 'lib' . DIRECTORY_SEPARATOR . 'Configuration.php';
|
||||
$content = preg_replace_callback(
|
||||
'#\'js/([a-z0-9.-]+.js)(\' +)=\> \'[^\']*\',#',
|
||||
function ($matches) {
|
||||
function ($matches) use (&$counter) {
|
||||
if (array_key_exists($matches[1], Helper::$hashes)) {
|
||||
$counter++;
|
||||
return '\'js/' . $matches[1] . $matches[2] .
|
||||
'=> \'sha512-' . Helper::$hashes[$matches[1]] . '\',';
|
||||
} else {
|
||||
return $matches[0];
|
||||
throw new Exception('SRI hash for file js/' . $matches[1] . ' not found, please add the missing file or remove it from lib/Configuration.php.');
|
||||
}
|
||||
},
|
||||
file_get_contents($file)
|
||||
);
|
||||
file_put_contents($file, $content);
|
||||
if ($counter != count(self::$hashes)) {
|
||||
throw new Exception('Mismatch between ' . count(self::$hashes) . ' found js files and ' . $counter . ' SRI hashes in lib/Configuration.php, please update lib/Configuration.php to match the list of js files.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user