TagSpaces is an offline, open source, document manager with tagging support
chore: hardening link opening
Ilian Sapundshiev6c6c264b2714
diff --git a/src/main/mainEvents.ts b/src/main/mainEvents.tsindex 2db9da147..7abc16a75 100644--- a/src/main/mainEvents.ts+++ b/src/main/mainEvents.ts@@ -567,6 +567,29 @@ export default function loadMainEvents() {});});ipcMain.on('openUrl', async (event, url) => {+ // Scheme allowlist: shell.openExternal hands the URL to the OS, which+ // means a malicious file:// or custom scheme (ms-msdt:, search-ms:, etc.)+ // could trigger arbitrary code execution. Only forward URLs whose scheme+ // is on the allowlist below. ts:// links are routed inside the renderer+ // and must never reach this handler.+ if (typeof url !== 'string' || url.length === 0) {+ console.warn('openUrl: ignored non-string url');+ return;+ }+ let parsed: URL;+ try {+ parsed = new URL(url);+ } catch (e) {+ console.warn('openUrl: ignored unparseable url:', url);+ return;+ }+ const allowed = new Set(['http:', 'https:', 'mailto:', 'tel:']);+ if (!allowed.has(parsed.protocol)) {+ console.warn(+ 'openUrl: blocked scheme ' + parsed.protocol + ' for url: ' + url,+ );+ return;+ }await shell.openExternal(url);});ipcMain.handle('selectDirectoryDialog', async () => {diff --git a/src/renderer/components/EntryContainer.tsx b/src/renderer/components/EntryContainer.tsxindex 23ff4a46a..08247cdb5 100644--- a/src/renderer/components/EntryContainer.tsx+++ b/src/renderer/components/EntryContainer.tsx@@ -268,9 +268,11 @@ function EntryContainer() {case 'playbackEnded':openNextFileAction();break;- // case 'openLinkExternally':- // // openLink(data.link);- // break;+ // 'openLinkExternally' is handled by the global window-message+ // listener in MainPage.tsx, which routes through openLink() for all+ // extension iframes. Do NOT handle it here — that would double-fire+ // openLink() and race the entry-open sequence (ts:// links break,+ // among other things).case 'loadDefaultTextContent':if (!openedEntry || !openedEntry.path) {// || openedEntry.changed) {diff --git a/src/renderer/containers/MainPage.tsx b/src/renderer/containers/MainPage.tsxindex ba6273b1d..29bd24285 100644--- a/src/renderer/containers/MainPage.tsx+++ b/src/renderer/containers/MainPage.tsx@@ -118,10 +118,19 @@ function MainPage() {);useEventListener('message', (e) => {+ // Only accept messages from same-origin frames or sandboxed extension+ // iframes (origin 'null'). Without this, any embedded frame could trigger+ // openLink() with attacker-controlled URLs.+ const trusted = e.origin === window.location.origin || e.origin === 'null';+ if (!trusted) return;if (typeof e.data === 'string') {try {const data = JSON.parse(e.data);- if (data.command === 'openLinkExternally') {+ if (+ data.command === 'openLinkExternally' &&+ typeof data.link === 'string' &&+ data.link.length > 0+ ) {openLink(data.link, { fullWidth: false });}} catch (ex) {