Skip to content

Commit 738ea95

Browse files
authored
Signature: Use lowercase key in Content-Digest (#1901)
1 parent 8274e99 commit 738ea95

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

includes/signature/class-http-message-signature.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
*/
2323
class Http_Message_Signature implements Signature_Standard {
2424

25+
/**
26+
* Digest algorithms.
27+
*
28+
* @var string[]
29+
*/
30+
private $digest_algorithms = array(
31+
'sha-256' => 'sha256',
32+
'sha-512' => 'sha512',
33+
);
34+
2535
/**
2636
* Generate RFC-9421 compliant Signature-Input and Signature headers for an outgoing HTTP request.
2737
*
@@ -105,7 +115,7 @@ public function verify( array $headers, $body = null ) {
105115
* @return string The digest.
106116
*/
107117
public function generate_digest( $body ) {
108-
return 'SHA-256=:' . \base64_encode( \hash( 'sha256', $body, true ) ) . ':';
118+
return 'sha-256=:' . \base64_encode( \hash( 'sha256', $body, true ) ) . ':';
109119
}
110120

111121
/**
@@ -216,16 +226,11 @@ private function verify_content_digest( $headers, $body ) {
216226
if ( \preg_match( '/^([a-z0-9-]+)=:(.+):$/i', $digest, $matches ) ) {
217227
list( , $alg, $encoded ) = $matches;
218228

219-
$map = array(
220-
'SHA-256' => 'sha256',
221-
'SHA-512' => 'sha512',
222-
);
223-
224-
if ( ! isset( $map[ $alg ] ) ) {
225-
return new \WP_Error( 'unsupported_digest', 'WordPress supports SHA-256 and SHA-512 in Digest header. Offered algorithm: ' . $alg );
229+
if ( ! isset( $this->digest_algorithms[ $alg ] ) ) {
230+
return new \WP_Error( 'unsupported_digest', 'WordPress supports sha-256 and sha-512 in Digest header. Offered algorithm: ' . $alg );
226231
}
227232

228-
if ( \hash_equals( $encoded, \base64_encode( \hash( $map[ $alg ], $body, true ) ) ) ) {
233+
if ( \hash_equals( $encoded, \base64_encode( \hash( $this->digest_algorithms[ $alg ], $body, true ) ) ) ) {
229234
return true;
230235
}
231236
}

tests/includes/class-test-signature.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ function () use ( $keys ) {
571571
$body = '{"type":"Create","actor":"https://example.org/author/admin","object":{"type":"Note","content":"Test content."}}';
572572

573573
// Generate a digest for the body.
574-
$digest = 'SHA-256=:' . \base64_encode( \hash( 'sha256', $body, true ) ) . ':';
574+
$digest = 'sha-256=:' . \base64_encode( \hash( 'sha256', $body, true ) ) . ':';
575575

576576
// Create a date for the request.
577577
$date = \gmdate( 'D, d M Y H:i:s T' );

0 commit comments

Comments
 (0)