diff --git a/packages/cli/src/media-use/lib/literal-media-input.mjs b/packages/cli/src/media-use/lib/literal-media-input.mjs index 85840a06af..a9293fb008 100644 --- a/packages/cli/src/media-use/lib/literal-media-input.mjs +++ b/packages/cli/src/media-use/lib/literal-media-input.mjs @@ -1,15 +1,14 @@ // The standalone twin of assertLiteralMediaInput in packages/parsers/src/literalMediaInput.ts, for inputs and outputs; a test keeps them equal. const PATTERN_IMAGE2_EXPANDS = /%\d*[d%*?[\]{}]/; const WILDCARD_MAKES_ANY_PERCENT_A_PATTERN = /[*?[{]/; -const URL_SCHEME = /^[a-z][a-z0-9+.-]+:/i; +const FORWARDED = /^https?:/i; /** ffmpeg's image2 reader or writer would use other files, folders included, than the one named. */ export function assertLiteralMediaPath(input) { - const local = !URL_SCHEME.test(input) || /^file:/i.test(input); const pattern = PATTERN_IMAGE2_EXPANDS.test(input) || (input.includes("%") && WILDCARD_MAKES_ANY_PERCENT_A_PATTERN.test(input)); - if (!local || !pattern) return; + if (FORWARDED.test(input) || !pattern) return; throw new Error( `[FFmpeg] refused media path "${input.split(/[\\/]/).pop()}": a "%" followed by "d", another "%" or a wildcard, ` + 'with or without digits between, or a "%" and a wildcard (*, ?, [ or {) anywhere in the path, would make ffmpeg ' + diff --git a/packages/parsers/src/assetResolution.test.ts b/packages/parsers/src/assetResolution.test.ts index c01a9e34b3..f1592bbc9f 100644 --- a/packages/parsers/src/assetResolution.test.ts +++ b/packages/parsers/src/assetResolution.test.ts @@ -2,7 +2,7 @@ import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { execFileSync } from "node:child_process"; import { existsSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; -import { join, posix, win32 } from "node:path"; +import { isAbsolute, join, posix, resolve, win32 } from "node:path"; import { pathToFileURL } from "node:url"; import { collectSubCompositionSrcs, @@ -143,6 +143,21 @@ describe("collectSubCompositionSrcs", () => { // The browser clamps `..` at the origin root; path.join does not. One resolver must serve lint and render. describe("resolveProjectRelativeSrc — the one src resolver for lint and render", () => { + // fails without the fix: with "." as the base the join stayed relative, so a src such as concat:a.wav|b.wav led + // the path ffmpeg was handed, and ffmpeg read it as its concat protocol instead of a project file. + it("returns an absolute path whatever base the caller passed", () => { + for (const base of [".", "./", ""]) + for (const src of [ + "concat:a.wav|b.wav", + "subfile,,start,0,end,1,,:clip.mp4", + "assets/clip.mp4", + ]) { + const path = resolveProjectRelativeSrc(src, base, base && `${base}/compiled`); + expect(isAbsolute(path)).toBe(true); + expect(path.startsWith(resolve(base))).toBe(true); + } + }); + let tmp: string; beforeAll(() => { diff --git a/packages/parsers/src/assetResolution.ts b/packages/parsers/src/assetResolution.ts index 7051480ab7..b3be7118c1 100644 --- a/packages/parsers/src/assetResolution.ts +++ b/packages/parsers/src/assetResolution.ts @@ -230,9 +230,11 @@ function gitBashFile(src: string): string | undefined { /** Resolves a media `src` like a browser URL (`..` clamps at the project root); a miss returns the base-dir join. */ export function resolveProjectRelativeSrc( src: string, - baseDir: string, - compiledDir?: string, + relativeOrAbsoluteBase: string, + relativeOrAbsoluteCompiled?: string, ): string { + const baseDir = resolve(relativeOrAbsoluteBase); + const compiledDir = relativeOrAbsoluteCompiled && resolve(relativeOrAbsoluteCompiled); const cleanSrc = fileUrlAsPath(cleanAssetUrl(src)); // A leading slash is an origin-root URL served from the project root, unless the absolute path exists. diff --git a/packages/parsers/src/literalMediaInput.test.ts b/packages/parsers/src/literalMediaInput.test.ts new file mode 100644 index 0000000000..e69d3940d8 --- /dev/null +++ b/packages/parsers/src/literalMediaInput.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from "vitest"; +import { assertLiteralMediaInput, assertLiteralMediaOutput } from "./literalMediaInput.js"; + +describe("assertLiteralMediaInput", () => { + // fails without the fix: every scheme but file: skipped the check, as if another check had vetted it. + it("checks a scheme-led name as a local one; only http and https are left to the caller", () => { + for (const path of [ + "concat:/a/b%d.png", + "subfile,,start,0,end,0,,:/a/b%d.png", + "data:/a%d.png", + ]) { + expect(() => assertLiteralMediaInput(path)).toThrow(/pattern of other files/); + expect(() => assertLiteralMediaOutput(path)).toThrow(/pattern of other files/); + } + for (const path of [ + "https://cdn.example/a%2d.png", + "HTTP://cdn.example/a%2d.png", + "/a/clip.mp4", + ]) + expect(() => assertLiteralMediaInput(path)).not.toThrow(); + }); +}); diff --git a/packages/parsers/src/literalMediaInput.ts b/packages/parsers/src/literalMediaInput.ts index f9674f44d8..ff028d460c 100644 --- a/packages/parsers/src/literalMediaInput.ts +++ b/packages/parsers/src/literalMediaInput.ts @@ -1,13 +1,12 @@ const PATTERN_IMAGE2_EXPANDS = /%\d*[d%*?[\]{}]/; const WILDCARD_MAKES_ANY_PERCENT_A_PATTERN = /[*?[{]/; -const URL_SCHEME = /^[a-z][a-z0-9+.-]+:/i; +const FORWARDED = /^https?:/i; function refusePattern(path: string, use: "read" | "write"): void { - const local = !URL_SCHEME.test(path) || /^file:/i.test(path); const pattern = PATTERN_IMAGE2_EXPANDS.test(path) || (path.includes("%") && WILDCARD_MAKES_ANY_PERCENT_A_PATTERN.test(path)); - if (!local || !pattern) return; + if (FORWARDED.test(path) || !pattern) return; throw new Error( `[FFmpeg] refused media path "${path.split(/[\\/]/).pop()}": a "%" followed by "d", another "%" or a wildcard, ` + `with or without digits between, or a "%" and a wildcard (*, ?, [ or {) anywhere in the path, would make ffmpeg ` + diff --git a/skills-manifest.json b/skills-manifest.json index dc61115497..f24d995e74 100644 --- a/skills-manifest.json +++ b/skills-manifest.json @@ -2,11 +2,11 @@ "source": "heygen-com/hyperframes", "skills": { "figma": { - "hash": "b1facb484ca64802", + "hash": "aee1018803b57ea3", "files": 4 }, "hyperframes": { - "hash": "1ebf5a311a4d1ed7", + "hash": "7b9108a831fc740a", "files": 85 }, "hyperframes-cli": { @@ -18,7 +18,7 @@ "files": 13 }, "media-use": { - "hash": "ff2fa3c4eb8a7bc1", + "hash": "fff20d7c6efcc737", "files": 135 }, "remotion-to-hyperframes": { diff --git a/skills/figma/scripts/lib/literal-media-input.mjs b/skills/figma/scripts/lib/literal-media-input.mjs index 85840a06af..a9293fb008 100644 --- a/skills/figma/scripts/lib/literal-media-input.mjs +++ b/skills/figma/scripts/lib/literal-media-input.mjs @@ -1,15 +1,14 @@ // The standalone twin of assertLiteralMediaInput in packages/parsers/src/literalMediaInput.ts, for inputs and outputs; a test keeps them equal. const PATTERN_IMAGE2_EXPANDS = /%\d*[d%*?[\]{}]/; const WILDCARD_MAKES_ANY_PERCENT_A_PATTERN = /[*?[{]/; -const URL_SCHEME = /^[a-z][a-z0-9+.-]+:/i; +const FORWARDED = /^https?:/i; /** ffmpeg's image2 reader or writer would use other files, folders included, than the one named. */ export function assertLiteralMediaPath(input) { - const local = !URL_SCHEME.test(input) || /^file:/i.test(input); const pattern = PATTERN_IMAGE2_EXPANDS.test(input) || (input.includes("%") && WILDCARD_MAKES_ANY_PERCENT_A_PATTERN.test(input)); - if (!local || !pattern) return; + if (FORWARDED.test(input) || !pattern) return; throw new Error( `[FFmpeg] refused media path "${input.split(/[\\/]/).pop()}": a "%" followed by "d", another "%" or a wildcard, ` + 'with or without digits between, or a "%" and a wildcard (*, ?, [ or {) anywhere in the path, would make ffmpeg ' + diff --git a/skills/hyperframes/scripts/lib/literal-media-input.mjs b/skills/hyperframes/scripts/lib/literal-media-input.mjs index 85840a06af..a9293fb008 100644 --- a/skills/hyperframes/scripts/lib/literal-media-input.mjs +++ b/skills/hyperframes/scripts/lib/literal-media-input.mjs @@ -1,15 +1,14 @@ // The standalone twin of assertLiteralMediaInput in packages/parsers/src/literalMediaInput.ts, for inputs and outputs; a test keeps them equal. const PATTERN_IMAGE2_EXPANDS = /%\d*[d%*?[\]{}]/; const WILDCARD_MAKES_ANY_PERCENT_A_PATTERN = /[*?[{]/; -const URL_SCHEME = /^[a-z][a-z0-9+.-]+:/i; +const FORWARDED = /^https?:/i; /** ffmpeg's image2 reader or writer would use other files, folders included, than the one named. */ export function assertLiteralMediaPath(input) { - const local = !URL_SCHEME.test(input) || /^file:/i.test(input); const pattern = PATTERN_IMAGE2_EXPANDS.test(input) || (input.includes("%") && WILDCARD_MAKES_ANY_PERCENT_A_PATTERN.test(input)); - if (!local || !pattern) return; + if (FORWARDED.test(input) || !pattern) return; throw new Error( `[FFmpeg] refused media path "${input.split(/[\\/]/).pop()}": a "%" followed by "d", another "%" or a wildcard, ` + 'with or without digits between, or a "%" and a wildcard (*, ?, [ or {) anywhere in the path, would make ffmpeg ' + diff --git a/skills/hyperframes/scripts/literal-media-paths.test.mjs b/skills/hyperframes/scripts/literal-media-paths.test.mjs index 7f1708c99d..e6c127fd2c 100644 --- a/skills/hyperframes/scripts/literal-media-paths.test.mjs +++ b/skills/hyperframes/scripts/literal-media-paths.test.mjs @@ -35,7 +35,11 @@ test("the standalone check refuses exactly what the parsers check refuses", () = script("skills/hyperframes/scripts/lib/literal-media-input.mjs"), "utf8", ); - for (const name of ["PATTERN_IMAGE2_EXPANDS", "WILDCARD_MAKES_ANY_PERCENT_A_PATTERN"]) { + for (const name of [ + "PATTERN_IMAGE2_EXPANDS", + "WILDCARD_MAKES_ANY_PERCENT_A_PATTERN", + "FORWARDED", + ]) { const pattern = (text) => new RegExp(`const ${name} = (.+);`).exec(text)?.[1]; assert.ok(pattern(source), name); assert.equal(pattern(ours), pattern(source), name); @@ -46,10 +50,19 @@ test("the standalone check refuses exactly what the parsers check refuses", () = "file:/a/b%%.png", "/a/credentials.json%a{.xface", "/a/x{/y%a.png", + "concat:/a/b%d.png", + "subfile,,start,0,end,0,,:/a/b%d.png", + "data:/a%d.png", + "crypto:/a/b%d.png", ]) { assert.throws(() => assertLiteralMediaPath(path), PATTERN); } - for (const path of ["/a/100% done.mp4", "/a/x{y}.png", "https://cdn.example/a%2d.png"]) { + for (const path of [ + "/a/100% done.mp4", + "/a/x{y}.png", + "https://cdn.example/a%2d.png", + "http://cdn.example/a%2d.png", + ]) { assert.doesNotThrow(() => assertLiteralMediaPath(path)); } }); diff --git a/skills/media-use/scripts/lib/literal-media-input.mjs b/skills/media-use/scripts/lib/literal-media-input.mjs index 85840a06af..a9293fb008 100644 --- a/skills/media-use/scripts/lib/literal-media-input.mjs +++ b/skills/media-use/scripts/lib/literal-media-input.mjs @@ -1,15 +1,14 @@ // The standalone twin of assertLiteralMediaInput in packages/parsers/src/literalMediaInput.ts, for inputs and outputs; a test keeps them equal. const PATTERN_IMAGE2_EXPANDS = /%\d*[d%*?[\]{}]/; const WILDCARD_MAKES_ANY_PERCENT_A_PATTERN = /[*?[{]/; -const URL_SCHEME = /^[a-z][a-z0-9+.-]+:/i; +const FORWARDED = /^https?:/i; /** ffmpeg's image2 reader or writer would use other files, folders included, than the one named. */ export function assertLiteralMediaPath(input) { - const local = !URL_SCHEME.test(input) || /^file:/i.test(input); const pattern = PATTERN_IMAGE2_EXPANDS.test(input) || (input.includes("%") && WILDCARD_MAKES_ANY_PERCENT_A_PATTERN.test(input)); - if (!local || !pattern) return; + if (FORWARDED.test(input) || !pattern) return; throw new Error( `[FFmpeg] refused media path "${input.split(/[\\/]/).pop()}": a "%" followed by "d", another "%" or a wildcard, ` + 'with or without digits between, or a "%" and a wildcard (*, ?, [ or {) anywhere in the path, would make ffmpeg ' +