Skip to content

Commit dfb749a

Browse files
committed
fix(studio-server): one rule for request file paths, so loudness finds files named with %, # or ?
1 parent 4fe9183 commit dfb749a

4 files changed

Lines changed: 32 additions & 17 deletions

File tree

‎packages/studio-server/src/helpers/loudness.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { promisify } from "node:util";
44
import { isAudibleVideoElement } from "@hyperframes/core/audible-video";
55
import { formatAudioGain, MAX_AUDIO_GAIN_DB } from "@hyperframes/core/audio-gain";
66
import { findFfBinary } from "@hyperframes/parsers/ff-binaries";
7+
import { requestedProjectPath } from "./requestSubPath.js";
78

89
const execFileAsync = promisify(execFile);
910

@@ -333,13 +334,7 @@ export function audioTags(html: string): AudioTag[] {
333334

334335
/** Resolve only a file contained by the project. Remote and traversal sources are not measured. */
335336
export function resolveLocalAudioPath(projectDir: string, src: string): string {
336-
const withoutSuffix = src.split(/[?#]/, 1)[0] ?? "";
337-
let decoded = "";
338-
try {
339-
decoded = decodeURIComponent(withoutSuffix);
340-
} catch {
341-
throw new Error(`Audio source must be a local project file: ${src}`);
342-
}
337+
const decoded = requestedProjectPath(projectDir, src);
343338
if (!decoded || isAbsolute(decoded) || /^(?:[a-z][a-z\d+.-]*:|\/\/)/i.test(decoded)) {
344339
throw new Error(`Audio source must be a local project file: ${src}`);
345340
}

‎packages/studio-server/src/helpers/requestSubPath.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { decodeWellFormedEscapes } from "@hyperframes/parsers/asset-paths";
1+
import { existsSync } from "node:fs";
2+
import { decodedUrlPath, decodeWellFormedEscapes } from "@hyperframes/parsers/asset-paths";
3+
import { resolveWithinProject } from "./safePath.js";
24
export { decodeWellFormedEscapes } from "@hyperframes/parsers/asset-paths";
35

46
// The decoded path after `route` ("projects/:id/preview", "composition"), cut by segment from the raw URL:
@@ -9,3 +11,10 @@ export function requestSubPath(url: string, route: string): string {
911
const start = segments.indexOf(routeSegments[0] ?? "");
1012
return decodeWellFormedEscapes(segments.slice(start + routeSegments.length).join("/"));
1113
}
14+
15+
// A project file a request names: the literal name when that file exists, otherwise the field read as a URL.
16+
export function requestedProjectPath(projectDir: string, field: string): string {
17+
const trimmed = field.trim().replace(/^[.]\//, "");
18+
const named = resolveWithinProject(projectDir, trimmed);
19+
return named && existsSync(named) ? trimmed : decodedUrlPath(trimmed);
20+
}

‎packages/studio-server/src/routes/loudness.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ describe("POST /projects/:id/loudness/normalize", () => {
6767
expect(plan.limitedBy).toBe("true-peak");
6868
});
6969

70+
it("measures a project file whose name holds %, # or ?, given raw or as a URL", async () => {
71+
const measure = vi.fn<MeasureLoudness>(async () => ({ integratedLufs: -16, truePeakDbfs: -3 }));
72+
const { post, projectDir } = setup(measure);
73+
const name = "sale 50% off #1?.mp3";
74+
writeFileSync(join(projectDir, name), "audio");
75+
76+
for (const src of [name, encodeURIComponent(name), "talk.mp4?v=2#t=1"]) {
77+
expect((await post({ src })).status, src).toBe(200);
78+
}
79+
expect(measure.mock.calls.map(([file]) => file)).toEqual([
80+
join(projectDir, name),
81+
join(projectDir, name),
82+
join(projectDir, "talk.mp4"),
83+
]);
84+
});
85+
7086
it("refuses a source outside the project", async () => {
7187
const { post } = setup(async () => ({ integratedLufs: -16, truePeakDbfs: -3 }));
7288
expect((await post({ src: "../etc/passwd" })).status).toBe(400);

‎packages/studio-server/src/routes/media.ts‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { streamSSE } from "hono/streaming";
33
import { existsSync } from "node:fs";
44
import { basename, dirname, extname, join, posix } from "node:path";
55
import type { MediaProcessingJobState, StudioApiAdapter } from "../types.js";
6+
import { requestedProjectPath } from "../helpers/requestSubPath.js";
67
import { mkdirWithinProject, pinWithinProject, resolveWithinProject } from "../helpers/safePath.js";
78
import { probeMediaMetadata } from "../helpers/mediaMetadata.js";
89

@@ -45,12 +46,6 @@ function isImagePath(path: string): boolean {
4546
return IMAGE_EXTENSIONS.has(extname(path).toLowerCase());
4647
}
4748

48-
function normalizeProjectAssetPath(path: string, projectDir: string): string {
49-
const trimmed = path.trim().replace(/^[.]\//, "");
50-
const named = resolveWithinProject(projectDir, trimmed);
51-
return named && existsSync(named) ? trimmed : trimmed.replace(/[?#].*$/, "");
52-
}
53-
5449
function containsNullByte(path: string): boolean {
5550
return path.includes("\0");
5651
}
@@ -148,7 +143,7 @@ export function registerMediaRoutes(
148143
const project = await adapter.resolveProject(c.req.param("id"));
149144
if (!project) return c.json({ error: "not found" }, 404);
150145

151-
const assetPath = normalizeProjectAssetPath(c.req.query("path") ?? "", project.dir);
146+
const assetPath = requestedProjectPath(project.dir, c.req.query("path") ?? "");
152147
if (!assetPath) return c.json({ error: "path required" }, 400);
153148
if (containsNullByte(assetPath)) return c.json({ error: "forbidden" }, 403);
154149
if (/^(?:https?:|data:|blob:)/i.test(assetPath)) {
@@ -177,7 +172,7 @@ export function registerMediaRoutes(
177172

178173
const body = (await c.req.json().catch(() => ({}))) as BackgroundRemovalBody;
179174
const inputAssetPath = body.inputPath
180-
? normalizeProjectAssetPath(body.inputPath, project.dir)
175+
? requestedProjectPath(project.dir, body.inputPath)
181176
: "";
182177
if (!inputAssetPath) return c.json({ error: "inputPath required" }, 400);
183178
if (containsNullByte(inputAssetPath)) return c.json({ error: "forbidden" }, 403);
@@ -196,7 +191,7 @@ export function registerMediaRoutes(
196191
}
197192

198193
const requestedOutput = body.outputPath
199-
? posix.normalize(normalizeProjectAssetPath(body.outputPath, project.dir))
194+
? posix.normalize(requestedProjectPath(project.dir, body.outputPath))
200195
: "";
201196
if (requestedOutput && containsNullByte(requestedOutput)) {
202197
return c.json({ error: "forbidden" }, 403);

0 commit comments

Comments
 (0)