Merge pull request #12249 from MrPetovan/bug/deprecated

Address several PHP 8.1 deprecation notices
This commit is contained in:
Philipp 2022-11-25 23:32:32 +01:00 committed by GitHub
commit c2c563be26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 267 additions and 190 deletions

View file

@ -45,7 +45,7 @@
"paragonie/hidden-string": "^1.0",
"patrickschur/language-detection": "^5.0.0",
"pear/console_table": "^1.3",
"phpseclib/phpseclib": "^2.0",
"phpseclib/phpseclib": "^3.0",
"pragmarx/google2fa": "^5.0",
"pragmarx/recovery": "^0.2",
"psr/container": "^1.0",

26
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "e8626dc6957dff9cc783daad10cfc26f",
"content-hash": "2e082bac083ca61cc0c22f7055d690bf",
"packages": [
{
"name": "asika/simple-console",
@ -2952,32 +2952,32 @@
},
{
"name": "phpseclib/phpseclib",
"version": "2.0.38",
"version": "3.0.17",
"source": {
"type": "git",
"url": "https://github.com/phpseclib/phpseclib.git",
"reference": "b03536539f43a4f9aa33c4f0b2f3a1c752088fcd"
"reference": "dbc2307d5c69aeb22db136c52e91130d7f2ca761"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/b03536539f43a4f9aa33c4f0b2f3a1c752088fcd",
"reference": "b03536539f43a4f9aa33c4f0b2f3a1c752088fcd",
"url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/dbc2307d5c69aeb22db136c52e91130d7f2ca761",
"reference": "dbc2307d5c69aeb22db136c52e91130d7f2ca761",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
"paragonie/constant_time_encoding": "^1|^2",
"paragonie/random_compat": "^1.4|^2.0|^9.99.99",
"php": ">=5.6.1"
},
"require-dev": {
"phing/phing": "~2.7",
"phpunit/phpunit": "^4.8.35|^5.7|^6.0|^9.4",
"squizlabs/php_codesniffer": "~2.0"
"phpunit/phpunit": "*"
},
"suggest": {
"ext-dom": "Install the DOM extension to load XML formatted public keys.",
"ext-gmp": "Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations.",
"ext-libsodium": "SSH2/SFTP can make use of some algorithms provided by the libsodium-php extension.",
"ext-mcrypt": "Install the Mcrypt extension in order to speed up a few other cryptographic operations.",
"ext-openssl": "Install the OpenSSL extension in order to speed up a wide variety of cryptographic operations.",
"ext-xml": "Install the XML extension to load XML formatted public keys."
"ext-openssl": "Install the OpenSSL extension in order to speed up a wide variety of cryptographic operations."
},
"type": "library",
"autoload": {
@ -2985,7 +2985,7 @@
"phpseclib/bootstrap.php"
],
"psr-4": {
"phpseclib\\": "phpseclib/"
"phpseclib3\\": "phpseclib/"
}
},
"notification-url": "https://packagist.org/downloads/",
@ -3054,7 +3054,7 @@
"type": "tidelift"
}
],
"time": "2022-09-02T17:04:26+00:00"
"time": "2022-10-24T10:51:50+00:00"
},
{
"name": "pragmarx/google2fa",

View file

@ -48,25 +48,27 @@ class BaseCollection extends \ArrayIterator
/**
* @inheritDoc
*/
public function offsetSet($offset, $value)
#[\ReturnTypeWillChange]
public function offsetSet($key, $value): void
{
if (is_null($offset)) {
if (is_null($key)) {
$this->totalCount++;
}
parent::offsetSet($offset, $value);
parent::offsetSet($key, $value);
}
/**
* @inheritDoc
*/
public function offsetUnset($offset)
#[\ReturnTypeWillChange]
public function offsetUnset($key): void
{
if ($this->offsetExists($offset)) {
if ($this->offsetExists($key)) {
$this->totalCount--;
}
parent::offsetUnset($offset);
parent::offsetUnset($key);
}
/**

View file

@ -794,7 +794,7 @@ class Conversation
return [];
}
$str_blocked = str_replace(["\n", "\r"], ",", $this->pConfig->get($this->session->getLocalUserId(), 'system', 'blocked'));
$str_blocked = str_replace(["\n", "\r"], ",", $this->pConfig->get($this->session->getLocalUserId(), 'system', 'blocked') ?? '');
if (empty($str_blocked)) {
return [];
}

View file

@ -57,6 +57,7 @@ class Database extends AbstractSessionHandler
return true;
}
#[\ReturnTypeWillChange]
public function read($id)
{
if (empty($id)) {
@ -136,6 +137,7 @@ class Database extends AbstractSessionHandler
}
}
#[\ReturnTypeWillChange]
public function gc($max_lifetime): bool
{
try {

View file

@ -110,7 +110,7 @@ class Database
$this->connected = false;
$port = 0;
$serveraddr = trim($this->configCache->get('database', 'hostname'));
$serveraddr = trim($this->configCache->get('database', 'hostname') ?? '');
$serverdata = explode(':', $serveraddr);
$host = trim($serverdata[0]);
if (count($serverdata) > 1) {

View file

@ -687,7 +687,7 @@ class Media
$previews[] = $medium['preview'];
}
$type = explode('/', explode(';', $medium['mimetype'])[0]);
$type = explode('/', explode(';', $medium['mimetype'] ?? '')[0]);
if (count($type) < 2) {
Logger::info('Unknown MimeType', ['type' => $type, 'media' => $medium]);
$filetype = 'unkn';

View file

@ -324,8 +324,8 @@ class Site extends BaseAdmin
/* Installed langs */
$lang_choices = DI::l10n()->getAvailableLanguages();
if (strlen(DI::config()->get('system', 'directory_submit_url')) &&
!strlen(DI::config()->get('system', 'directory'))) {
if (DI::config()->get('system', 'directory_submit_url') &&
!DI::config()->get('system', 'directory')) {
DI::config()->set('system', 'directory', dirname(DI::config()->get('system', 'directory_submit_url')));
DI::config()->delete('system', 'directory_submit_url');
}

View file

@ -327,7 +327,7 @@ class Profile extends BaseModule
'$submit' => $this->t('Submit'),
'$lbl_info1' => $lbl_info1,
'$lbl_info2' => $this->t('Their personal note'),
'$reason' => trim($contact['reason']),
'$reason' => trim($contact['reason'] ?? ''),
'$infedit' => $this->t('Edit contact notes'),
'$common_link' => 'contact/' . $contact['id'] . '/contacts/common',
'$relation_text' => $relation_text,

View file

@ -142,14 +142,9 @@ class Salmon extends \Friendica\BaseModule
throw new HTTPException\BadRequestException();
}
$key_info = explode('.', $key);
$this->logger->info('Key details', ['info' => $key]);
$m = Strings::base64UrlDecode($key_info[1]);
$e = Strings::base64UrlDecode($key_info[2]);
$this->logger->info('Key details', ['info' => $key_info]);
$pubkey = Crypto::meToPem($m, $e);
$pubkey = SalmonProtocol::magicKeyToPem($key);
// We should have everything we need now. Let's see if it verifies.

View file

@ -23,11 +23,9 @@ namespace Friendica\Module;
use Friendica\BaseModule;
use Friendica\Core\System;
use Friendica\DI;
use Friendica\Model\User;
use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Util\Crypto;
use Friendica\Util\Strings;
use Friendica\Protocol\Salmon;
/**
* prints the public RSA key of a user
@ -47,9 +45,10 @@ class PublicRSAKey extends BaseModule
throw new BadRequestException();
}
Crypto::pemToMe($user['spubkey'], $modulus, $exponent);
$content = 'RSA' . '.' . Strings::base64UrlEncode($modulus, true) . '.' . Strings::base64UrlEncode($exponent, true);
System::httpExit($content, Response::TYPE_BLANK, 'application/magic-public-key');
System::httpExit(
Salmon::salmonKey($user['spubkey']),
Response::TYPE_BLANK,
'application/magic-public-key'
);
}
}

View file

@ -233,7 +233,7 @@ class PortableContacts extends BaseModule
}
if ($selectedFields['tags']) {
$tags = str_replace(',', ' ', $contact['keywords']);
$tags = str_replace(',', ' ', $contact['keywords'] ?? '');
$tags = explode(' ', $tags);
$cleaned = [];

View file

@ -40,6 +40,7 @@ use Friendica\Protocol\ActivityNamespace;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Email;
use Friendica\Protocol\Feed;
use Friendica\Protocol\Salmon;
use Friendica\Util\Crypto;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Network;
@ -1512,12 +1513,10 @@ class Probe
$pubkey = $curlResult->getBody();
}
$key = explode('.', $pubkey);
try {
$data['pubkey'] = Salmon::magicKeyToPem($pubkey);
} catch (\Throwable $e) {
if (sizeof($key) >= 3) {
$m = Strings::base64UrlDecode($key[1]);
$e = Strings::base64UrlDecode($key[2]);
$data['pubkey'] = Crypto::meToPem($m, $e);
}
}
}

View file

@ -200,6 +200,7 @@ class Email implements IEmail
/**
* @inheritDoc
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->toArray();

View file

@ -25,9 +25,11 @@ use Friendica\Core\Logger;
use Friendica\DI;
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
use Friendica\Network\Probe;
use Friendica\Protocol\Salmon\Format\Magic;
use Friendica\Util\Crypto;
use Friendica\Util\Strings;
use Friendica\Util\XML;
use phpseclib3\Crypt\PublicKeyLoader;
/**
* Salmon Protocol class
@ -243,7 +245,19 @@ class Salmon
*/
public static function salmonKey(string $pubkey): string
{
Crypto::pemToMe($pubkey, $modulus, $exponent);
return 'RSA' . '.' . Strings::base64UrlEncode($modulus, true) . '.' . Strings::base64UrlEncode($exponent, true);
\phpseclib3\Crypt\RSA::addFileFormat(Magic::class);
return PublicKeyLoader::load($pubkey)->toString('Magic');
}
/**
* @param string $magic Magic key format starting with "RSA."
* @return string
*/
public static function magicKeyToPem(string $magic): string
{
\phpseclib3\Crypt\RSA::addFileFormat(Magic::class);
return (string) PublicKeyLoader::load($magic);
}
}

View file

@ -0,0 +1,77 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Protocol\Salmon\Format;
use Friendica\Util\Strings;
use phpseclib3\Math\BigInteger;
/**
* This custom public RSA key format class is meant to be used with the \phpseclib3\Crypto\RSA::addFileFormat method.
*
* It handles Salmon's specific magic key string starting with "RSA." and which MIME type is application/magic-key or
* application/magic-public-key
*
* @see https://web.archive.org/web/20160506073138/http://salmon-protocol.googlecode.com:80/svn/trunk/draft-panzer-magicsig-01.html#anchor13
*/
class Magic
{
public static function load($key, $password = ''): array
{
if (!is_string($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}
$key_info = explode('.', $key);
if (count($key_info) !== 3) {
throw new \UnexpectedValueException('Key should have three components separated by periods');
}
if ($key_info[0] !== 'RSA') {
throw new \UnexpectedValueException('Key first component should be "RSA"');
}
if (preg_match('#[+/]#', $key_info[1])
|| preg_match('#[+/]#', $key_info[1])
) {
throw new \UnexpectedValueException('Wrong encoding, expecting Base64URLencoding');
}
$m = Strings::base64UrlDecode($key_info[1]);
$e = Strings::base64UrlDecode($key_info[2]);
if (!$m || !$e) {
throw new \UnexpectedValueException('Base64 decoding produced an error');
}
return [
'modulus' => new BigInteger($m, 256),
'publicExponent' => new BigInteger($e, 256),
'isPublicKey' => true,
];
}
public static function savePublicKey(BigInteger $n, BigInteger $e, array $options = []): string
{
return 'RSA.' . Strings::base64UrlEncode($n->toBytes(), true) . '.' . Strings::base64UrlEncode($e->toBytes(), true);
}
}

View file

@ -21,14 +21,11 @@
namespace Friendica\Util;
use Exception;
use Friendica\Core\Hook;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\DI;
use ParagonIE\ConstantTime\Base64UrlSafe;
use phpseclib\Crypt\RSA;
use phpseclib\Math\BigInteger;
use phpseclib3\Crypt\PublicKeyLoader;
/**
* Crypto class
@ -66,22 +63,6 @@ class Crypto
return openssl_verify($data, $sig, $key, (($alg == 'sha1') ? OPENSSL_ALGO_SHA1 : $alg));
}
/**
/**
* @param string $m modulo
* @param string $e exponent
* @return string
*/
public static function meToPem($m, $e)
{
$rsa = new RSA();
$rsa->loadKey([
'e' => new BigInteger($e, 256),
'n' => new BigInteger($m, 256)
]);
return $rsa->getPublicKey();
}
/**
* Transform RSA public keys to standard PEM output
*
@ -91,29 +72,7 @@ class Crypto
*/
public static function rsaToPem(string $key)
{
$rsa = new RSA();
$rsa->setPublicKey($key);
return $rsa->getPublicKey(RSA::PUBLIC_FORMAT_PKCS8);
}
/**
* Extracts the modulo and exponent reference from a public PEM key
*
* @param string $key public PEM key
* @param string $modulus (ref) modulo reference
* @param string $exponent (ref) exponent reference
*
* @return void
*/
public static function pemToMe(string $key, &$modulus, &$exponent)
{
$rsa = new RSA();
$rsa->loadKey($key);
$rsa->setPublicKey();
$modulus = $rsa->modulus->toBytes();
$exponent = $rsa->exponent->toBytes();
return (string)PublicKeyLoader::load($key);
}
/**
@ -152,50 +111,6 @@ class Crypto
return $response;
}
/**
* Create a new elliptic curve key pair
*
* @return array with the elements "prvkey", "pubkey", "vapid-public" and "vapid-private"
*/
public static function newECKeypair()
{
$openssl_options = [
'curve_name' => 'prime256v1',
'private_key_type' => OPENSSL_KEYTYPE_EC
];
$conf = DI::config()->get('system', 'openssl_conf_file');
if ($conf) {
$openssl_options['config'] = $conf;
}
$result = openssl_pkey_new($openssl_options);
if (empty($result)) {
throw new Exception('Key creation failed');
}
$response = ['prvkey' => '', 'pubkey' => ''];
// Get private key
openssl_pkey_export($result, $response['prvkey']);
// Get public key
$pkey = openssl_pkey_get_details($result);
$response['pubkey'] = $pkey['key'];
// Create VAPID keys
// @see https://github.com/web-push-libs/web-push-php/blob/256a18b2a2411469c94943725fb6eccb9681bd75/src/Utils.php#L60-L62
$hexString = '04';
$hexString .= str_pad(bin2hex($pkey['ec']['x']), 64, '0', STR_PAD_LEFT);
$hexString .= str_pad(bin2hex($pkey['ec']['y']), 64, '0', STR_PAD_LEFT);
$response['vapid-public'] = Base64UrlSafe::encode(hex2bin($hexString));
// @see https://github.com/web-push-libs/web-push-php/blob/256a18b2a2411469c94943725fb6eccb9681bd75/src/VAPID.php
$response['vapid-private'] = Base64UrlSafe::encode(hex2bin(str_pad(bin2hex($pkey['ec']['d']), 64, '0', STR_PAD_LEFT)));
return $response;
}
/**
* Encrypt a string with 'aes-256-cbc' cipher method.
*

View file

@ -24,6 +24,7 @@ namespace Friendica\Util;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\DI;
use GuzzleHttp\Psr7\Uri;
/**
* Proxy utilities class
@ -173,12 +174,15 @@ class Proxy
*/
private static function parseQuery(string $url): array
{
$query = parse_url($url, PHP_URL_QUERY);
$query = html_entity_decode($query);
try {
$uri = new Uri($url);
parse_str($query, $arr);
parse_str($uri->getQuery(), $arr);
return $arr;
return $arr;
} catch (\Throwable $e) {
return [];
}
}
/**

View file

@ -23,6 +23,7 @@ namespace Friendica\Util;
use Friendica\Content\ContactSelector;
use Friendica\Core\Logger;
use ParagonIE\ConstantTime\Base64;
/**
* This class handles string functions
@ -245,16 +246,17 @@ class Strings
* @param string $s URL to encode
* @param boolean $strip_padding Optional. Default false
* @return string Encoded URL
* @see https://web.archive.org/web/20160506073138/http://salmon-protocol.googlecode.com:80/svn/trunk/draft-panzer-magicsig-01.html#params
*/
public static function base64UrlEncode(string $s, bool $strip_padding = false): string
{
$s = strtr(base64_encode($s), '+/', '-_');
if ($strip_padding) {
$s = str_replace('=', '', $s);
$s = Base64::encodeUnpadded($s);
} else {
$s = Base64::encode($s);
}
return $s;
return strtr($s, '+/', '-_');
}
/**
@ -263,26 +265,11 @@ class Strings
* @param string $s URL to decode
* @return string Decoded URL
* @throws \Exception
* @see https://web.archive.org/web/20160506073138/http://salmon-protocol.googlecode.com:80/svn/trunk/draft-panzer-magicsig-01.html#params
*/
public static function base64UrlDecode(string $s): string
{
/*
* // Placeholder for new rev of salmon which strips base64 padding.
* // PHP base64_decode handles the un-padded input without requiring this step
* // Uncomment if you find you need it.
*
* $l = strlen($s);
* if (!strpos($s,'=')) {
* $m = $l % 4;
* if ($m == 2)
* $s .= '==';
* if ($m == 3)
* $s .= '=';
* }
*
*/
return base64_decode(strtr($s, '-_', '+/'));
return Base64::decode(strtr($s, '-_', '+/'));
}
/**
@ -515,4 +502,4 @@ class Strings
return $text;
}
}
}

View file

@ -0,0 +1 @@
RSA.tvsoBZbLUvqWs-0d8C5hVQLjLCjjxyZb17Rm8_9FDqBYUigBSFDcJCzG27FM-zuddwpgJB0vDuPKQnt59kKRsw.AQAB

View file

@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALb7KAWWy1L6lrPtHfAuYVUC4ywo48cm
W9e0ZvP/RQ6gWFIoAUhQ3CQsxtuxTPs7nXcKYCQdLw7jykJ7efZCkbMCAwEAAQ==
-----END PUBLIC KEY-----

View file

@ -0,0 +1,105 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Test\src\Protocol;
use Friendica\Protocol\Salmon;
class SalmonTest extends \PHPUnit\Framework\TestCase
{
public function dataMagic(): array
{
return [
'salmon' => [
'magic' => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/salmon-public-magic'),
'pem' => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/salmon-public-pem'),
],
];
}
/**
* @dataProvider dataMagic
*
* @param $magic
* @param $pem
* @return void
* @throws \Exception
*/
public function testSalmonKey($magic, $pem)
{
$this->assertEquals($magic, Salmon::salmonKey($pem));
}
/**
* @dataProvider dataMagic
*
* @param $magic
* @param $pem
* @return void
*/
public function testMagicKeyToPem($magic, $pem)
{
$this->assertEquals($pem, Salmon::magicKeyToPem($magic));
}
public function dataMagicFailure(): array
{
return [
'empty string' => [
'magic' => '',
],
'Missing algo' => [
'magic' => 'tvsoBZbLUvqWs-0d8C5hVQLjLCjjxyZb17Rm8_9FDqBYUigBSFDcJCzG27FM-zuddwpgJB0vDuPKQnt59kKRsw.AQAB',
],
'Missing modulus' => [
'magic' => 'RSA.AQAB',
],
'Missing exponent' => [
'magic' => 'RSA.tvsoBZbLUvqWs-0d8C5hVQLjLCjjxyZb17Rm8_9FDqBYUigBSFDcJCzG27FM-zuddwpgJB0vDuPKQnt59kKRsw',
],
'Missing key parts' => [
'magic' => 'RSA.',
],
'Too many parts' => [
'magic' => 'RSA.tvsoBZbLUvqWs-0d8C5hVQLjLCjjxyZb17Rm8_9FDqBYUigBSFDcJCzG27FM-zuddwpgJB0vDuPKQnt59kKRsw.AQAB.AQAB',
],
'Wrong encoding' => [
'magic' => 'RSA.tvsoBZbLUvqWs-0d8C5hVQLjLCjjxyZb17Rm8/9FDqBYUigBSFDcJCzG27FM+zuddwpgJB0vDuPKQnt59kKRsw.AQAB',
],
'Wrong algo' => [
'magic' => 'ECDSA.tvsoBZbLUvqWs-0d8C5hVQLjLCjjxyZb17Rm8_9FDqBYUigBSFDcJCzG27FM-zuddwpgJB0vDuPKQnt59kKRsw.AQAB',
],
];
}
/**
* @dataProvider dataMagicFailure
*
* @param $magic
* @return void
*/
public function testMagicKeyToPemFailure($magic)
{
$this->expectException(\Throwable::class);
Salmon::magicKeyToPem($magic);
}
}

View file

@ -65,7 +65,7 @@ class CryptoTest extends TestCase
self::assertEquals(11111111, $test);
}
public function dataRsa()
public function dataRsa(): array
{
return [
'diaspora' => [
@ -92,34 +92,6 @@ class CryptoTest extends TestCase
],
];
}
/**
* @dataProvider dataPEM
*/
public function testPemToMe(string $key)
{
Crypto::pemToMe($key, $m, $e);
$expectedRSA = new RSA();
$expectedRSA->loadKey([
'e' => new BigInteger($e, 256),
'n' => new BigInteger($m, 256)
]);
self::assertEquals($expectedRSA->getPublicKey(), $key);
}
/**
* @dataProvider dataPEM
*/
public function testMeToPem(string $key)
{
Crypto::pemToMe($key, $m, $e);
$checkKey = Crypto::meToPem($m, $e);
self::assertEquals($key, $checkKey);
}
}
/**