Skip to content

The agent that grows with you

fix(desktop): open public http(s) pages in the preview pane

xxxigm986192cd1d8d
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index a55fb1bd2b0..54cb24d364a 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -448,6 +448,7 @@ import { capturePreviewContents } from './preview-capture'
import { onPreviewWatchOwnerDestroyed, sendPreviewFileChangedToOwner } from './preview-file-watch'
import { hasClosePreviewFlag, previewGuestInputAction } from './preview-guest-escape'
import { PreviewReachRegistry } from './preview-reach'
+import { previewHttpUrlTarget } from './preview-url-target'
import {
createPrimaryRemoteConnection,
FirstRunSetupResetError,
@@ -1703,7 +1704,6 @@ const MEDIA_MIME_TYPES = {
const PREVIEW_HTML_EXTENSIONS = new Set(['.html', '.htm'])
const PREVIEW_PDF_EXTENSIONS = new Set(['.pdf'])
const PREVIEW_WATCH_DEBOUNCE_MS = 120
-const LOCAL_PREVIEW_HOSTS = new Set(['0.0.0.0', '127.0.0.1', '::1', '[::1]', 'localhost'])
const TEXT_PREVIEW_MAX_BYTES = 512 * 1024
const PREVIEW_LANGUAGE_BY_EXT = {
@@ -6053,10 +6053,6 @@ async function writeComposerImage(buffer, ext = '.png', name = '') {
return filePath
}
-function previewLabelForUrl(url) {
- return `${url.host}${url.pathname === '/' ? '' : url.pathname}`
-}
-
function expandUserPath(filePath) {
const value = String(filePath || '').trim()
@@ -6185,30 +6181,6 @@ async function previewFileTarget(rawTarget, baseDir) {
}
}
-function previewUrlTarget(rawTarget) {
- const raw = String(rawTarget || '').trim()
- const url = new URL(raw)
-
- if (!['http:', 'https:'].includes(url.protocol)) {
- return null
- }
-
- if (!LOCAL_PREVIEW_HOSTS.has(url.hostname.toLowerCase())) {
- return null
- }
-
- if (url.hostname === '0.0.0.0') {
- url.hostname = '127.0.0.1'
- }
-
- return {
- kind: 'url',
- label: previewLabelForUrl(url),
- source: raw,
- url: url.toString()
- }
-}
-
async function normalizePreviewTarget(rawTarget, baseDir) {
const raw = String(rawTarget || '').trim()
@@ -6218,7 +6190,7 @@ async function normalizePreviewTarget(rawTarget, baseDir) {
try {
if (/^https?:\/\//i.test(raw)) {
- return previewUrlTarget(raw)
+ return previewHttpUrlTarget(raw)
}
return await previewFileTarget(raw, baseDir)
diff --git a/apps/desktop/electron/preview-url-target.ts b/apps/desktop/electron/preview-url-target.ts
new file mode 100644
index 00000000000..229f78969a8
--- /dev/null
+++ b/apps/desktop/electron/preview-url-target.ts
@@ -0,0 +1,46 @@
+/**
+ * HTTP(S) targets for the in-app preview pane.
+ *
+ * The pane is the desktop's browser: a public page the user asked to open
+ * has to navigate. Loopback stays special only for the 0.0.0.0 bind address,
+ * which the webview cannot load — rewrite that host to 127.0.0.1. Rejecting
+ * every other host made `preview.open` succeed while the pane stayed blank.
+ */
+
+export interface PreviewHttpUrlTarget {
+ kind: 'url'
+ label: string
+ source: string
+ url: string
+}
+
+export function previewHttpUrlTarget(rawTarget: string): PreviewHttpUrlTarget | null {
+ const raw = String(rawTarget || '').trim()
+
+ if (!raw) {
+ return null
+ }
+
+ let url: URL
+
+ try {
+ url = new URL(raw)
+ } catch {
+ return null
+ }
+
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
+ return null
+ }
+
+ if (url.hostname === '0.0.0.0') {
+ url.hostname = '127.0.0.1'
+ }
+
+ return {
+ kind: 'url',
+ label: `${url.host}${url.pathname === '/' ? '' : url.pathname}`,
+ source: raw,
+ url: url.toString()
+ }
+}