Skip to content

Commit ccd2de3

Browse files
committed
🔒 Hold the octal escape to Git's grammar, which stops at 3
Git's `unquote_c_style()` cases an octal escape's leading digit as `0`-`3` and nothing else, because an escape names one byte and `\400` is already 256. So `\4` through `\7` are escapes Git does not have: the quoting fails and `parse_alt_odb_entry()` reads the whole entry as ordinary literal text. The containment scanner accepted any leading octal digit and then assembled the value through a typed array, so `\457` became 303 truncated to 47 — a `/`. It saw a path separator where Git sees no escape at all, and one alternates entry then named two different object directories: extra segments the invented separator created, which a decoy inside the authenticated database answers for, and the literal fallback Git actually traverses. The grammar now stops at 3, and the byte a valid escape names is range-checked on the way in rather than assumed, so no value can reach a typed array and come back out as a different character. Everything Git rejects stays on the same literal fallback Git uses, and an entry whose exact path still cannot be named fails closed. Quoted entries, the transitive chain, symlink escapes, lazy completed replay and the withheld failure text are unchanged. The regression plants both readings of one entry: the decoy chain the truncating reading resolves, and a database outside the materialization that only Git's literal fallback reaches. Ordinary Git in the same export resolves a commit this run never held through it. Widening the leading digit back to `0-7` fails that test alone, with the other twenty-one steps in the suite still passing. Refs #370.
1 parent 3f889e1 commit ccd2de3

2 files changed

Lines changed: 148 additions & 3 deletions

File tree

‎packages/workflow/src/deno/composition/object-source.ts‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,36 @@ const ESCAPES: ReadonlyMap<string, number> = new Map([
150150
['"', 0x22],
151151
]);
152152

153+
/**
154+
* Git's octal escape grammar, which is narrower than "three octal digits".
155+
*
156+
* `unquote_c_style()` cases the leading digit as `0`–`3` and nothing else, so
157+
* `\\4` through `\\7` are escapes it does not know: quoting fails and
158+
* `parse_alt_odb_entry()` reads the whole entry as ordinary literal text. The
159+
* reason is arithmetic — an escape names one byte, and `\\400` is already
160+
* 256 — so a reader that accepted a wider leading digit would be computing a
161+
* value no byte can hold.
162+
*
163+
* Accepting one is not a near miss. `\\457` is 303, and 303 truncated into a
164+
* byte is 47, which is `/`: a reader that wrapped it would see a path separator
165+
* where Git sees an invalid escape, and the two would then resolve entirely
166+
* different object directories from the same entry.
167+
*/
168+
const OCTAL_LEAD = /^[0-3]$/;
153169
const OCTAL = /^[0-7]$/;
154170

171+
/**
172+
* One byte of an unquoted path, or `undefined` when the value is not one.
173+
*
174+
* Git's own grammar already keeps every value it accepts inside a byte, and
175+
* this is what makes that a property this reading checks rather than one it
176+
* assumes: nothing here reaches a typed array that would quietly truncate a
177+
* value into a different character.
178+
*/
179+
function pathByte(value: number): number | undefined {
180+
return Number.isInteger(value) && value >= 0 && value <= 0xff ? value : undefined;
181+
}
182+
155183
type Unquoted =
156184
/** The path the quoting names, and where the closing quote left off. */
157185
| { readonly kind: "path"; readonly value: string; readonly end: number }
@@ -181,7 +209,8 @@ function decodePath(bytes: readonly number[]): string | undefined {
181209
* the path this produces the path Git would open.
182210
*
183211
* Quoting Git rejects — an unterminated string, an escape it does not know, a
184-
* short octal escape — is not an error here either. Git falls back to reading
212+
* short octal escape, an octal escape whose leading digit is `4` or higher — is
213+
* not an error here either. Git falls back to reading
185214
* the line as ordinary text, so this says `literal` and the caller does the
186215
* same, which is what keeps a broken-looking line safely classified rather than
187216
* quietly resolved as something it is not.
@@ -224,16 +253,20 @@ function unquoteCStyle(text: string): Unquoted {
224253
const second = text[index];
225254
const third = text[index + 1];
226255
if (
227-
!OCTAL.test(escape) ||
256+
!OCTAL_LEAD.test(escape) ||
228257
second === undefined ||
229258
third === undefined ||
230259
!OCTAL.test(second) ||
231260
!OCTAL.test(third)
232261
) {
233262
return { kind: "literal" };
234263
}
264+
const octal = pathByte((Number(escape) << 6) | (Number(second) << 3) | Number(third));
265+
if (octal === undefined) {
266+
return { kind: "literal" };
267+
}
235268
index += 2;
236-
bytes.push((Number(escape) << 6) | (Number(second) << 3) | Number(third));
269+
bytes.push(octal);
237270
}
238271
}
239272

‎packages/workflow/tests/git-push.test.ts‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,118 @@ describe("workflow Git.Push object-source containment", () => {
10791079
});
10801080
});
10811081

