この記事の内容
実際に
JavaScriptの プロトタイプ汚染の 脆弱性を 防ぐ 方法
プロトタイプ汚染は、
この
JavaScriptのプロトタイプとは
JavaScriptでは、プロトタイプベースの
たとえば、const one = 1 をtoExponential() 関数にhasOwnProperty() に
数値変数の
ほとんどの
任意の
継承チェーンの
たとえば、
const myFirstObject = {};
この__proto__ キーワードを
myFirstObject.__proto__次の
const mySecondObject = {}
mySecondObject.__proto__.customProperty = "this is a property in the prototype"この
console.log(myFirstObject.__proto__)
console.log(mySecondObject.__proto__)どちらのconsole.log() の
[Object: null prototype] { customProperty: 'this is a property in the prototype' }
[Object: null prototype] { customProperty: 'this is a property in the prototype' }2つ目のcustomProperty は、
次のように
const myThirdObject = {}
console.log(myThirdObject.__proto__)ここにもcustomProperty が
[Object: null prototype] { customProperty: 'this is a property in the prototype' }オブジェクトに
const nonEmptyObject = {
customProperty: "this is a property defined on the object itself"
}
console.log(nonEmptyObject.customProperty) // "this is a property defined on the object itself"一方、
const anotherEmptyObject = {}
console.log(anotherEmptyObject.customProperty) // "this is a property in the prototype"JavaScriptのプロトタイプ汚染とは
プロトタイプ汚染は、
サーバーサイドと
この
攻撃者が
isAdmin: true
攻撃者はisAdmin: false プロパティがisAdmin: true を
プロトタイプ汚染は、toString() 関数を
let newObject = {}
newObject.__proto__.toString = function() {this.toString()}このtoString() RangeError: Maximum call stack size exceeded error が
攻撃者が
プロトタイプ汚染の実例
Node.jsで
一緒に
npm install
npm startブラウザーで
[
{
"_id": "654c062468efa31d8c055c13",
"text": "Jason's first todo item",
"open": true,
"visible": true,
"owner": "654c062468efa31d8c055c0e",
"__v": 0
},
{
"_id": "654c062468efa31d8c055c14",
"text": "First todo for Kelly",
"open": true,
"visible": true,
"owner": "654c062468efa31d8c055c0f",
"__v": 0
},
{
"_id": "654c062468efa31d8c055c15",
"text": "Paul's thing to be done",
"open": true,
"visible": true,
"owner": "654c062468efa31d8c055c10",
"__v": 0
}
]
クローンしたdatabase/seed.js を
const seedTodoItems = [
{
text: "Jason's first todo item",
open: true,
visible: true,
owner: "Jason"
},
{
text: "First todo for Kelly",
open: true,
visible: true,
owner: "Kelly"
},
{
text: "Paul's thing to be done",
open: true,
visible: true,
owner: "Paul"
},
{
text: "A HIDDEN TODO",
open: true,
owner: "Alexandra"
}
]
初期データのserver.js を
app.get('/todos/', (req, res) => {
TodoItem.find({visible: true})
.then(data => res.json(data))
.catch(error => res.json({error}))
})新しいhttp://localhost:8080/todos/add: に
POST http://localhost:8080/todos/add
Content-Type: application/json
{
"__proto__": {
"visible": true
},
"text": "😈 new todo item that tries to mess with the prototype",
"owner": "654c0cc960d82b5802a0b30d"
}コマンドラインから
curl -X POST -H 'content-type: application/json' http://localhost:8080/todos/ad
d --data '{ "__proto__": {
"visible": true
},
"text": "😈 new todo item that tries to mess with the prototype",
"owner": "654c0cc960d82b5802a0b30d"
}'もうhttp://localhost:8080/todos/ に
[
{
"_id": "654c0d59654aeeb63dfd5ae8",
"text": "Jason's first todo item",
"open": true,
"visible": true,
"owner": "654c0d59654aeeb63dfd5ae3",
"__v": 0
},
{
"_id": "654c0d59654aeeb63dfd5ae9",
"text": "First todo for Kelly",
"open": true,
"visible": true,
"owner": "654c0d59654aeeb63dfd5ae4",
"__v": 0
},
{
"_id": "654c0d59654aeeb63dfd5aea",
"text": "Paul's thing to be done",
"open": true,
"visible": true,
"owner": "654c0d59654aeeb63dfd5ae5",
"__v": 0
},
{
"_id": "654c0d59654aeeb63dfd5aeb",
"text": "A HIDDEN TODO",
"open": true,
"owner": "654c0d59654aeeb63dfd5ae6",
"__v": 0
},
{
"_id": "654c0e6f654aeeb63dfd5aef",
"text": "😈 new todo item that tries to mess with the prototype",
"open": true,
"owner": "654c0cc960d82b5802a0b30d",
"__v": 0
}
]
最初は
新しい
app.post('/todos/add', (req, res) => {
const defaults = {
open: true,
}
const todoToAdd = lodash.merge(defaults, req.body)
const todoItem = new TodoItem(todoToAdd);
todoItem.save()
.then(() => res.json({msg: `Successfully added a todo item`}))
.catch(error => res.json({error: error.message}))
})
アプリケーションはdefaults オブジェクトにmerge() メソッドをObject.__proto__.visible が
そのvisible プロパティを
app.get('/todos/', (req, res) => {
TodoItem.find({visible: true})
.then(data => res.json(data))
.catch(error => res.json({error}))
})
フィルターのvisible: false プロパティが
{
text: "A HIDDEN TODO",
open: true,
owner: "Alexandra"
}しかし、visible: true がvisible: true が
プロトタイプ汚染の脆弱性を防ぐ
先ほどの
プロパティキーをサニタイズする
最も
if (key == '__proto__') {
return;
}ただし、
許可する
if (allowedKeys.includes(key)) {
// Proceed with merge
}オブジェクトの代わりにMapを使う
JavaScriptには、Map がget() 関数で
const defaultsMap = new Map()
defaultsMap.set("open", true);
defaultsMap.set("hidden", false)
// vs
const defaultsObject = {
open: true,
hidden: false
}Object.freeze()またはObject.seal()で変更を防ぐ
JavaScriptには、Object.freeze() とObject.seal() です。
Object.freeze(Object.prototype)プロトタイプ汚染のObject.freeze() が
Object.seal() はObject.seal() をtoString() などの
Snykのnoppパッケージを使う
オブジェクトをnoppパッケージです。Object.freeze() を
先ほどのnopp を
npm install noppそしてserver.js の
import express from 'express';
import connect from "./database/connection.js";
import seedDatabase from "./database/seed.js";
import TodoItem from "./model/todoitem.model.js";
import lodash from "lodash";
import cors from "cors";
import "nopp";nopp を
Snyk IDE拡張機能でプロトタイプ汚染の脆弱性を検出する
プロトタイプ汚染を
このような
Snyk拡張機能は、
以下は、

Visual Studio Codeの

「Snyk Security - Code, Open Source Dependencies, IaC Configurations」と

Snyk拡張機能と
Visual Studio Codeの

Snykに

ログインすると、

「Authenticate」を

ブラウザーウィンドウを
Snyk拡張機能で

左側の
「Editor」
JavaScriptの
アプリケーションセキュリティを

Snyk Security IDE拡張機能を
JavaScriptの
JavaScriptアプリの セキュリティ対策を 始めましょう
Snykを
クレジットカードは
またはAzure AD Docker ID Bitbucketで
Snykを