Skip to content

The agent that grows with you

fix(desktop): play remote media whose file name holds a bare percent sign

emozillaa517bad018dd
diff --git a/apps/desktop/electron/media-protocol.test.ts b/apps/desktop/electron/media-protocol.test.ts
index 83da3062f2f..15f1d6ee580 100644
--- a/apps/desktop/electron/media-protocol.test.ts
+++ b/apps/desktop/electron/media-protocol.test.ts
@@ -61,6 +61,44 @@ describe('media protocol helpers', () => {
}
)
+ // A file name may hold a `%` that starts no escape. Agents write such a file URL as is and the
+ // gateway reads it back as is, so it plays like any other name.
+ it.each([
+ ['file:///tmp/100%.mp4', '/tmp/100%.mp4'],
+ ['file:///tmp/50% off.mp4', '/tmp/50% off.mp4'],
+ ['file:///tmp/%zz%20mix.mp4', '/tmp/%zz mix.mp4'],
+ ['file:///C:/media/100%.mp4', 'file:///C:/media/100%.mp4']
+ ])('plays remote media whose name holds a bare %%: %s', async (url, expectedPath) => {
+ const deps = dependencies()
+
+ expect(isStreamableMediaPath(url)).toBe(true)
+
+ const response = await createMediaProtocolHandler(deps)(request(`hermes-media://remote/${encodeURIComponent(url)}`))
+
+ expect(response.status).toBe(206)
+ expect(deps.fetchRemote).toHaveBeenCalledOnce()
+ expect(new URL(vi.mocked(deps.fetchRemote).mock.calls[0][0]).searchParams.get('path')).toBe(expectedPath)
+ })
+
+ // Escaping a bare `%` must not let a separator through: one encoded beside it, once or twice,
+ // or spelled by the escape itself, is still refused.
+ it.each([
+ 'file:///tmp/100%/..%2f..%2fetc/clip.mp4',
+ 'file:///tmp/100%/..%252f..%252fetc/clip.mp4',
+ 'file:///tmp/%%32%66etc/clip.mp4',
+ 'file:///tmp/%2%66etc/clip.mp4',
+ 'file:///C:/media/100%/..%5cclip.mp4',
+ 'file://%invalid/clip.mp4'
+ ])('still refuses %s', async url => {
+ const deps = dependencies()
+
+ expect(isStreamableMediaPath(url)).toBe(false)
+ expect(
+ (await createMediaProtocolHandler(deps)(request(`hermes-media://remote/${encodeURIComponent(url)}`))).status
+ ).toBe(415)
+ expect(deps.fetchRemote).not.toHaveBeenCalled()
+ })
+
it('forwards range/cache negotiation headers but strips renderer credentials', () => {
const headers = mediaRequestHeaders(
new Headers({
diff --git a/apps/desktop/electron/media-protocol.ts b/apps/desktop/electron/media-protocol.ts
index a30e84048f5..ad0e74c0711 100644
--- a/apps/desktop/electron/media-protocol.ts
+++ b/apps/desktop/electron/media-protocol.ts
@@ -98,8 +98,18 @@ export function mediaProtocolDownloadName(rawUrl: string): null | string {
return target.filePath.split(/[\\/]/).filter(Boolean).pop() || null
}
+/**
+ * The native path a media `file:` URL names. Agents write these URLs by hand, and a name may hold a
+ * `%` that starts no escape (`100%.mp4`), which the gateway reads as written. That `%` is escaped
+ * before the shared parser reads the URL, so the name plays while an encoded separator (`%2f`,
+ * `%5c`, or one encoded twice) is still refused.
+ */
+function mediaFileUrlPath(fileUrl: string): null | string {
+ return fileUrlToNativePath(fileUrl.replace(/%(?![0-9a-f]{2})/gi, '%25'))
+}
+
export function isStreamableMediaPath(filePath: string): boolean {
- const mediaPath = /^file:/i.test(filePath) ? fileUrlToNativePath(filePath) : filePath
+ const mediaPath = /^file:/i.test(filePath) ? mediaFileUrlPath(filePath) : filePath
if (mediaPath === null) {
return false
@@ -133,7 +143,7 @@ export function remoteMediaEndpoint(baseUrl: string, filePath: string, profile?:
}
if (/^file:/i.test(filePath)) {
- const nativePath = fileUrlToNativePath(filePath)
+ const nativePath = mediaFileUrlPath(filePath)
if (nativePath === null) {
throw new Error('Invalid media file URL')