Skip to content

Commit

Permalink
Merge pull request #5760 from nanasess/add-check-invalid-filename
Browse files Browse the repository at this point in the history
[4.2] Add check invalid filename
  • Loading branch information
chihiro-adachi authored Sep 5, 2022
2 parents 2853db3 + 1d2c141 commit babbb9a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Eccube/Controller/Admin/Content/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ public function upload(Request $request)
if (is_dir(rtrim($nowDir, '/\\').\DIRECTORY_SEPARATOR.$filename)) {
throw new UnsupportedMediaTypeHttpException(trans('admin.content.file.same_name_folder_exists'));
}
// 英数字, 半角スペース, _-.() のみ許可
if (!preg_match('/\A[a-zA-Z0-9_\-\.\(\) ]+\Z/', $filename)) {
throw new UnsupportedMediaTypeHttpException(trans('admin.content.file.folder_name_symbol_error'));
}
// phpファイルはアップロード不可
if ($file->getClientOriginalExtension() === 'php') {
throw new UnsupportedMediaTypeHttpException(trans('admin.content.file.phpfile_error'));
Expand Down
39 changes: 39 additions & 0 deletions tests/Eccube/Tests/Web/Admin/Content/FileControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,45 @@ public function testUploadIgnoreFiles()
unlink($dot);
}

public function testUploadInvalidFileName()
{
$quote = $this->getUserDataDir()."/../'quote'.txt";
touch($quote);

$quotefile = new UploadedFile(
realpath($quote), // file path
"'quote'.txt", // original name
'text/plain', // mimeType
null, // error
true // test mode
);

$crawler = $this->client->request(
'POST',
$this->generateUrl('admin_content_file'),
[
'form' => [
'_token' => 'dummy',
'create_file' => '',
'file' => [$quotefile],
],
'mode' => 'upload',
'now_dir' => '/',
],
['form' => ['file' => [$quotefile]]]
);

$messages = $crawler->filter('p.errormsg')->each(function (Crawler $node) {
return $node->text();
});

$this->assertTrue($this->client->getResponse()->isSuccessful());
$this->assertContains('使用できない文字が含まれています。', $messages);
$this->assertFalse(file_exists($this->getUserDataDir()."/'quote'.txt"));

unlink($quote);
}

protected function getUserDataDir()
{
return __DIR__.'/../../../../../../html/user_data';
Expand Down

0 comments on commit babbb9a

Please sign in to comment.