comparison mercurial/wireproto.py @ 36755:ff4bc0ab6740 stable

wireproto: check permissions when executing "batch" command (BC) (SEC) For as long as the "batch" command has existed (introduced by bd88561afb4b and first released as part of Mercurial 1.9), that command (like most wire commands introduced after 2008) lacked an entry in the hgweb permissions table. And since we don't verify permissions if an entry is missing from the permissions table, this meant that executing a command via "batch" would bypass all permissions checks. The security implications are significant: a Mercurial HTTP server would allow writes via "batch" wire protocol commands as long as the HTTP request were processed by Mercurial and the process running the Mercurial HTTP server had write access to the repository. The Mercurial defaults of servers being read-only and the various web.* config options to define access control were bypassed. In addition, "batch" could be used to exfiltrate data from servers that were configured to not allow read access. Both forms of permissions bypass could be mitigated to some extent by using HTTP authentication. This would prevent HTTP requests from hitting Mercurial's server logic. However, any authenticated request would still be able to bypass permissions checks via "batch" commands. The easiest exploit was to send "pushkey" commands via "batch" and modify the state of bookmarks, phases, and obsolescence markers. However, I suspect a well-crafted HTTP request could trick the server into running the "unbundle" wire protocol command, effectively performing a full `hg push` to create new changesets on the remote. This commit plugs this gaping security hole by having the "batch" command perform permissions checking on each sub-command that is being batched. We do this by threading a permissions checking callable all the way to the protocol handler. The threading is a bit hacky from a code perspective. But it preserves API compatibility, which is the proper thing to do on the stable branch. One of the subtle things we do is assume that a command with an undefined permission is a "push" command. This is the safest thing to do from a security perspective: we don't want to take chances that a command could perform a write even though the server is configured to not allow writes. As the test changes demonstrate, it is no longer possible to bypass permissions via the "batch" wire protocol command. .. bc:: The "batch" wire protocol command now enforces permissions of each invoked sub-command. Wire protocol commands must define their operation type or the "batch" command will assume they can write data and will prevent their execution on HTTP servers unless the HTTP request method is POST, the server is configured to allow pushes, and the (possibly authenticated) HTTP user is authorized to perform a push.
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 20 Feb 2018 18:55:58 -0800
parents e3c228b4510d
children 7bf80d9d9543
comparison
equal deleted inserted replaced
36754:e3c228b4510d 36755:ff4bc0ab6740
687 def register(func): 687 def register(func):
688 commands[name] = (func, args) 688 commands[name] = (func, args)
689 return func 689 return func
690 return register 690 return register
691 691
692 # TODO define a more appropriate permissions type to use for this.
693 permissions['batch'] = 'pull'
692 @wireprotocommand('batch', 'cmds *') 694 @wireprotocommand('batch', 'cmds *')
693 def batch(repo, proto, cmds, others): 695 def batch(repo, proto, cmds, others):
694 repo = repo.filtered("served") 696 repo = repo.filtered("served")
695 res = [] 697 res = []
696 for pair in cmds.split(';'): 698 for pair in cmds.split(';'):
699 for a in args.split(','): 701 for a in args.split(','):
700 if a: 702 if a:
701 n, v = a.split('=') 703 n, v = a.split('=')
702 vals[unescapearg(n)] = unescapearg(v) 704 vals[unescapearg(n)] = unescapearg(v)
703 func, spec = commands[op] 705 func, spec = commands[op]
706
707 # If the protocol supports permissions checking, perform that
708 # checking on each batched command.
709 # TODO formalize permission checking as part of protocol interface.
710 if util.safehasattr(proto, 'checkperm'):
711 # Assume commands with no defined permissions are writes / for
712 # pushes. This is the safest from a security perspective because
713 # it doesn't allow commands with undefined semantics from
714 # bypassing permissions checks.
715 proto.checkperm(permissions.get(op, 'push'))
716
704 if spec: 717 if spec:
705 keys = spec.split() 718 keys = spec.split()
706 data = {} 719 data = {}
707 for k in keys: 720 for k in keys:
708 if k == '*': 721 if k == '*':