comparison contrib/hg-ssh @ 32050:77eaf9539499 stable 4.1.3

dispatch: protect against malicious 'hg serve --stdio' invocations (sec) Some shared-ssh installations assume that 'hg serve --stdio' is a safe command to run for minimally trusted users. Unfortunately, the messy implementation of argument parsing here meant that trying to access a repo named '--debugger' would give the user a pdb prompt, thereby sidestepping any hoped-for sandboxing. Serving repositories over HTTP(S) is unaffected. We're not currently hardening any subcommands other than 'serve'. If your service exposes other commands to users with arbitrary repository names, it is imperative that you defend against repository names of '--debugger' and anything starting with '--config'. The read-only mode of hg-ssh stopped working because it provided its hook configuration to "hg serve --stdio" via --config parameter. This is banned for security reasons now. This patch switches it to directly call ui.setconfig(). If your custom hosting infrastructure relies on passing --config to "hg serve --stdio", you'll need to find a different way to get that configuration into Mercurial, either by using ui.setconfig() as hg-ssh does in this patch, or by placing an hgrc file someplace where Mercurial will read it. mitrandir@fb.com provided some extra fixes for the dispatch code and for hg-ssh in places that I overlooked.
author Augie Fackler <augie@google.com>
date Wed, 12 Apr 2017 11:23:55 -0700
parents 863075fd4cd0
children 42bc7f39376b
comparison
equal deleted inserted replaced
31760:68f263f52d2e 32050:77eaf9539499
30 """ 30 """
31 31
32 # enable importing on demand to reduce startup time 32 # enable importing on demand to reduce startup time
33 from mercurial import demandimport; demandimport.enable() 33 from mercurial import demandimport; demandimport.enable()
34 34
35 from mercurial import dispatch 35 from mercurial import dispatch, ui as uimod
36 36
37 import sys, os, shlex 37 import sys, os, shlex
38 38
39 def main(): 39 def main():
40 cwd = os.getcwd() 40 cwd = os.getcwd()
59 if cmdargv[:2] == ['hg', '-R'] and cmdargv[3:] == ['serve', '--stdio']: 59 if cmdargv[:2] == ['hg', '-R'] and cmdargv[3:] == ['serve', '--stdio']:
60 path = cmdargv[2] 60 path = cmdargv[2]
61 repo = os.path.normpath(os.path.join(cwd, os.path.expanduser(path))) 61 repo = os.path.normpath(os.path.join(cwd, os.path.expanduser(path)))
62 if repo in allowed_paths: 62 if repo in allowed_paths:
63 cmd = ['-R', repo, 'serve', '--stdio'] 63 cmd = ['-R', repo, 'serve', '--stdio']
64 req = dispatch.request(cmd)
64 if readonly: 65 if readonly:
65 cmd += [ 66 if not req.ui:
66 '--config', 67 req.ui = uimod.ui.load()
67 'hooks.pretxnopen.hg-ssh=python:__main__.rejectpush', 68 req.ui.setconfig('hooks', 'pretxnopen.hg-ssh',
68 '--config', 69 'python:__main__.rejectpush', 'hg-ssh')
69 'hooks.prepushkey.hg-ssh=python:__main__.rejectpush' 70 req.ui.setconfig('hooks', 'prepushkey.hg-ssh',
70 ] 71 'python:__main__.rejectpush', 'hg-ssh')
71 dispatch.dispatch(dispatch.request(cmd)) 72 dispatch.dispatch(req)
72 else: 73 else:
73 sys.stderr.write('Illegal repository "%s"\n' % repo) 74 sys.stderr.write('Illegal repository "%s"\n' % repo)
74 sys.exit(255) 75 sys.exit(255)
75 else: 76 else:
76 sys.stderr.write('Illegal command "%s"\n' % orig_cmd) 77 sys.stderr.write('Illegal command "%s"\n' % orig_cmd)