wireproto: check permissions when executing "batch" command (BC) (SEC) stable
authorGregory Szorc <gregory.szorc@gmail.com>
Tue, 20 Feb 2018 18:55:58 -0800
branchstable
changeset 36755 ff4bc0ab6740
parent 36754 e3c228b4510d
child 36756 2ecb0fc535b1
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.
mercurial/hgweb/hgweb_mod.py
mercurial/hgweb/protocol.py
mercurial/wireproto.py
tests/test-http-permissions.t
--- a/mercurial/hgweb/hgweb_mod.py	Tue Feb 20 18:54:27 2018 -0800
+++ b/mercurial/hgweb/hgweb_mod.py	Tue Feb 20 18:55:58 2018 -0800
@@ -360,8 +360,10 @@
             try:
                 if query:
                     raise ErrorResponse(HTTP_NOT_FOUND)
+
+                req.checkperm = lambda op: self.check_perm(rctx, req, op)
                 if cmd in perms:
-                    self.check_perm(rctx, req, perms[cmd])
+                    req.checkperm(perms[cmd])
                 return protocol.call(rctx.repo, req, cmd)
             except ErrorResponse as inst:
                 # A client that sends unbundle without 100-continue will
--- a/mercurial/hgweb/protocol.py	Tue Feb 20 18:54:27 2018 -0800
+++ b/mercurial/hgweb/protocol.py	Tue Feb 20 18:55:58 2018 -0800
@@ -52,6 +52,7 @@
         self.response = ''
         self.ui = ui
         self.name = 'http'
+        self.checkperm = req.checkperm
 
     def getargs(self, args):
         knownargs = self._args()
--- a/mercurial/wireproto.py	Tue Feb 20 18:54:27 2018 -0800
+++ b/mercurial/wireproto.py	Tue Feb 20 18:55:58 2018 -0800
@@ -689,6 +689,8 @@
         return func
     return register
 
+# TODO define a more appropriate permissions type to use for this.
+permissions['batch'] = 'pull'
 @wireprotocommand('batch', 'cmds *')
 def batch(repo, proto, cmds, others):
     repo = repo.filtered("served")
@@ -701,6 +703,17 @@
                 n, v = a.split('=')
                 vals[unescapearg(n)] = unescapearg(v)
         func, spec = commands[op]
+
+        # If the protocol supports permissions checking, perform that
+        # checking on each batched command.
+        # TODO formalize permission checking as part of protocol interface.
+        if util.safehasattr(proto, 'checkperm'):
+            # Assume commands with no defined permissions are writes / for
+            # pushes. This is the safest from a security perspective because
+            # it doesn't allow commands with undefined semantics from
+            # bypassing permissions checks.
+            proto.checkperm(permissions.get(op, 'push'))
+
         if spec:
             keys = spec.split()
             data = {}
--- a/tests/test-http-permissions.t	Tue Feb 20 18:54:27 2018 -0800
+++ b/tests/test-http-permissions.t	Tue Feb 20 18:55:58 2018 -0800
@@ -83,13 +83,12 @@
   read not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=listkeys+namespace%3Dphases'
-  200 Script output follows
+  401 read not authorized
   
-  cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b	1
-  publishing	True (no-eol)
+  0
+  read not authorized
+  [1]
 
 TODO custom commands don't check permissions
 
@@ -143,13 +142,12 @@
   read not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=listkeys+namespace%3Dphases'
-  200 Script output follows
+  401 read not authorized
   
-  cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b	1
-  publishing	True (no-eol)
+  0
+  read not authorized
+  [1]
 
 TODO custom commands don't check permissions
 
@@ -201,13 +199,12 @@
   read not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=listkeys+namespace%3Dphases'
-  200 Script output follows
+  401 read not authorized
   
-  cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b	1
-  publishing	True (no-eol)
+  0
+  read not authorized
+  [1]
 
 TODO custom commands don't check permissions
 
@@ -254,13 +251,12 @@
   read not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=listkeys+namespace%3Dphases'
-  200 Script output follows
+  401 read not authorized
   
-  cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b	1
-  publishing	True (no-eol)
+  0
+  read not authorized
+  [1]
 
 TODO custom commands don't check permissions
 
@@ -461,13 +457,12 @@
   read not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=listkeys+namespace%3Dphases'
-  200 Script output follows
+  401 read not authorized
   
-  cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b	1
-  publishing	True (no-eol)
+  0
+  read not authorized
+  [1]
 
 TODO custom commands don't check permissions
 
@@ -514,13 +509,12 @@
   read not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=listkeys+namespace%3Dphases'
-  200 Script output follows
+  401 read not authorized
   
-  cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b	1
-  publishing	True (no-eol)
+  0
+  read not authorized
+  [1]
 
 TODO custom commands don't check permissions
 
@@ -621,13 +615,12 @@
   read not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=listkeys+namespace%3Dphases'
-  200 Script output follows
+  401 read not authorized
   
-  cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b	1
-  publishing	True (no-eol)
+  0
+  read not authorized
+  [1]
 
 TODO custom commands don't check permissions
 
@@ -686,13 +679,12 @@
   pull not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=listkeys+namespace%3Dphases'
-  200 Script output follows
+  401 pull not authorized
   
