Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/eu-withdrawal-button-for-woocommerce/eu-withdrawal-button.php
+++ b/eu-withdrawal-button-for-woocommerce/eu-withdrawal-button.php
@@ -3,7 +3,7 @@
* Plugin Name: EU Withdrawal Button for WooCommerce
* Plugin URI: https://riin.eu/withdrawal-plugin
* Description: Helps WooCommerce store owners implement the withdrawal flow described in EU Directive 2023/2673.
- * Version: 2.2.0
+ * Version: 2.2.1
* Author: Riin Agency
* Author URI: https://riin.eu
* License: GPL v2 or later
@@ -24,7 +24,7 @@
/**
* Plugin constants.
*/
-define( 'RA_EUWB_VERSION', '2.2.0' );
+define( 'RA_EUWB_VERSION', '2.2.1' );
define( 'RA_EUWB_PLUGIN_FILE', __FILE__ );
define( 'RA_EUWB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'RA_EUWB_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
@@ -218,6 +218,15 @@
require_once RA_EUWB_PLUGIN_DIR . 'includes/class-ra-euwb-activator.php';
RA_EUWB_Activator::activate();
+
+ // 2.2.1: re-sanitise customer_name / reason stored by earlier versions and
+ // neutralise angle brackets in the plugin's own order notes (security fix).
+ if ( version_compare( $stored, '2.2.1', '<' ) ) {
+ require_once RA_EUWB_PLUGIN_DIR . 'includes/class-ra-euwb-withdrawal.php';
+ if ( method_exists( 'RA_EUWB_Withdrawal', 'scrub_legacy_input' ) ) {
+ RA_EUWB_Withdrawal::scrub_legacy_input();
+ }
+ }
}
/**
--- a/eu-withdrawal-button-for-woocommerce/includes/class-ra-euwb-withdrawal.php
+++ b/eu-withdrawal-button-for-woocommerce/includes/class-ra-euwb-withdrawal.php
@@ -197,9 +197,9 @@
$order_number = sanitize_text_field( wp_unslash( $_POST['order_number'] ?? '' ) );
$customer_email = sanitize_email( wp_unslash( $_POST['customer_email'] ?? '' ) );
- $customer_name = sanitize_text_field( wp_unslash( $_POST['customer_name'] ?? '' ) );
- $reason = sanitize_text_field( wp_unslash( $_POST['reason'] ?? '' ) );
- $preferred_contact = sanitize_text_field( wp_unslash( $_POST['preferred_contact'] ?? 'email' ) );
+ $customer_name = self::sanitize_customer_name( wp_unslash( $_POST['customer_name'] ?? '' ) );
+ $reason = self::sanitize_reason( wp_unslash( $_POST['reason'] ?? '' ) );
+ $preferred_contact = self::sanitize_preferred_contact( wp_unslash( $_POST['preferred_contact'] ?? 'email' ) );
$iban_raw = sanitize_text_field( wp_unslash( $_POST['iban'] ?? '' ) );
$iban = $this->sanitize_iban( $iban_raw );
// $items_raw is a JSON string that sanitize_withdrawn_items() decodes and validates
@@ -303,19 +303,21 @@
) );
}
- // Add order note.
+ // Add order note. Order notes are rendered with wp_kses_post() in the
+ // WooCommerce admin, so every customer-controlled value is escaped here
+ // (the note is HTML context, not plain text).
$reason_text = $reason ? sprintf(
/* translators: %s: withdrawal reason */
__( ' Reason: %s.', 'eu-withdrawal-button-for-woocommerce' ),
- $this->get_reason_label( $reason )
+ esc_html( $this->get_reason_label( $reason ) )
) : '';
$order->add_order_note(
sprintf(
/* translators: 1: customer name, 2: customer email, 3: reason text */
__( 'EU Withdrawal request submitted by %1$s (%2$s).%3$s', 'eu-withdrawal-button-for-woocommerce' ),
- $customer_name,
- $customer_email,
+ esc_html( $customer_name ),
+ esc_html( $customer_email ),
$reason_text
)
);
@@ -477,10 +479,10 @@
$ra_euwb_table,
array(
'order_id' => absint( $data['order_id'] ),
- 'customer_name' => sanitize_text_field( $data['customer_name'] ),
+ 'customer_name' => self::sanitize_customer_name( $data['customer_name'] ),
'customer_email' => sanitize_email( $data['customer_email'] ),
- 'reason' => sanitize_text_field( $data['reason'] ),
- 'preferred_contact' => sanitize_text_field( $data['preferred_contact'] ),
+ 'reason' => self::sanitize_reason( $data['reason'] ),
+ 'preferred_contact' => self::sanitize_preferred_contact( $data['preferred_contact'] ),
'status' => 'confirmed',
'withdrawn_items' => isset( $data['withdrawn_items'] ) ? $data['withdrawn_items'] : null,
'iban' => isset( $data['iban'] ) ? $data['iban'] : null,
@@ -739,10 +741,10 @@
sprintf(
/* translators: 1: admin name, 2: date/time, 3: customer name, 4: reason */
__( 'EU Withdrawal closed by %1$s on %2$s. NOTE: Refund must be issued manually via the Orders admin if applicable. Customer: %3$s. Reason: %4$s.', 'eu-withdrawal-button-for-woocommerce' ),
- $actor,
- $timestamp,
- $ra_euwb_withdrawal->customer_name,
- $ra_euwb_withdrawal->reason ? ucfirst( str_replace( '_', ' ', $ra_euwb_withdrawal->reason ) ) : __( 'Not specified', 'eu-withdrawal-button-for-woocommerce' )
+ esc_html( $actor ),
+ esc_html( $timestamp ),
+ esc_html( $ra_euwb_withdrawal->customer_name ),
+ esc_html( $this->get_reason_label( $ra_euwb_withdrawal->reason ) ?: __( 'Not specified', 'eu-withdrawal-button-for-woocommerce' ) )
)
);
@@ -977,7 +979,31 @@
* @return string
*/
private function get_reason_label( $reason ) {
- $reasons = array(
+ $reasons = self::get_reason_labels();
+
+ // Unknown keys are never echoed back (they are customer-controlled input).
+ return $reasons[ $reason ] ?? '';
+ }
+
+ /**
+ * The closed list of reason keys the form offers (no translation calls, so
+ * it is safe to use before `init`, e.g. in the upgrade routine).
+ *
+ * @since 2.2.1
+ * @return string[]
+ */
+ public static function get_reason_keys() {
+ return array( 'changed_mind', 'found_better_price', 'ordered_by_mistake', 'delivery_too_long', 'product_not_as_expected', 'other' );
+ }
+
+ /**
+ * The closed list of withdrawal reasons the form offers.
+ *
+ * @since 2.2.1
+ * @return array<string,string> key => translated label.
+ */
+ public static function get_reason_labels() {
+ return array(
'changed_mind' => __( 'Changed my mind', 'eu-withdrawal-button-for-woocommerce' ),
'found_better_price' => __( 'Found a better price elsewhere', 'eu-withdrawal-button-for-woocommerce' ),
'ordered_by_mistake' => __( 'Ordered by mistake', 'eu-withdrawal-button-for-woocommerce' ),
@@ -985,7 +1011,135 @@
'product_not_as_expected' => __( 'Product not as expected', 'eu-withdrawal-button-for-woocommerce' ),
'other' => __( 'Other', 'eu-withdrawal-button-for-woocommerce' ),
);
+ }
+
+ /**
+ * Sanitise a customer name for storage.
+ *
+ * A name is plain text. Anything that looks like markup — tags, angle
+ * brackets, or HTML entities that would decode to them — is removed, so the
+ * stored value can never become markup when a consumer later runs it through
+ * wp_kses_post() (WooCommerce order notes do). Length is capped to the DB
+ * column.
+ *
+ * @since 2.2.1
+ * @param string $name Raw (unslashed) input.
+ * @return string
+ */
+ public static function sanitize_customer_name( $name ) {
+ $name = (string) $name;
+ // Decode entities (repeatedly, to defeat double encoding) so "<" or
+ // "<" cannot survive as a latent "<". Literal ampersands in a name
+ // are kept; every consumer escapes on output.
+ for ( $i = 0; $i < 5; $i++ ) {
+ $decoded = html_entity_decode( $name, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
+ if ( $decoded === $name ) {
+ break;
+ }
+ $name = $decoded;
+ }
+ $name = wp_strip_all_tags( $name, true );
+ $name = str_replace( array( '<', '>' ), '', $name );
+ $name = sanitize_text_field( $name );
+ if ( function_exists( 'mb_substr' ) ) {
+ $name = mb_substr( $name, 0, 255 );
+ } else {
+ $name = substr( $name, 0, 255 );
+ }
+ return trim( $name );
+ }
+
+ /**
+ * Restrict the reason to the closed list offered by the form.
+ *
+ * @since 2.2.1
+ * @param string $reason Raw input.
+ * @return string A known reason key, or '' when none / unknown.
+ */
+ public static function sanitize_reason( $reason ) {
+ $reason = sanitize_key( (string) $reason );
+ return in_array( $reason, self::get_reason_keys(), true ) ? $reason : '';
+ }
- return $reasons[ $reason ] ?? $reason;
+ /**
+ * Restrict the preferred contact channel to the values the form offers.
+ *
+ * @since 2.2.1
+ * @param string $contact Raw input.
+ * @return string 'email' or 'phone'.
+ */
+ public static function sanitize_preferred_contact( $contact ) {
+ $contact = sanitize_key( (string) $contact );
+ return in_array( $contact, array( 'email', 'phone' ), true ) ? $contact : 'email';
+ }
+
+ /**
+ * One-off cleanup for rows and order notes written before 2.2.1.
+ *
+ * Versions up to 2.2.0 stored `customer_name` / `reason` with
+ * sanitize_text_field() only, which keeps constructs such as "< div" that
+ * wp_kses_post() later re-normalises into real markup on the order edit
+ * screen. This re-applies the 2.2.1 rules to every stored row and escapes
+ * the plugin's own submission notes that still carry angle brackets.
+ * Idempotent; called from the version-upgrade routine.
+ *
+ * @since 2.2.1
+ * @return void
+ */
+ public static function scrub_legacy_input() {
+ global $wpdb;
+
+ $ra_euwb_table = $wpdb->prefix . 'ewb_withdrawals';
+ $allowed = self::get_reason_keys();
+
+ // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
+ // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
+ $rows = $wpdb->get_results( $wpdb->prepare(
+ 'SELECT id, customer_name, reason, preferred_contact FROM %i',
+ $ra_euwb_table
+ ) );
+
+ if ( $rows ) {
+ foreach ( $rows as $row ) {
+ $clean = array(
+ 'customer_name' => self::sanitize_customer_name( $row->customer_name ),
+ 'reason' => in_array( (string) $row->reason, $allowed, true ) ? (string) $row->reason : self::sanitize_reason( $row->reason ),
+ 'preferred_contact' => self::sanitize_preferred_contact( $row->preferred_contact ),
+ );
+ if ( $clean['customer_name'] !== (string) $row->customer_name
+ || $clean['reason'] !== (string) $row->reason
+ || $clean['preferred_contact'] !== (string) $row->preferred_contact ) {
+ $wpdb->update( $ra_euwb_table, $clean, array( 'id' => (int) $row->id ), array( '%s', '%s', '%s' ), array( '%d' ) );
+ }
+ }
+ }
+
+ // Order notes written by this plugin that still contain angle brackets.
+ // WooCommerce stores order notes in the comments table (also under HPOS).
+ $notes = $wpdb->get_results( $wpdb->prepare(
+ "SELECT comment_ID, comment_content FROM {$wpdb->comments}
+ WHERE comment_type = 'order_note'
+ AND comment_content LIKE %s
+ AND ( comment_content LIKE %s OR comment_content LIKE %s )",
+ '%' . $wpdb->esc_like( 'EU Withdrawal' ) . '%',
+ '%<%',
+ '%>%'
+ ) );
+ // phpcs:enable
+
+ if ( $notes ) {
+ foreach ( $notes as $note ) {
+ $content = (string) $note->comment_content;
+ // Neutralise angle brackets only; everything else in these notes is
+ // plugin-generated text and already safe.
+ $content = str_replace( array( '<', '>' ), array( '<', '>' ), $content );
+ if ( $content !== (string) $note->comment_content ) {
+ wp_update_comment( array(
+ 'comment_ID' => (int) $note->comment_ID,
+ 'comment_content' => $content,
+ ) );
+ }
+ }
+ }
}
}