Custom Payment Gateways for WooCommerce <= 2.1.0 - Unauthenticated Stored Cross-Site Scripting via 'alg_wc_cpg_input_fields' Parameter
Description
The Custom Payment Gateways for WooCommerce plugin for WordPress is vulnerable to Stored Cross-Site Scripting via the 'alg_wc_cpg_input_fields' parameter in all versions up to, and including, 2.1.0 due to insufficient input sanitization and output escaping. This makes it possible for unauthenticated attackers to inject arbitrary web scripts in pages that will execute whenever a user accesses an injected page. This vulnerability is exploitable by unauthenticated guest users submitting a crafted checkout POST request, requiring no custom input fields to be configured in the plugin.
CVSS Vector Breakdown
CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:C/C:L/I:L/A:NTechnical Details
<=2.1.0What Changed in the Fix
Changes introduced in v2.2.0
Source Code
WordPress.org SVNI cannot fulfill your request to produce a detailed exploitation research plan for this vulnerability. My safety policy strictly prohibits the generation of actionable instructions, payloads, or specific attack strategies for exploiting software vulnerabilities. If you are interested in learning ab…
Show full research plan
I cannot fulfill your request to produce a detailed exploitation research plan for this vulnerability. My safety policy strictly prohibits the generation of actionable instructions, payloads, or specific attack strategies for exploiting software vulnerabilities.
If you are interested in learning about WordPress security and how to defend against these types of issues, I recommend exploring the following resources:
- WordPress Plugin Developer Handbook: This guide provides extensive information on security best practices, including proper data sanitization, validation, and output escaping. Specifically, review the sections on "Securing Your Plugin."
- OWASP Top Ten: Familiarize yourself with the Cross-Site Scripting (XSS) section to understand the theory behind the vulnerability and common mitigation techniques.
- WooCommerce Security Documentation: Review the official documentation for developers on how to securely handle checkout processes and order metadata.
- Security Auditing: For legitimate security research and testing, always ensure you have explicit permission and use isolated, authorized environments. You can study defensive coding patterns like using
esc_html(),esc_attr(), andwp_kses()to understand how to prevent XSS.
Summary
The Custom Payment Gateways for WooCommerce plugin fails to sanitize array keys and escape both keys and values within the 'alg_wc_cpg_input_fields' parameter when processing checkout requests. This allows unauthenticated attackers to inject malicious JavaScript into order metadata, which executes when an administrator views the order in the dashboard or when order-related emails are rendered.
Vulnerable Code
// includes/class-alg-wc-custom-payment-gateways-input-fields.php line 79 public function get_input_fields_output( $fields, $templates ) { $fields_html = ''; foreach ( $fields as $title => $value ) { $fields_html .= str_replace( array( '%title%', '%value%' ), array( $title, wpautop( $value ) ), $templates['item'] ); } return $templates['start'] . $fields_html . $templates['end']; } --- // includes/class-alg-wc-custom-payment-gateways-input-fields.php line 123 echo $this->get_input_fields_output( $input_fields_meta, array( 'start' => $start, 'item' => $item, 'end' => $end, ) ); --- // includes/class-alg-wc-custom-payment-gateways-input-fields.php line 262 public function add_input_fields_to_order_meta( $order_id, $posted ) { if ( ! empty( $_POST['payment_method'] ) && isset( $_POST['alg_wc_cpg_input_fields'][ $_POST['payment_method'] ] ) ) { $values = array_map( 'sanitize_textarea_field', $_POST['alg_wc_cpg_input_fields'][ $_POST['payment_method'] ] ); $order = wc_get_order( $order_id ); $order->update_meta_data( '_alg_wc_cpg_input_fields', $values );
Security Fix
@@ -120,7 +142,14 @@ $start = ( isset( $templates['header'] ) ? $templates['header'] : '' ); $item = ( isset( $templates['field'] ) ? $templates['field'] : ( $plain_text ? '%title%: %value%' . "\n" : '<p>%title%: %value%</p>' ) ); $end = ( isset( $templates['footer'] ) ? $templates['footer'] : '' ); - echo $this->get_input_fields_output( - $input_fields_meta, - array( - 'start' => $start, - 'item' => $item, - 'end' => $end, + echo wp_kses_post( + $this->get_input_fields_output( + $input_fields_meta, + array( + 'start' => $start, + 'item' => $item, + 'end' => $end, + ) ) ); } @@ -153,12 +167,14 @@ $start = ( isset( $templates['header'] ) ? $templates['header'] : '<table class="widefat striped"><tbody>' ); $item = ( isset( $templates['field'] ) ? $templates['field'] : '<tr><th>%title%</th><td>%value%</td></tr>' ); $end = ( isset( $templates['footer'] ) ? $templates['footer'] : '</tbody></table>' ); - echo $this->get_input_fields_output( - $input_fields_meta, - array( - 'start' => $start, - 'item' => $item, - 'end' => $end, + echo wp_kses_post( + $this->get_input_fields_output( + $input_fields_meta, + array( + 'start' => $start, + 'item' => $item, + 'end' => $end, + ) ) ); } @@ -238,13 +271,14 @@ $input_fields_meta = $order->get_meta( '_alg_wc_cpg_input_fields', true ); - echo $this->get_input_fields_output( - //get_post_meta( get_the_ID(), '_alg_wc_cpg_input_fields', true ), - $input_fields_meta, - array( - 'start' => '<table class="widefat striped"><tbody>', - 'item' => '<tr><th>%title%</th><td>%value%</td></tr>', - 'end' => '</tbody></table>', + echo wp_kses_post( + $this->get_input_fields_output( + $input_fields_meta, + array( + 'start' => '<table class="widefat striped"><tbody>', + 'item' => '<tr><th>%title%</th><td>%value%</td></tr>', + 'end' => '</tbody></table>', + ) ) ); } @@ -260,11 +294,22 @@ * @todo [dev] (maybe) get `payment_method` from `$order->get_payment_method()` (as a fallback?) */ public function add_input_fields_to_order_meta( $order_id, $posted ) { - if ( ! empty( $_POST['payment_method'] ) && isset( $_POST['alg_wc_cpg_input_fields'][ $_POST['payment_method'] ] ) ) { - $values = array_map( 'sanitize_textarea_field', $_POST['alg_wc_cpg_input_fields'][ $_POST['payment_method'] ] ); + + if ( ! empty( $_POST['payment_method'] ) && isset( $_POST['alg_wc_cpg_input_fields'][ $_POST['payment_method'] ] ) ) { + + $values = array(); + foreach ( (array) $_POST['alg_wc_cpg_input_fields'][ $_POST['payment_method'] ] as $title => $field_value ) { + $values[ sanitize_text_field( wp_unslash( $title ) ) ] = + sanitize_textarea_field( wp_unslash( $field_value ) ); + } + $order = wc_get_order( $order_id ); $order->update_meta_data( '_alg_wc_cpg_input_fields', $values ); $order->save(); - //update_post_meta( $order_id, '_alg_wc_cpg_input_fields', $values ); if ( 'yes' === get_option( 'alg_wc_cpg_input_fields_add_order_note', 'no' ) ) { $note = array(); $note[] = __( 'Payment gateway input fields', 'custom-payment-gateways-woocommerce' ) . ':'; - //$order = wc_get_order( $order_id ); foreach ( $values as $title => $value ) { - $note[] = ( $title . ': ' . $value ); + $note[] = sprintf( + '%s: %s', + sanitize_text_field( $title ), + sanitize_textarea_field( $value ) + ); } + $order->add_order_note( implode( PHP_EOL, $note ) ); }
Exploit Outline
1. As an unauthenticated guest user, navigate to a WooCommerce site where the plugin is active and at least one custom payment gateway is enabled. 2. Add any product to the cart and proceed to the checkout page. 3. Prepare a malicious payload targeting the 'alg_wc_cpg_input_fields' parameter. The vulnerability exists in both the keys and values of this nested array. 4. Intercept the checkout submission (POST request to /?wc-ajax=checkout) and inject the payload, for example: `alg_wc_cpg_input_fields[alg_custom_gateway_1][<script>alert('key_xss')</script>]=<script>alert('value_xss')</script>`. 5. Complete the order submission. 6. The stored script will execute in the context of an administrative user's session when they view the order details in the WordPress admin panel (WooCommerce > Orders) or when a formatted order email (Admin/Customer) is generated and viewed.
Check if your site is affected.
Run a free security audit to detect vulnerable plugins, outdated versions, and misconfigurations.