Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ const pm = require("picomatch");

export type Key = string | number | (() => string | number);

const ILLEGAL_KEYS = new Set(["prototype", "constructor", "__proto__"]);

function isIllegalKey(key: string): Boolean {
return ILLEGAL_KEYS.has(key);
}

function disallowProtopath(key: string | number): void {
if (typeof key === "string" && isIllegalKey(key)) {
throw new Error("Unsafe key encountered: " + key)
}
}

function getSegments(path: Key | Key[]): (string | number)[] {
let segments = [];
if (typeof path === "string") {
Expand Down Expand Up @@ -180,6 +192,7 @@ export default class PTree {
for (let i = 0; i < segments.length; i++) {
const current = obj;
const seg = segments[i];
disallowProtopath(seg);

if (i < segments.length - 1) {
obj = (<any>obj)[seg];
Expand Down
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let $p = require("../dist/").default;
const assert = require("assert")

function compare(actual, expected) {
console.log(
Expand Down Expand Up @@ -607,3 +608,15 @@ const obj25 = {
compare($p.from(obj25).wildcard("a.*").length, 2);
compare($p.from(obj25).wildcard("b.**").length, 4);
compare($p.from(obj25).wildcard("d.*").length, 0);

function isProtoPathPolluted() {
const t = new $p({});
t.set('__proto__.polluted', true)
}

function testProtoPathPollution() {
assert.throws(isProtoPathPolluted, "Test failed for Proto Path.")
console.log("Test passed.")
}

testProtoPathPollution();