diff --git a/packages/core/src/material-library.test.ts b/packages/core/src/material-library.test.ts new file mode 100644 index 00000000..344cbbb1 --- /dev/null +++ b/packages/core/src/material-library.test.ts @@ -0,0 +1,13 @@ +import { describe, expect, test } from 'bun:test' +import { getLibraryMaterialIdFromRef, getSceneMaterialIdFromRef } from './material-library' + +describe('material references', () => { + test('rejects malformed runtime values instead of calling string methods', () => { + const malformedRefs: unknown[] = [42, true, {}, []] + + for (const ref of malformedRefs) { + expect(getLibraryMaterialIdFromRef(ref as string)).toBeNull() + expect(getSceneMaterialIdFromRef(ref as string)).toBeNull() + } + }) +}) diff --git a/packages/core/src/material-library.ts b/packages/core/src/material-library.ts index f887d36b..e45a4887 100644 --- a/packages/core/src/material-library.ts +++ b/packages/core/src/material-library.ts @@ -4170,13 +4170,14 @@ export function toSceneMaterialRef(id: string) { } export function getLibraryMaterialIdFromRef(materialRef?: string | null) { - if (!materialRef) return null + if (typeof materialRef !== 'string') return null if (!materialRef.startsWith(LIBRARY_MATERIAL_REF_PREFIX)) return null return materialRef.slice(LIBRARY_MATERIAL_REF_PREFIX.length) } export function getSceneMaterialIdFromRef(materialRef?: string | null): string | null { - if (!materialRef?.startsWith(SCENE_MATERIAL_REF_PREFIX)) return null + if (typeof materialRef !== 'string') return null + if (!materialRef.startsWith(SCENE_MATERIAL_REF_PREFIX)) return null return materialRef.slice(SCENE_MATERIAL_REF_PREFIX.length) }