Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit ce735bf

Browse files
committed
Merge branch 'jc/hidden-refs'
Allow the server side to redact the refs/ namespace it shows to the client. Will merge to 'master'. * jc/hidden-refs: upload/receive-pack: allow hiding ref hierarchies upload-pack: simplify request validation upload-pack: share more code
2 parents abea4dc + daebaa7 commit ce735bf

File tree

7 files changed

+166
-30
lines changed

7 files changed

+166
-30
lines changed

Documentation/config.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,15 @@ receive.denyNonFastForwards::
18801880
even if that push is forced. This configuration variable is
18811881
set when initializing a shared repository.
18821882

1883+
receive.hiderefs::
1884+
String(s) `receive-pack` uses to decide which refs to omit
1885+
from its initial advertisement. Use more than one
1886+
definitions to specify multiple prefix strings. A ref that
1887+
are under the hierarchies listed on the value of this
1888+
variable is excluded, and is hidden when responding to `git
1889+
push`, and an attempt to update or delete a hidden ref by
1890+
`git push` is rejected.
1891+
18831892
receive.updateserverinfo::
18841893
If set to true, git-receive-pack will run git-update-server-info
18851894
after receiving data from git-push and updating refs.
@@ -2092,11 +2101,25 @@ transfer.fsckObjects::
20922101
not set, the value of this variable is used instead.
20932102
Defaults to false.
20942103

2104+
transfer.hiderefs::
2105+
This variable can be used to set both `receive.hiderefs`
2106+
and `uploadpack.hiderefs` at the same time to the same
2107+
values. See entries for these other variables.
2108+
20952109
transfer.unpackLimit::
20962110
When `fetch.unpackLimit` or `receive.unpackLimit` are
20972111
not set, the value of this variable is used instead.
20982112
The default value is 100.
20992113

2114+
uploadpack.hiderefs::
2115+
String(s) `upload-pack` uses to decide which refs to omit
2116+
from its initial advertisement. Use more than one
2117+
definitions to specify multiple prefix strings. A ref that
2118+
are under the hierarchies listed on the value of this
2119+
variable is excluded, and is hidden from `git ls-remote`,
2120+
`git fetch`, etc. An attempt to fetch a hidden ref by `git
2121+
fetch` will fail.
2122+
21002123
url.<base>.insteadOf::
21012124
Any URL that starts with this value will be rewritten to
21022125
start, instead, with <base>. In cases where some site serves a

builtin/receive-pack.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ static enum deny_action parse_deny_action(const char *var, const char *value)
5959

