1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-01-03 13:45:31 +00:00

PR #173 - Merge branch 'rpc-whitelist' of https://github.com/dnnr/attic into merge

This commit is contained in:
Thomas Waldmann 2015-03-06 22:55:53 +01:00
commit 8c4d290a93
2 changed files with 24 additions and 2 deletions

View file

@ -22,8 +22,23 @@ class ConnectionClosed(Error):
class PathNotAllowed(Error): class PathNotAllowed(Error):
"""Repository path not allowed""" """Repository path not allowed"""
class InvalidRPCMethod(Error):
"""RPC method is not valid"""
class RepositoryServer(object): class RepositoryServer(object):
rpc_methods = (
'__len__',
'check',
'commit',
'delete',
'get',
'list',
'negotiate',
'open',
'put',
'repair',
'rollback',
)
def __init__(self, restrict_to_paths): def __init__(self, restrict_to_paths):
self.repository = None self.repository = None
@ -47,6 +62,8 @@ def serve(self):
for type, msgid, method, args in unpacker: for type, msgid, method, args in unpacker:
method = method.decode('ascii') method = method.decode('ascii')
try: try:
if not method in self.rpc_methods:
raise InvalidRPCMethod(method)
try: try:
f = getattr(self, method) f = getattr(self, method)
except AttributeError: except AttributeError:
@ -155,8 +172,10 @@ def fetch_from_cache(args):
raise IntegrityError(res) raise IntegrityError(res)
elif error == b'PathNotAllowed': elif error == b'PathNotAllowed':
raise PathNotAllowed(*res) raise PathNotAllowed(*res)
if error == b'ObjectNotFound': elif error == b'ObjectNotFound':
raise Repository.ObjectNotFound(res[0], self.location.orig) raise Repository.ObjectNotFound(res[0], self.location.orig)
elif error == b'InvalidRPCMethod':
raise InvalidRPCMethod(*res)
raise self.RPCError(error) raise self.RPCError(error)
else: else:
yield res yield res

View file

@ -4,7 +4,7 @@
from attic.testsuite.mock import patch from attic.testsuite.mock import patch
from attic.hashindex import NSIndex from attic.hashindex import NSIndex
from attic.helpers import Location, IntegrityError, UpgradableLock from attic.helpers import Location, IntegrityError, UpgradableLock
from attic.remote import RemoteRepository from attic.remote import RemoteRepository, InvalidRPCMethod
from attic.repository import Repository from attic.repository import Repository
from attic.testsuite import AtticTestCase from attic.testsuite import AtticTestCase
@ -319,6 +319,9 @@ class RemoteRepositoryTestCase(RepositoryTestCase):
def open(self, create=False): def open(self, create=False):
return RemoteRepository(Location('__testsuite__:' + os.path.join(self.tmppath, 'repository')), create=create) return RemoteRepository(Location('__testsuite__:' + os.path.join(self.tmppath, 'repository')), create=create)
def test_invalid_rpc(self):
self.assert_raises(InvalidRPCMethod, lambda: self.repository.call('__init__', None))
class RemoteRepositoryCheckTestCase(RepositoryCheckTestCase): class RemoteRepositoryCheckTestCase(RepositoryCheckTestCase):