feat: block dangerous scope keys and harden findScope (#898)

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
Yang Jun
2026-07-15 23:02:06 +08:00
co-authored by Cursor
parent 61ed163821
commit 6bdf65a6a1
5 changed files with 105 additions and 4 deletions
+16 -1
View File
@@ -1,4 +1,5 @@
import { Drop } from '../drop/drop'
import { hasOwnProperty } from '../util'
export interface ScopeObject extends Record<string | number | symbol, any> {
toLiquid?: () => any;
@@ -6,8 +7,22 @@ export interface ScopeObject extends Record<string | number | symbol, any> {
export type Scope = ScopeObject | Drop
const BLOCKED_SCOPE_KEYS = new Set(['__proto__', 'constructor', 'prototype'])
export function isBlockedScopeKey (key: PropertyKey): boolean {
return typeof key === 'string' && BLOCKED_SCOPE_KEYS.has(key)
}
export function createScope (from?: ScopeObject): ScopeObject {
return from ? sanitizeScope(from) : Object.create(null)
}
export function sanitizeScope (obj: ScopeObject): ScopeObject {
const scope = Object.create(null)
if (from) Object.assign(scope, from)
for (const key of Object.keys(obj)) {
if (!isBlockedScopeKey(key) && hasOwnProperty.call(obj, key)) {
scope[key] = obj[key]
}
}
return scope
}