1082+
/**
1083+
* An escape Git's grammar rejects, which a wider reading turns into a
1084+
* separator.
1085+
*
1086+
* `unquote_c_style()` cases an octal escape's leading digit as `0`–`3`,
1087+
* because an escape names one byte and `\400` is already 256. So `\457` is
1088+
* not a short escape or a near miss — it is an escape Git does not have, and
1089+
* a quoted entry containing one fails to unquote and is read as ordinary
1090+
* literal text instead.
1091+
*
1092+
* A reader that accepted the wider digit would compute 303 and truncate it
1093+
* into a byte, which is 47, which is `/`. It would see a path separator where
1094+
* Git sees no escape at all, and the same entry would then name two different
1095+
* object directories: one inside the authenticated database, reached through
1096+
* segments the extra separator created, and one Git actually traverses.
1097+
*
1098+
* Both are planted. The decoy chain satisfies the truncating reading; the
1099+
* literal spelling Git falls back to reaches a database outside the
1100+
* materialization entirely.
1101+
*/
1102+
it("refuses an alternates entry whose octal escape Git's own grammar rejects", function* () {
1103+
const root = yield* useStorageRoot();
1104+
const remote = yield* useBareRemote(REMOTE);
1105+
const path = runPath(root, "release-1.4");
1106+
const foreign = yield* useBareRemote({
1107+
commits: [{ message: "foreign", entries: [{ path: "foreign.txt", content: "foreign\n" }] }],
1108+
});
1109+
const foreignCommit = foreign.heads.get("main") ?? "";
1110+
1111+
// `\457` is invalid to Git and decodes to `/` under a truncating reading.
1112+
const escape = "\\457";
1113+
// Quoted: Git rejects the quoting and reads the whole line, including both
1114+
// quotes, as one relative path. Truncating: `a` `/` `b`, two segments.
1115+
const entry = `"a${escape}b/../../foreign"`;
1116+
// Where each reading lands, relative to the object database:
1117+
// truncating → <objects>/a/b/../../foreign → <objects>/foreign
1118+
// Git → <objects>/"a\457b/../../foreign" → <repository>/.git/foreign"
1119+
const literalSegment = `"a${escape}b`;
1120+
const foreignSlot = `foreign"`;
1121+
1122+
yield* withStorage(root, function* () {
1123+
const database = yield* createRun();
1124+
const counting = countingHost();
1125+
const failure = yield* raised(
1126+
runWorkflowDocument(
1127+
database,
1128+
document(
1129+
remote.locator,
1130+
`<Git.Switch branch="${BRANCH}" />`,
1131+
`<Plant />`,
1132+
`<Git.Push />`,
1133+
),
1134+
countingOptions(counting),
1135+
(run) =>
1136+
scoped(function* () {
1137+
yield* registerComponents([
1138+
plantObjectGraph(database, function* (workspace, checkout) {
1139+
const objects = `${checkout}/.git/objects`;
1140+
yield* workspace.filesystem.mkdir(`${objects}/info`, { recursive: true });
1141+
yield* workspace.filesystem.writeFile(`${objects}/info/alternates`, `${entry}\n`);
1142+
// The decoy the truncating reading resolves: the two segments
1143+
// its invented separator creates, and the directory the
1144+
// `../..` after them lands back on.
1145+
yield* workspace.filesystem.mkdir(`${objects}/a/b`, { recursive: true });
1146+
yield* workspace.filesystem.mkdir(`${objects}/foreign`, { recursive: true });
1147+
// The one segment Git's literal reading starts from, so its
1148+
// path resolves rather than merely failing to exist.
1149+
yield* workspace.filesystem.mkdir(`${objects}/${literalSegment}`, {
1150+
recursive: true,
1151+
});
1152+
// Where Git's literal reading ends: an object database
1153+
// outside the materialization, holding a commit this run
1154+
// never had.
1155+
yield* workspace.filesystem.symlink(
1156+
`${foreign.locator}/objects`,
1157+
`${checkout}/.git/${foreignSlot}`,
1158+
);
1159+
}),
1160+
]);
1161+
return yield* run();
1162+
}),
1163+
),
1164+
);
1165+
1166+
// One fixed, cause-free boundary failure, repeating nothing an author
1167+
// wrote: not the entry, not its escape, not where it pointed.
1168+
expect(String(failure)).toContain("executed and published nothing");
1169+
expect(String(failure)).not.toContain(entry);
1170+
expect(String(failure)).not.toContain(escape);
1171+
expect(String(failure)).not.toContain(foreign.locator);
1172+
expect(String(failure)).not.toContain(foreignSlot);
1173+
1174+
expect(subcommands(counting.counters)).not.toContain("ls-remote");
1175+
expect(subcommands(counting.counters)).not.toContain("push");
1176+
expect(yield* gitHostEvents(database)).toHaveLength(0);
1177+
expect(publishedRoots(path)).toBe(3);
1178+
expect(committedRoot(path)).toBe(latestRoot(path));
1179+
expect(remoteRefs(remote).has(DESTINATION)).toBe(false);
1180+
1181+
// The escape really was reachable, and reachable only through Git's own
1182+
// reading of it: ordinary Git in the same export rejects the quoting,
1183+
// resolves the literal spelling, links the outside database and answers
1184+
// for a commit this run never held. The decoy chain holds no objects, so
1185+
// nothing but that fallback explains this.
1186+
const reached = yield* inCheckout(database, yield* checkoutPath(database), function* (run) {
1187+
const top = (yield* run(["rev-parse", "--show-toplevel"])).trim();
1188+
return nativeGit(["cat-file", "-t", foreignCommit], top, top);
1189+
});
1190+
expect(reached).toBe("commit");
1191+
});
1192+
});
1193+
10821194
/**
10831195
* The link an author can write, which needs no chain at all.
10841196
*

0 commit comments

Comments
 (0)