Skip to content
TypeScript

fix(shared): recognize numeric bare-repository config (#27500)

maco260d84387fbf
diff --git a/packages/shared/src/git.test.ts b/packages/shared/src/git.test.ts
index a788abb1d1d..a21a52cb70e 100644
--- a/packages/shared/src/git.test.ts
+++ b/packages/shared/src/git.test.ts
@@ -66,6 +66,47 @@ describe("normalizeGitRemoteUrl", () => {
});
describe("isBareGitConfig", () => {
+ it("reads Git's numeric booleans without accepting invalid or overflowing values", () => {
+ for (const value of [
+ "true",
+ "yes",
+ "on",
+ "1",
+ "2",
+ "-1",
+ "+1",
+ "01",
+ "0x10",
+ "1k",
+ "1m",
+ "1g",
+ "2147483647",
+ "-2147483648",
+ ]) {
+ expect(isBareGitConfig(`[core]\nbare = ${value}\n`), value).toBe(true);
+ }
+ for (const value of [
+ "false",
+ "no",
+ "off",
+ "0",
+ "-0",
+ "00",
+ "0x0",
+ "0k",
+ "08",
+ "1.5",
+ "2no",
+ "2g",
+ "2147483648",
+ "-2147483649",
+ ]) {
+ expect(isBareGitConfig(`[core]\nbare = ${value}\n`), value).toBe(false);
+ }
+ expect(isBareGitConfig("[core]\nbare = 2\nbare = 0\n")).toBe(false);
+ expect(isBareGitConfig("[core]\nbare = false\nbare = -2\n")).toBe(true);
+ });
+
it("reads continued bare values after comments and at end of input", () => {
const config = ["[core] bare = false # ignored backslash \\", 'bare = "tr\\', 'ue"'].join("\n");
diff --git a/packages/shared/src/git.ts b/packages/shared/src/git.ts
index ba7c9f0a97b..9f7c6c3227e 100644
--- a/packages/shared/src/git.ts
+++ b/packages/shared/src/git.ts
@@ -213,6 +213,21 @@ function parseGitConfigValue(raw: string): string {
return out.trim();
}
+function isTrueGitConfigValue(value: string): boolean {
+ if (value === "true" || value === "yes" || value === "on") return true;
+ const numeric = /^([+-]?)(0x[0-9a-f]+|0[0-7]*|[1-9][0-9]*)([kmg]?)$/.exec(value);
+ if (numeric === null) return false;
+ const digits = numeric[2]!;
+ const magnitude = digits.startsWith("0x")
+ ? Number.parseInt(digits.slice(2), 16)
+ : Number.parseInt(digits, digits.startsWith("0") ? 8 : 10);
+ const scale =
+ numeric[3] === "k" ? 1024 : numeric[3] === "m" ? 1024 ** 2 : numeric[3] === "g" ? 1024 ** 3 : 1;
+ const number = magnitude * scale * (numeric[1] === "-" ? -1 : 1);
+ // Git's boolean parser accepts integers only within its signed 32-bit range.
+ return number !== 0 && number >= -2147483648 && number <= 2147483647;
+}
+
export function isBareGitConfig(configText: string): boolean {
let inCore = false;
let bare = false;
@@ -228,7 +243,7 @@ export function isBareGitConfig(configText: string): boolean {
const match = /^bare\s*(?:=(.*)|[#;].*)?$/i.exec(line);
if (match === null) continue;
const value = match[1] === undefined ? "true" : parseGitConfigValue(match[1]).toLowerCase();
- bare = value === "true" || value === "yes" || value === "on" || value === "1";
+ bare = isTrueGitConfigValue(value);
}
return bare;
}