Push options in C Git
v3 cover Push options in C Git
This is not marked for RFC any more, as I do not recall any open points left for discussion. This addresses the only reply from Eric Wong on patch 3:
diff to v2:
send-pack.c +2 -1
@@ -536,7 +536,8 @@ int send_pack(struct send_pack_args *args,for_each_string_list_item (item, args->push_options)packet_buf_write (&sb,"%s" , item->string); -write_or_die (out, sb.buf, sb.len); + +write_or_die (out, sb.buf, sb.len);packet_flush (out);strbuf_release (&sb); }
transport.c +0 -1
@@ -641,7 +641,6 @@ struct transport *transport_get(struct remote *remote, const char *url)struct transport *ret =xcalloc (1 ,sizeof (*ret)); ret->progress =isatty (2 ); - ret->push_options =NULL ;if (!remote)die ("No remote provided to transport_get()" );
Cover letter v2:
================
Allow a user to pass information along a push to the pre/post-receive hook on the remote.
Jeff writes on v1:
Quoted 2 lines
Whereas in Dennis's patches, it was about specific information directly related to the act of pushing.
This allows to transmit arbitrary information as the backends of $VENDOR may have different options available related to the direct act of pushing.
Thanks,
Stefan
Cover letter v1:
================
Allow a user to pass information along a push to the pre/post-receive hook on the remote.
When using a remote that is more than just a plain Git host (e.g. Gerrit, Git{hub/lab}, etc) this may become more obvious: The (server backend specific) push options can instruct the server to:
* open a pull request
* send out emails asking for review
* (un)trigger continuous integration
* set priority for continuous integration (i.e. bots pushing may ask to be treated with lower priority compared to humans)
* ...
Most of these actions can be done on the client side as well, but in these remote-centric workflows it is easier to do that on the remote, which is why we need to transport the information there.
More concrete examples:
* When you want a change in Gerrit to be submitted to refs/heads/master, you push instead to a magic branch refs/for/master and Gerrit will create a change for you (similar to a pull request). Instead we could imagine that you push to a magical refs/heads/master with a push option "create-change".
* When pushing to Gerrit you can already attach some of this information by adding a '%' followed by the parameter to the ref, i.e. when interacting with Gerrit it is possible to do things like[1]:
git push origin HEAD:refs/for/master%draft%topic=example%[email protected]
This is not appealing to our users as it looks like hacks upon hacks to make it work. It would read better if it was spelled as:
git push origin HEAD:refs/for/master \
--push-option draft \
--push-option topic=example \
--push-option [email protected] (with a short form that is even easier to type,
but this is is more intuitive already)
This is a patch series to Git core, which is developed at the same time as a change is proposed to JGit by Dan Wang, see [2].
This code is also available at [3].
Thanks,
Stefan
[1] Not all Gerrit '%' options are documented, so here is a link to source code instead :(
https://gerrit.googlesource.com/gerrit/+/refs/heads/master/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java#1141
[2] https://git.eclipse.org/r/#/c/74570/
[3] https://github.com/stefanbeller/git/tree/pushoptions
Stefan Beller (4):
push options: {pre,post}-receive hook learns about push options
receive-pack: implement advertising and receiving push options
push: accept push options
add a test for push options
Documentation/config.txt | 7 +- Documentation/git-push.txt | 8 ++- Documentation/githooks.txt | 4 ++ Documentation/technical/pack-protocol.txt | 10 +-- Documentation/technical/protocol-capabilities.txt | 8 +++ builtin/push.c | 16 ++++- builtin/receive-pack.c | 85 +++++++++++++++++++---- send-pack.c | 29 ++++++++ send-pack.h | 3 + t/t5544-push-options.sh | 85 +++++++++++++++++++++++ transport.c | 2 + transport.h | 7 ++ 12 files changed, 242 insertions(+), 22 deletions(-) create mode 100755 t/t5544-push-options.sh
Signature · 1 line
2.9.0.141.gdd65b60
PATCH v3 2/4 receive-pack: implement advertising and receiving push options
The pre/post receive hook may be interested in more information from the user. This information can be transmitted when both client and server support the "push-options" capability, which when used is a phase directly after update commands ended by a flush pkt.
Similar to the atomic option, the server capability can be disabled via the receive.advertisePushOptions config variable. While documenting this, fix a nit in the receive.advertiseAtomic wording.
--- Documentation/config.txt | 7 ++- Documentation/technical/pack-protocol.txt | 10 ++-- Documentation/technical/protocol-capabilities.txt | 8 +++ builtin/receive-pack.c | 59 +++++++++++++++++++++++ 4 files changed, 79 insertions(+), 5 deletions(-)
Documentation/config.txt +6 -1
@@ -2410,7 +2410,12 @@ rebase.instructionFormat receive.advertiseAtomic:: By default, git-receive-pack will advertise the atomic push - capability to its clients. If you don't want to this capability + capability to its clients. If you don't want this capability + to be advertised, set this variable to false. + +receive.advertisePushOptions:: + By default, git-receive-pack will advertise the push options capability + to its clients. If you don't want this capability to be advertised, set this variable to false. receive.autogc::
Documentation/technical/pack-protocol.txt +6 -4
@@ -454,7 +454,8 @@ The reference discovery phase is done nearly the same way as it is in the fetching protocol. Each reference obj-id and name on the server is sent in packet-line format to the client, followed by a flush-pkt. The only real difference is that the capability listing is different - the only -possible values are 'report-status', 'delete-refs' and 'ofs-delta'. +possible values are 'report-status', 'delete-refs', 'ofs-delta' and +'push-options'. Reference Update Request and Packfile Transfer ----------------------------------------------
@@ -465,9 +466,10 @@ that it wants to update, it sends a line listing the obj-id currently on the server, the obj-id the client would like to update it to and the name of the reference. -This list is followed by a flush-pkt and then the packfile that should -contain all the objects that the server will need to complete the new -references. +This list is followed by a flush-pkt. Then the push options are transmitted +one per packet followed by another flush-pkt. After that the packfile that +should contain all the objects that the server will need to complete the new +references will be sent. ---- update-request = *shallow ( command-list | push-cert ) [packfile]
Documentation/technical/protocol-capabilities.txt +8 -0
@@ -253,6 +253,14 @@ atomic pushes. If the pushing client requests this capability, the server will update the refs in one atomic transaction. Either all refs are updated or none. +push-options +------------ + +If the server sends the 'push-options' capability it is capable to accept +push options after the update commands have been sent. If the pushing client +requests this capability, the server will pass the options to the pre and post +receive hooks that process this push request. + allow-tip-sha1-in-want ----------------------
builtin/receive-pack.c +59 -0
99 lines not on this page · raw message
Signature · 1 line
2.9.0.141.gd59d3e9.dirty
PATCH v3 3/4 push: accept push options
In reply to Stefan BellerThis implements everything that is required on the client side to make use of push options from the porcelain push command.
--- Documentation/git-push.txt | 8 +++++++- builtin/push.c | 16 +++++++++++++--- send-pack.c | 30 ++++++++++++++++++++++++++++++ send-pack.h | 3 +++ transport.c | 1 + transport.h | 7 +++++++ 6 files changed, 61 insertions(+), 4 deletions(-)
Documentation/git-push.txt +7 -1
@@ -11,7 +11,7 @@ SYNOPSIS [verse] 'git push' [--all | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>] [--repo=<repository>] [-f | --force] [-d | --delete] [--prune] [-v | --verbose] - [-u | --set-upstream] + [-u | --set-upstream] [--push-option=<string>] [--[no-]signed|--sign=(true|false|if-asked)] [--force-with-lease[=<refname>[:<expect>]]] [--no-verify] [<repository> [<refspec>...]]
@@ -156,6 +156,12 @@ already exists on the remote side. Either all refs are updated, or on error, no refs are updated. If the server does not support atomic pushes the push will fail. +-L:: +--push-option:: + Transmit the given string to the server, which passes them to + the pre-receive as well as the post-receive hook. Only C strings + containing no new lines are allowed. + --receive-pack=<git-receive-pack>:: --exec=<git-receive-pack>:: Path to the 'git-receive-pack' program on the remote
builtin/push.c +13 -3
@@ -353,7 +353,8 @@ static int push_with_options(struct transport *transport, int flags)return 1 ; } -static int do_push (const char *repo,int flags) +static int do_push (const char *repo,int flags, +const struct string_list *push_options) {int i, errs;struct remote *remote =pushremote_get (repo);
@@ -376,6 +377,9 @@ static int do_push(const char *repo, int flags)if (remote->mirror) flags |= (TRANSPORT_PUSH_MIRROR |TRANSPORT_PUSH_FORCE ); +if (push_options->nr) + flags |=TRANSPORT_PUSH_OPTIONS ; +if ((flags &TRANSPORT_PUSH_ALL ) && refspec) {if (!strcmp (*refspec,"refs/tags/*" ))return error (_ ("--all and --tags are incompatible" ));
@@ -406,13 +410,16 @@ static int do_push(const char *repo, int flags)for (i =0 ; i < url_nr; i++) {struct transport *transport =transport_get (remote, url[i]); +if (flags &TRANSPORT_PUSH_OPTIONS ) + transport->push_options = push_options;if (push_with_options (transport, flags)) errs++; } }else {struct transport *transport =transport_get (remote,NULL ); - +if (flags &TRANSPORT_PUSH_OPTIONS ) + transport->push_options = push_options;if (push_with_options (transport, flags)) errs++; }
@@ -500,6 +507,8 @@ int cmd_push(int argc, const char **argv, const char *prefix)int push_cert =-1 ;int rc;const char *repo =NULL ;/* default repository */ +static struct string_list push_options =STRING_LIST_INIT_DUP ; +struct option options[] = {OPT__VERBOSITY (&verbosity),OPT_STRING (0 ,"repo" , &repo,N_ ("repository" ),N_ ("repository" )),
@@ -533,6 +542,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)0 ,"signed" , &push_cert,"yes|no|if-asked" ,N_ ("GPG sign the push" ), PARSE_OPT_OPTARG, option_parse_push_signed },OPT_BIT (0 ,"atomic" , &flags,N_ ("request atomic transaction on remote side" ),TRANSPORT_PUSH_ATOMIC ), +OPT_STRING_LIST ('o' ,"push-option" , &push_options,N_ ("server-specific" ),N_ ("option to transmit" )),OPT_SET_INT ('4' ,"ipv4" , &family,N_ ("use IPv4 addresses only" ),TRANSPORT_FAMILY_IPV4 ), OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
@@ -563,7 +573,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)set_refspecs (argv +1 , argc -1 , repo); } - rc =do_push (repo, flags); + rc =do_push (repo, flags, &push_options);if (rc ==-1 )usage_with_options (push_usage, options);else
send-pack.c +30 -0
@@ -260,6 +260,7 @@ static int generate_push_cert(struct strbuf *req_buf, const char *push_cert_nonce) {const struct ref *ref; +struct string_list_item *item;char *signing_key =xstrdup (get_signing_key ());const char *cp, *np;struct strbuf cert =STRBUF_INIT ;
@@ -278,6 +279,12 @@ static int generate_push_cert(struct strbuf *req_buf,strbuf_addf (&cert,"nonce %s\n" , push_cert_nonce);strbuf_addstr (&cert,"\n" ); +if (args->push_options) { +for_each_string_list_item (item, args->push_options) +strbuf_addf (&cert,"push-option %s\n" , item->string); +strbuf_addstr (&cert,"\n" ); + } +for (ref = remote_refs; ref; ref = ref->next) {if (check_to_send_update (ref, args) <0 )continue ;
@@ -370,6 +377,8 @@ int send_pack(struct send_pack_args *args,int agent_supported =0 ;int use_atomic =0 ;int atomic_supported =0 ; +int use_push_options =0 ; +int push_options_supported =0 ;unsigned cmds_sent =0 ;int ret;struct async demux;
@@ -392,6 +401,8 @@ int send_pack(struct send_pack_args *args, args->use_thin_pack =0 ;if (server_supports ("atomic" )) atomic_supported =1 ; +if (server_supports ("push-options" )) + push_options_supported =1 ;if (args->push_cert !=SEND_PACK_PUSH_CERT_NEVER ) {int len;
@@ -418,6 +429,11 @@ int send_pack(struct send_pack_args *args, use_atomic = atomic_supported && args->atomic; +if (args->push_options && !push_options_supported) +die (_ ("the receiving end does not support push options" )); + + use_push_options = push_options_supported && args->push_options; +if (status_report)strbuf_addstr (&cap_buf," report-status" );if (use_sideband)
@@ -426,6 +442,8 @@ int send_pack(struct send_pack_args *args,strbuf_addstr (&cap_buf," quiet" );if (use_atomic)strbuf_addstr (&cap_buf," atomic" ); +if (use_push_options) +strbuf_addstr (&cap_buf," push-options" );if (agent_supported)strbuf_addf (&cap_buf," agent=%s" ,git_user_agent_sanitized ());
@@ -512,6 +530,18 @@ int send_pack(struct send_pack_args *args,strbuf_release (&req_buf);strbuf_release (&cap_buf); +if (use_push_options) { +struct string_list_item *item; +struct strbuf sb =STRBUF_INIT ; + +for_each_string_list_item (item, args->push_options) +packet_buf_write (&sb,"%s" , item->string); + +write_or_die (out, sb.buf, sb.len); +packet_flush (out); +strbuf_release (&sb); + } +if (use_sideband && cmds_sent) {memset (&demux,0 ,sizeof (demux)); demux.proc = sideband_demux;
send-pack.h +3 -0
@@ -1,6 +1,8 @@#ifndef SEND_PACK_H #define SEND_PACK_H +#include "string-list.h" +/* Possible values for push_cert field in send_pack_args. */ #define SEND_PACK_PUSH_CERT_NEVER 0#define SEND_PACK_PUSH_CERT_IF_ASKED 1
@@ -21,6 +23,7 @@ struct send_pack_args { push_cert:2, stateless_rpc:1, atomic:1; +const struct string_list *push_options; };struct option ;
transport.c +1 -0
@@ -510,6 +510,7 @@ static int git_transport_push(struct transport *transport, struct ref *remote_re args.dry_run = !!(flags &TRANSPORT_PUSH_DRY_RUN ); args.porcelain = !!(flags &TRANSPORT_PUSH_PORCELAIN ); args.atomic = !!(flags &TRANSPORT_PUSH_ATOMIC ); + args.push_options = transport->push_options; args.url = transport->url;if (flags &TRANSPORT_PUSH_CERT_ALWAYS )
transport.h +7 -0
@@ -48,6 +48,12 @@ struct transport {*/ unsigned cloning : 1; +/* +* These strings will be passed to the {pre, post}-receive hook, +* on the remote side, if both sides support the push options capability. +*/ +const struct string_list *push_options; +/** * Returns 0 if successful, positive if the option is not * recognized or is inapplicable, and negative if the option
@@ -134,6 +140,7 @@ struct transport {#define TRANSPORT_PUSH_CERT_ALWAYS 2048#define TRANSPORT_PUSH_CERT_IF_ASKED 4096#define TRANSPORT_PUSH_ATOMIC 8192 +#define TRANSPORT_PUSH_OPTIONS 16384#define TRANSPORT_SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)#define TRANSPORT_SUMMARY (x) (int)(TRANSPORT_SUMMARY_WIDTH + strlen(x) - gettext_width(x)), (x)
Signature · 1 line
2.9.0.141.gd59d3e9.dirty
PATCH v3 1/4 push options: {pre,post}-receive hook learns about push options
In reply to Stefan BellerThe environment variable GIT_PUSH_OPTION_COUNT is set to the number of push options sent, and GIT_PUSH_OPTION_{0,1,..} is set to the transmitted option.
The code is not executed as the push options are set to NULL, nor is the new capability advertised.
There was some discussion back and forth how to present these push options to the user as there are some ways to do it:
Keep all options in one environment variable
============================================
+ easiest way to implement in Git
- This would make things hard to parse correctly in the hook.
Put the options in files instead,
filenames are in GIT_PUSH_OPTION_FILES
======================================
+ After a discussion about environment variables and shells, we may not want to put user data into an environment variable (see [1] for example).
+ We could transmit binaries, i.e. we're not bound to C strings as we are when using environment variables to the user.
+ Maybe easier to parse than constructing environment variable names GIT_PUSH_OPTION_{0,1,..} yourself
- cleanup of the temporary files is hard to do reliably
- we have race conditions with multiple clients pushing, hence we'd need to use mkstemp. That's not too bad, but still.
Use environment variables, but restrict to key/value pairs
==========================================================
(When the user pushes a push option foo=bar, we'd GIT_PUSH_OPTION_foo=bar)
+ very easy to parse for a simple model of push options
- it's not sufficient for more elaborate models, e.g. it doesn't allow doubles (e.g. cc=reviewer@email)
Present the options in different environment variables
======================================================
(This is implemented)
* harder to parse as a user, but we have a sample hook for that.
- doesn't allow binary files
+ allows the same option twice, i.e. is not restrictive about options, except for binary files.
+ doesn't clutter a remote directory with (possibly stale) temporary files
As we first want to focus on getting simple strings to work reliably, we go with the last option for now. If we want to do transmission of binaries later, we can just attach a 'side-channel', e.g. "any push option that contains a '\0' is put into a file instead of the environment variable and we'd have new GIT_PUSH_OPTION_FILES, GIT_PUSH_OPTION_FILENAME_{0,1,..} environment variables".
We limit the push options for now
* to not exceed an arbitrary count, and
* to not exceed an arbitrary size.
This serves two purposes:
* DoS protection (i.e. one connection can add no more than 32kB now)
Quoted 5 lines · * We need to figure out how to handle large (>64kB). Jeff wrote:
Yes, but people are also happy when they can use a flexible and standardized tool to do a thing. I'd be more frustrated when I found out that Git's data-pushing protocol has arbitrary limitations (like, say, I can't push a data item larger than a single 64K pkt-line), which would easily just work with something like HTTP POSTs.
So to keep a way open in the future to deal with large pay loads, the size is restricted for now.
[1] 'Shellshock' https://lwn.net/Articles/614218/
--- Documentation/githooks.txt | 6 +++++ builtin/receive-pack.c | 48 +++++++++++++++++++++++++++---------- templates/hooks--pre-receive.sample | 22 +++++++++++++++++ 3 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 templates/hooks--pre-receive.sample
Documentation/githooks.txt +6 -0
@@ -247,6 +247,9 @@ Both standard output and standard error output are forwarded to 'git send-pack' on the other end, so you can simply `echo` messages for the user. +The number of push options are available in the variable GIT_PUSH_OPTION_COUNT +and the options itself are in GIT_PUSH_OPTION_{0,1,...}. + [[update]] update ~~~~~~
@@ -322,6 +325,9 @@ a sample script `post-receive-email` provided in the `contrib/hooks` directory in Git distribution, which implements sending commit emails. +The number of push options are available in the variable GIT_PUSH_OPTION_COUNT +and the options itself are in GIT_PUSH_OPTION_{0,1,...}. + [[post-update]] post-update ~~~~~~~~~~~
builtin/receive-pack.c +35 -13
108 lines not on this page · raw message
templates/hooks--pre-receive.sample new file +22 -0
@@ -0,0 +1,22 @@ +#!/bin/sh +# +# An example hook script to make use of push options. +# The example simply echoes all push options that start with 'echoback=' +# and rejects all pushes when the "reject" push option is used. +# +# To enable this hook, rename this file to "pre-receive". + +if test -n "$GIT_PUSH_OPTION_COUNT"; then + i=0 + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"; do + eval "value=\$GIT_PUSH_OPTION_$i" + case "$value" in + echoback=*) + echo "echo from the pre-receive-hook ${value#*=}" >&2 + ;; + reject) + exit 1 + esac + i=$((i + 1)) + done +fi
Signature · 1 line
2.9.0.141.gd59d3e9.dirty
PATCH v3 4/4 add a test for push options
In reply to Stefan BellerThe functions mk_repo_pair as well as test_refs are borrowed from t5543-atomic-push, with additional hooks installed.
--- t/t5544-push-options.sh | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 t/t5544-push-options.sh
t/t5544-push-options.sh new file +101 -0
@@ -0,0 +1,101 @@ +#!/bin/sh + +test_description='pushing to a repository using push options' + +. ./test-lib.sh + +mk_repo_pair () { + rm -rf workbench upstream && + test_create_repo upstream && + test_create_repo workbench && + ( + cd upstream && + git config receive.denyCurrentBranch warn && + mkdir -p .git/hooks && + cat >.git/hooks/pre-receive <<-'EOF' && + #!/bin/sh + if test -n "$GIT_PUSH_OPTION_COUNT"; then + i=0 + >hooks/pre-receive.push_options + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"; do + eval "value=\$GIT_PUSH_OPTION_$i" + echo $value >>hooks/pre-receive.push_options + i=$((i + 1)) + done + fi + EOF + chmod u+x .git/hooks/pre-receive + + cat >.git/hooks/post-receive <<-'EOF' && + #!/bin/sh + if test -n "$GIT_PUSH_OPTION_COUNT"; then + i=0 + >hooks/post-receive.push_options + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"; do + eval "value=\$GIT_PUSH_OPTION_$i" + echo $value >>hooks/post-receive.push_options + i=$((i + 1)) + done + fi + EOF + chmod u+x .git/hooks/post-receive + ) && + ( + cd workbench && + git remote add up ../upstream + ) +} + +# Compare the ref ($1) in upstream with a ref value from workbench ($2) +# i.e. test_refs second HEAD@{2} +test_refs () { + test $# = 2 && + git -C upstream rev-parse --verify "$1" >expect && + git -C workbench rev-parse --verify "$2" >actual && + test_cmp expect actual +} + +test_expect_success 'one push option works for a single branch' ' + mk_repo_pair && + ( + cd workbench && + test_commit one && + git push --mirror up && + test_commit two && + git push --push-option=asdf up master + ) && + test_refs master master && + echo "asdf" >expect && + test_cmp expect upstream/.git/hooks/pre-receive.push_options && + test_cmp expect upstream/.git/hooks/post-receive.push_options +' + +test_expect_success 'push option denied by remote' ' + mk_repo_pair && + git -C upstream config receive.advertisePushOptions false && + ( + cd workbench && + test_commit one && + git push --mirror up && + test_commit two && + test_must_fail git push --push-option=asdf up master + ) && + test_refs master HEAD@{1} +' + +test_expect_success 'two push options work' ' + mk_repo_pair && + ( + cd workbench && + test_commit one && + git push --mirror up && + test_commit two && + git push --push-option=asdf --push-option="more structured text" up master + ) && + test_refs master master && + printf "asdf\nmore structured text\n" >expect && + test_cmp expect upstream/.git/hooks/pre-receive.push_options && + test_cmp expect upstream/.git/hooks/post-receive.push_options +' + +test_done
Signature · 1 line
2.9.0.141.gd59d3e9.dirty
Re: [PATCH 4/4] add a test for push options
Quoted 8 lines · Stefan Beller
The functions
mk_repo_pairas well astest_refsare borrowed from t5543-atomic-push, with additional hooks installed.Signed-off-by Stefan Beller <[email protected]>--- t/t5544-push-options.sh | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 t/t5544-push-options.sh
FYI:
Applying: add a test for push options
Test number t5544 already taken
Re: [PATCH 4/4] add a test for push options
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Stefan Beller
The functions
mk_repo_pairas well astest_refsare borrowed from t5543-atomic-push, with additional hooks installed.Signed-off-by Stefan Beller <[email protected]>--- t/t5544-push-options.sh | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 t/t5544-push-options.sh
Quoted 5 lines
FYI:
Applying: add a test for push options
Test number t5544 already taken
I'll just move it over to t5545; no need to resend.
Re: [PATCH 1/4] push options: {pre,post}-receive hook learns about push options
In reply to Stefan BellerQuoted 2 lines · Stefan Beller
As we first want to focus on getting simple strings to work reliably, we go with the last option for now.
OK.
Quoted 15 lines
Documentation/githooks.txt +3
-0@@ -247,6 +247,9 @@ Both standard output and standard error output are forwarded to 'git send-pack' on the other end, so you can simply `echo` messages for the user. +The number of push options are available in the variable GIT_PUSH_OPTION_COUNT +and the options itself are in GIT_PUSH_OPTION_{0,1,...}. + [[update]] update ~~~~~~
Documentation/githooks.txt a sample script `post-receive-email` provided in the `contrib/hooks` · line 330
Quoted 6 lines
Documentation/githooks.txt +3
-0@@ -322,6 +325,9 @@ a sample script `post-receive-email` provided in the `contrib/hooks` directory in Git distribution, which implements sending commit emails. +The number of push options are available in the variable GIT_PUSH_OPTION_COUNT +and the options itself are in GIT_PUSH_OPTION_{0,1,...}. +
The mention of "push options" in these two entries sounded a bit too abrupt, at least to me. Perhaps add a short phrase before the sentence, e.g.
When 'git push --push-option=...' is used, the number of push options are avaiable ...
or
The number of push options given on the command line of "git push --push-option=..." can be read from the environment variable GIT_PUSH_OPTION_COUNT, and the options themselves are found in GIT_PUSH_OPTION_0, GIT_PUSH_OPTION_1,...
We can read any of the above variants in two ways to describe what should happen when "git push" is run without "--push-option=...". Is GIT_PUSH_OPTION_COUNT unset, or set to 0? Either way, it may be better to be a bit more explicit.
The hook driver code does not explicitly clear these environment variables; it is safe to assume that these won't seep in from the environment, I think, but I am not sure.
Quoted 37 lines
Documentation/githooks.txt +9
-2@@ -1756,6 +1771,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) if ((commands = read_head_info(&shallow)) != NULL) { const char *unpack_status = NULL; + struct string_list *push_options = NULL; prepare_shallow_info(&si, &shallow); if (!si.nr_ours && !si.nr_theirs)@@ -1764,13 +1780,19 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) unpack_status = unpack_with_sideband(&si); update_shallow_info(commands, &si, &ref); } - execute_commands(commands, unpack_status, &si); + execute_commands(commands, unpack_status, &si, + push_options); if (pack_lockfile) unlink_or_warn(pack_lockfile); if (report_status) report(commands, unpack_status); - run_receive_hook(commands, "post-receive", 1); + run_receive_hook(commands, "post-receive", 1, + push_options); run_update_post_hook(commands); + if (push_options) { + string_list_clear(push_options, 0); + free(push_options); + } if (auto_gc) { const char *argv_gc_auto[] = { "gc", "--auto", "--quiet", NULL,templates/hooks--pre-receive.sample new file +1
-0@@ -0,0 +1,22 @@ +#!/bin/sh
templates/hooks--pre-receive.sample line 11
Quoted 10 lines
templates/hooks--pre-receive.sample new file +10
-0+# +# An example hook script to make use of push options. +# The example simply echoes all push options that start with 'echoback=' +# and rejects all pushes when the "reject" push option is used. +# +# To enable this hook, rename this file to "pre-receive". + +if test -n "$GIT_PUSH_OPTION_COUNT"; then + i=0 + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"; do
Style:
if test -n "$GIT_PUSH_OPTION_COUNT"
then
...
while test ...
do
...
templates/hooks--pre-receive.sample line 22
Quoted 11 lines
templates/hooks--pre-receive.sample new file +11
-0+ eval "value=\$GIT_PUSH_OPTION_$i" + case "$value" in + echoback=*) + echo "echo from the pre-receive-hook ${value#*=}" >&2 + ;; + reject) + exit 1 + esac + i=$((i + 1)) + done +fi
What is suboptimal about the structure of the series is that we won't bisect down to any of the potential bugs in the above code even if we ever see any bug in the future. It also does not hint where push_options is expected to be read in the code in the subsequent patches in the series. If I were doing this series, I would probably have done 2/4 first without plumbing it through (i.e. it is sent and accumulated in a string list at the receiver, and then cleared and freed without being used), and then added the processing (i.e. this step) as the second patch.
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
In reply to Stefan BellerDocumentation/config.txt rebase.instructionFormat · line 2419
Quoted 13 lines · Stefan Beller
While documenting
this, fix a nit in thereceive.advertiseAtomicwording.receive.advertiseAtomic::
Documentation/config.txt +6
-1By default, git-receive-pack will advertise the atomic push - capability to its clients. If you don't want to this capability + capability to its clients. If you don't want this capability + to be advertised, set this variable to false. + +receive.advertisePushOptions:: + By default, git-receive-pack will advertise the push options capability + to its clients. If you don't want this capability to be advertised, set this variable to false.
I think we correcting the nit by avoiding passive voice, i.e.
If you don't want to advertise this capability, set this variable to false.
would make it easier to read.
Documentation/technical/pack-protocol.txt The reference discovery phase is done nearly the same way as it is in the · line 458
Quoted 5 lines
in packet-line format to the client, followed by a flush-pkt. The only real difference is that the capability listing is different - the only
Documentation/technical/pack-protocol.txt +2
-1-possible values are 'report-status', 'delete-refs' and 'ofs-delta'. +possible values are 'report-status', 'delete-refs', 'ofs-delta' and +'push-options'.
OK.
Documentation/technical/protocol-capabilities.txt atomic pushes. If the pushing client requests this capability, the server · line 259
Quoted 4 lines
Documentation/technical/protocol-capabilities.txt +4
-0+push-options +------------ + +If the server sends the 'push-options' capability it is capable to accept
Two nits:
- A comma would make it easier to read.
- "capable" goes with "of <gerund>", while "able" goes with "to <infinitive>".
i.e. "... capability, it is capable of accepting..."
Documentation/technical/protocol-capabilities.txt atomic pushes. If the pushing client requests this capability, the server · line 262
Quoted 3 lines
Documentation/technical/protocol-capabilities.txt +3
-0+push options after the update commands have been sent. If the pushing client +requests this capability, the server will pass the options to the pre and post +receive hooks that process this push request.
Missing dashes, i.e. "pre- and post-receive hooks"?
builtin/receive-pack.c static void show_ref(const char *path, const unsigned char *sha1) · line 221
Quoted 9 lines
builtin/receive-pack.c +2
-0@@ -207,6 +214,8 @@ static void show_ref(const char *path, const unsigned char *sha1) "report-status delete-refs side-band-64k quiet");if (advertise_atomic_push)strbuf_addstr (&cap," atomic" ); +if (advertise_push_options) +strbuf_addstr (&cap," push-options" );if (prefer_ofs_delta)strbuf_addstr (&cap," ofs-delta" );if (push_cert_nonce)
Hmph, was there a good reason to add it in the middle (contrast to the previous addition to the "only possible values are..." enumeration)?
Quoted 1 line
+static struct
string_list*read_push_options()
static struct string_list *read_push_options(void)
Quoted 7 lines
builtin/receive-pack.c +7
-0+{ +int i; +struct string_list *ret =xmalloc (sizeof (*ret)); +string_list_init (ret,1 ); + +/* NEEDSWORK: expose the limitations to be configurable. */ +int max_options =32 ;
builtin/receive-pack.c static struct command *read_head_info(struct sha1_array *shallow) · line 1518
Quoted 10 lines
builtin/receive-pack.c +10
-0+ +/* +* NEEDSWORK: expose the limitations to be configurable; +* Once the limit can be lifted, include a way for payloads +* larger than one pkt, e.g allow a payload of up to +* LARGE_PACKET_MAX - 1 only, and reserve the last byte +* to indicate whether the next pkt continues with this +* push option. +*/ +int max_size =1024 ;
Good NEEDSWORK comments; perhaps also hint that the configuration must not come from the repository level configuration file (i.e. Peff's "scoped configuration" from jk/upload-pack-hook topic)?
Quoted 11 lines
builtin/receive-pack.c +11
-0+for (i =0 ; i < max_options; i++) { +char *line; +int len; + + line =packet_read_line (0 , &len); + +if (!line) +break ; + +if (len > max_size) + die("protocol error: server configuration allows push "
builtin/receive-pack.c static struct command *read_head_info(struct sha1_array *shallow) · line 1540
Quoted 10 lines
builtin/receive-pack.c +10
-0+ "options of size up to %d bytes", max_size); + + len =strcspn (line,"\n" ); + line[len] ='\0' ; + +string_list_append (ret, line); + } +if (i == max_options) +die ("protocol error: server configuration only allows up " +"to %d push options" , max_options);
When not going over ssh://, does the user sees these messages?
More importantly, if we plan to make this configurable and not make the limit a hardwired constant of the wire protocol, it may be better to advertise push-options capability with the limit, e.g. "push-options=32" (or even "push-options=1024/32"), so that the client side can count and abort early?
I wondered how well the extra flush works with the extra framing smart-http does to wrap the wire protocol; as I do not see any change to the http side, I'd assume that there is no issue.
Quoted 17 lines
builtin/receive-pack.c +7
-0+ +return ret; +} +static const char *parse_pack_header (struct pack_header *hdr) {switch (read_pack_header (0 , hdr)) {@@ -1773,6 +1829,9 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)const char *unpack_status =NULL ;struct string_list *push_options =NULL ; +if (use_push_options) + push_options =read_push_options (); +prepare_shallow_info (&si, &shallow);if (!si.nr_ours && !si.nr_theirs) shallow_update =0 ;
Re: [PATCH 3/4] push: accept push options
In reply to Stefan BellerDocumentation/git-push.txt already exists on the remote side. · line 163
Quoted 5 lines · Stefan Beller
+-L:: +--push-option::Documentation/git-push.txt +3
-0+ Transmit the given string to the server, which passes them to + the pre-receive as well as the post-receive hook. Only C strings + containing no new lines are allowed.
This is to affect what happens at the remote end, so I would have understood "-R". I also would have understood "-P" as a short-hand for "--push-option". What is the justification of "-L"?
What does "C strings" mean? Did you mean to say "A sequence of bytes excluding NUL is passed verbatim"?
I do not think I saw anything in the code I reviewed so far that requires "no LF" limitation.
... Ahh, OK, you want to make sure that push-options are one-per-line in the push certificate. While I do not think it is absolutely necessary, starting with a possibly tighter than necessary limitation is much better than starting loose and having to tighten it later.
builtin/push.c static int do_push(const char *repo, int flags) · line 422
Quoted 6 lines
builtin/push.c +2
-1}else {struct transport *transport =transport_get (remote,NULL ); - +if (flags &TRANSPORT_PUSH_OPTIONS ) + transport->push_options = push_options;
The result would be easier to read without the removal of a blank that separates decl/defn and stmt here.
builtin/push.c int cmd_push(int argc, const char **argv, const char *prefix) · line 545
Quoted 5 lines
builtin/push.c +1
-0@@ -533,6 +542,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)0 ,"signed" , &push_cert,"yes|no|if-asked" ,N_ ("GPG sign the push" ), PARSE_OPT_OPTARG, option_parse_push_signed },OPT_BIT (0 ,"atomic" , &flags,N_ ("request atomic transaction on remote side" ),TRANSPORT_PUSH_ATOMIC ), + OPT_STRING_LIST('o', "push-option", &push_options, N_("server-specific"), N_("option to transmit")),
Here it seems to expect "-o". If we really want a short option, "-o" would probably be OK, as I do not think "git push" wants to have "send the output to this file" option.
Quoted 13 lines
send-pack.c +1
-0@@ -260,6 +260,7 @@ static int generate_push_cert(struct strbuf *req_buf, const char *push_cert_nonce) {const struct ref *ref; +struct string_list_item *item;char *signing_key =xstrdup (get_signing_key ());const char *cp, *np;struct strbuf cert =STRBUF_INIT ;
send-pack.c static int generate_push_cert(struct strbuf *req_buf, · line 285
Quoted 7 lines
send-pack.c +4
-0@@ -278,6 +279,12 @@ static int generate_push_cert(struct strbuf *req_buf,strbuf_addf (&cert,"nonce %s\n" , push_cert_nonce);strbuf_addstr (&cert,"\n" ); +if (args->push_options) { +for_each_string_list_item (item, args->push_options) +strbuf_addf (&cert,"push-option %s\n" , item->string); +strbuf_addstr (&cert,"\n" );
Why the extra blank?
I would actually have expected to see
certificate version ...
pusher ...
<datestamp>
pushee ... # optional
nonce ... # optional
push-option ... # optional
push-option ... # optional
<old> <new> <name>
...
by adding this between the two lines in the pre-context of this hunk, i.e.
if (push_cert_nonce[0]) strbuf_addf(&cert, "nonce %s\n", push_cert_nonce); if (args->push_options) for_each_string_list_item(item, args->push_options) strbuf_addf(&cert, "push-option %s\n", item->string); strbuf_addstr(&cert, "\n");
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
In reply to Junio C HamanoQuoted 14 lines · Junio C Hamano
builtin/receive-pack.c static void show_ref(const char *path, const unsigned char *sha1) · line 221
Quoted 10 lines
builtin/receive-pack.c +2
-0@@ -207,6 +214,8 @@ static void show_ref(const char *path, const unsigned char *sha1) "report-status delete-refs side-band-64k quiet");if (advertise_atomic_push)strbuf_addstr (&cap," atomic" ); +if (advertise_push_options) +strbuf_addstr (&cap," push-options" );if (prefer_ofs_delta)strbuf_addstr (&cap," ofs-delta" );if (push_cert_nonce)Hmph, was there a good reason to add it in the middle (contrast to the previous addition to the "only possible values are..." enumeration)?
No, there is no good objective reason. I added it just after the atomic flag as that is what I implemented.
Is there a reason for a particular order of capabilities? I always considered it a set of strings, i.e. any order is valid and there is no preference in which way to put it.
Quoted 18 lines
Quoted 2 lines
+static struct
string_list*read_push_options()static struct
string_list*read_push_options(void)Quoted 13 lines
builtin/receive-pack.c +13
-0+{ +int i; +struct string_list *ret =xmalloc (sizeof (*ret)); +string_list_init (ret,1 ); + +/* NEEDSWORK: expose the limitations to be configurable. */ +int max_options =32 ; + +/* +* NEEDSWORK: expose the limitations to be configurable; +* Once the limit can be lifted, include a way for payloads +* larger than one pkt, e.g allow a payload of up to +* LARGE_PACKET_MAX - 1 only, and reserve the last byte
Quoted 8 lines
builtin/receive-pack.c static struct command *read_head_info(struct sha1_array *shallow) · line 1518
Quoted 4 lines
builtin/receive-pack.c +4
-0+* to indicate whether the next pkt continues with this +* push option. +*/ +int max_size =1024 ;Good NEEDSWORK comments; perhaps also hint that the configuration must not come from the repository level configuration file (i.e. Peff's "scoped configuration" from jk/upload-pack-hook topic)?
Ok, I reviewed that series. It is unclear to me how the attack would actually look like in that case.
Quoted 4 lines · In 20b20a22f8f Jeff writes:
Because we promise that
upload-pack is safe to run in an untrusted repository, we cannot execute arbitrary code or commands found in the repository (neither in hooks/, nor in the config).
I agree on this for all content that can be modified by the user (e.g. files in the work tree such as .gitmodules), but the .git/config file cannot be changed remotely. So I wonder how an attack would look like for a hosting provider or anyone else?
We still rely on a sane system and trust /etc/gitconfig
so we do trust the host/admin but not the user?
Quoted 25 lines
Quoted 12 lines
builtin/receive-pack.c +11
-0+for (i =0 ; i < max_options; i++) { +char *line; +int len; + + line =packet_read_line (0 , &len); + +if (!line) +break ; + +if (len > max_size) + die("protocol error: server configuration allows push "builtin/receive-pack.c static struct command *read_head_info(struct sha1_array *shallow) · line 1540
Quoted 10 lines
builtin/receive-pack.c +10
-0+ "options of size up to %d bytes", max_size); + + len =strcspn (line,"\n" ); + line[len] ='\0' ; + +string_list_append (ret, line); + } +if (i == max_options) +die ("protocol error: server configuration only allows up " +"to %d push options" , max_options);When not going over ssh://, does the user sees these messages?
Quoted 5 lines
More importantly, if we plan to make this configurable and not make the limit a hardwired constant of the wire protocol, it may be better to advertise push-options capability with the limit, e.g. "push-options=32" (or even "push-options=1024/32"), so that the client side can count and abort early?
Yeah we may want to start out with a strict format here indicating the parameters used for evaluating the size.
So what do these numbers mean?
I assume (and hence I should document that,) that the first (1024) is the maximum number of bytes per push option. The second number (32) is the number of push options (not the number of pkts, as one push option may take more than one pkt if the first number is larger than 65k, see the NEEDSWORK comment.)
Do we really need 2 numbers, or could we just have one number describing the maximum total size in bytes before the remote rejects the connection?
Quoted 4 lines
I wondered how well the extra flush works with the extra framing smart-http does to wrap the wire protocol; as I do not see any change to the http side, I'd assume that there is no issue.
That's a dangerous assumption of yours, as I did not test the https side, yet.
Quoted 19 lines
Quoted 18 lines
builtin/receive-pack.c +7
-0+ +return ret; +} +static const char *parse_pack_header (struct pack_header *hdr) {switch (read_pack_header (0 , hdr)) {@@ -1773,6 +1829,9 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)const char *unpack_status =NULL ;struct string_list *push_options =NULL ; +if (use_push_options) + push_options =read_push_options (); +prepare_shallow_info (&si, &shallow);if (!si.nr_ours && !si.nr_theirs) shallow_update =0 ;
Re: [PATCH 1/4] push options: {pre,post}-receive hook learns about push options
In reply to Junio C HamanoQuoted 11 lines · Junio C Hamano
What is suboptimal about the structure of the series is that we won't bisect down to any of the potential bugs in the above code even if we ever see any bug in the future.
It also does not hint
wherepush_optionsis expected to be read in the code in the subsequent patches in the series. If I were doing this series, I would probably have done 2/4 first without plumbing it through (i.e. it is sent and accumulated in a string list at the receiver, and then cleared and freed without being used), and then added the processing (i.e. this step) as the second patch.
But your first patch (2/4) would not yet advertise the capability? Or advertise and then just ignoring it?
That shadows other bugs that would not properly bisect, I'd imagine?
It is better for documentation purposes in this patch though. It makes the other patch harder as "it allows transmitting push options, but in that patch nothing of value is done with them."
So I'll see if I can reorder easily.
Thanks,
Stefan
Re: [PATCH 4/4] add a test for push options
In reply to Junio C HamanoQuoted 12 lines · Junio C Hamano
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Stefan Beller
The functions
mk_repo_pairas well astest_refsare borrowed from t5543-atomic-push, with additional hooks installed.Signed-off-by Stefan Beller <[email protected]>--- t/t5544-push-options.sh | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 t/t5544-push-options.sh
Quoted 7 lines
Quoted 5 lines
FYI:
Applying: add a test for push options
Test number t5544 already takenI'll just move it over to t5545; no need to resend.
I'd resend because of the the first three patches anyway, so I can include a renamed version of tests, too.
Re: [PATCH 1/4] push options: {pre,post}-receive hook learns about push options
In reply to Stefan BellerQuoted 2 lines · Stefan Beller
But your first patch (2/4) would not yet advertise the capability? Or advertise and then just ignoring it?
As I wrote...
Quoted 5 lines
Quoted 5 lines
If I were doing this series, I
would probably have done 2/4 first without plumbing it through (i.e. it is sent and accumulated in a string list at the receiver, and then cleared and freed without being used), and then added the processing (i.e. this step) as the second patch.
... I would imagine it would advertise, allow the other side to send, receive and collect, and then discard (properly) without using.
Quoted 5 lines
It is better for documentation purposes in this patch though. It makes the other patch harder as "it allows transmitting push options, but in that patch nothing of value is done with them."
So I'll see if I can reorder easily.
It does not matter too much. Let's not spend too much time on the ordering.
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
In reply to Stefan BellerQuoted 26 lines · Stefan Beller
Quoted 13 lines
builtin/receive-pack.c static struct command *read_head_info(struct sha1_array *shallow) · line 1518
Quoted 12 lines
builtin/receive-pack.c +12
-0+/* NEEDSWORK: expose the limitations to be configurable. */ +int max_options =32 ; + +/* +* NEEDSWORK: expose the limitations to be configurable; +* Once the limit can be lifted, include a way for payloads +* larger than one pkt, e.g allow a payload of up to +* LARGE_PACKET_MAX - 1 only, and reserve the last byte +* to indicate whether the next pkt continues with this +* push option. +*/ +int max_size =1024 ;Quoted 3 lines
Good NEEDSWORK comments; perhaps also hint that the configuration must not come from the repository level configuration file (i.e. Peff's "scoped configuration" from jk/upload-pack-hook topic)?
Ok, I reviewed that series. It is unclear to me how the attack would actually look like in that case.
Quoted 4 lines · In 20b20a22f8f Jeff writes:
Because we promise that
upload-pack is safe to run in an untrusted repository, we cannot execute arbitrary code or commands found in the repository (neither in hooks/, nor in the config).
Quoted 6 lines
I agree on this for all content that can be modified by the user (e.g. files in the work tree such as .gitmodules), but the .git/config file cannot be changed remotely. So I wonder how an attack would look like for a hosting provider or anyone else?
We still rely on a sane system and trust/etc/gitconfig
so we do trust the host/admin but not the user?
The problem is for hosting sites which serve repositories via git-daemon from untrusted users who have real shell accounts (e.g., you set up git-daemon to run as the "daemon" user serving repositories out of people's home directories; you don't want users to escalate their shell access into running arbitrary code as "daemon").
But I don't think that case applies here. That is about running upload-pack on an untrusted repository, but your changes here are part of receive-pack. In such a scenario, users should be pushing as themselves via ssh. And if they are not (e.g., the admin set up push-over-smart-http centrally), they are already screwed, as a malicious user could just set up a pre-receive hook.
IOW, we promise only that upload-pack is safe to run an untrusted repo, but not receive-pack.
-Peff
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
Quoted 35 lines · Jeff King
Quoted 26 lines · Stefan Beller
Quoted 13 lines
builtin/receive-pack.c static struct command *read_head_info(struct sha1_array *shallow) · line 1518
Quoted 12 lines
builtin/receive-pack.c +12
-0+/* NEEDSWORK: expose the limitations to be configurable. */ +int max_options =32 ; + +/* +* NEEDSWORK: expose the limitations to be configurable; +* Once the limit can be lifted, include a way for payloads +* larger than one pkt, e.g allow a payload of up to +* LARGE_PACKET_MAX - 1 only, and reserve the last byte +* to indicate whether the next pkt continues with this +* push option. +*/ +int max_size =1024 ;Quoted 3 lines
Good NEEDSWORK comments; perhaps also hint that the configuration must not come from the repository level configuration file (i.e. Peff's "scoped configuration" from jk/upload-pack-hook topic)?
Ok, I reviewed that series. It is unclear to me how the attack would actually look like in that case.
Quoted 4 lines · In 20b20a22f8f Jeff writes:
Because we promise that
upload-pack is safe to run in an untrusted repository, we cannot execute arbitrary code or commands found in the repository (neither in hooks/, nor in the config).Quoted 6 lines
I agree on this for all content that can be modified by the user (e.g. files in the work tree such as .gitmodules), but the .git/config file cannot be changed remotely. So I wonder how an attack would look like for a hosting provider or anyone else?
We still rely on a sane system and trust/etc/gitconfig
so we do trust the host/admin but not the user?
Quoted 5 lines
The problem is for hosting sites which serve repositories via git-daemon from untrusted users who have real shell accounts (e.g., you set up git-daemon to run as the "daemon" user serving repositories out of people's home directories; you don't want users to escalate their shell access into running arbitrary code as "daemon").
I think you would want to lock down the
hosting site as much as possible and not put untrusted users home directories on there? So it is hard for me to imagine you'd go for such a setup in practice.
Quoted 7 lines
But I don't think that case applies here. That is about running upload-pack on an untrusted repository, but your changes here are part of receive-pack. In such a scenario, users should be pushing as themselves via ssh. And if they are not (e.g., the admin set up push-over-smart-http centrally), they are already screwed, as a malicious user could just set up a pre-receive hook.
I hear that as: "The pre-receive hook itself can do much more damage than an oversized push option payload".
OK.
Quoted 5 lines
IOW, we promise only that upload-pack is safe to run an untrusted repo, but not receive-pack.
-Peff
Thanks,
Stefan
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
In reply to Stefan BellerQuoted 6 lines · Stefan Beller
No, there is no good objective reason. I added it just after the atomic flag as that is what I implemented.
Is there a reason for a particular order of capabilities? I always considered it a set of strings, i.e. any order is valid and there is no preference in which way to put it.
That is correct, but "there is no inherent order or grouping" does not lead to "hence it is OK to put a new thing at a random place in the middle."
If a reviewer sees a new thing at some specific point in a collection, when the collection is understood not to have any specific order or grouping, it makes the reviewer doubt the belief--there must be a reason why this was placed not at the end; otherwise a new thing wouldn't be placed randomly at the middle.
If you place a new thing at the end, it still leaves ambiguity; it may be there because there is no inherent order or grouping, or the new one is sorts later than the one that used to be at the last or somehow related to it.
Quoted 6 lines
I agree on this for all content that can be modified by the user (e.g. files in the work tree such as .gitmodules), but the .git/config file cannot be changed remotely. So I wonder how an attack would look like for a hosting provider or anyone else?
We still rely on a sane system and trust/etc/gitconfig
so we do trust the host/admin but not the user?
It is not "we" do trust; it is "we let host/admin trust itself while making sure that they can protect themselves from their users".
Quoted 16 lines
Quoted 5 lines
More importantly, if we plan to make this configurable and not make the limit a hardwired constant of the wire protocol, it may be better to advertise push-options capability with the limit, e.g. "push-options=32" (or even "push-options=1024/32"), so that the client side can count and abort early?
Yeah we may want to start out with a strict format here indicating the parameters used for evaluating the size.
So what do these numbers mean?I assume (and hence I should document that,) that the first (1024) is the maximum number of bytes per push option. The second number (32) is the number of push options (not the number of pkts, as one push option may take more than one pkt if the first number is larger than 65k, see the NEEDSWORK comment.)
Quoted 3 lines
Do we really need 2 numbers, or could we just have one number describing the maximum total size in bytes before the remote rejects the connection?
That's for you to decide. push-options=32 is probably OK but it will prevent a well-behaving "git push" from catching a request that will be rejected, if you are basing the receiver side decision on the other number.
The suggestion was primarily my reaction after seeing the two new calls to die() on the receiver side, whose message I was not sure will be given to the user who is pushing, i.e.
Quoted 1 line
When not going over ssh://, does the user see these messages?
Your "git push" will be collecting the options in a string-list while parsing the options, so it would be able to check immediately upon seeing the advertised capability if it will trigger this protocol error even before making the request, which would be a good thing to do, and I am reasonably sure we can give a better error message if we do that on the local side without having the receiver to tell you (for one thing, we can i18n the local side error message more easily).
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
In reply to Stefan BellerQuoted 10 lines · Stefan Beller
Quoted 5 lines
The problem is for hosting sites which serve repositories via git-daemon from untrusted users who have real shell accounts (e.g., you set up git-daemon to run as the "daemon" user serving repositories out of people's home directories; you don't want users to escalate their shell access into running arbitrary code as "daemon").
I think you would want to lock down the
hosting site as much as possible and not put untrusted users home directories on there? So it is hard for me to imagine you'd go for such a setup in practice.
Sure, I think that's a good way to run a hosting site, too. But it doesn't mean people don't have other needs. kernel.org was run as I mentioned above for many years.
Another related case: you have a multi-user server where Alice might run "git fetch server:~bob/repo.git". Alice does not want to run arbitrary code based on what is in Bob's repo.git. Even if they are in the same company, it is a poor security practice.
Quoted 9 lines
Quoted 6 lines
But I don't think that case applies here. That is about running upload-pack on an untrusted repository, but your changes here are part of receive-pack. In such a scenario, users should be pushing as themselves via ssh. And if they are not (e.g., the admin set up push-over-smart-http centrally), they are already screwed, as a malicious user could just set up a pre-receive hook.
I hear that as: "The pre-receive hook itself can do much more damage than an oversized push option payload".
Exactly. Or more to the point: we promise nothing here except for upload-pack, so changes to receive-pack do not have to worry about this issue at all.
-Peff
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
In reply to Junio C HamanoHi,
Quoted 7 lines · Junio C Hamano
Quoted 5 lines · Stefan Beller
Quoted 5 lines
More importantly, if we plan to make this configurable and not make the limit a hardwired constant of the wire protocol, it may be better to advertise push-options capability with the limit, e.g. "push-options=32" (or even "push-options=1024/32"), so that the client side can count and abort early?
Sorry to butt into the conversation late, but: I am not yet convinced.
Is the idea that if the push options were very large, this would save the client from the cost of sending them? But this comes with a
downside: the server doesn't get to send an error message about where the maximum number of push options can come from (e.g., with a link to a page where the limit can be adjusted, or with an explanation of when clients tend to run into this problem and what they should do instead).
So I'd like to propose an alternative. What if the client tells the server the number of push options early on (and possibly also a cap on the length of those push options)? That way, the client doesn't have to waste bit sending the push options but the server gets an opportunity to send a helpful error message on sideband 3.
server> HEAD\0push-options ...
client> ... commands ...
client> push-options 2
client> my-first-option
client> my-second-option
Thanks,
Jonathan
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
Quoted 11 lines · Jonathan Nieder
Hi,
Quoted 7 lines · Junio C Hamano
Quoted 5 lines · Stefan Beller
Quoted 5 lines
More importantly, if we plan to make this configurable and not make the limit a hardwired constant of the wire protocol, it may be better to advertise push-options capability with the limit, e.g. "push-options=32" (or even "push-options=1024/32"), so that the client side can count and abort early?
Quoted 4 lines
Sorry to butt into the conversation late, but: I am not yet convinced.
Is the idea that if the push options were very large, this would save the client from the cost of sending them?
Not really. I have no strong opinion on the benefit of limiting number/size. Stefan limited the number/size at the receiving end and made receiving end die with its message. I was merely trying to tweak the arrangement so that the sending end can complain with its own message, possibly in its own language, especially because it was unclear to me if the die() message on the receiving end would always go back to the sending end correctly.
Quoted 7 lines
But this comes with a
downside: the server doesn't get to send an error message about where
the maximum number of push options can come from (e.g., with a link to a page where the limit can be adjusted, or with an explanation of when clients tend to run into this problem and what they should do instead).
Hmm, interesting point. That would be better told by the receiving end, as the way to configure it (if offered) would be different from installation to installation.
Quoted 11 lines
So I'd like to propose an alternative. What if the client tells the server the number of push options early on (and possibly also a cap on the length of those push options)? That way, the client doesn't have to waste bit sending the push options but the server gets an opportunity to send a helpful error message on sideband 3.
server> HEAD\0push-options ...
client> ... commands ...
client> push-options 2
client> my-first-option
client> my-second-option
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
Quoted 13 lines · Junio C Hamano
Quoted 11 lines · Jonathan Nieder
Hi,
Quoted 7 lines · Junio C Hamano
Quoted 5 lines · Stefan Beller
Quoted 5 lines
More importantly, if we plan to make this configurable and not make the limit a hardwired constant of the wire protocol, it may be better to advertise push-options capability with the limit, e.g. "push-options=32" (or even "push-options=1024/32"), so that the client side can count and abort early?
Quoted 8 lines
Quoted 4 lines
Sorry to butt into the conversation late, but: I am not yet convinced.
Is the idea that if the push options were very large, this would save the client from the cost of sending them?
Not really. I have no strong opinion on the benefit of limiting number/size. Stefan limited the number/size at the receiving end and made receiving end die with its message.
Jeff claimed we'd need some sort of DoS protection for this feature, so I considered just die-ing enough for an initial implementation.
Quoted 5 lines
I was merely trying to
tweak the arrangement so that the sending end can complain with its own message, possibly in its own language, especially because it was unclear to me if thedie()message on the receiving end would always go back to the sending end correctly.
I'm currently implementing Jonathans suggestion as it seems to be a reasonable trade off (client hasn't sent a lot of data when it is decided it doesn't go through,
the server can complain with a reasonable error message, only downside: no i18n localisation support on the client side as the server will currently report the
error in English).
That method will make heavy use of rp_error that uses the side band for communicating the actual error message.
Quoted 9 lines
Quoted 8 lines
But this comes with a
downside: the server doesn't get to send an error message about where
the maximum number of push options can come from (e.g., with a link to a page where the limit can be adjusted, or with an explanation of when clients tend to run into this problem and what they should do instead).
Quoted 16 lines
Hmm, interesting point. That would be better told by the receiving end, as the way to configure it (if offered) would be different from installation to installation.
Quoted 11 lines
So I'd like to propose an alternative. What if the client tells the server the number of push options early on (and possibly also a cap on the length of those push options)? That way, the client doesn't have to waste bit sending the push options but the server gets an opportunity to send a helpful error message on sideband 3.
server> HEAD\0push-options ...
client> ... commands ...
client> push-options 2
client> my-first-option
client> my-second-option
Another (slightly offtopic) observation:
If in the future we'll need to transmit push options >64k, instead of splitting the push option to multiple packets, we could invent "large packets". The current upper bound for packets is artificailly low, such that the server is able to interleave sideband information with the actual data in a fetch and have the client display the progress in a timely manner. When pushing to the server we'd not need to have progress information (the server doesn't care, and the client knows the size it is pushing).
As of today a packet consists of 4 bytes (hex characters) to indicate the length and then the payload follows. So instead we could transmit "v" (that is not a hex character) followed by a variable length integer for the length and then the payload which has no upper bound.
Quoted 2 lines · In the release notes for 2.3 you wrote:
* "git push" and "git fetch" did not communicate an overlong refname correctly. Now it uses 64kB sideband to accommodate longer ones.
That could also make use of these "large packets" instead.
Thanks,
Stefan
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
Quoted 11 lines · Stefan Beller
Quoted 8 lines
Quoted 4 lines
Sorry to butt into the conversation late, but: I am not yet convinced.
Is the idea that if the push options were very large, this would save the client from the cost of sending them?
Not really. I have no strong opinion on the benefit of limiting number/size. Stefan limited the number/size at the receiving end and made receiving end die with its message.
Jeff claimed we'd need some sort of DoS protection for this feature, so I considered just die-ing enough for an initial implementation.
I do not think we need to worry too much about niceties for these limits. The point is to protect servers from malicious nonsense, like somebody sending gigabytes of push options, or trying to overflow a buffer in a hook with a large value. If people are seeing these in routine use, then the limits are set too low, and this should happen roughly as often as a BUG assertion, and IMHO should be treated roughly the same: don't bother with translation, and don't worry about optimizing wasted bandwidth for this case. It won't happen enough to matter.
-Peff
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
Quoted 11 lines · Jeff King
Quoted 9 lines · Stefan Beller
Quoted 8 lines
Quoted 4 lines
Sorry to butt into the conversation late, but: I am not yet convinced.
Is the idea that if the push options were very large, this would save the client from the cost of sending them?
Not really. I have no strong opinion on the benefit of limiting number/size. Stefan limited the number/size at the receiving end and made receiving end die with its message.
Quoted 7 lines
Quoted 2 lines
Jeff claimed we'd need some sort of DoS protection for this feature, so I considered just die-ing enough for an initial implementation.
I do not think we need to worry too much about niceties for these limits. The point is to protect servers from malicious nonsense, like somebody sending gigabytes of push options, or trying to overflow a buffer in a hook with a large value.
Agreed. This would speak for keeping the implementation as is.
Quoted 6 lines
If people are seeing these in
routine use, then the limits are set too low, and this should happen roughly as often as a BUG assertion, and IMHO should be treated roughly the same: don't bother with translation, and don't worry about optimizing wasted bandwidth for this case. It won't happen enough to matter.
Well the wasted band width is part of the server protection, no? This would favor the idea Jonathan came up with:
server: I advertise push options
client: ok I want to use push options
client: I'll send you 1000 push options with upper bound of 1000M
server: It's a bit too much, eh?
* server quits
So this case only occurs for the (malicious?) corner case, where I do not bother a translation.
But having the size announcement not in
the capability advertisement, but in the actual push options phase makes sense to me as we do not want to clutter the capabilities with data that can come later. We would only waste a little bit of band width, (the initial ls-remote
and command list of the client).
Speaking of this, I can craft a malicious client that sends the following command list
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
refs/heads/loooooooooooooooooong-ref
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
refs/heads/loooooooooooooooooong-ref
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
refs/heads/loooooooooooooooooong-ref
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
refs/heads/loooooooooooooooooong-ref
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
refs/heads/loooooooooooooooooong-ref
<repeat the above a few times>
0000
IIUC in the receive-pack code we would queue that up and the error checking (two times null sha1? update of the same ref more than once?), is done just after we send out the flush packet, i.e. when all commands are received.
This would also result in sending gigabytes of junk as well as a memory issue on the server
side?
The new push options design is actually neat in the way that the client exactly says what it wants
and the server can reject early, but not cluttering the capability advertisement.
Thanks,
Stefan
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
Quoted 8 lines · Stefan Beller
Quoted 6 lines
If people are seeing these in
routine use, then the limits are set too low, and this should happen roughly as often as a BUG assertion, and IMHO should be treated roughly the same: don't bother with translation, and don't worry about optimizing wasted bandwidth for this case. It won't happen enough to matter.Well the wasted band width is part of the server protection, no?
Not if you stop receiving as soon as you hit the limits. Then of course they can send up to the limit each time, but that is not a DoS. That is things working as advertised.
Quoted 10 lines
This would favor the idea Jonathan came up with:
server: I advertise push options
client: ok I want to use push options
client: I'll send you 1000 push options with upper bound of 1000M
server: It's a bit too much, eh?
* server quitsSo this case only occurs for the (malicious?) corner case, where I do not bother a translation.
In the malicious case, the client says "I'll send you 10 push option with an upper bound of 1024K", and then sends gigabytes anyway. Either way the server has to react to what is sent, not what is promised.
-Peff
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
Quoted 23 lines · Jeff King
Quoted 8 lines · Stefan Beller
Quoted 6 lines
If people are seeing these in
routine use, then the limits are set too low, and this should happen roughly as often as a BUG assertion, and IMHO should be treated roughly the same: don't bother with translation, and don't worry about optimizing wasted bandwidth for this case. It won't happen enough to matter.Well the wasted band width is part of the server protection, no?
Not if you stop receiving as soon as you hit the limits. Then of course they can send up to the limit each time, but that is not a DoS. That is things working as advertised.
Quoted 8 lines
This would favor the idea Jonathan came up with:
server: I advertise push options
client: ok I want to use push options
client: I'll send you 1000 push options with upper bound of 1000M
server: It's a bit too much, eh?
* server quits
Quoted 6 lines
Quoted 2 lines
So this case only occurs for the (malicious?) corner case, where I do not bother a translation.
In the malicious case, the client says "I'll send you 10 push option with an upper bound of 1024K", and then sends gigabytes anyway. Either way the server has to react to what is sent, not what is promised.
Well that is what the initial patch did via:
+ for (i = 0; i < max_options; i++) {
+ char *line;
+ int len;
+
+ line = packet_read_line(0, &len);
+
+ if (!line)
+ break;
+
+ if (len > max_size)
+ die("protocol error: server configuration allows push "
+ "options of size up to %d bytes", max_size);
+
+ len = strcspn(line, "\n");
+ line[len] = '\0';
+
+ string_list_append(ret, line);
+ }
+ if (i == max_options)
+ die("protocol error: server configuration only allows up "
+ "to %d push options", max_options);
I assume the die is an effective way to "stop receiving".
Thinking further about what you said, I think the initial selections of max_size and max_options is sufficient and we only see those bounds in the malicious case.
This discussion rather makes me wonder if we want to stick to the initial design as it was easy and not overcomplicating things as we assume the abort case doesn't occur often.
Quoted 2 lines
-Peff
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
Quoted 30 lines · Stefan Beller
Quoted 3 lines
In the malicious case, the client says "I'll send you 10 push option with an upper bound of 1024K", and then sends gigabytes anyway. Either way the server has to react to what is sent, not what is promised.
Well that is what the initial patch did via:
(quoted diff) +21
-0+ for (i = 0; i < max_options; i++) { + char *line; + int len; + + line = packet_read_line(0, &len); + + if (!line) + break; + + if (len > max_size) + die("protocol error: server configuration allows push " + "options of size up to %d bytes", max_size); + + len = strcspn(line, "\n"); + line[len] = '\0'; + + string_list_append(ret, line); + } + if (i == max_options) + die("protocol error: server configuration only allows up " + "to %d push options", max_options);I assume the die is an effective way to "stop receiving".
Quoted 7 lines
Thinking further about what you said, I think the initial selections of
max_sizeandmax_optionsis sufficient and we only see those bounds in the malicious case.This discussion rather makes me wonder if we want to stick to the initial design as it was easy and not overcomplicating things as we assume the abort case doesn't occur often.
Yes. I haven't been following the intermediate discussion and patches, but I don't see anything wrong with the general design above. I think you do need to use rp_error() to get the die message to the client for non-ssh cases, though (that is a problem with other protocol-error messages, too; I wonder if we should install a custom die handler, or convert them all to some kind of rp_die()).
-Peff
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
Quoted 7 lines · Jeff King
Yes. I haven't been following the intermediate discussion and patches, but I don't see anything wrong with the general design above. I think you do need to use
rp_error()to get the die message to the client for non-ssh cases, though (that is a problem with other protocol-error messages, too; I wonder if we should install a custom die handler, or convert them all to some kind ofrp_die()).
Some of the rp_error messages do not want to die(), but most seem to be ok when the rp_error would die.
I'll look into that.
Quoted 2 lines
-Peff
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
Quoted 11 lines · Stefan Beller
Quoted 7 lines · Jeff King
Yes. I haven't been following the intermediate discussion and patches, but I don't see anything wrong with the general design above. I think you do need to use
rp_error()to get the die message to the client for non-ssh cases, though (that is a problem with other protocol-error messages, too; I wonder if we should install a custom die handler, or convert them all to some kind ofrp_die()).Some of the
rp_errormessages do not want todie(), but most seem to be ok when therp_errorwould die.
Sorry, I meant converting die() into:
rp_error(...); die(...);
possibly via an rp_die() helper. The existing rp_error() cases would remain untouched.
Installing a die handler could make that work automatically, though I suspect it would lead to corner cases where we break protocol (e.g., if we die() in the middle of writing out a packet).
-Peff
Re: [PATCH 2/4] receive-pack: implement advertising and receiving push options
Quoted 7 lines · Jeff King
Sorry, I meant converting
die()into:rp_error(...); die(...);possibly via an
rp_die()helper. The existingrp_error()cases would remain untouched.
Oh I see!
That makes a lot of sense.
Re: [PATCH 3/4] push: accept push options
In reply to Junio C HamanoQuoted 11 lines · Junio C Hamano
Documentation/git-push.txt already exists on the remote side. · line 163
Quoted 5 lines · Stefan Beller
+-L:: +--push-option::Documentation/git-push.txt +3
-0+ Transmit the given string to the server, which passes them to + the pre-receive as well as the post-receive hook. Only C strings + containing no new lines are allowed.This is to affect what happens at the remote end, so I would have understood "-R". I also would have understood "-P" as a short-hand for "--push-option". What is the justification of "-L"?
It was made up. The actual code took -o for option. Changed that.
Quoted 9 lines
What does "C strings" mean? Did you mean to say "A sequence of bytes excluding NUL is passed verbatim"?
I do not think I saw anything in the code I reviewed so far that requires "no LF" limitation.
It is enforced server side, but an additional
client side enforcement may be better indeed.
The rationale for no enforcement on the client side is an easier way forward if we allow it on the server as the client would "just work" and it's up to the server to complain.
That makes me wonder if we want to document that, i.e.:
-o::
--push-option::
Transmit the given argument to the server, which passes them to the pre-receive as well as the post-receive hook. As it is up to the server to react on these push options, it may reject push options that contain new line or NUL characters. .
Quoted 6 lines
... Ahh, OK, you want to make sure that push-options are one-per-line in the push certificate. While I do not think it is absolutely necessary, starting with a possibly tighter than necessary limitation is much better than starting loose and having to tighten it later.
This is not what I had in mind, but rather the pain of dealing with multi line environment variables.
Quoted 7 lines
builtin/push.c static int do_push(const char *repo, int flags) · line 422
Quoted 4 lines
transport_get(remote, NULL);builtin/push.c +2
-1- +if (flags &TRANSPORT_PUSH_OPTIONS ) + transport->push_options = push_options;The result would be easier to read without the removal of a blank that separates decl/defn and stmt here.
ok
Quoted 6 lines
Quoted 1 line
+
OPT_STRING_LIST('o', "push-option", &push_options, N_("server-specific"), N_("option to transmit")),Here it seems to expect "-o". If we really want a short option, "-o" would probably be OK, as I do not think "git push" wants to have "send the output to this file" option.
Ok, will update the documentation.
Quoted 11 lines
by adding this between the two lines in the pre-context of this hunk, i.e.
if (push_cert_nonce[0]) strbuf_addf(&cert, "nonce %s\n", push_cert_nonce); if (args->push_options) for_each_string_list_item(item, args->push_options) strbuf_addf(&cert, "push-option %s\n", item->string); strbuf_addstr(&cert, "\n");
makes sense.
Re: [PATCH 3/4] push: accept push options
Quoted 2 lines · Stefan Beller
This is not what I had in mind, but rather the pain of dealing with multi line environment variables.
Hmph, is that painful?
$ cmd "$VAR"
inside double quotes would do multi-line just fine, I think.
But I think it is OK (and probably preferrable) to limit the options to be a collection of one-liners.