-
Notifications
You must be signed in to change notification settings - Fork 104
/
rest-api.php
228 lines (210 loc) · 7.03 KB
/
rest-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* REST API integration for the plugin.
*
* @package optimization-detective
* @since 0.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Namespace for optimization-detective.
*
* @var string
*/
const OD_REST_API_NAMESPACE = 'optimization-detective/v1';
/**
* Route for storing a URL Metric.
*
* Note the `:store` art of the endpoint follows Google's guidance in AIP-136 for the use of the POST method in a way
* that does not strictly follow the standard usage. Namely, submitting a POST request to this endpoint will either
* create a new `od_url_metrics` post, or it will update an existing post if one already exists for the provided slug.
*
* @link https://google.aip.dev/136
* @var string
*/
const OD_URL_METRICS_ROUTE = '/url-metrics:store';
/**
* Registers endpoint for storage of URL Metric.
*
* @since 0.1.0
* @access private
*/
function od_register_endpoint(): void {
$args = array(
'slug' => array(
'type' => 'string',
'description' => __( 'An MD5 hash of the query args.', 'optimization-detective' ),
'required' => true,
'pattern' => '^[0-9a-f]{32}$',
// This is further validated via the validate_callback for the 'hmac' parameter, as it is provided as input
// with the 'url' argument to create the HMAC by the server. which then is verified to match in the REST API request.
),
'hmac' => array(
'type' => 'string',
'description' => __( 'HMAC originally computed by server required to authorize the request.', 'optimization-detective' ),
'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->get_param( 'slug' ), $request->get_param( 'url' ) ) ) {
return new WP_Error( 'invalid_hmac', __( 'URL Metrics HMAC verification failure.', 'optimization-detective' ) );
}
return true;
},
),
);
register_rest_route(
OD_REST_API_NAMESPACE,
OD_URL_METRICS_ROUTE,
array(
'methods' => 'POST',
'args' => array_merge(
$args,
rest_get_endpoint_args_for_schema( OD_Strict_URL_Metric::get_json_schema() )
),
'callback' => static function ( WP_REST_Request $request ) {
return od_handle_rest_request( $request );
},
'permission_callback' => static function () {
// Needs to be available to unauthenticated visitors.
if ( OD_Storage_Lock::is_locked() ) {
return new WP_Error(
'url_metric_storage_locked',
__( 'URL Metric storage is presently locked for the current IP.', 'optimization-detective' ),
array( 'status' => 403 )
);
}
return true;
},
)
);
}
add_action( 'rest_api_init', 'od_register_endpoint' );
/**
* Determines if the HTTP origin is an authorized one.
*
* Note that `is_allowed_http_origin()` is not used directly because the underlying `get_allowed_http_origins()` does
* not account for the URL port (although there is a to-do comment committed in core to address this). Additionally,
* the `is_allowed_http_origin()` function in core for some reason returns a string rather than a boolean.
*
* @since n.e.x.t
* @access private
*
* @see is_allowed_http_origin()
*
* @param string $origin Origin to check.
* @return bool Whether the origin is allowed.
*/
function od_is_allowed_http_origin( string $origin ): bool {
// Strip out the port number since core does not account for it yet as noted in get_allowed_http_origins().
$origin = preg_replace( '/:\d+$/', '', $origin );
return '' !== is_allowed_http_origin( $origin );
}
/**
* Handles REST API request to store metrics.
*
* @since 0.1.0
* @access private
*
* @phpstan-param WP_REST_Request<array<string, mixed>> $request
*
* @param WP_REST_Request $request Request.
* @return WP_REST_Response|WP_Error Response.
*/
function od_handle_rest_request( WP_REST_Request $request ) {
// Block cross-origin storage requests since by definition URL Metrics data can only be sourced from the frontend of the site.
$origin = $request->get_header( 'origin' );
if ( null === $origin || ! od_is_allowed_http_origin( $origin ) ) {
return new WP_Error(
'rest_cross_origin_forbidden',
__( 'Cross-origin requests are not allowed for this endpoint.', 'optimization-detective' ),
array( 'status' => 403 )
);
}
$post = OD_URL_Metrics_Post_Type::get_post( $request->get_param( 'slug' ) );
$url_metric_group_collection = new OD_URL_Metric_Group_Collection(
$post instanceof WP_Post ? OD_URL_Metrics_Post_Type::get_url_metrics_from_post( $post ) : array(),
od_get_breakpoint_max_widths(),
od_get_url_metrics_breakpoint_sample_size(),
od_get_url_metric_freshness_ttl()
);
// Block the request if URL Metrics aren't needed for the provided viewport width.
try {
$url_metric_group = $url_metric_group_collection->get_group_for_viewport_width(
$request->get_param( 'viewport' )['width']
);
} catch ( InvalidArgumentException $exception ) {
return new WP_Error( 'invalid_viewport_width', $exception->getMessage() );
}
if ( $url_metric_group->is_complete() ) {
return new WP_Error(
'url_metric_group_complete',
__( 'The URL Metric group for the provided viewport is already complete.', 'optimization-detective' ),
array( 'status' => 403 )
);
}
$data = $request->get_json_params();
if ( ! is_array( $data ) ) {
return new WP_Error(
'missing_array_json_body',
__( 'The request body is not JSON array.', 'optimization-detective' ),
array( 'status' => 400 )
);
}
OD_Storage_Lock::set_lock();
try {
// The "strict" URL Metric class is being used here to ensure additionalProperties of all objects are disallowed.
$url_metric = new OD_Strict_URL_Metric(
array_merge(
$data,
array(
// Now supply the readonly args which were omitted from the REST API params due to being `readonly`.
'timestamp' => microtime( true ),
'uuid' => wp_generate_uuid4(),
)
)
);
} catch ( OD_Data_Validation_Exception $e ) {
return new WP_Error(
'rest_invalid_param',
sprintf(
/* translators: %s is exception name */
__( 'Failed to validate URL Metric: %s', 'optimization-detective' ),
$e->getMessage()
),
array( 'status' => 400 )
);
}
// TODO: This should be changed from store_url_metric($slug, $url_metric) instead be update_post( $slug, $group_collection ). As it stands, store_url_metric() is duplicating logic here.
$result = OD_URL_Metrics_Post_Type::store_url_metric(
$request->get_param( 'slug' ),
$url_metric
);
if ( $result instanceof WP_Error ) {
return $result;
}
$post_id = $result;
/**
* Fires whenever a URL Metric was successfully stored.
*
* @since 0.7.0
*
* @param OD_URL_Metric_Store_Request_Context $context Context about the successful URL Metric collection.
*/
do_action(
'od_url_metric_stored',
new OD_URL_Metric_Store_Request_Context(
$request,
$post_id,
$url_metric_group_collection,
$url_metric_group,
$url_metric
)
);
return new WP_REST_Response(
array(
'success' => true,
)
);
}