Skip to content

Commit

Permalink
Merge pull request #5867 from nanasess/file_uploadable_extensions
Browse files Browse the repository at this point in the history
ファイル管理で許可した拡張子以外アップロード不可に変更
  • Loading branch information
chihiro-adachi committed Jan 12, 2023
2 parents 523a28b + ac0ed8e commit bfddf0b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 33 deletions.
14 changes: 14 additions & 0 deletions app/config/eccube/packages/eccube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,19 @@ parameters:
- admin_content_css
- admin_content_js
- admin_store_template_install
eccube_file_uploadable_extensions: # ファイル管理でアップロード可能な拡張子
- jpg
- jpeg
- png
- gif
- webp
- svg
- ico
- html
- htm
- js
- css
- txt
- pdf
eccube_login_throttling_max_attempts: 5
eccube_login_throttling_interval: '30 minutes'
2 changes: 1 addition & 1 deletion codeception/acceptance/EA06ContentsManagementCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function contentsmanagement_ファイル管理_php(AcceptanceTester $I)
->入力_ファイル('upload.php')
->アップロード();

$I->see('phpファイルはアップロードできません', '#form1 .errormsg');
$I->see('アップロードできないファイル拡張子です。', '#form1 .errormsg');
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Eccube/Controller/Admin/Content/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,14 @@ public function upload(Request $request)
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'));
}
// dotファイルはアップロード不可
if (strpos($filename, '.') === 0) {
throw new UnsupportedMediaTypeHttpException(trans('admin.content.file.dotfile_error'));
}
// 許可した拡張子以外アップロード不可
if (!in_array(strtolower($file->getClientOriginalExtension()), $this->eccubeConfig['eccube_file_uploadable_extensions'], true)) {
throw new UnsupportedMediaTypeHttpException(trans('admin.content.file.extension_error'));
}
} catch (UnsupportedMediaTypeHttpException $e) {
if (!in_array($e->getMessage(), array_column($this->errors, 'message'))) {
$this->errors[] = ['message' => $e->getMessage()];
Expand Down
3 changes: 2 additions & 1 deletion src/Eccube/Resource/locale/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -990,8 +990,9 @@ admin.content.file.folder_name_symbol_error: The folder name contains invalid ch
admin.content.file.folder_name_period_error: Folder names beginning with a period(.) are not allowed.
admin.content.file.dir_exists: '%file_name% is already exists.'
admin.content.file.same_name_folder_exists: 'Cannot upload because a folder with the same name as the file exists.'
admin.content.file.phpfile_error: 'php files cannot be uploaded.'
admin.content.file.phpfile_error: 'php files cannot be uploaded.' # Unused
admin.content.file.dotfile_error: 'Dot files cannot be uploaded.'
admin.content.file.extension_error: 'File extension cannot be uploaded.'
admin.content.layout_delete: Delete Layouts
admin.content.layout_no_page: Page not registered
admin.content.layout__card_title: Layout Overview
Expand Down
3 changes: 2 additions & 1 deletion src/Eccube/Resource/locale/messages.ja.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -990,8 +990,9 @@ admin.content.file.folder_name_symbol_error: 使用できない文字が含ま
admin.content.file.folder_name_period_error: ピリオド(.)で始まる名前は使用できません。
admin.content.file.dir_exists: '%file_name% は既に使用されています。別のフォルダ名を入力してください'
admin.content.file.same_name_folder_exists: 'ファイルと同じ名前のフォルダが存在するためアップロードできません。'
admin.content.file.phpfile_error: 'phpファイルはアップロードできません。'
admin.content.file.phpfile_error: 'phpファイルはアップロードできません。' # 未使用
admin.content.file.dotfile_error: '.で始まるファイルはアップロードできません。'
admin.content.file.extension_error: 'アップロードできないファイル拡張子です。'
admin.content.layout_delete: レイアウトを削除
admin.content.layout_no_page: ページが登録されていません
admin.content.layout__card_title: レイアウト概要
Expand Down
82 changes: 56 additions & 26 deletions tests/Eccube/Tests/Web/Admin/Content/FileControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,26 +204,55 @@ public function testIndexWithUpload()
$this->assertTrue(file_exists($this->getUserDataDir().'/bbb.html'));
}

public function testUploadIgnoreFiles()
public function dataProviderUploadIgnoreFiles(): array
{
$php = $this->getUserDataDir().'/../test.php';
touch($php);

$dot = $this->getUserDataDir().'/../.dotfile';
touch($dot);

$phpfile = new UploadedFile(
realpath($php), // file path
'test.php', // original name
'x-php', // mimeType
null, // error
true // test mode
);
return [
['test.php', 'x-php', 'アップロードできないファイル拡張子です', false],
['.dotfile', 'text/plain', '.で始まるファイルはアップロードできません。', false],
['test.jpg', 'image/jpeg', '', true],
['test.jpeg', 'image/jpeg', '', true],
['test.png', 'image/png', '', true],
['test.gif', 'image/gif', '', true],
['test.webp', 'image/webp', '', true],
['test.svg', 'image/svg+xml', '', true],
['test.ico', 'image/ico', '', true],
['test.html', 'text/html', '', true],
['test.htm', 'text/htm', '', true],
['test.js', 'text/javascript', '', true],
['test.css', 'text/css', '', true],
['test.txt', 'text/txt', '', true],
['test.pdf', 'application/pdf', '', true],
['test.zip', 'application/zip', 'アップロードできないファイル拡張子です', false],
['test.gz', 'application/gzip', 'アップロードできないファイル拡張子です', false],
['test.tar', 'application/tar', 'アップロードできないファイル拡張子です', false],
['test.doc', 'application/msword', 'アップロードできないファイル拡張子です', false],
['test.xls', 'application/vnd.ms-excel', 'アップロードできないファイル拡張子です', false],
['test.ppt', 'application/vnd.ms-powerpoint', 'アップロードできないファイル拡張子です', false],
['test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'アップロードできないファイル拡張子です', false],
['test.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'アップロードできないファイル拡張子です', false],
['test.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'アップロードできないファイル拡張子です', false],
['test.woff', 'application/font-woff', 'アップロードできないファイル拡張子です', false],
['test.woff2', 'application/font-woff2', 'アップロードできないファイル拡張子です', false],
['test.ttf', 'application/font-ttf', 'アップロードできないファイル拡張子です', false],
['test.otf', 'application/font-otf', 'アップロードできないファイル拡張子です', false],
['test.eot', 'application/vnd.ms-fontobject', 'アップロードできないファイル拡張子です', false],
['test.xml', 'text/xml', 'アップロードできないファイル拡張子です', false],
['test.csv', 'text/csv', 'アップロードできないファイル拡張子です', false],
['test.json', 'application/json', 'アップロードできないファイル拡張子です', false],
];
}
/**
* @dataProvider dataProviderUploadIgnoreFiles
*/
public function testUploadIgnoreFiles($fileName, $mimeType, $errorMessage, $exists)
{
$file = $this->getUserDataDir().'/../'.$fileName;
touch($file);

$dotfile = new UploadedFile(
realpath($dot), // file path
'.dotfile', // original name
'text/plain', // mimeType
$uploadFile = new UploadedFile(
realpath($file), // file path
$file, // original name
$mimeType, // mimeType
null, // error
true // test mode
);
Expand All @@ -235,12 +264,12 @@ public function testUploadIgnoreFiles()
'form' => [
'_token' => 'dummy',
'create_file' => '',
'file' => [$phpfile, $dotfile],
'file' => [$uploadFile],
],
'mode' => 'upload',
'now_dir' => '/',
],
['form' => ['file' => [$phpfile, $dotfile]]]
['form' => ['file' => [$uploadFile]]]
);

$messages = $crawler->filter('p.errormsg')->each(function (Crawler $node) {
Expand All @@ -249,13 +278,14 @@ public function testUploadIgnoreFiles()

$this->assertTrue($this->client->getResponse()->isSuccessful());

$this->assertContains('phpファイルはアップロードできません。', $messages);
$this->assertContains('.で始まるファイルはアップロードできません。', $messages);
$this->assertFalse(file_exists($this->getUserDataDir().'/test.php'));
$this->assertFalse(file_exists($this->getUserDataDir().'/.dotfile'));
$this->assertStringContainsString($errorMessage, implode(',', $messages));
$this->assertSame($exists, file_exists($this->getUserDataDir().'/'.$fileName));

unlink($php);
unlink($dot);
if ($exists) {
unlink($this->getUserDataDir().'/'.$fileName);
} else {
unlink($file);
}
}

public function testUploadInvalidFileName()
Expand Down

0 comments on commit bfddf0b

Please sign in to comment.