Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): parseColor answers null for names the prototype chain supplies (#10468)

Aashish Mahato6718d7e8e10a
diff --git a/.changeset/parsecolor-answers-null-for-a-colour-name-that-t-zed8.md b/.changeset/parsecolor-answers-null-for-a-colour-name-that-t-zed8.md
new file mode 100644
index 0000000000..b290489c05
--- /dev/null
+++ b/.changeset/parsecolor-answers-null-for-a-colour-name-that-t-zed8.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `parseColor` answers `null` for `constructor` and `__proto__` (in any case), as for any unknown name, instead of a truthy empty RGBA that made composed callers such as chart `colors.alpha` return `rgba(NaN, NaN, NaN, …)` (or `#NANNANNAN` at full opacity) instead of the original string (#10468)
+@aashish254
diff --git a/packages/core/src/utils/color.test.ts b/packages/core/src/utils/color.test.ts
index 66bddb138e..0c7892c3ba 100644
--- a/packages/core/src/utils/color.test.ts
+++ b/packages/core/src/utils/color.test.ts
@@ -109,6 +109,20 @@ describe('parseColor', () => {
expect(parseColor('oklch(0.5 0.1 200)')).toBeNull();
expect(parseColor('rebeccapurple')).toBeNull();
});
+
+ it('returns null for names the prototype chain answers', () => {
+ expect(parseColor('constructor')).toBeNull();
+ expect(parseColor('__proto__')).toBeNull();
+ expect(parseColor('CONSTRUCTOR')).toBeNull();
+ });
+
+ it('keeps the composed callers null-safe for those names', () => {
+ // The charts/lab helpers compose `parseColor(color) ?? parseHex(color)`
+ // and hand the result to formatColor; a truthy non-RGBA answer reaches
+ // it as undefined channels.
+ const rgba = parseColor('constructor') ?? parseHex('constructor');
+ expect(rgba).toBeNull();
+ });
});
describe('formatHex', () => {
@@ -230,4 +244,9 @@ describe('alpha application (conformance for charts/lab `colors.alpha`)', () =>
it('unparseable input is preserved unchanged', () => {
expect(applyAlpha('var(--color-accent)', 0.2)).toBe('var(--color-accent)');
});
+
+ it('preserves names the prototype chain answers', () => {
+ expect(applyAlpha('constructor', 0.2)).toBe('constructor');
+ expect(applyAlpha('__proto__', 1)).toBe('__proto__');
+ });
});
diff --git a/packages/core/src/utils/color.ts b/packages/core/src/utils/color.ts
index fe286c330d..24676eeb53 100644
--- a/packages/core/src/utils/color.ts
+++ b/packages/core/src/utils/color.ts
@@ -115,9 +115,11 @@ export function parseRgb(value: string): RGBA | null {
*/
export function parseColor(value: string): RGBA | null {
const trimmed = value.trim();
- const named = NAMED_COLORS[trimmed.toLowerCase()];
- if (named) {
- return {...named};
+ // A plain record answers `constructor` and `__proto__` from
+ // Object.prototype, so the lookup must test own properties.
+ const name = trimmed.toLowerCase();
+ if (Object.hasOwn(NAMED_COLORS, name)) {
+ return {...NAMED_COLORS[name]};
}
if (trimmed.startsWith('#')) {
return parseHex(trimmed);