Repository navigation
4.2 Security Allowlist CSP and Preload Bridge
Relevant source files
The following files were used as context for generating this wiki page:
This page documents the security architecture of the VariantScope desktop application, focusing on host enforcement via allowlists, Content Security Policy (CSP) generation, and the sandboxed preload bridge that safely exposes Main Process capabilities to the Renderer Process via context isolation.
Sources: electron/allowlist.js:1-49, electron/csp.js:1-25, electron/preload.cjs:1-70
1. URL Allowlist and Validation (electron/allowlist.js)
VariantScope enforces strict boundary controls on all outbound network connections and system-level hyperlink openings. The allowlist module (electron/allowlist.js) defines permissible target domains and implements validation helpers electron/allowlist.js:1-48.
The module maintains two distinct sets of frozen host arrays:
-
API_HOSTS: Restricts backend queries initiated via fetch abstractions to verified bioinformatics APIs over HTTPS (rest.uniprot.org,alphafold.ebi.ac.uk,eutils.ncbi.nlm.nih.gov,gnomad.broadinstitute.org) electron/allowlist.js:4-9. -
LINK_HOSTS: ExtendsAPI_HOSTSto include documentation and portal destinations (www.uniprot.org,www.ncbi.nlm.nih.gov,github.com) permitted for system browser navigation electron/allowlist.js:12-17.
-
parseHttps(raw): Parses string input into aURLinstance, enforcing that the protocol is strictlyhttps:, rejects credentials (usernameorpassword), and disallows non-standard ports electron/allowlist.js:19-32. -
checkApiUrl(raw): Evaluates a target API request URL, returning{ ok: true, url }upon success or an error object if the host or protocol fails validation electron/allowlist.js:35-42. -
isAllowedExternalLink(raw): Validates links targeted for external system browser execution electron/allowlist.js:45-48.
graph TD
A["Raw Input String"] --> B["parseHttps(raw)"]
B --> C{"Valid HTTPS URL?"}
C -->|No| D["Return {ok: false, error}"]
C -->|Yes| E{"API_HOSTS Match?"}
E -->|No| F["Return Blocked Error"]
E -->|Yes| G["Return {ok: true, url}"]
sub_CodeEntities["Code Entities"]
sub_CodeEntities --- H["electron/allowlist.js: checkApiUrl()"]
sub_CodeEntities --- I["electron/allowlist.js: API_HOSTS"]
sub_CodeEntities --- J["electron/allowlist.js: parseHttps()"]
Sources: electron/allowlist.js:1-49, tests/unit/allowlist.test.js:1-43
2. Content Security Policy (electron/csp.js)
Content Security Policies (CSPs) mitigate cross-site scripting (XSS) and injection vectors by restricting resource loading and network execution contexts electron/csp.js:1-25.
The buildCsp(target) function constructs policy strings tailored to three distinct execution targets electron/csp.js:5-25:
-
'electron': Setsconnect-src 'none', completely blocking direct renderer-initiated network calls and forcing all data fetching through the secure IPC preload bridge electron/csp.js:6-7. -
'web': Allows connections toAPI_HOSTSfor browser/web deployment environments electron/csp.js:8. -
'dev': Permits local websocket connections (ws://localhost:5173) and inline styling ('unsafe-inline') to support Vite hot module reloading during local development electron/csp.js:9-16.
Sources: electron/csp.js:1-25
3. The Preload Bridge and Context Isolation (electron/preload.cjs)
Electron's security model runs the renderer in a sandboxed, context-isolated environment. electron/preload.cjs acts as the exclusive gatekeeper between the renderer and Node.js/Electron main process APIs electron/preload.cjs:1-69.
Using contextBridge.exposeInMainWorld(), the preload script exposes exactly six validated functions under the window.api namespace electron/preload.cjs:1-12.
-
fetchJson(url, options): Proxies API requests through the main process cache and allowlist gatekeeper, bypassing CORS restrictions electron/preload.cjs:14-32. -
saveFile(kind, suggestedName, data): Triggers native save dialogs for CSV exports or PNG visualizations electron/preload.cjs:35-42. -
openExternal(url): Requests the main process to open an allowlisted URL in the external system browser electron/preload.cjs:44-48. -
listCache(): Queries stored gene payloads and sizes from the disk cache electron/preload.cjs:50-53. -
deleteCache(target): Purges specified gene cache entries or clears all stored assets electron/preload.cjs:55-63. -
getAppInfo(): Retrieves application metadata (version, platform, architecture) for the About modal electron/preload.cjs:65-68.
All parameters entering the bridge undergo rigorous type checking via requireString() to ensure strings are non-empty and bounded within safety limits (e.g., maximum URL length of 4096 characters, bodies capped at 1,000,000 bytes) electron/preload.cjs:5-10.
graph TD
A["Renderer Process (src/core/http.js)"] -->|window.api.fetchJson| B["Preload Bridge (electron/preload.cjs)"]
B -->|requireString validation| C["ipcRenderer.invoke('fetch-json')"]
C -->|IPC Message| D["Main Process (electron/fetcher.js)"]
D -->|checkApiUrl| E["Disk Cache / HTTPS Fetch"]
E -->|Result Payload| C
C -->|Resolved Promise| A
sub_BridgeFunctions["Exposed Methods on window.api"]
sub_BridgeFunctions --- F["fetchJson()"]
sub_BridgeFunctions --- G["saveFile()"]
sub_BridgeFunctions --- H["openExternal()"]
sub_BridgeFunctions --- I["listCache()"]
sub_BridgeFunctions --- J["deleteCache()"]
sub_BridgeFunctions --- K["getAppInfo()"]
Sources: electron/preload.cjs:1-70, src/core/http.js:35-51
Content imported from DeepWiki.