Skip to content

Commit

Permalink
Inspect and fix escape errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Aug 22, 2024
1 parent d5e6e21 commit 66bfb86
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class AbstractVersionConstraint implements ConstraintInterface
public function __construct($requiredVersion, $requiredPluginName = null)
{
$this->requiredVersion = $requiredVersion;
$this->requiredPluginName = $requiredPluginName;
$this->requiredPluginName = esc_html($requiredPluginName);
$this->error = '';
$this->message = '';
}
Expand All @@ -60,11 +60,13 @@ protected function checkVersion($actualVersion)
if ($result) {
return $result;
}
throw new ConstraintFailedException(
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new ConstraintFailedException(
$this,
$actualVersion,
[$this->error],
esc_html($this->message)
);
}
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
}
}
9 changes: 6 additions & 3 deletions pluginEnvironmentChecker/Constraints/ExtensionConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ExtensionConstraint extends AbstractVersionConstraint
public function __construct($requiredVersion)
{
parent::__construct($requiredVersion);
$this->error = 'Required Extension not loaded';
$this->error = esc_html('Required Extension not loaded');
}

/**
Expand All @@ -25,18 +25,21 @@ public function check()
{
$this->message = $this->requiredVersion
. ' extension is required. Enable it in your server or ask your webhoster to enable it for you.';
$this->message = esc_html($this->message);
if (function_exists('extension_loaded')
&& !extension_loaded(
$this->requiredVersion
)
) {
throw new ConstraintFailedException(
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new ConstraintFailedException(
$this,
$this->requiredVersion,
[$this->error],
$this->message
);
}
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
}
return true;
}
}
17 changes: 10 additions & 7 deletions pluginEnvironmentChecker/Constraints/PluginConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PluginConstraint extends AbstractVersionConstraint
public function __construct($requiredVersion, $requiredPluginName, $pluginDisplayName)
{
parent::__construct($requiredVersion, $requiredPluginName);
$this->error = 'Plugin incompatibility';
$this->error = esc_html('Plugin incompatibility');
$this->requiredPluginName = $requiredPluginName;
$this->pluginDisplayName = $pluginDisplayName;
}
Expand All @@ -33,23 +33,26 @@ public function check()
if (!$isPluginActive) {
$this->message
= "The {$this->pluginDisplayName} plugin must be active. Please install & activate {$this->pluginDisplayName}";

// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new ConstraintFailedException(
$this,
$this->requiredPluginName,
esc_html($this->requiredPluginName),
[$this->error],
esc_html($this->message)
);
}
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
}

$pathToPluginFile = $this->absolutePathToPlugin();
if (!$pathToPluginFile) {
throw new ConstraintFailedException(
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new ConstraintFailedException(
$this,
$this->requiredPluginName,
esc_html($this->requiredPluginName),
[$this->error],
esc_html("Cannot find absolute path to {$this->pluginDisplayName} plugin")
);
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
}
if (!function_exists('get_plugin_data')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
Expand All @@ -59,7 +62,7 @@ public function check()
$this->message = "The {$this->pluginDisplayName} plugin has to be version "
. $this->requiredVersion
. " or higher. Please update your {$this->pluginDisplayName} version.";

$this->message = esc_html($this->message);
return $this->checkVersion(
$currentVersion
);
Expand Down
3 changes: 2 additions & 1 deletion pluginEnvironmentChecker/Constraints/WordPressConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class WordPressConstraint extends AbstractVersionConstraint
public function __construct($requiredVersion)
{
parent::__construct($requiredVersion);
$this->error = 'Wordpress version incompatibility';
$this->error = esc_html('Wordpress version incompatibility');
}

/**
Expand All @@ -24,6 +24,7 @@ public function check()
$this->message = 'WordPress version has to be '
. $this->requiredVersion
. ' or higher. Please update your WordPress version';
$this->message = esc_html($this->message);

return $this->checkVersion(
$WPCurrentVersion
Expand Down
8 changes: 5 additions & 3 deletions pluginEnvironmentChecker/EnvironmentChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ public function check()

$errCount = count($this->errors);
if ($errCount) {
throw new ConstraintFailedException(
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new ConstraintFailedException(
$this,
'General Checker',
$this->errors,
$this->__('Validation failed with %1$d errors', [$errCount])
$this->esc_html__('Validation failed with %1$d errors', [$errCount])
);
}
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Payment/OrderItemsRefunder.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private function normalizedRemoteItems(array $remoteItems)
'Impossible to retrieve the order item ID related to the remote item: %1$s. Try to do a refund by amount.',
'mollie-payments-for-woocommerce'
),
$remoteItem->id
esc_html($remoteItem->id)
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Payment/RefundLineItemsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function buildLineItems(
'Cannot refund %s item because it was not found in Mollie order. Aborting refund process. Try to do a refund by amount.',
'mollie-payments-for-woocommerce'
),
$toRefundItemId
esc_html($toRefundItemId)
)
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/SDK/WordPressHttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public function send($httpMethod, $url, $headers, $httpBody)
if (is_wp_error($response)) {
$message = $response->get_error_message() ?? 'Unknown error';
$code = is_int($response->get_error_code()) ? $response->get_error_code() : 0;
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new ApiException(esc_html($message), $code);
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
}

return $this->parseResponse($response);
Expand Down
6 changes: 3 additions & 3 deletions src/Shared/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,21 +270,21 @@ public function getFilters(
$amountValue = $this->getAmountValue($orderTotal, $currency);
if ($amountValue <= 0) {
throw new InvalidArgumentException(
sprintf(esc_html__('Amount %s is not valid.', 'mollie-payments-for-woocommerce'), $amountValue)
sprintf(esc_html__('Amount %s is not valid.', 'mollie-payments-for-woocommerce'), esc_html($amountValue))
);
}

// Check if currency is in ISO 4217 alpha-3 format (ex: EUR)
if (!preg_match('/^[a-zA-Z]{3}$/', $currency)) {
throw new InvalidArgumentException(
sprintf(esc_html__('Currency %s is not valid.', 'mollie-payments-for-woocommerce'), $currency)
sprintf(esc_html__('Currency %s is not valid.', 'mollie-payments-for-woocommerce'), esc_html($currency))
);
}

// Check if billing country is in ISO 3166-1 alpha-2 format (ex: NL)
if (!preg_match('/^[a-zA-Z]{2}$/', $billingCountry)) {
throw new InvalidArgumentException(
sprintf(esc_html__('Billing Country %s is not valid.', 'mollie-payments-for-woocommerce'), $billingCountry)
sprintf(esc_html__('Billing Country %s is not valid.', 'mollie-payments-for-woocommerce'), esc_html($billingCountry))
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Shared/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ public function getMollieApiStatus($apiClient)
esc_html__('incorrect API key or other authentication issue. Please check your API keys!', 'mollie-payments-for-woocommerce')
);
}
$message = esc_html($apiException->getMessage());
throw new \Mollie\Api\Exceptions\ApiException($message);
$message = $apiException->getMessage();
throw new \Mollie\Api\Exceptions\ApiException(esc_html($message));
}
}
}

0 comments on commit 66bfb86

Please sign in to comment.