本页内容

Utils::ProcessArgs Class

class Utils::ProcessArgs

处理用 shell 引号引起来的过程参数。更多...

Header: #include <ProcessArgs>

静态公共成员

bool expandMacros(QString *cmd, const Utils::ProcessArgs::FindMacro &findMacro, Utils::OsType osType)

详细说明

成员函数文档

[static] bool ProcessArgs::expandMacros(QString *cmd, const Utils::ProcessArgs::FindMacro &findMacro, Utils::OsType osType)

使用函数 `findMacro ` 搜索宏,并对字符串 `cmd` 进行就地宏展开(替换),该字符串预计包含一个 shell 命令。`osType ` 指定了语法,其中 Unix 版本兼容 Bourne Shell,Windows 版本兼容cmd 。

如果无法安全地执行替换(例如因引号错误导致命令无法解析),则返回false 。

注意:该函数 的设计旨在确保在处理包含 shell 元字符的 expando 对象时能够安全使用。然而,若将 expando 放置在命令中的错误位置,可能会导致扩展器无法对内容进行正确引号处理,从而很可能导致命令执行错误。 特别是,包含不可信数据的“expando”可能会使应用程序的最终用户面临严重的shell代码注入漏洞。为避免这些问题,请遵循Unix security considerations 和Windows security considerations 中的指南。通常,调用shell脚本比组装复杂的一行命令更为妥当。

Unix 注意事项

显式支持的 shell 结构:\ '' "" {} () $(()) ${} $() ``

隐式支持的 shell 结构:(())

不支持且会导致问题的 shell 结构:

  • 简化的case $v in pat) 语法。请改用case $v in (pat) 。
  • Bash 风格的$"" 和$'' 字符串引号语法。

其余的 shell 语法(包括 bash 语法)通常不会引发问题,因此会被忽略。

Unix 安全注意事项
  • 对可展开项使用反斜杠转义将被视为引号错误。
  • 请勿将可展开项放入双引号替换中,因为这可能会在某些 shell 中触发解析器错误:
    "${VAR:-%{macro}}"
  • 请勿将可展开项放入作为嵌套 shell 命令的命令行参数中。例如,以下写法是不安全的:
    su %{user} -c 'foo %{file}'

    相反,您可以将宏赋值给一个环境变量,然后将其传递给调用:

    file=%{file} su %{user} -c 'foo "$file"'

Windows 注意事项

splitArgs() 支持的所有引号语法在此处同样适用。此外,通过圆括号进行的命令分组也被识别——但请注意,解析器对未加引号的圆括号的处理比cmd 本身要严格得多。cmd 语法的其余部分不应造成问题,且会被忽略。

Windows 安全注意事项
  • 对可展开项使用倒钩转义将被视为引号错误。
  • 在展开项正前面的闭合双引号以及在展开项正后面的开启双引号会被视为引号错误。
  • 请勿将展开项放入嵌套命令中。例如,以下写法是不安全的:
    for /f "usebackq" \%v in (`foo \%{file}`) do \@echo \%v
  • 宏的值中不得包含任何可能被解释为环境变量展开的内容。解决方案是将所有百分号替换为固定字符串(如\%PERCENT_SIGN\% ),并将PERCENT_SIGN=\% 注入到 shell 的环境中。
  • 启用延迟环境变量展开(cmd /v:on )不应产生安全影响。但这仍可能导致问题,因为解析器尚未做好准备:命令中的字面感叹号需要用circumflex 转义,而现有的 circumflex 符号则需要加倍。

Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.