-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Update documentation related to the recent cmd.exe fix #123709
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,8 +90,8 @@ | |
//! | ||
//! # Windows argument splitting | ||
//! | ||
//! On Unix systems arguments are passed to a new process as an array of strings | ||
//! but on Windows arguments are passed as a single commandline string and it's | ||
//! On Unix systems arguments are passed to a new process as an array of strings, | ||
//! but on Windows arguments are passed as a single commandline string and it is | ||
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. Commander Data, is that you? 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, the grammar lords always said to avoid short contractions in technical writing |
||
//! up to the child process to parse it into an array. Therefore the parent and | ||
//! child processes must agree on how the commandline string is encoded. | ||
//! | ||
|
@@ -107,26 +107,26 @@ | |
//! * Use [`raw_arg`] to build a custom commandline. This bypasses the escaping | ||
//! rules used by [`arg`] so should be used with due caution. | ||
//! | ||
//! `cmd.exe` and `.bat` use non-standard argument parsing and are especially | ||
//! `cmd.exe` and `.bat` files use non-standard argument parsing and are especially | ||
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. This makes it sound like " |
||
//! vulnerable to malicious input as they may be used to run arbitrary shell | ||
//! commands. Untrusted arguments should be restricted as much as possible. | ||
//! For examples on handling this see [`raw_arg`]. | ||
//! | ||
//! ### Bat file special handling | ||
//! ### Batch file special handling | ||
//! | ||
//! On Windows, `Command` uses the Windows API function [`CreateProcessW`] to | ||
//! spawn new processes. An undocumented feature of this function is that, | ||
//! spawn new processes. An undocumented feature of this function is that | ||
//! when given a `.bat` file as the application to run, it will automatically | ||
//! convert that into running `cmd.exe /c` with the bat file as the next argument. | ||
//! convert that into running `cmd.exe /c` with the batch file as the next argument. | ||
//! | ||
//! For historical reasons Rust currently preserves this behaviour when using | ||
//! [`Command::new`], and escapes the arguments according to `cmd.exe` rules. | ||
//! Due to the complexity of `cmd.exe` argument handling, it might not be | ||
//! possible to safely escape some special chars, and using them will result | ||
//! possible to safely escape some special characters, and using them will result | ||
//! in an error being returned at process spawn. The set of unescapeable | ||
//! special chars might change between releases. | ||
//! special characters might change between releases. | ||
//! | ||
//! Also note that running `.bat` scripts in this way may be removed in the | ||
//! Also note that running batch scripts in this way may be removed in the | ||
//! future and so should not be relied upon. | ||
//! | ||
//! [`spawn`]: Command::spawn | ||
|
@@ -655,16 +655,19 @@ impl Command { | |
/// | ||
/// Note that the argument is not passed through a shell, but given | ||
/// literally to the program. This means that shell syntax like quotes, | ||
/// escaped characters, word splitting, glob patterns, variable substitution, etc. | ||
/// have no effect. | ||
/// escaped characters, word splitting, glob patterns, variable substitution, | ||
/// etc. have no effect. | ||
/// | ||
/// <div class="warning"> | ||
/// | ||
/// On Windows use caution with untrusted inputs. Most applications use the | ||
/// standard convention for decoding arguments passed to them. These are safe to use with `arg`. | ||
/// However some applications, such as `cmd.exe` and `.bat` files, use a non-standard way of decoding arguments | ||
/// and are therefore vulnerable to malicious input. | ||
/// In the case of `cmd.exe` this is especially important because a malicious argument can potentially run arbitrary shell commands. | ||
/// On Windows, use caution with untrusted inputs. Most applications use the | ||
/// standard convention for decoding arguments passed to them. These are safe to | ||
/// use with `arg`. However, some applications such as `cmd.exe` and `.bat` files | ||
/// use a non-standard way of decoding arguments. They are therefore vulnerable | ||
/// to malicious input. | ||
/// | ||
/// In the case of `cmd.exe` this is especially important because a malicious | ||
/// argument can potentially run arbitrary shell commands. | ||
/// | ||
/// See [Windows argument splitting][windows-args] for more details | ||
/// or [`raw_arg`] for manually implementing non-standard argument encoding. | ||
Comment on lines
+663
to
673
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. Neither this nor the linked page really answer "I have untrusted input, how do I know if I can use it with 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. Yeah, and that's a tricky one to answer. I guess the short answer is you just have to know and trust the application/script that you're using uses MSVC compatible argument parsing rules. Which, I mean, is annoying. On the other hand sending untrusted arguments to an unknown application sounds wrong to me in any case? 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. I can put something like that into words, just explaining how Rust does quoting would help. We now use the cmd quoting if the executable for 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. We don't for an explicit call to Note that we also don't prevent Linux users from using |
||
|
@@ -706,11 +709,14 @@ impl Command { | |
/// | ||
/// <div class="warning"> | ||
/// | ||
/// On Windows use caution with untrusted inputs. Most applications use the | ||
/// standard convention for decoding arguments passed to them. These are safe to use with `args`. | ||
/// However some applications, such as `cmd.exe` and `.bat` files, use a non-standard way of decoding arguments | ||
/// and are therefore vulnerable to malicious input. | ||
/// In the case of `cmd.exe` this is especially important because a malicious argument can potentially run arbitrary shell commands. | ||
/// On Windows, use caution with untrusted inputs. Most applications use the | ||
/// standard convention for decoding arguments passed to them. These are safe to | ||
/// use with `arg`. However, some applications such as `cmd.exe` and `.bat` files | ||
/// use a non-standard way of decoding arguments. They are therefore vulnerable | ||
/// to malicious input. | ||
/// | ||
/// In the case of `cmd.exe` this is especially important because a malicious | ||
/// argument can potentially run arbitrary shell commands. | ||
/// | ||
/// See [Windows argument splitting][windows-args] for more details | ||
/// or [`raw_arg`] for manually implementing non-standard argument encoding. | ||
|
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.
There are many kinds of batch files used by many different job control languages. I believe only the
.bat
files of Windows are under discussion here. This retitling seems to be less correct.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.
I think the proper name is "DOS Batch file" but figured the DOS part is implied when discussing Windows. I have indeed seen "BAT File" though, just not 🦇
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.
BAT file seems like the one we might prefer here, yes.
However, I must wonder... do all of these caveats apply to files with the
.cmd
extension also?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.
The documentation so far doesn't seem to mention
.cmd
at all, despite that also being an applicable extension for Windows batch files with basically-the-same command language. I won't pretend to know what the exact caveats and corner cases are, though.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.
Hey, according to wiki .cmd is just another "Batch File" 😄 https://en.wikipedia.org/wiki/Batch_file
(I'll take another read through and make sure both are covered appropriately)
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.
Indeed!
I had a frisson of annoyance at that, as technically, there are many JCLs etc. etc. and then I realized that while indeed even in 2024 some people do use mainframes with such batch files that are not in the MS-DOS command language... everyone understands the meaning here from context, obviously.