DEV Community

VEILVM
VEILVM

Posted on

Userscripts can hook any function your page leaves on window

Tampermonkey, Violentmonkey, and similar userscript managers have millions of users. A userscript is just JavaScript that runs on your page, often before your own code with @run-at document-start. Whatever your app exposes globally, a userscript can read, wrap, or replace.

This is not an attack in the usual sense. It is the user customizing their own browser, and that is their right. But if your product makes decisions in client code, it helps to know exactly how easy it is to change them, and which parts you can reasonably harden.

What "exposed" means

Some of this is obvious, some is not:

  • Globals you set on purpose: window.APP_CONFIG, window.__INITIAL_STATE__, a debug helper you forgot to strip.
  • Framework internals: React DevTools hooks, Vue's __vue_app__ on the root element, Redux or Zustand stores reachable from the DOM.
  • Built-ins your code calls: fetch, XMLHttpRequest, WebSocket, Date.now, Math.random, setTimeout.

That last group matters most. A userscript running at document start can wrap them before your bundle loads:

const realFetch = window.fetch;
window.fetch = async (input, init) => {
  const res = await realFetch(input, init);
  if (String(input).includes("/api/limits")) {
    return new Response(JSON.stringify({ remaining: 999 }), res);
  }
  return res;
};
Enter fullscreen mode Exit fullscreen mode

Your code never changed, yet it now believes the server said 999. The same pattern can freeze Date.now to defeat a trial timer, make Math.random predictable for a client-side game, or log every WebSocket frame.

Capturing built-ins early helps less than you think

A common defense is to save references at startup: const _fetch = window.fetch. That only works if your code runs first, and document-start scripts usually win that race. Checking fetch.toString() for [native code] is easy to spoof by overriding toString too. These tricks raise the bar slightly but should be treated as hints, not guarantees.

Put the decision where the user cannot hook it

  • Limits, credits, and entitlements are enforced by the API, not by what the client thinks the API returned.
  • Time-based trials are measured with server timestamps.
  • Game results and scores are validated or recomputed server-side.
  • Avoid leaving stores, config, or debug helpers on window in production builds.

Once that is true, a userscript that fakes /api/limits only fools the UI. The server still refuses the 4th export.

Where code protection fits

Hooking is easiest when the code is readable: the script author searches your bundle for /api/limits or trialEndsAt, sees exactly which function consumes it, and writes a ten-line patch. If the client logic you care about is protected, that discovery step gets much harder.

Practical steps:

  1. Keep sensitive logic out of global scope; do not attach it to window or expose it through framework devtools in production.
  2. Encrypt strings like endpoint paths and storage keys so they are not trivially searchable.
  3. Put proprietary functions (a scoring formula, a rendering trick, an offline license check) through VM-based protection so their control flow is not readable.
  4. Remove public source maps.

None of this stops a user from wrapping fetch. It makes it harder to know what to fake and where your logic reads the result.

VEILVM is a browser-based JS VM obfuscator (disclosure: I work on it). It is meant for one extracted function at a time, not for wrapping an entire app.

Checklist

  • [ ] Search your production bundle and window for config, stores, and debug helpers you did not mean to expose.
  • [ ] Every limit and entitlement is enforced by the API.
  • [ ] Trials and cooldowns use server time.
  • [ ] Integrity checks on built-ins are logged as signals only.
  • [ ] Proprietary client logic is extracted and protected; source maps are off.
  • [ ] You tested the protected build with a simple userscript that wraps fetch.

If you want to try VM protection on a single extracted function, VEILVM runs in the browser with no sign-up: 3 free runs a day, up to 5 KB per file (resets at 00:00 UTC). A free account raises the input limit to 30 KB. Pro is $9/month or $79/year and adds an API so the protection step can run in CI. Standard requests are processed in memory and are not written to logs. Start at https://vm.ldvmp.com.

Top comments (0)