Skip to content

Commit

Permalink
Factor in ETag when computing HMAC
Browse files Browse the repository at this point in the history
  • Loading branch information
ShyamGadde committed Nov 26, 2024
1 parent 9999eea commit 3b01ea2
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion plugins/optimization-detective/detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function od_get_detection_script( string $slug, OD_URL_Metric_Group_Collection $
'currentUrl' => $current_url,
'urlMetricSlug' => $slug,
'cachePurgePostId' => od_get_cache_purge_post_id(),
'urlMetricHMAC' => od_get_url_metrics_storage_hmac( $slug, $current_url, $cache_purge_post_id ),
'urlMetricHMAC' => od_get_url_metrics_storage_hmac( $slug, $od_etag, $current_url, $cache_purge_post_id ),
'urlMetricGroupStatuses' => array_map(
static function ( OD_URL_Metric_Group $group ): array {
return array(
Expand Down
11 changes: 6 additions & 5 deletions plugins/optimization-detective/storage/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ function od_get_url_metrics_slug( array $query_vars ): string {
*
* @see od_verify_url_metrics_storage_hmac()
* @see od_get_url_metrics_slug()
* @todo This should also include an ETag as a parameter. See <https://github.com/WordPress/performance/issues/1466>.
*
* @param string $slug Slug (hash of normalized query vars).
* @param string $etag ETag.
* @param string $url URL.
* @param int|null $cache_purge_post_id Cache purge post ID.
* @return string HMAC.
*/
function od_get_url_metrics_storage_hmac( string $slug, string $url, ?int $cache_purge_post_id = null ): string {
$action = "store_url_metric:$slug:$url:$cache_purge_post_id";
function od_get_url_metrics_storage_hmac( string $slug, string $etag, string $url, ?int $cache_purge_post_id = null ): string {
$action = "store_url_metric:$slug:$etag:$url:$cache_purge_post_id";
return wp_hash( $action, 'nonce' );
}

Expand All @@ -173,12 +173,13 @@ function od_get_url_metrics_storage_hmac( string $slug, string $url, ?int $cache
*
* @param string $hmac HMAC.
* @param string $slug Slug (hash of normalized query vars).
* @param string $etag ETag.
* @param String $url URL.
* @param int|null $cache_purge_post_id Cache purge post ID.
* @return bool Whether the HMAC is valid.
*/
function od_verify_url_metrics_storage_hmac( string $hmac, string $slug, string $url, ?int $cache_purge_post_id = null ): bool {
return hash_equals( od_get_url_metrics_storage_hmac( $slug, $url, $cache_purge_post_id ), $hmac );
function od_verify_url_metrics_storage_hmac( string $hmac, string $slug, string $etag, string $url, ?int $cache_purge_post_id = null ): bool {
return hash_equals( od_get_url_metrics_storage_hmac( $slug, $etag, $url, $cache_purge_post_id ), $hmac );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion plugins/optimization-detective/storage/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function od_register_endpoint(): void {
'required' => true,
'pattern' => '^[0-9a-f]+$',
'validate_callback' => static function ( string $hmac, WP_REST_Request $request ) {
if ( ! od_verify_url_metrics_storage_hmac( $hmac, $request['slug'], $request['url'], $request['cache_purge_post_id'] ?? null ) ) {
if ( ! od_verify_url_metrics_storage_hmac( $hmac, $request['slug'], $request['eTag'], $request['url'], $request['cache_purge_post_id'] ?? null ) ) {
return new WP_Error( 'invalid_hmac', __( 'URL Metrics HMAC verification failure.', 'optimization-detective' ) );
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function ( $params ) {
'hmac' => 'not even a hash',
),
'invalid_hmac' => array(
'hmac' => od_get_url_metrics_storage_hmac( od_get_url_metrics_slug( array( 'different' => 'query vars' ) ), home_url( '/' ) ),
'hmac' => od_get_url_metrics_storage_hmac( od_get_url_metrics_slug( array( 'different' => 'query vars' ) ), '', home_url( '/' ) ),
),
'invalid_hmac_with_queried_object' => array(
'hmac' => od_get_url_metrics_storage_hmac( od_get_url_metrics_slug( array() ), home_url( '/' ), 1 ),
Expand Down Expand Up @@ -672,7 +672,7 @@ private function get_valid_params( array $extras = array() ): array {
$data = array_merge(
array(
'slug' => $slug,
'hmac' => od_get_url_metrics_storage_hmac( $slug, $data['url'] ),
'hmac' => od_get_url_metrics_storage_hmac( $slug, '', $data['url'] ),
),
$data
);
Expand Down

0 comments on commit 3b01ea2

Please sign in to comment.