archive: reject remote --add-file in callback
PATCH 1/1 archive: reject remote --add-file in callback
parse_archive_args() only checks "is_remote && args->extra_files.nr" after parse_options() returns. When git-upload-archive parses client arguments, add_file_cb() runs first and calls stat() on the requested path on the server filesystem, reporting "File not found" or "Not a regular file" before the "--remote" incompatibility check is reached (and succeeding altogether if "--no-add-file" follows).
Pass is_remote into add_file_cb() and reject "--add-file" and "--add-virtual-file" immediately before touching the filesystem.
--- archive.c | 31 ++++++++++++++++++++++--------- t/t5000-tar-tree.sh | 9 +++++++++ 2 files changed, 31 insertions(+), 9 deletions(-)
archive.c +22 -9
@@ -559,15 +559,27 @@ static void extra_file_info_clear(void *util, const char *str UNUSED)free (info); } +struct add_file_data { +struct archiver_args *args; +const char **basep; +int is_remote; +}; +static int add_file_cb (const struct option *opt,const char *arg,int unset) { -struct archiver_args *args = opt->value; -const char **basep = (const char **)opt->defval; -const char *base = *basep; +struct add_file_data *data = opt->value; +struct archiver_args *args = data->args; +const char *base = *data->basep;char *path;struct string_list_item *item;struct extra_file_info *info; +if (data->is_remote) +die (_ ("options '%s' and '%s' cannot be used together" ), + !strcmp (opt->long_name,"add-virtual-file" ) ? +"--add-virtual-file" :"--add-file" , +"--remote" ); +if (unset) {string_list_clear_func (&args->extra_files, extra_file_info_clear);
@@ -645,6 +657,11 @@ static int parse_archive_args(int argc, const char **argv,int i;int list =0 ;int worktree_attributes =0 ; +struct add_file_data add_file_data = { + .args = args, + .basep = &base, + .is_remote = is_remote, + };struct option opts[] = {OPT_GROUP ("" ),OPT_STRING (0 ,"format" , &format,N_ ("fmt" ),N_ ("archive format" )),
@@ -653,20 +670,18 @@ static int parse_archive_args(int argc, const char **argv, { .type = OPTION_CALLBACK, .long_name = "add-file", - .value = args, + .value = &add_file_data, .argh = N_("file"), .help = N_("add untracked file to archive"), .callback = add_file_cb, - .defval = (intptr_t) &base, }, { .type = OPTION_CALLBACK, .long_name = "add-virtual-file", - .value = args, + .value = &add_file_data, .argh = N_("path:content"), .help = N_("add untracked file to archive"), .callback = add_file_cb, - .defval = (intptr_t) &base, }, OPT_STRING('o', "output", &output, N_("file"), N_("write the archive to this file")),
@@ -702,8 +717,6 @@ static int parse_archive_args(int argc, const char **argv,die (_ ("the option '%s' requires '%s'" ),"--exec" ,"--remote" );if (output)die (_ ("Unexpected option --output" )); -if (is_remote && args->extra_files.nr) -die (_ ("options '%s' and '%s' cannot be used together" ),"--add-file" ,"--remote" );if (!base) base ="" ;
t/t5000-tar-tree.sh +9 -0
@@ -237,6 +237,15 @@ check_tar with_untracked2 check_added with_untracked2 untracked one/untracked check_added with_untracked2 untracked two/untracked +test_expect_success 'git archive --remote rejects --add-file before stat' ' + test_must_fail git archive --remote=. --add-file=no-such-file HEAD 2>err && + test_grep "cannot be used together" err && + test_grep ! "File not found" err && + echo untracked >untracked && + test_must_fail git archive --remote=. --add-file=untracked --no-add-file HEAD 2>err && + test_grep "cannot be used together" err +' + test_expect_success 'git archive on large files' ' test_config core.bigfilethreshold 1 && git archive HEAD >b3.tar &&
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Quoted 6 lines · Dominik Maier
parse_archive_args()only checks "is_remote&&args->extra_files.nr" afterparse_options()returns. When git-upload-archive parses client arguments,add_file_cb()runs first and callsstat()on the requested path on the server filesystem, reporting "File not found" or "Not a regular file" before the "--remote" incompatibility check is reached (and succeeding altogether if "--no-add-file" follows).
Ugh, good find.
Quoted 2 lines
Pass
is_remoteintoadd_file_cb()and reject "--add-file" and "--add-virtual-file" immediately before touching the filesystem.
The current code rejects the combination of --add-virtual-file and --remote, but that's arguably also a bug -- there's no reason to disallow it, since the option's value brings file name and content to the remote end.
Quoted 27 lines
Signed-off-by Dominik Maier <[email protected]>--- archive.c | 31 ++++++++++++++++++++++--------- t/t5000-tar-tree.sh | 9 +++++++++ 2 files changed, 31 insertions(+), 9 deletions(-)archive.c +8
-3@@ -559,15 +559,27 @@ static void extra_file_info_clear(void *util, const char *str UNUSED)free (info); } +struct add_file_data { +struct archiver_args *args; +const char **basep; +int is_remote; +}; +static int add_file_cb (const struct option *opt,const char *arg,int unset) { -struct archiver_args *args = opt->value; -const char **basep = (const char **)opt->defval; -const char *base = *basep; +struct add_file_data *data = opt->value; +struct archiver_args *args = data->args;
archive.c static void extra_file_info_clear(void *util, const char *str UNUSED) · line 581
Quoted 10 lines
archive.c +6
-0+const char *base = *data->basep;char *path;struct string_list_item *item;struct extra_file_info *info; +if (data->is_remote) +die (_ ("options '%s' and '%s' cannot be used together" ), + !strcmp (opt->long_name,"add-virtual-file" ) ? +"--add-virtual-file" :"--add-file" , +"--remote" );
That also rejects --remote --add-file=foo --no-add-file, which we could allow.
Quoted 40 lines
archive.c +8
-4+if (unset) {string_list_clear_func (&args->extra_files, extra_file_info_clear);@@ -645,6 +657,11 @@ static int parse_archive_args(int argc, const char **argv,int i;int list =0 ;int worktree_attributes =0 ; +struct add_file_data add_file_data = { + .args = args, + .basep = &base, + .is_remote = is_remote, + };struct option opts[] = {OPT_GROUP ("" ),OPT_STRING (0 ,"format" , &format,N_ ("fmt" ),N_ ("archive format" )),@@ -653,20 +670,18 @@ static int parse_archive_args(int argc, const char **argv, { .type = OPTION_CALLBACK, .long_name = "add-file", - .value = args, + .value = &add_file_data, .argh = N_("file"), .help = N_("add untracked file to archive"), .callback = add_file_cb, - .defval = (intptr_t) &base, }, { .type = OPTION_CALLBACK, .long_name = "add-virtual-file", - .value = args, + .value = &add_file_data, .argh = N_("path:content"), .help = N_("add untracked file to archive"), .callback = add_file_cb, - .defval = (intptr_t) &base, }, OPT_STRING('o', "output", &output, N_("file"), N_("write the archive to this file")),
archive.c static int parse_archive_args(int argc, const char **argv, · line 706
Quoted 5 lines
archive.c +0
-2@@ -702,8 +717,6 @@ static int parse_archive_args(int argc, const char **argv,die (_ ("the option '%s' requires '%s'" ),"--exec" ,"--remote" );if (output)die (_ ("Unexpected option --output" )); -if (is_remote && args->extra_files.nr) -die (_ ("options '%s' and '%s' cannot be used together" ),"--add-file" ,"--remote" );
How about moving the stat(2) call here instead? It would still happen before we start writing archive content, but after applying any --no-add-file. We can refuse to do it if is_remote is set. The files added using --add-file have content == NULL.
Quoted 23 lines
archive.c +0
-0if (!base) base ="" ;t/t5000-tar-tree.sh +9
-0@@ -237,6 +237,15 @@ check_tar with_untracked2 check_added with_untracked2 untracked one/untracked check_added with_untracked2 untracked two/untracked +test_expect_success 'git archive --remote rejects --add-file before stat' ' + test_must_fail git archive --remote=. --add-file=no-such-file HEAD 2>err && + test_grep "cannot be used together" err && + test_grep ! "File not found" err && + echo untracked >untracked && + test_must_fail git archive --remote=. --add-file=untracked --no-add-file HEAD 2>err && + test_grep "cannot be used together" err +' + test_expect_success 'git archive on large files' ' test_config core.bigfilethreshold 1 && git archive HEAD >b3.tar &&