From ce8d55d302c3d9949d839cff7043feaf5fd56020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Mon, 16 Nov 2026 18:37:36 +0000 Subject: [PATCH] fix(cli): validate and keyframes print project text with control characters as visible escapes (#7958) * fix(cli): validate and keyframes print project text with control characters as visible escapes * test(cli): the CSS case prints a payload the declaration split keeps whole * fix(cli): escape project-supplied object keys too, so keyframes prints tween property names safely --- packages/cli/src/commands/keyframes.test.ts | 69 +++++++++++++++++++++ packages/cli/src/commands/keyframes.ts | 14 +++-- packages/cli/src/commands/validate.test.ts | 50 +++++++++++++++ packages/cli/src/commands/validate.ts | 19 ++++-- packages/core/src/index.ts | 2 +- packages/core/src/terminalText.test.ts | 23 ++++++- packages/core/src/terminalText.ts | 16 +++++ 7 files changed, 180 insertions(+), 13 deletions(-) diff --git a/packages/cli/src/commands/keyframes.test.ts b/packages/cli/src/commands/keyframes.test.ts index 71791d4e44..682693d552 100644 --- a/packages/cli/src/commands/keyframes.test.ts +++ b/packages/cli/src/commands/keyframes.test.ts @@ -501,3 +501,72 @@ describe("keyframes project compositions", () => { }, ); }); + +describe("keyframes text output", () => { + const printed = async (html: string, extra: string[], file = "index.html") => { + const dir = mkdtempSync(join(tmpdir(), "hf-keyframes-text-")); + onTestFinished(() => rmSync(dir, { recursive: true, force: true })); + const source = join(dir, file); + writeFileSync(source, html); + const log = vi.spyOn(console, "log").mockImplementation(() => {}); + onTestFinished(() => log.mockRestore()); + await runCommand(keyframesCommand, { rawArgs: [source, ...extra] }); + return log.mock.calls.map((args) => args.join(" ")).join("\n"); + }; + + // fails without the fix: a tween target or CSS rule from the project reached the terminal with its control bytes. + it.each([ + [ + "a GSAP tween target", + wrap(` + const tl = gsap.timeline({ paused: true }); + tl.to("#dot\u001b]52;c;cGF5\u0007", { x: 10, duration: 1 }); + window.__timelines = [tl]; + `), + [], + "\\x1b]52;c;cGF5\\x07", + ], + [ + "a GSAP tween property name", + wrap(` + const tl = gsap.timeline({ paused: true }); + tl.to("#dot", { "\u001b]52;c;cGF5\u0007": 1, duration: 1 }); + window.__timelines = [tl]; + `), + [], + "\\x1b]52;c;cGF5\\x07", + ], + [ + "a CSS keyframes rule", + `
`, + ["--runtime", "css"], + "\\x1b[2J\\x07", + ], + ])( + "prints %s with its control characters as visible escapes", + async (_what, html, extra, escaped) => { + const output = await printed(html, extra); + expect(output).toContain(escaped); + expect(output).not.toContain("\u001b"); + expect(output).not.toContain("\u0007"); + }, + ); + + // Windows file names cannot hold control characters. + it.skipIf(process.platform === "win32")( + "prints a file name with control characters as visible escapes", + async () => { + const output = await printed( + ``, + [], + "a\u001b]0;x\u0007.html", + ); + expect(output).toContain("a\\x1b]0;x\\x07.html"); + expect(output).not.toContain("\u001b"); + expect(output).not.toContain("\u0007"); + }, + ); +}); diff --git a/packages/cli/src/commands/keyframes.ts b/packages/cli/src/commands/keyframes.ts index d566495aee..ed0156676d 100644 --- a/packages/cli/src/commands/keyframes.ts +++ b/packages/cli/src/commands/keyframes.ts @@ -1,4 +1,4 @@ -import { terminalText } from "@hyperframes/core"; +import { terminalText, terminalTextDeep } from "@hyperframes/core"; import { defineCommand } from "../utils/defineCommand.js"; import { existsSync, readFileSync } from "node:fs"; import { isWithinProjectRoot, readProjectFile } from "@hyperframes/parsers/asset-resolution"; @@ -1128,19 +1128,23 @@ function createKeyframesCommand(options: Partial = {}) console.log(); } - const total = comps.reduce( + const shown = terminalTextDeep(comps); + const projectShown = terminalText(projectName); + const total = shown.reduce( (n, cmp) => n + cmp.tweens.length + cmp.cssKeyframes.length + cmp.anime.length, 0, ); if (total === 0) { - console.log(`${c.success("◇")} ${c.accent(projectName)} ${c.dim("— no keyframes found")}`); + console.log( + `${c.success("◇")} ${c.accent(projectShown)} ${c.dim("— no keyframes found")}`, + ); return; } console.log( - `${c.success("◇")} ${c.accent(projectName)} ${c.dim("—")} ${c.dim(`${total} item${total === 1 ? "" : "s"}`)}`, + `${c.success("◇")} ${c.accent(projectShown)} ${c.dim("—")} ${c.dim(`${total} item${total === 1 ? "" : "s"}`)}`, ); console.log(); - for (const cmp of comps) printComposition(cmp); + for (const cmp of shown) printComposition(cmp); console.log( c.dim( `Tip: edit the keyframes in source, then \`${commandOptions.invocation} --shot out.png\` to see the rendered motion.`, diff --git a/packages/cli/src/commands/validate.test.ts b/packages/cli/src/commands/validate.test.ts index c013d35e43..dcbaa35c46 100644 --- a/packages/cli/src/commands/validate.test.ts +++ b/packages/cli/src/commands/validate.test.ts @@ -14,6 +14,8 @@ import { auditClipDurations, extractCompositionErrorsFromLint, navigationTimeoutHint, + emitFailureReport, + printValidationResult, projectPathOfUrl, raceMediaReady, shouldIgnoreRequestFailure, @@ -404,3 +406,51 @@ describe("projectPathOfUrl", () => { ); }); }); + +describe("validate text report", () => { + // fails without the fix: page console messages and contrast findings reached the terminal with raw control bytes. + it("prints page messages and contrast findings with control characters as visible escapes", () => { + const log = vi.spyOn(console, "log").mockImplementation(() => {}); + try { + const planted = "\u001b]52;c;cGF5\u0007"; + printValidationResult( + { + errors: [{ level: "error", text: `boom${planted}`, line: 3 }], + warnings: [{ level: "warning", text: `careful${planted}` }], + contrast: [ + { + time: 1, + selector: `#title${planted}`, + text: `Hello${planted}`, + ratio: 1.2, + wcagAA: false, + large: false, + fg: "#777", + bg: "#888", + }, + ], + }, + false, + ); + const output = log.mock.calls.map((args) => args.join(" ")).join("\n"); + expect(output.split("\\x1b]52;c;cGF5\\x07").length - 1).toBe(4); + expect(output).not.toContain("\u001b"); + expect(output).not.toContain("\u0007"); + } finally { + log.mockRestore(); + } + }); + + it("prints a failure message with control characters as visible escapes", () => { + const error = vi.spyOn(console, "error").mockImplementation(() => {}); + try { + emitFailureReport("page crashed\u001b]52;c;cGF5\u0007", false); + const output = error.mock.calls.map((args) => args.join(" ")).join("\n"); + expect(output).toContain("page crashed\\x1b]52;c;cGF5\\x07"); + expect(output).not.toContain("\u001b"); + expect(output).not.toContain("\u0007"); + } finally { + error.mockRestore(); + } + }); +}); diff --git a/packages/cli/src/commands/validate.ts b/packages/cli/src/commands/validate.ts index 1b76f70e56..b782ef385c 100644 --- a/packages/cli/src/commands/validate.ts +++ b/packages/cli/src/commands/validate.ts @@ -15,7 +15,11 @@ import { resolveProject, type ProjectDir } from "../utils/project.js"; import { normalizeErrorMessage } from "../utils/errorMessage.js"; import type { ProjectLintResult } from "../utils/lintProject.js"; import { resolveCompositionViewportFromHtml } from "../utils/compositionViewport.js"; -import { resolveNaturalMediaTimelineDuration } from "@hyperframes/core"; +import { + resolveNaturalMediaTimelineDuration, + terminalText, + terminalTextDeep, +} from "@hyperframes/core"; import { c } from "../ui/colors.js"; import { decodeWellFormedEscapes } from "@hyperframes/studio-server"; import { printDeprecationNotice, withMeta } from "../utils/updateCheck.js"; @@ -639,7 +643,7 @@ function emitTextReport( console.log(`${c.accent("◇")} ${formatTotals(errors, warnings, contrastFailures)}`); } -function emitFailureReport(message: string, asJson: boolean): void { +export function emitFailureReport(message: string, asJson: boolean): void { if (asJson) { console.log( JSON.stringify( @@ -653,7 +657,7 @@ function emitFailureReport(message: string, asJson: boolean): void { ); return; } - console.error(`${c.error("✗")} ${message}`); + console.error(`${c.error("✗")} ${terminalText(message)}`); } export default defineCommand({ @@ -691,7 +695,9 @@ Examples: const asJson = Boolean(args.json); if (!asJson) { - console.log(`${c.accent("◆")} Validating ${c.accent(project.name)} in headless Chrome`); + console.log( + `${c.accent("◆")} Validating ${c.accent(terminalText(project.name))} in headless Chrome`, + ); } try { @@ -707,7 +713,7 @@ Examples: }, }); -function printValidationResult( +export function printValidationResult( result: { errors: ConsoleEntry[]; warnings: ConsoleEntry[]; contrast?: ContrastEntry[] }, asJson: boolean, ): number { @@ -718,7 +724,8 @@ function printValidationResult( if (asJson) { emitJsonReport(errors, warnings, contrast, contrastFailures); } else { - emitTextReport(errors, warnings, contrastFailures, contrastPassed); + const shown = terminalTextDeep({ errors, warnings, contrastFailures, contrastPassed }); + emitTextReport(shown.errors, shown.warnings, shown.contrastFailures, shown.contrastPassed); } return errors.length > 0 ? 1 : 0; } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 6e411271d3..a50244e1bd 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -495,4 +495,4 @@ export { type MotionBlurFingerprint, type MotionBlurFingerprintField, } from "./motionBlurFingerprint.js"; -export { terminalText } from "./terminalText.js"; +export { terminalText, terminalTextDeep } from "./terminalText.js"; diff --git a/packages/core/src/terminalText.test.ts b/packages/core/src/terminalText.test.ts index 0cbe060d25..d192d4d446 100644 --- a/packages/core/src/terminalText.test.ts +++ b/packages/core/src/terminalText.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { terminalText } from "./terminalText.js"; +import { terminalText, terminalTextDeep } from "./terminalText.js"; describe("terminalText", () => { it("leaves ordinary text and tabs alone", () => { @@ -12,3 +12,24 @@ describe("terminalText", () => { ); }); }); + +describe("terminalTextDeep", () => { + it("escapes every string in nested arrays and objects and keeps other values", () => { + const shown = terminalTextDeep({ + id: "a\u001b]52;c;x\u0007", + list: [{ selector: "#b\u001b[2J", n: 3 }], + ok: true, + }); + expect(shown).toEqual({ + id: "a\\x1b]52;c;x\\x07", + list: [{ selector: "#b\\x1b[2J", n: 3 }], + ok: true, + }); + }); + + it("escapes object keys too", () => { + expect(terminalTextDeep({ "x\u001b]52;c;cGF5\u0007": 1 })).toEqual({ + "x\\x1b]52;c;cGF5\\x07": 1, + }); + }); +}); diff --git a/packages/core/src/terminalText.ts b/packages/core/src/terminalText.ts index 92a68efbd2..905cdc8620 100644 --- a/packages/core/src/terminalText.ts +++ b/packages/core/src/terminalText.ts @@ -9,3 +9,19 @@ export function terminalText(text: string): string { } return shown; } + +const isPlainObject = (value: object) => { + const proto = Object.getPrototypeOf(value); + return proto === Object.prototype || proto === null; +}; + +export function terminalTextDeep(value: T): T { + if (typeof value === "string") return terminalText(value) as T; + if (Array.isArray(value)) return value.map(terminalTextDeep) as T; + if (value !== null && typeof value === "object" && isPlainObject(value)) { + return Object.fromEntries( + Object.entries(value).map(([key, item]) => [terminalText(key), terminalTextDeep(item)]), + ) as T; + } + return value; +}