-
Notifications
You must be signed in to change notification settings - Fork 15
Properly escape commandline string. #712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a more general question: why do you put the platform dependent func in this file instead of the command_utils_.cpp? (for example, why not put MakeWindowsCommand() in src/dive/os/command_utils_win32.cpp)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. e.g. Python expose both Windows and Linux path function, and you can run the following on Linux environment import pathlib
print(str(pathlib.PureWindowsPath("C:/Program Files")))
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems that "MakeWindowsCommand" is only used by command_utils_win32.cpp? do you see possible usage for other platforms as well? I understand that MakeWindowsCommand is platform-independent logic (just string manipulation), If we moved MakeWindowsCommand to command_utils_win32.cpp, It would clearly signal that this logic is intended for the Windows platform (or targeting Windows).
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| /* | ||
| Copyright 2023 Google Inc. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2026
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol, this is supposed to be the same file as |
||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #include "dive/os/command_utils.h" | ||
|
|
||
| #include <cctype> | ||
| #include <set> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| #include "absl/base/no_destructor.h" | ||
|
|
||
| namespace Dive | ||
| { | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| constexpr int kMaxAscii = 127; | ||
| bool IsAscii(int c) { return 0 <= c && c <= kMaxAscii; } | ||
| bool IsBashKeyword(std::string_view str) | ||
| { | ||
| static absl::NoDestructor<std::set<std::string_view>> lut(std::set<std::string_view>( | ||
| {"if", "then", "elif", "else", "fi", "time", "for", "in", "until", "while", "do", "done", | ||
| "case", "esac", "coproc", "select", "function"})); | ||
| return lut->contains(str); | ||
| } | ||
|
|
||
| bool IsBashSafeCharacter(char c) | ||
| { | ||
| static const auto lut = []() { | ||
| constexpr std::string_view kShellSafeCharacters = | ||
| "+,-./0123456789:@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_abcdefghijklmnopqrstuvwxyz"; | ||
| std::array<bool, kMaxAscii + 1> result = {}; | ||
| for (char c : kShellSafeCharacters) | ||
| { | ||
| result[c] = true; | ||
| } | ||
| return result; | ||
| }(); | ||
| return IsAscii(c) && lut[c]; | ||
| } | ||
|
|
||
| std::string BashEscapeImpl(std::string_view original) | ||
| { | ||
| constexpr std::string_view kSingleQuoteReplacement = R"('"'"')"; | ||
| std::string escaped; | ||
| escaped.reserve(original.size() + 2); | ||
| escaped.push_back('\''); | ||
| for (char c : original) | ||
| { | ||
| if (c == '\'') | ||
| { | ||
| escaped.append(kSingleQuoteReplacement); | ||
| } | ||
| else | ||
| { | ||
| escaped.push_back(c); | ||
| } | ||
| } | ||
| escaped.push_back('\''); | ||
| return escaped; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| std::string BashEscape(std::string_view original, bool force_escape) | ||
| { | ||
| if (original.empty()) | ||
| { | ||
| return "\"\""; | ||
| } | ||
|
|
||
| if (!force_escape) | ||
| { | ||
| bool require_escape = IsBashKeyword(original); | ||
| for (char c : original) | ||
| { | ||
| if (!IsBashSafeCharacter(c)) | ||
| { | ||
| require_escape = true; | ||
| } | ||
| } | ||
| if (!require_escape) | ||
| { | ||
| return std::string(original); | ||
| } | ||
| } | ||
|
|
||
| return BashEscapeImpl(original); | ||
| } | ||
|
|
||
| // See | ||
| // https://learn.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-170#parsing-c-command-line-arguments | ||
| std::string WindowsCrtProgramEscape(std::string_view original) | ||
| { | ||
| bool require_escape = false; | ||
| for (char c : original) | ||
| { | ||
| switch (c) | ||
| { | ||
| case '<': | ||
| case '>': | ||
| case ':': | ||
| case '"': | ||
| case '|': | ||
| case '?': | ||
| case '*': | ||
| // Invalid character in program name. | ||
| return "\"\""; | ||
| case ' ': | ||
| case '\t': | ||
| // Documentation only says space and tab are seperator, so ignore newline and etc. | ||
| require_escape = true; | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
| if (!require_escape) | ||
| { | ||
| return std::string(original); | ||
| } | ||
| return "\"" + std::string(original) + "\""; | ||
| } | ||
|
|
||
| std::string WindowsCrtArgumentEscape(std::string_view original) | ||
| { | ||
| if (original.empty()) | ||
| { | ||
| return "\"\""; | ||
| } | ||
|
|
||
| std::string escaped; | ||
| escaped.reserve(original.size() + 2); | ||
| escaped.push_back('"'); | ||
| bool require_escape = false; | ||
| for (char c : original) | ||
| { | ||
| switch (c) | ||
| { | ||
| case ' ': | ||
| case '\f': | ||
| case '\n': | ||
| case '\r': | ||
| case '\t': | ||
| case '\v': | ||
| require_escape = true; | ||
| escaped.push_back(c); | ||
| break; | ||
| case '\\': | ||
| case '"': | ||
| require_escape = true; | ||
| escaped.push_back('\\'); | ||
| escaped.push_back(c); | ||
| break; | ||
| default: | ||
| if (!IsAscii(c)) | ||
| { | ||
| require_escape = true; | ||
| } | ||
| escaped.push_back(c); | ||
| break; | ||
| } | ||
| } | ||
| escaped.push_back('"'); | ||
| if (!require_escape) | ||
| { | ||
| return std::string(original); | ||
| } | ||
| return escaped; | ||
| } | ||
|
|
||
| std::string MakeUnixCommand(std::string_view program, const std::vector<std::string>& args) | ||
| { | ||
| std::string command = BashEscape(program); | ||
| for (const auto& arg : args) | ||
| { | ||
| command.push_back(' '); | ||
| command.append(BashEscape(arg)); | ||
| } | ||
| return command; | ||
| } | ||
|
|
||
| std::string MakeWindowsCommand(std::string_view program, const std::vector<std::string>& args) | ||
| { | ||
| std::string command = WindowsCrtProgramEscape(program); | ||
| for (const auto& arg : args) | ||
| { | ||
| command.push_back(' '); | ||
| command.append(WindowsCrtArgumentEscape(arg)); | ||
| } | ||
| return command; | ||
| } | ||
|
|
||
| } // namespace Dive | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would this fail if there is a space in on_device_capture_directory?
i see you have 2 overload Shell(), and this will trigger the
which doesn't call "MakeUnixCommand?" on the command string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it will fail.
It should be
.Shell("rm", {"-rf", on_device_capture_directory}), however most of these are find-replace regex edit.There's still a problem if
on_device_capture_directorystarts with-and rm think it is a parameter rather than a flag. However, I don't aim to solve every problem.I'd like to have
adb.Run(Adb::Remove{.recursive = true, .force = true, .path= on_device_capture_directory}), but that's a lot of code to write.