Skip to content

Commit 61d65e7

Browse files
jrusso1020claude
andauthored
fix(aws-lambda): match the shell to its own pin, ship only hashed files, refuse the shell ZIP in CDK and SAM (#8173)
* fix(aws-lambda): match the shell to its own pin, ship only hashed files, refuse the shell ZIP in CDK and SAM Review follow-ups to the pinned shell route: - build-zip --source=chrome-headless-shell accepts the folder only when its digest is pinned for this version, not for any pinned build. - It leaves out the files the digest skips (debug.log and chrome_debug.log, which Chrome writes as it runs), so nothing unhashed ships. - The CDK construct throws on chromeSource "chrome-headless-shell" and the SAM template allows only sparticuz, as lambda deploy already refused it: a shell ZIP cannot fit a function. The CHROME_PATH wiring for that ZIP is gone with it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(aws-lambda): raise the packed size budget to 82,000 bytes The shipped build-zip script now checks the shell against its own version's pin and skips the files the digest leaves out. That takes the tarball to 81,378 bytes in CI, over the 80,500 budget's 1% tolerance; main was already within about 200 bytes of it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9acf268 commit 61d65e7

10 files changed

Lines changed: 72 additions & 92 deletions

File tree

‎examples/aws-lambda/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ with `--profile <name>` or `AWS_PROFILE=<name>`.
200200
| `LambdaMemoryMb` | `10240` | Lambda memory; Lambda allocates CPU proportionally. 10 GB recommended for 1080p. |
201201
| `LambdaTimeoutSec` | `900` | Per-invocation timeout. 15 min is Lambda's hard ceiling. |
202202
| `ReservedConcurrency` | `-1` | Hard cap on simultaneous Lambda invocations. `-1` = unreserved. Set to e.g. `50` to bound cost. |
203-
| `ChromeSource` | `sparticuz` | Must match the `--source=` flag passed to `build-zip.ts`; a shell ZIP is over Lambda's limit. |
203+
| `ChromeSource` | `sparticuz` | Only `sparticuz`: a ZIP built with `--source=chrome-headless-shell` is over Lambda's limit. |
204204
| `ChunkInvocationAlarmThreshold` | `1000` | CloudWatch alarm threshold (RenderChunk invocations per hour). |
205205

206206
## Cleanup

‎examples/aws-lambda/template.yaml‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ Parameters:
4949
ChromeSource:
5050
Type: String
5151
Default: sparticuz
52-
AllowedValues: [sparticuz, chrome-headless-shell]
52+
AllowedValues: [sparticuz]
5353
Description: >-
5454
Which Chrome runtime the bundled ZIP was built with. Must match the
5555
`--source=` flag passed to `build-zip.ts`. The handler reads this
56-
via the HYPERFRAMES_LAMBDA_CHROME_SOURCE env var at boot. A
57-
chrome-headless-shell ZIP is over Lambda's unzipped limit today; the
58-
arm64 video image always runs its own chrome-headless-shell.
56+
via the HYPERFRAMES_LAMBDA_CHROME_SOURCE env var at boot. Only
57+
sparticuz: a chrome-headless-shell ZIP is over Lambda's unzipped
58+
limit, and the arm64 video image runs its own chrome-headless-shell.
5959
6060
ChunkInvocationAlarmThreshold:
6161
Type: Number
@@ -77,7 +77,6 @@ Parameters:
7777
Conditions:
7878
HasReservedConcurrency: !Not [!Equals [!Ref ReservedConcurrency, -1]]
7979
HasVideoFunction: !Not [!Equals [!Ref VideoImageUri, ""]]
80-
BundlesChromeHeadlessShell: !Equals [!Ref ChromeSource, chrome-headless-shell]
8180

8281
Globals:
8382
Function:
@@ -97,11 +96,6 @@ Globals:
9796
Variables:
9897
NODE_OPTIONS: "--enable-source-maps"
9998
HYPERFRAMES_LAMBDA_CHROME_SOURCE: !Ref ChromeSource
100-
# Where build-zip.ts --source=chrome-headless-shell puts the shell (BUNDLED_SHELL_PATH).
101-
HYPERFRAMES_LAMBDA_CHROME_PATH: !If
102-
- BundlesChromeHeadlessShell
103-
- /var/task/chrome-headless-shell-linux64/chrome-headless-shell
104-
- !Ref AWS::NoValue
10599

106100
Resources:
107101
# ── S3 bucket for plan tarballs, chunk outputs, and final renders ───────

‎packages/aws-lambda/README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ bun run --cwd packages/aws-lambda build:zip -- --source=chrome-headless-shell
104104

105105
The handler reads `HYPERFRAMES_LAMBDA_CHROME_SOURCE` at boot, and for
106106
`chrome-headless-shell` the executable from `HYPERFRAMES_LAMBDA_CHROME_PATH`.
107-
`examples/aws-lambda/template.yaml` and the CDK construct set both from the
108-
Chrome source. The build stages the repo's pinned shell build, and only when
109-
its folder matches the pinned digest. It stages the whole folder (the
107+
The build stages the repo's pinned shell build, and only when its folder
108+
matches that build's pinned digest. It stages the whole folder (the
110109
executable needs its ICU data and `.pak` files beside it), which is over
111110
Lambda's 250 MiB unzipped limit for a ZIP function, so the build fails its
112-
size gate and the x86 ZIP ships `@sparticuz/chromium`. `hyperframes lambda
113-
deploy` refuses `--chrome-source chrome-headless-shell` for the same reason.
111+
size gate and the x86 ZIP ships `@sparticuz/chromium`. For the same reason
112+
`hyperframes lambda deploy`, `examples/aws-lambda/template.yaml` and the CDK
113+
construct refuse `chrome-headless-shell` as the ZIP's Chrome source.
114114

115115
The arm64 video image (`image/Dockerfile`, `video` target) does not use
116116
`@sparticuz/chromium`: it installs Chrome for Testing's linux-arm64

‎packages/aws-lambda/scripts/_chromeHeadlessShell.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import { cpSync, existsSync } from "node:fs";
2-
import { join } from "node:path";
2+
import { join, relative } from "node:path";
33
import {
4-
isPinnedHeadlessShell,
4+
CHROME_WRITES_WHILE_RUNNING,
5+
headlessShellDigest,
56
managedChromeVersion,
7+
PINNED_SHELL_DIGESTS,
68
} from "@hyperframes/engine/chrome-host-ceiling";
79
import { BUNDLED_SHELL_DIR } from "../src/chromium.js";
810

911
/**
1012
* Stage the repo's pinned linux64 chrome-headless-shell from the Puppeteer cache under `home` into
1113
* `<stagingDir>/<BUNDLED_SHELL_DIR>/`. The whole folder: the executable alone exits at launch
1214
* ("Invalid file descriptor to ICU data received") without icudtl.dat and its .pak files.
13-
* Only the pinned build, and only when its folder matches the pinned digest. Returns the version.
15+
* Only the pinned build, and only when its folder matches that build's own pinned digest; the files the digest leaves
16+
* out are not copied. Returns the version.
1417
*/
1518
export function stageChromeHeadlessShell(
1619
stagingDir: string,
1720
home = process.env.HOME ?? "/root",
18-
isPinned: (executable: string) => boolean = isPinnedHeadlessShell,
21+
pinned: Readonly<Record<string, readonly string[]>> = PINNED_SHELL_DIGESTS,
1922
): string {
2023
const version = managedChromeVersion("linux");
2124
const cache = join(home, ".cache", "puppeteer");
@@ -33,13 +36,17 @@ export function stageChromeHeadlessShell(
3336
`before --source=chrome-headless-shell.`,
3437
);
3538
}
36-
if (!isPinned(executable)) {
39+
const digest = headlessShellDigest(folder);
40+
if (digest === undefined || !(pinned[version] ?? []).includes(digest)) {
3741
throw new Error(
3842
`[build-zip] ${folder} does not match the pinned chrome-headless-shell ${version} digest. ` +
3943
`Delete it and install it again.`,
4044
);
4145
}
4246
// cpSync keeps each file's mode, so the executable stays executable.
43-
cpSync(folder, join(stagingDir, BUNDLED_SHELL_DIR), { recursive: true });
47+
cpSync(folder, join(stagingDir, BUNDLED_SHELL_DIR), {
48+
recursive: true,
49+
filter: (path) => !CHROME_WRITES_WHILE_RUNNING.includes(relative(folder, path)),
50+
});
4451
return version;
4552
}

‎packages/aws-lambda/scripts/build-zip.test.ts‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import {
1212
import { tmpdir } from "node:os";
1313
import { dirname, join } from "node:path";
1414
import { pathToFileURL } from "node:url";
15-
import { managedChromeVersion } from "@hyperframes/engine/chrome-host-ceiling";
15+
import { headlessShellDigest, managedChromeVersion } from "@hyperframes/engine/chrome-host-ceiling";
1616
import * as esbuild from "esbuild";
17-
import { BUNDLED_SHELL_DIR, BUNDLED_SHELL_PATH } from "../src/chromium.js";
17+
import { BUNDLED_SHELL_DIR } from "../src/chromium.js";
1818
import { stageChromeHeadlessShell } from "./_chromeHeadlessShell.js";
1919
import { HANDLER_BANNER } from "./_handlerBanner.js";
2020

@@ -102,41 +102,53 @@ describe("build-zip chrome-headless-shell staging", () => {
102102
return home;
103103
}
104104
const pinned = managedChromeVersion("linux");
105+
const shellFolder = (home: string, version: string) =>
106+
join(
107+
home,
108+
".cache/puppeteer/chrome-headless-shell",
109+
`linux-${version}`,
110+
"chrome-headless-shell-linux64",
111+
);
105112

106-
it("stages the whole pinned shell folder where the template and the CDK stack point", () => {
113+
it("stages the whole pinned shell folder", () => {
107114
const home = puppeteerCache({
108115
"999.0.0.1": { "chrome-headless-shell": "newer" },
109116
[pinned]: {
110117
"chrome-headless-shell": "exe",
111118
"icudtl.dat": "icu",
112119
"headless_lib_data.pak": "pak",
113120
"locales/en-US.pak": "locale",
121+
"debug.log": "written by a run",
114122
},
115123
});
116124
const staging = mkdtempSync(join(tmpdir(), "hf-shell-staging-"));
117-
const checked: string[] = [];
118125
try {
119-
const isPinned = (exe: string) => checked.push(exe) > 0;
120-
expect(stageChromeHeadlessShell(staging, home, isPinned)).toBe(pinned);
121-
expect(checked).toEqual([
122-
join(
123-
home,
124-
".cache/puppeteer/chrome-headless-shell",
125-
`linux-${pinned}`,
126-
"chrome-headless-shell-linux64",
127-
"chrome-headless-shell",
128-
),
129-
]);
126+
const digests = { [pinned]: [headlessShellDigest(shellFolder(home, pinned))!] };
127+
expect(stageChromeHeadlessShell(staging, home, digests)).toBe(pinned);
130128
const shell = join(staging, BUNDLED_SHELL_DIR);
129+
// The digest leaves out what Chrome writes as it runs, so none of that ships.
130+
expect(existsSync(join(shell, "debug.log"))).toBe(false);
131131
// Without icudtl.dat beside it the shell exits at launch, so the folder ships whole.
132132
expect(readFileSync(join(shell, "icudtl.dat"), "utf-8")).toBe("icu");
133133
expect(readFileSync(join(shell, "headless_lib_data.pak"), "utf-8")).toBe("pak");
134134
expect(readFileSync(join(shell, "locales/en-US.pak"), "utf-8")).toBe("locale");
135135
expect(readFileSync(join(shell, "chrome-headless-shell"), "utf-8")).toBe("exe");
136136
expect(statSync(join(shell, "chrome-headless-shell")).mode & 0o111).not.toBe(0);
137-
expect(join("/var/task", BUNDLED_SHELL_DIR, "chrome-headless-shell")).toBe(
138-
BUNDLED_SHELL_PATH,
137+
} finally {
138+
rmSync(home, { recursive: true, force: true });
139+
rmSync(staging, { recursive: true, force: true });
140+
}
141+
});
142+
143+
it("refuses a folder whose digest is pinned only for another build", () => {
144+
const home = puppeteerCache({ [pinned]: { "chrome-headless-shell": "exe" } });
145+
const staging = mkdtempSync(join(tmpdir(), "hf-shell-staging-"));
146+
try {
147+
const digests = { "150.0.7871.124": [headlessShellDigest(shellFolder(home, pinned))!] };
148+
expect(() => stageChromeHeadlessShell(staging, home, digests)).toThrow(
149+
/does not match the pinned/,
139150
);
151+
expect(existsSync(join(staging, BUNDLED_SHELL_DIR))).toBe(false);
140152
} finally {
141153
rmSync(home, { recursive: true, force: true });
142154
rmSync(staging, { recursive: true, force: true });

‎packages/aws-lambda/src/cdk/HyperframesRenderStack.snapshot.test.ts‎

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
CONTENT_STORE_REFRESH_AFTER_DAYS,
3232
CONTENT_STORE_RETENTION_DAYS,
3333
} from "../contentStore.js";
34-
import { BUNDLED_SHELL_PATH } from "../chromium.js";
3534
import { SITE_MANIFEST_INVALID } from "../siteManifest.js";
3635
import {
3736
HyperframesRenderStack,
@@ -411,44 +410,15 @@ describe("HyperframesRenderStack — snapshot", () => {
411410
).toEqual([...new Set(render)].sort());
412411
});
413412

414-
it("points a chrome-headless-shell ZIP at its bundled shell, in CDK and SAM", () => {
415-
const chromeEnv = (synthed: ReturnType<typeof doSynth>) => {
416-
const fn = Object.values(synthed.template.findResources("AWS::Lambda::Function"))[0] as {
417-
Properties: { Environment: { Variables: Record<string, unknown> } };
418-
};
419-
const vars = fn.Properties.Environment.Variables;
420-
return [vars.HYPERFRAMES_LAMBDA_CHROME_SOURCE, vars.HYPERFRAMES_LAMBDA_CHROME_PATH];
421-
};
422-
expect(chromeEnv(SYNTHED)).toEqual(["sparticuz", undefined]);
423-
expect(chromeEnv(doSynth(undefined, "chrome-headless-shell"))).toEqual([
424-
"chrome-headless-shell",
425-
BUNDLED_SHELL_PATH,
426-
]);
427-
428-
const globals = requireRecordProperty(
429-
requireRecordProperty(
430-
requireRecordProperty(readExampleTemplate("template.yaml"), "Globals", "SAM globals"),
431-
"Function",
432-
"SAM function globals",
433-
),
434-
"Environment",
435-
"SAM global environment",
436-
) as { Variables: Record<string, unknown> };
437-
// The YAML parser drops the !If and !Ref tags and keeps [condition, then, else].
438-
expect(globals.Variables.HYPERFRAMES_LAMBDA_CHROME_PATH).toEqual([
439-
"BundlesChromeHeadlessShell",
440-
BUNDLED_SHELL_PATH,
441-
"AWS::NoValue",
442-
]);
443-
const conditions = requireRecordProperty(
444-
readExampleTemplate("template.yaml"),
445-
"Conditions",
446-
"SAM conditions",
413+
it("refuses a chrome-headless-shell ZIP, which cannot fit a function, in CDK and SAM", () => {
414+
expect(() => doSynth(undefined, "chrome-headless-shell")).toThrow(/does not fit a Lambda ZIP/);
415+
const sam = readExampleTemplate("template.yaml");
416+
const parameters = requireRecordProperty(sam, "Parameters", "SAM parameters");
417+
const chromeSource = requireRecordProperty(parameters, "ChromeSource", "ChromeSource");
418+
expect(chromeSource.AllowedValues).toEqual(["sparticuz"]);
419+
expect(envNames(sam, "Globals", "Function", "Environment", "Variables")).not.toContain(
420+
"HYPERFRAMES_LAMBDA_CHROME_PATH",
447421
);
448-
expect(conditions.BundlesChromeHeadlessShell).toEqual([
449-
"ChromeSource",
450-
"chrome-headless-shell",
451-
]);
452422
}, 30000);
453423

454424
it("lets the render function abort the multipart uploads its S3 stitch starts", () => {

‎packages/aws-lambda/src/cdk/HyperframesRenderStack.ts‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import * as s3 from "aws-cdk-lib/aws-s3";
3030
import * as sfn from "aws-cdk-lib/aws-stepfunctions";
3131
import * as tasks from "aws-cdk-lib/aws-stepfunctions-tasks";
3232
import { Construct } from "constructs";
33-
import { BUNDLED_SHELL_PATH } from "../chromium.js";
3433
import { CONTENT_STORE_PREFIX, CONTENT_STORE_RETENTION_DAYS } from "../contentStore.js";
3534

3635
/** Construction-time props for {@link HyperframesRenderStack}. */
@@ -43,7 +42,7 @@ export interface HyperframesRenderStackProps {
4342
lambdaTimeoutSec?: number;
4443
/** Lambda reserved concurrency cap. `undefined` = unreserved (account default). */
4544
reservedConcurrency?: number;
46-
/** Which Chrome runtime was bundled into the handler ZIP. Default `"sparticuz"`; a shell ZIP is over Lambda's limit. */
45+
/** Which Chrome runtime was bundled into the handler ZIP. Only `"sparticuz"`: a shell ZIP is over Lambda's limit. */
4746
chromeSource?: "sparticuz" | "chrome-headless-shell";
4847
/** Threshold for the runaway-invocations alarm. Default 1000 invocations/hour. */
4948
chunkInvocationAlarmThreshold?: number;
@@ -63,15 +62,14 @@ const DEFAULT_TIMEOUT_SEC = 900;
6362
const DEFAULT_CHROME_SOURCE = "sparticuz";
6463
const DEFAULT_ALARM_THRESHOLD = 1000;
6564

66-
/** The handler's Chrome source, and for the bundled shell, where build-zip.ts put it. */
67-
function chromeEnvironment(
65+
function zipChromeSource(
6866
source: NonNullable<HyperframesRenderStackProps["chromeSource"]>,
69-
): Record<string, string> {
70-
if (source === "sparticuz") return { HYPERFRAMES_LAMBDA_CHROME_SOURCE: source };
71-
return {
72-
HYPERFRAMES_LAMBDA_CHROME_SOURCE: source,
73-
HYPERFRAMES_LAMBDA_CHROME_PATH: BUNDLED_SHELL_PATH,
74-
};
67+
): "sparticuz" {
68+
if (source === "sparticuz") return source;
69+
throw new Error(
70+
"[HyperframesRenderStack] chromeSource chrome-headless-shell does not fit a Lambda ZIP function, so the " +
71+
"handler ZIP ships @sparticuz/chromium. The arm64 video image runs chrome-headless-shell.",
72+
);
7573
}
7674

7775
export class HyperframesRenderStack extends Construct {
@@ -88,7 +86,7 @@ export class HyperframesRenderStack extends Construct {
8886
const projectName = props.projectName ?? "hyperframes";
8987
const memorySize = props.lambdaMemoryMb ?? DEFAULT_MEMORY_MB;
9088
const timeoutSec = props.lambdaTimeoutSec ?? DEFAULT_TIMEOUT_SEC;
91-
const chromeSource = props.chromeSource ?? DEFAULT_CHROME_SOURCE;
89+
const chromeSource = zipChromeSource(props.chromeSource ?? DEFAULT_CHROME_SOURCE);
9290
const alarmThreshold = props.chunkInvocationAlarmThreshold ?? DEFAULT_ALARM_THRESHOLD;
9391
const handlerZipPath = props.handlerZipPath ?? defaultHandlerZipPath();
9492

@@ -129,7 +127,7 @@ export class HyperframesRenderStack extends Construct {
129127
environment: {
130128
NODE_OPTIONS: "--enable-source-maps",
131129
TMPDIR: "/tmp",
132-
...chromeEnvironment(chromeSource),
130+
HYPERFRAMES_LAMBDA_CHROME_SOURCE: chromeSource,
133131
HYPERFRAMES_RENDER_BUCKET: this.bucket.bucketName,
134132
HYPERFRAMES_LAMBDA_RESERVED_CONCURRENCY: String(props.reservedConcurrency ?? -1),
135133
HYPERFRAMES_STITCH_FUNCTION: `${projectName}-render`,

‎packages/aws-lambda/src/chromium.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import { existsSync } from "node:fs";
3939
export type ChromeSource = "sparticuz" | "chrome-headless-shell";
4040

4141
export const BUNDLED_SHELL_DIR = "chrome-headless-shell-linux64";
42-
export const BUNDLED_SHELL_PATH = `/var/task/${BUNDLED_SHELL_DIR}/chrome-headless-shell`;
4342

4443
/**
4544
* Thrown when the Chrome binary resolver can't produce a usable path.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const PINNED_SHELL_DIGESTS: Readonly<Record<string, readonly string[]>> =
5959
"fc5a7041fc62b0740093077cb476cae232853a0b4272c6851add5f60ce11d1c6",
6060
],
6161
};
62-
const CHROME_WRITES_WHILE_RUNNING = ["debug.log", "chrome_debug.log"];
62+
export const CHROME_WRITES_WHILE_RUNNING: readonly string[] = ["debug.log", "chrome_debug.log"];
6363

6464
function filesUnder(folder: string, prefix = ""): string[] | undefined {
6565
const names: string[] = [];

‎scripts/package-size-budgets.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"@hyperframes/aws-lambda": {
3-
"packed": 80500,
3+
"packed": 82000,
44
"unpacked": 298000
55
},
66
"@hyperframes/cli": {

0 commit comments

Comments
 (0)