Skip to content

Commit 3f0667b

Browse files
author
Oleksii Korshenko
committed
MAGETWO-50608: [Github][Security] Able to brute force API token access
1 parent 220e251 commit 3f0667b

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

app/code/Magento/Integration/Model/ResourceModel/Oauth/Token/RequestLog.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ protected function _construct()
5858
public function getFailuresCount($userName, $userType)
5959
{
6060
$select = $this->getConnection()->select();
61-
$select->columns('failures_count')
62-
->from($this->getMainTable())
63-
->where('user_login = ? AND user_type = ?', [$userName, $userType]);
61+
$select->from($this->getMainTable(), 'failures_count')
62+
->where('user_name = :user_name AND user_type = :user_type');
6463

65-
return (int)$this->getConnection()->fetchOne($select);
64+
return (int)$this->getConnection()->fetchOne($select, ['user_name' => $userName, 'user_type' => $userType]);
6665
}
6766

6867
/**
@@ -72,7 +71,7 @@ public function resetFailuresCount($userName, $userType)
7271
{
7372
$this->getConnection()->delete(
7473
$this->getMainTable(),
75-
['user_login = ?' => $userName, 'user_type = ?' => $userType]
74+
['user_name = ?' => $userName, 'user_type = ?' => $userType]
7675
);
7776
}
7877

@@ -83,15 +82,20 @@ public function incrementFailuresCount($userName, $userType)
8382
{
8483
$date = (new \DateTime())->setTimestamp($this->dateTime->gmtTimestamp());
8584
$date->add(new \DateInterval('PT' . $this->requestLogConfig->getLockTimeout() . 'S'));
85+
$dateTime = $date->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
86+
8687
$this->getConnection()->insertOnDuplicate(
8788
$this->getMainTable(),
8889
[
89-
'user_login' => $userName,
90+
'user_name' => $userName,
9091
'user_type' => $userType,
9192
'failures_count' => 1,
92-
'lock_expires_at' => $date
93+
'lock_expires_at' => $dateTime
9394
],
94-
['failures_count' => new \Zend_Db_Expr('failures_count+1'), 'lock_expires_at' => $date]
95+
[
96+
'failures_count' => new \Zend_Db_Expr('failures_count+1'),
97+
'lock_expires_at' => new \Zend_Db_Expr("'" . $dateTime . "'")
98+
]
9599
);
96100
}
97101

@@ -100,8 +104,10 @@ public function incrementFailuresCount($userName, $userType)
100104
*/
101105
public function clearExpiredFailures()
102106
{
107+
$date = (new \DateTime())->setTimestamp($this->dateTime->gmtTimestamp());
108+
$dateTime = $date->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
103109
$select = $this->getConnection()->select();
104-
$select->from($this->getMainTable())->where('lock_expires_at <= ?', $this->dateTime->gmtTimestamp());
110+
$select->from($this->getMainTable())->where('lock_expires_at <= ?', $dateTime);
105111
$this->getConnection()->delete($select);
106112
}
107113
}

app/code/Magento/Integration/Setup/UpgradeSchema.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
2828
$table = $setup->getConnection()->newTable(
2929
$setup->getTable('oauth_token_request_log')
3030
)->addColumn(
31-
'user_login',
31+
'log_id',
32+
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
33+
null,
34+
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
35+
'Log Id'
36+
)->addColumn(
37+
'user_name',
3238
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
3339
255,
34-
['nullable' => false, 'primary' => true],
40+
['nullable' => false],
3541
'Customer email or admin login'
3642
)->addColumn(
3743
'user_type',
@@ -49,11 +55,16 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
4955
'lock_expires_at',
5056
\Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
5157
null,
52-
[],
58+
['nullable' => false],
5359
'Lock expiration time'
5460
)->addIndex(
55-
$setup->getIdxName('oauth_token_request_log', ['user_login', 'user_type']),
56-
['user_login', 'user_type']
61+
$setup->getIdxName(
62+
'oauth_token_request_log',
63+
['user_name', 'user_type'],
64+
\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
65+
),
66+
['user_name', 'user_type'],
67+
['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE]
5768
)->setComment(
5869
'Log of token request authentication failures.'
5970
);

app/code/Magento/Integration/etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
<preference for="Magento\Framework\Oauth\TokenProviderInterface" type="Magento\Integration\Model\Oauth\Token\Provider"/>
1515
<preference for="Magento\Integration\Api\CustomerTokenServiceInterface" type="Magento\Integration\Model\CustomerTokenService" />
1616
<preference for="Magento\Integration\Api\AdminTokenServiceInterface" type="Magento\Integration\Model\AdminTokenService" />
17+
<preference for="Magento\Integration\Model\Oauth\Token\RequestLog\ReaderInterface" type="Magento\Integration\Model\ResourceModel\Oauth\Token\RequestLog" />
18+
<preference for="Magento\Integration\Model\Oauth\Token\RequestLog\WriterInterface" type="Magento\Integration\Model\ResourceModel\Oauth\Token\RequestLog" />
1719
<type name="Magento\Integration\Model\Oauth\Nonce\Generator">
1820
<arguments>
1921
<argument name="date" xsi:type="object">Magento\Framework\Stdlib\DateTime\DateTime\Proxy</argument>

0 commit comments

Comments
 (0)