Skip to content

Commit 9609972

Browse files
rscharfegitster
authored andcommitted
archive: expand only a single %(describe) per archive
Every %(describe) placeholder in $Format:...$ strings in files with the attribute export-subst is expanded by calling git describe. This can potentially result in a lot of such calls per archive. That's OK for local repositories under control of the user of git archive, but could be a problem for hosted repositories. Expand only a single %(describe) placeholder per archive for now to avoid denial-of-service attacks. We can make this limit configurable later if needed, but let's start out simple. Reported-by: Jeff King <[email protected]> Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 273c990 commit 9609972

File tree

6 files changed

+41
-7
lines changed

6 files changed

+41
-7
lines changed

Documentation/gitattributes.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,8 @@ tag then no replacement will be done. The placeholders are the same
11741174
as those for the option `--pretty=format:` of linkgit:git-log[1],
11751175
except that they need to be wrapped like this: `$Format:PLACEHOLDERS$`
11761176
in the file. E.g. the string `$Format:%H$` will be replaced by the
1177-
commit hash.
1177+
commit hash. However, only one `%(describe)` placeholder is expanded
1178+
per archive to avoid denial-of-service attacks.
11781179

11791180

11801181
Packing objects

archive.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@ void init_archivers(void)
3737

3838
static void format_subst(const struct commit *commit,
3939
const char *src, size_t len,
40-
struct strbuf *buf)
40+
struct strbuf *buf, struct pretty_print_context *ctx)
4141
{
4242
char *to_free = NULL;
4343
struct strbuf fmt = STRBUF_INIT;
44-
struct pretty_print_context ctx = {0};
45-
ctx.date_mode.type = DATE_NORMAL;
46-
ctx.abbrev = DEFAULT_ABBREV;
4744

4845
if (src == buf->buf)
4946
to_free = strbuf_detach(buf, NULL);
@@ -61,7 +58,7 @@ static void format_subst(const struct commit *commit,
6158
strbuf_add(&fmt, b + 8, c - b - 8);
6259

6360
strbuf_add(buf, src, b - src);
64-
format_commit_message(commit, fmt.buf, buf, &ctx);
61+
format_commit_message(commit, fmt.buf, buf, ctx);
6562
len -= c + 1 - src;
6663
src = c + 1;
6764
}
@@ -94,7 +91,7 @@ static void *object_file_to_archive(const struct archiver_args *args,
9491
strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
9592
convert_to_working_tree(args->repo->index, path, buf.buf, buf.len, &buf, &meta);
9693
if (commit)
97-
format_subst(commit, buf.buf, buf.len, &buf);
94+
format_subst(commit, buf.buf, buf.len, &buf, args->pretty_ctx);
9895
buffer = strbuf_detach(&buf, &size);
9996
*sizep = size;
10097
}
@@ -633,12 +630,19 @@ int write_archive(int argc, const char **argv, const char *prefix,
633630
const char *name_hint, int remote)
634631
{
635632
const struct archiver *ar = NULL;
633+
struct pretty_print_describe_status describe_status = {0};
634+
struct pretty_print_context ctx = {0};
636635
struct archiver_args args;
637636
int rc;
638637

639638
git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable);
640639
git_config(git_default_config, NULL);
641640

641+
describe_status.max_invocations = 1;
642+
ctx.date_mode.type = DATE_NORMAL;
643+
ctx.abbrev = DEFAULT_ABBREV;
644+
ctx.describe_status = &describe_status;
645+
args.pretty_ctx = &ctx;
642646
args.repo = repo;
643647
args.prefix = prefix;
644648
string_list_init(&args.extra_files, 1);

archive.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "pathspec.h"
66

77
struct repository;
8+
struct pretty_print_context;
89

910
struct archiver_args {
1011
struct repository *repo;
@@ -22,6 +23,7 @@ struct archiver_args {
2223
unsigned int convert : 1;
2324
int compression_level;
2425
struct string_list extra_files;
26+
struct pretty_print_context *pretty_ctx;
2527
};
2628

2729
/* main api */

pretty.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,14 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
12471247
struct child_process cmd = CHILD_PROCESS_INIT;
12481248
struct strbuf out = STRBUF_INIT;
12491249
struct strbuf err = STRBUF_INIT;
1250+
struct pretty_print_describe_status *describe_status;
1251+
1252+
describe_status = c->pretty_ctx->describe_status;
1253+
if (describe_status) {
1254+
if (!describe_status->max_invocations)
1255+
return 0;
1256+
describe_status->max_invocations--;
1257+
}
12501258

12511259
cmd.git_cmd = 1;
12521260
strvec_push(&cmd.args, "describe");

pretty.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ enum cmit_fmt {
2323
CMIT_FMT_UNSPECIFIED
2424
};
2525

26+
struct pretty_print_describe_status {
27+
unsigned int max_invocations;
28+
};
29+
2630
struct pretty_print_context {
2731
/*
2832
* Callers should tweak these to change the behavior of pp_* functions.
@@ -44,6 +48,7 @@ struct pretty_print_context {
4448
int color;
4549
struct ident_split *from_ident;
4650
unsigned encode_email_headers:1;
51+
struct pretty_print_describe_status *describe_status;
4752

4853
/*
4954
* Fields below here are manipulated internally by pp_* functions and

t/t5001-archive-attr.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,18 @@ test_expect_success 'export-subst' '
128128
test_cmp substfile2 archive/substfile2
129129
'
130130

131+
test_expect_success 'export-subst expands %(describe) once' '
132+
echo "\$Format:%(describe)\$" >substfile3 &&
133+
echo "\$Format:%(describe)\$" >>substfile3 &&
134+
echo "\$Format:%(describe)${LF}%(describe)\$" >substfile4 &&
135+
git add substfile[34] &&
136+
git commit -m export-subst-describe &&
137+
git tag -m export-subst-describe export-subst-describe &&
138+
git archive HEAD >archive-describe.tar &&
139+
extract_tar_to_dir archive-describe &&
140+
desc=$(git describe) &&
141+
grep -F "$desc" archive-describe/substfile[34] >substituted &&
142+
test_line_count = 1 substituted
143+
'
144+
131145
test_done

0 commit comments

Comments
 (0)