fix(phase-5.2): 消项 CR 第二轮 P1/P2/P3(通道/证据链/交付时序/后管证据页)
This commit is contained in:
+86
@@ -0,0 +1,86 @@
|
||||
package com.nanri.aiimage.common.security;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
/**
|
||||
* 安装包/教程包凭据件扫描(P2-4,对齐工作台包的 CR-02 口径)。
|
||||
*
|
||||
* <p>版本包与教程包此前只校验扩展名和体积,压缩包里是否带 {@code .env}/{@code *.pem}
|
||||
* 没人看;而它们会经公开 client 桶发给全部客户端。这里只读 zip 中央目录、按条目名判,
|
||||
* 不解压内容,代价与包体积无关。</p>
|
||||
*/
|
||||
public final class CredentialPackageGuard {
|
||||
|
||||
private static final Set<String> FORBIDDEN_NAMES = Set.of(
|
||||
"kw_share_cfg.json", ".env", ".netrc", ".git-credentials",
|
||||
"id_rsa", "id_dsa", "id_ecdsa", "id_ed25519");
|
||||
|
||||
private static final List<String> FORBIDDEN_SUFFIXES =
|
||||
List.of(".pem", ".key", ".p12", ".pfx", ".jks", ".keystore");
|
||||
|
||||
private static final int MAX_ENTRIES = 200_000;
|
||||
|
||||
private CredentialPackageGuard() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回第一个命中凭据名的条目;没有命中返回 {@code null}。
|
||||
*
|
||||
* @throws IOException 文件不是可读 zip / 条目数超限
|
||||
*/
|
||||
public static String firstForbiddenEntry(File packageFile) throws IOException {
|
||||
if (packageFile == null || !packageFile.isFile()) {
|
||||
throw new IOException("package file missing");
|
||||
}
|
||||
try (ZipFile zipFile = new ZipFile(packageFile)) {
|
||||
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
int scanned = 0;
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = entries.nextElement();
|
||||
if (++scanned > MAX_ENTRIES) {
|
||||
throw new IOException("zip entry limit exceeded");
|
||||
}
|
||||
if (entry.isDirectory()) {
|
||||
continue;
|
||||
}
|
||||
String base = baseName(entry.getName());
|
||||
if (isCredentialFileName(base)) {
|
||||
return entry.getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static boolean isCredentialFileName(String fileName) {
|
||||
String lower = fileName == null ? "" : fileName.trim().toLowerCase(Locale.ROOT);
|
||||
if (lower.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (FORBIDDEN_NAMES.contains(lower) || lower.startsWith(".env.")) {
|
||||
return true;
|
||||
}
|
||||
for (String suffix : FORBIDDEN_SUFFIXES) {
|
||||
if (lower.endsWith(suffix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String baseName(String entryName) {
|
||||
if (entryName == null) {
|
||||
return "";
|
||||
}
|
||||
String normalized = entryName.replace('\\', '/');
|
||||
int slash = normalized.lastIndexOf('/');
|
||||
return slash < 0 ? normalized : normalized.substring(slash + 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user