TagSpaces is an offline, open source, document manager with tagging support
feat: enable loading extconfig.json also from the electron profile folder
Ilian Sapundshievfc5b04ab8b18
diff --git a/src/main/main.ts b/src/main/main.tsindex d72d623cd..e13d2f62c 100644--- a/src/main/main.ts+++ b/src/main/main.ts@@ -22,6 +22,7 @@ import {import type { UtilityProcess } from 'electron';import windowStateKeeper from 'electron-window-state';import findFreePorts from 'find-free-ports';+import fs from 'fs';import path from 'path';import propertiesReader from 'properties-reader';import i18nInit from '../renderer/services/i18nInit';@@ -686,6 +687,26 @@ appipcMain.on('app-data-path-request', (event) => {event.returnValue = app.getPath('appData');});+ // Returns the raw contents of <userData>/extconfig.json so the renderer+ // can apply a profile-folder override that survives reinstalls. Returns+ // an empty string when the file is missing, unreadable, or exceeds the+ // 5 MB ceiling enforced by the renderer's loader.+ ipcMain.on('get-user-ext-config', (event) => {+ try {+ const filePath = path.join(app.getPath('userData'), 'extconfig.json');+ if (fs.existsSync(filePath)) {+ const stat = fs.statSync(filePath);+ if (stat.size <= 5 * 1024 * 1024) {+ event.returnValue = fs.readFileSync(filePath, 'utf8');+ return;+ }+ console.warn(`extconfig.json in userData exceeded 5 MB — ignored.`);+ }+ } catch (e) {+ console.warn('get-user-ext-config failed:', e);+ }+ event.returnValue = '';+ });ipcMain.on('app-version-request', (event) => {event.returnValue = app.getVersion();});diff --git a/src/main/preload.ts b/src/main/preload.tsindex 8bafde022..14973bcd6 100644--- a/src/main/preload.ts+++ b/src/main/preload.ts@@ -55,6 +55,7 @@ export type Channels =| 'load-extensions'| 'removeExtension'| 'getUserDataDir'+ | 'get-user-ext-config'| 'unZip'| 'getDirProperties'| 'folderChanged'diff --git a/src/renderer/reducers/index.ts b/src/renderer/reducers/index.tsindex 28e776de1..eb2e8be37 100644--- a/src/renderer/reducers/index.ts+++ b/src/renderer/reducers/index.ts@@ -186,56 +186,86 @@ const extConfigPath = AppConfig.isWeb// misconfiguration) doesn't make it into JSON.parse.const EXTCONFIG_MAX_SIZE = 5 * 1024 * 1024; // 5 MB-const xhr = new XMLHttpRequest();-xhr.open('GET', extConfigPath, false); // false = synchronous-try {- xhr.send(); // Will pause execution here until it finishes or fails- // Status 200 is for HTTP servers, Status 0 is for local file:// protocol- if ((xhr.status === 200 || xhr.status === 0) && xhr.responseText) {- // Content-Type guard — only accept what the server claims is JSON.- // file:// responses (status 0) typically don't set a Content-Type;- // empty header is tolerated. We only reject when something explicit- // and non-JSON came back (e.g. a dev-server fallback HTML page or a- // CDN error page with a 200).- const contentType = (- xhr.getResponseHeader('Content-Type') || ''- ).toLowerCase();- if (contentType && !contentType.startsWith('application/json')) {- console.warn(- `extconfig.json had unexpected Content-Type ${contentType} — ignored.`,- );- } else if (xhr.responseText.length > EXTCONFIG_MAX_SIZE) {- console.warn(- `extconfig.json exceeded ${EXTCONFIG_MAX_SIZE} bytes — ignored.`,- );- } else {- const jsonConfig = loadJSONString(xhr.responseText);- // Shape guard — loadJSONString returns undefined on malformed- // input and may parse legitimate-looking JSON that isn't a plain- // object (array, primitive). configureApp expects a record-like- // object.- if (- jsonConfig &&- typeof jsonConfig === 'object' &&- !Array.isArray(jsonConfig)- ) {- // Log keys only — never the values. extconfig.json may contain- // secrets (ExtAI[].apiKey, ExtLocations[].secretAccessKey, etc.)- // and dumping the full object to the console exposes them in- // DevTools, screenshots, and shared bug-report transcripts.- console.log('extconfig.json loaded — keys:', Object.keys(jsonConfig));- configureApp(jsonConfig);+function applyExtConfigText(text: string, source: string): boolean {+ if (text.length > EXTCONFIG_MAX_SIZE) {+ console.warn(+ `extconfig.json (${source}) exceeded ${EXTCONFIG_MAX_SIZE} bytes — ignored.`,+ );+ return false;+ }+ const jsonConfig = loadJSONString(text);+ // Shape guard — loadJSONString returns undefined on malformed+ // input and may parse legitimate-looking JSON that isn't a plain+ // object (array, primitive). configureApp expects a record-like+ // object.+ if (+ jsonConfig &&+ typeof jsonConfig === 'object' &&+ !Array.isArray(jsonConfig)+ ) {+ // Log keys only — never the values. extconfig.json may contain+ // secrets (ExtAI[].apiKey, ExtLocations[].secretAccessKey, etc.)+ // and dumping the full object to the console exposes them in+ // DevTools, screenshots, and shared bug-report transcripts.+ console.log(+ `extconfig.json (${source}) loaded — keys:`,+ Object.keys(jsonConfig),+ );+ configureApp(jsonConfig);+ return true;+ }+ console.warn(`extconfig.json (${source}) had unexpected shape — ignored.`);+ return false;+}++// Electron desktop: prefer <userData>/extconfig.json so deployer or end-user+// customisations survive app reinstalls/upgrades (the install-dir copy is+// blown away on every package install). Falls through to the install-dir XHR+// path below when no profile-folder file is present.+let extConfigApplied = false;+if (!AppConfig.isWeb && window.electronIO?.ipcRenderer?.getSync) {+ try {+ const profileText = window.electronIO.ipcRenderer.getSync(+ 'get-user-ext-config',+ ) as string;+ if (profileText) {+ extConfigApplied = applyExtConfigText(profileText, 'userData');+ }+ } catch (error) {+ console.warn('extconfig.json (userData) lookup failed:', error);+ }+}++if (!extConfigApplied) {+ const xhr = new XMLHttpRequest();+ xhr.open('GET', extConfigPath, false); // false = synchronous+ try {+ xhr.send(); // Will pause execution here until it finishes or fails+ // Status 200 is for HTTP servers, Status 0 is for local file:// protocol+ if ((xhr.status === 200 || xhr.status === 0) && xhr.responseText) {+ // Content-Type guard — only accept what the server claims is JSON.+ // file:// responses (status 0) typically don't set a Content-Type;+ // empty header is tolerated. We only reject when something explicit+ // and non-JSON came back (e.g. a dev-server fallback HTML page or a+ // CDN error page with a 200).+ const contentType = (+ xhr.getResponseHeader('Content-Type') || ''+ ).toLowerCase();+ if (contentType && !contentType.startsWith('application/json')) {+ console.warn(+ `extconfig.json had unexpected Content-Type ${contentType} — ignored.`,+ );} else {- console.warn('extconfig.json had unexpected shape — ignored.');+ applyExtConfigText(xhr.responseText, 'installDir');}+ } else {+ // Handled case: File exists but returned 404, 403, etc.+ console.warn(`extconfig.json returned status ${xhr.status}.`);}- } else {- // Handled case: File exists but returned 404, 403, etc.- console.warn(`extconfig.json returned status ${xhr.status}.`);+ } catch (error) {+ // Handled case: File completely missing, network offline, or CORS error+ console.warn('extconfig.json is not available or could not be loaded.');}-} catch (error) {- // Handled case: File completely missing, network offline, or CORS error- console.warn('extconfig.json is not available or could not be loaded.');}const blacklist = ['app'];