Skip to content

Commit b1c80c3

Browse files
committed
fix(exec): preserve heredoc line across splice; array-join output
Review feedback on #71154: - Do not reset the accumulated heredoc logical line when splicing a `\<newline>` inside an unquoted heredoc body. Bash joins the lines, so a terminator whose characters are split across a continuation (`EO\<LF>F` for `<<EOF`) should still terminate the heredoc, and conversely a continued body line whose tail happens to equal the delimiter (`foo \<LF>EOF`) should *not* terminate. The previous code cleared `heredocLine` on splice and mis-classified both edges. (Greptile / Codex reviews.) - Build the output and heredoc-line buffers via `string[]` + `join("")` instead of `+=` concatenation in a loop, so very large commands (big heredoc bodies) stay linear regardless of engine concatenation strategy. (Aisle security review.) Tests extended: new cases for the `EO\<LF>F` terminator and the `foo \<LF>EOF` non-terminator edges.
1 parent 4e64cf5 commit b1c80c3

2 files changed

Lines changed: 55 additions & 27 deletions

File tree

‎src/infra/exec-approvals-allowlist.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ describe("joinShellLineContinuations", () => {
118118
);
119119
});
120120

121+
it("recognizes a terminator whose letters are split by a continuation", () => {
122+
// bash: the logical line after splicing is `EOF`, so the heredoc terminates.
123+
expect(joinShellLineContinuations("cat <<EOF\nbody\nEO\\\nF\ntail\n")).toBe(
124+
"cat <<EOF\nbody\nEOF\ntail\n",
125+
);
126+
});
127+
128+
it("does not terminate when a continuation joins text into the delimiter line", () => {
129+
// bash: the logical line is `foo EOF`, which is NOT the delimiter; keep
130+
// consuming the body until a standalone `EOF` arrives.
131+
expect(joinShellLineContinuations("cat <<EOF\nfoo \\\nEOF\nEOF\ntail\n")).toBe(
132+
"cat <<EOF\nfoo EOF\nEOF\ntail\n",
133+
);
134+
});
135+
121136
it("returns the original command for unterminated quotes", () => {
122137
const input = "echo 'unterminated \\\n";
123138
expect(joinShellLineContinuations(input)).toBe(input);

‎src/infra/exec-approvals-analysis.ts‎

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,16 @@ function parseHeredocDelimiter(source: string, start: number): ParsedHeredocDeli
147147
export function joinShellLineContinuations(command: string): string {
148148
type HeredocSpec = { delimiter: string; stripTabs: boolean; quoted: boolean };
149149

150-
let out = "";
150+
// Build output via chunked array + `join("")` at the end so concatenation
151+
// stays linear for large commands (e.g. big heredoc bodies) rather than
152+
// relying on engine-specific optimizations for `+=` concatenation.
153+
const out: string[] = [];
154+
const heredocLine: string[] = [];
151155
let inSingle = false;
152156
let inDouble = false;
153157
let inComment = false;
154158
const pendingHeredocs: HeredocSpec[] = [];
155159
let inHeredocBody = false;
156-
let heredocLine = "";
157160

158161
const isContinuation = (i: number): 0 | 2 | 3 => {
159162
if (command[i] !== "\\") {
@@ -169,6 +172,12 @@ export function joinShellLineContinuations(command: string): string {
169172
return 0;
170173
};
171174

175+
const resetHeredocLine = () => {
176+
heredocLine.length = 0;
177+
};
178+
179+
const currentHeredocLine = (): string => heredocLine.join("");
180+
172181
let i = 0;
173182
while (i < command.length) {
174183
const ch = command[i];
@@ -179,33 +188,39 @@ export function joinShellLineContinuations(command: string): string {
179188
if (current && !current.quoted) {
180189
const skip = isContinuation(i);
181190
if (skip) {
182-
heredocLine = "";
191+
// Splice: drop the backslash-newline without resetting the logical
192+
// heredoc line. A continued line joins into the next physical line,
193+
// so characters accumulated so far still belong to this logical
194+
// line — needed to correctly classify `foo \<LF>EOF` (does not
195+
// terminate <<EOF, since the logical line is "foo EOF") and
196+
// `EO\<LF>F` (does terminate <<EOF, since the logical line is "EOF").
183197
i += skip;
184198
continue;
185199
}
186200
}
187201
if (ch === "\n" || ch === "\r") {
188202
if (current) {
189-
const line = current.stripTabs ? heredocLine.replace(/^\t+/, "") : heredocLine;
203+
const raw = currentHeredocLine();
204+
const line = current.stripTabs ? raw.replace(/^\t+/, "") : raw;
190205
if (line === current.delimiter) {
191206
pendingHeredocs.shift();
192207
if (pendingHeredocs.length === 0) {
193208
inHeredocBody = false;
194209
}
195210
}
196211
}
197-
heredocLine = "";
198-
out += ch;
212+
resetHeredocLine();
213+
out.push(ch);
199214
if (ch === "\r" && next === "\n") {
200-
out += "\n";
215+
out.push("\n");
201216
i += 2;
202217
} else {
203218
i += 1;
204219
}
205220
continue;
206221
}
207-
heredocLine += ch;
208-
out += ch;
222+
heredocLine.push(ch);
223+
out.push(ch);
209224
i += 1;
210225
continue;
211226
}
@@ -214,7 +229,7 @@ export function joinShellLineContinuations(command: string): string {
214229
if (ch === "\n" || ch === "\r") {
215230
inComment = false;
216231
}
217-
out += ch;
232+
out.push(ch);
218233
i += 1;
219234
continue;
220235
}
@@ -223,7 +238,7 @@ export function joinShellLineContinuations(command: string): string {
223238
if (ch === "'") {
224239
inSingle = false;
225240
}
226-
out += ch;
241+
out.push(ch);
227242
i += 1;
228243
continue;
229244
}
@@ -235,15 +250,14 @@ export function joinShellLineContinuations(command: string): string {
235250
continue;
236251
}
237252
if (ch === "\\" && next !== undefined && DOUBLE_QUOTE_ESCAPES.has(next)) {
238-
out += ch;
239-
out += next;
253+
out.push(ch, next);
240254
i += 2;
241255
continue;
242256
}
243257
if (ch === '"') {
244258
inDouble = false;
245259
}
246-
out += ch;
260+
out.push(ch);
247261
i += 1;
248262
continue;
249263
}
@@ -255,45 +269,44 @@ export function joinShellLineContinuations(command: string): string {
255269
}
256270

257271
if (ch === "\\" && next !== undefined) {
258-
out += ch;
259-
out += next;
272+
out.push(ch, next);
260273
i += 2;
261274
continue;
262275
}
263276

264277
if (ch === "'") {
265278
inSingle = true;
266-
out += ch;
279+
out.push(ch);
267280
i += 1;
268281
continue;
269282
}
270283
if (ch === '"') {
271284
inDouble = true;
272-
out += ch;
285+
out.push(ch);
273286
i += 1;
274287
continue;
275288
}
276289

277290
if (isShellCommentStart(command, i)) {
278291
inComment = true;
279-
out += ch;
292+
out.push(ch);
280293
i += 1;
281294
continue;
282295
}
283296

284297
if (ch === "<" && next === "<") {
285-
out += "<<";
298+
out.push("<<");
286299
let scanIndex = i + 2;
287300
let stripTabs = false;
288301
if (command[scanIndex] === "-") {
289302
stripTabs = true;
290-
out += "-";
303+
out.push("-");
291304
scanIndex += 1;
292305
}
293306
const parsed = parseHeredocDelimiter(command, scanIndex);
294307
if (parsed) {
295308
pendingHeredocs.push({ delimiter: parsed.delimiter, stripTabs, quoted: parsed.quoted });
296-
out += command.slice(scanIndex, parsed.end);
309+
out.push(command.slice(scanIndex, parsed.end));
297310
i = parsed.end;
298311
} else {
299312
i = scanIndex;
@@ -302,28 +315,28 @@ export function joinShellLineContinuations(command: string): string {
302315
}
303316

304317
if (ch === "\n" || ch === "\r") {
305-
out += ch;
318+
out.push(ch);
306319
if (ch === "\r" && next === "\n") {
307-
out += "\n";
320+
out.push("\n");
308321
i += 2;
309322
} else {
310323
i += 1;
311324
}
312325
if (pendingHeredocs.length > 0) {
313326
inHeredocBody = true;
314-
heredocLine = "";
327+
resetHeredocLine();
315328
}
316329
continue;
317330
}
318331

319-
out += ch;
332+
out.push(ch);
320333
i += 1;
321334
}
322335

323336
if (inSingle || inDouble || (inHeredocBody && pendingHeredocs.length > 0)) {
324337
return command;
325338
}
326-
return out;
339+
return out.join("");
327340
}
328341

329342
function splitShellPipeline(command: string): { ok: boolean; reason?: string; segments: string[] } {

0 commit comments

Comments
 (0)