Safe Object Path Get And Set
Read and copy-on-write nested values with dot and bracket paths while blocking proto, constructor and prototype segments.
Code
const BLOCKED = new Set(["__proto__", "prototype", "constructor"]);
export function parsePath(path) {
if (typeof path !== "string" || path.trim() === "") {
throw new TypeError("path must be a non-empty string");
}
const normalized = path.replace(/\[(\d+)\]/g, ".$1");
if (/[\[\]]/.test(normalized)) throw new SyntaxError("unsupported bracket syntax");
if (normalized.startsWith(".") || normalized.endsWith(".") || normalized.includes("..")) {
throw new SyntaxError("path contains an empty segment");
}
return normalized.split(".");
}
function assertSafe(token) {
if (BLOCKED.has(token)) throw new TypeError("unsafe path segment: " + token);
}
function isPlainObject(value) {
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
const prototype = Object.getPrototypeOf(value);
return prototype === Object.prototype || prototype === null;
}
export function getPath(source, path, fallback) {
let current = source;
for (const token of parsePath(path)) {
assertSafe(token);
if (current === null || (typeof current !== "object" && typeof current !== "function")) {
return fallback;
}
if (!Object.prototype.hasOwnProperty.call(current, token)) return fallback;
current = current[token];
}
return current === undefined ? fallback : current;
}
export function setPath(source, path, value) {
const tokens = parsePath(path);
for (const token of tokens) assertSafe(token);
const root = Array.isArray(source)
? [...source]
: isPlainObject(source)
? { ...source }
: {};
let current = root;
for (let index = 0; index < tokens.length - 1; index += 1) {
const token = tokens[index];
const nextToken = tokens[index + 1];
if (Array.isArray(current) && !/^\d+$/.test(token)) {
throw new TypeError("array path segments must be numeric");
}
const existing = Object.prototype.hasOwnProperty.call(current, token)
? current[token]
: undefined;
let next;
if (Array.isArray(existing)) next = [...existing];
else if (isPlainObject(existing)) next = { ...existing };
else next = /^\d+$/.test(nextToken) ? [] : {};
current[token] = next;
current = next;
}
const last = tokens[tokens.length - 1];
if (Array.isArray(current)) {
if (!/^\d+$/.test(last)) throw new TypeError("array path segments must be numeric");
current[Number(last)] = value;
} else {
current[last] = value;
}
return root;
}How it works
Count the functions parsePath, assertSafe, isPlainObject and getPath as the entire surface area of Safe Object Path Get And Set.
Iteration is nested: for (const token of parsePath(path)) frames the pass, for (const token of tokens) assertSafe(token); does the per-item step. Failure is loud: TypeError and SyntaxError aborts the current operation instead of degrading it.
Applying it
Entry signature: export function parsePath(path)
Import parsePath from the file and call it — the rest of the code is implementation detail.
Statements that matter
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
if (!/^\d+$/.test(last)) throw new TypeError("array path segments must be numeric");
if (/[\[\]]/.test(normalized)) throw new SyntaxError("unsupported bracket syntax");
Interactive Tools for This Topic
Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.CSP Header BuilderBuild a Content-Security-Policy header from per-directive sources, skipping empty ones and rejecting a blanket wildcard.
FAQ
- How does the code behave when input is bad?
- It throws `TypeError` and `SyntaxError` before any real work runs — handle it at the call site or let it propagate deliberately.
- What does the package manager see?
- An empty dependency file. The snippet imports nothing that needs fetching. The file defines `parsePath`, `assertSafe`, `isPlainObject` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.