Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,17 @@ public function __construct(
*/
public function execute()
{
$invoiceId = $this->getRequest()->getParam('invoice_id');
$invoiceId = (int) $this->getRequest()->getParam('invoice_id');
if ($invoiceId) {
$invoice = $this->_objectManager->create(
\Magento\Sales\Api\InvoiceRepositoryInterface::class
)->get($invoiceId);
if ($invoice) {
$pdf = $this->_objectManager->create(\Magento\Sales\Model\Order\Pdf\Invoice::class)->getPdf([$invoice]);
$date = $this->_objectManager->get(
\Magento\Framework\Stdlib\DateTime\DateTime::class
)->date('Y-m-d_H-i-s');
return $this->_fileFactory->create(
'invoice' . $date . '.pdf',
"invoice_$invoiceId.pdf",
Copy link
Copy Markdown
Contributor

@phoenix128 phoenix128 Jun 24, 2018

Choose a reason for hiding this comment

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

Would it better to filter $invoiceId before? I see we are taking it directly from a request.
I am concerned about a potential path traversal attack.
I suppose changing line 51 would be just fine: $invoiceId = (int) $this->getRequest()->getParam('invoice_id');.

Copy link
Copy Markdown
Contributor Author

@rogyar rogyar Jun 24, 2018

Choose a reason for hiding this comment

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

Thanks, Riccardo. That's a very good point. I've added the mentioned change

$pdf->render(),
DirectoryList::VAR_DIR,
DirectoryList::PDF,
'application/pdf'
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public function testExecute()
->willReturnSelf();
$pdfMock->expects($this->once())
->method('render');
$dateTimeMock = $this->createMock(\Magento\Framework\Stdlib\DateTime\DateTime::class);

$invoiceRepository = $this->getMockBuilder(\Magento\Sales\Api\InvoiceRepositoryInterface::class)
->disableOriginalConstructor()
Expand All @@ -143,10 +142,7 @@ public function testExecute()
->method('create')
->with(\Magento\Sales\Model\Order\Pdf\Invoice::class)
->willReturn($pdfMock);
$this->objectManagerMock->expects($this->at(2))
->method('get')
->with(\Magento\Framework\Stdlib\DateTime\DateTime::class)
->willReturn($dateTimeMock);


$this->assertNull($this->controller->execute());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ class DirectoryList extends \Magento\Framework\Filesystem\DirectoryList
*/
const GENERATED_METADATA = 'metadata';

/**
* Relative directory key for generated PDFs
*/
const PDF = 'pdf';

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -164,6 +169,7 @@ public static function getDefaultConfig()
self::GENERATED => [parent::PATH => 'generated'],
self::GENERATED_CODE => [parent::PATH => Io::DEFAULT_DIRECTORY],
self::GENERATED_METADATA => [parent::PATH => 'generated/metadata'],
self::PDF => [parent::PATH => 'var/pdf'],
];
return parent::getDefaultConfig() + $result;
}
Expand Down