Skip to content

Commit b22afd6

Browse files
committed
fix(engine): reject malformed JPEG input before encoding
1 parent 3a7fcd1 commit b22afd6

5 files changed

Lines changed: 146 additions & 9 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// A 64x64 JPEG encoded by FFmpeg; contains real DQT, DHT, SOF and scan data.
2+
export const validJpeg = Buffer.from(
3+
"/9j/4AAQSkZJRgABAgAAAQABAAD//gAQTGF2YzYyLjI4LjEwMQD/2wBDAAgEBAQEBAUFBQUFBQYGBgYGBgYGBgYGBgYHBwcICAgHBwcGBgcHCAgICAkJCQgICAgJCQoKCgwMCwsODg4RERT/xABNAAEBAAAAAAAAAAAAAAAAAAAABgEBAQEAAAAAAAAAAAAAAAAAAAYHEAEAAAAAAAAAAAAAAAAAAAAAEQEAAAAAAAAAAAAAAAAAAAAA/8AAEQgAQABAAwEiAAIRAAMRAP/aAAwDAQACEQMRAD8AiwEm38AAAAAAAAAAAAAAAAAAAAAAAAAAAAAB/9k=",
4+
"base64",
5+
);

‎packages/engine/src/services/streamingEncoder.test.ts‎

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
* master-display / max-cll and ship as SDR BT.2020 again.
1111
*/
1212

13+
import { validJpeg } from "./__fixtures__/jpeg.js";
14+
1315
import { EventEmitter } from "events";
1416
import { mkdtempSync } from "fs";
1517
import { tmpdir } from "os";
@@ -711,6 +713,41 @@ describe("spawnStreamingEncoder lifecycle and cleanup", () => {
711713
expect(threw).toBe(false);
712714
});
713715

