Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added whitelist to OAuth consumer callback URLs to allow custom URL scheme #3774

Merged
merged 1 commit into from
Feb 8, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -92,7 +92,6 @@ protected function _prepareForm()
'title' => Mage::helper('oauth')->__('Callback URL'),
'required' => false,
'value' => $model->getCallbackUrl(),
'class' => 'validate-url',
]);

$fieldset->addField('rejected_callback_url', 'text', [
Expand All @@ -101,7 +100,6 @@ protected function _prepareForm()
'title' => Mage::helper('oauth')->__('Rejected Callback URL'),
'required' => false,
'value' => $model->getRejectedCallbackUrl(),
'class' => 'validate-url',
]);

$fieldset->addField(
Expand Down
19 changes: 3 additions & 16 deletions app/code/core/Mage/Oauth/Model/Consumer.php
Expand Up @@ -65,6 +65,8 @@ protected function _beforeSave()
if (!$this->getId()) {
$this->setUpdatedAt(time());
}
$this->setCallbackUrl(trim($this->getCallbackUrl()));
$this->setRejectedCallbackUrl(trim($this->getRejectedCallbackUrl()));
$this->validate();
parent::_beforeSave();
return $this;
Expand All @@ -73,26 +75,11 @@ protected function _beforeSave()
/**
* Validate data
*
* @return array|bool
* @return bool
* @throw Mage_Core_Exception|Exception Throw exception on fail validation
*/
public function validate()
{
if ($this->getCallbackUrl() || $this->getRejectedCallbackUrl()) {
$this->setCallbackUrl(trim($this->getCallbackUrl()));
$this->setRejectedCallbackUrl(trim($this->getRejectedCallbackUrl()));

/** @var Mage_Core_Model_Url_Validator $validatorUrl */
$validatorUrl = Mage::getSingleton('core/url_validator');

if ($this->getCallbackUrl() && !$validatorUrl->isValid($this->getCallbackUrl())) {
Mage::throwException(Mage::helper('oauth')->__('Invalid Callback URL'));
}
if ($this->getRejectedCallbackUrl() && !$validatorUrl->isValid($this->getRejectedCallbackUrl())) {
Mage::throwException(Mage::helper('oauth')->__('Invalid Rejected Callback URL'));
}
}

/** @var Mage_Oauth_Model_Consumer_Validator_KeyLength $validatorLength */
$validatorLength = Mage::getModel('oauth/consumer_validator_keyLength', ['length' => self::KEY_LENGTH]);

Expand Down
7 changes: 6 additions & 1 deletion app/code/core/Mage/Oauth/Model/Server.php
Expand Up @@ -432,7 +432,12 @@ protected function _validateCallbackUrlParam()
if (!is_string($this->_protocolParams['oauth_callback'])) {
$this->_throwException('oauth_callback', self::ERR_PARAMETER_REJECTED);
}
if (self::CALLBACK_ESTABLISHED != $this->_protocolParams['oauth_callback']
// Is the callback URL whitelisted?
$callbackUrl = $this->_consumer->getCallbackUrl();
if ($callbackUrl && strpos($this->_protocolParams['oauth_callback'], $callbackUrl) === 0) {
return;
}
if (self::CALLBACK_ESTABLISHED !== $this->_protocolParams['oauth_callback']
&& !Zend_Uri::check($this->_protocolParams['oauth_callback'])
) {
$this->_throwException('oauth_callback', self::ERR_PARAMETER_REJECTED);
Expand Down
17 changes: 9 additions & 8 deletions app/code/core/Mage/Oauth/Model/Token.php
Expand Up @@ -220,18 +220,19 @@ protected function _beforeSave()
/**
* Validate data
*
* @return array|bool
* @return bool
* @throw Mage_Core_Exception|Exception Throw exception on fail validation
*/
public function validate()
{
/** @var Mage_Core_Model_Url_Validator $validatorUrl */
$validatorUrl = Mage::getSingleton('core/url_validator');
if (Mage_Oauth_Model_Server::CALLBACK_ESTABLISHED != $this->getCallbackUrl()
&& !$validatorUrl->isValid($this->getCallbackUrl())
) {
$messages = $validatorUrl->getMessages();
Mage::throwException(array_shift($messages));
if (Mage_Oauth_Model_Server::CALLBACK_ESTABLISHED !== $this->getCallbackUrl()) {
$callbackUrl = $this->getConsumer()->getCallbackUrl();
$isWhitelisted = $callbackUrl && strpos($this->getCallbackUrl(), $callbackUrl) === 0;
$validatorUrl = Mage::getSingleton('core/url_validator');
if (!$isWhitelisted && !$validatorUrl->isValid($this->getCallbackUrl())) {
$messages = $validatorUrl->getMessages();
Mage::throwException(array_shift($messages));
}
}

/** @var Mage_Oauth_Model_Consumer_Validator_KeyLength $validatorLength */
Expand Down