Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions inc/Smartling/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ private function initRoles(): void
#[NoReturn]
public function updateGlobalExpertSettings(): void
{
check_ajax_referer('smartling_expert_global_settings', '_wpnonce');
if (!current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_PROFILE_CAP)) {
wp_send_json(['error' => 'Insufficient permissions'], 403);
return;
}

$data = $_POST['params'];

$rawPageSize = (int)$data['pageSize'];
Expand Down
1 change: 1 addition & 0 deletions inc/Smartling/ContentTypes/CustomPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ protected function registerJobWidget(): void
->addArgument($di->getDefinition('site.helper'))
->addArgument($di->getDefinition('manager.submission'))
->addArgument($di->getDefinition('site.cache'))
->addArgument($di->getDefinition('wp.proxy'))
->addMethodCall('setServedContentType', [$this->getSystemName()]);
$di->get($tag)->register();
}
Expand Down
1 change: 1 addition & 0 deletions inc/Smartling/ContentTypes/CustomTaxonomyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ protected function registerJobWidget()
->addArgument($di->getDefinition('site.helper'))
->addArgument($di->getDefinition('manager.submission'))
->addArgument($di->getDefinition('site.cache'))
->addArgument($di->getDefinition('wp.proxy'))
->addMethodCall('setServedContentType', [static::getSystemName()])
->addMethodCall('setBaseType', ['taxonomy']);
$di->get($tag)->register();
Expand Down
14 changes: 11 additions & 3 deletions inc/Smartling/Helpers/UiMessageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ class UiMessageHelper

public static function dismissMessage(): void
{
check_ajax_referer(self::DISMISS_MESSAGE_ACTION, '_wpnonce');
if (!current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_MENU_CAP)) {
wp_send_json_error(['message' => 'Insufficient permissions'], 403);
return;
}
$cache = self::getCache();
if (array_key_exists('hash', $_GET)) {
$cache->set(self::CACHE_KEY_PREFIX . $_GET['hash'], true, 60 * 60 * 180);
$hash = isset($_POST['hash']) ? sanitize_text_field(wp_unslash($_POST['hash'])) : '';
if ($hash !== '') {
$cache->set(self::CACHE_KEY_PREFIX . $hash, true, 60 * 60 * 180);
}
wp_send_json_success();
}

