# HG changeset patch # User Mads Kiilerich # Date 1323358098 -3600 # Node ID cc021114fc98f0f91e2ab5ce946e062ea414759e # Parent 30c34fde40cc547d8b0c0d3e695f7ec077fbd6fe hg-ssh: use shlex for shell-like parsing of SSH_ORIGINAL_COMMAND The Mercurial ssh protocol is defined as if it was ssh-ing to a shell account on an ordinary ssh server, and where hg was available in $PATH and it executed the command "hg -R REPOPATH serve --stdio". The Mercurial ssh client can in most cases just pass REPOPATH to the shell, but if it contains unsafe characters the client will have to quote it so the shell will pass the right -R value to hg. Correct quoting of repopaths was introduced in d8fa35c28335 and tweaked in 86fc364ca5f8. hg-ssh doesn't create the command via a shell and used a simple parser instead. It worked fine for simple paths without any quoting, but if any kind of quoting was used it failed to parse the command like the shell would do it. This makes hg-ssh behave more like a normal shell with hg in the path would do. diff -r 30c34fde40cc -r cc021114fc98 contrib/hg-ssh --- a/contrib/hg-ssh Sun Jan 15 13:50:12 2012 -0700 +++ b/contrib/hg-ssh Thu Dec 08 16:28:18 2011 +0100 @@ -31,15 +31,20 @@ from mercurial import dispatch -import sys, os +import sys, os, shlex cwd = os.getcwd() allowed_paths = [os.path.normpath(os.path.join(cwd, os.path.expanduser(path))) for path in sys.argv[1:]] orig_cmd = os.getenv('SSH_ORIGINAL_COMMAND', '?') +try: + cmdargv = shlex.split(orig_cmd) +except ValueError, e: + sys.stderr.write("Illegal command %r: %s\n" % (orig_cmd, e)) + sys.exit(-1) -if orig_cmd.startswith('hg -R ') and orig_cmd.endswith(' serve --stdio'): - path = orig_cmd[6:-14] +if cmdargv[:2] == ['hg', '-R'] and cmdargv[3:] == ['serve', '--stdio']: + path = cmdargv[2] repo = os.path.normpath(os.path.join(cwd, os.path.expanduser(path))) if repo in allowed_paths: dispatch.dispatch(dispatch.request(['-R', repo, 'serve', '--stdio'])) diff -r 30c34fde40cc -r cc021114fc98 tests/test-ssh.t --- a/tests/test-ssh.t Sun Jan 15 13:50:12 2012 -0700 +++ b/tests/test-ssh.t Thu Dec 08 16:28:18 2011 +0100 @@ -275,6 +275,19 @@ $ hg id --ssh "python $TESTDIR/dummyssh" "ssh://user@dummy/a repo" 3fb238f49e8c +Test hg-ssh: + + $ SSH_ORIGINAL_COMMAND="'hg' -R 'a repo' serve --stdio" hg id --ssh "python \"$TESTDIR\"/../contrib/hg-ssh \"$TESTTMP/a repo\"" "ssh://user@dummy/a repo" + 3fb238f49e8c + $ SSH_ORIGINAL_COMMAND="'hg' -R 'a repo' serve --stdio" hg id --ssh "python \"$TESTDIR\"/../contrib/hg-ssh \"$TESTTMP\"" "ssh://user@dummy/a repo" + remote: Illegal repository '$TESTTMP/a repo' + abort: no suitable response from remote hg! + [255] + $ SSH_ORIGINAL_COMMAND="'hg' -R 'a'repo' serve --stdio" hg id --ssh "python \"$TESTDIR\"/../contrib/hg-ssh \"$TESTTMP\"" "ssh://user@dummy/a repo" + remote: Illegal command "'hg' -R 'a'repo' serve --stdio": No closing quotation + abort: no suitable response from remote hg! + [255] + $ cat dummylog Got arguments 1:user@dummy 2:hg -R nonexistent serve --stdio Got arguments 1:user@dummy 2:hg -R /$TESTTMP/nonexistent serve --stdio