diff --git a/docs/reference/share-status.mdx b/docs/reference/share-status.mdx index 993e168a5e..8a4503ea53 100644 --- a/docs/reference/share-status.mdx +++ b/docs/reference/share-status.mdx @@ -18,6 +18,9 @@ identifies when the operation began. These states have `ready: false` and After the command finishes, status reflects the server's processing result. A crashed local process does not leave an active progress indicator forever. +Terminal output shows control characters in share messages and links as visible escapes. +Use `--json` to read the original field values programmatically. + Free workspaces can share privately or with anyone who has the link. Team and invite-only access require a paid plan. Free sharing includes the video; editable project sources require Pro. The five-project Free limit still applies. diff --git a/packages/cli/src/commands/share.test.ts b/packages/cli/src/commands/share.test.ts index 74cdbfb675..3d94675d96 100644 --- a/packages/cli/src/commands/share.test.ts +++ b/packages/cli/src/commands/share.test.ts @@ -75,6 +75,33 @@ it("reports the failed attempt on stderr without calling the previous version re expect(out).toHaveBeenCalledWith("Previous shared version: https://hyperframes.dev/r/previous"); expect(out.mock.calls.flat().join(" ")).not.toContain("still processing"); }); +it("escapes dynamic text for terminal output and preserves failure fields in JSON", async () => { + const raw = "detail" + String.fromCharCode(27, 13, 7, 155, 9, 10); + const shown = "detail\\x1b\\x0d\\x07\\x9b\\x09\\x0a"; + const out = vi.spyOn(console, "log").mockImplementation(() => {}); + const err = vi.spyOn(console, "error").mockImplementation(() => {}); + mocks.run.mockResolvedValue({ + state: "failed", + source: {}, + version: { failure: raw }, + previousUrl: raw, + }); + await runCommand(command, { rawArgs: ["--status"] }); + expect(err).toHaveBeenCalledWith(shown); + expect(out).toHaveBeenCalledWith(`Previous shared version: ${shown}`); + await runCommand(command, { rawArgs: ["--status", "--json"] }); + expect(JSON.parse(String(out.mock.calls.at(-1)?.[0]))).toMatchObject({ + error: { message: raw }, + version: { failure: raw }, + previousUrl: raw, + }); + err.mockClear(); + mocks.run.mockRejectedValue(new Error(raw)); + await runCommand(command, { rawArgs: ["--status"] }); + expect(err).toHaveBeenCalledWith(shown); + await runCommand(command, { rawArgs: ["--status", "--json"] }); + expect(JSON.parse(String(out.mock.calls.at(-1)?.[0])).error.message).toBe(raw); +}); it("still opens management when the latest export failed", async () => { const out = vi.spyOn(console, "log").mockImplementation(() => {}); await runCommand(command, { rawArgs: ["--manage", "--json"] }); @@ -126,7 +153,7 @@ it("prints a failure's control characters as visible escapes", async () => { vi.spyOn(console, "log").mockImplementation(() => {}); mocks.run.mockRejectedValue(new Error(`Sharing blocked${esc}[2J.\nTry again`)); await runCommand(command, { rawArgs: ["--private", "-y"] }); - expect(friendly).toHaveBeenLastCalledWith("Sharing blocked\\x1b[2J.\nTry again"); + expect(friendly).toHaveBeenLastCalledWith("Sharing blocked\\x1b[2J.\\x0aTry again"); }); // fails without the fix: a failed export's reason reached the terminal with its control characters live. diff --git a/packages/cli/src/commands/share.ts b/packages/cli/src/commands/share.ts index b989b3862f..b7a8178f0d 100644 --- a/packages/cli/src/commands/share.ts +++ b/packages/cli/src/commands/share.ts @@ -4,7 +4,7 @@ import { CliUsageError, setCommandExitCode } from "../utils/commandResult.js"; import { isUnattended } from "../utils/attendedTerminal.js"; import * as prompts from "../ui/prompts.js"; import { c } from "../ui/colors.js"; -import { terminalLines, terminalText } from "@hyperframes/core"; +import { terminalText } from "@hyperframes/core"; import { receivedRecordStatus } from "../utils/privateProjectFile.js"; import { changesNamedReview, @@ -25,6 +25,14 @@ export const examples: Example[] = [ ["Open sharing settings on the web", "hyperframes share --manage"], ]; +function escapeTabs(text: string): string { + return text.replaceAll("\t", "\\x09"); +} + +function shareLine(text: string): string { + return terminalText(escapeTabs(text)); +} + export default defineCommand({ meta: { name: "share", @@ -78,7 +86,7 @@ export default defineCommand({ let lastProgress = ""; const service: ShareService = createShareService(project.dir, (value) => { if (!value || args.json) return; - const message = `Sharing: ${terminalText(value.step)}${value.slow ? " (still processing)" : ""}`; + const message = `Sharing: ${shareLine(value.step)}${value.slow ? " (still processing)" : ""}`; if (message !== lastProgress) process.stderr.write(`${message}\n`); lastProgress = message; }); @@ -92,7 +100,7 @@ export default defineCommand({ console.log( JSON.stringify({ ...jsonFailure(failure.code, failure.message), error: failure }), ); - else console.error(terminalLines(failure.message)); + else console.error(terminalText(escapeTabs(failure.message))); setCommandExitCode(error instanceof CliUsageError ? 2 : 1); } }, @@ -211,8 +219,8 @@ async function askToChangeReview(current: ShareState) { console.log( ` ${c.bold("Sharing changes that review under your account: who can open it, its link or its versions.")}`, ); - console.log(` ${c.dim(`Review: ${namedReview(current)}`)}`); - if (current.share?.url) console.log(` ${c.dim(`Link: ${terminalText(current.share.url)}`)}`); + console.log(` ${c.dim(`Review: ${shareLine(namedReview(current))}`)}`); + if (current.share?.url) console.log(` ${c.dim(`Link: ${shareLine(current.share.url)}`)}`); console.log(` ${c.dim("If you did not share this review before, cancel.")}`); console.log(); const approved = await prompts.confirm({ message: "Share to this review?", initialValue: false }); @@ -277,13 +285,13 @@ async function presentSuccessfulResult(result: ShareState, input: ShareAction, j if (json) console.log(JSON.stringify({ ok: true, ...result })); else if (result.activity) console.log( - `Sharing: ${terminalText(result.activity.step.replaceAll("_", " "))}. Run hyperframes share --status to check progress.`, + `Sharing: ${shareLine(result.activity.step.replaceAll("_", " "))}. Run hyperframes share --status to check progress.`, ); else if (result.upToDate) console.log("Already up to date."); else if (input.action === "manage") { - console.log(terminalText(result.manageUrl)); + console.log(shareLine(result.manageUrl)); if (!isUnattended()) await (await import("open")).default(result.manageUrl); - } else if (result.ready) console.log(`Shared video ready: ${terminalText(String(result.url))}`); + } else if (result.ready) console.log(shareLine(`Shared video ready: ${result.url}`)); else if (result.state === "not_shared") console.log("This project has not been shared yet."); else if (result.state === "link_off") console.log("This share's link is off. Run hyperframes share --manage to turn it on."); @@ -295,9 +303,9 @@ function presentFailure(result: ShareState, json: boolean) { if (json) console.log(JSON.stringify({ ...result, ...jsonFailure("share_export_failed", message) })); else { - console.error(terminalLines(message)); + console.error(shareLine(message)); if (result.previousUrl) - console.log(`Previous shared version: ${terminalText(result.previousUrl)}`); + console.log(`Previous shared version: ${shareLine(result.previousUrl)}`); } setCommandExitCode(1); } diff --git a/packages/share/src/reviewShare.mjs b/packages/share/src/reviewShare.mjs index 36ca1819e5..ea54281e7f 100644 --- a/packages/share/src/reviewShare.mjs +++ b/packages/share/src/reviewShare.mjs @@ -78,6 +78,22 @@ const isSavedIdentity = (record) => isOptionalBoolean(record.removed) && (!record.opened || isSavedId(record.projectId)); +// This is a display hint. Keep ordinary mailbox names, including internationalized ones. +/** @param {unknown} value */ +function displayOwnerEmail(value) { + if (typeof value !== "string" || value.length > 254) return null; + const parts = value.split("@"); + if (parts.length !== 2) return null; + const [local, domain] = parts; + const atom = /^[\p{L}\p{M}\p{N}!#$%&'*+/=?^`_{|}~-]+$/u; + const label = /^[\p{L}\p{N}](?:[\p{L}\p{M}\p{N}-]{0,61}[\p{L}\p{M}\p{N}])?$/u; + return local.length <= 64 && + local.split(".").every((part) => atom.test(part)) && + domain.split(".").every((part) => label.test(part)) + ? value + : null; +} + /** @param {unknown} value @param {string} studioProjectId @param {string} file */ function savedRegistration(value, studioProjectId, file) { if (value == null) return undefined; @@ -101,7 +117,7 @@ async function readRecord(projectPath, at = site()) { removed: record.removed === true, registration: savedRegistration(record.registration, record.studioProjectId, file), site: madeOn, - ownerEmail: typeof record.ownerEmail === "string" ? record.ownerEmail : null, + ownerEmail: displayOwnerEmail(record.ownerEmail), }; } diff --git a/packages/share/tests/reviewPersistence.test.mjs b/packages/share/tests/reviewPersistence.test.mjs index 691863c3d6..4fcd93beb2 100644 --- a/packages/share/tests/reviewPersistence.test.mjs +++ b/packages/share/tests/reviewPersistence.test.mjs @@ -15,6 +15,8 @@ import { recheckHeld, reviewDestination, reviewFiles, + reviewRecord, + reviewStatus, reviewVersion, ReviewFailed, shareForReview, @@ -58,6 +60,38 @@ async function project(t) { return { dir, metadata, render, stateFile: join(metadata, "review-export.json") }; } +test("hosts only see plausible saved owner addresses, preserving their spelling", async (t) => { + const { dir, metadata } = await project(t); + const file = join(metadata, "review.json"); + const record = JSON.parse(await readFile(file, "utf8")); + const client = { + signedInEmail: async () => "me@example.org", + share: async () => { + throw new ReviewFailed(403, "permission_denied"); + }, + }; + for (const [ownerEmail, expected] of [ + ["A.Lee+Équipe@example.org", "A.Lee+Équipe@example.org"], + ["display name", null], + ["a@@example.org", null], + ["a@bad domain.org", null], + ["a@example.org\r", null], + ["a\x1b@example.org", null], + ["a\x07@example.org", null], + ["a@exam\x85ple.org", null], + ["a@exam\u202eple.org", null], + [42, null], + [undefined, null], + ]) { + await writeFile(file, JSON.stringify({ ...record, ownerEmail })); + assert.equal((await reviewRecord(dir)).ownerEmail, expected); + await assert.rejects(reviewStatus({ projectPath: dir, client }), { + status: 403, + reason: expected ? `owner_signed_out:${expected}` : "no_access", + }); + } +}); + // Hold a real second process at the create request, after its pending state has been written. function uploader(t, dir) { const moduleUrl = new URL("../src/index.mjs", import.meta.url).href;