アーカイブ展開時のパストラバーサル(Zip Slip)

アーカイブ展開時のパストラバーサル(Zip Slip)

説明

ZIPの項目名からファイルを作る前に、書き込み先を展開ディレクトリ内に限定してください。絶対パス、親ディレクトリへの移動、シンボリックリンクにより、保存先が変わることがあります。

想定される影響

サービスアカウントが書き込めるファイルが上書きされると、設定変更やサービス停止につながります。より強い権限のプロセスが後でそのファイルを読んだり実行したりする場合は、情報漏えいや権限昇格のおそれもあります。

対処方法

  • 信頼できる親の下に新しい非公開ディレクトリを作り、必要なファイル構造だけを許可します。アーカイブ内のリンクは復元しないでください。
  • 絶対パスと親への移動を拒否し、base.resolve(entryName).normalize() の結果を Path.startsWith(base) で確かめます。階層が必要な場合は、信頼できるディレクトリを基準にリンクをたどらず各親を開いてから子要素を作成します。
  • 最小限の権限で展開し、CREATE_NEW と NOFOLLOW_LINKS で新規ファイルを作成します。

例

変更前

java
import java.io.*;
import java.util.zip.*;
public class UnsafeUnzipper {
    public void extract(InputStream zipStream, File destDir) throws IOException {
        ZipInputStream zis = new ZipInputStream(zipStream);
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // エントリ名を保存先へ直接結合
            File outFile = new File(destDir, entry.getName());
            if (entry.isDirectory()) {
                outFile.mkdirs();
                continue;
            }
            outFile.getParentFile().mkdirs();
            try (OutputStream os = new FileOutputStream(outFile)) {
                byte[] buf = new byte[8192];
                int len;
                while ((len = zis.read(buf)) != -1) {
                    os.write(buf, 0, len);
                }
            }
        }
    }
}

変更後

Java 11以降の例です。destDir は他の利用者が変更できないようにしてください。展開した実際のバイト数と項目数には別途上限を設けます。

java
import java.io.*;
import java.nio.channels.*;
import java.nio.file.*;
import java.util.*;
import java.util.zip.*;
public class SafeUnzipper {
    public void extract(InputStream zipStream, File destDir) throws IOException {
        // アプリケーション管理下に今回の展開専用の非公開ディレクトリを作成
        Path serviceRoot = destDir.toPath().toRealPath();
        Path base = Files.createTempDirectory(serviceRoot, "extract-")
                .toRealPath();
        try (ZipInputStream zis = new ZipInputStream(zipStream)) {
            ZipEntry entry;
            byte[] buf = new byte[8192];
            while ((entry = zis.getNextEntry()) != null) {
                String name = entry.getName();
                Path entryPath = Path.of(name);
                // 通常のファイル名だけを許可し、リンクになり得る親ディレクトリを作らない
                if (entry.isDirectory() || name.isBlank() || entryPath.isAbsolute()
                        || name.indexOf('/') >= 0 || name.indexOf('\\') >= 0
                        || name.equals(".") || name.equals("..")) {
                    throw new IOException("Invalid zip entry name: " + name);
                }

                Path target = base.resolve(entryPath).normalize();
                if (!target.startsWith(base) || !target.getParent().equals(base)) {
                    throw new IOException("Zip Slip detected: " + name);
                }

                Set<OpenOption> options = Set.of(
                        StandardOpenOption.CREATE_NEW,
                        StandardOpenOption.WRITE,
                        LinkOption.NOFOLLOW_LINKS);
                try (SeekableByteChannel channel = Files.newByteChannel(target, options);
                     OutputStream os = Channels.newOutputStream(channel)) {
                    int len;
                    while ((len = zis.read(buf)) != -1) {
                        os.write(buf, 0, len);
                    }
                }
            }
        }
    }
}

各項目には階層のないファイル名を指定します。各ファイルは新しいディレクトリの直下に作成されます。同名のファイルがあれば CREATE_NEW が作成を拒否します。

参考資料