Skip to content

Commit

Permalink
Prevent prototype pollution reported in issue #4
Browse files Browse the repository at this point in the history
  • Loading branch information
brycebaril committed Dec 21, 2023
1 parent b7d97ec commit 27d569b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions flatten.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

module.exports = flatten

function flatten(obj) {
Expand Down
4 changes: 4 additions & 0 deletions nest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

module.exports = nest

var seek = require("./seek")
Expand Down Expand Up @@ -37,6 +39,8 @@ function insert(target, path, value) {
var len = pathBits.length
for (var i = 0; i < len; i += 2) {
var key = pathBits[i]
if (key === "__proto__") continue
if (key === "constructor" && typeof target[key] == "function") continue
var type = pathBits[i + 1]

if (type == null && key) parent[key] = value
Expand Down
2 changes: 2 additions & 0 deletions seek.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

module.exports = seek

var nestedRe = /(\.|\[)/
Expand Down
2 changes: 2 additions & 0 deletions test/flatten.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

var test = require("tape").test

var flatten = require("../flatten")
Expand Down
10 changes: 10 additions & 0 deletions test/nest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

var test = require("tape").test

var nest = require("../nest")
Expand Down Expand Up @@ -139,3 +141,11 @@ test("nest empty", function (t) {
t.deepEquals(nest(struct), expect, "empty object is still empty")
t.end()
})

test("no prototype pollution", function (t) {
nest({'constructor.prototype.fail': true})
nest({'__proto__.bad': true})
t.false({}.fail, "constructor.prototype not polluted")
t.false({}.bad, "__proto__ not polluted")
t.end()
})

0 comments on commit 27d569b

Please sign in to comment.