Skip to content

Commit ee27ca4

Browse files
peffgitster
authored andcommitted
archive: don't let remote clients get unreachable commits
Usually git is careful not to allow clients to fetch arbitrary objects from the database; for example, objects received via upload-pack must be reachable from a ref. Upload-archive breaks this by feeding the client's tree-ish directly to get_sha1, which will accept arbitrary hex sha1s, reflogs, etc. This is not a problem if all of your objects are publicly reachable anyway (or at least public to anybody who can run upload-archive). Or if you are making the repo available by dumb protocols like http or rsync (in which case the client can read your whole object db directly). But for sites which allow access only through smart protocols, clients may be able to fetch trees from commits that exist in the server's object database but are not referenced (e.g., because history was rewound). This patch tightens upload-archive's lookup to use dwim_ref rather than get_sha1. This means a remote client can only fetch the tip of a named ref, not an arbitrary sha1 or reflog entry. This also restricts some legitimate requests, too: 1. Reachable non-tip commits, like: git archive --remote=$url v1.0~5 2. Sub-trees of reachable commits, like: git archive --remote=$url v1.7.7:Documentation Local requests continue to use get_sha1, and are not restricted at all. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c503467 commit ee27ca4

5 files changed

+27
-8
lines changed

archive.c

+15-5
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ static void parse_pathspec_arg(const char **pathspec,
196196
}
197197

198198
static void parse_treeish_arg(const char **argv,
199-
struct archiver_args *ar_args, const char *prefix)
199+
struct archiver_args *ar_args, const char *prefix,
200+
int remote)
200201
{
201202
const char *name = argv[0];
202203
const unsigned char *commit_sha1;
@@ -205,8 +206,17 @@ static void parse_treeish_arg(const char **argv,
205206
const struct commit *commit;
206207
unsigned char sha1[20];
207208

208-
if (get_sha1(name, sha1))
209-
die("Not a valid object name");
209+
/* Remotes are only allowed to fetch actual refs */
210+
if (remote) {
211+
char *ref = NULL;
212+
if (!dwim_ref(name, strlen(name), sha1, &ref))
213+
die("no such ref: %s", name);
214+
free(ref);
215+
}
216+
else {
217+
if (get_sha1(name, sha1))
218+
die("Not a valid object name");
219+
}
210220

211221
commit = lookup_commit_reference_gently(sha1, 1);
212222
if (commit) {
@@ -324,7 +334,7 @@ static int parse_archive_args(int argc, const char **argv,
324334
}
325335

326336
int write_archive(int argc, const char **argv, const char *prefix,
327-
int setup_prefix)
337+
int setup_prefix, int remote)
328338
{
329339
const struct archiver *ar = NULL;
330340
struct archiver_args args;
@@ -333,7 +343,7 @@ int write_archive(int argc, const char **argv, const char *prefix,
333343
if (setup_prefix && prefix == NULL)
334344
prefix = setup_git_directory();
335345

336-
parse_treeish_arg(argv, &args, prefix);
346+
parse_treeish_arg(argv, &args, prefix, remote);
337347
parse_pathspec_arg(argv + 1, &args);
338348

339349
git_config(git_default_config, NULL);

archive.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern int write_tar_archive(struct archiver_args *);
2424
extern int write_zip_archive(struct archiver_args *);
2525

2626
extern int write_archive_entries(struct archiver_args *args, write_archive_entry_fn_t write_entry);
27-
extern int write_archive(int argc, const char **argv, const char *prefix, int setup_prefix);
27+
extern int write_archive(int argc, const char **argv, const char *prefix,
28+
int setup_prefix, int remote);
2829

2930
#endif /* ARCHIVE_H */

builtin-archive.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,5 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
117117

118118
setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
119119

120-
return write_archive(argc, argv, prefix, 1);
120+
return write_archive(argc, argv, prefix, 1, 0);
121121
}

builtin-upload-archive.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static int run_upload_archive(int argc, const char **argv, const char *prefix)
6464
sent_argv[sent_argc] = NULL;
6565

6666
/* parse all options sent by the client */
67-
return write_archive(sent_argc, sent_argv, prefix, 0);
67+
return write_archive(sent_argc, sent_argv, prefix, 0, 1);
6868
}
6969

7070
static void error_clnt(const char *fmt, ...)

t/t5000-tar-tree.sh

+8
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,12 @@ test_expect_success \
213213
'git archive --list outside of a git repo' \
214214
'GIT_DIR=some/non-existing/directory git archive --list'
215215

216+
test_expect_success 'clients cannot access unreachable commits' '
217+
test_commit unreachable &&
218+
sha1=`git rev-parse HEAD` &&
219+
git reset --hard HEAD^ &&
220+
git archive $sha1 >remote.tar &&
221+
test_must_fail git archive --remote=. $sha1 >remote.tar
222+
'
223+
216224
test_done

0 commit comments

Comments
 (0)