contrib/debugcmdserver.py
author Augie Fackler <augie@google.com>
Wed, 12 Apr 2017 11:23:55 -0700
branchstable
changeset 32050 77eaf9539499
parent 28353 cd03fbd5ab57
child 43076 2372284d9457
permissions -rwxr-xr-x
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.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15259
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     2
#
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     3
# Dumps output generated by Mercurial's command server in a formatted style to a
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     4
# given file or stderr if '-' is specified. Output is also written in its raw
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     5
# format to stdout.
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     6
#
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     7
# $ ./hg serve --cmds pipe | ./contrib/debugcmdserver.py -
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     8
# o, 52   -> 'capabilities: getencoding runcommand\nencoding: UTF-8'
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     9
28353
cd03fbd5ab57 debugcmdserver: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 16687
diff changeset
    10
from __future__ import absolute_import, print_function
cd03fbd5ab57 debugcmdserver: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 16687
diff changeset
    11
import struct
cd03fbd5ab57 debugcmdserver: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 16687
diff changeset
    12
import sys
15259
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    13
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    14
if len(sys.argv) != 2:
28353
cd03fbd5ab57 debugcmdserver: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 16687
diff changeset
    15
    print('usage: debugcmdserver.py FILE')
15259
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    16
    sys.exit(1)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    17
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    18
outputfmt = '>cI'
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    19
outputfmtsize = struct.calcsize(outputfmt)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    20
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    21
if sys.argv[1] == '-':
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    22
    log = sys.stderr
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    23
else:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    24
    log = open(sys.argv[1], 'a')
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    25
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    26
def read(size):
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    27
    data = sys.stdin.read(size)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    28
    if not data:
16687
e34106fa0dc3 cleanup: "raise SomeException()" -> "raise SomeException"
Brodie Rao <brodie@sf.io>
parents: 15259
diff changeset
    29
        raise EOFError
15259
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    30
    sys.stdout.write(data)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    31
    sys.stdout.flush()
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    32
    return data
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    33
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    34
try:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    35
    while True:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    36
        header = read(outputfmtsize)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    37
        channel, length = struct.unpack(outputfmt, header)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    38
        log.write('%s, %-4d' % (channel, length))
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    39
        if channel in 'IL':
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    40
            log.write(' -> waiting for input\n')
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    41
        else:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    42
            data = read(length)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    43
            log.write(' -> %r\n' % data)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    44
        log.flush()
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    45
except EOFError:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    46
    pass
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    47
finally:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    48
    if log != sys.stderr:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    49
        log.close()