Skip to content

Commit f8c0919

Browse files
committed
[red] Guard exact unqualified firmware beta
Signed-off-by: Viwat Vchirawongkwin <viwat.v@chula.ac.th>
1 parent c06fa99 commit f8c0919

5 files changed

Lines changed: 118 additions & 15 deletions

File tree

‎tools/web/src/test/build-contract.test.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import { promisify } from "node:util";
1010
import { describe, expect, it } from "vitest";
1111

1212
import { firmwareReleaseSelectedAtBuild } from "@/lib/firmware-release-selection";
13-
import { pendingPublicFirmwareRelease } from "@/test/fixtures/firmware-release";
13+
import {
14+
pendingPublicFirmwareRelease,
15+
publicBetaFirmwareRelease,
16+
} from "@/test/fixtures/firmware-release";
1417

1518
const execFile = promisify(execFileCallback);
1619

@@ -169,6 +172,36 @@ describe("production build contract", () => {
169172
}
170173
});
171174

175+
it("accepts only the exact attested public-beta selector at the build boundary", async () => {
176+
const selectionRoot = await mkdtemp(join(tmpdir(), "pyble-beta-selection-"));
177+
const selectionFile = join(selectionRoot, "selection.json");
178+
await writeFile(
179+
selectionFile,
180+
JSON.stringify(publicBetaFirmwareRelease),
181+
"utf8",
182+
);
183+
184+
const previousSelection = process.env.PYBLE_FLASH_SELECTION_FILE;
185+
process.env.PYBLE_FLASH_SELECTION_FILE = selectionFile;
186+
try {
187+
expect(firmwareReleaseSelectedAtBuild()).toEqual(
188+
publicBetaFirmwareRelease,
189+
);
190+
191+
const altered = structuredClone(publicBetaFirmwareRelease);
192+
altered.releaseJson.sha256 = "0".repeat(64);
193+
await writeFile(selectionFile, JSON.stringify(altered), "utf8");
194+
expect(() => firmwareReleaseSelectedAtBuild()).toThrow(/public beta/i);
195+
} finally {
196+
if (previousSelection === undefined) {
197+
delete process.env.PYBLE_FLASH_SELECTION_FILE;
198+
} else {
199+
process.env.PYBLE_FLASH_SELECTION_FILE = previousSelection;
200+
}
201+
await rm(selectionRoot, { recursive: true, force: true });
202+
}
203+
});
204+
172205
it("requires explicit server-side build inputs for protected candidate staging", async () => {
173206
const [packageJson, stagingSource] = await Promise.all([
174207
readFile(join(process.cwd(), "package.json"), "utf8").then(
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// SPDX-License-Identifier: MIT
2+
// Part of PyBLE (https://pyble.dev) — see /LICENSE.
3+
4+
import { describe, expect, it } from "vitest";
5+
6+
import { isExactPublicBetaFirmwareRelease } from "@/lib/firmware-release";
7+
import { publicBetaFirmwareRelease } from "@/test/fixtures/firmware-release";
8+
9+
describe("exact public firmware beta policy", () => {
10+
it("accepts only the attested unrestricted pending v0.4.1 descriptor", () => {
11+
expect(isExactPublicBetaFirmwareRelease(publicBetaFirmwareRelease)).toBe(
12+
true,
13+
);
14+
15+
for (const mutate of [
16+
(descriptor: Record<string, unknown>) => {
17+
descriptor.version = "0.4.2";
18+
},
19+
(descriptor: Record<string, unknown>) => {
20+
descriptor.hilStatus = "passed";
21+
},
22+
(descriptor: Record<string, unknown>) => {
23+
descriptor.accessControlled = true;
24+
},
25+
(descriptor: Record<string, unknown>) => {
26+
const releaseJson = descriptor.releaseJson as Record<string, unknown>;
27+
releaseJson.sha256 = "0".repeat(64);
28+
},
29+
]) {
30+
const descriptor = structuredClone(
31+
publicBetaFirmwareRelease,
32+
) as unknown as Record<string, unknown>;
33+
mutate(descriptor);
34+
expect(isExactPublicBetaFirmwareRelease(descriptor)).toBe(false);
35+
}
36+
});
37+
});

‎tools/web/src/test/fixtures/firmware-release.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const firmwareProfiles = [
4646
] as const;
4747

4848
export type FirmwareProfileId = (typeof firmwareProfiles)[number]["id"];
49-
export type FirmwareDeployment = "public" | "candidate";
49+
export type FirmwareDeployment = "public" | "candidate" | "public-beta";
5050
export type HilStatus = "pending" | "passed";
5151

5252
interface TestArtifact {
@@ -708,6 +708,16 @@ export const passedPublicFirmwareRelease =
708708
export const pendingPublicFirmwareRelease = createFirmwareReleaseFixture({
709709
hilStatus: "pending",
710710
}).descriptor;
711+
export const publicBetaFirmwareRelease = {
712+
...structuredClone(pendingPublicFirmwareRelease),
713+
deployment: "public-beta",
714+
accessControlled: false,
715+
releaseJson: {
716+
path: "/firmware/v0.4.1/release.json",
717+
sha256:
718+
"8b84fbb65a0463d20369e1d86dac566ca7a2039ebc30f9186f55c05421962445",
719+
},
720+
} satisfies FirmwareReleaseDescriptor;
711721
export const pendingCandidateFirmwareRelease = createFirmwareReleaseFixture({
712722
deployment: "candidate",
713723
accessControlled: true,

‎tools/web/src/test/flash-installer.test.tsx‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
passedPublicFirmwareRelease,
2525
pendingCandidateFirmwareRelease,
2626
pendingPublicFirmwareRelease,
27+
publicBetaFirmwareRelease,
2728
type FirmwareProfileId,
2829
uncontrolledCandidateFirmwareRelease,
2930
verifiedProfile,
@@ -313,6 +314,35 @@ describe("browser firmware installer states", () => {
313314
expect(document.querySelector("esp-web-install-button")).toBeNull();
314315
});
315316

317+
it("offers the exact unrestricted v0.4.1 beta with prominent unqualified warnings", async () => {
318+
renderInstaller({ release: publicBetaFirmwareRelease });
319+
320+
expect(screen.getByRole("status")).toHaveTextContent(
321+
/unqualified firmware beta.*full hardware-in-the-loop.*pending.*use at your own risk/i,
322+
);
323+
expect(screen.queryByText(/protected release candidate/i)).toBeNull();
324+
expect(screen.queryByText(/qualified release/i)).toBeNull();
325+
326+
const profileGroup = screen.getByRole("radiogroup", {
327+
name: /select the exact module profile/i,
328+
});
329+
expect(within(profileGroup).getAllByRole("radio")).toHaveLength(2);
330+
expect(within(profileGroup).queryByText(/esp32-c3/i)).toBeNull();
331+
332+
selectS3Profile();
333+
acceptEveryConsent();
334+
startVerification();
335+
await waitFor(() => {
336+
expect(document.querySelector("esp-web-install-button")).not.toBeNull();
337+
});
338+
expect(screen.getByText(/unqualified beta.*use at your own risk/i)).toBeInTheDocument();
339+
expect(
340+
screen.getByRole("button", {
341+
name: /install unqualified beta pyble 0\.4\.1/i,
342+
}),
343+
).toBeInTheDocument();
344+
});
345+
316346
it("starts disabled with exactly both qualified profiles and keeps deferred C3 out of the selector", () => {
317347
renderInstaller();
318348

‎tools/web/src/test/vps-deployment-contract.test.ts‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,16 @@ describe("Cloudflare-fronted VPS deployment", () => {
8989
expect(headers).toContain("Strict-Transport-Security");
9090
});
9191

92-
it("quarantines the burned pre-public firmware candidate", async () => {
92+
it("routes the exact v0.4.1 public beta through the immutable firmware boundary", async () => {
9393
const config = await readFile(
9494
join(deploymentRoot, "nginx", "10-pyble-dev-https.conf"),
9595
"utf8",
9696
);
97-
const quarantine =
98-
/location \^~ \/firmware\/v0\.4\.1\/\s*\{([\s\S]*?)\n\s*\}/.exec(
99-
config,
100-
)?.[1];
101-
const quarantineIndex = config.indexOf("location ^~ /firmware/v0.4.1/");
102-
const generalFirmwareIndex = config.indexOf("location ^~ /firmware/ {");
103-
104-
expect(quarantine).toBeDefined();
105-
expect.soft(quarantine).toMatch(/Cache-Control\s+"no-store"/);
106-
expect.soft(quarantine).toMatch(/return\s+404/);
107-
expect(quarantineIndex).toBeGreaterThan(-1);
108-
expect(quarantineIndex).toBeLessThan(generalFirmwareIndex);
97+
expect(config).not.toContain("location ^~ /firmware/v0.4.1/");
98+
expect(config).not.toContain("@burned_firmware_candidate");
99+
expect(config).toMatch(
100+
/location \^~ \/firmware\/\s*\{[\s\S]*?alias \/srv\/pyble\/firmware\//,
101+
);
109102
});
110103

111104
it("preserves path and query while canonicalizing every alternate host", async () => {

0 commit comments

Comments
 (0)