DEV Community

VEILVM
VEILVM

Posted on

Code splitting and dynamic import() do not hide your JavaScript

A common piece of advice in code reviews is: "put the paid logic in a dynamic import() so it is not in the main bundle." Route-based splitting, React.lazy, and "load this only after checkout" feel like they move the sensitive code out of reach.

They do not. Code splitting is a loading and caching strategy. It decides when a file is fetched, not whether a determined reader can get it. This article is about what a split chunk actually hides, how people pull those chunks anyway, and what to protect instead.

A split chunk is still a public file

A typical paywall-shaped split looks like this:

async function openExporter() {
  if (!user.plan.includes("pro")) {
    showUpgrade();
    return;
  }
  const { exportReport } = await import("./export-report.js");
  return exportReport(currentDoc);
}
Enter fullscreen mode Exit fullscreen mode

After a Vite or webpack build, export-report.js becomes something like assets/export-report-Dg8k2a.js on the same origin (or CDN) as the rest of the app. The browser has to be able to download it, so anyone else can too.

A reader can:

  • Watch the Network panel, click Export once, and save the chunk.
  • Search the main bundle for the chunk name or a distinctive string from the exporter, then request that URL directly.
  • Open assets/ on the CDN if directory listing or a predictable hashed name is enough, or harvest names from the module preload graph.
  • Trigger the import() from the console after patching the plan check.

The hash in the filename is a cache-busting token, not an access token. If the HTML or the parent chunk mentions it, the file is public.

"Load it only after payment" is still a client check

The user.plan guard runs in the same tab as the importer. It is a UI branch. If export-report.js is already on the CDN, a visitor does not need to pass your if.

// conceptual: skip the guard, load the chunk yourself
const mod = await import("/assets/export-report-Dg8k2a.js");
mod.exportReport(stolenDoc);
Enter fullscreen mode Exit fullscreen mode

Even if you mint a short-lived URL for the chunk after payment, the bytes still arrive in the browser. From that point the file can be saved, pretty-printed, and reused. A signed URL can stop a stranger who never paid from fetching today's build. It cannot stop a paying customer, or anyone who obtained the file, from reading the exporter.

If the exporter is the product — a report layout, a file converter, a proprietary formatter — hiding the <script> tag was never the control you needed.

What code splitting is still good for

Keep using it. Split large editors, charting libraries, and locale packs so the first paint stays fast. Prefetch chunks you know a given route will need. That is what the feature is for.

Do not describe a dynamic import() as obfuscation. Do not leave an unprotected algorithm in a lazily loaded file and call the security review done. And do not put unpublished or staff-only tools in a public chunk that happens not to be linked from the marketing nav; if it is on the production CDN, it will be found.

When the chunk itself is the asset

Some lazily loaded modules have to run on the client and would hurt you if they were copied wholesale: an offline exporter, a paid formatter, a document engine. For those:

  1. Keep the server as the authority for charges, entitlements, and anything that must not be spoofed.
  2. Minify the whole app as usual, including split chunks.
  3. Extract the sensitive function from the chunk and apply a real protection pass — identifier renaming and string tables as a floor, VM-style protection if wholesale reuse is the risk you care about.
  4. Keep production source maps off the public CDN. Do not leave staff-only or unreleased features in a public asset directory.

A JS VM obfuscator compiles that function to a custom bytecode and ships a small interpreter. Pretty-printing the chunk no longer reconstructs the original exportReport; you get bytecode and a dispatcher. That is slower than a bare module, which is why you do not wrap every lazy route in it.

VEILVM is a browser-based JSVMP workspace you can point at one extracted function before you wire protection into CI. Protection raises the cost of casual inspection and wholesale reuse. It does not make a public CDN path a secret, and it does not replace server-side checks.

Checklist

  • [ ] Dynamic import() and route splitting are used for performance, not as a security control.
  • [ ] A chunk that contains a paid or proprietary engine is protected, or that engine runs on the server after an entitlement check.
  • [ ] Staff-only or unreleased tools are not sitting in the public /assets directory.
  • [ ] Production source maps are not downloadable from the CDN.
  • [ ] End-to-end tests run against the minified (and, if applicable, protected) production chunks, including lazily loaded ones.

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)