Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/wireproto.py @ 35847:e3c228b4510d stable
wireproto: declare operation type for most commands (BC) (SEC)
The permissions model of hgweb relies on a dictionary to declare
the operation associated with each command - either "pull" or
"push." This dictionary was established by d3147b4e3e8a in 2008.
Unfortunately, we neglected to update this dictionary as new
wire protocol commands were introduced.
This commit defines the operations of most wire protocol commands
in the permissions dictionary. The "batch" command is omitted because
it is special and requires a more complex solution.
Since permissions checking is skipped unless a command has an entry in
this dictionary (this security issue will be addressed in a subsequent
commit), the practical effect of this change is that various wire
protocol commands now HTTP 401 if web.deny_read or web.allow-pull,
etc are set to deny access. This is reflected by test changes. Note
how various `hg pull` and `hg push` operations now fail before
discovery. (They fail during the initial "capabilities" request.)
This change fixes a security issue where built-in wire protocol
commands would return repository data even if the web config were
configured to deny access to that data.
I'm on the fence as to whether we should HTTP 401 the capabilities
request. On one hand, it can expose repository metadata and can tell
callers things like what version of Mercurial the server is running.
On the other hand, a client may need to know the capabilities in order
to authenticate in a follow-up request. It appears that Mercurial
clients handle the HTTP 401 on *any* protocol request, so we should
be OK sending a 401 for "capabilities." But if this causes problems,
it should be possible to allow "capabilities" to always work.
.. bc::
Various read-only wire protocol commands now return HTTP 401
Unauthorized if the hgweb configuration denies read/pull access to
the repository.
Previously, various wire protocol commands would still work and
return data if read access was disabled.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Tue, 20 Feb 2018 18:54:27 -0800 |
parents | 742ce6fbc109 |
children | ff4bc0ab6740 |
comparison
equal
deleted
inserted
replaced
35846:742ce6fbc109 | 35847:e3c228b4510d |
---|---|
719 if isinstance(result, ooberror): | 719 if isinstance(result, ooberror): |
720 return result | 720 return result |
721 res.append(escapearg(result)) | 721 res.append(escapearg(result)) |
722 return ';'.join(res) | 722 return ';'.join(res) |
723 | 723 |
724 permissions['between'] = 'pull' | |
724 @wireprotocommand('between', 'pairs') | 725 @wireprotocommand('between', 'pairs') |
725 def between(repo, proto, pairs): | 726 def between(repo, proto, pairs): |
726 pairs = [decodelist(p, '-') for p in pairs.split(" ")] | 727 pairs = [decodelist(p, '-') for p in pairs.split(" ")] |
727 r = [] | 728 r = [] |
728 for b in repo.between(pairs): | 729 for b in repo.between(pairs): |
729 r.append(encodelist(b) + "\n") | 730 r.append(encodelist(b) + "\n") |
730 return "".join(r) | 731 return "".join(r) |
731 | 732 |
733 permissions['branchmap'] = 'pull' | |
732 @wireprotocommand('branchmap') | 734 @wireprotocommand('branchmap') |
733 def branchmap(repo, proto): | 735 def branchmap(repo, proto): |
734 branchmap = repo.branchmap() | 736 branchmap = repo.branchmap() |
735 heads = [] | 737 heads = [] |
736 for branch, nodes in branchmap.iteritems(): | 738 for branch, nodes in branchmap.iteritems(): |
737 branchname = urlreq.quote(encoding.fromlocal(branch)) | 739 branchname = urlreq.quote(encoding.fromlocal(branch)) |
738 branchnodes = encodelist(nodes) | 740 branchnodes = encodelist(nodes) |
739 heads.append('%s %s' % (branchname, branchnodes)) | 741 heads.append('%s %s' % (branchname, branchnodes)) |
740 return '\n'.join(heads) | 742 return '\n'.join(heads) |
741 | 743 |
744 permissions['branches'] = 'pull' | |
742 @wireprotocommand('branches', 'nodes') | 745 @wireprotocommand('branches', 'nodes') |
743 def branches(repo, proto, nodes): | 746 def branches(repo, proto, nodes): |
744 nodes = decodelist(nodes) | 747 nodes = decodelist(nodes) |
745 r = [] | 748 r = [] |
746 for b in repo.branches(nodes): | 749 for b in repo.branches(nodes): |
747 r.append(encodelist(b) + "\n") | 750 r.append(encodelist(b) + "\n") |
748 return "".join(r) | 751 return "".join(r) |
749 | 752 |
753 permissions['clonebundles'] = 'pull' | |
750 @wireprotocommand('clonebundles', '') | 754 @wireprotocommand('clonebundles', '') |
751 def clonebundles(repo, proto): | 755 def clonebundles(repo, proto): |
752 """Server command for returning info for available bundles to seed clones. | 756 """Server command for returning info for available bundles to seed clones. |
753 | 757 |
754 Clients will parse this response and determine what bundle to fetch. | 758 Clients will parse this response and determine what bundle to fetch. |
807 | 811 |
808 return caps | 812 return caps |
809 | 813 |
810 # If you are writing an extension and consider wrapping this function. Wrap | 814 # If you are writing an extension and consider wrapping this function. Wrap |
811 # `_capabilities` instead. | 815 # `_capabilities` instead. |
816 permissions['capabilities'] = 'pull' | |
812 @wireprotocommand('capabilities') | 817 @wireprotocommand('capabilities') |
813 def capabilities(repo, proto): | 818 def capabilities(repo, proto): |
814 return ' '.join(_capabilities(repo, proto)) | 819 return ' '.join(_capabilities(repo, proto)) |
815 | 820 |
816 permissions['changegroup'] = 'pull' | 821 permissions['changegroup'] = 'pull' |
832 missingheads=heads) | 837 missingheads=heads) |
833 cg = changegroupmod.makechangegroup(repo, outgoing, '01', 'serve') | 838 cg = changegroupmod.makechangegroup(repo, outgoing, '01', 'serve') |
834 gen = iter(lambda: cg.read(32768), '') | 839 gen = iter(lambda: cg.read(32768), '') |
835 return streamres(gen=gen) | 840 return streamres(gen=gen) |
836 | 841 |
842 permissions['debugwireargs'] = 'pull' | |
837 @wireprotocommand('debugwireargs', 'one two *') | 843 @wireprotocommand('debugwireargs', 'one two *') |
838 def debugwireargs(repo, proto, one, two, others): | 844 def debugwireargs(repo, proto, one, two, others): |
839 # only accept optional args from the known set | 845 # only accept optional args from the known set |
840 opts = options('debugwireargs', ['three', 'four'], others) | 846 opts = options('debugwireargs', ['three', 'four'], others) |
841 return repo.debugwireargs(one, two, **pycompat.strkwargs(opts)) | 847 return repo.debugwireargs(one, two, **pycompat.strkwargs(opts)) |
905 chunks = bundler.getchunks() | 911 chunks = bundler.getchunks() |
906 prefercompressed = False | 912 prefercompressed = False |
907 | 913 |
908 return streamres(gen=chunks, prefer_uncompressed=not prefercompressed) | 914 return streamres(gen=chunks, prefer_uncompressed=not prefercompressed) |
909 | 915 |
916 permissions['heads'] = 'pull' | |
910 @wireprotocommand('heads') | 917 @wireprotocommand('heads') |
911 def heads(repo, proto): | 918 def heads(repo, proto): |
912 h = repo.heads() | 919 h = repo.heads() |
913 return encodelist(h) + "\n" | 920 return encodelist(h) + "\n" |
914 | 921 |
922 permissions['hello'] = 'pull' | |
915 @wireprotocommand('hello') | 923 @wireprotocommand('hello') |
916 def hello(repo, proto): | 924 def hello(repo, proto): |
917 '''the hello command returns a set of lines describing various | 925 '''the hello command returns a set of lines describing various |
918 interesting things about the server, in an RFC822-like format. | 926 interesting things about the server, in an RFC822-like format. |
919 Currently the only one defined is "capabilities", which | 927 Currently the only one defined is "capabilities", which |
927 @wireprotocommand('listkeys', 'namespace') | 935 @wireprotocommand('listkeys', 'namespace') |
928 def listkeys(repo, proto, namespace): | 936 def listkeys(repo, proto, namespace): |
929 d = repo.listkeys(encoding.tolocal(namespace)).items() | 937 d = repo.listkeys(encoding.tolocal(namespace)).items() |
930 return pushkeymod.encodekeys(d) | 938 return pushkeymod.encodekeys(d) |
931 | 939 |
940 permissions['lookup'] = 'pull' | |
932 @wireprotocommand('lookup', 'key') | 941 @wireprotocommand('lookup', 'key') |
933 def lookup(repo, proto, key): | 942 def lookup(repo, proto, key): |
934 try: | 943 try: |
935 k = encoding.tolocal(key) | 944 k = encoding.tolocal(key) |
936 c = repo[k] | 945 c = repo[k] |
939 except Exception as inst: | 948 except Exception as inst: |
940 r = str(inst) | 949 r = str(inst) |
941 success = 0 | 950 success = 0 |
942 return "%d %s\n" % (success, r) | 951 return "%d %s\n" % (success, r) |
943 | 952 |
953 permissions['known'] = 'pull' | |
944 @wireprotocommand('known', 'nodes *') | 954 @wireprotocommand('known', 'nodes *') |
945 def known(repo, proto, nodes, others): | 955 def known(repo, proto, nodes, others): |
946 return ''.join(b and "1" or "0" for b in repo.known(decodelist(nodes))) | 956 return ''.join(b and "1" or "0" for b in repo.known(decodelist(nodes))) |
947 | 957 |
948 permissions['pushkey'] = 'push' | 958 permissions['pushkey'] = 'push' |