public static function displayMessages(): void
Expand Down Expand Up @@ -55,8 +62,9 @@ private static function getClickHandler(string $string): string
{
$action = self::DISMISS_MESSAGE_ACTION;
$hash = self::getCacheHash($string);
$nonce = wp_create_nonce(self::DISMISS_MESSAGE_ACTION);
return <<<JS
jQuery.post(ajaxurl + '?action=$action&hash=$hash');
jQuery.post(ajaxurl + '?action=$action', {hash: '$hash', _wpnonce: '$nonce'});
this.parentNode.parentNode.style.display='none';
return false;
JS;
Expand Down
17 changes: 16 additions & 1 deletion inc/Smartling/Services/ContentRelationsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Exception;
use Smartling\Exception\SmartlingHumanReadableException;
use Smartling\Helpers\LoggerSafeTrait;
use Smartling\Helpers\SmartlingUserCapabilities;
use Smartling\Helpers\WordpressFunctionProxyHelper;
use Smartling\Models\UserCloneRequest;
use Smartling\Models\UserTranslationRequest;

Expand Down Expand Up @@ -44,7 +46,8 @@ class ContentRelationsHandler extends BaseAjaxServiceAbstract
public const FORM_ACTION_UPLOAD = 'upload';

private ContentRelationsDiscoveryService $service;
public function __construct(ContentRelationsDiscoveryService $service)

public function __construct(ContentRelationsDiscoveryService $service, private WordpressFunctionProxyHelper $wpProxy)
{
parent::__construct($_GET);
$this->service = $service;
Expand Down Expand Up @@ -78,6 +81,12 @@ public function register(): void
*/
public function createSubmissionsHandler(array $data = null): void
{
$this->wpProxy->check_ajax_referer('smartling_translation', '_wpnonce');
if (!$this->wpProxy->current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_WIDGET_CAP)) {
$this->returnError('permission.denied', 'Insufficient permissions', 403);
return;
}
Comment on lines +84 to +88
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be implemented as a middleware in WP?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, no such thing in WP


if ($data === null) {
$data = $_POST;
}
Expand All @@ -95,6 +104,12 @@ public function createSubmissionsHandler(array $data = null): void

public function actionHandler(): void
{
$this->wpProxy->check_ajax_referer('smartling_translation', '_wpnonce');
if (!$this->wpProxy->current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_WIDGET_CAP)) {
$this->returnError('permission.denied', 'Insufficient permissions', 403);
return;
}

$data = $_GET;
$data['targetBlogIds'] = $this->convertTargetBlogIds($data['targetBlogIds']);
try {
Expand Down
9 changes: 9 additions & 0 deletions inc/Smartling/WP/Controller/CheckStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public function wp_enqueue()
wp_enqueue_script($this->pluginInfo->getName() . "submission", $this->pluginInfo
->getUrl() . 'js/smartling-submissions-check.js', ['jquery'], $this->pluginInfo
->getVersion(), false);
wp_localize_script($this->pluginInfo->getName() . "submission", 'smartlingCheckStatus', [
'nonce' => wp_create_nonce('smartling_check_status'),
]);
}

public function register(): void
Expand All @@ -36,6 +39,12 @@ public function register(): void
*/
public function ajaxHandler()
{
check_ajax_referer('smartling_check_status', '_wpnonce');
if (!current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_WIDGET_CAP)) {
wp_send_json(['error' => 'Insufficient permissions'], 403);
return false;
}

if ($_REQUEST["action"] === "ajax_submissions_update_status") {

$items = $this->checkItems($_REQUEST["ids"]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function wp_enqueue(): void
foreach ($jsFiles as $jFile) {
wp_enqueue_script($jFile, $jFile, ['jquery'], $ver, false);
}
wp_localize_script($jsPath . 'configuration-profile-form.js', 'smartlingProfileForm', [
'expertSettingsNonce' => wp_create_nonce('smartling_expert_global_settings'),
]);
}

public function register(): void
Expand All @@ -47,6 +50,12 @@ public function register(): void

public function initTestConnectionEndpoint(): void
{
check_ajax_referer('smartling_test_connection', '_wpnonce');
if (!current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_PROFILE_CAP)) {
wp_send_json(['status' => 403, 'message' => 'Insufficient permissions'], 403);
return;
}

$data =& $_POST;

$result = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public function wp_enqueue(): void
$this->pluginInfo->getVersion(),
false
);
wp_localize_script($this->pluginInfo->getName() . 'settings', 'smartlingConnector', [
'nonce' => wp_create_nonce('smartling_connector_ajax'),
]);
wp_enqueue_script(
$this->pluginInfo->getName() . 'settings-admin-footer',
$this->pluginInfo->getUrl() . 'js/smartling-connector-gutenberg-lock-attributes.js',
Expand Down
28 changes: 28 additions & 0 deletions inc/Smartling/WP/Controller/ContentEditJobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,44 @@

use DateTimeZone;
use Exception;
use Smartling\ApiWrapperInterface;
use Smartling\Bootstrap;
use Smartling\DbAl\LocalizationPluginProxyInterface;
use Smartling\Exceptions\SmartlingApiException;
use Smartling\Helpers\ArrayHelper;
use Smartling\Helpers\Cache;
use Smartling\Helpers\DateTimeHelper;
use Smartling\Helpers\DiagnosticsHelper;
use Smartling\Helpers\HtmlTagGeneratorHelper;
use Smartling\Helpers\PluginInfo;
use Smartling\Helpers\SiteHelper;
use Smartling\Helpers\SmartlingUserCapabilities;
use Smartling\Helpers\WordpressFunctionProxyHelper;
use Smartling\Settings\SettingsManager;
use Smartling\Submissions\SubmissionManager;
use Smartling\Vendor\Smartling\Jobs\JobStatus;
use Smartling\WP\WPAbstract;
use Smartling\WP\WPHookInterface;

class ContentEditJobController extends WPAbstract implements WPHookInterface
{
public const SMARTLING_JOB_API_PROXY = 'smartling_job_api_proxy';

private WordpressFunctionProxyHelper $wpProxy;

public function __construct(
ApiWrapperInterface $api,
LocalizationPluginProxyInterface $localizationPluginProxy,
PluginInfo $pluginInfo,
SettingsManager $settingsManager,
SiteHelper $siteHelper,
SubmissionManager $submissionManager,
Cache $cache,
WordpressFunctionProxyHelper $wpProxy,
) {
parent::__construct($api, $localizationPluginProxy, $pluginInfo, $settingsManager, $siteHelper, $submissionManager, $cache);
$this->wpProxy = $wpProxy;
}
/**
* @var string
*/
Expand Down Expand Up @@ -65,6 +87,12 @@ public function setServedContentType($servedContentType)
public function initJobApiProxy(): void
{
add_action('wp_ajax_' . self::SMARTLING_JOB_API_PROXY, function () {
$this->wpProxy->check_ajax_referer('smartling_translation', '_wpnonce');
if (!$this->wpProxy->current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_WIDGET_CAP)) {
$this->wpProxy->wp_send_json(['status' => 403, 'message' => 'Insufficient permissions'], 403);
return;
}

$data =& $_POST;

$result = [
Expand Down
15 changes: 13 additions & 2 deletions inc/Smartling/WP/Controller/InstantTranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Smartling\Helpers\DateTimeHelper;
use Smartling\Helpers\FileUriHelper;
use Smartling\Helpers\LoggerSafeTrait;
use Smartling\Helpers\SmartlingUserCapabilities;
use Smartling\Helpers\WordpressFunctionProxyHelper;
use Smartling\Submissions\SubmissionEntity;
use Smartling\Submissions\SubmissionFactory;
Expand Down Expand Up @@ -36,7 +37,12 @@ public function register(): void

public function handleRequestTranslation(): void
{
$this->wpProxy->check_ajax_referer('smartling_instant_translation', '_wpnonce');
$this->wpProxy->check_ajax_referer('smartling_translation', '_wpnonce');

if (!$this->wpProxy->current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_WIDGET_CAP)) {
$this->wpProxy->wp_send_json_error(['message' => 'Insufficient permissions'], 403);
return;
}

try {
$contentType = $this->wpProxy->sanitize_text_field($this->wpProxy->wp_unslash($_POST['contentType'] ?? ''));
Expand Down Expand Up @@ -133,7 +139,12 @@ public function handleRequestTranslation(): void

public function handlePollStatus(): void
{
$this->wpProxy->check_ajax_referer('smartling_instant_translation', '_wpnonce');
$this->wpProxy->check_ajax_referer('smartling_translation', '_wpnonce');

if (!$this->wpProxy->current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_WIDGET_CAP)) {
$this->wpProxy->wp_send_json_error(['message' => 'Insufficient permissions'], 403);
return;
}

try {
$submissionId = (int)($_POST['submissionId'] ?? 0);
Expand Down
9 changes: 9 additions & 0 deletions inc/Smartling/WP/Controller/LiveNotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Smartling\Helpers\DiagnosticsHelper;
use Smartling\Helpers\LoggerSafeTrait;
use Smartling\Helpers\PluginInfo;
use Smartling\Helpers\SmartlingUserCapabilities;
use Smartling\Models\NotificationParameters;
use Smartling\Settings\SettingsManager;
use Smartling\Submissions\SubmissionEntity;
Expand Down Expand Up @@ -98,11 +99,13 @@ public function placeJsConfig(): void

$wrapperClassName = static::UI_NOTIFICATION_IDENTIFIER_CLASS;
$wrapperClassNameGeneral = static::UI_NOTIFICATION_IDENTIFIER_CLASS_GENERAL;
$deleteNonce = wp_create_nonce(self::DELETE_NOTIFICATION_ACTION_NAME);

echo <<<EOF
<script>
var firebaseConfig = $configs;
var deleteNotificationEndpoint = "$deleteEndpoint";
var deleteNotificationNonce = "$deleteNonce";
var firebaseIds = $firebaseIds;
var notificationClassName = "$wrapperClassName";
var notificationClassNameGeneral = "$wrapperClassNameGeneral";
Expand All @@ -113,6 +116,12 @@ public function placeJsConfig(): void

public function deleteNotificationAjaxHandler(): void
{
check_ajax_referer(self::DELETE_NOTIFICATION_ACTION_NAME, '_wpnonce');
if (!current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_WIDGET_CAP)) {
wp_send_json(['code' => 'error', 'message' => 'Insufficient permissions'], 403);
return;
}

$data = $_POST;

$projectId = $data['project_id'];
Expand Down
13 changes: 13 additions & 0 deletions inc/Smartling/WP/Controller/PostBasedWidgetControllerStd.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class PostBasedWidgetControllerStd extends WPAbstract implements WPHookInterface
private const WIDGET_NAME = 'smartling_connector_widget';
public const WIDGET_DATA_NAME = 'smartling';
private const CONNECTOR_NONCE = 'smartling_connector_nonce';
private const AJAX_NONCE_ACTION = 'smartling_connector_ajax';

protected string $servedContentType = 'undefined';
protected string $needSave = 'Need to have title';
Expand Down Expand Up @@ -115,6 +116,12 @@ public function setNoOriginalFound($noOriginalFound)

public function ajaxDownloadHandler(): void
{
check_ajax_referer(self::AJAX_NONCE_ACTION, '_wpnonce');
if (!current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_WIDGET_CAP)) {
wp_send_json(['status' => self::RESPONSE_AJAX_STATUS_FAIL, 'message' => 'Insufficient permissions'], 403);
return;
}

$logSubmissions = [];
$result = ['status' => self::RESPONSE_AJAX_STATUS_SUCCESS];
$submissions = [];
Expand Down Expand Up @@ -175,6 +182,12 @@ private function validateTargetBlog($blogId)

public function ajaxUploadHandler()
{
check_ajax_referer(self::AJAX_NONCE_ACTION, '_wpnonce');
if (!current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_WIDGET_CAP)) {
wp_send_json(['status' => self::RESPONSE_AJAX_STATUS_FAIL, 'message' => 'Insufficient permissions'], 403);
return;
}

$result = [];

$data = &$_POST;
Expand Down
14 changes: 11 additions & 3 deletions inc/Smartling/WP/Controller/TaxonomyLinksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

class TaxonomyLinksController extends WPAbstract implements WPHookInterface
{
private const NONCE_ACTION = 'smartling_link_taxonomies';

public function __construct(
protected ApiWrapperInterface $api,
PluginInfo $pluginInfo,
Expand Down Expand Up @@ -150,11 +152,17 @@ private function getMappedTerms()

public function linkTaxonomies($data)
{
$this->wordpressProxy->check_ajax_referer(self::NONCE_ACTION, '_wpnonce');
if (!$this->wordpressProxy->current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_MENU_CAP)) {
$this->wordpressProxy->wp_send_json_error(['message' => 'Insufficient permissions'], 403);
return;
}

if ($data === "") {
$data = $_POST;
}
if (!isset($data['sourceBlogId'], $data['sourceId'], $data['taxonomy'])) {
wp_send_json_error('Required parameter missing');
$this->wordpressProxy->wp_send_json_error('Required parameter missing');
}
$sourceBlogId = (int)$data['sourceBlogId'];
$sourceId = (int)$data['sourceId'];
Expand Down Expand Up @@ -202,13 +210,13 @@ public function linkTaxonomies($data)
}
$submissions = array_merge($submissionsToAdd, $submissionsToUpdate);
if (count(array_merge($submissions, $submissionsToDelete)) === 0) {
wp_send_json_error('No changes');
$this->wordpressProxy->wp_send_json_error('No changes');
}
$this->submissionManager->storeSubmissions($submissions);
foreach ($submissionsToDelete as $submission) {
$this->submissionManager->delete($submission);
}
wp_send_json(['success' => true, 'submissions' => $this->getSubmissions()]);
$this->wordpressProxy->wp_send_json(['success' => true, 'submissions' => $this->getSubmissions()]);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions inc/Smartling/WP/Controller/TestRunController.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ public function getBlogs(): array

public function testRun($data): void
{
check_ajax_referer('smartling_test_run', '_wpnonce');
if (!current_user_can(SmartlingUserCapabilities::SMARTLING_CAPABILITY_PROFILE_CAP)) {
wp_send_json_error(['message' => 'Insufficient permissions'], 403);
return;
}

if ($data === "") {
$data = $_POST;
}
Expand Down
Loading