Skip to content

3D engine that AI agents drive

Reject inherited names as subsurface fields (#2538)

Nguyen Xuan Anb1bfca67d44a
diff --git a/changes/2538.md b/changes/2538.md
new file mode 100644
index 00000000..67b1b293
--- /dev/null
+++ b/changes/2538.md
@@ -0,0 +1 @@
+- A subsurface block now rejects inherited names such as `constructor` as unknown fields (#2538).
diff --git a/src/subsurface-limits.js b/src/subsurface-limits.js
index a25d9241..7366a773 100644
--- a/src/subsurface-limits.js
+++ b/src/subsurface-limits.js
@@ -23,7 +23,7 @@ function checkTriple(label, value) {
export function validateSubsurface(block, tag = 'surface') {
if (block === null || typeof block !== 'object' || Array.isArray(block)) throw new TypeError('subsurface must be an object: { weight, radius, scale, color }.');
for (const key of Object.keys(block)) {
- if (!(key in SUBSURFACE_RANGES)) throw new RangeError(`Unknown subsurface field ${key}. Use weight, radius, scale or color.`);
+ if (!Object.hasOwn(SUBSURFACE_RANGES, key)) throw new RangeError(`Unknown subsurface field ${key}. Use weight, radius, scale or color.`);
}
for (const field of ['weight', 'scale']) {
const value = block[field];
diff --git a/tests/subsurface.test.js b/tests/subsurface.test.js
index 1f81cdd8..77342799 100644
--- a/tests/subsurface.test.js
+++ b/tests/subsurface.test.js
@@ -29,6 +29,9 @@ test('the subsurface block is checked against bounded ranges and names the field
assert.throws(() => validateSubsurface({ radius: [1, 2, 101] }), /subsurface.radius must be three numbers between 0 and 100/);
assert.throws(() => validateSubsurface({ color: [0, 0, 2] }), /subsurface.color must be three numbers between 0 and 1/);
assert.throws(() => validateSubsurface({ anisotropy: 0.5 }), /Unknown subsurface field anisotropy/);
+ for (const inherited of ['constructor', 'toString', 'hasOwnProperty']) {
+ assert.throws(() => validateSubsurface(JSON.parse(`{"${inherited}": 1}`)), new RegExp(`Unknown subsurface field ${inherited}`));
+ }
assert.throws(() => validateSubsurface([1]), /subsurface must be an object/);
assert.throws(() => validateSubsurface({ weight: 1 }, 'foam'), /subsurface needs a standard PBR material tag/);
});