Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Guard caller-chosen keys flagged by the first CodeQL scan (closes #475)

Nguyen Xuan Anc99562ee3ff6
diff --git a/changes/475-codeql-input-guards.md b/changes/475-codeql-input-guards.md
new file mode 100644
index 00000000..47f7a2af
--- /dev/null
+++ b/changes/475-codeql-input-guards.md
@@ -0,0 +1 @@
+- Names and kinds the caller picks now reach only their own table entry, never one inherited from `Object.prototype`. A mesh attribute named `__proto__`, `constructor` or `prototype` throws a TypeError. A cloth collider of an unknown kind throws a TypeError that lists ball, capsule and box, and a pinned particle that does not exist throws a RangeError. Baking a pattern of an unknown type throws the same RangeError that normalizing it does (issue #475).
diff --git a/changes/475-codeql-input-guards.size.md b/changes/475-codeql-input-guards.size.md
new file mode 100644
index 00000000..a410fa34
--- /dev/null
+++ b/changes/475-codeql-input-guards.size.md
@@ -0,0 +1,2 @@
+- entry:cloth: +87 B for the collider kind and pinned particle checks, which turn a silent call through an inherited method into an error that names the allowed kinds.
+- scene:agent-scene+lazy: +133 B for the same cloth checks, the reserved attribute names in mesh data and the pattern type check.
diff --git a/src/cloth.js b/src/cloth.js
index bbfa6ab4..d7e9cb3b 100644
--- a/src/cloth.js
+++ b/src/cloth.js
@@ -391,6 +391,12 @@ export function createCloth(positions, triangles, options = {}) {
const particleCount = positions.length / 3;
const colliders = options.colliders ?? [];
const moving = options.moving ?? null;
+ for (const collider of colliders) {
+ if (!Object.hasOwn(PUSH_OUT, collider?.kind ?? '')) throw new TypeError(`Cloth collider kind must be one of ${Object.keys(PUSH_OUT).join(', ')}.`);
+ }
+ for (const particle of options.pinned ?? []) {
+ if (!Number.isInteger(particle) || particle < 0 || particle >= particleCount) throw new RangeError(`Cloth pinned particle ${particle} does not exist; there are ${particleCount}.`);
+ }
const constraints = buildConstraints(positions, triangles, particleCount, options.bend ?? 0.1, options.stiffness ?? 0.9);
checkWork(particleCount, constraints.count, triangles.length / 3, colliders.length + (moving?.colliderCount ?? 0), options.frameCount ?? 1);
const density = options.density ?? 0.2;
diff --git a/src/material-patterns.js b/src/material-patterns.js
index 2654aea9..78618e42 100644
--- a/src/material-patterns.js
+++ b/src/material-patterns.js
@@ -229,6 +229,7 @@ export function bakeMaterialPattern(pattern) {
};
if (pattern.type === GRAPH_TYPE) return { ...bakeMaterialGraph(pattern.graph, pattern), ...placement };
const { resolution, type, seed, count, colors, bump } = pattern;
+ if (!Object.hasOwn(PATTERN_SAMPLERS, type)) throw patternError(`type must be one of ${MATERIAL_PATTERN_TYPES.join(', ')}.`);
const sampler = PATTERN_SAMPLERS[type];
const pixelCount = resolution * resolution;
const heights = new Float32Array(pixelCount);
diff --git a/src/mesh-data.js b/src/mesh-data.js
index aaaab6cb..fed009d5 100644
--- a/src/mesh-data.js
+++ b/src/mesh-data.js
@@ -5,6 +5,7 @@ export const DOMAINS = Object.freeze(['point', 'edge', 'face', 'corner']);
export const MAX_MESH_VERTICES = 4_000_000;
export const MAX_MESH_CORNERS = 16_000_000;
export const TAG_PREFIX = 'tag:';
+const RESERVED_ATTRIBUTE_NAMES = new Set(['__proto__', 'constructor', 'prototype']);
const ATTRIBUTE_TYPES = new Map([
[Float32Array, 'float32'], [Uint8Array, 'uint8'], [Uint16Array, 'uint16'],
@@ -26,6 +27,8 @@ function domainCount(mesh, domain) {
}
function validateAttribute(name, attribute, expectedCount) {
+ // Attributes are stored by name on plain objects, where these names would reach the prototype instead of an attribute.
+ if (RESERVED_ATTRIBUTE_NAMES.has(name)) throw new TypeError(`Attribute name ${name} is reserved; pick another name.`);
if (!attribute || typeof attribute !== 'object') throw new TypeError(`Attribute ${name} must be { data, size }.`);
const { data, size } = attribute;
if (!ATTRIBUTE_TYPES.has(data?.constructor)) throw new TypeError(`Attribute ${name} needs a Float32, Uint8, Uint16, Uint32 or Int32 array.`);
diff --git a/tests/input-key-guards.test.js b/tests/input-key-guards.test.js
new file mode 100644
index 00000000..1b1f8d2f
--- /dev/null
+++ b/tests/input-key-guards.test.js
@@ -0,0 +1,42 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { createCloth } from '../src/cloth.js';
+import { createMeshData } from '../src/mesh-data.js';
+import { bakeMaterialPattern, normalizeMaterialPattern } from '../src/material-patterns.js';
+
+// Names and kinds that come from the caller pick an own entry of a table or object, never one inherited from
+// Object.prototype (CodeQL js/remote-property-injection and js/unvalidated-dynamic-method-call).
+
+function triangleMesh(attributes) {
+ return createMeshData({
+ positions: new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]),
+ faceOffsets: new Uint32Array([0, 3]),
+ cornerVertices: new Uint32Array([0, 1, 2]),
+ attributes,
+ });
+}
+
+test('a mesh attribute cannot be named after a prototype key', () => {
+ const attribute = { data: new Float32Array(3), size: 1 };
+ for (const name of ['__proto__', 'constructor', 'prototype']) {
+ const point = JSON.parse(`{"${name}": 0}`);
+ point[name] = attribute;
+ assert.throws(() => triangleMesh({ point }), /reserved/, name);
+ }
+ assert.equal(triangleMesh({ point: { height: attribute } }).attributes.point.height.size, 1);
+});
+
+test('a cloth collider needs a known kind and a pin needs an existing particle', () => {
+ const positions = new Float32Array([0, 0, 0, 1, 0, 0, 0, 0, 1]);
+ const triangles = new Uint32Array([0, 1, 2]);
+ assert.throws(() => createCloth(positions, triangles, { colliders: [{ kind: 'constructor' }] }), /collider kind must be one of ball, capsule, box/);
+ assert.throws(() => createCloth(positions, triangles, { colliders: [{ kind: 'toString' }] }), /collider kind/);
+ assert.throws(() => createCloth(positions, triangles, { pinned: ['__proto__'] }), /pinned particle/);
+ assert.throws(() => createCloth(positions, triangles, { pinned: [3] }), /pinned particle 3 does not exist/);
+ assert.ok(createCloth(positions, triangles, { pinned: [0], colliders: [{ kind: 'ball', center: [0, -1, 0], radius: 0.5 }] }));
+});
+
+test('baking a pattern refuses a type that is not its own sampler', () => {
+ const pattern = normalizeMaterialPattern({ type: 'bricks' });
+ assert.throws(() => bakeMaterialPattern({ ...pattern, type: 'constructor' }), /type must be one of/);
+});
diff --git a/tests/kernel-offload.test.js b/tests/kernel-offload.test.js
index 7723b3ef..5bcf41fd 100644
--- a/tests/kernel-offload.test.js
+++ b/tests/kernel-offload.test.js
@@ -59,8 +59,8 @@ test('a worker that dies mid-job leaves the work to the inline path with the sam
test('an error a kernel raises on the worker comes back as the same class and message', async () => {
const raised = answerKernelRequest({ name: 'pattern', args: [{ type: 'nope' }] });
- assert.equal(raised.response.error.name, 'TypeError');
- assert.equal(typeof raised.response.error.message, 'string');
+ assert.equal(raised.response.error.name, 'RangeError');
+ assert.match(raised.response.error.message, /type must be one of/);
const raising = {
postMessage() {
setTimeout(() => raising.onmessage?.({ data: { error: { name: 'RangeError', message: 'Light bake passed maxSeconds (20).' } } }), 0);