Line data Source code
1 : #include "ArchiveExtractor.h"
2 : #include <filesystem>
3 : #include "libarchive_extract.h"
4 : #include "Logger.h"
5 : #include "str_utils.h"
6 : #include "PathSafety.h"
7 : #include "ProcessSpawn.h"
8 : #include <stdexcept>
9 : #include <string>
10 : #include "macros.h"
11 :
12 : using namespace std::filesystem;
13 :
14 : namespace Utils
15 : {
16 115 : ArchiveExtractor::ArchiveExtractor(const std::string& path, bool bExtractLzmaInProc /* = false*/, bool bExtractLzmaUsing7Zip /* = true*/)
17 115 : : m_path(path)
18 115 : , m_bExtractLzmaInProc(bExtractLzmaInProc)
19 115 : , m_bExtractLzmaUsing7Zip(bExtractLzmaUsing7Zip)
20 : {
21 115 : }
22 :
23 0 : path ArchiveExtractor::GetPath() const
24 : {
25 0 : return m_path;
26 : }
27 :
28 101 : std::string ArchiveExtractor::GetExtractionDirectory() const
29 : {
30 101 : return m_extractionDirectory;
31 : }
32 :
33 752 : bool ArchiveExtractor::IsArchiveTypeSupported(const path& file)
34 : {
35 : if (ALLOW_ALL_ARCHIVE_EXTENSION_TYPES) return true;
36 :
37 752 : for (const auto& ext : SupportedExtensions)
38 : {
39 752 : auto extLower = str_to_lower_copy(file.extension().string());
40 1504 : if (extLower == "." + std::string(ext))
41 : {
42 752 : return true;
43 : }
44 752 : }
45 :
46 0 : return false;
47 : }
48 :
49 0 : bool ArchiveExtractor::IsArchiveTypeSupported(const std::string& file)
50 : {
51 0 : return IsArchiveTypeSupported(path(file));
52 : }
53 :
54 105 : bool ArchiveExtractor::Extract()
55 : {
56 : //auto extractionPath = path.replace_extension().string();
57 105 : auto extractionPath = m_path.parent_path() / m_path.stem();
58 210 : return Extract(extractionPath.string());
59 105 : }
60 :
61 0 : bool Utils::ArchiveExtractor::ExtractLzmaInProc(const std::filesystem::path& destinationPath)
62 : {
63 0 : return false;
64 : }
65 :
66 1 : bool ArchiveExtractor::ExtractLzmaOutOfProc(const path& destinationPath)
67 : {
68 : // Security-audit F001: this used to concatenate the archive and
69 : // destination paths into a shell command string for std::system(), so
70 : // shell metacharacters inside archive-derived paths (nested step/layer
71 : // directory names reach here through getUncompressedFilePath) executed
72 : // arbitrary commands. The 7z tool is now spawned with an argv array:
73 : // every path travels as a single argument and is never re-parsed by a
74 : // shell. "-o<dir>" is one argv entry, so paths with spaces need no
75 : // quoting at all.
76 : // https://documentation.help/7-Zip/extract_full.htm
77 : const std::vector<std::string> args =
78 : {
79 : "x",
80 : m_path.string(),
81 1 : "-o" + destinationPath.string(),
82 : "-y", // yes to all prompts
83 : "-aoa", // overwrite all
84 7 : };
85 :
86 3 : loginfo("running 7z (argv spawn, no shell): x [" + m_path.string() + "] -o[" + destinationPath.string() + "]");
87 :
88 1 : auto exitCode = run_process("7z", args);
89 :
90 1 : if (exitCode < 0)
91 : {
92 0 : auto message = "failed to start 7z process (is 7z installed and on PATH?)";
93 0 : logerror(message);
94 0 : throw std::runtime_error(message);
95 : }
96 :
97 1 : if (exitCode != static_cast<int>(e7zExitCode::Success) &&
98 : exitCode != static_cast<int>(e7zExitCode::Warning))
99 : {
100 0 : auto message = "7z command failed (exit code = " + std::to_string(exitCode) + ")";
101 0 : message += ", cwd: [" + current_path().string() + "]";
102 0 : logerror(message);
103 0 : throw std::runtime_error(message.c_str());
104 0 : }
105 :
106 4 : loginfo("7z command succeeded");
107 :
108 1 : m_extractionDirectory = destinationPath.string();
109 1 : return true;
110 4 : }
111 :
112 115 : bool ArchiveExtractor::Extract(const std::string& destinationPath)
113 : {
114 230 : if (m_bExtractLzmaUsing7Zip &&
115 230 : std::find(LZMA_FILE_EXTENSIONS.begin(), LZMA_FILE_EXTENSIONS.end(), m_path.extension()) != LZMA_FILE_EXTENSIONS.end())
116 : {
117 1 : if (m_bExtractLzmaInProc)
118 : {
119 0 : return ExtractLzmaInProc(destinationPath);
120 : }
121 : else
122 : {
123 1 : return ExtractLzmaOutOfProc(destinationPath);
124 : }
125 : }
126 : else
127 : {
128 : // macOS temp/design locations sit behind symlinks (/var -> /private/var,
129 : // /tmp -> /private/tmp), and libarchive runs with
130 : // ARCHIVE_EXTRACT_SECURE_SYMLINKS, which rejects any entry whose path
131 : // traverses a symlink - extraction into such a destination fails on
132 : // every entry. Resolve the destination to its canonical real path
133 : // before handing it to libarchive (no-op on Linux/Windows).
134 114 : std::error_code canonicalError;
135 114 : auto canonicalDestination = weakly_canonical(path(destinationPath), canonicalError);
136 114 : if (canonicalError)
137 : {
138 0 : canonicalDestination = path(destinationPath);
139 : }
140 :
141 114 : if (extract(m_path.string().c_str(), canonicalDestination.string().c_str()))
142 : {
143 102 : m_extractionDirectory = canonicalDestination.string();
144 102 : return true;
145 : }
146 114 : }
147 :
148 12 : return false;
149 : }
150 :
151 22486 : /*static*/ path ArchiveExtractor::getUncompressedFilePath(const path& directory, const std::string& filename)
152 : {
153 22486 : path uncompressedPath;
154 :
155 : // Security-audit F001: filename comes from ODB++ file content (feature
156 : // list names, eda data names...) and is joined onto an extraction-root
157 : // subdirectory below. Reject rooted/traversal/separator-escape names
158 : // before they can become filesystem paths (or 7z arguments).
159 22486 : if (filename.empty() || !is_safe_relative_entry_path(filename))
160 : {
161 : // distinguish a rejected hostile name from a merely missing file:
162 : // ODB++-derived content failing the safety check is audit-worthy
163 3 : if (!filename.empty())
164 : {
165 6 : logwarn("getUncompressedFilePath refused unsafe member name: [" + filename + "]");
166 : }
167 3 : return uncompressedPath;
168 : }
169 :
170 22483 : path possibleUncompressedFilePath = directory / filename;
171 22483 : auto uncompressedFileExists = exists(possibleUncompressedFilePath) && is_regular_file(possibleUncompressedFilePath);
172 22483 : if (uncompressedFileExists)
173 : {
174 15811 : uncompressedPath = possibleUncompressedFilePath;
175 : }
176 : else
177 : {
178 6672 : path possibleCompressedFilePath = directory / filename;
179 6672 : possibleCompressedFilePath.replace_extension("Z");
180 :
181 6672 : auto compressedFileExists = false;
182 6672 : if (exists(possibleCompressedFilePath) && is_regular_file(possibleCompressedFilePath))
183 : {
184 0 : compressedFileExists = true;
185 : }
186 : else
187 : {
188 6672 : possibleCompressedFilePath.replace_extension("z");
189 6672 : if (exists(possibleCompressedFilePath) && is_regular_file(possibleCompressedFilePath))
190 : {
191 0 : compressedFileExists = true;
192 : }
193 : }
194 :
195 6672 : if (compressedFileExists)
196 : {
197 : // extract and set edaDataFilePath to file
198 0 : ArchiveExtractor extractor(possibleCompressedFilePath.string());
199 0 : if (extractor.Extract())
200 : {
201 0 : uncompressedPath = extractor.GetExtractionDirectory();
202 0 : uncompressedPath /= filename;
203 : }
204 0 : }
205 6672 : }
206 :
207 22483 : return uncompressedPath;
208 22483 : }
209 :
210 4 : /*static*/ bool ArchiveExtractor::CompressDir(const std::string& srcDir, const std::string& destDir,
211 : const std::string& archiveName, std::string& fileOut,
212 : CompressionType type)
213 : {
214 4 : return compress_dir(srcDir.c_str(), destDir.c_str(), archiveName.c_str(), fileOut, type);
215 : }
216 : }
|