@@ -1119,14 +1119,32 @@ static int safe_directory_cb(const char *key, const char *value, void *d)
1119
1119
return 0 ;
1120
1120
}
1121
1121
1122
- static int ensure_valid_ownership (const char * path )
1122
+ /*
1123
+ * Check if a repository is safe, by verifying the ownership of the
1124
+ * worktree (if any), the git directory, and the gitfile (if any).
1125
+ *
1126
+ * Exemptions for known-safe repositories can be added via `safe.directory`
1127
+ * config settings; for non-bare repositories, their worktree needs to be
1128
+ * added, for bare ones their git directory.
1129
+ */
1130
+ static int ensure_valid_ownership (const char * gitfile ,
1131
+ const char * worktree , const char * gitdir )
1123
1132
{
1124
- struct safe_directory_data data = { .path = path };
1133
+ struct safe_directory_data data = {
1134
+ .path = worktree ? worktree : gitdir
1135
+ };
1125
1136
1126
1137
if (!git_env_bool ("GIT_TEST_ASSUME_DIFFERENT_OWNER" , 0 ) &&
1127
- is_path_owned_by_current_user (path ))
1138
+ (!gitfile || is_path_owned_by_current_user (gitfile )) &&
1139
+ (!worktree || is_path_owned_by_current_user (worktree )) &&
1140
+ (!gitdir || is_path_owned_by_current_user (gitdir )))
1128
1141
return 1 ;
1129
1142
1143
+ /*
1144
+ * data.path is the "path" that identifies the repository and it is
1145
+ * constant regardless of what failed above. data.is_safe should be
1146
+ * initialized to false, and might be changed by the callback.
1147
+ */
1130
1148
read_very_early_config (safe_directory_cb , & data );
1131
1149
1132
1150
return data .is_safe ;
@@ -1214,6 +1232,8 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
1214
1232
current_device = get_device_or_die (dir -> buf , NULL , 0 );
1215
1233
for (;;) {
1216
1234
int offset = dir -> len , error_code = 0 ;
1235
+ char * gitdir_path = NULL ;
1236
+ char * gitfile = NULL ;
1217
1237
1218
1238
if (offset > min_offset )
1219
1239
strbuf_addch (dir , '/' );
@@ -1224,21 +1244,50 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
1224
1244
if (die_on_error ||
1225
1245
error_code == READ_GITFILE_ERR_NOT_A_FILE ) {
1226
1246
/* NEEDSWORK: fail if .git is not file nor dir */
1227
- if (is_git_directory (dir -> buf ))
1247
+ if (is_git_directory (dir -> buf )) {
1228
1248
gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT ;
1249
+ gitdir_path = xstrdup (dir -> buf );
1250
+ }
1229
1251
} else if (error_code != READ_GITFILE_ERR_STAT_FAILED )
1230
1252
return GIT_DIR_INVALID_GITFILE ;
1231
- }
1253
+ } else
1254
+ gitfile = xstrdup (dir -> buf );
1255
+ /*
1256
+ * Earlier, we tentatively added DEFAULT_GIT_DIR_ENVIRONMENT
1257
+ * to check that directory for a repository.
1258
+ * Now trim that tentative addition away, because we want to
1259
+ * focus on the real directory we are in.
1260
+ */
1232
1261
strbuf_setlen (dir , offset );
1233
1262
if (gitdirenv ) {
1234
- if (!ensure_valid_ownership (dir -> buf ))
1235
- return GIT_DIR_INVALID_OWNERSHIP ;
1236
- strbuf_addstr (gitdir , gitdirenv );
1237
- return GIT_DIR_DISCOVERED ;
1263
+ enum discovery_result ret ;
1264
+
1265
+ if (ensure_valid_ownership (gitfile ,
1266
+ dir -> buf ,
1267
+ (gitdir_path ? gitdir_path : gitdirenv ))) {
1268
+ strbuf_addstr (gitdir , gitdirenv );
1269
+ ret = GIT_DIR_DISCOVERED ;
1270
+ } else
1271
+ ret = GIT_DIR_INVALID_OWNERSHIP ;
1272
+
1273
+ /*
1274
+ * Earlier, during discovery, we might have allocated
1275
+ * string copies for gitdir_path or gitfile so make
1276
+ * sure we don't leak by freeing them now, before
1277
+ * leaving the loop and function.
1278
+ *
1279
+ * Note: gitdirenv will be non-NULL whenever these are
1280
+ * allocated, therefore we need not take care of releasing
1281
+ * them outside of this conditional block.
1282
+ */
1283
+ free (gitdir_path );
1284
+ free (gitfile );
1285
+
1286
+ return ret ;
1238
1287
}
1239
1288
1240
1289
if (is_git_directory (dir -> buf )) {
1241
- if (!ensure_valid_ownership (dir -> buf ))
1290
+ if (!ensure_valid_ownership (NULL , NULL , dir -> buf ))
1242
1291
return GIT_DIR_INVALID_OWNERSHIP ;
1243
1292
strbuf_addstr (gitdir , "." );
1244
1293
return GIT_DIR_BARE ;
@@ -1376,7 +1425,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
1376
1425
struct strbuf quoted = STRBUF_INIT ;
1377
1426
1378
1427
sq_quote_buf_pretty (& quoted , dir .buf );
1379
- die (_ ("unsafe repository ( '%s' is owned by someone else) \n"
1428
+ die (_ ("detected dubious ownership in repository at '%s'\n"
1380
1429
"To add an exception for this directory, call:\n"
1381
1430
"\n"
1382
1431
"\tgit config --global --add safe.directory %s" ),
0 commit comments