OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
ArchiveExtractor.cpp
Go to the documentation of this file.
1#include "ArchiveExtractor.h"
2#include <filesystem>
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
12using namespace std::filesystem;
13
14namespace Utils
15{
16 ArchiveExtractor::ArchiveExtractor(const std::string& path, bool bExtractLzmaInProc /* = false*/, bool bExtractLzmaUsing7Zip /* = true*/)
17 : m_path(path)
18 , m_bExtractLzmaInProc(bExtractLzmaInProc)
19 , m_bExtractLzmaUsing7Zip(bExtractLzmaUsing7Zip)
20 {
21 }
22
24 {
25 return m_path;
26 }
27
29 {
30 return m_extractionDirectory;
31 }
32
33 bool ArchiveExtractor::IsArchiveTypeSupported(const path& file)
34 {
35 if (ALLOW_ALL_ARCHIVE_EXTENSION_TYPES) return true;
36
37 for (const auto& ext : SupportedExtensions)
38 {
39 auto extLower = str_to_lower_copy(file.extension().string());
40 if (extLower == "." + std::string(ext))
41 {
42 return true;
43 }
44 }
45
46 return false;
47 }
48
49 bool ArchiveExtractor::IsArchiveTypeSupported(const std::string& file)
50 {
51 return IsArchiveTypeSupported(path(file));
52 }
53
55 {
56 //auto extractionPath = path.replace_extension().string();
57 auto extractionPath = m_path.parent_path() / m_path.stem();
58 return Extract(extractionPath.string());
59 }
60
61 bool Utils::ArchiveExtractor::ExtractLzmaInProc(const std::filesystem::path& destinationPath)
62 {
63 return false;
64 }
65
66 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 "-o" + destinationPath.string(),
82 "-y", // yes to all prompts
83 "-aoa", // overwrite all
84 };
85
86 loginfo("running 7z (argv spawn, no shell): x [" + m_path.string() + "] -o[" + destinationPath.string() + "]");
87
88 auto exitCode = run_process("7z", args);
89
90 if (exitCode < 0)
91 {
92 auto message = "failed to start 7z process (is 7z installed and on PATH?)";
93 logerror(message);
94 throw std::runtime_error(message);
95 }
96
97 if (exitCode != static_cast<int>(e7zExitCode::Success) &&
98 exitCode != static_cast<int>(e7zExitCode::Warning))
99 {
100 auto message = "7z command failed (exit code = " + std::to_string(exitCode) + ")";
101 message += ", cwd: [" + current_path().string() + "]";
102 logerror(message);
103 throw std::runtime_error(message.c_str());
104 }
105
106 loginfo("7z command succeeded");
107
108 m_extractionDirectory = destinationPath.string();
109 return true;
110 }
111
112 bool ArchiveExtractor::Extract(const std::string& destinationPath)
113 {
114 if (m_bExtractLzmaUsing7Zip &&
115 std::find(LZMA_FILE_EXTENSIONS.begin(), LZMA_FILE_EXTENSIONS.end(), m_path.extension()) != LZMA_FILE_EXTENSIONS.end())
116 {
117 if (m_bExtractLzmaInProc)
118 {
119 return ExtractLzmaInProc(destinationPath);
120 }
121 else
122 {
123 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 std::error_code canonicalError;
135 auto canonicalDestination = weakly_canonical(path(destinationPath), canonicalError);
136 if (canonicalError)
137 {
138 canonicalDestination = path(destinationPath);
139 }
140
141 if (extract(m_path.string().c_str(), canonicalDestination.string().c_str()))
142 {
143 m_extractionDirectory = canonicalDestination.string();
144 return true;
145 }
146 }
147
148 return false;
149 }
150
151 /*static*/ path ArchiveExtractor::getUncompressedFilePath(const path& directory, const std::string& filename)
152 {
153 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 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 if (!filename.empty())
164 {
165 logwarn("getUncompressedFilePath refused unsafe member name: [" + filename + "]");
166 }
167 return uncompressedPath;
168 }
169
170 path possibleUncompressedFilePath = directory / filename;
171 auto uncompressedFileExists = exists(possibleUncompressedFilePath) && is_regular_file(possibleUncompressedFilePath);
172 if (uncompressedFileExists)
173 {
174 uncompressedPath = possibleUncompressedFilePath;
175 }
176 else
177 {
178 path possibleCompressedFilePath = directory / filename;
179 possibleCompressedFilePath.replace_extension("Z");
180
181 auto compressedFileExists = false;
182 if (exists(possibleCompressedFilePath) && is_regular_file(possibleCompressedFilePath))
183 {
184 compressedFileExists = true;
185 }
186 else
187 {
188 possibleCompressedFilePath.replace_extension("z");
189 if (exists(possibleCompressedFilePath) && is_regular_file(possibleCompressedFilePath))
190 {
191 compressedFileExists = true;
192 }
193 }
194
195 if (compressedFileExists)
196 {
197 // extract and set edaDataFilePath to file
198 ArchiveExtractor extractor(possibleCompressedFilePath.string());
199 if (extractor.Extract())
200 {
201 uncompressedPath = extractor.GetExtractionDirectory();
202 uncompressedPath /= filename;
203 }
204 }
205 }
206
207 return uncompressedPath;
208 }
209
210 /*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 return compress_dir(srcDir.c_str(), destDir.c_str(), archiveName.c_str(), fileOut, type);
215 }
216}
#define logerror(s)
Definition Logger.h:17
#define logwarn(s)
Definition Logger.h:20
#define loginfo(s)
Definition Logger.h:14
std::string GetExtractionDirectory() const
static bool CompressDir(const std::string &srcDir, const std::string &destDir, const std::string &archiveName, std::string &fileOut, CompressionType type=CompressionType::TarGzip)
static std::filesystem::path getUncompressedFilePath(const std::filesystem::path &directory, const std::string &filename)
static bool IsArchiveTypeSupported(const std::filesystem::path &file)
static constexpr bool ALLOW_ALL_ARCHIVE_EXTENSION_TYPES
static constexpr const char * SupportedExtensions[]
std::filesystem::path GetPath() const
ArchiveExtractor(const std::string &path, bool bExtractLzmaInProc=false, bool bExtractLzmaUsing7Zip=true)
std::int64_t run_process(const std::string &program, const std::vector< std::string > &args, bool discardOutput)
bool extract(const char *filename, const char *destDir)
std::string str_to_lower_copy(const std::string &s)
bool compress_dir(const char *srcDir, const char *destDir, const char *archiveName, std::string &fileOut, CompressionType type)
bool is_safe_relative_entry_path(const std::string &name)