{"id":436,"date":"2023-08-20T17:07:14","date_gmt":"2023-08-20T09:07:14","guid":{"rendered":"https:\/\/blog.byzhb.top\/?p=436"},"modified":"2023-09-02T17:07:49","modified_gmt":"2023-09-02T09:07:49","slug":"jswr","status":"publish","type":"post","link":"https:\/\/blog.byzhb.top\/index.php\/2023\/08\/20\/jswr\/","title":{"rendered":"JavaScript\u539f\u578b\u94fe\u6c61\u67d3"},"content":{"rendered":"<h1>\u4e00\u3001\u4ec0\u4e48\u662f\u539f\u578b\u94fe\u6c61\u67d3(JS)<\/h1>\n<p>\u539f\u578b\u94fe\u6c61\u67d3\uff08Prototype Pollution\uff09\u662f\u4e00\u79cd\u5b89\u5168\u6f0f\u6d1e\uff0c\u5728 JavaScript \u4e2d\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u548c\u4e0d\u5b89\u5168\u7684\u884c\u4e3a\u3002\u5b83\u5229\u7528\u4e86 JavaScript \u4e2d\u7684\u539f\u578b\u7ee7\u627f\u673a\u5236\uff0c\u901a\u8fc7\u4fee\u6539\u5bf9\u8c61\u7684\u539f\u578b\u94fe\u6765\u6c61\u67d3\u6216\u7be1\u6539\u76ee\u6807\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<h1>\u4e8c\u3001\u524d\u7f6e\u77e5\u8bc6<\/h1>\n<h2>2.1 JS\u5bf9\u8c61<\/h2>\n<p>\u5c5e\u6027\u58f0\u660e\u53ca\u8c03\u7528\u683c\u5f0f\u5982\u4e0b<\/p>\n<pre><code class=\"language-js\">key:value \/\/\u58f0\u660e\n\nclassname.key \/\/\u8c03\u7528<\/code><\/pre>\n<p>\u65b9\u6cd5\u58f0\u660e\u53ca\u8c03\u7528\u683c\u5f0f\u5982\u4e0b<\/p>\n<pre><code class=\"language-js\">methodname:function(){\n    \u2026\u2026\n} \/\/\u58f0\u660e\n\nclassname.methodname()  \/\/\u8c03\u7528<\/code><\/pre>\n<h3>2.11 \u901a\u8fc7\u7c7b\u521b\u5efa<\/h3>\n<p>\u7c7b\u7684\u58f0\u660e\u4e0e\u5b9e\u4f8b\u5316\u548c\u5176\u4ed6\u8bed\u8a00\u76f8\u4f3c\uff0c\u5982\u679c\u4e0d\u58f0\u660e\u6784\u9020\u51fd\u6570\uff0c\u5219\u8be5\u51fd\u6570\u4ecd\u5b58\u5728\u4f46\u5185\u5bb9\u4e3aNULL<\/p>\n<pre><code class=\"language-js\">class demo{\n  constructor(name, age) {\n    this.name = name;\n    this.age = age;\n  }\n}\nlet person = new demo(ELITE,25);\n\/\/ \u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\nconsole.log(person.name); \/\/ \u8f93\u51fa:ELITE\nconsole.log(person.age); \/\/ \u8f93\u51fa: 25<\/code><\/pre>\n<h3>2.12 \u5b57\u9762\u91cf\u6a21\u5f0f\u521b\u5efa<\/h3>\n<p>JavaScript\u4e2d\u5bf9\u8c61\u7684\u5b57\u9762\u91cf\u6a21\u5f0f\u5b9a\u4e49\u7c7b\u4f3c\u4e8epython\u4e2d\u7684\u5b57\u5178\uff0c\u5c5e\u6027\u662f\u952e\u503c\u5bf9\u7684\u7ec4\u5408\uff0c\u65b9\u6cd5\u4e5f\u4ee5\u7c7b\u4f3c\u7684\u5f62\u5f0f\u5b9a\u4e49\u793a\u4f8b\u5982\u4e0b<\/p>\n<pre><code class=\"language-js\">var person = {\n    name:&quot;ELITE&quot;,\n    age:19,\n    gender:&#039;male&#039;,\n    sayName:function(){\n        console.log(&quot;hello world!&quot;);\n    }\n};<\/code><\/pre>\n<h3>2.13\u6784\u9020\u51fd\u6570\u6a21\u5f0f\u521b\u5efa<\/h3>\n<pre><code class=\"language-js\">\/\/ \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u5c06\u4f5c\u4e3a\u5bf9\u8c61\u7684\u6784\u9020\u65b9\u6cd5\nfunction Person(name, age) {\n  this.name = name;\n  this.age = age;\n}\n\/\/ \u4f7f\u7528\u6784\u9020\u65b9\u6cd5\u521b\u5efa\u5bf9\u8c61\u5b9e\u4f8b\nvar person1 = new Person(&#039;Alice&#039;, 25);  \/\/\u7ed9\u6784\u9020\u65b9\u6cd5\u4f20\u503c\n\/\/ \u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\nconsole.log(person1.name); \/\/ \u8f93\u51fa: Alice\nconsole.log(person1.age); \/\/ \u8f93\u51fa: 25<\/code><\/pre>\n<p>JavaScript\u91cc\u7684\u6784\u9020\u65b9\u6cd5\u548c\u5176\u4ed6\u8bed\u8a00\u4e2d\u7684\u6784\u9020\u65b9\u6cd5\u76f8\u540c\uff0c\u90fd\u4f1a\u5728\u5bf9\u8c61\u521d\u59cb\u5316\u65f6\u81ea\u52a8\u6267\u884c<\/p>\n<h2>2.2 \u9ed8\u8ba4\u5c5e\u6027<\/h2>\n<p>\u5728JavaScript\u4e2d\u4e00\u5207\u7686\u5bf9\u8c61(<em>\u8fd9\u91cc\u5c31\u548cpython\u7c7b\u4f3c\u4e86<\/em>)\uff0c\u51fd\u6570\u4e5f\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u79cd\u7279\u6b8a\u7684\u53d8\u91cf<\/p>\n<ul>\n<li><strong><code>__proto__<\/code><\/strong> \u5c5e\u6027\uff0c\u5b83\u662f<strong>\u5bf9\u8c61\u72ec\u6709\u7684<\/strong>\uff0c\u6307\u5411\u5bf9\u8c61(<strong>\u6784\u9020\u51fd\u6570<\/strong>)\u7684\u539f\u578b(\u53ef\u4ee5\u8bf4\u662f\u7236\u7c7b)<\/li>\n<li><code>constructor<\/code>\u5c5e\u6027\uff0c\u5b83\u662f<strong>\u5bf9\u8c61\u72ec\u6709\u7684<\/strong>\uff0c\u6307\u5411\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570<\/li>\n<li><strong><code>prototype<\/code><\/strong>\u5c5e\u6027\uff0c\u5b83\u662f<strong>\u51fd\u6570\u72ec\u6709\u7684<\/strong>\uff0c\u6307\u5411\u51fd\u6570\u7684\u539f\u578b\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u8bf4\u662f\u8be5\u51fd\u6570\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u521b\u5efa\u7684\u5b9e\u4f8b\u7684\u539f\u578b\u5bf9\u8c61<\/li>\n<\/ul>\n<p>\u5177\u4f53\u5173\u7cfb\u5982\u56fe\u6240\u793a<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/blog.byzhb.top\/wp-content\/uploads\/2023\/09\/image-20230810235628733.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/blog.byzhb.top\/wp-content\/uploads\/2023\/09\/image-20230810235628733.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><br \/>\n\u5e76\u4e14\u5b58\u5728\u8fd9\u6837\u4e00\u4e2a\u7b49\u5f0f<\/p>\n<pre><code class=\"language-js\">\u5bf9\u8c61.__proto__ == \u6784\u9020\u51fd\u6570.prototype<\/code><\/pre>\n<p>\u4e00\u822c\u521b\u5efa\u7684\u5bf9\u8c61\u7684\u539f\u578b\u9ed8\u8ba4\u90fd\u662fobject\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-js\">let person = {\n  name: &#039;John&#039;,\n  age: 30,\n  city: &#039;New York&#039;\n};\n\/\/\u672a\u6307\u660e\u6784\u9020\u51fd\u6570<\/code><\/pre>\n<h1>\u4e09\u3001\u6c61\u67d3\u5229\u7528<\/h1>\n<p>\u6c61\u67d3\u9700\u8981\u7528\u5230\u8fd9\u6837\u4e00\u4e2a\u5408\u5e76\u51fd\u6570\uff0c\u548cpython\u91cc\u7684merge\u51fd\u6570\u5dee\u4e0d\u591a<\/p>\n<pre><code class=\"language-js\">function merge(target, source) {\n    console.log(&#039;merge&#039;, target, source);\n    \/\/ \u904d\u5386 source \u4e2d\u7684 key\u3002\n    for (let key in source) {\n        if (key in source &amp;&amp; key in target) {\n            merge(target[key], source[key])  \/\/\u9012\u5f52\u904d\u5386\n        } else {\n            target[key] = source[key]  \/\/\u5408\u5e76\u8d4b\u503c\n        }\n    }\n}<\/code><\/pre>\n<p>\u91cd\u70b9\u5728\u4e8e\u8fd9\u4e00\u6761\u8bed\u53e5<\/p>\n<pre><code class=\"language-js\">merge(target[key], source[key])<\/code><\/pre>\n<p>\u5047\u5b9a\u5bf9\u8c61\u5982\u4e0b<\/p>\n<pre><code class=\"language-javascript\">let a = {}\nlet b = {&quot;id&quot;: 1, &quot;__proto__&quot;: {&quot;no&quot;: 2}}\n\n\/\/\u8fd9\u4fe9\u90fd\u672a\u6307\u5b9a\u539f\u578b\uff0c\u9ed8\u8ba4\u90fd\u4e3aObject<\/code><\/pre>\n<p>\u6211\u4eec\u8981\u901a\u8fc7a\u4e0eb\u7684\u5408\u5e76\uff0c\u53bb\u6c61\u67d3object\u5bf9\u8c61\uff0cb\u4e3a\u539f\u5bf9\u8c61<\/p>\n<p>\u5f53for\u8bed\u53e5\u8fed\u4ee3\u5230b\u5bf9\u8c61\u7684\u7b2c\u4e8c\u4e2a\u5c5e\u6027\u65f6\u6c61\u67d3\u5f00\u59cb\uff0c\u6211\u4eec\u7684\u76ee\u7684\u662f\u5f97\u5230 <strong>a[__proto__]<\/strong>\u5c5e\u6027\uff0c\u4e5f\u5c31\u662f<strong>Object\u5bf9\u8c61<\/strong>\uff0c\u4f46\u662f\u6211\u4eec\u8fd9\u79cd\u5b57\u9762\u91cf\u5b9a\u4e49\u65b9\u5f0f\u662f\u9519\u7684<\/p>\n<pre><code class=\"language-js\">let b = {&quot;id&quot;: 1, &quot;__proto__&quot;: {&quot;no&quot;: 2}}<\/code><\/pre>\n<p>__proto__\u5e76\u4e0d\u4f1a\u88ab\u539f\u5c01\u4e0d\u52a8\u7684\u89e3\u91ca\u4e3a\u952e\u503c\uff0c\u800c\u662f\u4f1a<strong>\u88ab\u5f53\u505a\u9ed8\u8ba4\u5c5e\u6027<\/strong>\u89e3\u6790\u4e3a b\u5bf9\u8c61 \u7684<strong>\u539f\u578b<\/strong>\uff0c\u8fd9\u65f6b\u5bf9\u8c61\u7684\u539f\u578b\u53d1\u751f\u6539\u53d8\uff0c\u4e0d\u518d\u662f\u9ed8\u8ba4\u7684Object\uff0c\u800c\u662f\u540e\u6765\u5b9a\u4e49\u7684 <strong>{&quot;no&quot;: 2}<\/strong>\uff0c\u8fd9\u6837b\u5bf9\u8c61\u7684\u952e\u503c\u53ea\u6709a\u548cb\uff0cb\u662f\u5728\u539f\u578b(\u7236\u7c7b)\u4e2d\u7684\uff0c\u8fd9\u6837\u53ea\u662f\u4f1a\u6b63\u5e38\u7684\u5408\u5e76\uff0c\u800c\u4e0d\u4f1a\u6c61\u67d3\u5230\u4e0a\u4e00\u5c42<\/p>\n<p>\u793a\u4f8b\u5982\u4e0b<\/p>\n<p>demo\uff1a<\/p>\n<pre><code class=\"language-js\">function merge(target, source) {\n    for (let key in source) {\n        if (key in source &amp;&amp; key in target) {\n           merge(target[key], source[key])\n        } else {\n           target[key] = source[key]\n        }\n   }\n}\nlet a = {}\nlet b = {&quot;id&quot;: 1, &quot;__proto__&quot;: {&quot;no&quot;: 2}}\nmerge(a, b)\nconsole.log(a.id,a.no)\nconsole.log(a.__proto__)<\/code><\/pre>\n<p>\u7ed3\u679c<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/blog.byzhb.top\/wp-content\/uploads\/2023\/09\/image-20230811012403104.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/blog.byzhb.top\/wp-content\/uploads\/2023\/09\/image-20230811012403104.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><br \/>\n\u6211\u4eec\u5e94\u8be5\u5bf9payload\u8fdb\u884cJSON\u89e3\u6790\u5904\u7406\uff0c\u8fd9\u6837<strong>__proto__<\/strong>\u5c31\u53ef\u4ee5\u88ab\u5f53\u505a\u952e\u503c\uff0c\u800c\u4e0d\u4f1a\u88ab\u89e3\u6790\uff0c\u793a\u4f8b\u5982\u4e0b<\/p>\n<pre><code class=\"language-javascript\">let b = JSON.parse(&#039;{&quot;id&quot;: 1, &quot;__proto__&quot;: {&quot;no&quot;: 2}}&#039;)<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/blog.byzhb.top\/wp-content\/uploads\/2023\/09\/image-20230811012704828.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/blog.byzhb.top\/wp-content\/uploads\/2023\/09\/image-20230811012704828.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><br \/>\n\u53ef\u4ee5\u770b\u5230\u6210\u529f\u6c61\u67d3\u5230\u4e86Object\u5bf9\u8c61<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u578b\u94fe\u6c61\u67d3\uff08Prototype Pollution\uff09\u662f\u4e00\u79cd\u5b89\u5168\u6f0f\u6d1e\uff0c\u5728 JavaScript \u4e2d\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u548c\u4e0d\u5b89\u5168\u7684\u884c\u4e3a\u3002<\/p>\n","protected":false},"author":1,"featured_media":437,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,37,22],"tags":[15,38,26],"class_list":["post-436","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ctf","category-nodejs","category-webs","tag-ctf","tag-nodejs","tag-web"],"_links":{"self":[{"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/posts\/436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/comments?post=436"}],"version-history":[{"count":1,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/posts\/436\/revisions"}],"predecessor-version":[{"id":440,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/posts\/436\/revisions\/440"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/media\/437"}],"wp:attachment":[{"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/media?parent=436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/categories?post=436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/tags?post=436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}