Skip to content

Commit 7b46784

Browse files
committed
Validate draft bodies early and allow final IDAT padding
1 parent e60c7c4 commit 7b46784

3 files changed

Lines changed: 90 additions & 13 deletions

File tree

‎examples/repl-bench/lib/publication-files-deno.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ const adam7 = [
114114
[0, 1, 1, 2],
115115
];
116116

117-
function validScanlines(header: DataView, compressed: Uint8Array): boolean {
117+
function validScanlines(header: DataView, compressed: Uint8Array, finalIdatStart: number): boolean {
118118
const width = header.getUint32(16);
119119
const height = header.getUint32(20);
120120
const depth = header.getUint8(24);
@@ -155,7 +155,8 @@ function validScanlines(header: DataView, compressed: Uint8Array): boolean {
155155
!(decoded.buffer instanceof Uint8Array) ||
156156
!("engine" in decoded) ||
157157
!(decoded.engine instanceof Inflate) ||
158-
decoded.engine.bytesWritten !== compressed.length ||
158+
// PNG allows unused bytes only at the end of the final IDAT chunk.
159+
decoded.engine.bytesWritten < finalIdatStart ||
159160
decoded.buffer.length !== expected
160161
) {
161162
return false;
@@ -190,14 +191,16 @@ export function png(bytes: Uint8Array): boolean {
190191
}
191192
const compressed = new Uint8Array(bytes.length);
192193
let compressedLength = 0;
194+
let finalIdatStart = 0;
193195
for (let at = 8; at + 12 <= bytes.length; ) {
194196
const length = view.getUint32(at);
195197
const type = view.getUint32(at + 4);
196198
const end = at + length + 12;
197199
if (end > bytes.length || crc32(bytes.subarray(at + 4, end - 4)) !== view.getUint32(end - 4)) {
198200
return false;
199201
}
200-
if (type === 0x49444154 && length > 0) {
202+
if (type === 0x49444154) {
203+
finalIdatStart = compressedLength;
201204
compressed.set(bytes.subarray(at + 8, end - 4), compressedLength);
202205
compressedLength += length;
203206
}
@@ -206,7 +209,7 @@ export function png(bytes: Uint8Array): boolean {
206209
compressedLength > 0 &&
207210
length === 0 &&
208211
end === bytes.length &&
209-
validScanlines(view, compressed.subarray(0, compressedLength))
212+
validScanlines(view, compressed.subarray(0, compressedLength), finalIdatStart)
210213
);
211214
}
212215
at = end;

‎examples/repl-bench/lib/publication.ts‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,30 @@ import {
2020
} from "./publication-markdown.ts";
2121
import { BODY_LIMIT, loadEvidence, type Evidence } from "./publication-receipt.ts";
2222

23+
type Body = ReturnType<typeof inspectBody>;
24+
25+
function renderDescription(evidence: Evidence, { before, after, slots }: Body): string {
26+
const body = before + renderEvidence(evidence, slots) + after;
27+
if (body.length > BODY_LIMIT) {
28+
throw new Error("The proposed draft description exceeds the GitHub body limit.");
29+
}
30+
return body;
31+
}
32+
2333
export function* preparePublication(path: string, mode: "dry-run" | "publish") {
2434
const evidence = yield* loadEvidence(resolve(yield* cwd(), path));
2535
yield* checkCli();
2636
const { target, pins, identity, sources } = evidence;
2737
if (mode === "publish") {
2838
if (target.kind === "create") {
29-
yield* checkCheckout(evidence);
30-
if (inspectBody(evidence, target.body).present) {
39+
const prepared = inspectBody(evidence, target.body);
40+
if (prepared.present) {
3141
throw new Error(
3242
"Supply prepared draft prose without an evidence section, or resume by PR number.",
3343
);
3444
}
45+
renderDescription(evidence, prepared);
46+
yield* checkCheckout(evidence);
3547
}
3648
return {
3749
kind: "publish",
@@ -50,7 +62,8 @@ export function* preparePublication(path: string, mode: "dry-run" | "publish") {
5062
: new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
5163
yield* readRegular(resolve(evidence.directory, evidence.previewBody), BODY_LIMIT),
5264
);
53-
const { before, after, slots } = inspectBody(evidence, body);
65+
const proposed = inspectBody(evidence, body);
66+
const { slots } = proposed;
5467
if (
5568
slots.some((url, index) => url !== pending(evidence.slots[index]) && !nativeAttachment(url))
5669
) {
@@ -59,7 +72,7 @@ export function* preparePublication(path: string, mode: "dry-run" | "publish") {
5972
return {
6073
kind: "dry-run",
6174
identity,
62-
markdown: before + renderEvidence(evidence, slots) + after,
75+
markdown: renderDescription(evidence, proposed),
6376
images: evidence.slots.flatMap((slot, index) =>
6477
slots[index] === pending(slot) ? [slot.path] : [],
6578
),
@@ -68,7 +81,6 @@ export function* preparePublication(path: string, mode: "dry-run" | "publish") {
6881
};
6982
}
7083

71-
type Body = ReturnType<typeof inspectBody>;
7284
type SlotState = {
7385
id: string;
7486
sha256: string;
@@ -154,10 +166,7 @@ export function* publishEvidence(
154166
: {}),
155167
};
156168
}
157-
const body = current.before + renderEvidence(evidence, current.slots) + current.after;
158-
if (body.length > BODY_LIMIT) {
159-
throw new Error("The proposed draft description exceeds the GitHub body limit.");
160-
}
169+
const body = renderDescription(evidence, current);
161170

162171
const editExit = yield* scoped(function* () {
163172
const scratch = yield* useTempDirectory("xmd-repl-evidence-");

‎examples/repl-bench/tests/publish-evidence.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const urls = images.map(
5454
(_, index) =>
5555
`https://github.com/user-attachments/assets/${index + 1}0000000-0000-0000-0000-000000000000`,
5656
);
57+
const oversizedProse = "x".repeat(65_500);
5758
const beforeProse = "# Prepared change\r\n\r\n Leading text \n\n";
5859
const afterProse = "\r\n\r\n More prose\n\n\n";
5960

@@ -353,6 +354,42 @@ describe("REPL UI evidence publication", () => {
353354
expect(host.events).toEqual(["help", "help"]);
354355
});
355356

357+
it("rejects a generated description over the body limit in preview and numbered publish", function* () {
358+
const input = yield* useFixture();
359+
const host = modeledHost(() => snapshot(input, oversizedProse));
360+
yield* writeTextFile(join(input.directory, "preview.md"), oversizedProse);
361+
const diagnostic = "The proposed draft description exceeds the GitHub body limit.";
362+
expect(String(yield* raised(run(input, host, "dry-run")))).toContain(diagnostic);
363+
expect(host.events).toEqual(["help"]);
364+
host.events.length = 0;
365+
expect(String(yield* raised(run(input, host)))).toContain(diagnostic);
366+
expect(host.events).toEqual(["help", "read"]);
367+
});
368+
369+
it("publishes a PNG with final IDAT padding without changing its approved bytes", function* () {
370+
const input = yield* useFixture();
371+
const padded = Uint8Array.from(
372+
atob(
373+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAEElEQVR4nGP4z8AAAAMBAQAAAAAAOUhUywAAAABJRU5ErkJggg==",
374+
),
375+
(character) => character.charCodeAt(0),
376+
);
377+
const digest = sha256(padded);
378+
yield* until(writeFile(input.paths[0], padded));
379+
yield* writeTextFile(
380+
input.path,
381+
JSON.stringify({
382+
...input.receipt,
383+
captures: [{ ...input.receipt.captures[0], sha256: digest }, input.receipt.captures[1]],
384+
}),
385+
);
386+
const host = modeledHost(() => snapshot(input));
387+
const result = yield* run(input, host);
388+
expect(result.status).toBe("complete");
389+
expect(host.edits[0].images[0].sha256).toBe(digest);
390+
expect(host.bytes.get(urls[0])).toEqual(padded);
391+
});
392+
356393
it("publishes only staged images and preserves bytes on both sides of the evidence", function* () {
357394
const input = yield* useFixture();
358395
const section = yield* preview(input);
@@ -645,6 +682,34 @@ describe("REPL UI evidence publication", () => {
645682

646683
host.events.length = 0;
647684
host.help = true;
685+
yield* writeTextFile(
686+
input.path,
687+
JSON.stringify({
688+
...input.receipt,
689+
target: {
690+
kind: "create",
691+
title: "Prepared title",
692+
body: oversizedProse,
693+
draftRecord: "draft.json",
694+
},
695+
}),
696+
);
697+
expect(String(yield* raised(run(input, host, "publish", setup)))).toContain(
698+
"The proposed draft description exceeds the GitHub body limit.",
699+
);
700+
expect(host.events).toEqual(["help"]);
701+
expect(git.counters.commands).toEqual([]);
702+
expect(creations(store)).toBe(0);
703+
expect(remoteBranch(remote, "feature")).toBeUndefined();
704+
705+
host.events.length = 0;
706+
yield* writeTextFile(
707+
input.path,
708+
JSON.stringify({
709+
...input.receipt,
710+
target: { kind: "create", title: "Prepared title", body, draftRecord: "draft.json" },
711+
}),
712+
);
648713
const result = yield* run(input, host, "publish", setup);
649714
expect(creations(store)).toBe(1);
650715
expect(store.pullRequests[0]).toMatchObject({ title: "Prepared title", body, draft: true });

0 commit comments

Comments
 (0)