Repository navigation
Conversation
phpseclib 4 moved to the `phpseclib4\` namespace. Every call site that used phpseclib 3 now prefers v4 and falls back to v3 (and to v2 / OpenSSL where that chain already existed), so either major works. - Storage\EncryptionTrait::signString - Core\Testing\KeyPairGenerateTrait::getKeyPair - Auth\ServiceAccountSignerTrait::signBlob - Auth\AccessToken::checkAndInitializePhpsec / loadPhpsecPublicKey - Firebase\JWT\JWT::signPS256 / verifyPS256 Widen the require-dev constraints to allow ^4.0.1 and refresh the suggest texts, which still pointed at version ^2. Also fix the phpseclib guard in ServiceAccountSignerTrait: the check used a relative class name, which resolved to `Google\Auth\phpseclib3\Crypt\RSA` and was therefore never true, so signBlob() always fell through to OpenSSL. The signature output is unchanged. CI gains a "Unit Test + phpseclib 4" matrix leg and a "Package Tests (phpseclib 4)" job, both pinning phpseclib to ^4.0.1 before Composer resolves.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
CLA as been signed |
bshaffer
left a comment
There was a problem hiding this comment.
Hello! First of all, THANK YOU for this submission! It's great.
However, I think we should revert all the changes made in .github. If we support both PHPSecLib 3 and PHPSecLib 4, and our dependencies can resolve them, then there's no need for additional logic to test them. You could add a unit test to confirm that phpseclib4 is installed when --prefer-lowest ISNT used, if you like (or just confirm locally once), but what we have here seems overkill
|
We are getting the following PHPStan errors because of the two separate versions. We may need to mark some classes/methods as ignore or surround them in |
|
In CI,
private function loadPhpsecPublicKey(string $modulus, string $exponent): string
{
/** @var string $phpseclib */
$phpseclib = class_exists(RSA4::class) ? 'phpseclib4' : 'phpseclib3';
$publicKeyLoader = $phpseclib . '\\Crypt\\PublicKeyLoader';
$bigInteger = $phpseclib . '\\Math\\BigInteger';
$key = $publicKeyLoader::load([
'n' => new $bigInteger($this->callJwtStatic('urlsafeB64Decode', [
$modulus,
]), 256),
'e' => new $bigInteger($this->callJwtStatic('urlsafeB64Decode', [
$exponent
]), 256),
]);
$signedString = '';
/** @var string|null $phpseclib */
$phpseclib = match (true) {
class_exists(RSA4::class) => 'phpseclib4',
class_exists(RSA3::class) => 'phpseclib3',
default => null,
};
if ($phpseclib && !$forceOpenssl) {
$publicKeyLoader = $phpseclib . '\\Crypt\\PublicKeyLoader';
$rsaClass = $phpseclib . '\\Crypt\\RSA';
$key = $publicKeyLoader::load($privateKey);
$rsa = $key->withHash('sha256')->withPadding($rsaClass::SIGNATURE_PKCS1);
$signedString = $rsa->sign($stringToSign);
} elseif (extension_loaded('openssl')) {
- /** @var \phpseclib3\Crypt\RSA\PrivateKey $rsa */
$rsa = $publicKeyLoader::load($key);- /** @var \phpseclib3\Crypt\RSA\PublicKey $rsa */
$rsa = $publicKeyLoader::load($key); |
Summary
phpseclib 4.0 moved to the
phpseclib4\namespace. This PR lets the library work with either v3 or v4, in the same spirit as the existing v2/v3 fallback inStorage\EncryptionTrait.Every call site that used phpseclib 3 now prefers v4 and falls back to v3 (and to v2 / OpenSSL where that chain already existed). Only one major can be installed at a time, so the
class_exists()branch simply picks whichever is present. The v4 API surface used here (PublicKeyLoader::load(),RSA::createKey(),RSA::loadPrivateKey(),withPadding(),withHash(),sign(),verify(),BigInteger) is unchanged from v3.Touched call sites:
Google\Cloud\Storage\EncryptionTrait::signString()Google\Cloud\Core\Testing\KeyPairGenerateTrait::getKeyPair()Google\Auth\ServiceAccountSignerTrait::signBlob()Google\Auth\AccessToken::checkAndInitializePhpsec()/loadPhpsecPublicKey()Firebase\JWT\JWT::signPS256()/verifyPS256()require-devconstraints in the root,Auth,JwtandStoragecomposer.jsonnow allow^4.0.1. Thesuggestentries still said "Please require version ^2", so they have been refreshed to^3 or ^4, along with the matching notes inJwt/README.mdand thePubSub\Subscriptiondocblock.Drive-by bug fix
ServiceAccountSignerTraitguarded onclass_exists(phpseclib3\Crypt\RSA::class). That is a relative name insidenamespace Google\Auth, so it resolved toGoogle\Auth\phpseclib3\Crypt\RSAand was never true —signBlob()always fell through to OpenSSL, even with phpseclib installed. It now uses the imported aliases.This is a behavior change (phpseclib is actually used when present), but the produced signature is byte-identical to the OpenSSL path; see the verification table below.
CI
Unit Test + phpseclib 4leg in thetestmatrix, which pinsphpseclib/phpseclib:^4.0.1beforecomposer update. Other legs are untouched — the step is gated on a matrix key they do not define.Package Tests (phpseclib 4)job, scoped toAuth Jwt Storageso it costs a fraction of a full package-test run.run-package-tests.shgains an optionalPHPSECLIBenv var. When set, components that declarephpseclib/phpseclibinrequire-devget that constraint pinned in theircomposer-local.jsonbefore Composer resolves. When unset the script behaves exactly as before.Verification
Each affected path was exercised against both majors installed side by side, comparing output to the OpenSSL path:
JWT::encode/decodePS256 roundtripEncryptionTrait::signStringvs OpenSSLServiceAccountSignerTrait::signBlobvs OpenSSLAccessToken::loadPhpsecPublicKeyvs OpenSSL public keyThe package-test script changes were dry-run with a stubbed Composer to confirm the pin is applied to
Auth,JwtandStorageonly, that components without the dependency are skipped, that the committedcomposer.jsonfiles are left untouched, and that an unsetPHPSECLIBis a no-op.Note
phpseclib 4 requires PHP >= 8.1.
Jwt/composer.jsonstill allows PHP ^8.0, where Composer will simply resolve to v3 — no conflict.