Skip to content

BcZip: ZIP展開時にエントリパス検証を追加(Zip Slip対策)#4300

Merged
ryuring merged 4 commits intobaserproject:5.2.xfrom
kaminuma:hotfix/zip-slip-vulnerability
Jan 16, 2026
Merged

BcZip: ZIP展開時にエントリパス検証を追加(Zip Slip対策)#4300
ryuring merged 4 commits intobaserproject:5.2.xfrom
kaminuma:hotfix/zip-slip-vulnerability

Conversation

@kaminuma
Copy link
Contributor

@kaminuma kaminuma commented Dec 23, 2025

概要

BcZipクラスのZIP展開処理において、ZIP内エントリのパスを事前に検証し、
展開先ディレクトリ配下に収まらないエントリを含むZIPを拒否する処理を追加しました。
Zip Slip(パストラバーサル)脆弱性への対策となります。

補足(設計意図)

本PRは、ZIP 展開処理におけるパス検証の欠如を補う目的で、
Defense in Depth の観点から実装したものになります。

変更点

  • BcZip::_extractByPhpLib および _extractByCommand メソッドに、ZIPエントリのパス検証処理を追加しました。
  • ZIP内の各エントリについて、正規化後の展開先パスが指定された展開先ディレクトリ($target)配下に収まっているかをチェックします。
  • 展開先ディレクトリ配下に収まらないエントリを含むZIPは、展開処理を中断します。
  • 意図しないディレクトリへの書き込み(Zip Slip / パストラバーサル)を防止します。
  • 関連するユニットテストを追加しました。

背景

ZIP展開処理は、実行環境や展開経路(ZipArchive / unzip コマンド)によって挙動が異なる可能性があります。
そのため、ランタイムやライブラリ側の挙動に依存せず、アプリケーション側で明示的にZIPエントリの検証を行うことで、Defense in Depth(多層防御)の観点から安全性を向上させています。

動作確認

1. ユニットテスト

BcZipTest.php に追加したテストケースが通過することを確認しました。

2. 管理画面での動作確認(GUI)

正常系(リグレッション確認)

修正適用後、通常のテーマ・プラグインのアップロード機能に影響がないことを確認しました。

  • テーマ管理:管理画面 > テーマ管理 > 新規追加
    正常なテーマZIPをアップロード → 正常にインストール完了
  • プラグイン管理:管理画面 > プラグイン管理 > 新規追加
    正常なプラグインZIPをアップロード → 正常にインストール完了

異常系(脆弱性対策確認)

不正なパス(例:../ を含むエントリ)を含むZIPファイルが、適切に拒否されることを確認しました。

  • テーマ管理 / プラグイン管理ともに
    不正なZIPをアップロード → エラーとなり、展開されないことを確認

ZIP内エントリのパスを事前に検証し、
展開先ディレクトリ配下に収まらないパスを拒否する処理を追加。
Copilot AI review requested due to automatic review settings December 23, 2025 12:22
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses the Zip Slip (path traversal) vulnerability in the BcZip class by adding validation to ensure ZIP entry paths remain within the intended extraction directory. The implementation follows a defense-in-depth approach by validating paths before extraction regardless of runtime behavior.

Key Changes:

  • Added pre-extraction path validation to both _extractByPhpLib and _extractByCommand methods
  • Implemented helper methods for path normalization and safety checks (_normalizeTargetPath, _normalizeRelativePath, _normalizeAbsolutePath, _isZipEntrySafe, _validateZipEntries)
  • Added unit tests to verify rejection of malicious ZIP files containing path traversal attempts

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 16 comments.

File Description
plugins/baser-core/src/Utility/BcZip.php Implemented ZIP entry path validation logic with new helper methods to detect and reject malicious paths (e.g., ../, absolute paths, null bytes) before extraction
plugins/baser-core/tests/TestCase/Utility/BcZipTest.php Added unit tests for Zip Slip protection, path normalization, and entry safety validation to verify the security fixes work correctly

- Zip Slip脆弱性対策として、展開先パスの検証ロジックを強化
- ユニットテストに親ディレクトリへのファイル流出チェックを追加
- Copilot指摘事項の修正: Zip Slip脆弱性対応の強化とテスト追加
- エラーメッセージを __d() 関数で国際化対応
- 各メソッドに @checked, @notodo, @UnitTest アノテーションを追加
- _isZipEntrySafe メソッドの空文字チェックを調整
指摘の通り、上記を含めるとリスクがあるので、
指摘通り削除します。

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.

@momofff momofff added this to the 5.2.2 milestone Jan 15, 2026
@ryuring
Copy link
Collaborator

ryuring commented Jan 16, 2026

@kaminuma 手動での動作確認も問題ありませんでしたのでマージさせて頂きます。ありがとうございました。

@ryuring ryuring merged commit 79010a7 into baserproject:5.2.x Jan 16, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants