安全地提供单行bash安装脚本

时间:2014-07-08 23:46:39

标签: bash shell curl wget

我喜欢为事物提供单行bash安装脚本,看起来像这样:

curl https://gist.githubusercontent.com/.../raw/install.sh | bash

甚至

curl http://tinyurl.com/installmything | bash

但对于那些不信任我的人来说,这显然是一个巨大的安全风险。

是否有任何方便,简单的方法:

  1. 让用户在运行之前查看脚本,
  2. 仍然只需要一次复制粘贴操作
  3. 是不是太麻烦了?
  4. 例如,我可以像这样提供单行:

    wget https://gist.githubusercontent.com/.../installthing.sh ; cat installthing.sh ; read -p "Type YES to continue: " X && [ $X = "YES" ] && bash installthing.sh
    

    这不符合繁琐的要求。有更好的方法吗?

1 个答案:

答案 0 :(得分:5)

有两种类型的人不信任你:

  1. 谁知道bash:这些人无论如何都会下载你的脚本并查看其内容。他们了解curl ... | bash做了什么,他们不会执行该命令。

  2. 谁不知道bash:即使你向这些人展示源代码,他们也无法做到这一点。他们无法决定该代码是否存在安全风险。

  3. 所以我认为不需要让用户在执行前查看脚本的内容。但是如果你仍然需要它,你可以运行脚本,但在实际代码执行之前询问用户。类似的东西:

    #!/bin/bash
    
    read -p "Do you wish to see the content of this script before execution?" -n 1 -r
    echo
    
    if [[ $REPLY =~ ^[Yy]$ ]]
    then
      tail -n +20 "$0"
    
      echo
      read -p "Continue? " -n 1 -r
      echo
    
      if [[ $REPLY =~ ^[Nn]$ ]]
      then
        exit
      fi
    fi
    
    echo "INSTALL"
    

    这样你可以继续使用:

    curl https://gist.githubusercontent.com/.../raw/install.sh | bash
    
相关问题