Skip to content

Commit fcef8db

Browse files
authored
Merge pull request #3460 from Ghabry/ff-oob
String ToFile: Sanitize path
2 parents 2bcad49 + 1c0070b commit fcef8db

3 files changed

Lines changed: 19 additions & 19 deletions

File tree

src/filefinder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ std::string FileFinder::MakeCanonical(std::string_view path, int initial_deepnes
186186
// Ignore, we are in root
187187
--initial_deepness;
188188
} else {
189-
Output::Debug("Path traversal out of game directory: {}", path);
189+
Output::Warning("Path traversal out of game directory: {}", path);
190190
}
191191
} else if (path_comp.empty() || path_comp == ".") {
192192
// ignore

src/filefinder.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,6 @@ namespace FileFinder {
229229
*/
230230
Filesystem_Stream::InputStream OpenText(std::string_view name);
231231

232-
/**
233-
* Writes data to a txt file.
234-
* If the file exists, it will be overwritten.
235-
*
236-
* @param name the text file path and name
237-
* @param data the content of the text file to be written
238-
*/
239-
void WriteText(std::string_view name, std::string_view data);
240-
241232
/**
242233
* Appends name to directory.
243234
*

src/game_strings.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <lcf/encoder.h>
2121
#include <lcf/reader_util.h>
2222
#include "async_handler.h"
23+
#include "filefinder.h"
24+
#include "filesystem_stream.h"
2325
#include "game_map.h"
2426
#include "game_message.h"
2527
#include "game_strings.h"
@@ -237,20 +239,27 @@ bool Game_Strings::ToFile(Str_Params params, std::string filename, int encoding)
237239
filename += ".txt";
238240
}
239241

240-
auto txt_out = FileFinder::Save().OpenOutputStream(filename);
241-
auto txt_dir = FileFinder::GetPathAndFilename(filename).first;
242+
filename = FileFinder::MakeCanonical(filename, 1);
242243

243-
if (!txt_out) {
244-
if (!FileFinder::Save().MakeDirectory(txt_dir, false)) {
244+
auto txt_file = FileFinder::Save().FindFile(filename);
245+
Filesystem_Stream::OutputStream txt_out;
246+
247+
if (txt_file.empty()) {
248+
// File not found: Create directory hierarchy to ensure file creation succeeds
249+
auto txt_dir = FileFinder::GetPathAndFilename(filename).first;
250+
251+
if (!txt_dir.empty() && !FileFinder::Save().MakeDirectory(txt_dir, false)) {
245252
Output::Warning("Maniac String Op ToFile failed. Cannot create directory {}", txt_dir);
246253
return false;
247254
}
248255

249-
txt_out = FileFinder::Save().OpenOutputStream(filename);
250-
if (!txt_out) {
251-
Output::Warning("Maniac String Op ToFile failed. Cannot write to {}", filename);
252-
return false;
253-
}
256+
txt_file = filename;
257+
}
258+
259+
txt_out = FileFinder::Save().OpenOutputStream(txt_file);
260+
if (!txt_out) {
261+
Output::Warning("Maniac String Op ToFile failed. Cannot write to {}", filename);
262+
return false;
254263
}
255264

256265
if (encoding == 0) {

0 commit comments

Comments
 (0)