ファイル形式をzipしか受け入れないようにチェックするバリデーションを追加 - #4476
Merged
Merged
Conversation
teratai3
reviewed
Jul 22, 2026
| if (!$zip->extract(TMP . $name, TMP)) { | ||
| $finfo = new \finfo(FILEINFO_MIME_TYPE); | ||
| $mimeType = $finfo->file(TMP . $name); | ||
| if (!in_array($mimeType, ['application/zip', 'application/x-zip-compressed'], true) || !$zip->extract(TMP . $name, TMP)) { |
Contributor
There was a problem hiding this comment.
すでに extract メソッドは他の箇所でも使われており、このMIME typeのチェックは、ほぼセットで呼び出す必要があると認識しています。
このPRいただいた形だと、今後ほかの箇所で対応する際にも同じ処理をコピーする前提になっており、修正漏れや後から処理を変更するコスト高そうです。
AIで実装する場合も既存コードを参考にすることが多いため、明確な指示をしない限り、同じ形で処理が増えていく可能性はありそうです。
- https://github.com/baserproject/basercms/blob/5.2.8/plugins/baser-core/src/Service/ThemesService.php#L155
- https://github.com/baserproject/basercms/blob/5.2.8/plugins/baser-core/src/Service/PluginsService.php#L360
※ updateCoreFiles は、固定のzipなので、必須ではありませんが、ここでもextractは呼び出されています。
Contributor
There was a problem hiding this comment.
Pull request overview
プラグインアップロード(PluginsService::add())に対して、アップロードされたファイルのMIMEタイプを検査し、ZIP形式のみ受け付けるようにすることで、誤ったファイル形式の混入を早期に弾くことを目的としたPRです。
Changes:
- アップロードされた一時ファイルのMIMEタイプを
finfo(FILEINFO_MIME_TYPE)で判定する処理を追加 - ZIP(
application/zip,application/x-zip-compressed)以外の場合に展開処理へ進まないようガードを追加
・MIME不一致のケースと、ZIPファイルの展開に失敗したケースを分ける ・例外前は1時ファイルを削除() [ PluginsServiceTest.php ] ・MIME不一致の際のテストの追加
- ext-fileinfo が無効な環境で finfo クラスが存在せず致命的エラーになるため、
class_exists() でガードし、判定できない場合は展開時のエラーに委ねるようにした。
レンタルサーバー等で無効化されている可能性があるため、require ではなく
suggest に記載して任意依存として扱う。
既存の BcBlog RssHelper も function_exists('mime_content_type') で
同様にガードしている。
- 「ZIPファイルをアップロードしてください。」を英語の翻訳ファイルに追加。
同じメソッド内の既存メッセージは登録済みのため揃えた。
- application/x-zip-compressed はブラウザが申告する MIME であり finfo が
返す値ではない旨をコメントで補足。誤解を避けるため残しつつ意図を明示した。
- 失敗時のテンポラリファイル削除が2箇所に重複していたため、try/catch で
1箇所にまとめた。
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Collaborator
|
取り急ぎは、本件だけを対象としてコミット内容を調整しました。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@ryuring
MIMEタイプをチェックして、zipしか受け入れないようにバリデーションを追加しました。ご確認宜しくお願いします!