2 #include "object-store.h"
3 #include "repository.h"
17 #include "submodule-config.h"
19 #include "credential.h"
22 static struct oidset gitmodules_found = OIDSET_INIT;
23 static struct oidset gitmodules_done = OIDSET_INIT;
25 #define FOREACH_MSG_ID(FUNC) \
27 FUNC(NUL_IN_HEADER, FATAL) \
28 FUNC(UNTERMINATED_HEADER, FATAL) \
30 FUNC(BAD_DATE, ERROR) \
31 FUNC(BAD_DATE_OVERFLOW, ERROR) \
32 FUNC(BAD_EMAIL, ERROR) \
33 FUNC(BAD_NAME, ERROR) \
34 FUNC(BAD_OBJECT_SHA1, ERROR) \
35 FUNC(BAD_PARENT_SHA1, ERROR) \
36 FUNC(BAD_TAG_OBJECT, ERROR) \
37 FUNC(BAD_TIMEZONE, ERROR) \
38 FUNC(BAD_TREE, ERROR) \
39 FUNC(BAD_TREE_SHA1, ERROR) \
40 FUNC(BAD_TYPE, ERROR) \
41 FUNC(DUPLICATE_ENTRIES, ERROR) \
42 FUNC(MISSING_AUTHOR, ERROR) \
43 FUNC(MISSING_COMMITTER, ERROR) \
44 FUNC(MISSING_EMAIL, ERROR) \
45 FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
46 FUNC(MISSING_OBJECT, ERROR) \
47 FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \
48 FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \
49 FUNC(MISSING_TAG, ERROR) \
50 FUNC(MISSING_TAG_ENTRY, ERROR) \
51 FUNC(MISSING_TREE, ERROR) \
52 FUNC(MISSING_TREE_OBJECT, ERROR) \
53 FUNC(MISSING_TYPE, ERROR) \
54 FUNC(MISSING_TYPE_ENTRY, ERROR) \
55 FUNC(MULTIPLE_AUTHORS, ERROR) \
56 FUNC(TREE_NOT_SORTED, ERROR) \
57 FUNC(UNKNOWN_TYPE, ERROR) \
58 FUNC(ZERO_PADDED_DATE, ERROR) \
59 FUNC(GITMODULES_MISSING, ERROR) \
60 FUNC(GITMODULES_BLOB, ERROR) \
61 FUNC(GITMODULES_LARGE, ERROR) \
62 FUNC(GITMODULES_NAME, ERROR) \
63 FUNC(GITMODULES_SYMLINK, ERROR) \
64 FUNC(GITMODULES_URL, ERROR) \
65 FUNC(GITMODULES_PATH, ERROR) \
66 FUNC(GITMODULES_UPDATE, ERROR) \
68 FUNC(BAD_FILEMODE, WARN) \
69 FUNC(EMPTY_NAME, WARN) \
70 FUNC(FULL_PATHNAME, WARN) \
72 FUNC(HAS_DOTDOT, WARN) \
73 FUNC(HAS_DOTGIT, WARN) \
74 FUNC(NULL_SHA1, WARN) \
75 FUNC(ZERO_PADDED_FILEMODE, WARN) \
76 FUNC(NUL_IN_COMMIT, WARN) \
77 /* infos (reported as warnings, but ignored by default) */ \
78 FUNC(GITMODULES_PARSE, INFO) \
79 FUNC(BAD_TAG_NAME, INFO) \
80 FUNC(MISSING_TAGGER_ENTRY, INFO) \
81 /* ignored (elevated when requested) */ \
82 FUNC(EXTRA_HEADER_ENTRY, IGNORE)
84 #define MSG_ID(id, msg_type) FSCK_MSG_##id,
86 FOREACH_MSG_ID(MSG_ID)
92 #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
94 const char *id_string;
95 const char *downcased;
96 const char *camelcased;
97 enum fsck_msg_type msg_type;
98 } msg_id_info[FSCK_MSG_MAX + 1] = {
99 FOREACH_MSG_ID(MSG_ID)
100 { NULL, NULL, NULL, -1 }
104 static void prepare_msg_ids(void)
108 if (msg_id_info[0].downcased)
111 /* convert id_string to lower case, without underscores. */
112 for (i = 0; i < FSCK_MSG_MAX; i++) {
113 const char *p = msg_id_info[i].id_string;
115 char *q = xmalloc(len);
117 msg_id_info[i].downcased = q;
122 *(q)++ = tolower(*(p)++);
125 p = msg_id_info[i].id_string;
127 msg_id_info[i].camelcased = q;
134 *q++ = tolower(*p++);
141 static int parse_msg_id(const char *text)
147 for (i = 0; i < FSCK_MSG_MAX; i++)
148 if (!strcmp(text, msg_id_info[i].downcased))
154 void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
160 for (i = 0; i < FSCK_MSG_MAX; i++)
161 list_config_item(list, prefix, msg_id_info[i].camelcased);
164 static enum fsck_msg_type fsck_msg_type(enum fsck_msg_id msg_id,
165 struct fsck_options *options)
167 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
169 if (!options->msg_type) {
170 enum fsck_msg_type msg_type = msg_id_info[msg_id].msg_type;
172 if (options->strict && msg_type == FSCK_WARN)
173 msg_type = FSCK_ERROR;
177 return options->msg_type[msg_id];
180 static enum fsck_msg_type parse_msg_type(const char *str)
182 if (!strcmp(str, "error"))
184 else if (!strcmp(str, "warn"))
186 else if (!strcmp(str, "ignore"))
189 die("Unknown fsck message type: '%s'", str);
192 int is_valid_msg_type(const char *msg_id, const char *msg_type)
194 if (parse_msg_id(msg_id) < 0)
196 parse_msg_type(msg_type);
200 void fsck_set_msg_type(struct fsck_options *options,
201 const char *msg_id_str, const char *msg_type_str)
203 int msg_id = parse_msg_id(msg_id_str);
204 enum fsck_msg_type msg_type;
207 die("Unhandled message id: %s", msg_id_str);
208 msg_type = parse_msg_type(msg_type_str);
210 if (msg_type != FSCK_ERROR && msg_id_info[msg_id].msg_type == FSCK_FATAL)
211 die("Cannot demote %s to %s", msg_id_str, msg_type_str);
213 if (!options->msg_type) {
215 enum fsck_msg_type *severity;
216 ALLOC_ARRAY(severity, FSCK_MSG_MAX);
217 for (i = 0; i < FSCK_MSG_MAX; i++)
218 severity[i] = fsck_msg_type(i, options);
219 options->msg_type = severity;
222 options->msg_type[msg_id] = msg_type;
225 void fsck_set_msg_types(struct fsck_options *options, const char *values)
227 char *buf = xstrdup(values), *to_free = buf;
231 int len = strcspn(buf, " ,|"), equal;
241 equal < len && buf[equal] != '=' && buf[equal] != ':';
243 buf[equal] = tolower(buf[equal]);
246 if (!strcmp(buf, "skiplist")) {
248 die("skiplist requires a path");
249 oidset_parse_file(&options->skiplist, buf + equal + 1);
255 die("Missing '=': '%s'", buf);
257 fsck_set_msg_type(options, buf, buf + equal + 1);
263 static int object_on_skiplist(struct fsck_options *opts,
264 const struct object_id *oid)
266 return opts && oid && oidset_contains(&opts->skiplist, oid);
269 __attribute__((format (printf, 5, 6)))
270 static int report(struct fsck_options *options,
271 const struct object_id *oid, enum object_type object_type,
272 enum fsck_msg_id msg_id, const char *fmt, ...)
275 struct strbuf sb = STRBUF_INIT;
276 enum fsck_msg_type msg_type = fsck_msg_type(msg_id, options);
279 if (msg_type == FSCK_IGNORE)
282 if (object_on_skiplist(options, oid))
285 if (msg_type == FSCK_FATAL)
286 msg_type = FSCK_ERROR;
287 else if (msg_type == FSCK_INFO)
288 msg_type = FSCK_WARN;
291 strbuf_addf(&sb, "%s: ", msg_id_info[msg_id].camelcased);
294 strbuf_vaddf(&sb, fmt, ap);
295 result = options->error_func(options, oid, object_type,
303 void fsck_enable_object_names(struct fsck_options *options)
305 if (!options->object_names)
306 options->object_names = kh_init_oid_map();
309 const char *fsck_get_object_name(struct fsck_options *options,
310 const struct object_id *oid)
313 if (!options->object_names)
315 pos = kh_get_oid_map(options->object_names, *oid);
316 if (pos >= kh_end(options->object_names))
318 return kh_value(options->object_names, pos);
321 void fsck_put_object_name(struct fsck_options *options,
322 const struct object_id *oid,
323 const char *fmt, ...)
326 struct strbuf buf = STRBUF_INIT;
330 if (!options->object_names)
333 pos = kh_put_oid_map(options->object_names, *oid, &hashret);
337 strbuf_vaddf(&buf, fmt, ap);
338 kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL);
342 const char *fsck_describe_object(struct fsck_options *options,
343 const struct object_id *oid)
345 static struct strbuf bufs[] = {
346 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
350 const char *name = fsck_get_object_name(options, oid);
353 b = (b + 1) % ARRAY_SIZE(bufs);
355 strbuf_addstr(buf, oid_to_hex(oid));
357 strbuf_addf(buf, " (%s)", name);
362 static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
364 struct tree_desc desc;
365 struct name_entry entry;
369 if (parse_tree(tree))
372 name = fsck_get_object_name(options, &tree->object.oid);
373 if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
375 while (tree_entry_gently(&desc, &entry)) {
379 if (S_ISGITLINK(entry.mode))
382 if (S_ISDIR(entry.mode)) {
383 obj = (struct object *)lookup_tree(the_repository, &entry.oid);
385 fsck_put_object_name(options, &entry.oid, "%s%s/",
387 result = options->walk(obj, OBJ_TREE, data, options);
389 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
390 obj = (struct object *)lookup_blob(the_repository, &entry.oid);
392 fsck_put_object_name(options, &entry.oid, "%s%s",
394 result = options->walk(obj, OBJ_BLOB, data, options);
397 result = error("in tree %s: entry %s has bad mode %.6o",
398 fsck_describe_object(options, &tree->object.oid),
399 entry.path, entry.mode);
409 static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
411 int counter = 0, generation = 0, name_prefix_len = 0;
412 struct commit_list *parents;
417 if (parse_commit(commit))
420 name = fsck_get_object_name(options, &commit->object.oid);
422 fsck_put_object_name(options, get_commit_tree_oid(commit),
425 result = options->walk((struct object *)get_commit_tree(commit),
426 OBJ_TREE, data, options);
431 parents = commit->parents;
432 if (name && parents) {
433 int len = strlen(name), power;
435 if (len && name[len - 1] == '^') {
437 name_prefix_len = len - 1;
439 else { /* parse ~<generation> suffix */
440 for (generation = 0, power = 1;
441 len && isdigit(name[len - 1]);
443 generation += power * (name[--len] - '0');
444 if (power > 1 && len && name[len - 1] == '~')
445 name_prefix_len = len - 1;
447 /* Maybe a non-first parent, e.g. HEAD^2 */
449 name_prefix_len = len;
456 struct object_id *oid = &parents->item->object.oid;
459 fsck_put_object_name(options, oid, "%s^%d",
461 else if (generation > 0)
462 fsck_put_object_name(options, oid, "%.*s~%d",
463 name_prefix_len, name,
466 fsck_put_object_name(options, oid, "%s^", name);
468 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
473 parents = parents->next;
478 static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
480 const char *name = fsck_get_object_name(options, &tag->object.oid);
485 fsck_put_object_name(options, &tag->tagged->oid, "%s", name);
486 return options->walk(tag->tagged, OBJ_ANY, data, options);
489 int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
494 if (obj->type == OBJ_NONE)
495 parse_object(the_repository, &obj->oid);
501 return fsck_walk_tree((struct tree *)obj, data, options);
503 return fsck_walk_commit((struct commit *)obj, data, options);
505 return fsck_walk_tag((struct tag *)obj, data, options);
507 error("Unknown object type for %s",
508 fsck_describe_object(options, &obj->oid));
518 static void name_stack_push(struct name_stack *stack, const char *name)
520 ALLOC_GROW(stack->names, stack->nr + 1, stack->alloc);
521 stack->names[stack->nr++] = name;
524 static const char *name_stack_pop(struct name_stack *stack)
526 return stack->nr ? stack->names[--stack->nr] : NULL;
529 static void name_stack_clear(struct name_stack *stack)
531 FREE_AND_NULL(stack->names);
532 stack->nr = stack->alloc = 0;
536 * The entries in a tree are ordered in the _path_ order,
537 * which means that a directory entry is ordered by adding
538 * a slash to the end of it.
540 * So a directory called "a" is ordered _after_ a file
541 * called "a.c", because "a/" sorts after "a.c".
543 #define TREE_UNORDERED (-1)
544 #define TREE_HAS_DUPS (-2)
546 static int is_less_than_slash(unsigned char c)
548 return '\0' < c && c < '/';
551 static int verify_ordered(unsigned mode1, const char *name1,
552 unsigned mode2, const char *name2,
553 struct name_stack *candidates)
555 int len1 = strlen(name1);
556 int len2 = strlen(name2);
557 int len = len1 < len2 ? len1 : len2;
558 unsigned char c1, c2;
561 cmp = memcmp(name1, name2, len);
565 return TREE_UNORDERED;
568 * Ok, the first <len> characters are the same.
569 * Now we need to order the next one, but turn
570 * a '\0' into a '/' for a directory entry.
576 * git-write-tree used to write out a nonsense tree that has
577 * entries with the same name, one blob and one tree. Make
578 * sure we do not have duplicate entries.
580 return TREE_HAS_DUPS;
581 if (!c1 && S_ISDIR(mode1))
583 if (!c2 && S_ISDIR(mode2))
587 * There can be non-consecutive duplicates due to the implicitly
596 * Record non-directory candidates (like "foo" and "foo.bar" in
597 * the example) on a stack and check directory candidates (like
598 * foo/" and "foo.bar/") against that stack.
600 if (!c1 && is_less_than_slash(c2)) {
601 name_stack_push(candidates, name1);
602 } else if (c2 == '/' && is_less_than_slash(c1)) {
605 const char *f_name = name_stack_pop(candidates);
609 if (!skip_prefix(name2, f_name, &p))
612 return TREE_HAS_DUPS;
613 if (is_less_than_slash(*p)) {
614 name_stack_push(candidates, f_name);
620 return c1 < c2 ? 0 : TREE_UNORDERED;
623 static int fsck_tree(const struct object_id *oid,
624 const char *buffer, unsigned long size,
625 struct fsck_options *options)
628 int has_null_sha1 = 0;
629 int has_full_path = 0;
630 int has_empty_name = 0;
634 int has_zero_pad = 0;
635 int has_bad_modes = 0;
636 int has_dup_entries = 0;
637 int not_properly_sorted = 0;
638 struct tree_desc desc;
641 struct name_stack df_dup_candidates = { NULL };
643 if (init_tree_desc_gently(&desc, buffer, size)) {
644 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
653 const char *name, *backslash;
654 const struct object_id *oid;
656 oid = tree_entry_extract(&desc, &name, &mode);
658 has_null_sha1 |= is_null_oid(oid);
659 has_full_path |= !!strchr(name, '/');
660 has_empty_name |= !*name;
661 has_dot |= !strcmp(name, ".");
662 has_dotdot |= !strcmp(name, "..");
663 has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
664 has_zero_pad |= *(char *)desc.buffer == '0';
666 if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
668 oidset_insert(&gitmodules_found, oid);
670 retval += report(options,
672 FSCK_MSG_GITMODULES_SYMLINK,
673 ".gitmodules is a symbolic link");
676 if ((backslash = strchr(name, '\\'))) {
679 has_dotgit |= is_ntfs_dotgit(backslash);
680 if (is_ntfs_dotgitmodules(backslash)) {
682 oidset_insert(&gitmodules_found, oid);
684 retval += report(options, oid, OBJ_TREE,
685 FSCK_MSG_GITMODULES_SYMLINK,
686 ".gitmodules is a symbolic link");
688 backslash = strchr(backslash, '\\');
692 if (update_tree_entry_gently(&desc)) {
693 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
708 * This is nonstandard, but we had a few of these
709 * early on when we honored the full set of mode
713 if (!options->strict)
721 switch (verify_ordered(o_mode, o_name, mode, name,
722 &df_dup_candidates)) {
724 not_properly_sorted = 1;
738 name_stack_clear(&df_dup_candidates);
741 retval += report(options, oid, OBJ_TREE, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
743 retval += report(options, oid, OBJ_TREE, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
745 retval += report(options, oid, OBJ_TREE, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
747 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOT, "contains '.'");
749 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTDOT, "contains '..'");
751 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
753 retval += report(options, oid, OBJ_TREE, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
755 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
757 retval += report(options, oid, OBJ_TREE, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
758 if (not_properly_sorted)
759 retval += report(options, oid, OBJ_TREE, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
763 static int verify_headers(const void *data, unsigned long size,
764 const struct object_id *oid, enum object_type type,
765 struct fsck_options *options)
767 const char *buffer = (const char *)data;
770 for (i = 0; i < size; i++) {
773 return report(options, oid, type,
774 FSCK_MSG_NUL_IN_HEADER,
775 "unterminated header: NUL at offset %ld", i);
777 if (i + 1 < size && buffer[i + 1] == '\n')
783 * We did not find double-LF that separates the header
784 * and the body. Not having a body is not a crime but
785 * we do want to see the terminating LF for the last header
788 if (size && buffer[size - 1] == '\n')
791 return report(options, oid, type,
792 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
795 static int fsck_ident(const char **ident,
796 const struct object_id *oid, enum object_type type,
797 struct fsck_options *options)
799 const char *p = *ident;
802 *ident = strchrnul(*ident, '\n');
807 return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
808 p += strcspn(p, "<>\n");
810 return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
812 return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
814 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
816 p += strcspn(p, "<>\n");
818 return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
821 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
823 if (*p == '0' && p[1] != ' ')
824 return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
825 if (date_overflows(parse_timestamp(p, &end, 10)))
826 return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
827 if ((end == p || *end != ' '))
828 return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
830 if ((*p != '+' && *p != '-') ||
836 return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
841 static int fsck_commit(const struct object_id *oid,
842 const char *buffer, unsigned long size,
843 struct fsck_options *options)
845 struct object_id tree_oid, parent_oid;
846 unsigned author_count;
848 const char *buffer_begin = buffer;
851 if (verify_headers(buffer, size, oid, OBJ_COMMIT, options))
854 if (!skip_prefix(buffer, "tree ", &buffer))
855 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
856 if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
857 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
862 while (skip_prefix(buffer, "parent ", &buffer)) {
863 if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') {
864 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
871 while (skip_prefix(buffer, "author ", &buffer)) {
873 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
877 if (author_count < 1)
878 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
879 else if (author_count > 1)
880 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
883 if (!skip_prefix(buffer, "committer ", &buffer))
884 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
885 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
888 if (memchr(buffer_begin, '\0', size)) {
889 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT,
890 "NUL byte in the commit object body");
897 static int fsck_tag(const struct object_id *oid, const char *buffer,
898 unsigned long size, struct fsck_options *options)
900 struct object_id tagged_oid;
902 return fsck_tag_standalone(oid, buffer, size, options, &tagged_oid,
906 int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
907 unsigned long size, struct fsck_options *options,
908 struct object_id *tagged_oid,
913 struct strbuf sb = STRBUF_INIT;
916 ret = verify_headers(buffer, size, oid, OBJ_TAG, options);
920 if (!skip_prefix(buffer, "object ", &buffer)) {
921 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
924 if (parse_oid_hex(buffer, tagged_oid, &p) || *p != '\n') {
925 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
931 if (!skip_prefix(buffer, "type ", &buffer)) {
932 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
935 eol = strchr(buffer, '\n');
937 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
940 *tagged_type = type_from_string_gently(buffer, eol - buffer, 1);
941 if (*tagged_type < 0)
942 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
947 if (!skip_prefix(buffer, "tag ", &buffer)) {
948 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
951 eol = strchr(buffer, '\n');
953 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
956 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
957 if (check_refname_format(sb.buf, 0)) {
958 ret = report(options, oid, OBJ_TAG,
959 FSCK_MSG_BAD_TAG_NAME,
960 "invalid 'tag' name: %.*s",
961 (int)(eol - buffer), buffer);
967 if (!skip_prefix(buffer, "tagger ", &buffer)) {
968 /* early tags do not contain 'tagger' lines; warn only */
969 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
974 ret = fsck_ident(&buffer, oid, OBJ_TAG, options);
978 if (!starts_with(buffer, "\n")) {
980 * The verify_headers() check will allow
981 * e.g. "[...]tagger <tagger>\nsome
982 * garbage\n\nmessage" to pass, thinking "some
983 * garbage" could be a custom header. E.g. "mktag"
984 * doesn't want any unknown headers.
986 ret = report(options, oid, OBJ_TAG, FSCK_MSG_EXTRA_HEADER_ENTRY, "invalid format - extra header(s) after 'tagger'");
997 * Like builtin/submodule--helper.c's starts_with_dot_slash, but without
998 * relying on the platform-dependent is_dir_sep helper.
1000 * This is for use in checking whether a submodule URL is interpreted as
1001 * relative to the current directory on any platform, since \ is a
1002 * directory separator on Windows but not on other platforms.
1004 static int starts_with_dot_slash(const char *str)
1006 return str[0] == '.' && (str[1] == '/' || str[1] == '\\');
1010 * Like starts_with_dot_slash, this is a variant of submodule--helper's
1011 * helper of the same name with the twist that it accepts backslash as a
1012 * directory separator even on non-Windows platforms.
1014 static int starts_with_dot_dot_slash(const char *str)
1016 return str[0] == '.' && starts_with_dot_slash(str + 1);
1019 static int submodule_url_is_relative(const char *url)
1021 return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url);
1025 * Count directory components that a relative submodule URL should chop
1026 * from the remote_url it is to be resolved against.
1028 * In other words, this counts "../" components at the start of a
1031 * Returns the number of directory components to chop and writes a
1032 * pointer to the next character of url after all leading "./" and
1033 * "../" components to out.
1035 static int count_leading_dotdots(const char *url, const char **out)
1039 if (starts_with_dot_dot_slash(url)) {
1041 url += strlen("../");
1044 if (starts_with_dot_slash(url)) {
1045 url += strlen("./");
1053 * Check whether a transport is implemented by git-remote-curl.
1055 * If it is, returns 1 and writes the URL that would be passed to
1056 * git-remote-curl to the "out" parameter.
1058 * Otherwise, returns 0 and leaves "out" untouched.
1061 * http::https://example.com/repo.git -> 1, https://example.com/repo.git
1062 * https://example.com/repo.git -> 1, https://example.com/repo.git
1063 * git://example.com/repo.git -> 0
1065 * This is for use in checking for previously exploitable bugs that
1066 * required a submodule URL to be passed to git-remote-curl.
1068 static int url_to_curl_url(const char *url, const char **out)
1071 * We don't need to check for case-aliases, "http.exe", and so
1072 * on because in the default configuration, is_transport_allowed
1073 * prevents URLs with those schemes from being cloned
1076 if (skip_prefix(url, "http::", out) ||
1077 skip_prefix(url, "https::", out) ||
1078 skip_prefix(url, "ftp::", out) ||
1079 skip_prefix(url, "ftps::", out))
1081 if (starts_with(url, "http://") ||
1082 starts_with(url, "https://") ||
1083 starts_with(url, "ftp://") ||
1084 starts_with(url, "ftps://")) {
1091 static int check_submodule_url(const char *url)
1093 const char *curl_url;
1095 if (looks_like_command_line_option(url))
1098 if (submodule_url_is_relative(url) || starts_with(url, "git://")) {
1104 * This could be appended to an http URL and url-decoded;
1105 * check for malicious characters.
1107 decoded = url_decode(url);
1108 has_nl = !!strchr(decoded, '\n');
1115 * URLs which escape their root via "../" can overwrite
1116 * the host field and previous components, resolving to
1117 * URLs like https::example.com/submodule.git and
1118 * https:///example.com/submodule.git that were
1119 * susceptible to CVE-2020-11008.
1121 if (count_leading_dotdots(url, &next) > 0 &&
1122 (*next == ':' || *next == '/'))
1126 else if (url_to_curl_url(url, &curl_url)) {
1127 struct credential c = CREDENTIAL_INIT;
1129 if (credential_from_url_gently(&c, curl_url, 1) ||
1132 credential_clear(&c);
1139 struct fsck_gitmodules_data {
1140 const struct object_id *oid;
1141 struct fsck_options *options;
1145 static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
1147 struct fsck_gitmodules_data *data = vdata;
1148 const char *subsection, *key;
1149 size_t subsection_len;
1152 if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
1156 name = xmemdupz(subsection, subsection_len);
1157 if (check_submodule_name(name) < 0)
1158 data->ret |= report(data->options,
1159 data->oid, OBJ_BLOB,
1160 FSCK_MSG_GITMODULES_NAME,
1161 "disallowed submodule name: %s",
1163 if (!strcmp(key, "url") && value &&
1164 check_submodule_url(value) < 0)
1165 data->ret |= report(data->options,
1166 data->oid, OBJ_BLOB,
1167 FSCK_MSG_GITMODULES_URL,
1168 "disallowed submodule url: %s",
1170 if (!strcmp(key, "path") && value &&
1171 looks_like_command_line_option(value))
1172 data->ret |= report(data->options,
1173 data->oid, OBJ_BLOB,
1174 FSCK_MSG_GITMODULES_PATH,
1175 "disallowed submodule path: %s",
1177 if (!strcmp(key, "update") && value &&
1178 parse_submodule_update_type(value) == SM_UPDATE_COMMAND)
1179 data->ret |= report(data->options, data->oid, OBJ_BLOB,
1180 FSCK_MSG_GITMODULES_UPDATE,
1181 "disallowed submodule update setting: %s",
1188 static int fsck_blob(const struct object_id *oid, const char *buf,
1189 unsigned long size, struct fsck_options *options)
1191 struct fsck_gitmodules_data data;
1192 struct config_options config_opts = { 0 };
1194 if (!oidset_contains(&gitmodules_found, oid))
1196 oidset_insert(&gitmodules_done, oid);
1198 if (object_on_skiplist(options, oid))
1203 * A missing buffer here is a sign that the caller found the
1204 * blob too gigantic to load into memory. Let's just consider
1207 return report(options, oid, OBJ_BLOB,
1208 FSCK_MSG_GITMODULES_LARGE,
1209 ".gitmodules too large to parse");
1213 data.options = options;
1215 config_opts.error_action = CONFIG_ERROR_SILENT;
1216 if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
1217 ".gitmodules", buf, size, &data, &config_opts))
1218 data.ret |= report(options, oid, OBJ_BLOB,
1219 FSCK_MSG_GITMODULES_PARSE,
1220 "could not parse gitmodules blob");
1225 int fsck_object(struct object *obj, void *data, unsigned long size,
1226 struct fsck_options *options)
1229 return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
1231 if (obj->type == OBJ_BLOB)
1232 return fsck_blob(&obj->oid, data, size, options);
1233 if (obj->type == OBJ_TREE)
1234 return fsck_tree(&obj->oid, data, size, options);
1235 if (obj->type == OBJ_COMMIT)
1236 return fsck_commit(&obj->oid, data, size, options);
1237 if (obj->type == OBJ_TAG)
1238 return fsck_tag(&obj->oid, data, size, options);
1240 return report(options, &obj->oid, obj->type,
1241 FSCK_MSG_UNKNOWN_TYPE,
1242 "unknown type '%d' (internal fsck error)",
1246 int fsck_error_function(struct fsck_options *o,
1247 const struct object_id *oid,
1248 enum object_type object_type,
1249 enum fsck_msg_type msg_type, const char *message)
1251 if (msg_type == FSCK_WARN) {
1252 warning("object %s: %s", fsck_describe_object(o, oid), message);
1255 error("object %s: %s", fsck_describe_object(o, oid), message);
1259 void register_found_gitmodules(const struct object_id *oid)
1261 oidset_insert(&gitmodules_found, oid);
1264 int fsck_finish(struct fsck_options *options)
1267 struct oidset_iter iter;
1268 const struct object_id *oid;
1270 oidset_iter_init(&gitmodules_found, &iter);
1271 while ((oid = oidset_iter_next(&iter))) {
1272 enum object_type type;
1276 if (oidset_contains(&gitmodules_done, oid))
1279 buf = read_object_file(oid, &type, &size);
1281 if (is_promisor_object(oid))
1283 ret |= report(options,
1285 FSCK_MSG_GITMODULES_MISSING,
1286 "unable to read .gitmodules blob");
1290 if (type == OBJ_BLOB)
1291 ret |= fsck_blob(oid, buf, size, options);
1293 ret |= report(options,
1295 FSCK_MSG_GITMODULES_BLOB,
1296 "non-blob found at .gitmodules");
1301 oidset_clear(&gitmodules_found);
1302 oidset_clear(&gitmodules_done);
1306 int git_fsck_config(const char *var, const char *value, void *cb)
1308 struct fsck_options *options = cb;
1309 if (strcmp(var, "fsck.skiplist") == 0) {
1311 struct strbuf sb = STRBUF_INIT;
1313 if (git_config_pathname(&path, var, value))
1315 strbuf_addf(&sb, "skiplist=%s", path);
1317 fsck_set_msg_types(options, sb.buf);
1318 strbuf_release(&sb);
1322 if (skip_prefix(var, "fsck.", &var)) {
1323 fsck_set_msg_type(options, var, value);
1327 return git_default_config(var, value, cb);