fix: harden extension security and plugin reliability

Address audit findings across background handlers, openers,
plugins, and UI: URL allowlists, XSS reductions, popup lifecycle
fixes, plugin dispose/cleanup, cloud sync hardening, global search
mathjs sandbox, and settings storage fixes.
This commit is contained in:
AdenMGB committed 2026-06-17 10:52:43 +09:30
1 parent 0e696e0175
commit 8a5424c5a4
70 files changed
+1229 -430

No files matched your search

+40 -10
View File
@@ -85,7 +85,10 @@ function createSEQTAAPI(): SEQTAAPI {
*/
function createSettingsAPI<T extends PluginSettings>(
plugin: Plugin<T>,
): SettingsAPI<T> & { loaded: Promise<void> } {
): {
settings: SettingsAPI<T> & { loaded: Promise<void> };
dispose: () => void;
} {
const storageKey = `plugin.${plugin.id}.settings`;
const listeners = new Map<keyof T, Set<(value: any) => void>>();
@@ -167,6 +170,10 @@ function createSettingsAPI<T extends PluginSettings>(
browser.storage.onChanged.addListener(handleStorageChange);
const dispose = () => {
browser.storage.onChanged.removeListener(handleStorageChange);
};
const proxy = new Proxy(settingsWithMeta, {
get(target, prop) {
return target[prop];
@@ -183,6 +190,17 @@ function createSettingsAPI<T extends PluginSettings>(
dataToStore[key] = target[key];
}
// Preserve enabled flag managed separately for disableToggle plugins
if (plugin.disableToggle) {
const allSettings = settingsState.getAll() as Record<string, unknown>;
const existing = allSettings[storageKey] as
| { enabled?: boolean }
| undefined;
if (existing?.enabled !== undefined) {
dataToStore.enabled = existing.enabled;
}
}
browser.storage.local.set({ [storageKey]: dataToStore });
listeners.get(prop as keyof T)?.forEach((cb) => cb(value));
@@ -190,18 +208,18 @@ function createSettingsAPI<T extends PluginSettings>(
},
}) as SettingsAPI<T> & { loaded: Promise<void> };
return proxy;
return { settings: proxy, dispose };
}
function createStorageAPI<T = any>(
pluginId: string,
): StorageAPI<T> & { [K in keyof T]: T[K] } {
): {
storage: StorageAPI<T> & { [K in keyof T]: T[K] };
dispose: () => void;
} {
const prefix = `plugin.${pluginId}.storage.`;
const cache: Record<string, any> = {};
const listeners = new Map<string, Set<(value: any) => void>>();
const storageListeners = new Set<
(changes: { [key: string]: any }, area: string) => void
>();
// Load all existing storage values for this plugin
const loadStoragePromise = (async () => {
@@ -243,10 +261,13 @@ function createStorageAPI<T = any>(
}
};
browser.storage.onChanged.addListener(handleStorageChange);
storageListeners.add(handleStorageChange);
const dispose = () => {
browser.storage.onChanged.removeListener(handleStorageChange);
};
// Create the proxy for direct property access
return new Proxy(cache, {
const storage = new Proxy(cache, {
get(target, prop: string) {
if (prop === "onChange") {
return (key: keyof T, callback: (value: T[keyof T]) => void) => {
@@ -288,6 +309,8 @@ function createStorageAPI<T = any>(
return true;
},
}) as StorageAPI<T> & { [K in keyof T]: T[K] };
return { storage, dispose };
}
function createEventsAPI(pluginId: string): EventsAPI {
@@ -357,10 +380,17 @@ function createEventsAPI(pluginId: string): EventsAPI {
export function createPluginAPI<T extends PluginSettings, S = any>(
plugin: Plugin<T, S>,
): PluginAPI<T, S> {
const { settings, dispose: disposeSettings } = createSettingsAPI(plugin);
const { storage, dispose: disposeStorage } = createStorageAPI<S>(plugin.id);
return {
seqta: createSEQTAAPI(),
settings: createSettingsAPI(plugin),
storage: createStorageAPI<S>(plugin.id),
settings,
storage,
events: createEventsAPI(plugin.id),
dispose: () => {
disposeSettings();
disposeStorage();
},
};
}