6060
static int receive_pack_config(const char *var, const char *value, void *cb)
6161
{
62+
int status = parse_hide_refs_config(var, value, "receive");
63+
64+
if (status)
65+
return status;
66+
6267
if (strcmp(var, "receive.denydeletes") == 0) {
6368
deny_deletes = git_config_bool(var, value);
6469
return 0;
@@ -119,6 +124,9 @@ static int receive_pack_config(const char *var, const char *value, void *cb)
119124

120125
static void show_ref(const char *path, const unsigned char *sha1)
121126
{
127+
if (ref_is_hidden(path))
128+
return;
129+
122130
if (sent_capabilities)
123131
packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
124132
else
@@ -685,6 +693,20 @@ static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
685693
return -1; /* end of list */
686694
}
687695

696+
static void reject_updates_to_hidden(struct command *commands)
697+
{
698+
struct command *cmd;
699+
700+
for (cmd = commands; cmd; cmd = cmd->next) {
701+
if (cmd->error_string || !ref_is_hidden(cmd->ref_name))
702+
continue;
703+
if (is_null_sha1(cmd->new_sha1))
704+
cmd->error_string = "deny deleting a hidden ref";
705+
else
706+
cmd->error_string = "deny updating a hidden ref";
707+
}
708+
}
709+
688710
static void execute_commands(struct command *commands, const char *unpacker_error)
689711
{
690712
struct command *cmd;
@@ -701,6 +723,8 @@ static void execute_commands(struct command *commands, const char *unpacker_erro
701723
0, &cmd))
702724
set_connectivity_errors(commands);
703725

726+
reject_updates_to_hidden(commands);
727+
704728
if (run_receive_hook(commands, "pre-receive", 0)) {
705729
for (cmd = commands; cmd; cmd = cmd->next) {
706730
if (!cmd->error_string)

refs.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "object.h"
44
#include "tag.h"
55
#include "dir.h"
6+
#include "string-list.h"
67

78
/*
89
* Make sure "ref" is something reasonable to have under ".git/refs/";
@@ -2554,3 +2555,46 @@ char *shorten_unambiguous_ref(const char *refname, int strict)
25542555
free(short_name);
25552556
return xstrdup(refname);
25562557
}
2558+
2559+
static struct string_list *hide_refs;
2560+
2561+
int parse_hide_refs_config(const char *var, const char *value, const char *section)
2562+
{
2563+
if (!strcmp("transfer.hiderefs", var) ||
2564+
/* NEEDSWORK: use parse_config_key() once both are merged */
2565+
(!prefixcmp(var, section) && var[strlen(section)] == '.' &&
2566+
!strcmp(var + strlen(section), ".hiderefs"))) {
2567+
char *ref;
2568+
int len;
2569+
2570+
if (!value)
2571+
return config_error_nonbool(var);
2572+
ref = xstrdup(value);
2573+
len = strlen(ref);
2574+
while (len && ref[len - 1] == '/')
2575+
ref[--len] = '\0';
2576+
if (!hide_refs) {
2577+
hide_refs = xcalloc(1, sizeof(*hide_refs));
2578+
hide_refs->strdup_strings = 1;
2579+
}
2580+
string_list_append(hide_refs, ref);
2581+
}
2582+
return 0;
2583+
}
2584+
2585+
int ref_is_hidden(const char *refname)
2586+
{
2587+
struct string_list_item *item;
2588+
2589+
if (!hide_refs)
2590+
return 0;
2591+
for_each_string_list_item(item, hide_refs) {
2592+
int len;
2593+
if (prefixcmp(refname, item->string))
2594+
continue;
2595+
len = strlen(item->string);
2596+
if (!refname[len] || refname[len] == '/')
2597+
return 1;
2598+
}
2599+
return 0;
2600+
}

refs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,7 @@ int update_ref(const char *action, const char *refname,
147147
const unsigned char *sha1, const unsigned char *oldval,
148148
int flags, enum action_on_err onerr);
149149

150+
extern int parse_hide_refs_config(const char *var, const char *value, const char *);
151+
extern int ref_is_hidden(const char *);
152+
150153
#endif /* REFS_H */

t/t5512-ls-remote.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,16 @@ test_expect_success 'Report match with --exit-code' '
126126
test_cmp expect actual
127127
'
128128

129+
for configsection in transfer uploadpack
130+
do
131+
test_expect_success "Hide some refs with $configsection.hiderefs" '
132+
test_config $configsection.hiderefs refs/tags &&
133+
git ls-remote . >actual &&
134+
test_unconfig $configsection.hiderefs &&
135+
git ls-remote . |
136+
sed -e "/ refs\/tags\//d" >expect &&
137+
test_cmp expect actual
138+
'
139+
done
140+
129141
test_done

t/t5516-fetch-push.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,4 +1016,31 @@ test_expect_success 'push --prune refspec' '
10161016
! check_push_result $the_first_commit tmp/foo tmp/bar
10171017
'
10181018

1019+
for configsection in transfer receive
1020+
do
1021+
test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
1022+
mk_test heads/master hidden/one hidden/two hidden/three &&
1023+
(
1024+
cd testrepo &&
1025+
git config $configsection.hiderefs refs/hidden
1026+
) &&
1027+
1028+
# push to unhidden ref succeeds normally
1029+
git push testrepo master:refs/heads/master &&
1030+
check_push_result $the_commit heads/master &&
1031+
1032+
# push to update a hidden ref should fail
1033+
test_must_fail git push testrepo master:refs/hidden/one &&
1034+
check_push_result $the_first_commit hidden/one &&
1035+
1036+
# push to delete a hidden ref should fail
1037+
test_must_fail git push testrepo :refs/hidden/two &&
1038+
check_push_result $the_first_commit hidden/two &&
1039+
1040+
# idempotent push to update a hidden ref should fail
1041+
test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
1042+
check_push_result $the_first_commit hidden/three
1043+
'
1044+
done
1045+
10191046
test_done

upload-pack.c

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "run-command.h"
1313
#include "sigchain.h"
1414
#include "version.h"
15+
#include "string-list.h"
1516

1617
static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
1718

@@ -28,7 +29,7 @@ static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<
2829

2930
static unsigned long oldest_have;
3031

31-
static int multi_ack, nr_our_refs;
32+
static int multi_ack;
3233
static int no_done;
3334
static int use_thin_pack, use_ofs_delta, use_include_tag;
3435
static int no_progress, daemon_mode;
@@ -139,7 +140,6 @@ static void create_pack_file(void)
139140
{
140141
struct async rev_list;
141142
struct child_process pack_objects;
142-
int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
143143
char data[8193], progress[128];
144144
char abort_msg[] = "aborting due to possible repository "
145145
"corruption on the remote side.";
@@ -151,9 +151,7 @@ static void create_pack_file(void)
151151
argv[arg++] = "pack-objects";
152152
if (!shallow_nr) {
153153
argv[arg++] = "--revs";
154-
if (create_full_pack)
155-
argv[arg++] = "--all";
156-
else if (use_thin_pack)
154+
if (use_thin_pack)
157155
argv[arg++] = "--thin";
158156
}
159157

@@ -185,15 +183,15 @@ static void create_pack_file(void)
185183
}
186184
else {
187185
FILE *pipe_fd = xfdopen(pack_objects.in, "w");
188-
if (!create_full_pack) {
189-
int i;
190-
for (i = 0; i < want_obj.nr; i++)
191-
fprintf(pipe_fd, "%s\n", sha1_to_hex(want_obj.objects[i].item->sha1));
192-
fprintf(pipe_fd, "--not\n");
193-
for (i = 0; i < have_obj.nr; i++)
194-
fprintf(pipe_fd, "%s\n", sha1_to_hex(have_obj.objects[i].item->sha1));
195-
}
186+
int i;
196187

188+
for (i = 0; i < want_obj.nr; i++)
189+
fprintf(pipe_fd, "%s\n",
190+
sha1_to_hex(want_obj.objects[i].item->sha1));
191+
fprintf(pipe_fd, "--not\n");
192+
for (i = 0; i < have_obj.nr; i++)
193+
fprintf(pipe_fd, "%s\n",
194+
sha1_to_hex(have_obj.objects[i].item->sha1));
197195
fprintf(pipe_fd, "\n");
198196
fflush(pipe_fd);
199197
fclose(pipe_fd);
@@ -729,15 +727,30 @@ static void receive_needs(void)
729727
free(shallows.objects);
730728
}
731729

730+
/* return non-zero if the ref is hidden, otherwise 0 */
731+
static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
732+
{
733+
struct object *o = lookup_unknown_object(sha1);
734+
735+
if (ref_is_hidden(refname))
736+
return 1;
737+
if (!o)
738+
die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
739+
o->flags |= OUR_REF;
740+
return 0;
741+
}
742+
732743
static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
733744
{
734745
static const char *capabilities = "multi_ack thin-pack side-band"
735746
" side-band-64k ofs-delta shallow no-progress"
736747
" include-tag multi_ack_detailed";
737-
struct object *o = lookup_unknown_object(sha1);
738748
const char *refname_nons = strip_namespace(refname);
739749
unsigned char peeled[20];
740750

751+
if (mark_our_ref(refname, sha1, flag, cb_data))
752+
return 0;
753+
741754
if (capabilities)
742755
packet_write(1, "%s %s%c%s%s agent=%s\n",
743756
sha1_to_hex(sha1), refname_nons,
@@ -747,27 +760,11 @@ static int send_ref(const char *refname, const unsigned char *sha1, int flag, vo
747760
else
748761
packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
749762
capabilities = NULL;
750-
if (!(o->flags & OUR_REF)) {
751-
o->flags |= OUR_REF;
752-
nr_our_refs++;
753-
}
754763
if (!peel_ref(refname, peeled))
755764
packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
756765
return 0;
757766
}
758767

759-
static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
760-
{
761-
struct object *o = parse_object(sha1);
762-
if (!o)
763-
die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
764-
if (!(o->flags & OUR_REF)) {
765-
o->flags |= OUR_REF;
766-
nr_our_refs++;
767-
}
768-
return 0;
769-
}
770-
771768
static void upload_pack(void)
772769
{
773770
if (advertise_refs || !stateless_rpc) {
@@ -789,6 +786,11 @@ static void upload_pack(void)
789786
}
790787
}
791788

789+
static int upload_pack_config(const char *var, const char *value, void *unused)
790+
{
791+
return parse_hide_refs_config(var, value, "uploadpack");
792+
}
793+
792794
int main(int argc, char **argv)
793795
{
794796
char *dir;
@@ -840,6 +842,7 @@ int main(int argc, char **argv)
840842
die("'%s' does not appear to be a git repository", dir);
841843
if (is_repository_shallow())
842844
die("attempt to fetch/clone from a shallow repository");
845+
git_config(upload_pack_config, NULL);
843846
if (getenv("GIT_DEBUG_SEND_PACK"))
844847
debug_fd = atoi(getenv("GIT_DEBUG_SEND_PACK"));
845848
upload_pack();

0 commit comments

Comments
 (0)