在shell中禁用命令replace

我有一个运行Subversion和Mercurial服务的ssh盒子。 连接到这个盒子后,脚本会validation用户是否只运行mercurial或svn相关的命令:

#!/bin/bash # # Verify that the requested command is an svnserve call. # # Note that sshd stores the command requested by the client in # the variable "SSH_ORIGINAL_COMMAND". echo $SSH_ORIGINAL_COMMAND | grep -E '^hg -R' ISHG=$? if [[ $SSH_ORIGINAL_COMMAND = "svnserve -t" || $ISHG -eq 0 ]] then exec $SSH_ORIGINAL_COMMAND else echo "You are only allowed svn access to this server." fi 

问题是,hgvalidation不是很干净或安全。 如果我在远程SSH命令中包含反引号,那么“echo $ SSH_ORIGINAL_COMMAND”这一行会很高兴地执行它。 有没有人有任何build议来清理这一点点?

谢谢!

是啊。 不要使用shell。 用一种语言编写程序,可以确保将要执行的唯一二进制文件是白名单。

Mercurial正好带有一个工具! 使用我们提供的contrib/hg-ssh脚本来限制命令。 该文件包含这个头文件来解释如何使用它:

通过command选项在~/.ssh/authorized_keys使用,请参见sshd(8):

 command="hg-ssh path/to/repo1 /path/to/repo2 ~/repo3 ~user/repo4" ssh-dss ... 

(可能还有其他一些有用的选项: no-port-forwardingno-X11-forwardingno-agent-forwarding

这允许通过SSH从/向作为参数给出的存储库进行拉/推。 如果您的所有存储库都是公共目录的子目录,则可以通过以下方式允许更短的path:

 command="cd path/to/my/repositories && hg-ssh repo1 subdir/repo2" 

您可以使用正常shell的模式匹配,例如:

 command="cd repos && hg-ssh user/thomas/* projects/{mercurial,foo}"