Skip to content

The agent that grows with you

fix(desktop): give the gateway save dialog a file type to keep

Jack Lau022647f13165
diff --git a/apps/desktop/electron/gateway-file-download-transport.test.ts b/apps/desktop/electron/gateway-file-download-transport.test.ts
index 48e5ff4c74e..5e18d18fdf4 100644
--- a/apps/desktop/electron/gateway-file-download-transport.test.ts
+++ b/apps/desktop/electron/gateway-file-download-transport.test.ts
@@ -124,8 +124,12 @@ test.each(authCases)(
'session-token',
context,
{
- showSaveDialog: async (settings: { defaultPath: string; title: string }): Promise<GatewaySaveDialogResult> => {
- expect(settings).toEqual({ defaultPath: 'server.bin', title: 'Save File' })
+ showSaveDialog: async (settings: { defaultPath: string; filters?: unknown; title: string }): Promise<GatewaySaveDialogResult> => {
+ // #92480: the dialog must carry the download's file type so Windows has
+ // a default extension to append; the resolved name reaches it intact.
+ expect(settings.defaultPath).toBe('server.bin')
+ expect(settings.title).toBe('Save File')
+ expect(settings.filters).toEqual([{ name: 'BIN File', extensions: ['bin'] }, { name: 'All Files', extensions: ['*'] }])
dialogEntered.resolve()
return decision.promise
@@ -384,3 +388,41 @@ test('data-URL fallback keeps a pre-existing temp collision and destination inta
expect(await fs.promises.readFile(destination, 'utf8')).toBe('original')
expect(await fs.promises.readFile(temp, 'utf8')).toBe('other download')
})
+
+// #92480: both gateway save dialogs opened with no `filters`, so the Windows
+// dialog offered only "All Files" and had no default extension to append. The
+// streaming path is asserted above inside the token-download test; this covers
+// the data-url fallback, which any gateway old enough to 404 the streaming
+// route falls back into. The helper's own behavior (whitelist, All Files last)
+// is covered in gateway-file-download.test.ts.
+test('the data-url save dialog carries a file type too', async (): Promise<void> => {
+ const suggested = 'suggested.bin'
+ const seen: { filters?: unknown }[] = []
+ const destination: string = path.join(directory, 'saved.bin')
+
+ const result: GatewayFileSaveResult = await saveGatewayDownload(
+ { dataUrl: '/api/fs/read-data-url?path=/x', download: '/download' },
+ { fallbackName: 'fallback.bin', suggested },
+ {
+ download: async (): Promise<GatewayFileSaveResult> => {
+ throw Object.assign(new Error('not found'), { statusCode: 404 })
+ },
+ readDataUrl: async (): Promise<string> => 'data:application/octet-stream,hello',
+ showSaveDialog: async (settings: { defaultPath: string; filters?: unknown }): Promise<GatewaySaveDialogResult> => {
+ seen.push(settings)
+
+ return { canceled: false, filePath: destination }
+ }
+ }
+ )
+
+ expect(result.saved).toBe(true)
+ expect(seen).toEqual([
+ {
+ defaultPath: suggested,
+ filters: [{ name: 'BIN File', extensions: ['bin'] }, { name: 'All Files', extensions: ['*'] }],
+ title: 'Save File'
+ }
+ ])
+ await expect(fs.promises.readFile(destination, 'utf8')).resolves.toBe('hello')
+})
diff --git a/apps/desktop/electron/gateway-file-download.test.ts b/apps/desktop/electron/gateway-file-download.test.ts
index 337c8ac13c4..e6cfd6924c2 100644
--- a/apps/desktop/electron/gateway-file-download.test.ts
+++ b/apps/desktop/electron/gateway-file-download.test.ts
@@ -15,6 +15,7 @@ import {
parseDataUrlToBuffer,
pumpStreamToFile,
resolveGatewayFileBackend,
+ saveDialogFilters,
writeBufferToFile
} from './gateway-file-download'
@@ -318,6 +319,7 @@ test('isNotFoundError matches only HTTP 404', () => {
assert.equal(isNotFoundError(null), false)
})
+
test('resolveGatewayFileBackend pins registered files to their owning connection', async () => {
const calls: string[] = []
@@ -369,3 +371,63 @@ test('resolveGatewayFileBackend preserves the legacy route when no connection ow
assert.equal(route.profile, 'coder')
assert.deepEqual(route.connection, { baseUrl: 'http://local.invalid' })
})
+
+// #92480: a .pptx saved through the gateway dialog arrived as a typeless
+// "File". The name reaching the dialog was correct; the dialog had no file
+// type to keep it with, so Windows had no default extension to append.
+test('saveDialogFilters offers the download own type before All Files', () => {
+ assert.deepEqual(saveDialogFilters('Presentation_2026-08-14.pptx'), [
+ { name: 'PPTX File', extensions: ['pptx'] },
+ { name: 'All Files', extensions: ['*'] }
+ ])
+ assert.deepEqual(saveDialogFilters('report.pdf'), [
+ { name: 'PDF File', extensions: ['pdf'] },
+ { name: 'All Files', extensions: ['*'] }
+ ])
+})
+
+test('saveDialogFilters keeps All Files last so any name stays saveable', () => {
+ // The escape hatch must survive every branch: a filter list without it turns
+ // a save dialog into a rename requirement.
+ for (const name of ['a.pptx', 'a.tar.gz', 'noext', '.gitignore', '', null]) {
+ const filters = saveDialogFilters(name)
+
+ assert.deepEqual(filters[filters.length - 1], { name: 'All Files', extensions: ['*'] })
+ }
+})
+
+test('saveDialogFilters normalizes case and reads only the last extension', () => {
+ assert.deepEqual(saveDialogFilters('SHOUTING.PDF')[0], { name: 'PDF File', extensions: ['pdf'] })
+ // Not 'tar.gz': the shell appends one extension, and gz is the one the name
+ // actually ends with.
+ assert.deepEqual(saveDialogFilters('archive.tar.gz')[0], { name: 'GZ File', extensions: ['gz'] })
+})
+
+test('saveDialogFilters falls back to All Files when there is no usable extension', () => {
+ // A dotfile has no extension in path.extname terms, and inventing one from
+ // the basename would offer to save .gitignore as "GITIGNORE File".
+ assert.deepEqual(saveDialogFilters('.gitignore'), [{ name: 'All Files', extensions: ['*'] }])
+ assert.deepEqual(saveDialogFilters('README'), [{ name: 'All Files', extensions: ['*'] }])
+ assert.deepEqual(saveDialogFilters(''), [{ name: 'All Files', extensions: ['*'] }])
+ assert.deepEqual(saveDialogFilters(undefined), [{ name: 'All Files', extensions: ['*'] }])
+})
+
+test('saveDialogFilters refuses an extension it cannot vouch for', () => {
+ // The name can come from a server-supplied Content-Disposition header, so the
+ // extension is whitelisted rather than merely extracted. Rejection is not a
+ // failure: it saves under All Files, which is today's behavior.
+ assert.deepEqual(saveDialogFilters('x.' + 'a'.repeat(17)), [{ name: 'All Files', extensions: ['*'] }])
+ assert.deepEqual(saveDialogFilters('x.p p t'), [{ name: 'All Files', extensions: ['*'] }])
+ assert.deepEqual(saveDialogFilters('x.pp*t'), [{ name: 'All Files', extensions: ['*'] }])
+ assert.deepEqual(saveDialogFilters('x.p;t'), [{ name: 'All Files', extensions: ['*'] }])
+})
+
+test('saveDialogFilters reads the basename, not a directory component', () => {
+ // filenameFromContentDisposition already reduces to a basename; this makes
+ // the helper safe on its own rather than only in that company.
+ assert.deepEqual(saveDialogFilters('/tmp/a.pdf/report.pptx')[0], {
+ name: 'PPTX File',
+ extensions: ['pptx']
+ })
+})
+
diff --git a/apps/desktop/electron/gateway-file-download.ts b/apps/desktop/electron/gateway-file-download.ts
index b83d0d6fcad..2885bfd8d9f 100644
--- a/apps/desktop/electron/gateway-file-download.ts
+++ b/apps/desktop/electron/gateway-file-download.ts
@@ -330,7 +330,43 @@ export function filenameFromContentDisposition(value: unknown): string {
}
}
+export interface SaveDialogFilter {
+ name: string
+ extensions: string[]
+}
+
+const ALL_FILES: SaveDialogFilter = { name: 'All Files', extensions: ['*'] }
+
+// Build the Electron save-dialog `filters` for a resolved download name.
+//
+// Not cosmetic. Electron hands `filters` to the platform save dialog as its set
+// of file types, and on Windows the selected type is also what supplies the
+// default extension. Omit it and the dialog has exactly one type, "All Files",
+// with no default extension to append — so a name the shell chose to display
+// without its extension is then SAVED without it, and a .pptx lands as a
+// typeless "File" that Explorer cannot open (#92480). The image-save dialog
+// already carries a filter for the same reason.
+//
+// The extension is whitelisted rather than merely extracted, because the name
+// it comes from can be attacker-influenced: `filenameFromContentDisposition`
+// reads a server-supplied header. A filter is a poor injection target, but an
+// unbounded string from the network has no business reaching a native dialog,
+// and anything failing the test still saves — under "All Files", exactly as
+// it does today.
+export function saveDialogFilters(filename: unknown): SaveDialogFilter[] {
+ const name = path.basename(String(filename || '').trim())
+ const ext = path.extname(name).replace(/^\./, '').toLowerCase()
+
+ if (!/^[a-z0-9]{1,16}$/.test(ext)) {
+ return [ALL_FILES]
+ }
+
+ return [{ name: `${ext.toUpperCase()} File`, extensions: [ext] }, ALL_FILES]
+}
+
// Preserve file URIs: only the gateway knows its native drive/UNC semantics.
+// Normalize a gateway file path that may arrive as a bare path or a file:// URL.
+
export function gatewayFilePath(rawPath: unknown): string {
return String(rawPath || '').trim()
}
@@ -353,6 +389,7 @@ export interface GatewayFileSaveResult {
export interface GatewaySaveDialogOptions {
defaultPath: string
+ filters?: SaveDialogFilter[]
title: string
}
@@ -392,7 +429,11 @@ export async function finalizeGatewayDownload(
const filename: string = filenameFromContentDisposition(disposition) || context.suggested || context.fallbackName
try {
- const result: GatewaySaveDialogResult = await deps.showSaveDialog({ defaultPath: filename, title: 'Save File' })
+ const result: GatewaySaveDialogResult = await deps.showSaveDialog({
+ defaultPath: filename,
+ filters: saveDialogFilters(filename),
+ title: 'Save File'
+ })
if (result.canceled || !result.filePath) {
abort()
@@ -461,6 +502,7 @@ export async function saveGatewayDownload(
const result: GatewaySaveDialogResult = await deps.showSaveDialog({
defaultPath: context.suggested || context.fallbackName,
+ filters: saveDialogFilters(context.suggested || context.fallbackName),
title: 'Save File'
})