Skip to content

Commit 0ad9c70

Browse files
fix(lint): give randomness guidance for crypto values (#3659)
Builds on #1729 in the extracted lint package. Co-authored-by: AGRO-CODEX <[email protected]>
1 parent 29efd5d commit 0ad9c70

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎packages/lint/src/rules/core.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,25 @@ describe("core rules", () => {
601601
});
602602

603603
describe("non_deterministic_code", () => {
604+
it("gives randomness guidance for crypto and clock guidance for wall time", async () => {
605+
const result = await lintHyperframeHtml(`<html><body>
606+
<div data-composition-id="c1" data-width="1920" data-height="1080"></div>
607+
<script>
608+
crypto.getRandomValues(new Uint32Array(1));
609+
Date.now();
610+
window.__timelines = { c1: gsap.timeline({ paused: true }) };
611+
</script>
612+
</body></html>`);
613+
const crypto = result.findings.find((finding) =>
614+
finding.message.includes("crypto.getRandomValues"),
615+
);
616+
const clock = result.findings.find((finding) => finding.message.includes("Date.now"));
617+
expect(crypto).toMatchObject({ code: "non_deterministic_code", severity: "error" });
618+
expect(crypto?.fixHint).toContain("seeded PRNG");
619+
expect(crypto?.fixHint).not.toContain("time-dependent");
620+
expect(clock?.fixHint).toContain("wall-clock time");
621+
});
622+
604623
it("detects Math.random() in script content", async () => {
605624
const html = `
606625
<html><body>

‎packages/lint/src/rules/core.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ export const coreRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
478478
{
479479
pattern: /crypto\.getRandomValues\s*\(/,
480480
label: "crypto.getRandomValues()",
481-
hint: "Remove time-dependent code. Use a seeded PRNG for deterministic renders.",
481+
hint: "Use a seeded PRNG (e.g. a simple mulberry32) so renders are deterministic across frames.",
482482
},
483483
{
484484
pattern: /gsap\.utils\.random\s*\(/,

0 commit comments

Comments
 (0)