Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/cargo-shipping-location-for-woocommerce/cargo-shipping-location.php
+++ b/cargo-shipping-location-for-woocommerce/cargo-shipping-location.php
@@ -3,7 +3,7 @@
* Plugin Name: Cargo Shipping Location for WooCommerce
* Plugin URI: https://cargo.co.il/
* Description: Location Selection for Shipping Method for WooCommerce
- * Version: 5.7.1
+ * Version: 6.0
* Author: Astraverdes
* Author URI: https://astraverdes.com/
* License: GPLv2 or later
@@ -11,9 +11,9 @@
* Text Domain: cargo-shipping-location-for-woocommerce
* Requires Plugins: woocommerce
* Requires at least: 6.2
- * Tested up to: 7.1
+ * Tested up to: 7.1.4
* WC requires at least: 6.0.0
- * WC tested up to: 9.2.3
+ * WC tested up to: 11.2.1
*/
use CSLFWIncludesCargoAPICargo;
@@ -36,7 +36,7 @@
}
if ( !defined( 'CSLFW_VERSION' ) ) {
- define( 'CSLFW_VERSION', '5.7.1' );
+ define( 'CSLFW_VERSION', '6.0' );
}
if (!isset($cslfw_cargo_autoloader) || $cslfw_cargo_autoloader === false) {
--- a/cargo-shipping-location-for-woocommerce/includes/CargoApi/Webhook.php
+++ b/cargo-shipping-location-for-woocommerce/includes/CargoApi/Webhook.php
@@ -7,6 +7,10 @@
class Webhook
{
use Helpers;
+
+ const SECRET_OPTION = 'cslfw_webhook_secret';
+
+ private $apiBaseUrl = 'https://api-v2.cargo.co.il';
private $apiKey;
private $headers;
/**
@@ -30,6 +34,7 @@
add_action('wp_ajax_cslfw_add_webhooks', [$this, 'send_webhooks_to_cargo']);
add_action('wp_ajax_cslfw_delete_webhooks', [$this, 'delete_webhooks_from_cargo']);
add_action('admin_enqueue_scripts', [$this, 'import_assets'] );
+ add_action('admin_notices', [$this, 'webhook_secret_notice']);
add_action('rest_api_init', [$this, 'check_rest_api'] );
}
@@ -53,51 +58,76 @@
);
}
- public function cargo_update_shipment_status_permission($request)
+ /**
+ * Authenticates the status-update webhook.
+ *
+ * Request headers (e.g. a "Cargo domain" header) are attacker-controlled and
+ * cannot prove the caller is Cargo. Instead every site has a random secret
+ * that is only shared with Cargo, embedded in the webhook URL we register
+ * with them, and verified here with a constant-time comparison.
+ */
+ public function cargo_update_shipment_status_permission($request)
{
- $allowed_domains = [
- 'api-v2.cargo.co.il',
- 'dashboard.cargo.co.il',
- ];
-
- // Get the custom header value
- // WordPress REST API converts headers to lowercase and replaces - with _
- // Http-Cargo-Domain becomes http_cargo_domain
- $cargo_domain = sanitize_text_field( $request->get_header( 'Http-Cargo-Domain' ) );
+ $secret = get_option(self::SECRET_OPTION);
+ $token = (string) $request->get_param('token');
- if ( empty( $cargo_domain ) ) {
+ if (empty($secret) || $token === '' || !hash_equals((string) $secret, $token)) {
return new WP_Error(
'rest_forbidden',
- __( 'Missing Cargo domain header.', 'cargo-shipping-location-for-woocommerce' ),
- [ 'status' => 401 ]
+ __('Invalid webhook token.', 'cargo-shipping-location-for-woocommerce'),
+ ['status' => 401]
);
}
- // Strip www. prefix and sanitize
- // Strip protocol (http:// or https://)
- $cargo_domain = preg_replace( '/^https?:///', '', $cargo_domain );
-
- // Strip slashes
- $cargo_domain = trim( $cargo_domain, '/' );
+ return true;
+ }
- // Strip www. prefix
- $cargo_domain = preg_replace( '/^www./', '', $cargo_domain );
+ /**
+ * Returns the webhook secret, generating it on first use.
+ */
+ protected function get_webhook_secret()
+ {
+ $secret = get_option(self::SECRET_OPTION);
- // Strip anything after the domain (path, query string, port)
- $cargo_domain = strtolower( parse_url( 'https://' . $cargo_domain, PHP_URL_HOST ) );
+ if (empty($secret)) {
+ $secret = wp_generate_password(64, false, false);
+ update_option(self::SECRET_OPTION, $secret, false);
+ }
+ return $secret;
+ }
- if ( ! in_array( $cargo_domain, $allowed_domains, true ) ) {
- return new WP_Error(
- 'rest_forbidden',
- __( 'Domain not allowed.', 'cargo-shipping-location-for-woocommerce' ),
- [ 'status' => 403 ]
- );
+ /**
+ * Sites that registered webhooks before the secret existed must re-sync,
+ * otherwise Cargo keeps calling the old URL without a token and gets 401.
+ */
+ public function webhook_secret_notice()
+ {
+ if (!current_user_can('manage_options') || get_option(self::SECRET_OPTION) || !get_option('cslfw_webhooks_installed')) {
+ return;
}
- return true;
+ $url = admin_url('admin.php?page=cargo_shipping_webhook');
+ printf(
+ '<div class="notice notice-warning"><p>%s <a href="%s">%s</a></p></div>',
+ esc_html__('Cargo shipping: webhook security was updated. Please click "Update webhooks" to keep receiving shipment status updates.', 'cargo-shipping-location-for-woocommerce'),
+ esc_url($url),
+ esc_html__('Open webhook settings', 'cargo-shipping-location-for-woocommerce')
+ );
+ }
- return true;
+ /**
+ * Admin AJAX guard: capability + nonce.
+ */
+ protected function verify_admin_request($nonce, $action)
+ {
+ if (!current_user_can('manage_options') || empty($nonce) || !wp_verify_nonce(sanitize_text_field($nonce), $action)) {
+ echo wp_json_encode([
+ 'error' => true,
+ 'message' => 'Bad request, try again later.',
+ ]);
+ wp_die();
+ }
}
/**
@@ -116,6 +146,17 @@
$data = $request->get_params();
+ $deliveries = null;
+ $updated = false;
+
+ if (empty($data['shipment_id']) || !is_scalar($data['shipment_id']) || !isset($data['new_status_code']) || !is_numeric($data['new_status_code'])) {
+ return array(
+ 'success' => false,
+ 'data' => null,
+ 'message' => 'Failed to update database',
+ );
+ }
+
if ($this->helpers->HPOS_enabled()) {
$tableName = 'wc_orders_meta';
$orderIdField = 'order_id';
@@ -147,10 +188,19 @@
$logs->add_debug_message($message . PHP_EOL);
$order = wc_get_order($orderId);
+ if (!$order) {
+ continue;
+ }
$deliveries = $order->get_meta('cslfw_shipping');
- $deliveries[$data['shipment_id']]['status']['number'] = $data['new_status_code'];
- $deliveries[$data['shipment_id']]['status']['text'] = $data['new_status'];
+ // LIKE is only a pre-filter ("12" also matches "123"); require an exact key match.
+ if (!is_array($deliveries) || !isset($deliveries[$data['shipment_id']])) {
+ continue;
+ }
+
+ $deliveries[$data['shipment_id']]['status']['number'] = (int) $data['new_status_code'];
+ $deliveries[$data['shipment_id']]['status']['text'] = sanitize_text_field($data['new_status'] ?? '');
+ $updated = true;
$order->update_meta_data('cslfw_shipping', $deliveries);
@@ -165,9 +215,8 @@
}
$response = array(
- 'success' => $orders ? true : false,
- 'data' => $deliveries,
- 'message' => $orders ? 'Database updated successfully' : 'Failed to update database',
+ 'success' => $updated,
+ 'message' => $updated ? 'Database updated successfully' : 'Failed to update database',
);
// Return the response
@@ -176,20 +225,14 @@
public function save_cargo_api()
{
- parse_str(sanitize_text_field($_POST['form_data']), $data);
+ parse_str(sanitize_text_field(wp_unslash($_POST['form_data'] ?? '')), $data);
- if (!isset($data['_wpnonce']) || !wp_verify_nonce(sanitize_text_field($data['_wpnonce']), 'cslfw-save-api-key')) {
- echo wp_json_encode([
- 'error' => true,
- 'message' => 'Bad request, try again later.',
- ]);
- wp_die();
- }
+ $this->verify_admin_request($data['_wpnonce'] ?? '', 'cslfw-save-api-key');
$headers = [
"Authorization" => "Bearer {$data['cslfw_api_key']}",
];
- $response = $this->get("https://api-v2.cargo.co.il/api/token/auth", [], $headers);
+ $response = $this->get("{$this->apiBaseUrl}/api/token/auth", [], $headers);
if ($response && !$response->errors && !empty($data['cslfw_api_key'])) {
if (empty($this->apiKey)) {
@@ -228,13 +271,7 @@
public function delete_webhooks_from_cargo()
{
- if (!isset($_POST['_wpnonce']) && !wp_verify_nonce( sanitize_text_field($_POST['_wpnonce']), 'cslfw_cargo_update_or_remove_webhook')) {
- echo wp_json_encode([
- 'error' => true,
- 'message' => 'Bad request, try again later.',
- ]);
- wp_die();
- }
+ $this->verify_admin_request($_POST['_wpnonce'] ?? '', 'cslfw_cargo_update_or_remove_webhook');
$customerCodes = [
'express' => get_option('shipping_cargo_express'),
@@ -270,6 +307,8 @@
public function send_webhooks_to_cargo()
{
+ $this->verify_admin_request($_POST['_wpnonce'] ?? '', 'cslfw_cargo_update_or_remove_webhook');
+
$customerCodes = [
'express' => get_option('shipping_cargo_express'),
'express_24' => get_option('shipping_cargo_express_24'),
@@ -318,13 +357,18 @@
*/
protected function getWebhookArgs($customerCode)
{
- $host = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https://" : "http://";
- $host.= sanitize_text_field($_SERVER['HTTP_HOST']);
+ // rest_url() uses the configured site URL (not the spoofable Host header)
+ // and works with plain permalinks too.
+ $url = add_query_arg(
+ 'token',
+ rawurlencode($this->get_webhook_secret()),
+ rest_url('cargo-shipping-location-for-woocommerce/v1/update-status/')
+ );
return [
'type' => 'status-update',
'customer_code' => (int) $customerCode,
- 'webhook_url' => "{$host}/wp-json/cargo-shipping-location-for-woocommerce/v1/update-status/"
+ 'webhook_url' => $url,
];
}
@@ -336,17 +380,17 @@
{
$args = $this->getWebhookArgs($customerCode);
$args['webhook_id'] = $webhookId;
+ $args['id'] = $webhookId;
+
+ $response = $this->post("{$this->apiBaseUrl}/api/webhooks/update", $args, $this->headers);
+
if (get_option('cslfw_debug_mode')) {
$logs = new CSLFW_Logs();
- $message = "update WEBHOOK : ".wp_json_encode($args,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES). PHP_EOL;
- $message .= "update WEBHOOK HEADERS: ".wp_json_encode($this->headers,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES). PHP_EOL;
+ $message = "update WEBHOOK customer_code: {$customerCode}, webhook_id: {$webhookId}" . PHP_EOL;
+ $message .= "update WEBHOOK RESPONSE: ".wp_json_encode($response,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES). PHP_EOL;
+ $logs->add_debug_message($message . PHP_EOL , $args);
}
- $response = $this->post("https://dashboard.cargo.co.il/api/webhooks/update", $args, $this->headers);
-
- $message .= "update WEBHOOK HEADERS: ".wp_json_encode($response,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES). PHP_EOL;
- $logs->add_debug_message($message . PHP_EOL );
-
return $response;
}
@@ -357,15 +401,19 @@
public function addWebhookToCargo($customerCode)
{
$args = $this->getWebhookArgs($customerCode);
+
+ $response = $this->post("{$this->apiBaseUrl}/api/webhooks/create", $args, $this->headers);
+
if (get_option('cslfw_debug_mode')) {
$logs = new CSLFW_Logs();
- $message = "ADD WEBHOOK : " . wp_json_encode($args, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
- $message .= "ADD WEBHOOK HEADERS: " . wp_json_encode($this->headers, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
+ $message = "ADD WEBHOOK customer_code: {$customerCode}" . PHP_EOL;
+ $message .= "ADD WEBHOOK RESPONSE: ".wp_json_encode($response,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES). PHP_EOL;
- $logs->add_debug_message($message . PHP_EOL);
+ $logs->add_debug_message($message . PHP_EOL, $args);
}
- return $this->post("https://dashboard.cargo.co.il/api/webhooks/create", $args, $this->headers);
+
+ return $response;
}
/**
@@ -376,11 +424,21 @@
public function deleteWebhook($customerCode, $webhookId)
{
$args = [
+ 'id' => $webhookId,
'webhook_id' => $webhookId,
'customer_code' => $customerCode,
];
- return $this->delete("https://dashboard.cargo.co.il/api/webhooks/delete", $args, $this->headers);
+ $response = $this->delete("{$this->apiBaseUrl}/api/webhooks/delete", $args, $this->headers);
+
+ if (get_option('cslfw_debug_mode')) {
+ $logs = new CSLFW_Logs();
+ $message = "delete WEBHOOK customer_code: {$customerCode}, webhook_id: {$webhookId}" . PHP_EOL;
+ $message .= "delete WEBHOOK RESPONSE: ".wp_json_encode($response,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES). PHP_EOL;
+ $logs->add_debug_message($message . PHP_EOL , $args);
+ }
+
+ return $response;
}
public function add_menu_link()
--- a/cargo-shipping-location-for-woocommerce/includes/cslfw-settings.php
+++ b/cargo-shipping-location-for-woocommerce/includes/cslfw-settings.php
@@ -3,6 +3,7 @@
* Main Settings
*
*/
+if ( ! defined( 'ABSPATH' ) ) exit;
use CSLFWIncludesCSLFW_Helpers;
--- a/cargo-shipping-location-for-woocommerce/includes/processes/cslfw-cargo-job.php
+++ b/cargo-shipping-location-for-woocommerce/includes/processes/cslfw-cargo-job.php
@@ -1,5 +1,5 @@
<?php
-
+if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'CSLFW_Cargo_Job' ) ) {
abstract class CSLFW_Cargo_Job {
--- a/cargo-shipping-location-for-woocommerce/templates/admin/shipment.php
+++ b/cargo-shipping-location-for-woocommerce/templates/admin/shipment.php
@@ -168,7 +168,7 @@
}
?>
</div>
-<?php print_r(get_option('mailchimp_auto_subscribe'));?>
+
<div class="cargo-button">
<a href="#" class="cslfw-create-new-shipment button button-primary"><?php esc_html_e('יצירת משלוח חדש', 'cargo-shipping-location-for-woocommerce') ?></a>
<p style="font-size: 12px;"><?php esc_html_e('פעולה זו לא תבטל את המשלוח הקודם (יש לפנות לשירות הלקוחות) אלא תיצור משלוח חדש', 'cargo-shipping-location-for-woocommerce') ?></p>