Is there a special case in Apache2 when calling a CGI and the URI includes a single query string parameter without a value? - apache

Today I got an error and was very surprised to get it since everything looked just fine...
I have a CGI written in C++ which accepts URIs with a query string. The query string is what selects the page, etc. The CGI is installed in the standard location for an Ubuntu installation:
/usr/lib/cgi-bin/snapmanager.cgi
Today I was finishing up adding a Login screen and once logged in, I wanted to add a logout link. The link simply adds ?logout at the end of the URI:
http://www.example.com/cgi-bin/snapmanager.cgi?logout
That failed.
Checking the error log, I got an error saying that "logout" actually appeared on the command line. Rather surprising, if you ask me! I tried with:
http://www.example.com/cgi-bin/snapmanager.cgi?logout=now
and everything worked as expected. No logout on the command line.
I also tried:
http://www.example.com/cgi-bin/snapmanager.cgi?logout&host=foo
And that worked too. Again, no logout on the command line.
However, if I switch the parameters position it fails again:
http://www.example.com/cgi-bin/snapmanager.cgi?host=foo&logout
So it looks like Apache2 calls my CGI with the logout query string as a parameter on the command line when that one query string name is defined last.
Just in case, I tried to add dashes at the start of the name, and sure enough, that appears as a command line switch in my logs!
error:snapmanager.cgi: option --logout is not supported.
Really scary. This is a huge security risk if you know of a switch that can "tweak things your way"...
Is that documented somewhere?

I actually found the answer in RFC3875 in paragraph 4.4
4.4. The Script Command Line
Some systems support a method for supplying an array of strings to the CGI script. This is only used in the case of an 'indexed' HTTP query, which is identified by a 'GET' or 'HEAD' request with a URI query string that does not contain any unencoded "=" characters. For such a request, the server SHOULD treat the query-string as a search-string and parse it into words, using the rules
search-string = search-word *( "+" search-word )
search-word = 1*schar
schar = unreserved | escaped | xreserved
xreserved = ";" | "/" | "?" | ":" | "#" | "&" | "=" | "," |
"$"
After parsing, each search-word is URL-decoded, optionally encoded in a system-defined manner and then added to the command line argument list.
If the server cannot create any part of the argument list, then the server MUST NOT generate any command line information. For example, the number of arguments may be greater than operating system or server limits, or one of the words may not be representable as an argument.
The script SHOULD check to see if the QUERY_STRING value contains an unencoded "=" character, and SHOULD NOT use the command line arguments if it does.
Emphasis Mine

Related

curl: no matches found

