Validate shell-safe arguments for search. */ Search/grep paths and globs are ALWAYS single-quoted (POSIX sh) or * double-/single-quoted (Windows cmd/PowerShell) on the command line, which * neutralises '&' — a very common character in real paths (R&D, "Foo & Bar", * OneDrive). Accept '&' here while still rejecting every metacharacter that * could break out of the quoting (#272). */
| 5041 | * OneDrive). Accept '&' here while still rejecting every metacharacter that |
| 5042 | * could break out of the quoting (#272). */ |
| 5043 | static bool validate_search_path_arg(const char *s) { |
| 5044 | if (!s) { |
| 5045 | return false; |
| 5046 | } |
| 5047 | for (const char *p = s; *p; p++) { |
| 5048 | switch (*p) { |
| 5049 | case '\'': |
| 5050 | case '"': |
| 5051 | case ';': |
| 5052 | case '|': |
| 5053 | case '$': |
| 5054 | case '`': |
| 5055 | case '<': |
| 5056 | case '>': |
| 5057 | case '\n': |
| 5058 | case '\r': |
| 5059 | #ifndef _WIN32 |
| 5060 | case '\\': |
| 5061 | #endif |
| 5062 | return false; |
| 5063 | default: |
| 5064 | break; |
| 5065 | } |
| 5066 | } |
| 5067 | return true; |
| 5068 | } |
| 5069 | |
| 5070 | static bool validate_search_args(const char *root_path, const char *file_pattern) { |
| 5071 | if (!validate_search_path_arg(root_path)) { |
no outgoing calls
no test coverage detected