716+
it("rejects a malformed JPEG before writing it and reports its input frame index", async () => {
717+
const { spawn, calls } = createSpawnSpy();
718+
vi.resetModules();
719+
vi.doMock("child_process", () => ({ spawn }));
720+
const { spawnStreamingEncoder } = await import("./streamingEncoder.js");
721+
const dir = mkdtempSync(join(tmpdir(), "se-jpeg-"));
722+
const encoder = await spawnStreamingEncoder(join(dir, "out.mp4"), baseOptions);
723+
const proc = calls[0]!.proc;
724+
const write = vi.spyOn(proc.stdin, "write");
725+
expect(await encoder.writeFrame(validJpeg)).toBe(true);
726+
const malformed = Buffer.from(validJpeg);
727+
malformed[malformed.indexOf(Buffer.from([0xff, 0xdb])) + 4] = 0x20;
728+
await expect(encoder.writeFrame(malformed)).rejects.toThrow(
729+
"Invalid JPEG input at frame 1: invalid JPEG DQT precision 2",
730+
);
731+
expect(write).toHaveBeenCalledTimes(1);
732+
process.nextTick(() => proc.emit("close", 0));
733+
await encoder.close();
734+
});
735+
736+
it.each([
737+
{ ...baseOptions, imageFormat: "png" as const },
738+
{ ...baseOptions, rawInputFormat: "rgb48le" as const },
739+
])("does not apply JPEG validation to PNG or raw frames", async (options) => {
740+
const { spawn, calls } = createSpawnSpy();
741+
vi.resetModules();
742+
vi.doMock("child_process", () => ({ spawn }));
743+
const { spawnStreamingEncoder } = await import("./streamingEncoder.js");
744+
const dir = mkdtempSync(join(tmpdir(), "se-non-jpeg-"));
745+
const encoder = await spawnStreamingEncoder(join(dir, "out.mp4"), options);
746+
expect(await encoder.writeFrame(Buffer.from([0]))).toBe(true);
747+
process.nextTick(() => calls[0]!.proc.emit("close", 0));
748+
await encoder.close();
749+
});
750+
714751
it("writeFrame returns false after ffmpeg has exited", async () => {
715752
const { spawn, calls } = createSpawnSpy();
716753
vi.resetModules();
@@ -720,7 +757,7 @@ describe("spawnStreamingEncoder lifecycle and cleanup", () => {
720757
const dir = mkdtempSync(join(tmpdir(), "se-writefail-"));
721758
const encoder = await spawnStreamingEncoder(join(dir, "out.mp4"), baseOptions);
722759

723-
expect(await encoder.writeFrame(Buffer.from([0]))).toBe(true);
760+
expect(await encoder.writeFrame(validJpeg)).toBe(true);
724761

725762
const proc = calls[0]!.proc;
726763
await new Promise<void>((resolve) => {
@@ -730,7 +767,7 @@ describe("spawnStreamingEncoder lifecycle and cleanup", () => {
730767
});
731768
});
732769

733-
expect(await encoder.writeFrame(Buffer.from([0]))).toBe(false);
770+
expect(await encoder.writeFrame(validJpeg)).toBe(false);
734771
});
735772

736773
it("waits for child close when stdin dies first so the interruption reason is observable", async () => {
@@ -744,7 +781,7 @@ describe("spawnStreamingEncoder lifecycle and cleanup", () => {
744781
const proc = calls[0]!.proc;
745782
proc.stdin.destroyed = true;
746783

747-
const writePromise = encoder.writeFrame(Buffer.from([0]));
784+
const writePromise = encoder.writeFrame(validJpeg);
748785
await expect(resolveWithin(writePromise, 10)).resolves.toBe("timeout");
749786

750787
proc.stderr.emit("data", Buffer.from("Exiting normally, received signal 15.\n"));
@@ -766,7 +803,7 @@ describe("spawnStreamingEncoder lifecycle and cleanup", () => {
766803
const proc = calls[0]!.proc;
767804
proc.stdin.write = (_chunk: Buffer): boolean => false;
768805

769-
const writeResult = encoder.writeFrame(Buffer.from([1])) as unknown;
806+
const writeResult = encoder.writeFrame(validJpeg) as unknown;
770807
expect(writeResult).toBeInstanceOf(Promise);
771808

772809
const writePromise = writeResult as Promise<boolean>;
@@ -804,7 +841,7 @@ describe("spawnStreamingEncoder lifecycle and cleanup", () => {
804841
proc.stdin.write = (_chunk: Buffer): boolean => false;
805842

806843
for (let i = 0; i < 12; i++) {
807-
const writePromise = encoder.writeFrame(Buffer.from([i]));
844+
const writePromise = encoder.writeFrame(validJpeg);
808845

809846
await Promise.resolve();
810847
expect(proc.stdin.listenerCount("drain")).toBe(baselineDrainListeners + 1);
@@ -833,7 +870,7 @@ describe("spawnStreamingEncoder lifecycle and cleanup", () => {
833870
const proc = calls[0]!.proc;
834871
proc.stdin.write = (_chunk: Buffer): boolean => false;
835872

836-
const writeResult = encoder.writeFrame(Buffer.from([1])) as unknown;
873+
const writeResult = encoder.writeFrame(validJpeg) as unknown;
837874
expect(writeResult).toBeInstanceOf(Promise);
838875

839876
const writePromise = writeResult as Promise<boolean>;
@@ -872,7 +909,7 @@ describe("spawnStreamingEncoder lifecycle and cleanup", () => {
872909
return false;
873910
};
874911

875-
const writePromise = encoder.writeFrame(Buffer.from([1]));
912+
const writePromise = encoder.writeFrame(validJpeg);
876913

877914
await expect(resolveWithin(writePromise)).resolves.toBe(false);
878915
expect(encoder.getExitStatus()).toBe("error");
@@ -927,7 +964,7 @@ describe("spawnStreamingEncoder lifecycle and cleanup", () => {
927964
// progressing" capture the encoder must still be alive. The old total-
928965
// render timeout would have fired SIGTERM at ~1000ms.
929966
for (let i = 0; i < 9; i++) {
930-
await encoder.writeFrame(Buffer.from([i]));
967+
await encoder.writeFrame(validJpeg);
931968
vi.advanceTimersByTime(900);
932969
}
933970
expect(proc.kill).not.toHaveBeenCalled();
@@ -964,7 +1001,7 @@ describe("spawnStreamingEncoder lifecycle and cleanup", () => {
9641001
// A buffered write should remain pending and must NOT reset the timer.
9651002
// The 1000ms timer (last reset on spawn) therefore elapses while the
9661003
// caller is correctly back-pressured on the first frame.
967-
const writePromise = encoder.writeFrame(Buffer.from([0]));
1004+
const writePromise = encoder.writeFrame(validJpeg);
9681005
await Promise.resolve();
9691006

9701007
vi.advanceTimersByTime(1100);

‎packages/engine/src/services/streamingEncoder.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* exposes an async `writeFrame(buffer)` + `close()` API.
1414
*/
1515

16+
import { jpegInputError } from "../utils/jpegInput.js";
1617
import { spawn, type ChildProcess } from "child_process";
1718
import { once } from "events";
1819
import { trackChildProcess } from "../utils/processTracker.js";
@@ -537,6 +538,7 @@ export async function spawnStreamingEncoder(
537538
}
538539
};
539540

541+
let inputFrameIndex = 0;
540542
const encoder: StreamingEncoder = {
541543
writeFrame: async (buffer: Buffer): Promise<boolean> => {
542544
const stdin = ffmpeg.stdin;
@@ -557,7 +559,12 @@ export async function spawnStreamingEncoder(
557559
// so without this copy the pipe would read partially-overwritten data
558560
// and flicker.
559561
const copy = Buffer.from(buffer);
562+
if (!options.rawInputFormat && (options.imageFormat ?? "jpeg") === "jpeg") {
563+
const error = jpegInputError(copy);
564+
if (error) throw new Error(`Invalid JPEG input at frame ${inputFrameIndex}: ${error}`);
565+
}
560566
const accepted = stdin.write(copy);
567+
inputFrameIndex++;
561568
// Reset inactivity timer immediately ONLY on `accepted === true`. `true`
562569
// means the write went through to the kernel pipe without buffering in
563570
// Node — proof FFmpeg is actually consuming. `false` means Node's writable
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, expect, it } from "vitest";
2+
import { validJpeg } from "../services/__fixtures__/jpeg.js";
3+
import { jpegInputError } from "./jpegInput.js";
4+
5+
function withDqt(descriptor: number, bytes: number): Buffer {
6+
return Buffer.concat([
7+
Buffer.from([0xff, 0xd8, 0xff, 0xdb, 0, bytes + 3, descriptor]),
8+
Buffer.alloc(bytes, 1),
9+
Buffer.from([0xff, 0xda, 0, 2]),
10+
]);
11+
}
12+
13+
describe("JPEG input diagnostics", () => {
14+
it("accepts an actual FFmpeg-encoded JPEG", () => {
15+
expect(jpegInputError(validJpeg)).toBeUndefined();
16+
});
17+
it("identifies the reproduced invalid DQT precision", () => {
18+
const malformed = Buffer.from(validJpeg);
19+
const dqt = malformed.indexOf(Buffer.from([0xff, 0xdb]));
20+
expect(dqt).toBeGreaterThan(0);
21+
malformed[dqt + 4] = 0x20;
22+
expect(jpegInputError(malformed)).toBe("invalid JPEG DQT precision 2");
23+
});
24+
it.each([0, 1])("accepts DQT precision %i", (precision) => {
25+
expect(jpegInputError(withDqt(precision << 4, precision === 0 ? 64 : 128))).toBeUndefined();
26+
});
27+
it("rejects a truncated DQT table within a complete segment", () => {
28+
expect(jpegInputError(withDqt(0, 63))).toBe("truncated JPEG DQT table");
29+
});
30+
it.each([
31+
[Buffer.alloc(0), "empty frame"],
32+
[Buffer.from([0]), "missing JPEG SOI marker"],
33+
[Buffer.from([0xff, 0xd8, 0xff, 0xdb, 0, 67]), "invalid JPEG segment length"],
34+
[Buffer.from([0xff, 0xd8, 0xff]), "truncated JPEG marker"],
35+
])("reports malformed input without reading past its buffer", (buffer, message) => {
36+
expect(jpegInputError(buffer)).toBe(message);
37+
});
38+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/** Check JPEG headers before image2pipe; entropy-coded pixels still belong to FFmpeg. */
2+
export function jpegInputError(buffer: Buffer): string | undefined {
3+
if (buffer.length === 0) return "empty frame";
4+
if (buffer[0] !== 0xff || buffer[1] !== 0xd8) return "missing JPEG SOI marker";
5+
let offset = 2;
6+
while (offset < buffer.length) {
7+
const segment = readSegment(buffer, offset);
8+
if (typeof segment === "string") return segment;
9+
const { marker, start, end } = segment;
10+
if (marker === 0xdb) {
11+
const error = quantizationTableError(buffer, start, end);
12+
if (error) return error;
13+
}
14+
// Do not scan compressed pixel data as markers (byte stuffing/restarts).
15+
if (marker === 0xda) return undefined;
16+
offset = end;
17+
}
18+
return "missing JPEG scan";
19+
}
20+
21+
function readSegment(
22+
buffer: Buffer,
23+
offset: number,
24+
): { marker: number; start: number; end: number } | string {
25+
if (buffer[offset++] !== 0xff) return "invalid JPEG marker";
26+
while (buffer[offset] === 0xff) offset++;
27+
const marker = buffer[offset++];
28+
if (marker === undefined) return "truncated JPEG marker";
29+
if (marker === 0x01) return { marker, start: offset, end: offset };
30+
if (offset + 2 > buffer.length) return "truncated JPEG segment length";
31+
const length = buffer.readUInt16BE(offset);
32+
const end = offset + length;
33+
if (length < 2 || end > buffer.length) return "invalid JPEG segment length";
34+
return { marker, start: offset + 2, end };
35+
}
36+
37+
function quantizationTableError(buffer: Buffer, start: number, end: number): string | undefined {
38+
if (start === end) return "empty JPEG DQT segment";
39+
let offset = start;
40+
while (offset < end) {
41+
const descriptor = buffer[offset++];
42+
if (descriptor === undefined) return "truncated JPEG DQT descriptor";
43+
const precision = descriptor >> 4;
44+
if (precision > 1) return `invalid JPEG DQT precision ${precision}`;
45+
if ((descriptor & 0x0f) > 3) return "invalid JPEG DQT table id";
46+
offset += precision === 0 ? 64 : 128;
47+
if (offset > end) return "truncated JPEG DQT table";
48+
}
49+
return undefined;
50+
}

0 commit comments

Comments
 (0)