trying to hit a public endpoint that works in the browser(gives json response), But does not in curl.
curl https://yts.lt/api/v2/list_movies.json?query_term=tt11296058
It gives the following message
no matches found: https://yts.lt/api/v2/list_movies.json?query_term=tt11296058
Not sure what could be the issue here, any input would be appreciated.
It may be due to do with what shell you are using. Try wrapping the URL in single or double quotes as suggested here.
Here is the quote from the above link discussing ZSH.
You need to escape the question mark, otherwise zsh thinks it is a globbing or wildcard character and tries to find files that match it (that's why it says no matches found).

Informix 11.5 SQL Select Carriage Return and Line Feed

Informix 11.5
I am trying to search for carriage returns and line feeds that may exist in a VARCHAR field. First, I need a SELECT statement to show that they exist. Second, I need to REPLACE them with a space or other character.
I've tried all kinds of variations:
CHR(10) + CHR(13)
CHR(10) || CHR(13)
CHAR(13) + CHAR(10)
CHAR(13) || CHAR(10)
SELECT CHR(10) from systables;
Everything gives an error: Routine (chr) can not be resolved.
I've been searching all over and just can't find anything that works, and I'm sure this is crazy stupid easy.
Get the ASCII package from the IIUG
The CHR() function was added to IDS 11.70; it isn't in IDS 11.50.
The good news is you can add the function because IDS is an extensible server. The better news for you is that you can obtain the relevant code from the IIUG web site in the Software Archive under the Miscellaneous section as ascii.
That should allow you to do what you need. (Note: I wrote the code way back when — before there was support built into any of the servers.)
Windows makes things more complicated
I was uploading the ascii.unl file and I get an error that the number of columns do not match on line 13. Have you seen this before? I'm on Windows 2008. The errors are:
846: Number of values in load file is not equal to number of columns.
847: Error in load file line 13.
I hadn't seen it before, but I've not tried the file on Windows and … well, let's say life gets trickier on Windows than it is on Unix (and this bit isn't all that simple on Unix).
First of all, the data file needs to have CRLF line endings instead of the NL-only line endings that are standard on Unix. (Note that NL, newline, is another name for LF, line feed — aka '\n'.) For most lines in the unload file, that isn't a problem.
The two entries for which it might be (is) a problem are for CR and LF — entries 13 and 10 respectively. In theory, if the entry for line 10 contains (in C string notation) "10|\\\n\r\n" (that is, 10, pipe, backslash, newline, CRLF), all should be OK; the absence of an error message for line 10 suggests that it is OK.
Similarly, the entry for line 13 is "13|\r\r\n", which apparently causes grief. The simplest trial fix is to add a backslash here too: "13|\\\r\r\nn". The backslash says "the next character doesn't have a special meaning". If that doesn't work, we'll probably have to try hex-escape notation: "13|\\0d\r\n" — and use dbaccess -X to enable the hex escape notation.
With luck, one of those two (or both) will work. If neither works, come back and we'll try to think of something else.
As per my above comment:
I was uploading the ascii.unl file and I get an error that the number of columns do not match on line 13. Have you seen this before? I'm on Windows 2008. 846: Number of values in load file is not equal to number of columns. 847: Error in load file line 13.
Here is what I see in the ascii.unl file.
If I put this into MS Word and turn on Show Formatting/Paragraph marks, it shows this:

SSIS pkg does not recognize the file path

I'm using the 2012 version of Visual Studio to build an SSIS package. I have a variable var_root which has the string value - 'C:\Projects\OBC\Clients\ABC'. When I try to run the pkg, I get the following error:
Error: The expression contains unrecognized token "C". If "C" is a variable, it should be expressed as "#C". The specified token is not valid. If the token is intended to be a variable name, it should be prefixed with the # symbol.
Error: Attempt to parse the expression "C:\Projects\OBC\Clients\ABC" failed and returned error code 0xC00470A4. The expression cannot be parsed. It might contain invalid elements or it might not be well-formed. There may also be an out-of-memory error.
Now, this runs fine in the 2008 version of the Business Intelligence studio. I don't know how to specify the variable name. Please help me if possible. Thanks
The SSIS expression language is a C based language and the \ is a token, this means you have to escape it with another one. i.e "\" becomes "\", unlike C# you can't prefix the string with a #, you have to use the escaping route.
In summary when ever you want to use \ you need to use two \
Why use the expression though when you can set the value directly in the values column for the variable - without the quotes or double slashes - Just - C:\Projects\OBC\Clients\ABC

why does using "\" shows error in jython

