Site builder: security & data-loss hardening + unified asset picker + audit backlog #3
@@ -30,3 +30,17 @@ describe('Menu.toHtml deterministic + unique scope ids (thread node id, no Math.
|
|||||||
expect(html1).toBe(html2);
|
expect(html1).toBe(html2);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Menu.toHtml XSS hardening (linkHoverColor into <style>)', () => {
|
||||||
|
test('a linkHoverColor value containing </style><script> is neutralized', () => {
|
||||||
|
const malicious = '#fff}</style><script>alert(1)</script><style>{';
|
||||||
|
const { html } = toHtml({ linkHoverColor: malicious }, '', 'node-xss');
|
||||||
|
expect(html).not.toContain('</style><script');
|
||||||
|
expect(html).not.toContain('<script>alert(1)</script>');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a normal linkHoverColor still renders in the hover rule', () => {
|
||||||
|
const { html } = toHtml({ linkHoverColor: '#ff0000' }, '', 'node-normal');
|
||||||
|
expect(html).toMatch(/:hover\s*\{\s*color:\s*#ff0000/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { CSSProperties, useState } from 'react';
|
import React, { CSSProperties, useState } from 'react';
|
||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { escapeHtml, escapeAttr, safeUrl, scopeId } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, safeUrl, scopeId, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
/* ---------- Types ---------- */
|
/* ---------- Types ---------- */
|
||||||
|
|
||||||
@@ -125,14 +125,18 @@ Menu.craft = {
|
|||||||
/* ---------- HTML export ---------- */
|
/* ---------- HTML export ---------- */
|
||||||
|
|
||||||
(Menu as any).toHtml = (props: MenuProps, _childrenHtml: string, nodeId?: string) => {
|
(Menu as any).toHtml = (props: MenuProps, _childrenHtml: string, nodeId?: string) => {
|
||||||
const linkCol = props.linkColor || '#3f3f46';
|
// Sanitized once here -- linkCol/hoverCol/ctaBg/ctaText/gap/fSize are raw
|
||||||
const hoverCol = props.linkHoverColor || '#3b82f6';
|
// string-interpolation sinks below (hoverCol goes into a <style> block,
|
||||||
const ctaBg = props.ctaBgColor || '#3b82f6';
|
// the worst case: </style> breakout -> arbitrary <script>), see
|
||||||
const ctaText = props.ctaTextColor || '#ffffff';
|
// task-cssxss-brief.md.
|
||||||
const gap = props.gap || '24px';
|
const linkCol = cssValue(props.linkColor) || '#3f3f46';
|
||||||
|
const hoverCol = cssValue(props.linkHoverColor) || '#3b82f6';
|
||||||
|
const ctaBg = cssValue(props.ctaBgColor) || '#3b82f6';
|
||||||
|
const ctaText = cssValue(props.ctaTextColor) || '#ffffff';
|
||||||
|
const gap = cssValue(props.gap) || '24px';
|
||||||
const orientation = props.orientation || 'horizontal';
|
const orientation = props.orientation || 'horizontal';
|
||||||
const alignment = props.alignment || 'right';
|
const alignment = props.alignment || 'right';
|
||||||
const fSize = props.fontSize || '14px';
|
const fSize = cssValue(props.fontSize) || '14px';
|
||||||
|
|
||||||
const justifyMap: Record<string, string> = { left: 'flex-start', center: 'center', right: 'flex-end' };
|
const justifyMap: Record<string, string> = { left: 'flex-start', center: 'center', right: 'flex-end' };
|
||||||
|
|
||||||
|
|||||||
@@ -26,3 +26,38 @@ describe('Navbar.toHtml hamburger accessibility (F2.3)', () => {
|
|||||||
expect(html).not.toContain('navbar-hamburger');
|
expect(html).not.toContain('navbar-hamburger');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Navbar.toHtml XSS hardening (hoverColor/backgroundColor/ctaColor into <style>)', () => {
|
||||||
|
test('a hoverColor value containing </style><script> is neutralized in the hover <style> block', () => {
|
||||||
|
const malicious = '#fff}</style><script>alert(1)</script><style>{';
|
||||||
|
const { html } = toHtml({ hoverColor: malicious }, '');
|
||||||
|
expect(html).not.toContain('</style><script');
|
||||||
|
expect(html).not.toContain('<script>alert(1)</script>');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a backgroundColor value containing </style><script> is neutralized (mobile media-query rule)', () => {
|
||||||
|
const malicious = '#fff}</style><script>alert(2)</script><style>{';
|
||||||
|
const { html } = toHtml({ backgroundColor: malicious, showMobileMenu: true }, '');
|
||||||
|
expect(html).not.toContain('</style><script');
|
||||||
|
expect(html).not.toContain('<script>alert(2)</script>');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a ctaColor value containing </style><script> is neutralized', () => {
|
||||||
|
const malicious = '#fff}</style><script>alert(3)</script><style>{';
|
||||||
|
const { html } = toHtml({ ctaColor: malicious }, '');
|
||||||
|
expect(html).not.toContain('</style><script');
|
||||||
|
expect(html).not.toContain('<script>alert(3)</script>');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a textColor value containing a quote breakout does not escape the hamburger span style attribute', () => {
|
||||||
|
const malicious = '#333" onmouseover="alert(1)';
|
||||||
|
const { html } = toHtml({ textColor: malicious, showMobileMenu: true }, '');
|
||||||
|
expect(html).not.toMatch(/style="[^"]*"[^>]*onmouseover/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('normal colors still render correctly', () => {
|
||||||
|
const { html } = toHtml({ hoverColor: '#ff0000', backgroundColor: '#123456', ctaColor: '#00ff00' }, '');
|
||||||
|
expect(html).toMatch(/:hover\s*\{\s*color:\s*#ff0000/);
|
||||||
|
expect(html).toContain('background-color:#123456');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React, { CSSProperties, useState } from 'react';
|
|||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { useSiteDesign } from '../../state/SiteDesignContext';
|
import { useSiteDesign } from '../../state/SiteDesignContext';
|
||||||
import { escapeHtml, escapeAttr, safeUrl } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, safeUrl, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
/* ---------- Types ---------- */
|
/* ---------- Types ---------- */
|
||||||
|
|
||||||
@@ -211,12 +211,15 @@ Navbar.craft = {
|
|||||||
/* ---------- HTML export ---------- */
|
/* ---------- HTML export ---------- */
|
||||||
|
|
||||||
(Navbar as any).toHtml = (props: NavbarProps, _childrenHtml: string) => {
|
(Navbar as any).toHtml = (props: NavbarProps, _childrenHtml: string) => {
|
||||||
const bgColor = props.backgroundColor || '#ffffff';
|
// Sanitized once here -- these are raw string-interpolation sinks below
|
||||||
const textCol = props.textColor || '#3f3f46';
|
// (hoverCol/bgColor go into a <style> block, the worst case: </style>
|
||||||
const hoverCol = props.hoverColor || '#3b82f6';
|
// breakout -> arbitrary <script>), see task-cssxss-brief.md.
|
||||||
const ctaCol = props.ctaColor || '#3b82f6';
|
const bgColor = cssValue(props.backgroundColor) || '#ffffff';
|
||||||
const ctaTextCol = props.ctaTextColor || '#ffffff';
|
const textCol = cssValue(props.textColor) || '#3f3f46';
|
||||||
const pad = props.padding || '16px 24px';
|
const hoverCol = cssValue(props.hoverColor) || '#3b82f6';
|
||||||
|
const ctaCol = cssValue(props.ctaColor) || '#3b82f6';
|
||||||
|
const ctaTextCol = cssValue(props.ctaTextColor) || '#ffffff';
|
||||||
|
const pad = cssValue(props.padding) || '16px 24px';
|
||||||
const alignment = props.navAlignment || 'space-between';
|
const alignment = props.navAlignment || 'space-between';
|
||||||
const sticky = props.isSticky;
|
const sticky = props.isSticky;
|
||||||
const mobile = props.showMobileMenu;
|
const mobile = props.showMobileMenu;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { CSSProperties } from 'react';
|
import React, { CSSProperties } from 'react';
|
||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { escapeAttr, safeUrl } from '../../utils/escape';
|
import { escapeAttr, safeUrl, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
interface SocialLink {
|
interface SocialLink {
|
||||||
platform: string;
|
platform: string;
|
||||||
@@ -163,9 +163,10 @@ SocialLinks.craft = {
|
|||||||
|
|
||||||
(SocialLinks as any).toHtml = (props: SocialLinksProps, _childrenHtml: string) => {
|
(SocialLinks as any).toHtml = (props: SocialLinksProps, _childrenHtml: string) => {
|
||||||
const links = props.links || defaultLinks;
|
const links = props.links || defaultLinks;
|
||||||
const iconSize = props.iconSize || '20px';
|
// Sanitized -- raw string-interpolation sinks in aStyle/getShapeStr below.
|
||||||
const iconColor = props.iconColor || '#ffffff';
|
const iconSize = cssValue(props.iconSize) || '20px';
|
||||||
const iconBgColor = props.iconBgColor || '#374151';
|
const iconColor = cssValue(props.iconColor) || '#ffffff';
|
||||||
|
const iconBgColor = cssValue(props.iconBgColor) || '#374151';
|
||||||
const iconShape = props.iconShape || 'circle';
|
const iconShape = props.iconShape || 'circle';
|
||||||
const gap = props.gap || '10px';
|
const gap = props.gap || '10px';
|
||||||
const alignment = props.alignment || 'center';
|
const alignment = props.alignment || 'center';
|
||||||
|
|||||||
@@ -21,3 +21,22 @@ describe('StarRating.toHtml accessibility (F2.2)', () => {
|
|||||||
expect(html).toContain('aria-label="Rating: 2 out of 10"');
|
expect(html).toContain('aria-label="Rating: 2 out of 10"');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('StarRating.toHtml XSS hardening (filledColor/emptyColor/size into style=)', () => {
|
||||||
|
test('a filledColor value containing a quote breakout is neutralized', () => {
|
||||||
|
const malicious = '#f00" onmouseover="alert(1)';
|
||||||
|
const { html } = toHtml({ rating: 3, maxStars: 5, filledColor: malicious }, '');
|
||||||
|
expect(html).not.toMatch(/style="[^"]*"[^>]*onmouseover/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a size value containing </style><script> is neutralized', () => {
|
||||||
|
const malicious = '24px</style><script>alert(1)</script>';
|
||||||
|
const { html } = toHtml({ rating: 3, maxStars: 5, size: malicious }, '');
|
||||||
|
expect(html).not.toContain('<script>alert(1)</script>');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a normal filled color still renders', () => {
|
||||||
|
const { html } = toHtml({ rating: 5, maxStars: 5, filledColor: '#ff9900' }, '');
|
||||||
|
expect(html).toContain('color:#ff9900');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import React, { CSSProperties } from 'react';
|
import React, { CSSProperties } from 'react';
|
||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
|
import { cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
interface StarRatingProps {
|
interface StarRatingProps {
|
||||||
rating?: number;
|
rating?: number;
|
||||||
@@ -99,9 +100,10 @@ StarRating.craft = {
|
|||||||
(StarRating as any).toHtml = (props: StarRatingProps, _childrenHtml: string) => {
|
(StarRating as any).toHtml = (props: StarRatingProps, _childrenHtml: string) => {
|
||||||
const rating = props.rating ?? 4.5;
|
const rating = props.rating ?? 4.5;
|
||||||
const maxStars = props.maxStars || 5;
|
const maxStars = props.maxStars || 5;
|
||||||
const size = props.size || '24px';
|
// Sanitized -- raw string-interpolation sinks in the star glyphs below.
|
||||||
const filledColor = props.filledColor || '#f59e0b';
|
const size = cssValue(props.size) || '24px';
|
||||||
const emptyColor = props.emptyColor || '#d1d5db';
|
const filledColor = cssValue(props.filledColor) || '#f59e0b';
|
||||||
|
const emptyColor = cssValue(props.emptyColor) || '#d1d5db';
|
||||||
const wrapperStyle = cssPropsToString({
|
const wrapperStyle = cssPropsToString({
|
||||||
display: 'inline-flex',
|
display: 'inline-flex',
|
||||||
alignItems: 'center',
|
alignItems: 'center',
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React, { CSSProperties } from 'react';
|
|||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { relayFormWiring } from '../../utils/form-relay-wiring';
|
import { relayFormWiring } from '../../utils/form-relay-wiring';
|
||||||
import { escapeHtml, escapeAttr, slugId } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, slugId, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
interface ContactFormField {
|
interface ContactFormField {
|
||||||
type: 'text' | 'email' | 'tel' | 'textarea' | 'select';
|
type: 'text' | 'email' | 'tel' | 'textarea' | 'select';
|
||||||
@@ -175,9 +175,11 @@ ContactForm.craft = {
|
|||||||
gap: '20px',
|
gap: '20px',
|
||||||
...props.style,
|
...props.style,
|
||||||
});
|
});
|
||||||
const labelColor = props.labelColor || '#374151';
|
// Sanitized -- raw string-interpolation sinks in inputStyleStr/labelHtml
|
||||||
const inputBg = props.inputBg || '#ffffff';
|
// below.
|
||||||
const inputBorder = props.inputBorder || '#d1d5db';
|
const labelColor = cssValue(props.labelColor) || '#374151';
|
||||||
|
const inputBg = cssValue(props.inputBg) || '#ffffff';
|
||||||
|
const inputBorder = cssValue(props.inputBorder) || '#d1d5db';
|
||||||
const inputStyleStr = `width:100%;padding:10px 14px;font-size:14px;font-family:Inter,sans-serif;border:1px solid ${inputBorder};border-radius:6px;background-color:${inputBg};color:#1f2937;box-sizing:border-box;outline:none`;
|
const inputStyleStr = `width:100%;padding:10px 14px;font-size:14px;font-family:Inter,sans-serif;border:1px solid ${inputBorder};border-radius:6px;background-color:${inputBg};color:#1f2937;box-sizing:border-box;outline:none`;
|
||||||
|
|
||||||
const fieldsHtml = (props.fields || defaultFields).map((field, i) => {
|
const fieldsHtml = (props.fields || defaultFields).map((field, i) => {
|
||||||
|
|||||||
@@ -59,3 +59,24 @@ describe('ColumnLayout.toHtml deterministic + unique scope ids (thread node id,
|
|||||||
expect(html1).toBe(html2);
|
expect(html1).toBe(html2);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('ColumnLayout.toHtml XSS hardening (gap into <style>)', () => {
|
||||||
|
test('a gap value containing </style><script> is neutralized in the <style>-context nth-child rule', () => {
|
||||||
|
const malicious = '0px)}</style><script>alert(1)</script><style>{';
|
||||||
|
const { html } = toHtml({ columns: 2, split: '50-50', gap: malicious }, '<div>A</div><div>B</div>', 'node-xss');
|
||||||
|
expect(html).not.toContain('</style><script');
|
||||||
|
expect(html).not.toContain('<script>alert(1)</script>');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a gap value containing a quote/semicolon breakout is neutralized in the style attribute', () => {
|
||||||
|
const malicious = '16px" onmouseover="alert(1)';
|
||||||
|
const { html } = toHtml({ columns: 2, split: '50-50', gap: malicious }, '', 'node-xss2');
|
||||||
|
expect(html).not.toMatch(/style="[^"]*"[^>]*onmouseover/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a normal gap value still renders correctly', () => {
|
||||||
|
const { html } = toHtml({ columns: 2, split: '50-50', gap: '24px' }, '<div>A</div>', 'node-normal');
|
||||||
|
expect(html).toContain('gap:24px');
|
||||||
|
expect(html).toMatch(/calc\(50% - 24px\)/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React, { CSSProperties } from 'react';
|
|||||||
import { useNode, Element, UserComponent } from '@craftjs/core';
|
import { useNode, Element, UserComponent } from '@craftjs/core';
|
||||||
import { Container } from './Container';
|
import { Container } from './Container';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { escapeAttr, scopeId } from '../../utils/escape';
|
import { escapeAttr, scopeId, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
type SplitOption =
|
type SplitOption =
|
||||||
| '100'
|
| '100'
|
||||||
@@ -117,7 +117,11 @@ ColumnLayout.craft = {
|
|||||||
(ColumnLayout as any).toHtml = (props: ColumnLayoutProps, childrenHtml: string, nodeId?: string) => {
|
(ColumnLayout as any).toHtml = (props: ColumnLayoutProps, childrenHtml: string, nodeId?: string) => {
|
||||||
const columns = props.columns || 2;
|
const columns = props.columns || 2;
|
||||||
const split = props.split || '50-50';
|
const split = props.split || '50-50';
|
||||||
const gap = props.gap || '16px';
|
// Sanitized once here so BOTH the raw <style> nth-child rule below AND the
|
||||||
|
// cssPropsToString-built outerStyle get a safe value -- gap is a raw
|
||||||
|
// string-interpolation sink into a <style> block (worst case: </style>
|
||||||
|
// breakout -> arbitrary <script>), see task-cssxss-brief.md.
|
||||||
|
const gap = cssValue(props.gap) || '16px';
|
||||||
const widths = getWidths(split, columns);
|
const widths = getWidths(split, columns);
|
||||||
|
|
||||||
const outerStyle = cssPropsToString({
|
const outerStyle = cssPropsToString({
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React, { CSSProperties } from 'react';
|
|||||||
import { useNode, Element, UserComponent } from '@craftjs/core';
|
import { useNode, Element, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { Container } from './Container';
|
import { Container } from './Container';
|
||||||
import { escapeAttr } from '../../utils/escape';
|
import { escapeAttr, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
/* ---------- Shape Divider SVG Paths ---------- */
|
/* ---------- Shape Divider SVG Paths ---------- */
|
||||||
|
|
||||||
@@ -167,7 +167,8 @@ function buildDividerHtml(
|
|||||||
|
|
||||||
const isTop = position === 'top';
|
const isTop = position === 'top';
|
||||||
const h = height || '50px';
|
const h = height || '50px';
|
||||||
const c = color || '#ffffff';
|
// Sanitized -- raw string-interpolation sink in the SVG `fill:${c}` below.
|
||||||
|
const c = cssValue(color) || '#ffffff';
|
||||||
|
|
||||||
const wrapperStyle = cssPropsToString({
|
const wrapperStyle = cssPropsToString({
|
||||||
position: 'absolute',
|
position: 'absolute',
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { CSSProperties, useState } from 'react';
|
import React, { CSSProperties, useState } from 'react';
|
||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { escapeHtml, escapeAttr } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
interface AccordionItem {
|
interface AccordionItem {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -151,10 +151,12 @@ Accordion.craft = {
|
|||||||
...props.style,
|
...props.style,
|
||||||
});
|
});
|
||||||
const idAttr = props.anchorId ? ` id="${escapeAttr(props.anchorId)}"` : '';
|
const idAttr = props.anchorId ? ` id="${escapeAttr(props.anchorId)}"` : '';
|
||||||
const headerBg = props.headerBg || '#f8fafc';
|
// Sanitized -- raw string-interpolation sinks in the <details>/<summary>
|
||||||
const headerColor = props.headerColor || '#18181b';
|
// style attributes below.
|
||||||
const contentBg = props.contentBg || '#ffffff';
|
const headerBg = cssValue(props.headerBg) || '#f8fafc';
|
||||||
const borderColor = props.borderColor || '#e2e8f0';
|
const headerColor = cssValue(props.headerColor) || '#18181b';
|
||||||
|
const contentBg = cssValue(props.contentBg) || '#ffffff';
|
||||||
|
const borderColor = cssValue(props.borderColor) || '#e2e8f0';
|
||||||
const items = props.items || defaultItems;
|
const items = props.items || defaultItems;
|
||||||
|
|
||||||
const panels = items.map((item, i) => {
|
const panels = items.map((item, i) => {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React, { CSSProperties } from 'react';
|
|||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { CtaButton, normalizeCtas, ctaInlineStyle, ctasToHtml } from './_cta-helpers';
|
import { CtaButton, normalizeCtas, ctaInlineStyle, ctasToHtml } from './_cta-helpers';
|
||||||
import { escapeHtml, escapeAttr } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
interface CallToActionProps {
|
interface CallToActionProps {
|
||||||
heading?: string;
|
heading?: string;
|
||||||
@@ -144,7 +144,9 @@ CallToAction.craft = {
|
|||||||
(CallToAction as any).toHtml = (props: CallToActionProps, _childrenHtml: string) => {
|
(CallToAction as any).toHtml = (props: CallToActionProps, _childrenHtml: string) => {
|
||||||
const bgType = props.bgType || 'gradient';
|
const bgType = props.bgType || 'gradient';
|
||||||
const bgValue = props.bgValue || defaultGradient;
|
const bgValue = props.bgValue || defaultGradient;
|
||||||
const textColor = props.textColor || '#ffffff';
|
// Sanitized -- raw string-interpolation sink in the heading/description
|
||||||
|
// style attributes below.
|
||||||
|
const textColor = cssValue(props.textColor) || '#ffffff';
|
||||||
const buttonColor = props.buttonColor || '#ffffff';
|
const buttonColor = props.buttonColor || '#ffffff';
|
||||||
const isButtonDark = buttonColor === '#ffffff' || buttonColor === '#f8fafc';
|
const isButtonDark = buttonColor === '#ffffff' || buttonColor === '#f8fafc';
|
||||||
const buttonTextColor = isButtonDark ? '#18181b' : '#ffffff';
|
const buttonTextColor = isButtonDark ? '#18181b' : '#ffffff';
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { CSSProperties, useState, useEffect, useRef, useCallback } from 'react';
|
import React, { CSSProperties, useState, useEffect, useRef, useCallback } from 'react';
|
||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { escapeHtml, escapeAttr, safeUrl, scopeId } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, safeUrl, scopeId, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
interface Slide {
|
interface Slide {
|
||||||
type: 'image' | 'content';
|
type: 'image' | 'content';
|
||||||
@@ -266,11 +266,14 @@ ContentSlider.craft = {
|
|||||||
|
|
||||||
const slidesHtml = items.map((slide, i) => {
|
const slidesHtml = items.map((slide, i) => {
|
||||||
const hasBgImage = slide.imageSrc;
|
const hasBgImage = slide.imageSrc;
|
||||||
|
// Sanitized -- slide.bgColor is a per-slide raw string-interpolation
|
||||||
|
// sink (a malicious value could break out of the style="..." attribute).
|
||||||
|
const safeBgColor = cssValue(slide.bgColor) || '#3b82f6';
|
||||||
const bgStyle = hasBgImage
|
const bgStyle = hasBgImage
|
||||||
? `background-image:url('${escapeAttr(safeUrl(slide.imageSrc!))}');background-size:cover;background-position:center`
|
? `background-image:url('${escapeAttr(safeUrl(slide.imageSrc!))}');background-size:cover;background-position:center`
|
||||||
: slide.bgColor?.startsWith('linear-gradient')
|
: slide.bgColor?.startsWith('linear-gradient')
|
||||||
? `background-image:${slide.bgColor}`
|
? `background-image:${safeBgColor}`
|
||||||
: `background-color:${slide.bgColor || '#3b82f6'}`;
|
: `background-color:${safeBgColor}`;
|
||||||
|
|
||||||
const contentParts: string[] = [];
|
const contentParts: string[] = [];
|
||||||
if (slide.heading) {
|
if (slide.heading) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { CSSProperties, useEffect, useState } from 'react';
|
import React, { CSSProperties, useEffect, useState } from 'react';
|
||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { escapeHtml, escapeAttr, scopeId } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, scopeId, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
interface CountdownProps {
|
interface CountdownProps {
|
||||||
targetDate?: string;
|
targetDate?: string;
|
||||||
@@ -154,10 +154,12 @@ Countdown.craft = {
|
|||||||
targetDate = DEFAULT_TARGET,
|
targetDate = DEFAULT_TARGET,
|
||||||
heading = 'Coming Soon',
|
heading = 'Coming Soon',
|
||||||
style = {},
|
style = {},
|
||||||
digitColor = '#ffffff',
|
|
||||||
labelColor = 'rgba(255,255,255,0.7)',
|
|
||||||
bgColor = '#18181b',
|
bgColor = '#18181b',
|
||||||
} = props;
|
} = props;
|
||||||
|
// Sanitized -- raw string-interpolation sinks in the heading/digit/label
|
||||||
|
// style attributes below.
|
||||||
|
const digitColor = cssValue(props.digitColor) || '#ffffff';
|
||||||
|
const labelColor = cssValue(props.labelColor) || 'rgba(255,255,255,0.7)';
|
||||||
|
|
||||||
const sectionStyle = cssPropsToString({
|
const sectionStyle = cssPropsToString({
|
||||||
padding: '60px 20px',
|
padding: '60px 20px',
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { CSSProperties } from 'react';
|
import React, { CSSProperties } from 'react';
|
||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { escapeHtml, escapeAttr, safeUrl, scopeId } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, safeUrl, scopeId, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
interface GalleryImage {
|
interface GalleryImage {
|
||||||
src: string;
|
src: string;
|
||||||
@@ -130,8 +130,14 @@ Gallery.craft = {
|
|||||||
...props.style,
|
...props.style,
|
||||||
});
|
});
|
||||||
const images = props.images || defaultImages;
|
const images = props.images || defaultImages;
|
||||||
const columns = props.columns || 3;
|
// Number() coercion: `columns` is a raw string-interpolation sink into the
|
||||||
const gap = props.gap || '16px';
|
// grid style attribute below (repeat(${columns},1fr)) -- a non-numeric
|
||||||
|
// (e.g. hand-crafted/AI-generated tree) value would otherwise be able to
|
||||||
|
// break out; Number() of anything non-numeric collapses safely to NaN.
|
||||||
|
const columns = Number(props.columns) || 3;
|
||||||
|
// Sanitized -- gap is a raw string-interpolation sink into the grid style
|
||||||
|
// attribute below.
|
||||||
|
const gap = cssValue(props.gap) || '16px';
|
||||||
const lightbox = props.lightbox || false;
|
const lightbox = props.lightbox || false;
|
||||||
|
|
||||||
// Deterministic AND unique id, scoped on the Craft node id, for this
|
// Deterministic AND unique id, scoped on the Craft node id, for this
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React, { CSSProperties } from 'react';
|
|||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { CtaButton, normalizeCtas, ctaInlineStyle, ctasToHtml } from './_cta-helpers';
|
import { CtaButton, normalizeCtas, ctaInlineStyle, ctasToHtml } from './_cta-helpers';
|
||||||
import { escapeHtml, escapeAttr, safeUrl } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, safeUrl, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
interface HeroProps {
|
interface HeroProps {
|
||||||
heading?: string;
|
heading?: string;
|
||||||
@@ -220,7 +220,8 @@ HeroSimple.craft = {
|
|||||||
|
|
||||||
let overlayHtml = '';
|
let overlayHtml = '';
|
||||||
if ((props.overlayOpacity || 0) > 0) {
|
if ((props.overlayOpacity || 0) > 0) {
|
||||||
overlayHtml = `<div style="position:absolute;top:0;left:0;right:0;bottom:0;background-color:${props.overlayColor || '#000'};opacity:${(props.overlayOpacity || 0) / 100};z-index:1"></div>`;
|
const overlayColor = cssValue(props.overlayColor) || '#000';
|
||||||
|
overlayHtml = `<div style="position:absolute;top:0;left:0;right:0;bottom:0;background-color:${overlayColor};opacity:${(props.overlayOpacity || 0) / 100};z-index:1"></div>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
let videoHtml = '';
|
let videoHtml = '';
|
||||||
@@ -239,12 +240,13 @@ HeroSimple.craft = {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const idAttr = props.anchorId ? ` id="${escapeAttr(props.anchorId)}"` : '';
|
const idAttr = props.anchorId ? ` id="${escapeAttr(props.anchorId)}"` : '';
|
||||||
|
const heroTextColor = cssValue(props.textColor) || '#fff';
|
||||||
return {
|
return {
|
||||||
html: `<section${idAttr} style="${sectionStyle}">
|
html: `<section${idAttr} style="${sectionStyle}">
|
||||||
${videoHtml}${overlayHtml}
|
${videoHtml}${overlayHtml}
|
||||||
<div style="max-width:800px;width:100%;position:relative;z-index:2;text-align:${textAlign}">
|
<div style="max-width:800px;width:100%;position:relative;z-index:2;text-align:${textAlign}">
|
||||||
<h1 style="font-size:48px;font-weight:700;color:${props.textColor || '#fff'};margin-bottom:16px;line-height:1.2">${escapeHtml(props.heading || '')}</h1>
|
<h1 style="font-size:48px;font-weight:700;color:${heroTextColor};margin-bottom:16px;line-height:1.2">${escapeHtml(props.heading || '')}</h1>
|
||||||
<p style="font-size:20px;color:${props.textColor || '#fff'};opacity:0.85;margin-bottom:32px;line-height:1.6;white-space:pre-line">${escapeHtml(props.subtitle || '')}</p>
|
<p style="font-size:20px;color:${heroTextColor};opacity:0.85;margin-bottom:32px;line-height:1.6;white-space:pre-line">${escapeHtml(props.subtitle || '')}</p>
|
||||||
<div style="display:flex;gap:12px;justify-content:${justifyBtn};flex-wrap:wrap">${buttonsHtml}</div>
|
<div style="display:flex;gap:12px;justify-content:${justifyBtn};flex-wrap:wrap">${buttonsHtml}</div>
|
||||||
</div>
|
</div>
|
||||||
</section>`,
|
</section>`,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { CSSProperties } from 'react';
|
import React, { CSSProperties } from 'react';
|
||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { escapeHtml, escapeAttr, scopeId } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, scopeId, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
interface Counter {
|
interface Counter {
|
||||||
number: number;
|
number: number;
|
||||||
@@ -112,12 +112,17 @@ NumberCounter.craft = {
|
|||||||
(NumberCounter as any).toHtml = (props: NumberCounterProps, _childrenHtml: string, nodeId?: string) => {
|
(NumberCounter as any).toHtml = (props: NumberCounterProps, _childrenHtml: string, nodeId?: string) => {
|
||||||
const {
|
const {
|
||||||
counters = defaultCounters,
|
counters = defaultCounters,
|
||||||
columns = 4,
|
|
||||||
numberColor = '#3b82f6',
|
|
||||||
labelColor = '#6b7280',
|
|
||||||
numberSize = '48px',
|
|
||||||
style = {},
|
style = {},
|
||||||
} = props;
|
} = props;
|
||||||
|
// Number() coercion: `columns` is a raw string-interpolation sink into the
|
||||||
|
// grid style attribute below (repeat(${columns},1fr)); Number() of
|
||||||
|
// anything non-numeric collapses safely to NaN instead of breaking out.
|
||||||
|
const columns = Number(props.columns) || 4;
|
||||||
|
// Sanitized -- raw string-interpolation sinks in the counter/label spans
|
||||||
|
// below.
|
||||||
|
const numberColor = cssValue(props.numberColor) || '#3b82f6';
|
||||||
|
const labelColor = cssValue(props.labelColor) || '#6b7280';
|
||||||
|
const numberSize = cssValue(props.numberSize) || '48px';
|
||||||
|
|
||||||
const items = counters.length > 0 ? counters : defaultCounters;
|
const items = counters.length > 0 ? counters : defaultCounters;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { CSSProperties } from 'react';
|
import React, { CSSProperties } from 'react';
|
||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { escapeHtml, escapeAttr, safeUrl } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, safeUrl, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
interface PricingPlan {
|
interface PricingPlan {
|
||||||
name: string;
|
name: string;
|
||||||
@@ -225,7 +225,9 @@ PricingTable.craft = {
|
|||||||
});
|
});
|
||||||
const idAttr = props.anchorId ? ` id="${escapeAttr(props.anchorId)}"` : '';
|
const idAttr = props.anchorId ? ` id="${escapeAttr(props.anchorId)}"` : '';
|
||||||
const plans = props.plans || defaultPlans;
|
const plans = props.plans || defaultPlans;
|
||||||
const featuredBg = props.featuredBg || '#3b82f6';
|
// Sanitized -- featuredBg is a raw string-interpolation sink below (drives
|
||||||
|
// cardBg/btnBg/btnColor, all raw-interpolated into style="...").
|
||||||
|
const featuredBg = cssValue(props.featuredBg) || '#3b82f6';
|
||||||
|
|
||||||
const cards = plans.map((plan) => {
|
const cards = plans.map((plan) => {
|
||||||
const cardBg = plan.isFeatured ? featuredBg : '#ffffff';
|
const cardBg = plan.isFeatured ? featuredBg : '#ffffff';
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { CSSProperties, useState } from 'react';
|
import React, { CSSProperties, useState } from 'react';
|
||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { escapeHtml, escapeAttr, scopeId } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, scopeId, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
interface TabItem {
|
interface TabItem {
|
||||||
label: string;
|
label: string;
|
||||||
@@ -131,11 +131,15 @@ Tabs.craft = {
|
|||||||
});
|
});
|
||||||
const idAttr = props.anchorId ? ` id="${escapeAttr(props.anchorId)}"` : '';
|
const idAttr = props.anchorId ? ` id="${escapeAttr(props.anchorId)}"` : '';
|
||||||
const tabs = props.tabs || defaultTabs;
|
const tabs = props.tabs || defaultTabs;
|
||||||
const activeTabBg = props.activeTabBg || '#3b82f6';
|
// Sanitized -- raw string-interpolation sinks below, both into style="..."
|
||||||
const activeTabColor = props.activeTabColor || '#ffffff';
|
// attributes AND into an inline <script> as single-quoted JS string
|
||||||
const inactiveTabBg = props.inactiveTabBg || '#f1f5f9';
|
// literals (a stray `'` there breaks out of the JS string, not just CSS);
|
||||||
const inactiveTabColor = props.inactiveTabColor || '#64748b';
|
// cssValue strips quotes too so it neutralizes both contexts at once.
|
||||||
const contentBg = props.contentBg || '#ffffff';
|
const activeTabBg = cssValue(props.activeTabBg) || '#3b82f6';
|
||||||
|
const activeTabColor = cssValue(props.activeTabColor) || '#ffffff';
|
||||||
|
const inactiveTabBg = cssValue(props.inactiveTabBg) || '#f1f5f9';
|
||||||
|
const inactiveTabColor = cssValue(props.inactiveTabColor) || '#64748b';
|
||||||
|
const contentBg = cssValue(props.contentBg) || '#ffffff';
|
||||||
|
|
||||||
// tabId scopes the functional wiring (onclick/getElementById) as well as
|
// tabId scopes the functional wiring (onclick/getElementById) as well as
|
||||||
// the ARIA tab<->panel linking ids. It must be BOTH deterministic (so
|
// the ARIA tab<->panel linking ids. It must be BOTH deterministic (so
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { CSSProperties } from 'react';
|
import React, { CSSProperties } from 'react';
|
||||||
import { useNode, UserComponent } from '@craftjs/core';
|
import { useNode, UserComponent } from '@craftjs/core';
|
||||||
import { cssPropsToString } from '../../utils/style-helpers';
|
import { cssPropsToString } from '../../utils/style-helpers';
|
||||||
import { escapeHtml, escapeAttr } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
interface Testimonial {
|
interface Testimonial {
|
||||||
quote: string;
|
quote: string;
|
||||||
@@ -139,11 +139,16 @@ Testimonials.craft = {
|
|||||||
const {
|
const {
|
||||||
testimonials = defaultTestimonials,
|
testimonials = defaultTestimonials,
|
||||||
layout = 'grid',
|
layout = 'grid',
|
||||||
columns = 3,
|
|
||||||
style = {},
|
style = {},
|
||||||
cardBg = '#f8fafc',
|
|
||||||
starColor = '#f59e0b',
|
|
||||||
} = props;
|
} = props;
|
||||||
|
// Number() coercion: `columns` is a raw string-interpolation sink into the
|
||||||
|
// grid style attribute below (repeat(${columns},1fr)); Number() of
|
||||||
|
// anything non-numeric collapses safely to NaN instead of breaking out.
|
||||||
|
const columns = Number(props.columns) || 3;
|
||||||
|
// Sanitized -- raw string-interpolation sinks below (cardCss / starsHtml
|
||||||
|
// style attributes).
|
||||||
|
const cardBg = cssValue(props.cardBg) || '#f8fafc';
|
||||||
|
const starColor = cssValue(props.starColor) || '#f59e0b';
|
||||||
|
|
||||||
const items = testimonials.length > 0 ? testimonials : defaultTestimonials;
|
const items = testimonials.length > 0 ? testimonials : defaultTestimonials;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { CSSProperties } from 'react';
|
import { CSSProperties } from 'react';
|
||||||
import { escapeHtml, escapeAttr, safeUrl } from '../../utils/escape';
|
import { escapeHtml, escapeAttr, safeUrl, cssValue } from '../../utils/escape';
|
||||||
|
|
||||||
export type CtaVariant = 'primary' | 'outline' | 'ghost';
|
export type CtaVariant = 'primary' | 'outline' | 'ghost';
|
||||||
|
|
||||||
@@ -68,14 +68,21 @@ export function ctaInlineStyle(cta: CtaButton, defaults: CtaStyleDefaults): CSSP
|
|||||||
|
|
||||||
export function ctaCssString(cta: CtaButton, defaults: CtaStyleDefaults): string {
|
export function ctaCssString(cta: CtaButton, defaults: CtaStyleDefaults): string {
|
||||||
const variant = cta.variant || 'primary';
|
const variant = cta.variant || 'primary';
|
||||||
|
// Sanitized -- these are raw string-interpolation sinks into style="...".
|
||||||
|
// Callers pass user-controlled design-token colors (e.g. HeroSimple's
|
||||||
|
// buttonBgColor/buttonTextColor/textColor) through CtaStyleDefaults, so
|
||||||
|
// sanitize once here rather than at every call site.
|
||||||
|
const outlineText = cssValue(defaults.outlineText) || '#000000';
|
||||||
|
const primaryBg = cssValue(defaults.primaryBg) || '#000000';
|
||||||
|
const primaryText = cssValue(defaults.primaryText) || '#ffffff';
|
||||||
switch (variant) {
|
switch (variant) {
|
||||||
case 'outline':
|
case 'outline':
|
||||||
return `display:inline-block;padding:14px 36px;background-color:transparent;color:${defaults.outlineText};text-decoration:none;border-radius:8px;font-weight:600;font-size:16px;border:2px solid ${defaults.outlineText}`;
|
return `display:inline-block;padding:14px 36px;background-color:transparent;color:${outlineText};text-decoration:none;border-radius:8px;font-weight:600;font-size:16px;border:2px solid ${outlineText}`;
|
||||||
case 'ghost':
|
case 'ghost':
|
||||||
return `display:inline-block;padding:14px 24px;background-color:transparent;color:${defaults.outlineText};text-decoration:underline;border-radius:8px;font-weight:600;font-size:16px`;
|
return `display:inline-block;padding:14px 24px;background-color:transparent;color:${outlineText};text-decoration:underline;border-radius:8px;font-weight:600;font-size:16px`;
|
||||||
case 'primary':
|
case 'primary':
|
||||||
default:
|
default:
|
||||||
return `display:inline-block;padding:14px 36px;background-color:${defaults.primaryBg};color:${defaults.primaryText};text-decoration:none;border-radius:8px;font-weight:600;font-size:16px`;
|
return `display:inline-block;padding:14px 36px;background-color:${primaryBg};color:${primaryText};text-decoration:none;border-radius:8px;font-weight:600;font-size:16px`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
import { describe, test, expect } from 'vitest';
|
||||||
|
import { cssValue } from './escape';
|
||||||
|
|
||||||
|
describe('cssValue', () => {
|
||||||
|
test('neutralizes </style> breakout', () => {
|
||||||
|
expect(cssValue('0px)}</style><script>alert(1)</script><style>{')).not.toContain('</style');
|
||||||
|
expect(cssValue('0px)}</style><script>alert(1)</script><style>{')).not.toContain('<script');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('strips double quote (attribute breakout)', () => {
|
||||||
|
expect(cssValue('red" onmouseover="alert(1)')).not.toContain('"');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('strips single quote', () => {
|
||||||
|
expect(cssValue("red' onmouseover='alert(1)")).not.toContain("'");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('strips semicolon (declaration injection)', () => {
|
||||||
|
expect(cssValue('red;background:url(javascript:alert(1))')).not.toContain(';');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('strips curly braces (rule injection)', () => {
|
||||||
|
expect(cssValue('red}body{background:red')).not.toContain('{');
|
||||||
|
expect(cssValue('red}body{background:red')).not.toContain('}');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('strips angle brackets', () => {
|
||||||
|
expect(cssValue('<b>red</b>')).not.toContain('<');
|
||||||
|
expect(cssValue('<b>red</b>')).not.toContain('>');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('strips backslash', () => {
|
||||||
|
expect(cssValue('red\\3c script')).not.toContain('\\');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('neutralizes url( to block javascript: via background shorthand', () => {
|
||||||
|
expect(cssValue("red;background:url(javascript:alert(1))")).not.toMatch(/url\s*\(/i);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('non-string input returns empty string', () => {
|
||||||
|
expect(cssValue(undefined)).toBe('');
|
||||||
|
expect(cssValue(null)).toBe('');
|
||||||
|
expect(cssValue(123 as unknown)).toBe('');
|
||||||
|
expect(cssValue({} as unknown)).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('preserves hex colors', () => {
|
||||||
|
expect(cssValue('#fff')).toBe('#fff');
|
||||||
|
expect(cssValue('#3b82f6')).toBe('#3b82f6');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('preserves rgba()', () => {
|
||||||
|
expect(cssValue('rgba(0,0,0,.5)')).toBe('rgba(0,0,0,.5)');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('preserves linear-gradient()', () => {
|
||||||
|
expect(cssValue('linear-gradient(90deg,#aaa 0%,#bbb 100%)')).toBe('linear-gradient(90deg,#aaa 0%,#bbb 100%)');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('preserves calc()', () => {
|
||||||
|
expect(cssValue('calc(50% - 8px)')).toBe('calc(50% - 8px)');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('preserves var()', () => {
|
||||||
|
expect(cssValue('var(--x)')).toBe('var(--x)');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('preserves border shorthand', () => {
|
||||||
|
expect(cssValue('1px solid #ccc')).toBe('1px solid #ccc');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('preserves px/%/em/rem sizes', () => {
|
||||||
|
expect(cssValue('24px')).toBe('24px');
|
||||||
|
expect(cssValue('50%')).toBe('50%');
|
||||||
|
expect(cssValue('1.5em')).toBe('1.5em');
|
||||||
|
expect(cssValue('2rem')).toBe('2rem');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -79,6 +79,30 @@ export function safeUrl(s: string): string {
|
|||||||
return trimmed;
|
return trimmed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Neutralizes CSS-context breakout for a single design-token value (color,
|
||||||
|
* length, gradient, etc.) so it is safe to interpolate RAW into either CSS
|
||||||
|
* context found in exported HTML:
|
||||||
|
* - ATTRIBUTE context: `style="color:${v}"` -- a `"` or `'` closes the
|
||||||
|
* attribute early; a `;` injects an extra declaration.
|
||||||
|
* - ELEMENT context: `<style>.x{color:${v}}</style>` -- `<`, `>`, `{`, `}`
|
||||||
|
* let a value close the rule / the `<style>` element itself and open a
|
||||||
|
* `<script>`, which is the worst case (arbitrary script execution).
|
||||||
|
* Strips the characters that can terminate a value, declaration, rule, or
|
||||||
|
* the `<style>` element itself, and neutralizes `url(` so a background
|
||||||
|
* shorthand can't smuggle a `javascript:` URL. None of hex colors,
|
||||||
|
* `rgb()`/`rgba()`, `var(...)`, gradients, `calc(...)`, or `px`/`%`/`em`/
|
||||||
|
* `rem` sizes contain any stripped character, so legitimate values pass
|
||||||
|
* through unchanged. If a sink legitimately needs a background image URL,
|
||||||
|
* route that value through `safeUrl` instead of `cssValue`.
|
||||||
|
*/
|
||||||
|
export function cssValue(v: unknown): string {
|
||||||
|
if (typeof v !== 'string') return '';
|
||||||
|
return v
|
||||||
|
.replace(/[<>{};"'\\]/g, '')
|
||||||
|
.replace(/url\s*\(/gi, '');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Derives a deterministic, HTML-id-safe slug from a field's `name`/`label`
|
* Derives a deterministic, HTML-id-safe slug from a field's `name`/`label`
|
||||||
* for wiring `<label for>` to a matching `<input id>` in exported markup.
|
* for wiring `<label for>` to a matching `<input id>` in exported markup.
|
||||||
|
|||||||
Reference in New Issue
Block a user