From 4d8ba7788d10e62928404b0272de241580e00e92 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Wed, 19 Mar 2008 21:49:47 +0200 Subject: Allow absolute paths in repo paths, treat them as relative. As the only convenient way to use non-standard SSH ports with git is via the ssh://user@host:port/path syntax, and that syntax forces absolute urls, just force convert absolute paths to relative paths; you'll never really want absolute paths via gitosis, anyway. --- gitosis/serve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gitosis/serve.py') diff --git a/gitosis/serve.py b/gitosis/serve.py index 0f9cb5c..37ad97f 100644 --- a/gitosis/serve.py +++ b/gitosis/serve.py @@ -15,7 +15,7 @@ from gitosis import gitdaemon from gitosis import app from gitosis import util -ALLOW_RE = re.compile("^'(?P[a-zA-Z0-9][a-zA-Z0-9@._-]*(/[a-zA-Z0-9][a-zA-Z0-9@._-]*)*)'$") +ALLOW_RE = re.compile("^'/*(?P[a-zA-Z0-9][a-zA-Z0-9@._-]*(/[a-zA-Z0-9][a-zA-Z0-9@._-]*)*)'$") COMMANDS_READONLY = [ 'git-upload-pack', -- cgit v1.2.3 From 38561aa6a51a2ef6cc04aa119481df62d213ffa4 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Sat, 19 Apr 2008 19:10:36 +0300 Subject: Understand the popular gitosis.conf typo "writeable". Log a warning still, don't want that to get too common. --- gitosis/serve.py | 29 +++++++++++++++++++++++------ gitosis/test/test_access.py | 1 + gitosis/test/test_serve.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) (limited to 'gitosis/serve.py') diff --git a/gitosis/serve.py b/gitosis/serve.py index 37ad97f..c0b7135 100644 --- a/gitosis/serve.py +++ b/gitosis/serve.py @@ -15,6 +15,8 @@ from gitosis import gitdaemon from gitosis import app from gitosis import util +log = logging.getLogger('gitosis.serve') + ALLOW_RE = re.compile("^'/*(?P[a-zA-Z0-9][a-zA-Z0-9@._-]*(/[a-zA-Z0-9][a-zA-Z0-9@._-]*)*)'$") COMMANDS_READONLY = [ @@ -80,6 +82,21 @@ def serve( mode='writable', path=path) + if newpath is None: + # didn't have write access; try once more with the popular + # misspelling + newpath = access.haveAccess( + config=cfg, + user=user, + mode='writeable', + path=path) + if newpath is not None: + log.warning( + 'Repository %r config has typo "writeable", ' + +'should be "writable"', + path, + ) + if newpath is None: # didn't have write access @@ -147,15 +164,15 @@ class Main(app.App): except ValueError: parser.error('Missing argument USER.') - log = logging.getLogger('gitosis.serve.main') + main_log = logging.getLogger('gitosis.serve.main') os.umask(0022) cmd = os.environ.get('SSH_ORIGINAL_COMMAND', None) if cmd is None: - log.error('Need SSH_ORIGINAL_COMMAND in environment.') + main_log.error('Need SSH_ORIGINAL_COMMAND in environment.') sys.exit(1) - log.debug('Got command %(cmd)r' % dict( + main_log.debug('Got command %(cmd)r' % dict( cmd=cmd, )) @@ -168,10 +185,10 @@ class Main(app.App): command=cmd, ) except ServingError, e: - log.error('%s', e) + main_log.error('%s', e) sys.exit(1) - log.debug('Serving %s', newcmd) + main_log.debug('Serving %s', newcmd) os.execvp('git-shell', ['git-shell', '-c', newcmd]) - log.error('Cannot execute git-shell.') + main_log.error('Cannot execute git-shell.') sys.exit(1) diff --git a/gitosis/test/test_access.py b/gitosis/test/test_access.py index 751b0b4..f39444c 100644 --- a/gitosis/test/test_access.py +++ b/gitosis/test/test_access.py @@ -1,5 +1,6 @@ from nose.tools import eq_ as eq +import logging from ConfigParser import RawConfigParser from gitosis import access diff --git a/gitosis/test/test_serve.py b/gitosis/test/test_serve.py index a223c43..56d50b0 100644 --- a/gitosis/test/test_serve.py +++ b/gitosis/test/test_serve.py @@ -1,7 +1,9 @@ from nose.tools import eq_ as eq from gitosis.test.util import assert_raises +import logging import os +from cStringIO import StringIO from ConfigParser import RawConfigParser from gitosis import serve @@ -410,3 +412,32 @@ def test_absolute(): command="git-upload-pack '/foo'", ) eq(got, "git-upload-pack '%s/foo.git'" % tmp) + +def test_typo_writeable(): + tmp = util.maketemp() + repository.init(os.path.join(tmp, 'foo.git')) + cfg = RawConfigParser() + cfg.add_section('gitosis') + cfg.set('gitosis', 'repositories', tmp) + cfg.add_section('group foo') + cfg.set('group foo', 'members', 'jdoe') + cfg.set('group foo', 'writeable', 'foo') + log = logging.getLogger('gitosis.serve') + buf = StringIO() + handler = logging.StreamHandler(buf) + log.addHandler(handler) + try: + got = serve.serve( + cfg=cfg, + user='jdoe', + command="git-receive-pack 'foo'", + ) + finally: + log.removeHandler(handler) + eq(got, "git-receive-pack '%s/foo.git'" % tmp) + handler.flush() + eq( + buf.getvalue(), + "Repository 'foo' config has typo \"writeable\", shou" + +"ld be \"writable\"\n", + ) -- cgit v1.2.3 From 72c754b2f03a139122dc4a3877b05704fa88f751 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Thu, 26 Jun 2008 11:33:48 +0300 Subject: Accept "git upload-pack" etc, for future compatibility. --- gitosis/serve.py | 14 ++++++- gitosis/test/test_serve.py | 100 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 107 insertions(+), 7 deletions(-) (limited to 'gitosis/serve.py') diff --git a/gitosis/serve.py b/gitosis/serve.py index c0b7135..9a91fcb 100644 --- a/gitosis/serve.py +++ b/gitosis/serve.py @@ -21,10 +21,12 @@ ALLOW_RE = re.compile("^'/*(?P[a-zA-Z0-9][a-zA-Z0-9@._-]*(/[a-zA-Z0-9][a-z COMMANDS_READONLY = [ 'git-upload-pack', + 'git upload-pack', ] COMMANDS_WRITE = [ 'git-receive-pack', + 'git receive-pack', ] class ServingError(Exception): @@ -62,9 +64,19 @@ def serve( try: verb, args = command.split(None, 1) except ValueError: - # all known commands take one argument; improve if/when needed + # all known "git-foo" commands take one argument; improve + # if/when needed raise UnknownCommandError() + if verb == 'git': + try: + subverb, args = args.split(None, 1) + except ValueError: + # all known "git foo" commands take one argument; improve + # if/when needed + raise UnknownCommandError() + verb = '%s %s' % (verb, subverb) + if (verb not in COMMANDS_WRITE and verb not in COMMANDS_READONLY): raise UnknownCommandError() diff --git a/gitosis/test/test_serve.py b/gitosis/test/test_serve.py index 56d50b0..f1c1930 100644 --- a/gitosis/test/test_serve.py +++ b/gitosis/test/test_serve.py @@ -23,7 +23,7 @@ def test_bad_newLine(): eq(str(e), 'Command may not contain newline') assert isinstance(e, serve.ServingError) -def test_bad_nospace(): +def test_bad_dash_noargs(): cfg = RawConfigParser() e = assert_raises( serve.UnknownCommandError, @@ -35,6 +35,18 @@ def test_bad_nospace(): eq(str(e), 'Unknown command denied') assert isinstance(e, serve.ServingError) +def test_bad_space_noargs(): + cfg = RawConfigParser() + e = assert_raises( + serve.UnknownCommandError, + serve.serve, + cfg=cfg, + user='jdoe', + command='git upload-pack', + ) + eq(str(e), 'Unknown command denied') + assert isinstance(e, serve.ServingError) + def test_bad_command(): cfg = RawConfigParser() e = assert_raises( @@ -83,7 +95,7 @@ def test_bad_unsafeArguments_dotdot(): eq(str(e), 'Arguments to command look dangerous') assert isinstance(e, serve.ServingError) -def test_bad_forbiddenCommand_read(): +def test_bad_forbiddenCommand_read_dash(): cfg = RawConfigParser() e = assert_raises( serve.ReadAccessDenied, @@ -96,7 +108,20 @@ def test_bad_forbiddenCommand_read(): assert isinstance(e, serve.AccessDenied) assert isinstance(e, serve.ServingError) -def test_bad_forbiddenCommand_write_noAccess(): +def test_bad_forbiddenCommand_read_space(): + cfg = RawConfigParser() + e = assert_raises( + serve.ReadAccessDenied, + serve.serve, + cfg=cfg, + user='jdoe', + command="git upload-pack 'foo'", + ) + eq(str(e), 'Repository read access denied') + assert isinstance(e, serve.AccessDenied) + assert isinstance(e, serve.ServingError) + +def test_bad_forbiddenCommand_write_noAccess_dash(): cfg = RawConfigParser() e = assert_raises( serve.ReadAccessDenied, @@ -111,7 +136,22 @@ def test_bad_forbiddenCommand_write_noAccess(): assert isinstance(e, serve.AccessDenied) assert isinstance(e, serve.ServingError) -def test_bad_forbiddenCommand_write_readAccess(): +def test_bad_forbiddenCommand_write_noAccess_space(): + cfg = RawConfigParser() + e = assert_raises( + serve.ReadAccessDenied, + serve.serve, + cfg=cfg, + user='jdoe', + command="git receive-pack 'foo'", + ) + # error message talks about read in an effort to make it more + # obvious that jdoe doesn't have *even* read access + eq(str(e), 'Repository read access denied') + assert isinstance(e, serve.AccessDenied) + assert isinstance(e, serve.ServingError) + +def test_bad_forbiddenCommand_write_readAccess_dash(): cfg = RawConfigParser() cfg.add_section('group foo') cfg.set('group foo', 'members', 'jdoe') @@ -127,7 +167,23 @@ def test_bad_forbiddenCommand_write_readAccess(): assert isinstance(e, serve.AccessDenied) assert isinstance(e, serve.ServingError) -def test_simple_read(): +def test_bad_forbiddenCommand_write_readAccess_space(): + cfg = RawConfigParser() + cfg.add_section('group foo') + cfg.set('group foo', 'members', 'jdoe') + cfg.set('group foo', 'readonly', 'foo') + e = assert_raises( + serve.WriteAccessDenied, + serve.serve, + cfg=cfg, + user='jdoe', + command="git receive-pack 'foo'", + ) + eq(str(e), 'Repository write access denied') + assert isinstance(e, serve.AccessDenied) + assert isinstance(e, serve.ServingError) + +def test_simple_read_dash(): tmp = util.maketemp() repository.init(os.path.join(tmp, 'foo.git')) cfg = RawConfigParser() @@ -143,7 +199,23 @@ def test_simple_read(): ) eq(got, "git-upload-pack '%s/foo.git'" % tmp) -def test_simple_write(): +def test_simple_read_space(): + tmp = util.maketemp() + repository.init(os.path.join(tmp, 'foo.git')) + cfg = RawConfigParser() + cfg.add_section('gitosis') + cfg.set('gitosis', 'repositories', tmp) + cfg.add_section('group foo') + cfg.set('group foo', 'members', 'jdoe') + cfg.set('group foo', 'readonly', 'foo') + got = serve.serve( + cfg=cfg, + user='jdoe', + command="git upload-pack 'foo'", + ) + eq(got, "git upload-pack '%s/foo.git'" % tmp) + +def test_simple_write_dash(): tmp = util.maketemp() repository.init(os.path.join(tmp, 'foo.git')) cfg = RawConfigParser() @@ -159,6 +231,22 @@ def test_simple_write(): ) eq(got, "git-receive-pack '%s/foo.git'" % tmp) +def test_simple_write_space(): + tmp = util.maketemp() + repository.init(os.path.join(tmp, 'foo.git')) + cfg = RawConfigParser() + cfg.add_section('gitosis') + cfg.set('gitosis', 'repositories', tmp) + cfg.add_section('group foo') + cfg.set('group foo', 'members', 'jdoe') + cfg.set('group foo', 'writable', 'foo') + got = serve.serve( + cfg=cfg, + user='jdoe', + command="git receive-pack 'foo'", + ) + eq(got, "git receive-pack '%s/foo.git'" % tmp) + def test_push_inits_if_needed(): # a push to a non-existent repository (but where config authorizes # you to do that) will create the repository on the fly -- cgit v1.2.3 From 73a032520493f6b4186185d4826d12edb5614135 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Mon, 25 Aug 2008 19:55:45 +0300 Subject: Use "git shell" instead of "git-shell", for compatibility with git 1.6. --- gitosis/serve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gitosis/serve.py') diff --git a/gitosis/serve.py b/gitosis/serve.py index 9a91fcb..867249e 100644 --- a/gitosis/serve.py +++ b/gitosis/serve.py @@ -201,6 +201,6 @@ class Main(app.App): sys.exit(1) main_log.debug('Serving %s', newcmd) - os.execvp('git-shell', ['git-shell', '-c', newcmd]) + os.execvp('git', ['git', 'shell', '-c', newcmd]) main_log.error('Cannot execute git-shell.') sys.exit(1) -- cgit v1.2.3