I am trying to use a copy command for Windows and we have directories such as c:\oracle.
While trying to execute one such, we get the following error:
source_file=folder+"\"
^
SyntaxError: Lexical error at line 17, column 23. Encountered: "\r" (13), after : ""
Here folder is my path of c:\oracle and while trying to add file to it like:
source=folder+"\"+src_file
I am not able to do so. Any suggestion on how to solve this issue?
I tried with / but my copy windows calling source in os.command is getting "the syntax is incorrect" and the only way to solve it is to use \ but I am getting the above error in doing so.
Please suggest. Thanks for your help
Thanks.
Short answer:
You need:
source_file = folder + "\\" + src_file
Long answer:
The problem with
source_file = folder + "\" + src_file
is that \ is the escape character. What it's doing in this particular case is escaping the " so that it's treated as a character of the string rather than the string terminator, similar to:
source_file = folder + "X + src_file
which would have the same problem.
In other words, you're trying to construct a string consisting of ", some other text and the end of line (\r, the carriage return character). That's where your error is coming from:
Encountered: "\r" (13)
Paxdiablo is absolutely correct about why \ isn't working for you. However, you could also solve your problem by using os.path.normpath instead of trying to construct the proper platform-specific path characters yourself.
In all programming languages I know of, you can't put a quote inside a string like this: "this is a quote: "." The reason for this is that the first quote opens the string, the second then closes it (!), and then the third one opens another string - with the following two problems:
whatever is between the quotes #2 and #3 is probably not valid code;
the quote #3 is probably not being closed.
There are two common mechanisms of solving this: doubling and escaping. Escaping is far more common, and what it means is you put a special character (usually \) in front of characters that you don't want to be interpreted in their usual value. Thus, "no, *this* is a quote: \"." is a proper string, where the quote #2 is not closing the string - and the character \ does not appear.
However, now you have another problem - how do you actually make the escape character appear in a string? Simple: escape it! "This is an escape: \\!" is how you do it: the backslash #1 is the escape character, and the backslash #2 is the escapee: it will not be interpreted with its usual escape semantics, but as a simple backslash character.
Thus, your line should say this:
source=folder+"\\"+src_file
BTW: upvote for both #paxdiablo (who got in before my diatribe) and #Nick (who has a proper Pythonic way to do what you want to do)

Escaping Double Quotes in Batch Script

