Skip to content

Commit 34bd0e2

Browse files
MAGETWO-70325: Add filename paramenter log #4147
2 parents 1c047ed + 256335c commit 34bd0e2

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

lib/internal/Magento/Framework/Logger/Handler/Base.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,50 @@ class Base extends StreamHandler
3131
/**
3232
* @param DriverInterface $filesystem
3333
* @param string $filePath
34+
* @param string $fileName
3435
*/
3536
public function __construct(
3637
DriverInterface $filesystem,
37-
$filePath = null
38+
$filePath = null,
39+
$fileName = null
3840
) {
3941
$this->filesystem = $filesystem;
42+
if (!empty($fileName)) {
43+
$this->fileName = $this->sanitizeFileName($fileName);
44+
}
4045
parent::__construct(
41-
$filePath ? $filePath . $this->fileName : BP . $this->fileName,
46+
$filePath ? $filePath . $this->fileName : BP . DIRECTORY_SEPARATOR . $this->fileName,
4247
$this->loggerType
4348
);
49+
4450
$this->setFormatter(new LineFormatter(null, null, true));
4551
}
4652

53+
/**
54+
* @param string $fileName
55+
*
56+
* @return string
57+
* @throws \InvalidArgumentException
58+
*/
59+
private function sanitizeFileName($fileName)
60+
{
61+
if (!is_string($fileName)) {
62+
throw new \InvalidArgumentException('Filename expected to be a string');
63+
}
64+
65+
$parts = explode('/', $fileName);
66+
$parts = array_filter($parts, function ($value) {
67+
return !in_array($value, ['', '.', '..']);
68+
});
69+
70+
return implode('/', $parts);
71+
}
72+
4773
/**
4874
* @{inheritDoc}
4975
*
5076
* @param $record array
77+
*
5178
* @return void
5279
*/
5380
public function write(array $record)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Logger\Test\Unit\Handler;
7+
8+
class BaseTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Framework\Logger\Handler\Base|\PHPUnit_Framework_MockObject_MockObject
12+
*/
13+
private $model;
14+
15+
/**
16+
* @var \ReflectionMethod
17+
*/
18+
private $sanitizeMethod;
19+
20+
protected function setUp()
21+
{
22+
$driverMock = $this->getMockBuilder(\Magento\Framework\Filesystem\DriverInterface::class)
23+
->disableOriginalConstructor()
24+
->getMock();
25+
$this->model = new \Magento\Framework\Logger\Handler\Base($driverMock);
26+
27+
$class = new \ReflectionClass($this->model);
28+
$this->sanitizeMethod = $class->getMethod('sanitizeFileName');
29+
$this->sanitizeMethod->setAccessible(true);
30+
}
31+
32+
public function testSanitizeEmpty()
33+
{
34+
$this->assertEquals('', $this->sanitizeMethod->invokeArgs($this->model, ['']));
35+
}
36+
37+
public function testSanitizeSimpleFilename()
38+
{
39+
$this->assertEquals('custom.log', $this->sanitizeMethod->invokeArgs($this->model, ['custom.log']));
40+
}
41+
42+
public function testSanitizeLeadingSlashFilename()
43+
{
44+
$this->assertEquals(
45+
'customfolder/custom.log',
46+
$this->sanitizeMethod->invokeArgs($this->model, ['/customfolder/custom.log'])
47+
);
48+
}
49+
50+
public function testSanitizeParentLevelFolder()
51+
{
52+
$this->assertEquals(
53+
'var/hack/custom.log',
54+
$this->sanitizeMethod->invokeArgs($this->model, ['../../../var/hack/custom.log'])
55+
);
56+
}
57+
58+
/**
59+
* @expectedException \InvalidArgumentException
60+
* @expectedExceptionMessage Filename expected to be a string
61+
*/
62+
public function testSanitizeFileException()
63+
{
64+
$this->sanitizeMethod->invokeArgs($this->model, [['filename' => 'notValid']]);
65+
}
66+
}

0 commit comments

Comments
 (0)