开发者

Identifying unquoted special characters in shell command text

开发者 https://www.devze.com 2023-02-25 06:31 出处:网络
I am implementing the wordexp function, which needs to identify and optionally reject occurrences of command substitution (i.e. $(...) or backticks), and which, from a quality standpoint, should also

I am implementing the wordexp function, which needs to identify and optionally reject occurrences of command substitution (i.e. $(...) or backticks), and which, from a quality standpoint, should also identify and reject unquoted occurrences of the special characters |, &, ;, <, and > whenever command substitution is disabled.

What I'm looking for is a simple way to identify the presence of these special cha开发者_开发百科racters without duplicating a huge amount of shell logic. Any ideas?

Once the string is validated, I'm passing it to sh with the following -c argument:

printf '%s\0' [string inserted here]

which builds a nice multi-string ready for consumption by the C code.


This approach seems to be correct, but I'd welcome corrections if it's wrong:

If the WRDE_NOCMD flag is not specified, don't do any checking. The specification for wordexp says the application

shall ensure that words does not contain an unquoted character or any of the unquoted shell special characters ...

and there is certainly no security benefit gained from checking for bogus special characters outside of command-substitution contexts when command-substitution is already allowed.

Now, if WRDE_NOCMD is specified, process the string sequentially, keeping a flag for whether we're in a single-quoted or double-quoted context.

  • Encountering a backslash in a non-single-quoted context skips the next character.
  • Encountering a single quote in a non-double-quoted context toggles the single quote context.
  • Encountering a double quote in a non-single-quoted context toggles the double quote context.
  • Encountering a backtick, or a $( not immediately followed by another (, in a non-single-quoted context results in WRDE_CMDSUB.
  • Encountering any of the special characters in a non-quoted context results in WRDE_BADCHAR.

Since we're going to disallow all command substitution, there are no nested $( contexts to worry about; entering the first level already generates an error.

Have I made any stupid (or subtle and not-so-stupid) mistakes here?

0

精彩评论

暂无评论...
验证码 换一张
取 消