@@ -162,12 +162,10 @@ public static function verify_http_signature( $request ) {
162162 $ route = '/ ' . rest_get_url_prefix () . '/ ' . ltrim ( $ request ->get_route (), '/ ' );
163163 }
164164 $ headers = $ request ->get_headers ();
165- $ actor = isset ( json_decode ( $ request ->get_body () )->actor ) ? json_decode ( $ request ->get_body () )->actor : '' ;
166165 $ headers ['(request-target) ' ][0 ] = strtolower ( $ request ->get_method () ) . ' ' . $ route ;
167166 } else {
168167 $ request = self ::format_server_request ( $ request );
169168 $ headers = $ request ['headers ' ]; // $_SERVER array
170- $ actor = null ;
171169 $ headers ['(request-target) ' ][0 ] = strtolower ( $ headers ['request_method ' ][0 ] ) . ' ' . $ headers ['request_uri ' ][0 ];
172170 }
173171
@@ -176,9 +174,9 @@ public static function verify_http_signature( $request ) {
176174 }
177175
178176 if ( array_key_exists ( 'signature ' , $ headers ) ) {
179- $ signature_block = self ::parse_signature_header ( $ headers ['signature ' ] );
177+ $ signature_block = self ::parse_signature_header ( $ headers ['signature ' ][ 0 ] );
180178 } elseif ( array_key_exists ( 'authorization ' , $ headers ) ) {
181- $ signature_block = self ::parse_signature_header ( $ headers ['authorization ' ] );
179+ $ signature_block = self ::parse_signature_header ( $ headers ['authorization ' ][ 0 ] );
182180 }
183181
184182 if ( ! isset ( $ signature_block ) || ! $ signature_block ) {
@@ -217,11 +215,8 @@ public static function verify_http_signature( $request ) {
217215 }
218216 }
219217
220- if ( $ actor ) {
221- $ public_key = self ::get_remote_key ( $ actor );
222- } else {
223- $ public_key = self ::get_remote_key ( $ signature_block ['keyId ' ] );
224- }
218+ $ public_key = self ::get_remote_key ( $ signature_block ['keyId ' ] );
219+
225220 if ( \is_wp_error ( $ public_key ) ) {
226221 return $ public_key ;
227222 }
@@ -242,7 +237,7 @@ public static function verify_http_signature( $request ) {
242237 * @return string The public key.
243238 */
244239 public static function get_remote_key ( $ key_id ) { // phpcs:ignore
245- $ actor = get_remote_metadata_by_actor ( strtok ( strip_fragment_from_url ( $ key_id ), ' ? ' ) ); // phpcs:ignore
240+ $ actor = get_remote_metadata_by_actor ( strip_fragment_from_url ( $ key_id ) ); // phpcs:ignore
246241 if ( \is_wp_error ( $ actor ) ) {
247242 return $ actor ;
248243 }
@@ -274,32 +269,31 @@ public static function get_signature_algorithm( $signature_block ) {
274269 /**
275270 * Parses the Signature header
276271 *
277- * @param array $header The signature header.
272+ * @param string $signature The signature header.
278273 *
279274 * @return array signature parts
280275 */
281- public static function parse_signature_header ( $ header ) {
282- $ parsed_header = array ();
283- $ matches = array ();
284- $ h_string = \implode ( ', ' , (array ) $ header [0 ] );
276+ public static function parse_signature_header ( $ signature ) {
277+ $ parsed_header = array ();
278+ $ matches = array ();
285279
286- if ( \preg_match ( '/keyId="(.*?)"/ism ' , $ h_string , $ matches ) ) {
287- $ parsed_header ['keyId ' ] = $ matches [1 ];
280+ if ( \preg_match ( '/keyId="(.*?)"/ism ' , $ signature , $ matches ) ) {
281+ $ parsed_header ['keyId ' ] = trim ( $ matches [1 ] ) ;
288282 }
289- if ( \preg_match ( '/created=([0-9]*)/ism ' , $ h_string , $ matches ) ) {
290- $ parsed_header ['(created) ' ] = $ matches [1 ];
283+ if ( \preg_match ( '/created=([0-9]*)/ism ' , $ signature , $ matches ) ) {
284+ $ parsed_header ['(created) ' ] = trim ( $ matches [1 ] ) ;
291285 }
292- if ( \preg_match ( '/expires=([0-9]*)/ism ' , $ h_string , $ matches ) ) {
293- $ parsed_header ['(expires) ' ] = $ matches [1 ];
286+ if ( \preg_match ( '/expires=([0-9]*)/ism ' , $ signature , $ matches ) ) {
287+ $ parsed_header ['(expires) ' ] = trim ( $ matches [1 ] ) ;
294288 }
295- if ( \preg_match ( '/algorithm="(.*?)"/ism ' , $ h_string , $ matches ) ) {
296- $ parsed_header ['algorithm ' ] = $ matches [1 ];
289+ if ( \preg_match ( '/algorithm="(.*?)"/ism ' , $ signature , $ matches ) ) {
290+ $ parsed_header ['algorithm ' ] = trim ( $ matches [1 ] ) ;
297291 }
298- if ( \preg_match ( '/headers="(.*?)"/ism ' , $ h_string , $ matches ) ) {
299- $ parsed_header ['headers ' ] = \explode ( ' ' , $ matches [1 ] );
292+ if ( \preg_match ( '/headers="(.*?)"/ism ' , $ signature , $ matches ) ) {
293+ $ parsed_header ['headers ' ] = \explode ( ' ' , trim ( $ matches [1 ] ) );
300294 }
301- if ( \preg_match ( '/signature="(.*?)"/ism ' , $ h_string , $ matches ) ) {
302- $ parsed_header ['signature ' ] = \base64_decode ( preg_replace ( '/\s+/ ' , '' , $ matches [1 ] ) ); // phpcs:ignore
295+ if ( \preg_match ( '/signature="(.*?)"/ism ' , $ signature , $ matches ) ) {
296+ $ parsed_header ['signature ' ] = \base64_decode ( preg_replace ( '/\s+/ ' , '' , trim ( $ matches [1 ] ) ) ); // phpcs:ignore
303297 }
304298
305299 if ( ( $ parsed_header ['signature ' ] ) && ( $ parsed_header ['algorithm ' ] ) && ( ! $ parsed_header ['headers ' ] ) ) {
@@ -312,7 +306,7 @@ public static function parse_signature_header( $header ) {
312306 /**
313307 * Gets the header data from the included pseudo headers
314308 *
315- * @param array $signed_headers
309+ * @param array $signed_headers The signed headers.
316310 * @param array $signature_block (pseudo-headers)
317311 * @param array $headers (http headers)
318312 *
0 commit comments