Introduce receive.denyDeletes
PATCH 1/1 Introduce receive.denyDeletes
Occasionally, it may be useful to prevent branches from getting deleted from a centralized repository, particularly when no administrative access to the server is available to undo it via reflog. It also makes receive.denyNonFastForwards more useful if it is used for access control, since it prevents force-updating refs by deleting and re-creating a ref.
---
Fairly low invasiveness. Includes documentation and test case. I have run all parts of the test suite that use receive-pack, send-pack and friends.
Documentation/config.txt | 4 ++++ builtin-receive-pack.c | 12 ++++++++++++ t/t5400-send-pack.sh | 11 +++++++++++ 3 files changed, 27 insertions(+), 0 deletions(-)
Documentation/config.txt +4 -0
@@ -1188,6 +1188,10 @@ receive.unpackLimit:: especially on slow filesystems. If not set, the value of `transfer.unpackLimit` is used instead. +receive.denyDeletes:: + If set to true, git-receive-pack will deny a ref update that deletes + the ref. Use this to prevent such a ref deletion via a push. + receive.denyNonFastForwards:: If set to true, git-receive-pack will deny a ref update which is not a fast forward. Use this to prevent such an update via a push,
builtin-receive-pack.c +12 -0
@@ -11,6 +11,7 @@static const char receive_pack_usage[] ="git-receive-pack <git-dir>" ; +static int deny_deletes =0 ;static int deny_non_fast_forwards =0 ;static int receive_fsck_objects;static int receive_unpack_limit =-1 ;
@@ -23,6 +24,11 @@ static int capabilities_sent;static int receive_pack_config (const char *var,const char *value,void *cb) { +if (strcmp (var,"receive.denydeletes" ) ==0 ) { + deny_deletes =git_config_bool (var, value); +return 0 ; + } +if (strcmp (var,"receive.denynonfastforwards" ) ==0 ) { deny_non_fast_forwards =git_config_bool (var, value);return 0 ;
@@ -185,6 +191,12 @@ static const char *update(struct command *cmd) "but I can't find it!", sha1_to_hex(new_sha1));return "bad pack" ; } +if (deny_deletes &&is_null_sha1 (new_sha1) && + !is_null_sha1 (old_sha1) && + !prefixcmp (name,"refs/heads/" )) { +error ("denying ref deletion for %s" , name); +return "deletion prohibited" ; + }if (deny_non_fast_forwards && !is_null_sha1 (new_sha1) && !is_null_sha1 (old_sha1) && !prefixcmp (name,"refs/heads/" )) {
t/t5400-send-pack.sh +11 -0
@@ -104,6 +104,17 @@ HOME=`pwd`/no-such-directory export HOME ;# this way we force the victim/.git/config to be used. test_expect_success \ + 'pushing a delete should be denied with denyDeletes' ' + cd victim && + git config receive.denyDeletes true && + git branch extra master && + cd .. && + test -f victim/.git/refs/heads/extra && + git send-pack ./victim/.git/ :extra master && return 1 + rm -f victim/.git/refs/heads/extra +' + +test_expect_success \ 'pushing with --force should be denied with denyNonFastforwards' ' cd victim && git config receive.denyNonFastforwards true &&
Signature · 1 line
1.6.0.3.523.g304d0.dirty
Quoted 5 lines · Jan Krrrger
Occasionally, it may be useful to prevent branches from getting deleted from a centralized repository, particularly when no administrative access to the server is available to undo it via reflog. It also makes receive.denyNonFastForwards more useful if it is used for access control, since it prevents force-updating refs by deleting and re-creating a ref.
...
t/t5400-send-pack.sh HOME=`pwd`/no-such-directory · line 113
Quoted 8 lines
test_expect_success\t/t5400-send-pack.sh +7
-0+ 'pushing a delete should be denied with denyDeletes' ' + cd victim && + git config receive.denyDeletes true && + git branch extra master && + cd .. && + test -f victim/.git/refs/heads/extra && + git send-pack ./victim/.git/ :extra master && return 1
Hmm, that should be:
test_must_fail git send-pack ./victim/.git/ :extra master
Signature · 1 line
Shawn.
Quoted 7 lines · Shawn O. Pearce
t/t5400-send-pack.sh HOME=`pwd`/no-such-directory · line 113
Quoted 3 lines
test_expect_success\
[snip]t/t5400-send-pack.sh +1
-0+ git send-pack ./victim/.git/ :extra master && return 1Hmm, that should be:
test_must_failgit send-pack ./victim/.git/ :extra master
Can I then delete the branch afterwards without lots of juggling (in case the test fails due to a random other reason that the branch accidentally getting deleted by receive-pack)? I'd expect I'd have to save the exit code to a temporary variable and that's just as ugly.
Disclaimer: I sort of just hacked the test case together by stringing together pieces of existing test code, so I don't presume full understanding of the test framework.
By the way, I love what your mail client did to my name.
-Jan
Quoted 4 lines · Jan Krüger
Can I then delete the branch afterwards without lots of juggling (in case the test fails due to a random other reason that the branch accidentally getting deleted by receive-pack)? I'd expect I'd have to save the exit code to a temporary variable and that's just as ugly.
Although I agree that your attempt to allow the test continue even when this test fails is a very good practice, I personally do not find the alternative you mention ugly at all. I actually find that "return 1" uglier because it feels like it knows too much about how test_expect_success is implemented.
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Jan Krüger
Can I then delete the branch afterwards without lots of juggling (in case the test fails due to a random other reason that the branch accidentally getting deleted by receive-pack)? I'd expect I'd have to save the exit code to a temporary variable and that's just as ugly.
If you want to delete the branch after the test is done, do it outside of the test_expect_success's 3rd argument. Then it will run the branch deletion whether or not the test was successful.
Quoted 5 lines
Although I agree that your attempt to allow the test continue even when this test fails is a very good practice, I personally do not find the alternative you mention ugly at all. I actually find that "return 1" uglier because it feels like it knows too much about how
test_expect_successis implemented.
Yea, I also found the "return 1" to be horribly difficult to read, and knowing far too much about the test suite.
Signature · 1 line
Shawn.
Hi,
Quoted 1 line · Jan Krüger
By the way, I love what your mail client did to my name.
I think Shawn forgot to turn his pirate filter off.
Ciao,
Dscho
PATCH v2 1/1 Introduce receive.denyDeletes
In reply to Shawn O. PearceOccasionally, it may be useful to prevent branches from getting deleted from a centralized repository, particularly when no administrative access to the server is available to undo it via reflog. It also makes receive.denyNonFastForwards more useful if it is used for access control since it prevents force-updating by deleting and re-creating a ref.
---
Like this, then?
Documentation/config.txt | 4 ++++ builtin-receive-pack.c | 12 ++++++++++++ t/t5400-send-pack.sh | 11 +++++++++++ 3 files changed, 27 insertions(+), 0 deletions(-)
Documentation/config.txt +4 -0
@@ -1188,6 +1188,10 @@ receive.unpackLimit:: especially on slow filesystems. If not set, the value of `transfer.unpackLimit` is used instead. +receive.denyDeletes:: + If set to true, git-receive-pack will deny a ref update that deletes + the ref. Use this to prevent such a ref deletion via a push. + receive.denyNonFastForwards:: If set to true, git-receive-pack will deny a ref update which is not a fast forward. Use this to prevent such an update via a push,
builtin-receive-pack.c +12 -0
@@ -11,6 +11,7 @@static const char receive_pack_usage[] ="git-receive-pack <git-dir>" ; +static int deny_deletes =0 ;static int deny_non_fast_forwards =0 ;static int receive_fsck_objects;static int receive_unpack_limit =-1 ;
@@ -23,6 +24,11 @@ static int capabilities_sent;static int receive_pack_config (const char *var,const char *value,void *cb) { +if (strcmp (var,"receive.denydeletes" ) ==0 ) { + deny_deletes =git_config_bool (var, value); +return 0 ; + } +if (strcmp (var,"receive.denynonfastforwards" ) ==0 ) { deny_non_fast_forwards =git_config_bool (var, value);return 0 ;
@@ -185,6 +191,12 @@ static const char *update(struct command *cmd) "but I can't find it!", sha1_to_hex(new_sha1));return "bad pack" ; } +if (deny_deletes &&is_null_sha1 (new_sha1) && + !is_null_sha1 (old_sha1) && + !prefixcmp (name,"refs/heads/" )) { +error ("denying ref deletion for %s" , name); +return "deletion prohibited" ; + }if (deny_non_fast_forwards && !is_null_sha1 (new_sha1) && !is_null_sha1 (old_sha1) && !prefixcmp (name,"refs/heads/" )) {
t/t5400-send-pack.sh +11 -0
@@ -103,6 +103,17 @@ unset GIT_CONFIG GIT_CONFIG_LOCAL HOME=`pwd`/no-such-directory export HOME ;# this way we force the victim/.git/config to be used. +test_expect_failure \ + 'pushing a delete should be denied with denyDeletes' ' + cd victim && + git config receive.denyDeletes true && + git branch extra master && + cd .. && + test -f victim/.git/refs/heads/extra && + test_must_fail git send-pack ./victim/.git/ :extra master +' +rm -f victim/.git/refs/heads/extra + test_expect_success \ 'pushing with --force should be denied with denyNonFastforwards' ' cd victim &&
Signature · 1 line
1.6.0.3.524.g86cf.dirty
Quoted 9 lines · Jan Krrrger
Occasionally, it may be useful to prevent branches from getting deleted from a centralized repository, particularly when no administrative access to the server is available to undo it via reflog. It also makes receive.denyNonFastForwards more useful if it is used for access control since it prevents force-updating by deleting and re-creating a ref.
Signed-off-by Jan Krüger <[email protected]>---
Like this, then?
Quoted 81 lines
Documentation/config.txt | 4 ++++ builtin-receive-pack.c | 12 ++++++++++++ t/t5400-send-pack.sh | 11 +++++++++++ 3 files changed, 27 insertions(+), 0 deletions(-)Documentation/config.txt +4
-0@@ -1188,6 +1188,10 @@ receive.unpackLimit:: especially on slow filesystems. If not set, the value of `transfer.unpackLimit` is used instead. +receive.denyDeletes:: + If set to true, git-receive-pack will deny a ref update that deletes + the ref. Use this to prevent such a ref deletion via a push. + receive.denyNonFastForwards:: If set to true, git-receive-pack will deny a ref update which is not a fast forward. Use this to prevent such an update via a push,builtin-receive-pack.c +12
-0@@ -11,6 +11,7 @@static const char receive_pack_usage[] ="git-receive-pack <git-dir>" ; +static int deny_deletes =0 ;static int deny_non_fast_forwards =0 ;static int receive_fsck_objects;static int receive_unpack_limit =-1 ;@@ -23,6 +24,11 @@ static int capabilities_sent;static int receive_pack_config (const char *var,const char *value,void *cb) { +if (strcmp (var,"receive.denydeletes" ) ==0 ) { + deny_deletes =git_config_bool (var, value); +return 0 ; + } +if (strcmp (var,"receive.denynonfastforwards" ) ==0 ) { deny_non_fast_forwards =git_config_bool (var, value);return 0 ;@@ -185,6 +191,12 @@ static const char *update(struct command *cmd) "but I can't find it!", sha1_to_hex(new_sha1));return "bad pack" ; } +if (deny_deletes &&is_null_sha1 (new_sha1) && + !is_null_sha1 (old_sha1) && + !prefixcmp (name,"refs/heads/" )) { +error ("denying ref deletion for %s" , name); +return "deletion prohibited" ; + }if (deny_non_fast_forwards && !is_null_sha1 (new_sha1) && !is_null_sha1 (old_sha1) && !prefixcmp (name,"refs/heads/" )) {t/t5400-send-pack.sh +11
-0@@ -103,6 +103,17 @@ unset GIT_CONFIG GIT_CONFIG_LOCAL HOME=`pwd`/no-such-directory export HOME ;# this way we force the victim/.git/config to be used. +test_expect_failure \ + 'pushing a delete should be denied with denyDeletes' ' + cd victim && + git config receive.denyDeletes true && + git branch extra master && + cd .. && + test -f victim/.git/refs/heads/extra && + test_must_fail git send-pack ./victim/.git/ :extra master +' +rm -f victim/.git/refs/heads/extra + test_expect_success \ 'pushing with --force should be denied with denyNonFastforwards' ' cd victim &&Signature · 1 line
1.6.0.3.524.g86cf.dirty
Signature · 1 line
Shawn.