Closures, IIFEs, and private fields do not hide your JavaScript
A well-meaning review comment still shows up on pull requests: "wrap it in an IIFE so nobody can call it from the console." Sometimes the suggestion is a closure, a revealing module, or a #private field. The intent is re
A well-meaning review comment still shows up on pull requests: "wrap it in an IIFE so nobody can call it from the console." Sometimes the suggestion is a closure, a revealing module, or a #private field. The intent is reasonable. The protection is not real.
Anything that runs in the browser is reachable. Scope is a language rule for your program, not a boundary against a reader who already has the source. This article is about what closures and private fields actually do, how an attacker walks around them, and what to protect instead.
Scope is not a security boundary
JavaScript scope decides which names a function can see. It is invaluable for encapsulation. It does not stop someone from reading, rewriting, or hooking the same function.
const License = (() => {
const SECRET = "pro-ok";
function check(token) {
return token === SECRET || token.startsWith("pro-");
}
return { check };
})();
// later, in your app
if (!License.check(userToken)) showPaywall();
SECRET is not on window. License.check is. Pretty-print the bundle and the comparison is right there. A reader can:
- Copy
checkandSECRETinto a new file. - In DevTools, assign
License.check = () => true. - Set a breakpoint on the comparison and read
tokenandSECRETin the Scope pane. - Search the minified file for
pro-okorpro-.
An IIFE only hid a name from casual window inspection. It did not hide the algorithm.
#private fields have the same limit
Class private fields are a better encapsulation tool than a leading underscore. They prevent ordinary code in the same realm from reading obj.#key. They are not encryption.
class Entitlements {
#plan;
constructor(plan) { this.#plan = plan; }
canExport() { return this.#plan === "pro" || this.#plan === "team"; }
}
After a production build, the private name may be rewritten, but the comparison remains. DevTools will show the field on a breakpoint. A debugger, a hook on canExport, or a copy of the class body all bypass the "privacy."
The same is true of closures over const, WeakMap bags of "private" state, and TypeScript private modifiers — the last of which disappear at compile time.
How people actually bypass this
You do not need a decompiler. The usual path is:
- Beautify the chunk.
- Search for distinctive strings: plan names, error messages,
/api/paths, magic numbers. - Hook the exported function, or the method that calls it, from the console.
- If the value lives only in a closure, set a breakpoint where it is used and read the Scope pane, or replace the caller.
// conceptual: hook the public surface, ignore the closure
const original = License.check;
License.check = (token) => {
console.log("token", token);
return true;
};
If your paywall, export button, or scoring routine trusts that hookable function, the closure was decorative.
What closures are still good for
Keep using them. IIFEs, modules, and #private fields make a codebase easier to change and harder to call by accident. They are the right default for API design.
Do not describe them as obfuscation. Do not store an authoritative price, a secret API key, or a license bypass in a closed-over constant and call the review done.
When the function itself is the asset
Some logic has to run on the client and would hurt you if it were copied wholesale: an offline preview, a paid calculator, a document layout engine, a license mapper. For those:
- Keep the server as the authority for charges, entitlements, and anything that must not be spoofed.
- Minify the whole app as usual.
- Extract the sensitive function 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.
- Keep production source maps off the public CDN, or upload them only to a private crash tracker.
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 if / return; you get bytecode and a dispatcher. That is slower than a closure, which is why you do not wrap the entire application 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 closed-over string a secret, and it does not replace server-side checks.
Checklist
- [ ] You use closures, modules, and
#privatefields for encapsulation, not as a security control. - [ ] Distinctive constants and plan names in the client are ones you accept being public, or they live behind a stronger pass.
- [ ] Paywalls and entitlements are enforced again on the server. A hooked
check()cannot grant a charge or a download by itself. - [ ] Production source maps are not downloadable from the CDN.
- [ ] End-to-end tests run against the minified (and, if applicable, protected) production output.
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.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.