Article View: linux.kernel.git
Article #306[PATCH v3 4/4] fast-import: use early_scan_options() for --allow-unsafe-features
From: Christian Couder
Date: Mon, 12 Oct 2026 07:44
Date: Mon, 12 Oct 2026 07:44
162 lines
15769 bytes
15769 bytes
The "feature" lines at the start of the stream are processed before the
command line options are parsed, so cmd_fast_import() scans its
arguments early to find out if `--allow-unsafe-features` was given.
That scan doesn't know which options take their value as a separate
argument, and it stops at the first argument that doesn't start with a
dash. So it disagrees with parse_options(), which accepts values
separated from their option by a space, for a command line like
"--depth 5 --allow-unsafe-features": the scan stops at "5" and never
sees the option, so unsafe "feature" commands from the stream are
refused even though the option was given.
Let's fix this by using early_scan_options(), which scans the very
same `struct option` array that parse_options() uses, so that both
agree on which options take a value, and by marking
`--allow-unsafe-features` with PARSE_OPT_EARLY so that the scan
reports it.
As early_scan_options() looks up long options the same way as
parse_options(), the two also agree on abbreviations now. So
`--allow-unsafe` allows unsafe "feature" commands, while
"--quiet --export-pack --allow-unsafe-features" refuses them, as
parse_options() takes `--allow-unsafe-features` there as the value of
`--export-pack-edges`. (The old scan wrongly allowed them in this
case, as all the arguments start with a dash.)
The note about having to spell the option in full in the
documentation is therefore removed, and so is the NEEDSWORK comment
about it.
Helped-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
Signed-off-by: Christian Couder <christian.couder@gmail.com>
---
Documentation/git-fast-import.adoc | 7 ------
builtin/fast-import.c | 37 ++++++++++++++++--------------
t/t9300-fast-import.sh | 29 +++++++++++++++++++++++
3 files changed, 49 insertions(+), 24 deletions(-)
diff --git a/Documentation/git-fast-import.adoc b/Documentation/git-fast-import.adoc
index fd165e11d2..7c5900e048 100644
--- a/Documentation/git-fast-import.adoc
+++ b/Documentation/git-fast-import.adoc
@@ -65,13 +65,6 @@ Only enable this option if you trust the program generating the
fast-import stream! This option is enabled automatically for
remote-helpers that use the `import` capability, as they are
already trusted to run their own code.
-+
-Note that this option has to be spelled in full, and has to appear
-before any option whose value is separated from it by a space, for
-the unsafe `feature` commands in the stream to be allowed. So
-`--allow-unsafe` or `--depth 5 --allow-unsafe-features` still refuse
-them, while `--allow-unsafe-features --depth 5` and
-`--depth=5 --allow-unsafe-features` allow them.
`--signed-tags=<mode>`::
Specify how to handle signed tags. Behaves in the same way as
diff --git a/builtin/fast-import.c b/builtin/fast-import.c
index fbd919982c..2e667af349 100644
--- a/builtin/fast-import.c
+++ b/builtin/fast-import.c
@@ -4120,6 +4120,18 @@ static int option_parse_quiet(const struct option *opt UNUSED,
return 0;
}
+static int option_parse_early_allow_unsafe(const struct option *option,
+ const char *value UNUSED,
+ bool unset, int pos UNUSED,
+ void *data)
+{
+ struct fast_import_state *state = data;
+
+ if (!strcmp(option->long_name, "allow-unsafe-features"))
+ state->allow_unsafe_features = !unset;
+ return 0;
+}
+
int cmd_fast_import(int argc,
const char **argv,
const char *prefix,
@@ -4184,7 +4196,7 @@ int cmd_fast_import(int argc,
OPT_HIDDEN_GROUP(N_("Advanced")),
OPT_BOOL_F(0, "allow-unsafe-features", &state.allow_unsafe_features,
N_("allow unsafe mark commands from the stream"),
- PARSE_OPT_HIDDEN | PARSE_OPT_NONEG),
+ PARSE_OPT_HIDDEN | PARSE_OPT_NONEG | PARSE_OPT_EARLY),
OPT_CALLBACK_F(0, "export-pack-edges", &state, N_("file"),
N_("dump edge commits to <file>"),
PARSE_OPT_HIDDEN | PARSE_OPT_NONEG,
@@ -4218,23 +4230,14 @@ int cmd_fast_import(int argc,
* line to override stream data). But we must do an early parse of any
* command-line options that impact how we interpret the feature lines.
*
- * NEEDSWORK: This scan only matches the exact "--allow-unsafe-features"
- * spelling and stops at the first argument that doesn't start with a
- * dash. As parse_options() below also accepts unambiguous abbreviations
- * and values separated by a space from their option, the two disagree
- * for command lines like "--allow-unsafe" or "--depth 5
- * --allow-unsafe-features": parse_options() accepts the option, but
- * this scan doesn't see it, so unsafe features from the stream are
- * still refused. This errs on the safe side, but should be fixed by
- * teaching this scan about the options that take a value.
+ * The scan uses the same options as parse_options() below, so the
+ * two agree on which options take a value and on abbreviations. In
+ * case of doubt, the scan stops early, so unsafe features are then
+ * refused rather than wrongly allowed.
*/
- for (int i = 1; i < argc; i++) {
- const char *arg = argv[i];
- if (*arg != '-' || !strcmp(arg, "--"))
- break;
- if (!strcmp(arg, "--allow-unsafe-features"))
- state.allow_unsafe_features = 1;
- }
+ early_scan_options(argc - 1, argv + 1, fast_import_options,
+ EARLY_SCAN_STOP_AT_NON_OPTION,
+ option_parse_early_allow_unsafe, &state);
rc_free = mem_pool_alloc(&fi_mem_pool, cmd_save * sizeof(*rc_free));
for (unsigned int i = 0; i < (cmd_save - 1); i++)
diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
index d9de2ef0d8..85059a5385 100755
--- a/t/t9300-fast-import.sh
+++ b/t/t9300-fast-import.sh
@@ -2344,6 +2344,35 @@ test_expect_success 'R: export-marks options can be overridden by commandline op
test_path_is_missing feature-sub
'
+test_expect_success 'R: --allow-unsafe-features found after a value' '
+ echo "feature import-marks-if-exists=nonexistent.marks" >input &&
+ git fast-import --allow-unsafe-features <input &&
+ git fast-import --depth=5 --allow-unsafe-features <input &&
+ git fast-import --depth 5 --allow-unsafe-features <input &&
+ git fast-import --date-format raw --allow-unsafe-features <input
+'
+
+test_expect_success 'R: --allow-unsafe-features can be abbreviated' '
+ echo "feature import-marks-if-exists=nonexistent.marks" >input &&
+ GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
+ git fast-import --allow-unsafe <input
+'
+
+test_expect_success 'R: disallowed abbreviation of --allow-unsafe-features' '
+ echo "feature import-marks-if-exists=nonexistent.marks" >input &&
+ test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \
+ git fast-import --allow-unsafe <input 2>err &&
+ test_grep "forbidden in input without --allow-unsafe-features" err
+'
+
+test_expect_success 'R: --allow-unsafe-features as the value of an abbreviation' '
+ echo "feature import-marks-if-exists=nonexistent.marks" >input &&
+ test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
+ git fast-import --quiet --export-pack \
+ --allow-unsafe-features <input 2>err &&
+ test_grep "forbidden in input without --allow-unsafe-features" err
+'
+
test_expect_success 'R: catch typo in marks file name' '
test_must_fail git fast-import --import-marks=nonexistent.marks </dev/null &&
echo "feature import-marks=nonexistent.marks" |
--
2.56.0.116.g6de20f6092
Technical Details
Message-ID:
<20261012074413.337183-5-christian.couder@gmail.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-71733-linux-kernel-git=dewy.ruReferences:
<20260923080928.1534413-1-christian.couder@gmail.com> <20261012074413.337183-1-christian.couder@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-71733-linux-kernel-git=dewy.ru
From: christian.couder@gmail.com (Christian Couder)
Newsgroups: linux.kernel.git
Subject: [PATCH v3 4/4] fast-import: use early_scan_options() for --allow-unsafe-features
Date: Mon, 12 Oct 2026 09:44:13 +0200
Organization: Usenet Network
Message-ID: <20261012074413.337183-5-christian.couder@gmail.com>
References: <20260923080928.1534413-1-christian.couder@gmail.com>
<20261012074413.337183-1-christian.couder@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="1710588"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:UDFbn9oQmaJ0WoDzUkz+3+Rxrgw= sha256:7WCe7xdsD2z2qkJh2G9SeON0d7qrc0fCPzvC9SFM4Sg=
sha1:I8eCf3eErJbbnDRV4ZCj40eUJfM= sha256:wZ6ruh4DTlVh55/hucHSJ2jKdU1aLOd4MVMK8tb+3yU=
List-ID: <git.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
by mail.dewy.ru (Postfix) with ESMTP id 1021410FB1C; Mon, 12 Oct 2026 10:46:01 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -0.848
X-Spam-Status: No, score=-0.848 tagged_above=-9999 required=6.31
tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,
FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001,
HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1,
SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001]
autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id LsICyRRNK9DF;
Mon, 12 Oct 2026 10:45:58 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=git+bounces-71733-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74])
by mail.dewy.ru (Postfix) with ESMTPS id 1FE0010FB19; Mon, 12 Oct 2026 10:45:50 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1])
by sto.lore.kernel.org (Postfix) with ESMTP id 8A52D6041C2; Mon, 12 Oct 2026 07:45:21 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id 6D343450405;
Mon, 12 Oct 2026 07:45:02 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kByFf13G"
X-Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50])
(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id C304B4477F7
for <git@vger.kernel.org>; Mon, 12 Oct 2026 07:44:59 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1791791101; cv=none; b=l+sdMJbfgWmO+f1HVBjWmJ3DsLzShwLlOAmtj8jRzn/m65Fh4D+PE1bjV6a4Ig9If9uzN/FTVOG0UOX+Pjuj7KjnE+XVFo4mm5cvBpFO8C66Qeq1Ov+BayZJHtQM4oOE9xRtxw4SWxifwfHO191nAf8ORO2/+Yf8qVorbEzljQA=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1791791101; c=relaxed/simple;
bh=Jp2elNvJsdQfRKqbBoZzLB4Gp9/Nmy1oHW/OF3HojZg=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
MIME-Version; b=US9u9+e7ciQVBlAJBPBLrguoPFQts2QaXOu3CyR5X7AYGcWt6KDe2tiKeWgSZSdnYwT/OBds4HqynNr2XuQ4UWAXGLglEkb4qNadA9Jb4rO5QdWzTWTxNepdCYr8TosSMojPr4taXi1RDpSZ3Eol6BPrs+IuiSogwMp5oCBwjkk=
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kByFf13G; arc=none smtp.client-ip=209.85.128.50
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-4a172eb31f0so10448335e9.1
for <git@vger.kernel.org>; Mon, 12 Oct 2026 00:44:59 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20251104; t=1791791098; x=1792395898; darn=vger.kernel.org;
h=content-transfer-encoding:mime-version:references:in-reply-to
:message-id:date:subject:cc:to:from:from:to:cc:subject:date
:message-id:reply-to:content-type;
bh=Rb9/vl6lTqBhLeKptlhg4ipfsTo/v61C436fKup+5dY=;
b=kByFf13GbEnuBb43MLhD2gYwzuyogCENC2Op+Waz+1sKClZBNpvm/Opn1gO53A//4x
vVF1cxkkAIR0WXevGjHuRnri3pkt4DMC+YTYuAKsydiPPnIFtD4bR3s7NTcKkH5CwbnP
WjhvVsI5ELOiDmyeZJFbEkkcbel3D12xH9fuEWkbPJjcVBrfgsaqZTf23rWYf/xWMFxF
dTKHXUKbZhUfnY5XeV3eEMiRIv+90iR3pCTaMCHfDmvXD7WZfu9UkG7/K14/oOWqFHqR
W3HNYfAlS9X8eIfzntL+LXIU0FdtAzQrSSrzVcJlTBP8bxMRMI9919zhY0DhAH4oVnj8
3Jyw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1791791098; x=1792395898;
h=content-transfer-encoding:mime-version:references:in-reply-to
:message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from
:to:cc:subject:date:message-id:reply-to:content-type;
bh=Rb9/vl6lTqBhLeKptlhg4ipfsTo/v61C436fKup+5dY=;
b=ltMSK35cVYjUQKBN6Bpsf8gBZmCYdHjnmCrdpd+QihwUZ3WbUz5PZggJ+hU6SIVgzB
WLStb92HMXfy4FpwKUahyuIuIuMaIJAwYCKjuLmFBOU7bbLqTsJOOiYE+G3f97ByzZ57
KsGPbS6Y1Qa+szeDzqW8zOCLppKgd86I8RGQ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1f17b1804b1-4a18e49d640mr164738035e9.8.1791791097723;
Mon, 12 Oct 2026 00:44:57 -0700 (PDT)
X-Received: from christian--20230123--2G7D3 ([62.35.114.108])
by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48db99b800asm17680723f8f.22.2026.10.12.00.44.56
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Mon, 12 Oct 2026 00:44:56 -0700 (PDT)
X-Cc: Junio C Hamano <gitster@pobox.com>,
Kaartic Sivaraam <kaartic.sivaraam@gmail.com>,
Patrick Steinhardt <ps@pks.im>,
Elijah Newren <newren@gmail.com>,
Jeff King <peff@peff.net>,
"brian m . carlson" <sandals@crustytoothpaste.net>,
Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Justin Tobler <jltobler@gmail.com>,
Christian Couder <christian.couder@gmail.com>
X-Mailer: git-send-email 2.56.0.116.g6de20f6092
X-In-Reply-To: <20261012074413.337183-1-christian.couder@gmail.com>
X-Precedence: bulk
X-Mailing-List: git@vger.kernel.org
X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org>