Check illegal characters from the file and parent folder name

PiperOrigin-RevId: 634941778
This commit is contained in:
Eiden Kim
2024-05-17 18:46:26 -07:00
committed by Copybara-Service
parent 4ff7c1511c
commit b224f8660c
5 changed files with 72 additions and 144 deletions
@@ -37,19 +37,16 @@
namespace nearby {
namespace windows {
const wchar_t* kUpOneLevel = L"../";
constexpr wchar_t kEndDot = L'.';
const wchar_t* kUpOneLevel = L"/..";
constexpr wchar_t kPathDelimiter = L'/';
constexpr wchar_t kReplacementChar = L'_';
constexpr wchar_t kForwardSlash = L'/';
constexpr wchar_t kBackSlash = L'\\';
const wchar_t kIllegalFileCharacters[] = {L'*', L'?', L'\"', L'<', L'>', L'|'};
const wchar_t* const kForbiddenPathNames[] = {
L"CON", L"PRN", L"AUX", L"NUL", L"COM1", L"COM2", L"COM3",
L"COM4", L"COM5", L"COM6", L"COM7", L"COM8", L"COM9", L"COM¹",
L"COM²", L"COM³", L"LPT1", L"LPT2", L"LPT3", L"LPT4", L"LPT5",
L"LPT6", L"LPT7", L"LPT8", L"LPT9", L"LPT¹", L"LPT²", L"LPT³"};
wchar_t const* kForbiddenPathNames[] = {
L"CON", L"PRN", L"AUX", L"NUL", L"COM1", L"COM2", L"COM3", L"COM4",
L"COM5", L"COM6", L"COM7", L"COM8", L"COM9", L"LPT1", L"LPT2", L"LPT3",
L"LPT4", L"LPT5", L"LPT6", L"LPT7", L"LPT8", L"LPT9"};
std::wstring FilePath::GetCustomSavePath(std::wstring parent_folder,
std::wstring file_name) {
@@ -131,6 +128,7 @@ std::wstring FilePath::CreateOutputFileWithRename(std::wstring path) {
std::replace(sanitized_path.begin(), sanitized_path.end(), kBackSlash,
kForwardSlash);
// Remove any /..'s
SanitizePath(sanitized_path);
auto last_delimiter = sanitized_path.find_last_of(kPathDelimiter);
@@ -236,21 +234,26 @@ void FilePath::SanitizePath(std::wstring& path) {
// If found then erase it from string
path.erase(pos, wcslen(kUpOneLevel));
}
while (path[path.size() - 1] == kEndDot) {
// If found then erase it from string
path.erase(path.size() - 1, 1);
}
path = MutateForbiddenPathElements(path);
ReplaceInvalidCharacters(path);
}
char kIllegalFileCharacters[] = {'?', '*', '\'', '<', '>', '|', ':'};
void FilePath::ReplaceInvalidCharacters(std::wstring& path) {
auto it = path.begin();
it += 2; // Skip the 'C:' or any other drive specifier
for (; it != path.end(); it++) {
// If 0 < character < 32, it's illegal, replace it
if (*it > 0 && *it < 32) {
NEARBY_LOGS(INFO) << "In path " << wstring_to_string(path)
<< " replaced \'" << std::string(1, *it) << "\' with \'"
<< std::string(1, kReplacementChar);
*it = kReplacementChar;
}
for (auto illegal_character : kIllegalFileCharacters) {
if (*it == illegal_character) {
NEARBY_LOGS(INFO) << "In path " << wstring_to_string(path)
@@ -259,19 +262,6 @@ void FilePath::ReplaceInvalidCharacters(std::wstring& path) {
*it = kReplacementChar;
}
}
if (*it > 0 &&
*it < 32) { // If 0 < character < 32, it's illegal, replace it
NEARBY_LOGS(INFO) << "In path " << wstring_to_string(path)
<< " replaced \'" << std::string(1, *it) << "\' with \'"
<< std::string(1, kReplacementChar);
*it = kReplacementChar;
}
if (*it == 0) { // character is null
NEARBY_LOGS(INFO) << "In path " << wstring_to_string(path)
<< " replaced \'NULL\' with \'"
<< std::string(1, kReplacementChar);
*it = kReplacementChar;
}
}
}
@@ -31,16 +31,14 @@ namespace windows {
namespace {
const wchar_t* kIllegalPathNames[] = {
L"CON", L"PRN", L"AUX", L"NUL", L"COM1", L"COM2", L"COM3",
L"COM4", L"COM5", L"COM6", L"COM7", L"COM8", L"COM9", L"COM¹",
L"COM²", L"COM³", L"LPT1", L"LPT2", L"LPT3", L"LPT4", L"LPT5",
L"LPT6", L"LPT7", L"LPT8", L"LPT9", L"LPT¹", L"LPT²", L"LPT³"};
L"CON", L"PRN", L"AUX", L"NUL", L"COM1", L"COM2", L"COM3", L"COM4",
L"COM5", L"COM6", L"COM7", L"COM8", L"COM9", L"LPT1", L"LPT2", L"LPT3",
L"LPT4", L"LPT5", L"LPT6", L"LPT7", L"LPT8", L"LPT9"};
const wchar_t* kFileName(L"increment_file_test.txt");
const wchar_t* kFirstIterationFileName(L"/increment_file_test (1).txt");
const wchar_t* kSecondIterationFileName(L"/increment_file_test (2).txt");
const wchar_t* kThirdIterationFileName(L"/increment_file_test (3).txt");
const wchar_t* kFileNameWithNullReplaced(L"/increment_file_test.txt_.txt");
const wchar_t* kNoDotsFileName(L"incrementfiletesttxt");
const wchar_t* kOneIterationNoDotsFileName(L"/incrementfiletesttxt (1)");
const wchar_t* kMultipleDotsFileName(L"increment.file.test.txt");
@@ -593,7 +591,7 @@ TEST_F(FilePathTests, GetDownloadPath_IllegalFileNameCharacterColon\
ReturnsFileNameWithUnderbarSubstituted) {
// char illegal_character_sequence[]{ 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2f,
// 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0 };
auto illegal_character_sequence(L"Test\"Test");
auto illegal_character_sequence(L"Test:Test");
std::wstring parent_folder(L"");
@@ -656,45 +654,6 @@ FileWithIncrementedName) {
ASSERT_FALSE(input_file.rdstate() == std::ifstream::goodbit);
}
TEST_F(FilePathTests, GetDownloadPath_FileExistsReturns\
FileWithIncrementedNameWithNull) {
std::wstring file_name(kFileName);
int size = file_name.size();
file_name.append(L"1.txt");
file_name[size] = L'\x00';
std::wstring renamed_file_name(kFileNameWithNullReplaced);
std::wstring parent_folder(L"");
std::wstring output_file_path(default_download_path_);
output_file_path.append(L"/");
output_file_path.append(file_name);
std::wstring expected(default_download_path_);
expected += renamed_file_name;
std::wifstream input_file;
std::wofstream output_file;
output_file.open(output_file_path,
std::ofstream::binary | std::ofstream::out);
ASSERT_TRUE(output_file.rdstate() == std::ofstream::goodbit);
output_file.close();
auto actual(FilePath::GetDownloadPath(parent_folder, file_name));
EXPECT_EQ(actual, expected);
// Remove the file and check that it is removed
// File 1
_wremove(output_file_path.c_str());
input_file.open(output_file_path, std::ifstream::binary | std::ifstream::in);
ASSERT_FALSE(input_file.rdstate() == std::ifstream::goodbit);
}
TEST_F(FilePathTests, GetDownloadPath_MultipleFilesExist\
ReturnsNextIncrementedFileName) {
std::ofstream output_file;