-  cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b	1
-  publishing	True (no-eol)
+  0
+  pull not authorized
+  [1]
 
 TODO custom commands don't check permissions
 
@@ -742,16 +734,18 @@
   push requires POST request
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=pushkey+namespace%3Dbookmarks%2Ckey%3Dbm%2Cold%3D%2Cnew%3Dcb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b'
-  200 Script output follows
+  405 push requires POST request
   
-  1
+  0
+  push requires POST request
+  [1]
 
   $ hg bookmarks
-     bm                        0:cb9a9f314b8b
+  no bookmarks set
   $ hg bookmark -d bm
+  abort: bookmark 'bm' does not exist
+  [255]
 
 TODO custom commands don't check permissions
 
@@ -781,16 +775,18 @@
   push requires POST request
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=pushkey+namespace%3Dbookmarks%2Ckey%3Dbm%2Cold%3D%2Cnew%3Dcb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b'
-  200 Script output follows
+  405 push requires POST request
   
-  1
+  0
+  push requires POST request
+  [1]
 
   $ hg bookmarks
-     bm                        0:cb9a9f314b8b
+  no bookmarks set
   $ hg bookmark -d bm
+  abort: bookmark 'bm' does not exist
+  [255]
 
 TODO custom commands don't check permissions
 
@@ -823,16 +819,15 @@
   ssl required
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=pushkey+namespace%3Dbookmarks%2Ckey%3Dbm%2Cold%3D%2Cnew%3Dcb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b'
-  200 Script output follows
+  403 ssl required
   
-  1
+  0
+  ssl required
+  [1]
 
   $ hg bookmarks
-     bm                        0:cb9a9f314b8b
-  $ hg book -d bm
+  no bookmarks set
 
 TODO custom commands don't check permissions
 
@@ -887,16 +882,15 @@
   push not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=pushkey+namespace%3Dbookmarks%2Ckey%3Dbm%2Cold%3D%2Cnew%3Dcb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b'
-  200 Script output follows
+  401 push not authorized
   
-  1
+  0
+  push not authorized
+  [1]
 
   $ hg bookmarks
-     bm                        0:cb9a9f314b8b
-  $ hg book -d bm
+  no bookmarks set
 
 TODO custom commands don't check permissions
 
@@ -945,16 +939,15 @@
   push not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=pushkey+namespace%3Dbookmarks%2Ckey%3Dbm%2Cold%3D%2Cnew%3Dcb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b'
-  200 Script output follows
+  401 push not authorized
   
-  1
+  0
+  push not authorized
+  [1]
 
   $ hg bookmarks
-     bm                        0:cb9a9f314b8b
-  $ hg book -d bm
+  no bookmarks set
 
 TODO custom commands don't check permissions
 
@@ -1009,16 +1002,15 @@
   push not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=pushkey+namespace%3Dbookmarks%2Ckey%3Dbm%2Cold%3D%2Cnew%3Dcb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b'
-  200 Script output follows
+  401 push not authorized
   
-  1
+  0
+  push not authorized
+  [1]
 
   $ hg bookmarks
-     bm                        0:cb9a9f314b8b
-  $ hg book -d bm
+  no bookmarks set
 
 TODO custom commands don't check permissions
 
@@ -1067,16 +1059,15 @@
   push not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=pushkey+namespace%3Dbookmarks%2Ckey%3Dbm%2Cold%3D%2Cnew%3Dcb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b'
-  200 Script output follows
+  401 push not authorized
   
-  1
+  0
+  push not authorized
+  [1]
 
   $ hg bookmarks
-     bm                        0:cb9a9f314b8b
-  $ hg book -d bm
+  no bookmarks set
 
 TODO custom commands don't check permissions
 
@@ -1241,16 +1232,15 @@
   push not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=pushkey+namespace%3Dbookmarks%2Ckey%3Dbm%2Cold%3D%2Cnew%3Dcb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b'
-  200 Script output follows
+  401 push not authorized
   
-  1
+  0
+  push not authorized
+  [1]
 
   $ hg bookmarks
-     bm                        0:cb9a9f314b8b
-  $ hg book -d bm
+  no bookmarks set
 
 TODO custom commands don't check permissions
 
@@ -1367,16 +1357,15 @@
   push not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=pushkey+namespace%3Dbookmarks%2Ckey%3Dbm%2Cold%3D%2Cnew%3Dcb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b'
-  200 Script output follows
+  401 push not authorized
   
-  1
+  0
+  push not authorized
+  [1]
 
   $ hg bookmarks
-     bm                        0:cb9a9f314b8b
-  $ hg book -d bm
+  no bookmarks set
 
 TODO custom commands don't check permissions
 
@@ -1432,16 +1421,15 @@
   read not authorized
   [1]
 
-TODO batch command doesn't check permissions
-
   $ get-with-headers.py $LOCALIP:$HGPORT '?cmd=batch' --requestheader 'x-hgarg-1=cmds=pushkey+namespace%3Dbookmarks%2Ckey%3Dbm%2Cold%3D%2Cnew%3Dcb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b'
-  200 Script output follows
+  401 read not authorized
   
-  1
+  0
+  read not authorized
+  [1]
 
   $ hg bookmarks
-     bm                        0:cb9a9f314b8b
-  $ hg book -d bm
+  no bookmarks set
 
 TODO custom commands don't check permissions