build(web,sign): drop vite-plugin-node-polyfills and its elliptic dependency (#70)

vite-plugin-node-polyfills depends on node-stdlib-browser, which always
installs crypto-browserify -> browserify-sign/create-ecdh -> elliptic
(GHSA-848j-6mx2-7j84, no patched release). No module in either bundle
ever imported the crypto polyfill; what the plugin actually provided was
three injected globals and the `events` alias.

sign: the only call sites were @pezkuwi/x-global (`global`, after a
globalThis check), shared/lib env helpers (`process`, typeof-guarded) and
bn.js's optional Buffer probe (try/catch; BN#toBuffer has no callers).
The plugin, the process shim alias and the `buffer` pre-bundle entry are
removed outright.

web: WalletConnect calls `Buffer.from` unguarded and imports `events`,
so the globals are still needed. src/lib/node-globals.ts now provides
Buffer/global/process, injected as scoped imports by @rollup/plugin-inject
in the build and by esbuild's inject in the dev pre-bundle - the same 18
modules the plugin injected into, and still nothing written onto
globalThis. `events` becomes a direct dependency. The unused shim files,
the vitest shim aliases and the `elliptic` override are removed.
This commit is contained in:
SatoshiQaziMuhammed authored and GitHub committed 2026-11-15 05:28:59 +04:00
1 parent d703331d85
commit 11fbe41a8a
16 files changed
+62 -3079

No files matched your search

+2 -2
View File
@@ -1012,8 +1012,8 @@ jobs:
run: | run: |
npm ci npm ci
# Audit only production dependencies. Build tooling (vite, esbuild, # Audit only production dependencies. Build tooling (vite, esbuild,
# vite-plugin-node-polyfills → elliptic, etc.) ships to no user, and # etc.) ships to no user, and advisories on those dev deps kept
# advisories on those dev deps kept blocking production deploys. # blocking production deploys.
npm audit --audit-level=high --omit=dev npm audit --audit-level=high --omit=dev
- name: TruffleHog — PR diff (verified secrets only) - name: TruffleHog — PR diff (verified secrets only)
+1 -1905
View File
File diff suppressed because it is too large. Load diff
+1 -2
View File
@@ -26,7 +26,6 @@
"@types/react-dom": "^18.3.1", "@types/react-dom": "^18.3.1",
"@vitejs/plugin-react-swc": "^3.5.0", "@vitejs/plugin-react-swc": "^3.5.0",
"typescript": "^5.6.3", "typescript": "^5.6.3",
"vite": "^6.4.5", "vite": "^6.4.5"
"vite-plugin-node-polyfills": "^0.25.0"
} }
} }
-12
View File
@@ -1,12 +0,0 @@
// Production Rollup alias for vite-plugin-node-polyfills/shims/process (mirrors web/'s copy -
// see its comment for why: Rollup can't resolve the plugin's virtual module during a real
// build, only in dev). IMPORTANT: must not reference the `process` identifier at runtime -
// vite-plugin-node-polyfills rewrites it to `__process_polyfill`, creating a circular TDZ. Use
// bracket notation so the plugin leaves this file alone.
const g: Record<string, unknown> =
typeof globalThis !== 'undefined' ? (globalThis as Record<string, unknown>)
: typeof window !== 'undefined' ? (window as unknown as Record<string, unknown>)
: {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default (g['process'] ?? { env: {} }) as any;
+6 -17
View File
@@ -1,41 +1,30 @@
import { defineConfig } from 'vite'; import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react-swc'; import react from '@vitejs/plugin-react-swc';
import path from 'path'; import path from 'path';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
// Deliberately minimal compared to web/vite.config.ts: this app has exactly one job (gate + // Deliberately minimal compared to web/vite.config.ts: this app has exactly one job (gate +
// sign multisig operations), so it carries none of web/'s SPA routing, i18n, or UI-kit // sign multisig operations), so it carries none of web/'s SPA routing, i18n, or UI-kit
// machinery - fewer dependencies is itself a security property for a signing-critical site. // machinery - fewer dependencies is itself a security property for a signing-critical site.
export default defineConfig(({ command }) => ({ export default defineConfig({
server: { server: {
host: '::', host: '::',
port: 8090, port: 8090,
}, },
plugins: [ // No Node polyfills: nothing here imports a Node builtin, and the few dependency call sites
react(), // that touch `global`/`process`/`Buffer` all guard with a typeof check first.
nodePolyfills({ plugins: [react()],
globals: { Buffer: true, global: true, process: true },
protocolImports: true,
}),
],
resolve: { resolve: {
mainFields: ['browser', 'module', 'main', 'exports'], mainFields: ['browser', 'module', 'main', 'exports'],
alias: { alias: {
// Rollup cannot resolve the plugin's virtual shim module in production - alias to a real
// file (mirrors web/vite.config.ts's identical workaround). Dev mode leaves the plugin's
// own virtual module handling it.
...(command === 'build'
? { 'vite-plugin-node-polyfills/shims/process': path.resolve(__dirname, './src/lib/process-shim.ts') }
: {}),
'@': path.resolve(__dirname, './src'), '@': path.resolve(__dirname, './src'),
'@pezkuwi/lib': path.resolve(__dirname, '../shared/lib'), '@pezkuwi/lib': path.resolve(__dirname, '../shared/lib'),
}, },
dedupe: ['react', '@pezkuwi/util-crypto', '@pezkuwi/util', '@pezkuwi/api', '@pezkuwi/extension-dapp', '@pezkuwi/keyring'], dedupe: ['react', '@pezkuwi/util-crypto', '@pezkuwi/util', '@pezkuwi/api', '@pezkuwi/extension-dapp', '@pezkuwi/keyring'],
}, },
optimizeDeps: { optimizeDeps: {
include: ['@pezkuwi/util-crypto', '@pezkuwi/util', '@pezkuwi/api', '@pezkuwi/extension-dapp', '@pezkuwi/keyring', 'buffer'], include: ['@pezkuwi/util-crypto', '@pezkuwi/util', '@pezkuwi/api', '@pezkuwi/extension-dapp', '@pezkuwi/keyring'],
}, },
build: { build: {
chunkSizeWarningLimit: 600, chunkSizeWarningLimit: 600,
}, },
})); });
+8 -1082
View File
File diff suppressed because it is too large. Load diff
+2 -2
View File
@@ -69,6 +69,7 @@
"date-fns": "^3.6.0", "date-fns": "^3.6.0",
"dompurify": "^3.4.17", "dompurify": "^3.4.17",
"embla-carousel-react": "^8.3.0", "embla-carousel-react": "^8.3.0",
"events": "^3.3.0",
"highlight.js": "^11.9.0", "highlight.js": "^11.9.0",
"i18next": "^23.7.6", "i18next": "^23.7.6",
"i18next-browser-languagedetector": "^7.2.0", "i18next-browser-languagedetector": "^7.2.0",
@@ -116,7 +117,6 @@
"@pezkuwi/x-textencoder": "^14.0.26", "@pezkuwi/x-textencoder": "^14.0.26",
"@pezkuwi/x-ws": "^14.0.26", "@pezkuwi/x-ws": "^14.0.26",
"@pezkuwi/networks": "^14.0.26", "@pezkuwi/networks": "^14.0.26",
"elliptic": "^6.6.1",
"@tailwindcss/typography": { "@tailwindcss/typography": {
"postcss-selector-parser": "6.1.5" "postcss-selector-parser": "6.1.5"
}, },
@@ -126,6 +126,7 @@
}, },
"devDependencies": { "devDependencies": {
"@eslint/js": "^9.9.0", "@eslint/js": "^9.9.0",
"@rollup/plugin-inject": "^5.0.5",
"@tailwindcss/typography": "^0.5.16", "@tailwindcss/typography": "^0.5.16",
"@testing-library/jest-dom": "^6.9.1", "@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.0", "@testing-library/react": "^16.3.0",
@@ -152,7 +153,6 @@
"typescript": "^5.5.3", "typescript": "^5.5.3",
"typescript-eslint": "^8.0.1", "typescript-eslint": "^8.0.1",
"vite": "^7.3.7", "vite": "^7.3.7",
"vite-plugin-node-polyfills": "^0.25.0",
"vite-plugin-subresource-integrity": "^0.0.12", "vite-plugin-subresource-integrity": "^0.0.12",
"vitest": "^4.1.12" "vitest": "^4.1.12"
} }
+17
View File
@@ -0,0 +1,17 @@
// Values for the three Node globals that some browser dependencies reference as free
// identifiers (WalletConnect calls `Buffer.from(...)` unguarded; a few libraries probe
// `global` / `process`). vite.config.ts injects these as scoped imports into exactly the
// modules that reference them - the build through @rollup/plugin-inject, the dev pre-bundle
// through esbuild's `inject` - so nothing is assigned onto `globalThis`.
//
// Nothing here may read the bare `process` / `global` / `Buffer` identifiers at runtime:
// the injector would rewrite them into a self-import. Use bracket access on globalThis.
import { Buffer } from 'buffer';
const g = globalThis as unknown as Record<string, unknown>;
const nodeGlobal = globalThis;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const nodeProcess = (g['process'] ?? { env: {} }) as any;
export { Buffer, nodeGlobal as global, nodeProcess as process };
-11
View File
@@ -1,11 +0,0 @@
// Production Rollup alias for vite-plugin-node-polyfills/shims/process.
// IMPORTANT: must not reference the `process` identifier at runtime —
// vite-plugin-node-polyfills rewrites it to `__process_polyfill`, creating
// a circular TDZ. Use bracket notation so the plugin leaves this file alone.
const g: Record<string, unknown> =
typeof globalThis !== 'undefined' ? (globalThis as Record<string, unknown>)
: typeof window !== 'undefined' ? (window as unknown as Record<string, unknown>)
: {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default (g['process'] ?? { env: {} }) as any;
-4
View File
@@ -1,4 +0,0 @@
// Buffer polyfill shim for vite-plugin-node-polyfills
import { Buffer } from 'buffer';
export { Buffer };
export default Buffer;
-2
View File
@@ -1,2 +0,0 @@
// Global polyfill shim for vite-plugin-node-polyfills
export default typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : {};
-9
View File
@@ -1,9 +0,0 @@
// Process polyfill shim for vite-plugin-node-polyfills
const processShim = {
env: {},
browser: true,
version: '',
versions: {},
};
export default processShim;
-2
View File
@@ -1,2 +0,0 @@
import { Buffer } from 'buffer';
export default Buffer;
-1
View File
@@ -1 +0,0 @@
export default globalThis;
-1
View File
@@ -1 +0,0 @@
export default { env: {} };
+25 -27
View File
@@ -2,9 +2,12 @@
import { defineConfig, configDefaults } from "vitest/config"; import { defineConfig, configDefaults } from "vitest/config";
import react from "@vitejs/plugin-react-swc"; import react from "@vitejs/plugin-react-swc";
import path from "path"; import path from "path";
import { nodePolyfills } from 'vite-plugin-node-polyfills'; import inject from '@rollup/plugin-inject';
import subresourceIntegrity from 'vite-plugin-subresource-integrity'; import subresourceIntegrity from 'vite-plugin-subresource-integrity';
// Node globals some browser dependencies reference as free identifiers; see the file header.
const nodeGlobals = path.resolve(__dirname, './src/lib/node-globals.ts');
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig(({ command }) => ({ export default defineConfig(({ command }) => ({
test: { test: {
@@ -14,11 +17,6 @@ export default defineConfig(({ command }) => ({
// supabase/** holds Deno tests (Deno.test + pglite) run via `deno test`, // supabase/** holds Deno tests (Deno.test + pglite) run via `deno test`,
// not vitest — exclude them from the web (node/jsdom) test sweep. // not vitest — exclude them from the web (node/jsdom) test sweep.
exclude: [...configDefaults.exclude, 'supabase/**'], exclude: [...configDefaults.exclude, 'supabase/**'],
alias: {
'vite-plugin-node-polyfills/shims/buffer': path.resolve(__dirname, './src/tests/mocks/buffer-shim.ts'),
'vite-plugin-node-polyfills/shims/global': path.resolve(__dirname, './src/tests/mocks/global-shim.ts'),
'vite-plugin-node-polyfills/shims/process': path.resolve(__dirname, './src/tests/mocks/process-shim.ts'),
},
}, },
server: { server: {
host: "::", host: "::",
@@ -34,14 +32,6 @@ export default defineConfig(({ command }) => ({
}, },
plugins: [ plugins: [
react(), react(),
nodePolyfills({
globals: {
Buffer: true,
global: true,
process: true,
},
protocolImports: true,
}),
// SRI: production build sırasında <script>/<link> tag'lerine // SRI: production build sırasında <script>/<link> tag'lerine
// sha384 integrity hash ekle. CDN/proxy compromise olsa bile // sha384 integrity hash ekle. CDN/proxy compromise olsa bile
// tampered asset browser tarafından load edilmez. // tampered asset browser tarafından load edilmez.
@@ -50,11 +40,6 @@ export default defineConfig(({ command }) => ({
resolve: { resolve: {
mainFields: ['browser', 'module', 'main', 'exports'], mainFields: ['browser', 'module', 'main', 'exports'],
alias: { alias: {
// Rollup cannot resolve virtual shim modules in production — alias to real file.
// Dev mode: the plugin's own virtual module handles it; do NOT override it here.
...(command === 'build' ? {
'vite-plugin-node-polyfills/shims/process': path.resolve(__dirname, './src/lib/process-shim.ts'),
} : {}),
"@": path.resolve(__dirname, "./src"), "@": path.resolve(__dirname, "./src"),
"@pezkuwi/i18n": path.resolve(__dirname, "../shared/i18n"), "@pezkuwi/i18n": path.resolve(__dirname, "../shared/i18n"),
"@pezkuwi/lib": path.resolve(__dirname, "../shared/lib"), "@pezkuwi/lib": path.resolve(__dirname, "../shared/lib"),
@@ -68,6 +53,19 @@ export default defineConfig(({ command }) => ({
}, },
optimizeDeps: { optimizeDeps: {
include: ['@pezkuwi/util-crypto', '@pezkuwi/util', '@pezkuwi/api', '@pezkuwi/extension-dapp', '@pezkuwi/keyring', 'buffer'], include: ['@pezkuwi/util-crypto', '@pezkuwi/util', '@pezkuwi/api', '@pezkuwi/extension-dapp', '@pezkuwi/keyring', 'buffer'],
esbuildOptions: {
inject: [nodeGlobals],
plugins: [
{
// Vite's dependency scanner marks every path outside node_modules as external, and
// esbuild refuses an external inject target; resolve it as a real file instead.
name: 'node-globals-resolver',
setup(build) {
build.onResolve({ filter: /[\\/]src[\\/]lib[\\/]node-globals\.ts$/ }, () => ({ path: nodeGlobals }));
},
},
],
},
}, },
// Strip all console.* and debugger statements from production bundles so the // Strip all console.* and debugger statements from production bundles so the
// 600+ dev-time console calls never ship. Dev keeps full logging. // 600+ dev-time console calls never ship. Dev keeps full logging.
@@ -75,13 +73,13 @@ export default defineConfig(({ command }) => ({
build: { build: {
rollupOptions: { rollupOptions: {
external: [], external: [],
onwarn(warning, warn) { plugins: [
// Suppress the buffer shim warning - it's handled by vite-plugin-node-polyfills inject({
if (warning.message?.includes('vite-plugin-node-polyfills/shims/buffer')) { Buffer: [nodeGlobals, 'Buffer'],
return; global: [nodeGlobals, 'global'],
} process: [nodeGlobals, 'process'],
warn(warning); }),
}, ],
output: { output: {
manualChunks: { manualChunks: {
'pezkuwi': ['@pezkuwi/api', '@pezkuwi/extension-dapp', '@pezkuwi/keyring', '@pezkuwi/util', '@pezkuwi/util-crypto'], 'pezkuwi': ['@pezkuwi/api', '@pezkuwi/extension-dapp', '@pezkuwi/keyring', '@pezkuwi/util', '@pezkuwi/util-crypto'],
@@ -94,5 +92,5 @@ export default defineConfig(({ command }) => ({
}, },
chunkSizeWarningLimit: 600 chunkSizeWarningLimit: 600
}, },
// assetsInclude: ['**/*.json'], // Disabled: interferes with node_modules JSON imports (crypto-browserify) // assetsInclude: ['**/*.json'], // Disabled: interferes with node_modules JSON imports
})); }));