Menu

1 / 2

Self-defending JavaScript and checksums fail once the attacker can patch the checker

DEV Community: javascript·VEILVM·about 23 hours ago
#k8dhmvR4
#devtojavascript#devto#dev#function#tostring
Reading 0:00
15s threshold

A popular last line of defense in client JavaScript is an integrity check. The script hashes its own source, compares the result to a baked-in constant, and if the bytes have changed it refuses to run, wipes state, or phones home. Related tricks include "self-defending" wrappers that break when someone pretty-prints the file, and runtime checks that Function.prototype.toString() still matches a known snapshot.

These ideas sound like tamper detection. They are not a trust boundary. The checker is itself JavaScript in the same download as the program it is trying to protect. Anyone who can edit the bundle can edit the checker.

This article is about why checksums, self-hashing, and toString snapshots fail against a determined reader, and what is actually worth doing instead.

What a self-check usually looks like

A simplified checksum gate:

function assertIntact ( src ) { const expected = "a1b2c3d4..." ; // baked in at build time const actual = sha256 ( src ) ; if ( actual !== expected ) { location .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.