[Javascript] Prevent JavaScript Object Tampering with the SES Library harden Function
https://www.npmjs.com/package/ses
Lockdown
The lockdown()
function also tames some objects including regular expressions, locale methods, and errors. A tamed RegExp
does not have the deprecated compile
method. A tamed error does not have a V8 stack
, but the console
can still see the stack. Lockdown replaces locale methods like String.prototype.localeCompare
with generic versions that do not reveal the host locale.
import 'ses';
lockdown();
console.log(Object.isFrozen([].__proto__));
// true
Harden
SES introduces the harden
function. After calling lockdown
, the harden
function ensures that every object in the transitive closure over property and prototype access starting with that object has been frozen by Object.freeze
. This means that the object can be passed among programs and none of those programs will be able to tamper with the surface of that object graph. They can only read the surface data and call the surface functions.
problem code:
const makeCounter = () => {
let count = 0;
return {
count,
incr() {
this.count += 1
return this.count;
},
decr() {
this.count -= 1
return this.count;
},
};
};
const myCounter = makeCounter();
myCounter.incr();
myCounter.incr();
myCounter.count = 'hehehehe';
myCounter.incr();
myCounter.decr();
console.log(myCounter); // { count: NaN, incr: {}, decr: {} }
Or mutate the function prop:
const myCounter = makeCounter();
myCounter.incr();
myCounter.incr();
myCounter.incr();
myCounter.incr = () => {
console.log('I have hijacked your increment. There is nothing you can do.');
};
myCounter.decr();
const lastValue = myCounter.incr();
console.log({lastValue}); // { lasValue: undefined }
Solution:
import 'ses';
lockdown();
const makeCounter = () => {
let count = 0;
return harden({
incr() {
this.count += 1
return this.count;
},
decr() {
this.count -= 1
return this.count;
},
});
};
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2022-11-08 [Typescript] 90. Medium - Replace
2022-11-08 [Typescript] 89. Hard - Currying 1
2022-11-08 [Typescript] 88. Hard - Simple Vue
2022-11-08 [Typescript] ThisType
2020-11-08 [Kotlin] Mapping between two entities
2018-11-08 [Node.js] Trigger a File Download in Express
2018-11-08 [WASM Rust] Create and Publish a NPM Package Containing Rust Generated WebAssembly using wasm-pack