How would I go about replacing all of the double quotes in my batch file's parameters with escaped double quotes? This is my current batch file, which expands all of its command line parameters inside the string:
#echo off
call bash --verbose -c "g++-linux-4.1 %*"
It then uses that string to make a call to Cygwin's bash, executing a Linux cross-compiler. Unfortunately, I'm getting parameters like these passed in to my batch file:
"launch-linux-g++.bat" -ftemplate-depth-128 -O3 -finline-functions
-Wno-inline -Wall -DNDEBUG -c
-o "C:\Users\Me\Documents\Testing\SparseLib\bin\Win32\LinuxRelease\hello.o"
"c:\Users\Me\Documents\Testing\SparseLib\SparseLib\hello.cpp"
Where the first quote around the first path passed in is prematurely ending the string being passed to GCC, and passing the rest of the parameters directly to bash (which fails spectacularly.)
I imagine if I can concatenate the parameters into a single string then escape the quotes it should work fine, but I'm having difficulty determining how to do this. Does anyone know?
The escape character in batch scripts is ^. But for double-quoted strings, double up the quotes:
"string with an embedded "" character"
eplawless's own answer simply and effectively solves his specific problem: it replaces all " instances in the entire argument list with \", which is how Bash requires double-quotes inside a double-quoted string to be represented.
To generally answer the question of how to escape double-quotes inside a double-quoted string using cmd.exe, the Windows command-line interpreter (whether on the command line - often still mistakenly called the "DOS prompt" - or in a batch file):See bottom for a look at PowerShell.
tl;dr:
The answer depends on which program you're calling:
You must use "" when passing an argument to a(nother) batch file and you may use "" with applications created with Microsoft's C/C++/.NET compilers (which also accept \"), which on Windows includes Python, Node.js, and PowerShell (Core) 7+'s CLI (pwsh) but not Windows PowerShell's (powershell.exe):
Example: foo.bat "We had 3"" of rain."
The following applies to targeting batch files only:
"" is the only way to get the command interpreter (cmd.exe) to treat the whole double-quoted string as a single argument (though that won't matter if you simply pass all arguments through to another program, with %*)
Sadly, however, not only are the enclosing double-quotes retained (as usual), but so are the doubled escaped ones, so obtaining the intended string is a two-step process; e.g., assuming that the double-quoted string is passed as the 1st argument, %1:
set "str=%~1" removes the enclosing double-quotes; set "str=%str:""="%" then converts the doubled double-quotes to single ones.
Be sure to use the enclosing double-quotes around the assignment parts to prevent unwanted interpretation of the values.
\" is required - as the only option - by many other programs, (e.g., Ruby, Perl, PHP, as well as programs that use the CommandLineToArgv Windows API function to parse their command-line arguments), but it use from cmd.exe is not robust and safe:
\" is what many executables and interpreters either require - including Windows PowerShell - when passed strings from the outside, on the command line - or, in the case of Microsoft's compilers, support as an alternative to "" - ultimately, though, it's up to the target program to parse the argument list.
Example: foo.exe "We had 3\" of rain."
However, use of \" can break calls and at least hypothetically result in unwanted, arbitrary execution of commands and/or input/output redirections:
The following characters present this risk: & | < >
For instance, the following results in unintended execution of the ver command; see further below for an explanation and the next bullet point for a workaround:
foo.exe "3\" of snow" "& ver."
For calling the Windows PowerShell CLI, powershell.exe, \"" and "^"" are robust, but limited alternatives (see section "Calling PowerShell's CLI ..." below).
If you must use \" from cmd.exe, there are only 3 safe approaches from cmd.exe, which are, however quite cumbersome: Tip of the hat to T S for his help.
Using (possibly selective) delayed variable expansion in your batch file, you can store literal \" in a variable and reference that variable inside a "..." string using !var! syntax - see T S's helpful answer.
The above approach, despite being cumbersome, has the advantage that you can apply it methodically and that it works robustly, with any input.
Only with LITERAL strings - ones NOT involving VARIABLES - do you get a similarly methodical approach: categorically ^-escape all cmd.exe metacharacters: " & | < > and - if you also want to suppress variable expansion - %:
foo.exe ^"3\^" of snow^" ^"^& ver.^"
Otherwise, you must formulate your string based on recognizing which portions of the string cmd.exe considers unquoted due to misinterpreting \" as closing delimiters:
in literal portions containing shell metacharacters: ^-escape them; using the example above, it is & that must be ^-escaped:
foo.exe "3\" of snow" "^& ver."
in portions with %...%-style variable references: ensure that cmd.exe considers them part of a "..." string and that that the variable values do not themselves have embedded, unbalanced quotes - which is not even always possible.
Background
Note: This is based on my own experiments. Do let me know if I'm wrong.
POSIX-like shells such as Bash on Unix-like systems tokenize the argument list (string) before passing arguments individually to the target program: among other expansions, they split the argument list into individual words (word splitting) and remove quoting characters from the resulting words (quote removal). The target program is handed an array of individual, verbatim arguments, i.e. with syntactic quotes removed.
By contrast, the Windows command interpreter apparently does not tokenize the argument list and simply passes the single string comprising all arguments - including quoting chars. - to the target program.
However, some preprocessing takes place before the single string is passed to the target program: ^ escape chars. outside of double-quoted strings are removed (they escape the following char.), and variable references (e.g., %USERNAME%) are interpolated first.
Thus, unlike in Unix, it is the target program's responsibility to parse to parse the arguments string and break it down into individual arguments with quotes removed.
Thus, different programs can require differing escaping methods and there's no single escaping mechanism that is guaranteed to work with all programs - https://stackoverflow.com/a/4094897/45375 contains excellent background on the anarchy that is Windows command-line parsing.
In practice, \" is very common, but NOT SAFE from cmd.exe, as mentioned above:
Since cmd.exe itself doesn't recognize \" as an escaped double-quote, it can misconstrue later tokens on the command line as unquoted and potentially interpret them as commands and/or input/output redirections.
In a nutshell: the problem surfaces, if any of the following characters follow an opening or unbalanced \": & | < >; for example:
foo.exe "3\" of snow" "& ver."
cmd.exe sees the following tokens, resulting from misinterpreting \" as a regular double-quote:
"3\"
of
snow" "
rest: & ver.
Since cmd.exe thinks that & ver. is unquoted, it interprets it as & (the command-sequencing operator), followed by the name of a command to execute (ver. - the . is ignored; ver reports cmd.exe's version information).
The overall effect is:
First, foo.exe is invoked with the first 3 tokens only.
Then, command ver is executed.
Even in cases where the accidental command does no harm, your overall command won't work as designed, given that not all arguments are passed to it.
Many compilers / interpreters recognize ONLY \" - e.g., the GNU C/C++ compiler, Perl, Ruby, PHP, as well as programs that use the CommandLineToArgv Windows API function to parse their command-line arguments - and for them there is no simple solution to this problem.
Essentially, you'd have to know in advance which portions of your command line are misinterpreted as unquoted, and selectively ^-escape all instances of & | < > in those portions.
By contrast, use of "" is SAFE, but is regrettably only supported by Microsoft-compiler-based executables and batch files (in the case of batch files, with the quirks discussed above), which notable excludes PowerShell - see next section.
Calling PowerShell's CLI from cmd.exe or POSIX-like shells:
Note: See the bottom section for how quoting is handled inside PowerShell.
When invoked from the outside - e.g., from cmd.exe, whether from the command line or a batch file:
PowerShell [Core] v6+ now properly recognizes "" (in addition to \"), which is both safe to use and whitespace-preserving.
pwsh -c " ""a & c"".length " doesn't break and correctly yields 6
Windows PowerShell (the legacy edition whose latest and final version is 5.1) recognizes only \" or """, the latter being the most robust choice from cmd.exe, in the form "^""" (even though internally PowerShell uses ` as the escape character in double-quoted strings and also accepts "" - see bottom section), as discussed next:
Calling Windows PowerShell from cmd.exe / a batch file:
"" breaks, because it is fundamentally unsupported:
powershell -c " ""ab c"".length " -> error "The string is missing the terminator"
\" and """ work in principle, but aren't safe:
powershell -c " \"ab c\".length " works as intended: it outputs 5 (note the 2 spaces)
But it isn't safe, because cmd.exe metacharacters break the command, unless escaped:
powershell -c " \"a& c\".length " breaks, due to the &, which would have to be escaped as ^&
\"" is safe, but normalizes interior whitespace, which can be undesired:
powershell -c " \""a& c\"".length " outputs 4(!), because the 2 spaces are normalized to 1.
"^"" is the best choice for Windows PowerShell, specifically Credit goes to Venryx for discovering this approach. and "" for PowerShell (Core) 7+:
Windows PowerShell: powershell -c " "^""a& c"^"".length " works: doesn't break - despite & - and outputs 5, i.e., correctly preserved whitespace.
PowerShell Core: pwsh -c """a& c"".length "
See this answer for more information.
On Unix-like platforms (Linux, macOS), when calling PowerShell [Core]'s CLI, pwsh, from a POSIX-like shell such as bash:
You must use \", which, however is both safe and whitespace-preserving:
$ pwsh -c " \"a& c\".length " # OK: 5
# Alternative, with '...' quoting: no escaping of " needed.
$ pwsh -c ' "a& c".length ' # OK: 5
Related information
^ can only be used as the escape character in unquoted strings - inside double-quoted strings, ^ is not special and treated as a literal.
CAVEAT: Use of ^ in parameters passed to the call statement is broken (this applies to both uses of call: invoking another batch file or binary, and calling a subroutine in the same batch file):
^ instances in double-quoted values are inexplicably doubled, altering the value being passed: e.g., if variable %v% contains literal value a^b, call :foo "%v%" assigns "a^^b"(!) to %1 (the first parameter) in subroutine :foo.
Unquoted use of ^ with call is broken altogether in that ^ can no longer be used to escape special characters: e.g., call foo.cmd a^&b quietly breaks (instead of passing literal a&b too foo.cmd, as would be the case without call) - foo.cmd is never even invoked(!), at least on Windows 7.
Escaping a literal % is a special case, unfortunately, which requires distinct syntax depending on whether a string is specified on the command line vs. inside a batch file; see https://stackoverflow.com/a/31420292/45375
The short of it: Inside a batch file, use %%. On the command line, % cannot be escaped, but if you place a ^ at the start, end, or inside a variable name in an unquoted string (e.g., echo %^foo%), you can prevent variable expansion (interpolation); % instances on the command line that are not part of a variable reference are treated as literals (e.g, 100%).
Generally, to safely work with variable values that may contain spaces and special characters:
Assignment: Enclose both the variable name and the value in a single pair of double-quotes; e.g., set "v=a & b" assigns literal value a & b to variable %v% (by contrast, set v="a & b" would make the double-quotes part of the value). Escape literal % instances as %% (works only in batch files - see above).
Reference: Double-quote variable references to make sure their value is not interpolated; e.g., echo "%v%" does not subject the value of %v% to interpolation and prints "a & b" (but note that the double-quotes are invariably printed too). By contrast, echo %v% passes literal a to echo, interprets & as the command-sequencing operator, and therefore tries to execute a command named b.
Also note the above caveat re use of ^ with the call statement.
External programs typically take care of removing enclosing double-quotes around parameters, but, as noted, in batch files you have to do it yourself (e.g., %~1 to remove enclosing double-quotes from the 1st parameter) and, sadly, there is no direct way that I know of to get echo to print a variable value faithfully without the enclosing double-quotes.
Neil offers a for-based workaround that works as long as the value has no embedded double quotes; e.g.:
set "var=^&')|;,%!" for /f "delims=" %%v in ("%var%") do echo %%~v
cmd.exe does not recognize single-quotes as string delimiters ('...') - they are treated as literals and cannot generally be used to delimit strings with embedded whitespace; also, it follows that the tokens abutting the single-quotes and any tokens in between are treated as unquoted by cmd.exe and interpreted accordingly.
However, given that target programs ultimately perform their own argument parsing, some programs such as Ruby do recognize single-quoted strings even on Windows; by contrast, C/C++ executables and Perl do not recognize them.
Even if supported by the target program, however, it is not advisable to use single-quoted strings, given that their contents are not protected from potentially unwanted interpretation by cmd.exe.
Quoting from within PowerShell:
Windows PowerShell is a much more advanced shell than cmd.exe, and it has been a part of Windows for many years now (and PowerShell Core brought the PowerShell experience to macOS and Linux as well).
PowerShell works consistently internally with respect to quoting:
inside double-quoted strings, use `" or "" to escape double-quotes
inside single-quoted strings, use '' to escape single-quotes
This works on the PowerShell command line and when passing parameters to PowerShell scripts or functions from within PowerShell.
(As discussed above, passing an escaped double-quote to PowerShell from the outside requires \" or, more robustly, \"" - nothing else works).
Sadly, when invoking external programs from PowerShell, you're faced with the need to both accommodate PowerShell's own quoting rules and to escape for the target program:
This problematic behavior is also discussed and summarized in this answer; the experimental PSNativeCommandArgumentPassing feature introduced in PowerShell Core 7.2.0-preview.5 - assuming it becomes an official feature - will fix this at least for those external programs that accept \".
Double-quotes inside double-quoted strings:
Consider string "3`" of rain", which PowerShell-internally translates to literal 3" of rain.
If you want to pass this string to an external program, you have to apply the target program's escaping in addition to PowerShell's; say you want to pass the string to a C program, which expects embedded double-quotes to be escaped as \":
foo.exe "3\`" of rain"
Note how both `" - to make PowerShell happy - and the \ - to make the target program happy - must be present.
The same logic applies to invoking a batch file, where "" must be used:
foo.bat "3`"`" of rain"
By contrast, embedding single-quotes in a double-quoted string requires no escaping at all.
Single-quotes inside single-quoted strings do not require extra escaping; consider '2'' of snow', which is PowerShell' representation of 2' of snow.
foo.exe '2'' of snow'
foo.bat '2'' of snow'
PowerShell translates single-quoted strings to double-quoted ones before passing them to the target program.
However, double-quotes inside single-quoted strings, which do not need escaping for PowerShell, do still need to be escaped for the target program:
foo.exe '3\" of rain'
foo.bat '3"" of rain'
PowerShell v3 introduced the magic --% option, called the stop-parsing symbol, which alleviates some of the pain, by passing anything after it uninterpreted to the target program, save for cmd.exe-style environment-variable references (e.g., %USERNAME%), which are expanded; e.g.:
foo.exe --% "3\" of rain" -u %USERNAME%
Note how escaping the embedded " as \" for the target program only (and not also for PowerShell as \`") is sufficient.
However, this approach:
does not allow for escaping % characters in order to avoid environment-variable expansions.
precludes direct use of PowerShell variables and expressions; instead, the command line must be built in a string variable in a first step, and then invoked with Invoke-Expression in a second.
An alternative workaround* that addresses this problem is to call via cmd /c with a single argument containing the entire command line:
cmd /c "foo.exe `"3\`" of rain`" -u $env:USERNAME"
Thus, despite its many advancements, PowerShell has not made escaping easier when calling external programs - on the contrary. It has, however, introduced support for single-quoted strings.
If you don't mind installing a third-party module (authored by me), the Native module (Install-Module Native) offers backward- and forward-compatible helper function ie, which obviates the need for the extra escaping and contains important accommodations for high-profile CLIs on Windows:
# Simply prepend 'ie' to your external-program calls.
ie foo.exe '3" of rain' -u $env:USERNAME
Google eventually came up with the answer. The syntax for string replacement in batch is this:
set v_myvar=replace me
set v_myvar=%v_myvar:ace=icate%
Which produces "replicate me". My script now looks like this:
#echo off
set v_params=%*
set v_params=%v_params:"=\"%
call bash -c "g++-linux-4.1 %v_params%"
Which replaces all instances of " with \", properly escaped for bash.
As an addition to mklement0's excellent answer:
Almost all executables accept \" as an escaped ". Safe usage in cmd however is almost only possible using DELAYEDEXPANSION.
To explicitely send a literal " to some process, assign \" to an environment variable, and then use that variable, whenever you need to pass a quote. Example:
SETLOCAL ENABLEDELAYEDEXPANSION
set q=\"
child "malicious argument!q!&whoami"
Note SETLOCAL ENABLEDELAYEDEXPANSION seems to work only within batch files. To get DELAYEDEXPANSION in an interactive session, start cmd /V:ON.
If your batchfile does't work with DELAYEDEXPANSION, you can enable it temporarily:
::region without DELAYEDEXPANSION
SETLOCAL ENABLEDELAYEDEXPANSION
::region with DELAYEDEXPANSION
set q=\"
echoarg.exe "ab !q! & echo danger"
ENDLOCAL
::region without DELAYEDEXPANSION
If you want to pass dynamic content from a variable that contains quotes that are escaped as "" you can replace "" with \" on expansion:
SETLOCAL ENABLEDELAYEDEXPANSION
foo.exe "danger & bar=region with !dynamic_content:""=\"! & danger"
ENDLOCAL
This replacement is not safe with %...% style expansion!
In case of OP bash -c "g++-linux-4.1 !v_params:"=\"!" is the safe version.
If for some reason even temporarily enabling DELAYEDEXPANSION is not an option, read on:
Using \" from within cmd is a little bit safer if one always needs to escape special characters, instead of just sometimes. (It's less likely to forget a caret, if it's consistent...)
To achieve this, one precedes any quote with a caret (^"), quotes that should reach the child process as literals must additionally be escaped with a backlash (\^"). ALL shell meta characters must be escaped with ^ as well, e.g. & => ^&; | => ^|; > => ^>; etc.
Example:
child ^"malicious argument\^"^&whoami^"
Source: Everyone quotes command line arguments the wrong way, see "A better method of quoting"
To pass dynamic content, one needs to ensure the following:
The part of the command that contains the variable must be considered "quoted" by cmd.exe (This is impossible if the variable can contain quotes - don't write %var:""=\"%). To achieve this, the last " before the variable and the first " after the variable are not ^-escaped. cmd-metacharacters between those two " must not be escaped. Example:
foo.exe ^"danger ^& bar=\"region with %dynamic_content% & danger\"^"
This isn't safe, if %dynamic_content% can contain unmatched quotes.
If the string is already within quotes then use another quote to nullify its action.
echo "Insert tablename(col1) Values('""val1""')"
At Windows 10 21H1.
If from a batch (.bat) file I want to run the Everything application, I use """ inside double quotes argument:
"C:\Program Files\Everything\Everything.exe" -search "<"""D:\My spaced folder""" | """Z:\My_non_spaced_folder"""> <*.jpg | *.jpeg | *.avi | *.mp4>"
Hope it helps.