diff --git a/sidecars/panel/app.js b/sidecars/panel/app.js index bc1a20b..0c3efc3 100644 --- a/sidecars/panel/app.js +++ b/sidecars/panel/app.js @@ -8677,7 +8677,7 @@ function getPickerTypesForFilename(filename, mimeType) { * @returns {Promise} */ async function revealExportedFile(filePath) { - if (!filePath) return false; + if (!filePath || !isHostEmbeddedWebview()) return false; try { const res = await callSidecarApi('/api/reveal-file', { method: 'POST', @@ -8749,25 +8749,25 @@ async function runExportPipelineWithFeedback({ backendPayload, writablePayload, }) { - const { res: localRes } = await requestLocalBackendExport(backendPayload); - if (localRes && localRes.ok && localRes.filePath) { - const savedPath = String(localRes.filePath); - const savedName = String(localRes.fileName || safeName); - showUiToast({ - message: t('export.savedLocal', { file: savedName }), - detail: savedPath, - actionLabel: t('export.openFolder'), - onAction: () => { - void revealExportedFile(savedPath); - }, - tone: 'ok', - durationMs: 7000, - }); - return { ok: true, mode: 'local', filePath: savedPath, fileName: savedName }; - } - - // 在 Antigravity Sidecar / 双栖 Electron 宿主内, 会被静默吞掉,绝不误报“已下载到浏览器” if (isHostEmbeddedWebview()) { + const { res: localRes } = await requestLocalBackendExport(backendPayload); + if (localRes && localRes.ok && localRes.filePath) { + const savedPath = String(localRes.filePath); + const savedName = String(localRes.fileName || safeName); + showUiToast({ + message: t('export.savedLocal', { file: savedName }), + detail: savedPath, + actionLabel: t('export.openFolder'), + onAction: () => { + void revealExportedFile(savedPath); + }, + tone: 'ok', + durationMs: 7000, + }); + return { ok: true, mode: 'local', filePath: savedPath, fileName: savedName }; + } + + // 在 Antigravity Sidecar / 双栖 Electron 宿主内, 会被静默吞掉,绝不误报“已下载到浏览器” showUiToast({ message: t('export.failed'), detail: t('export.hostUnavailable'), diff --git a/sidecars/panel/backend/standalone-server.mjs b/sidecars/panel/backend/standalone-server.mjs index 9e2df9a..3cb0a92 100644 --- a/sidecars/panel/backend/standalone-server.mjs +++ b/sidecars/panel/backend/standalone-server.mjs @@ -21,7 +21,6 @@ import { ensureCliStatusLineConfigured } from './cli-statusline-config.mjs'; import { syncCloudCatalog } from './plugin-updater.mjs'; const MAX_JSON_BODY_BYTES = 1024 * 1024; -const MAX_EXPORT_JSON_BODY_BYTES = 8 * 1024 * 1024; const AUTH_COOKIE_NAME = 'ag_um_token'; const NO_STORE_HEADERS = { 'Cache-Control': 'no-store, no-cache, must-revalidate', @@ -512,10 +511,7 @@ export function createStandaloneWebServer(options = {}) { } if (pathname.startsWith('/api/')) { - if ( - (pathname === '/api/export-file' || pathname === '/api/reveal-file') && - !isLoopbackRemoteAddress(req.socket?.remoteAddress) - ) { + if (pathname === '/api/export-file' || pathname === '/api/reveal-file') { sendResponse( res, 403, @@ -523,7 +519,7 @@ export function createStandaloneWebServer(options = {}) { JSON.stringify({ ok: false, fallbackBrowser: true, - error: 'Local file export is restricted to loopback clients', + error: 'Standalone web mode uses browser file download instead of server disk export', }) ); return; @@ -533,11 +529,7 @@ export function createStandaloneWebServer(options = {}) { data = Object.fromEntries(urlObj.searchParams.entries()); delete data.token; } else if (method === 'POST') { - const maxBody = - pathname === '/api/export-file' || pathname === '/api/settings' - ? MAX_EXPORT_JSON_BODY_BYTES - : MAX_JSON_BODY_BYTES; - const rawBody = await readRawBody(req, maxBody); + const rawBody = await readRawBody(req, MAX_JSON_BODY_BYTES); if (rawBody.trim()) { try { data = JSON.parse(rawBody); @@ -557,8 +549,7 @@ export function createStandaloneWebServer(options = {}) { pathname === '/api/settings' && data && typeof data === 'object' && - typeof data.__exportFileAction === 'string' && - !isLoopbackRemoteAddress(req.socket?.remoteAddress) + typeof data.__exportFileAction === 'string' ) { sendResponse( res, @@ -567,7 +558,7 @@ export function createStandaloneWebServer(options = {}) { JSON.stringify({ ok: false, fallbackBrowser: true, - error: 'Local file export is restricted to loopback clients', + error: 'Standalone web mode uses browser file download instead of server disk export', }) ); return; diff --git a/sidecars/panel/index.html b/sidecars/panel/index.html index 9b4bd15..62aad77 100644 --- a/sidecars/panel/index.html +++ b/sidecars/panel/index.html @@ -4,11 +4,11 @@ 用量监控 - +
- + diff --git a/sidecars/panel/src/core/fileExportClient.js b/sidecars/panel/src/core/fileExportClient.js index f071e1f..dad2b79 100644 --- a/sidecars/panel/src/core/fileExportClient.js +++ b/sidecars/panel/src/core/fileExportClient.js @@ -46,7 +46,7 @@ function getPickerTypesForFilename(filename, mimeType) { * @returns {Promise} */ export async function revealExportedFile(filePath) { - if (!filePath) return false; + if (!filePath || !isHostEmbeddedWebview()) return false; try { const res = await callSidecarApi('/api/reveal-file', { method: 'POST', @@ -118,25 +118,25 @@ async function runExportPipelineWithFeedback({ backendPayload, writablePayload, }) { - const { res: localRes } = await requestLocalBackendExport(backendPayload); - if (localRes && localRes.ok && localRes.filePath) { - const savedPath = String(localRes.filePath); - const savedName = String(localRes.fileName || safeName); - showUiToast({ - message: t('export.savedLocal', { file: savedName }), - detail: savedPath, - actionLabel: t('export.openFolder'), - onAction: () => { - void revealExportedFile(savedPath); - }, - tone: 'ok', - durationMs: 7000, - }); - return { ok: true, mode: 'local', filePath: savedPath, fileName: savedName }; - } - - // 在 Antigravity Sidecar / 双栖 Electron 宿主内,
会被静默吞掉,绝不误报“已下载到浏览器” if (isHostEmbeddedWebview()) { + const { res: localRes } = await requestLocalBackendExport(backendPayload); + if (localRes && localRes.ok && localRes.filePath) { + const savedPath = String(localRes.filePath); + const savedName = String(localRes.fileName || safeName); + showUiToast({ + message: t('export.savedLocal', { file: savedName }), + detail: savedPath, + actionLabel: t('export.openFolder'), + onAction: () => { + void revealExportedFile(savedPath); + }, + tone: 'ok', + durationMs: 7000, + }); + return { ok: true, mode: 'local', filePath: savedPath, fileName: savedName }; + } + + // 在 Antigravity Sidecar / 双栖 Electron 宿主内, 会被静默吞掉,绝不误报“已下载到浏览器” showUiToast({ message: t('export.failed'), detail: t('export.hostUnavailable'), diff --git a/sidecars/panel/test/practical-enhancements.test.mjs b/sidecars/panel/test/practical-enhancements.test.mjs index bf7652b..4505917 100644 --- a/sidecars/panel/test/practical-enhancements.test.mjs +++ b/sidecars/panel/test/practical-enhancements.test.mjs @@ -1919,6 +1919,9 @@ test('Feature 14: compact vertical ActionMenu replaces export bars, backend expo const { document } = createMockDom(); globalThis.document = document; globalThis.window = { + sidecar: { + fetch: (url, init) => globalThis.fetch(url, init), + }, addEventListener: () => {}, removeEventListener: () => {}, requestAnimationFrame: (cb) => { @@ -2102,6 +2105,40 @@ test('Feature 14: compact vertical ActionMenu replaces export bars, backend expo assert.equal(failedRes.ok, false); assert.equal(failedRes.mode, 'failed'); assert.equal(uiStore.getState().toast?.tone, 'warn'); + + // 7. Standalone browser mode (no window.sidecar and no window bridge) skips /api/export-file + // completely (even over ssh -L / loopback reverse proxy) and invokes native Save Picker directly + delete globalThis.window.sidecar; + let browserExportBackendCalled = false; + globalThis.fetch = async () => { + browserExportBackendCalled = true; + return { ok: true, status: 200, json: async () => ({ ok: true, filePath: '/remote/Downloads/leak.json' }) }; + }; + let pickedSuggestedName = ''; + let pickedWrittenPayload = null; + globalThis.window.showSaveFilePicker = async (opts) => { + pickedSuggestedName = opts?.suggestedName || ''; + return { + name: pickedSuggestedName, + createWritable: async () => ({ + write: async (data) => { + pickedWrittenPayload = data; + }, + close: async () => {}, + }), + }; + }; + const standalonePickerRes = await exportTextFileWithFeedback( + 'standalone-report.md', + '# Hello Browser\n', + 'text/markdown' + ); + assert.equal(browserExportBackendCalled, false, 'Standalone browser must NOT call /api/export-file'); + assert.equal(standalonePickerRes.ok, true); + assert.equal(standalonePickerRes.mode, 'picker'); + assert.equal(pickedSuggestedName, 'standalone-report.md'); + assert.equal(pickedWrittenPayload, '# Hello Browser\n'); + assert.equal(await revealExportedFile('/remote/Downloads/leak.json'), false); }); test('P0 regression: V4 display-projected steps open inspector, preserve waterfall/diagnostics/XLSX duration, match anomaly events, compute harmonic speed, and clean up UI observers/i18n', async (t) => { diff --git a/sidecars/panel/test/window-bridge.test.mjs b/sidecars/panel/test/window-bridge.test.mjs index ee78cb0..e91da06 100644 --- a/sidecars/panel/test/window-bridge.test.mjs +++ b/sidecars/panel/test/window-bridge.test.mjs @@ -4417,6 +4417,31 @@ test('Standalone Web server enforces loopback/non-loopback security boundaries, data: { range: 'today' }, }); + // Loopback ssh -L / reverse-proxy requests to /api/export-file, /api/reveal-file, and __exportFileAction return 403 fallbackBrowser + const blockedExport = await fetch(`${st.url}/api/export-file`, { + method: 'POST', + headers: { Origin: st.url, 'Content-Type': 'application/json' }, + body: JSON.stringify({ filename: 'leak.json', content: '{}' }), + }); + assert.equal(blockedExport.status, 403); + assert.equal((await blockedExport.json()).fallbackBrowser, true); + + const blockedReveal = await fetch(`${st.url}/api/reveal-file`, { + method: 'POST', + headers: { Origin: st.url, 'Content-Type': 'application/json' }, + body: JSON.stringify({ filePath: '/tmp/leak.json' }), + }); + assert.equal(blockedReveal.status, 403); + assert.equal((await blockedReveal.json()).fallbackBrowser, true); + + const blockedSettingsExport = await fetch(`${st.url}/api/settings`, { + method: 'POST', + headers: { Origin: st.url, 'Content-Type': 'application/json' }, + body: JSON.stringify({ __exportFileAction: 'save', payload: { filename: 'leak.json', content: '{}' } }), + }); + assert.equal(blockedSettingsExport.status, 403); + assert.equal((await blockedSettingsExport.json()).fallbackBrowser, true); + // Cross-site Origin blocked with 403 const crossSiteRes = await fetch(`${st.url}/api/telemetry`, { headers: { Origin: 'https://evil.example' },