Zero-dependency protection & obfuscation for HTML, JavaScript and React sites
You ship a website — the browser necessarily receives all of your HTML, JS and bundles. Anyone can open DevTools, read your API endpoints, pricing logic, feature flags, licensing keys and hidden admin routes, and lift your code into their own product.
HTML-Guard is a post-build step that makes that copy-paste expensive:
- every meaningful string literal is moved into an encrypted pool (XOR + base64) and decoded only at runtime
- comments, indentation and sourcemap clues are stripped
- local
<script>/<link>tags get SRI integrity hashes — a modified file stops loading - optional heavy armor: domain lock, self-defending output, anti-devtools, anti-copy, console silencing
It runs on plain HTML sites, on React/Vite/CRA builds and as a Vite plugin — with zero npm dependencies (no supply chain, nothing to audit).
Obfuscation is not encryption: a determined attacker can still reverse the runtime. It raises the cost of casual theft from two clicks to hours of work. Use it to protect business logic, not to hide malicious behavior.
| Layer | Default | Effect |
|---|---|---|
| String encryption | on | 'sk-live-9f2b77' → _0x3fa2(17) from an XOR+base64 pool |
| Safe minification | on | comments & indentation removed, ASI semantics preserved |
| IIFE wrap | auto | classic scripts hide their top-level scope |
| Sourcemap removal | on | sourceMappingURL refs stripped, *.map deleted |
| SRI integrity | on | integrity="sha384-…" on local scripts & styles |
| HTML comments | on | stripped (conditional comments preserved) |
| Watermark | on | <meta name="generator" content="HTML-Guard 1.0.0"> |
| Identifier mangling | off | renames top-level var/function (conservative) |
| Self-defending | off | output breaks if someone beautifies it |
| Domain lock | off | code only runs on your hostnames |
| Anti-devtools | off | off / warn / redirect |
| Anti-copy | off | right-click, selection, drag & copy hotkeys |
| Kill console | off | replaces console methods with no-ops |
npm install --save-dev html-guardor use it straight from this repository:
git clone https://github.com/R3lexa/HTML-Guard.git
node HTML-Guard/bin/html-guard.js --help# protect a build output in place (ready to deploy)
npx html-guard protect dist --react
# or protect any site / folder into <name>-protected/
npx html-guard protect public
# a single file
npx html-guard protect app.jsOutput:
┌──────────────────────────────────────────────────────────────┐
│ Protection report │
├──────────────────────────────────────────────────────────────┤
│ app.js 1.3 KB → 1.9 KB string-encryption, minify, iife-wr… │
│ index.html 1.5 KB → 2.2 KB comment-stripping, sri-integr… │
├──────────────────────────────────────────────────────────────┤
│ 3 file(s) protected · 3.9 KB → 5.2 KB │
└──────────────────────────────────────────────────────────────┘
html-guard protect <file-or-directory> [options]
html-guard init # create htmlguard.config.js
html-guard help | version
| Option | Values | Description |
|---|---|---|
--out <path> |
path | output file/directory (default <name>-protected) |
--in-place |
overwrite the source | |
--react |
post-build mode; default output becomes in-place | |
--config <file> |
path | explicit config (js/json) |
--dry-run |
process without writing | |
--json |
machine-readable report on stdout | |
--silent |
no console output | |
--no-strings |
disable string encryption | |
--string-min-length <n> |
int | minimum string length to encode (default 3) |
--no-minify |
keep formatting | |
--mangle |
rename top-level identifiers (experimental) | |
--wrap / --no-wrap |
force/skip IIFE wrapping | |
--self-defending |
break on beautified output | |
--domain-lock <list> |
a.com,b.com |
hostname allowlist |
--devtools <mode> |
off/warn/redirect |
DevTools detection |
--kill-console |
silence console methods | |
--anti-debug |
periodic debugger statement (aggressive) |
|
--banner <text> |
text | visible banner comment |
--anti-copy |
page copy protection | |
--keep-comments / --keep-whitespace |
keep HTML comments / whitespace | |
--no-inline |
skip inline <script> bodies |
|
--no-watermark / --no-integrity |
disable watermark / SRI | |
--keep-maps |
copy *.map instead of deleting |
|
--ext <list> |
js,mjs,html |
extensions to process |
htmlguard.config.js (or .json, or package.json#htmlguard) — CLI flags
always win:
module.exports = {
js: {
strings: true,
stringMinLength: 3,
minify: true,
wrap: 'auto',
mangle: false,
selfDefending: false,
domainLock: ['example.com', 'www.example.com'],
devtools: 'off', // 'off' | 'warn' | 'redirect'
killConsole: false,
antiDebug: false,
banner: null
},
html: {
comments: true,
whitespace: true,
inline: true,
watermark: true,
integrity: true,
antiCopy: false,
strictAntiCopy: false
},
files: {
extensions: ['.js', '.mjs', '.html', '.htm', '.css'],
deleteMaps: true,
copyOthers: true
}
};See the fully annotated htmlguard.config.example.js.
// vite.config.js
import react from '@vitejs/plugin-react';
import htmlGuard from 'html-guard/plugins/vite';
export default {
plugins: [
react(),
htmlGuard({ js: { strings: true, minify: true }, html: { integrity: true } })
]
};Every vite build now runs protection on dist/ automatically.
# Vite
vite build && npx html-guard protect dist --react
# Create React App
react-scripts build && npx html-guard protect build --react
# Next.js static export
next build && npx html-guard protect out --react
# plain static site
npx html-guard protect publicA runnable demo lives in examples/react-vite/
(the package is linked via file:../.. — no publishing needed).
const { protectJs, protectHtml, protectDirectory } = require('html-guard');
const { code, stats } = protectJs(source, { mangle: true });
const page = protectHtml(html, { antiCopy: true }, { inlineJsOpts: {} });
protectDirectory('dist', 'dist', { js: {}, html: {}, files: {} }); // in place- Lex — a hand-written, dependency-free JavaScript tokenizer understands strings, template literals with nested interpolations, regex-vs-division disambiguation, comments, all number forms and every modern punctuator.
- Encrypt — eligible string literals are replaced with
_0x…(index)calls; values are UTF-8 → XOR with a rotating key → base64, decoded by a small ES5 shim injected into the file. - Wrap — classic scripts are wrapped into an IIFE (skipped for ES
modules, inline scripts,
evalusers and top-levelthis). - Render — an ASI-safe renderer re-emits the token stream: comments and
indentation gone, but every newline that carries semantics is kept, so
restricted productions (
return\n,throw\n,a++\n) can never break. - Harden — SRI hashes are computed on the final output and written into the HTML; optional runtime guards are injected.
If a file cannot be tokenized safely it is passed through untouched with a warning — HTML-Guard never ships broken output.
- Obfuscation ≠ encryption; runtime strings can still be dumped from a modified environment. Expect deterrence, not DRM.
- Property names (
obj.apiKey) stay as written — only identifiers can be mangled, and only conservatively (--mangleis off by default). - Files using
evalskip wrapping/mangling automatically; exotic indirectevalvia concatenated strings can defeat detection — test your build. - SRI protects entry points and preloaded assets; browser support for
dynamic
import()integrity is incomplete. - Anti-devtools / anti-debug measures inconvenience legitimate users (including your own developers) — keep them off unless you accept that.
- Use responsibly. Do not use HTML-Guard to conceal malicious code, trackers or scams. It is a tool for protecting honest work.
Will it break my site?
The engine is intentionally conservative: ESM never gets IIFE-wrapped, module
specifiers and the "use strict" directive are never encrypted, unparseable
files pass through unchanged, and the minifier preserves ASI semantics. 57
roundtrip tests (including self-hosting — the engine protecting its own
source) guard every release.
Does it work with already-minified bundles? Yes — that is the main React use case. Terser output is valid input; string encryption is what removes the remaining readable hints.
Why does my file grow?
The decoder shim and pool add ~1 KB + overhead per encoded string. For large
bundles this is a few percent; tune with stringMinLength.
Can I keep debugging?
Run --no-strings --no-minify in a staging profile, or keep sourcemaps with
--keep-maps — but never ship maps in production.
Node support?
Node 18+ (uses node: built-ins and node:test).
bin/html-guard.js CLI entry
src/lexer.js dependency-free JS tokenizer
src/minify.js ASI-safe renderer
src/string-array.js XOR+base64 string pool
src/mangle.js conservative identifier renaming
src/snippets.js runtime guards (domain lock, self-defending, …)
src/protect-js.js JS pipeline
src/protect-html.js HTML pipeline
src/protect-dir.js directory / build runner
src/cli.js command line interface
plugins/vite.js Vite plugin
test/ 57 tests, incl. self-hosting
examples/plain-html/ static site demo
examples/react-vite/ React + Vite demo
npm test # node --test (57 tests)
node scripts/smoke.js # quick roundtrip demoContributions are welcome — especially lexer edge cases and new test corpora.
MIT © 2026 R3lexa