-
Notifications
You must be signed in to change notification settings - Fork 82
Signature: Avoid PHP warning when algorithm
is not set
#1803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR prevents a PHP warning when the algorithm
key is missing in the signature block and refines the PHPCS ignore comment to target specific rules.
- Wraps the
algorithm
access inisset()
to avoid undefined array key warnings - Specifies exact PHPCS rules to ignore on the digest comparison line
Comments suppressed due to low confidence (1)
includes/class-signature.php:357
- There isn’t a test case covering when the
algorithm
key is missing or empty. Please add unit tests to verify that missing or invalid algorithms neither trigger warnings nor cause unexpected return values.
if ( isset( $signature_block['algorithm'] ) ) {
@@ -354,7 +354,7 @@ public static function get_remote_key( $key_id ) { | |||
* @return string The signature algorithm. | |||
*/ | |||
public static function get_signature_algorithm( $signature_block ) { | |||
if ( $signature_block['algorithm'] ) { | |||
if ( isset( $signature_block['algorithm'] ) ) { | |||
switch ( $signature_block['algorithm'] ) { | |||
case 'rsa-sha-512': | |||
return 'sha512'; // hs2019 https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures-12. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mediaformat While looking at this function I opened the spec that's linked to in the rsa-sha-512
case.
Any example there mentioning SHA512
has hs2019
set as its algorithm, not rsa-sha-512
:
The authorization header and signature would be generated as:
Authorization: Signature keyId="rsa-key-1",algorithm="hs2019", headers="(request-target) (created) host digest content-length", signature="Base64(RSA-SHA512(signing string))"
The error that gets returned when there is no algorithm says "only rsa-sha256 and hs2019 are supported":
wordpress-activitypub/includes/class-signature.php
Lines 286 to 288 in 499bcad
if ( ! $algorithm ) { | |
return new WP_Error( 'activitypub_signature', __( 'Unsupported signature algorithm (only rsa-sha256 and hs2019 are supported)', 'activitypub' ), array( 'status' => 401 ) ); | |
} |
Should the case statement test for hs2019
instead of rsa-sha-512
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@obenland I think the error message is correct, in that the declared algorithm should be either rsa-256
or hs2019
, because under hs2019
the key is meant to be derived from keyId
.
Implementers SHOULD derive the
digital signature algorithm used by an implementation from the key
metadata identified by thekeyId
rather than from this field. If
algorithm
is provided and differs from the key metadata identified
by thekeyId
, for examplersa-sha256
but an EdDSA key is
identified viakeyId
, then an implementation MUST produce an error.
Implementers should note that previous versions of thealgorithm
parameter did not use the key information to derive the digital
signature type and thus could be utilized by attackers to expose
security vulnerabilities.
— algorithm
The implementation actually derives the key from the digest header rather than the keyId
.
wordpress-activitypub/includes/class-signature.php
Lines 294 to 298 in 499bcad
$algorithm = 'sha256'; | |
$digest = explode( '=', $headers['digest'], 2 ); | |
if ( 'SHA-512' === $digest[0] ) { | |
$algorithm = 'sha512'; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
algorithm
is provided
@mediaformat Based on the doc you shared, I guess we shouldn't return an error if algorithm is not set?
What about checking if |
Fixes PHP warning when there is no
algorithm
key.Proposed changes:
algorithm
key exists before accessing it.Other information:
Testing instructions:
Changelog entry
Changelog Entry Details
Significance
Type
Message
Inbox requests that are missing an
algorithm
parameter in their signature no longer create a PHP warning.