[PATCH] wireproto: correctly escape batched args and responses (issue4739)

Augie Fackler augie at google.com
Wed Jul 1 15:27:34 CDT 2015


# HG changeset patch
# User Augie Fackler <augie at google.com>
# Date 1435706357 14400
#      Tue Jun 30 19:19:17 2015 -0400
# Node ID 1ce7f4429cbcbd3c84264d62e128c57552cd5608
# Parent  c76e8d14383a44a740d986d87db6f58276fb57e8
wireproto: correctly escape batched args and responses (issue4739)

This issue appears to be as old as wireproto batching itself: I can
reproduce the failure as far back as 08ef6b5f3715 trivially by
rebasing the test changes in this patch, which was back in the 1.9
era. I didn't test before that change, because prior to that the
testfile has a different name and I'm lazy.

Note that the test thought it was checking this case, but it actually
wasn't: it put a literal ; in the arg and response for its greet
command, but the mangle/unmangle step defined in the test meant that
instead of "Fo, =;o" going over the wire, "Gp-!><p" went instead,
which doesn't contain any special characters (those being [.=;]) and
thus not exercising the escaping. The test has been updated to use
pre-unmangled special characters, so the request is now "Fo+<:o",
which mangles to "Gp,=;p". I have confirmed that the test fails
without the adjustment to the escaping rules in wireproto.py.

No existing clients of RPC batching were depending on the old behavior
in any way. The only *actual* users of batchable RPCs in core were:

1) largefiles, wherein it batches up many statlfile calls. It sends
hexlified hashes over the wire and gets a 0, 1, or 2 back as a
response. No risk of special characters.

2) setdiscovery, which was using heads() and known(), both of which
communicate via hexlified nodes. Again, no risk of special characters.

Since the escaping functionality has been completely broken since it
was introduced, we know that it has no users. As such, we can change
the escaping mechanism without having to worry about backwards
compatibility issues.

For the curious, this was detected by chance: it happens that the
lz4-compressed text of a test file for remotefilelog compressed to
something containing a ;, which then caused the failure when I moved
remotefilelog to using batching for file content fetching.

diff --git a/mercurial/wireproto.py b/mercurial/wireproto.py
--- a/mercurial/wireproto.py
+++ b/mercurial/wireproto.py
@@ -182,17 +182,17 @@ def encodelist(l, sep=' '):
 
 def escapearg(plain):
     return (plain
-            .replace(':', '::')
-            .replace(',', ':,')
-            .replace(';', ':;')
-            .replace('=', ':='))
+            .replace(':', ':c')
+            .replace(',', ':o')
+            .replace(';', ':s')
+            .replace('=', ':e'))
 
 def unescapearg(escaped):
     return (escaped
-            .replace(':=', '=')
-            .replace(':;', ';')
-            .replace(':,', ',')
-            .replace('::', ':'))
+            .replace(':e', '=')
+            .replace(':s', ';')
+            .replace(':o', ',')
+            .replace(':c', ':'))
 
 # mapping of options accepted by getbundle and their types
 #
@@ -221,10 +221,11 @@ class wirepeer(peer.peerrepository):
     def _submitbatch(self, req):
         cmds = []
         for op, argsdict in req:
-            args = ','.join('%s=%s' % p for p in argsdict.iteritems())
+            args = ','.join('%s=%s' % (escapearg(k), escapearg(v))
+                            for k, v in argsdict.iteritems())
             cmds.append('%s %s' % (op, args))
         rsp = self._call("batch", cmds=';'.join(cmds))
-        return rsp.split(';')
+        return [unescapearg(r) for r in rsp.split(';')]
     def _submitone(self, op, args):
         return self._call(op, **args)
 
diff --git a/tests/test-wireproto.py b/tests/test-wireproto.py
--- a/tests/test-wireproto.py
+++ b/tests/test-wireproto.py
@@ -43,6 +43,6 @@ clt = clientpeer(srv)
 
 print clt.greet("Foobar")
 b = clt.batch()
-fs = [b.greet(s) for s in ["Fo, =;o", "Bar"]]
+fs = [b.greet(s) for s in ["Fo, =;:<o", "Bar"]]
 b.submit()
 print [f.value for f in fs]
diff --git a/tests/test-wireproto.py.out b/tests/test-wireproto.py.out
--- a/tests/test-wireproto.py.out
+++ b/tests/test-wireproto.py.out
@@ -1,2 +1,2 @@
 Hello, Foobar
-['Hello, Fo, =;o', 'Hello, Bar']
+['Hello, Fo, =;:<o', 'Hello, Bar']


More information about the Mercurial-devel mailing list