Skip to content

Commit

Permalink
Validate route params before making request (#8901)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakpathania authored Jun 7, 2024
1 parent a719ace commit bbdbafb
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 26 deletions.
4 changes: 4 additions & 0 deletions changelog/update-route-param-validation
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: dev

Add validation for path variables.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<payment_intent_id>\w+)',
'/' . $this->rest_base . '/(?P<payment_intent_id>(ch|pi|py)_[A-Za-z0-9]+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_authorization' ],
Expand Down
4 changes: 2 additions & 2 deletions includes/admin/class-wc-rest-payments-charges-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class WC_REST_Payments_Charges_Controller extends WC_Payments_REST_Controller {
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<charge_id>\w+)',
'/' . $this->rest_base . '/(?P<charge_id>ch_[A-Za-z0-9]+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_charge' ],
Expand All @@ -37,7 +37,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/order/(?P<order_id>\w+)',
'/' . $this->rest_base . '/order/(?P<order_id>[A-Za-z0-9_\-]+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'generate_charge_from_order' ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<customer_id>\w+)/payment_methods',
'/' . $this->rest_base . '/(?P<customer_id>[A-Za-z0-9_\-]+)/payment_methods',
[
[
'methods' => WP_REST_Server::READABLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<deposit_id>\w+)',
'/' . $this->rest_base . '/(?P<deposit_id>[A-Za-z0-9_\-]+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_deposit' ],
Expand Down
6 changes: 3 additions & 3 deletions includes/admin/class-wc-rest-payments-disputes-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<dispute_id>\w+)',
'/' . $this->rest_base . '/(?P<dispute_id>(dp|dispute)_[A-Za-z0-9]+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_dispute' ],
Expand All @@ -63,7 +63,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<dispute_id>\w+)',
'/' . $this->rest_base . '/(?P<dispute_id>(dp|dispute)_[A-Za-z0-9]+)',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'update_dispute' ],
Expand All @@ -72,7 +72,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<dispute_id>\w+)/close',
'/' . $this->rest_base . '/(?P<dispute_id>(dp|dispute)_[A-Za-z0-9]+)/close',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'close_dispute' ],
Expand Down
6 changes: 3 additions & 3 deletions includes/admin/class-wc-rest-payments-files-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function register_routes() {

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<file_id>\w+)/details',
'/' . $this->rest_base . '/(?P<file_id>[A-Za-z0-9_\-]+)/details',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_file_detail' ],
Expand All @@ -45,7 +45,7 @@ public function register_routes() {

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<file_id>\w+)/content',
'/' . $this->rest_base . '/(?P<file_id>[A-Za-z0-9_\-]+)/content',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_file_content' ],
Expand All @@ -55,7 +55,7 @@ public function register_routes() {

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<file_id>\w+)',
'/' . $this->rest_base . '/(?P<file_id>[A-Za-z0-9_\-]+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_file' ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class WC_REST_Payments_Fraud_Outcomes_Controller extends WC_Payments_REST_Contro
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>\w+)/latest',
'/' . $this->rest_base . '/(?P<id>[A-Za-z0-9_\-]+)/latest',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_latest_fraud_outcome' ],
Expand Down
8 changes: 4 additions & 4 deletions includes/admin/class-wc-rest-payments-orders-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct( WC_Payments_API_Client $api_client, WC_Payment_Gate
public function register_routes() {
register_rest_route(
$this->namespace,
$this->rest_base . '/(?P<order_id>\w+)/capture_terminal_payment',
$this->rest_base . '/(?P<order_id>[A-Za-z0-9_\-]+)/capture_terminal_payment',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'capture_terminal_payment' ],
Expand All @@ -82,7 +82,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
$this->rest_base . '/(?P<order_id>\w+)/capture_authorization',
$this->rest_base . '/(?P<order_id>[A-Za-z0-9_\-]+)/capture_authorization',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'capture_authorization' ],
Expand All @@ -96,7 +96,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
$this->rest_base . '/(?P<order_id>\w+)/cancel_authorization',
$this->rest_base . '/(?P<order_id>[A-Za-z0-9_\-]+)/cancel_authorization',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'cancel_authorization' ],
Expand All @@ -110,7 +110,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
$this->rest_base . '/(?P<order_id>\w+)/create_terminal_intent',
$this->rest_base . '/(?P<order_id>[A-Za-z0-9_\-]+)/create_terminal_intent',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'create_terminal_intent' ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class WC_REST_Payments_Payment_Intents_Controller extends WC_Payments_REST_Contr
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<payment_intent_id>\w+)',
'/' . $this->rest_base . '/(?P<payment_intent_id>(ch|pi|py)_[A-Za-z0-9]+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_payment_intent' ],
Expand Down
4 changes: 2 additions & 2 deletions includes/admin/class-wc-rest-payments-reader-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function register_routes() {

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/charges/(?P<transaction_id>\w+)',
'/' . $this->rest_base . '/charges/(?P<transaction_id>[A-Za-z0-9_\-]+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_summary' ],
Expand All @@ -132,7 +132,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/receipts/(?P<payment_intent_id>\w+)',
'/' . $this->rest_base . '/receipts/(?P<payment_intent_id>(ch|pi|py)_[A-Za-z0-9]+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'generate_print_receipt' ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<location_id>\w+)',
'/' . $this->rest_base . '/(?P<location_id>[A-Za-z0-9_\-]+)',
[
'methods' => WP_REST_Server::DELETABLE,
'callback' => [ $this, 'delete_location' ],
Expand All @@ -47,7 +47,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<location_id>\w+)',
'/' . $this->rest_base . '/(?P<location_id>[A-Za-z0-9_\-]+)',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'update_location' ],
Expand All @@ -66,7 +66,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<location_id>\w+)',
'/' . $this->rest_base . '/(?P<location_id>[A-Za-z0-9_\-]+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_location' ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class WC_REST_Payments_Timeline_Controller extends WC_Payments_REST_Controller {
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<intention_id>\w+)',
'/' . $this->rest_base . '/(?P<intention_id>(ch|pi|py)_[A-Za-z0-9]+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_timeline' ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<transaction_id>\w+)',
'/' . $this->rest_base . '/(?P<transaction_id>[A-Za-z0-9_\-]+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_transaction' ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>\w+)',
'/' . $this->rest_base . '/(?P<id>[A-Za-z0-9_\-]+)',
[
[
'methods' => WP_REST_Server::READABLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function register_routes() {
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>\w+)',
'/' . $this->rest_base . '/(?P<id>[A-Za-z0-9_\-]+)',
[
[
'methods' => WP_REST_Server::READABLE,
Expand Down
26 changes: 26 additions & 0 deletions includes/wc-payment-api/class-wc-payments-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,17 @@ public function get_disputes( array $filters = [] ) {
*
* @param string $dispute_id id of requested dispute.
* @return array dispute object.
* @throws API_Exception - Exception thrown in case route validation fails.
*/
public function get_dispute( $dispute_id ) {
if ( ! preg_match( '/(dp|dispute)_[A-Za-z0-9]+/', $dispute_id ) ) {
throw new API_Exception(
__( 'Route param validation failed.', 'woocommerce-payments' ),
'wcpay_route_validation_failure',
400
);
}

$dispute = $this->request( [], self::DISPUTES_API . '/' . $dispute_id, self::GET );

if ( is_wp_error( $dispute ) ) {
Expand Down Expand Up @@ -726,8 +735,17 @@ public function create_token( $request ) {
* @return array
*
* @throws Exception - Exception thrown on request failure.
* @throws API_Exception - Exception thrown in case route validation fails.
*/
public function get_timeline( $id ) {
if ( ! preg_match( '/(ch|pi|py)_[A-Za-z0-9]+/', $id ) ) {
throw new API_Exception(
__( 'Route param validation failed.', 'woocommerce-payments' ),
'wcpay_route_validation_failure',
400
);
}

$timeline = $this->request( [], self::TIMELINE_API . '/' . $id, self::GET );

$has_fraud_outcome_event = false;
Expand Down Expand Up @@ -1199,6 +1217,14 @@ public function update_charge( string $charge_id, array $data = [] ) {
* @throws API_Exception
*/
public function get_charge( string $charge_id ) {
if ( ! preg_match( '/(ch|pi|py)_[A-Za-z0-9]+/', $charge_id ) ) {
throw new API_Exception(
__( 'Route param validation failed.', 'woocommerce-payments' ),
'wcpay_route_validation_failure',
400
);
}

return $this->request(
[],
self::CHARGES_API . '/' . $charge_id,
Expand Down

0 comments on commit bbdbafb

Please sign in to comment.