MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / validateZipFile

Function validateZipFile

source code/utils/dxt/zip.ts:65–104  ·  view source on GitHub ↗
(
  file: ZipFileMetadata,
  state: ZipValidationState,
)

Source from the content-addressed store, hash-verified

63 * Validates a single file during zip extraction
64 */
65export function validateZipFile(
66 file: ZipFileMetadata,
67 state: ZipValidationState,
68): FileValidationResult {
69 state.fileCount++
70
71 let error: string | undefined
72
73 // Check file count
74 if (state.fileCount > LIMITS.MAX_FILE_COUNT) {
75 error = `Archive contains too many files: ${state.fileCount} (max: ${LIMITS.MAX_FILE_COUNT})`
76 }
77
78 // Validate path safety
79 if (!isPathSafe(file.name)) {
80 error = `Unsafe file path detected: "${file.name}". Path traversal or absolute paths are not allowed.`
81 }
82
83 // Check individual file size
84 const fileSize = file.originalSize || 0
85 if (fileSize > LIMITS.MAX_FILE_SIZE) {
86 error = `File "${file.name}" is too large: ${Math.round(fileSize / 1024 / 1024)}MB (max: ${Math.round(LIMITS.MAX_FILE_SIZE / 1024 / 1024)}MB)`
87 }
88
89 // Track total uncompressed size
90 state.totalUncompressedSize += fileSize
91
92 // Check total size
93 if (state.totalUncompressedSize > LIMITS.MAX_TOTAL_SIZE) {
94 error = `Archive total size is too large: ${Math.round(state.totalUncompressedSize / 1024 / 1024)}MB (max: ${Math.round(LIMITS.MAX_TOTAL_SIZE / 1024 / 1024)}MB)`
95 }
96
97 // Check compression ratio for zip bomb detection
98 const currentRatio = state.totalUncompressedSize / state.compressedSize
99 if (currentRatio > LIMITS.MAX_COMPRESSION_RATIO) {
100 error = `Suspicious compression ratio detected: ${currentRatio.toFixed(1)}:1 (max: ${LIMITS.MAX_COMPRESSION_RATIO}:1). This may be a zip bomb.`
101 }
102
103 return error ? { isValid: false, error } : { isValid: true }
104}
105
106/**
107 * Unzips data from a Buffer and returns its contents as a record of file paths to Uint8Array data.

Callers 1

unzipFileFunction · 0.85

Calls 1

isPathSafeFunction · 0.85

Tested by

no test coverage detected