Skip to content

Commit 2c7cd74

Browse files
author
Bohdan Korablov
committed
MAGETWO-46855: Block Cache Exploit
1 parent e7abf62 commit 2c7cd74

File tree

3 files changed

+76
-33
lines changed

3 files changed

+76
-33
lines changed

dev/tests/integration/testsuite/Magento/Framework/View/Element/AbstractBlockTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Framework\View\Element;
77

8+
use Magento\Framework\View\Element\AbstractBlock;
9+
810
/**
911
* @magentoAppIsolation enabled
1012
*/
@@ -556,7 +558,7 @@ public function testGetCacheKey()
556558
$this->assertNotEquals($name, $key);
557559

558560
$block->setCacheKey('key');
559-
$this->assertEquals('key', $block->getCacheKey());
561+
$this->assertEquals(AbstractBlock::CACHE_KEY_PREFIX . 'key', $block->getCacheKey());
560562
}
561563

562564
/**

lib/internal/Magento/Framework/View/Element/AbstractBlock.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ abstract class AbstractBlock extends \Magento\Framework\DataObject implements Bl
2424
*/
2525
const CACHE_GROUP = \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER;
2626

27+
/**
28+
* Prefix for cache key of block
29+
*/
30+
const CACHE_KEY_PREFIX = 'BLOCK_';
31+
2732
/**
2833
* Design
2934
*
@@ -958,7 +963,7 @@ public function getCacheKeyInfo()
958963
public function getCacheKey()
959964
{
960965
if ($this->hasData('cache_key')) {
961-
return $this->getData('cache_key');
966+
return static::CACHE_KEY_PREFIX . $this->getData('cache_key');
962967
}
963968
/**
964969
* don't prevent recalculation by saving generated cache key
@@ -970,7 +975,7 @@ public function getCacheKey()
970975
// ignore array keys
971976
$key = implode('|', $key);
972977
$key = sha1($key);
973-
return $key;
978+
return static::CACHE_KEY_PREFIX . $key;
974979
}
975980

976981
/**

lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,30 @@
88

99
namespace Magento\Framework\View\Test\Unit\Element;
1010

11+
use Magento\Framework\View\Element\AbstractBlock;
12+
use Magento\Framework\View\Element\Context;
13+
use Magento\Framework\Config\View;
14+
use Magento\Framework\View\ConfigInterface;
15+
1116
class AbstractBlockTest extends \PHPUnit_Framework_TestCase
1217
{
18+
/**
19+
* @var AbstractBlock
20+
*/
21+
protected $block;
22+
23+
/**
24+
* @return void
25+
*/
26+
protected function setUp()
27+
{
28+
$contextMock = $this->getMock(Context::class, [], [], '', false);
29+
$this->block = $this->getMockForAbstractClass(
30+
AbstractBlock::class,
31+
['context' => $contextMock]
32+
);
33+
}
34+
1335
/**
1436
* @param string $expectedResult
1537
* @param string $nameInLayout
@@ -18,11 +40,8 @@ class AbstractBlockTest extends \PHPUnit_Framework_TestCase
1840
*/
1941
public function testGetUiId($expectedResult, $nameInLayout, $methodArguments)
2042
{
21-
/** @var $block \Magento\Framework\View\Element\AbstractBlock|\PHPUnit_Framework_MockObject_MockObject */
22-
$block = $this->getMockForAbstractClass('Magento\Framework\View\Element\AbstractBlock', [], '', false);
23-
$block->setNameInLayout($nameInLayout);
24-
25-
$this->assertEquals($expectedResult, call_user_func_array([$block, 'getUiId'], $methodArguments));
43+
$this->block->setNameInLayout($nameInLayout);
44+
$this->assertEquals($expectedResult, call_user_func_array([$this->block, 'getUiId'], $methodArguments));
2645
}
2746

2847
/**
@@ -57,46 +76,63 @@ public function getUiIdDataProvider()
5776
];
5877
}
5978

79+
/**
80+
* @return void
81+
*/
6082
public function testGetVar()
6183
{
62-
$this->markTestIncomplete('MAGETWO-11727');
63-
$config = $this->getMock('Magento\Framework\Config\View', ['getVarValue'], [], '', false);
84+
$config = $this->getMock(View::class, ['getVarValue'], [], '', false);
6485
$module = uniqid();
65-
$config->expects(
66-
$this->at(0)
67-
)->method(
68-
'getVarValue'
69-
)->with(
70-
'Magento_Theme',
71-
'v1'
72-
)->will(
73-
$this->returnValue('one')
74-
);
75-
$config->expects($this->at(1))->method('getVarValue')->with($module, 'v2')->will($this->returnValue('two'));
7686

77-
$configManager = $this->getMock('Magento\Framework\View\ConfigInterface', [], [], '', false);
78-
$configManager->expects($this->exactly(2))->method('getViewConfig')->will($this->returnValue($config));
87+
$config->expects($this->any())
88+
->method('getVarValue')
89+
->willReturnMap([
90+
['Magento_Theme', 'v1', 'one'],
91+
[$module, 'v2', 'two']
92+
]);
93+
94+
$configManager = $this->getMock(ConfigInterface::class, [], [], '', false);
95+
$configManager->expects($this->exactly(2))->method('getViewConfig')->willReturn($config);
7996

80-
/** @var $block \Magento\Framework\View\Element\AbstractBlock|\PHPUnit_Framework_MockObject_MockObject */
97+
/** @var $block AbstractBlock|\PHPUnit_Framework_MockObject_MockObject */
8198
$params = ['viewConfig' => $configManager];
8299
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
83100
$block = $this->getMockForAbstractClass(
84-
'Magento\Framework\View\Element\AbstractBlock',
85-
$helper->getConstructArguments('Magento\Framework\View\Element\AbstractBlock', $params),
86-
uniqid('Magento\\Theme\\Block\\AbstractBlock\\')
101+
AbstractBlock::class,
102+
$helper->getConstructArguments(AbstractBlock::class, $params)
87103
);
104+
$block->setData('module_name', 'Magento_Theme');
88105

89106
$this->assertEquals('one', $block->getVar('v1'));
90107
$this->assertEquals('two', $block->getVar('v2', $module));
91108
}
92109

110+
/**
111+
* @return void
112+
*/
93113
public function testIsScopePrivate()
94114
{
95-
$contextMock = $this->getMock('Magento\Framework\View\Element\Context', [], [], '', false);
96-
$block = $this->getMockForAbstractClass(
97-
'Magento\Framework\View\Element\AbstractBlock',
98-
['context' => $contextMock]
99-
);
100-
$this->assertEquals(false, $block->isScopePrivate());
115+
$this->assertFalse($this->block->isScopePrivate());
116+
}
117+
118+
/**
119+
* @return void
120+
*/
121+
public function testGetCacheKey()
122+
{
123+
$cacheKey = 'testKey';
124+
$this->block->setData('cache_key', $cacheKey);
125+
$this->assertEquals(AbstractBlock::CACHE_KEY_PREFIX . $cacheKey, $this->block->getCacheKey());
126+
}
127+
128+
/**
129+
* @return void
130+
*/
131+
public function testGetCacheKeyByName()
132+
{
133+
$nameInLayout = 'testBlock';
134+
$this->block->setNameInLayout($nameInLayout);
135+
$cacheKey = sha1($nameInLayout);
136+
$this->assertEquals(AbstractBlock::CACHE_KEY_PREFIX . $cacheKey, $this->block->getCacheKey());
101137
}
102138
}

0 commit comments

Comments
 (0)