OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
FileUploadController.cpp
Go to the documentation of this file.
2#include <string>
3#include <vector>
4#include <filesystem>
5#include "fastmove.h"
6#include "PathSafety.h"
7#include "UrlEncoding.h"
8#include <system_error>
9#include <fstream>
10#include <App/RouteController.h>
11#include <crow/json.h>
12#include "CrossPlatform.h"
13
14using namespace std::filesystem;
15using namespace Odb::Lib::App;
16using namespace Utils;
17
18namespace
19{
20 // Fire-and-forget auto-warm: after an uploaded archive lands in the designs
21 // directory, kick a background parse (same path as RequestLoadDesign) so the
22 // first client request is warm. Never fails the upload response on a warm
23 // error; warm failures are logged only and stay retryable.
24 void autoWarmDesign(Odb::Lib::App::DesignCache& designs, const std::string& filename)
25 {
26 // Design names are archive file stems (matching DesignCache's scan)
27 const auto designName = path(filename).stem().string();
28 try
29 {
30 // An overwrite of an already-cached design must drop the stale
31 // cache entry first: LoadDesignAsync happily reports a fresh
32 // background load, but its single-flight cache-hit path would
33 // return the object parsed from the OLD archive without ever
34 // re-reading disk — a silent no-op that then pins the stale body
35 // behind the overwritten file's fresh ETag.
37 {
38 designs.InvalidateDesign(designName);
39 CROW_LOG_INFO << "Invalidated cached design \"" << designName << "\" for re-parse of the replaced archive";
40 }
41
42 if (designs.LoadDesignAsync(designName))
43 {
44 CROW_LOG_INFO << "Auto-warm load kicked for uploaded design \"" << designName << "\"";
45 }
46 else
47 {
48 CROW_LOG_DEBUG << "Auto-warm for \"" << designName << "\" skipped: a load is already queued or in progress";
49 }
50 }
51 catch (const std::exception& e)
52 {
53 CROW_LOG_ERROR << "Auto-warm load failed to start for \"" << designName << "\": " << e.what();
54 }
55 }
56}
57
58namespace Odb::App::Server
59{
64
66 {
67 CROW_ROUTE(m_serverApp.crow_app(), "/files/upload/<string>")
68 .methods(crow::HTTPMethod::POST)
69 ([&](const crow::request& req, std::string filename)
70 {
71 // authenticate request before sending to handler
72 auto authResp = m_serverApp.request_auth().AuthenticateRequest(req);
73 if (authResp.code != crow::status::OK)
74 {
75 return authResp;
76 }
77
78 const auto& contentType = req.get_header_value(CONTENT_TYPE_HEADER_NAME);
79 if (contentType != CONTENT_TYPE_APPLICATION_OCTET_STREAM)
80 {
81 return crow::response(crow::status::BAD_REQUEST, std::string("unsupported content type: this endpoint only accepts '") + CONTENT_TYPE_APPLICATION_OCTET_STREAM + "'");
82 }
83
84 return handleOctetStreamUpload(filename, req);
85 });
86
87 CROW_ROUTE(m_serverApp.crow_app(), "/files/upload")
88 .methods(crow::HTTPMethod::POST)
89 ([&](const crow::request& req)
90 {
91 // authenticate request before sending to handler
92 auto authResp = m_serverApp.request_auth().AuthenticateRequest(req);
93 if (authResp.code != crow::status::OK)
94 {
95 return authResp;
96 }
97
98 const auto& contentType = req.get_header_value(CONTENT_TYPE_HEADER_NAME);
99 if (contentType.find(CONTENT_TYPE_MULTIPART_FORM_DATA) != 0)
100 {
101 // "Content-Type" header doesn't start with "multipart/form-data"
102 return crow::response(crow::status::BAD_REQUEST, std::string("unsupported content type: this endpoint only accepts '") + CONTENT_TYPE_MULTIPART_FORM_DATA + "'");
103 }
104
105 return handleMultipartFormUpload(req);
106 });
107
108 CROW_ROUTE(m_serverApp.crow_app(), "/files/list")
109 .methods(crow::HTTPMethod::GET)
110 ([&](const crow::request& req)
111 {
112 // authenticate request before sending to handler
113 auto authResp = m_serverApp.request_auth().AuthenticateRequest(req);
114 if (authResp.code != crow::status::OK)
115 {
116 return authResp;
117 }
118
119 return makeLoadedFileModelsResponse(false);
120 });
121
122// CROW_ROUTE(m_serverApp.crow_app(), "/designs/list/<string>").methods(crow::HTTPMethod::GET)
123// ([&](const crow::request& req, std::string query)
124// {
125// if (! m_serverApp.designs().isQueryValid(query))
126// {
127// return crow::response(crow::status::BAD_REQUEST, "invalid query");
128// }
129//
130// auto designNames = m_serverApp.designs().getUnloadedNames(query);
131// if (designNames.empty())
132// {
133// return crow::response(crow::status::NOT_FOUND, "no matching design names found");
134// }
135//
136// crow::json::wvalue jsonResponse;
137// jsonResponse["names"] = designNames;
138//
139//#if defined(_DEBUG)
140// auto j = jsonResponse.dump();
141//#endif
142//
143// return crow::response(jsonResponse);
144// });
145 }
146
147 crow::response FileUploadController::handleOctetStreamUpload(const std::string& filename, const crow::request& req)
148 {
149 // Security-audit F002: route-parameter filenames are fully
150 // client-controlled (including Windows backslash traversal via the
151 // URL path) - URL-decode first (crow route params arrive encoded;
152 // every other controller decodes manually), then reject anything
153 // that is not one safe component.
154 auto safeName = sanitizeFilename(UrlEncoding::decode(filename));
155 if (!safeName.has_value())
156 {
157 return crow::response(crow::status::BAD_REQUEST, "invalid filename");
158 }
159
160 std::string tempName;
161 if (!CrossPlatform::tmpnam_safe(tempName))
162 {
163 return crow::response(crow::status::INTERNAL_SERVER_ERROR, "failed to generate temp file name");
164 }
165 const auto tempPath = temp_directory_path() / tempName;
166 std::ofstream outfile(tempPath, std::ofstream::binary);
167 if (!outfile)
168 {
169 return crow::response(crow::status::INTERNAL_SERVER_ERROR, "failed handling new file");
170 }
171 outfile << req.body;
172 outfile.close();
173 // check write AND close: an ENOSPC mid-write must not commit a
174 // truncated archive (same fail-closed posture as the multipart path)
175 if (!outfile)
176 {
177 std::error_code removeEc;
178 remove(tempPath, removeEc);
179 return crow::response(crow::status::INTERNAL_SERVER_ERROR, "failed handling new file");
180 }
181
182 path finalPath(m_serverApp.args().designsDir());
183 finalPath /= safeName.value();
184
185 std::error_code ec;
186 fastmove_file(tempPath, finalPath, false, ec);
187 if (ec == std::errc::file_exists)
188 {
189 std::error_code removeEc;
190 remove(tempPath, removeEc);
191 return crow::response(crow::status::CONFLICT, "a design with that filename already exists");
192 }
193 if (ec.value() != 0)
194 {
195 // no temp leak on a persistently failing move (unwritable designs
196 // dir, cross-device fallback failure)
197 std::error_code removeEc;
198 remove(tempPath, removeEc);
199 return crow::response(crow::status::INTERNAL_SERVER_ERROR, "failed handling new file");
200 }
201
202 autoWarmDesign(m_serverApp.designs(), safeName.value());
203
204 return makeLoadedFileModelsResponse(true);
205 }
206
207 crow::response FileUploadController::handleMultipartFormUpload(const crow::request& req)
208 {
209 crow::multipart::message file_message(req);
210
211 // Pass 1 — validate EVERY part before committing any of them to disk.
212 // The old single loop fastmove'd + cache-warmed each part as it went
213 // and then returned 400/409 on a later bad part, leaving earlier
214 // parts permanently stored behind a response implying total failure
215 // (a retry then 409'd on the already-stored part). Filenames and
216 // target conflicts are checked up front; the commit pass below keeps
217 // fastmove's no-overwrite as the atomic race guard and rolls back on
218 // a mid-commit failure.
219 struct ValidatedPart
220 {
221 const crow::multipart::part* pPart;
222 std::string safeName;
223 path finalPath;
224 };
225 std::vector<ValidatedPart> validatedParts;
226
227 for (const auto& part : file_message.part_map)
228 {
229 const auto& part_name = part.first;
230 const auto& part_value = part.second;
231
232 CROW_LOG_DEBUG << "Part: " << part_name;
233 if (MULTIPART_FORM_DATA_PART_NAME != part_name)
234 {
235 // log to debug and skip rest of the loop
236 CROW_LOG_DEBUG << " Value: " << part_value.body << '\n';
237 CROW_LOG_ERROR
238 << "multipart/form-data POST failed! Part name was: [" << part_name
239 << "], which is not supported. Part name should be [" << MULTIPART_FORM_DATA_PART_NAME
240 << "].";
241 continue;
242 }
243
244 // Extract the file name
245 auto headers_it = part_value.headers.find(CONTENT_DISPOSITION_HEADER_NAME);
246 if (headers_it == part_value.headers.end())
247 {
248 CROW_LOG_ERROR << "No " << CONTENT_DISPOSITION_HEADER_NAME << " found";
249 return crow::response(crow::status::BAD_REQUEST);
250 }
251 auto params_it = headers_it->second.params.find(MULTIPART_FORM_DATA_PART_FILENAME);
252 if (params_it == headers_it->second.params.end())
253 {
254 CROW_LOG_ERROR << "Part with name \"" << "MULTIPART_FORMDATA_PART_NAME" << "\" should have a \"" << "MULTIPART_FORMDATA_PART_FILENAME" << "\" parameter";
255 return crow::response(crow::status::BAD_REQUEST);
256 }
257 const std::string outfile_name = params_it->second;
258
259 for (const auto& part_header : part_value.headers)
260 {
261 const auto& part_header_name = part_header.first;
262 const auto& part_header_val = part_header.second;
263 CROW_LOG_DEBUG << "Header: " << part_header_name << '=' << part_header_val.value;
264 for (const auto& param : part_header_val.params)
265 {
266 const auto& param_key = param.first;
267 const auto& param_val = param.second;
268 CROW_LOG_DEBUG << " Param: " << param_key << ',' << param_val;
269 }
270 }
271
272 // Security-audit F002: multipart Content-Disposition filenames are
273 // fully client-controlled (traversal, absolute paths) - reject
274 // anything that is not one safe component.
275 auto safeName = sanitizeFilename(outfile_name);
276 if (!safeName.has_value())
277 {
278 return crow::response(crow::status::BAD_REQUEST, "invalid filename");
279 }
280
281 path finalPath(m_serverApp.args().designsDir());
282 finalPath /= safeName.value();
283
284 std::error_code existEc;
285 if (exists(finalPath, existEc))
286 {
287 return crow::response(crow::status::CONFLICT, "a design with that filename already exists");
288 }
289
290 validatedParts.push_back({ &part_value, safeName.value(), std::move(finalPath) });
291 }
292
293 // Pass 2 — commit the validated parts. On any mid-commit failure,
294 // roll back the parts this request already moved (and their warmed
295 // cache entries) so a failed upload leaves no partial state.
296 std::vector<std::string> committedNames;
297 const auto rollbackCommitted = [&]()
298 {
299 for (const auto& name : committedNames)
300 {
301 path committedPath(m_serverApp.args().designsDir());
302 committedPath /= name;
303 std::error_code removeEc;
304 remove(committedPath, removeEc);
305 if (removeEc)
306 {
307 // leftover partial state must be diagnosable: the client
308 // was told the upload failed, but this file survived
309 CROW_LOG_ERROR << "rollback could not remove committed upload ["
310 << committedPath.string() << "]: " << removeEc.message();
311 }
312 // cache keys are design names = archive file stems (see autoWarmDesign)
313 m_serverApp.designs().InvalidateDesign(path(name).stem().string());
314 }
315 };
316
317 for (const auto& validated : validatedParts)
318 {
319 // Create a new file with the extracted file name and write file
320 // contents to it. Any failure here fails the WHOLE request (roll
321 // back already-committed parts + 5xx): silently skipping a part
322 // would return success for an upload that lost data, and a retry
323 // would then 409 on the parts that did commit. Write and close
324 // are both checked so an ENOSPC mid-write cannot commit a
325 // truncated file (same posture as the extraction write path).
326 std::string tempName;
327 if (!CrossPlatform::tmpnam_safe(tempName))
328 {
329 CROW_LOG_ERROR << " Failed to generate temp file name\n";
330 rollbackCommitted();
331 return crow::response(crow::status::INTERNAL_SERVER_ERROR, "failed handling new file");
332 }
333 const auto tempPath = temp_directory_path() / tempName;
334 std::ofstream out_file(tempPath, std::ofstream::binary);
335 if (!out_file)
336 {
337 CROW_LOG_ERROR << " Write to file failed\n";
338 rollbackCommitted();
339 return crow::response(crow::status::INTERNAL_SERVER_ERROR, "failed handling new file");
340 }
341 out_file << validated.pPart->body;
342 out_file.close();
343 if (!out_file)
344 {
345 CROW_LOG_ERROR << " Write to file failed (write/close)\n";
346 std::error_code removeEc;
347 remove(tempPath, removeEc);
348 rollbackCommitted();
349 return crow::response(crow::status::INTERNAL_SERVER_ERROR, "failed handling new file");
350 }
351
352 std::error_code ec;
353 fastmove_file(tempPath, validated.finalPath, false, ec);
354 if (ec == std::errc::file_exists)
355 {
356 std::error_code removeEc;
357 remove(tempPath, removeEc);
358 rollbackCommitted();
359 return crow::response(crow::status::CONFLICT, "a design with that filename already exists");
360 }
361 if (ec.value() != 0)
362 {
363 std::error_code removeEc;
364 remove(tempPath, removeEc);
365 rollbackCommitted();
366 return crow::response(crow::status::INTERNAL_SERVER_ERROR, "failed handling new file");
367 }
368
369 autoWarmDesign(m_serverApp.designs(), validated.safeName);
370 committedNames.push_back(validated.safeName);
371
372 CROW_LOG_INFO << " Contents written to " << validated.safeName << '\n';
373 }
374
375 return makeLoadedFileModelsResponse(true);
376 }
377
378 std::optional<std::string> FileUploadController::sanitizeFilename(const std::string& filename) const
379 {
380 // Security-audit F002: rejection, not laundering. The name must be a
381 // single safe component (no separators, traversal, rooted/drive forms,
382 // control characters, trailing dots/spaces, sane length). The shared
383 // validator is also used by the archive extraction paths (F003/F001),
384 // so upload and extraction enforce one vocabulary.
386 {
387 // the name is URL-decoded by now and may carry raw control
388 // bytes; escape them so one rejection cannot forge additional
389 // log lines (audit F017 class)
390 std::string printable;
391 printable.reserve(filename.size());
392 for (const auto c : filename)
393 {
394 const auto uc = static_cast<unsigned char>(c);
395 if (uc < 0x20 || uc == 0x7F)
396 {
397 printable += "\\x";
398 const char hex[] = "0123456789ABCDEF";
399 printable += hex[uc >> 4];
400 printable += hex[uc & 0x0F];
401 }
402 else
403 {
404 printable += c;
405 }
406 }
407 CROW_LOG_ERROR << "Rejected unsafe upload filename: [" << printable << "]";
408 return std::nullopt;
409 }
410 return filename;
411 }
412}
FileUploadController(Odb::Lib::App::IOdbServerApp &serverApp)
void InvalidateDesign(const std::string &designName)
bool LoadDesignAsync(const std::string &designName)
LoadState GetLoadState(const std::string &designName) const
virtual const OdbDesignArgs & args() const =0
virtual DesignCache & designs()=0
virtual RequestAuthenticationBase & request_auth()=0
virtual CrowApp & crow_app()=0
std::string designsDir() const
virtual crow::response AuthenticateRequest(const crow::request &req)
crow::response makeLoadedFileModelsResponse(bool) const
static bool tmpnam_safe(std::string &tempNameOut)
static std::string decode(const std::string &encoded)
void fastmove_file(const path &source, const path &dest, bool overwriteExisting, std::error_code &ec)
Definition fastmove.cpp:24
bool is_safe_filename_component(const std::string &name)