vadivam-react 0.0.42 → 0.0.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,7 +8,7 @@ Tree-shakeable 24px outline SVG icons for React.
8
8
  [![downloads](https://img.shields.io/npm/dw/vadivam-react?style=flat-square&color=666)](https://www.npmjs.com/package/vadivam-react)
9
9
  [![license](https://img.shields.io/npm/l/vadivam-react?style=flat-square&color=111)](https://github.com/praveenjuge/vadivam/blob/main/LICENSE)
10
10
 
11
- ![All Vadivam icons](https://raw.githubusercontent.com/praveenjuge/vadivam/main/apps/docs/public/preview.png?v=0.0.42)
11
+ ![All Vadivam icons](https://raw.githubusercontent.com/praveenjuge/vadivam/main/apps/docs/public/preview.png?v=0.0.46)
12
12
 
13
13
  ```sh
14
14
  npm install vadivam-react
package/dist/Icon.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use client";
2
2
  import React, { forwardRef } from "react";
3
3
  import defaultAttributes from "./defaultAttributes.js";
4
+ import { sanitizeIconNode, sanitizePaint, sanitizeRootAttributes } from "./runtimePolicy.js";
4
5
  import { useVadivamContext } from "./context.js";
5
6
 
6
7
  function mergeClasses(...values) {
@@ -13,7 +14,7 @@ function renderNode([tag, attrs]) {
13
14
  return React.createElement(tag, attrs);
14
15
  }
15
16
 
16
- export const Icon = forwardRef(({ color, size, strokeWidth, absoluteStrokeWidth, className = "", title, children, iconNode, ...rest }, ref) => {
17
+ export const Icon = forwardRef(({ color, size, strokeWidth, absoluteStrokeWidth, className = "", title, children, iconNode = [], ...rest }, ref) => {
17
18
  const context = useVadivamContext();
18
19
  const resolvedSize = size ?? context.size ?? 24;
19
20
  const resolvedStrokeWidth = strokeWidth ?? context.strokeWidth ?? 2;
@@ -21,7 +22,8 @@ export const Icon = forwardRef(({ color, size, strokeWidth, absoluteStrokeWidth,
21
22
  const numericStrokeWidth = Number(resolvedStrokeWidth);
22
23
  const useAbsoluteStrokeWidth = absoluteStrokeWidth ?? context.absoluteStrokeWidth ?? false;
23
24
  const calculatedStrokeWidth = useAbsoluteStrokeWidth && Number.isFinite(numericSize) && numericSize !== 0 && Number.isFinite(numericStrokeWidth) ? numericStrokeWidth * 24 / numericSize : resolvedStrokeWidth;
24
- const labelled = Boolean(title) || hasA11yProp(rest);
25
+ const safeRootAttributes = sanitizeRootAttributes(rest, true);
26
+ const labelled = Boolean(title) || hasA11yProp(safeRootAttributes);
25
27
  return React.createElement(
26
28
  "svg",
27
29
  {
@@ -29,15 +31,15 @@ export const Icon = forwardRef(({ color, size, strokeWidth, absoluteStrokeWidth,
29
31
  ...defaultAttributes,
30
32
  width: resolvedSize,
31
33
  height: resolvedSize,
32
- stroke: color ?? context.color ?? "currentColor",
34
+ stroke: sanitizePaint(color ?? context.color),
33
35
  strokeWidth: calculatedStrokeWidth,
34
36
  className: mergeClasses("vadivam", context.className, className),
35
37
  ...(!labelled ? { "aria-hidden": "true" } : {}),
36
38
  ...(title ? { role: "img" } : {}),
37
- ...rest
39
+ ...safeRootAttributes
38
40
  },
39
41
  title ? React.createElement("title", { key: "title" }, title) : null,
40
- ...iconNode.map(renderNode),
42
+ ...sanitizeIconNode(iconNode).map(renderNode),
41
43
  children
42
44
  );
43
45
  });
@@ -8,7 +8,7 @@ function toPascalCase(value) {
8
8
  return value.split(/[-_ ]+/).filter(Boolean).map((part) => part[0].toUpperCase() + part.slice(1)).join("");
9
9
  }
10
10
  export function createIcon(iconName, iconNode) {
11
- const Component = forwardRef(({ className, ...props }, ref) => React.createElement(Icon, { ref, iconNode, className: [`vadivam-${toKebabCase(iconName)}`, className].filter(Boolean).join(" "), ...props }));
11
+ const Component = forwardRef(({ className, ...props }, ref) => React.createElement(Icon, { ...props, ref, iconNode, className: [`vadivam-${toKebabCase(iconName)}`, className].filter(Boolean).join(" ") }));
12
12
  Component.displayName = toPascalCase(iconName);
13
13
  return Component;
14
14
  }
package/dist/index.d.ts CHANGED
@@ -510,4 +510,4 @@ export { Icon } from "./Icon.js";
510
510
  export { createIcon, createVadivamIcon } from "./createIcon.js";
511
511
  export { icons } from "./icons.js";
512
512
  export { VadivamProvider, useVadivamContext } from "./context.js";
513
- export type { IconName, IconNode, SVGAttributes, VadivamIcon, VadivamProps } from "./types.js";
513
+ export type { IconName, IconNode, SVGAttributes, SVGElementType, VadivamIcon, VadivamProps } from "./types.js";
@@ -0,0 +1,4 @@
1
+ import type { IconNode } from "./types.js";
2
+ export declare function sanitizeIconNode(iconNode: unknown): IconNode;
3
+ export declare function sanitizePaint(value: unknown, fallback?: string): string | number;
4
+ export declare function sanitizeRootAttributes(attrs: unknown, allowEventFunctions?: boolean): Record<string, unknown>;
@@ -0,0 +1,141 @@
1
+ const SAFE_ICON_TAGS = new Set(["circle","ellipse","line","path","polygon","polyline","rect"]);
2
+ const GEOMETRY_ATTRIBUTES = {
3
+ circle: new Set(["cx", "cy", "r"]),
4
+ ellipse: new Set(["cx", "cy", "rx", "ry"]),
5
+ line: new Set(["x1", "y1", "x2", "y2"]),
6
+ path: new Set(["d", "pathLength"]),
7
+ polygon: new Set(["points", "pathLength"]),
8
+ polyline: new Set(["points", "pathLength"]),
9
+ rect: new Set(["x", "y", "width", "height", "rx", "ry", "pathLength"]),
10
+ };
11
+ const PRESENTATION_ATTRIBUTES = new Set([
12
+ "class", "className", "id", "role", "key", "opacity", "transform",
13
+ "clip-rule", "clipRule", "fill", "fill-opacity", "fillOpacity", "fill-rule", "fillRule",
14
+ "shape-rendering", "shapeRendering", "stroke", "stroke-dasharray", "strokeDasharray",
15
+ "stroke-dashoffset", "strokeDashoffset", "stroke-linecap", "strokeLinecap",
16
+ "stroke-linejoin", "strokeLinejoin", "stroke-miterlimit", "strokeMiterlimit",
17
+ "stroke-opacity", "strokeOpacity", "stroke-width", "strokeWidth", "vector-effect", "vectorEffect",
18
+ ]);
19
+ const SAFE_ATTRIBUTE_NAME = /^[A-Za-z_][A-Za-z0-9_.-]*$/;
20
+ const SAFE_DATA_ATTRIBUTE = /^data-[A-Za-z0-9_.-]+$/;
21
+ const SAFE_ARIA_ATTRIBUTE = /^aria-[A-Za-z0-9_.-]+$/;
22
+ const SAFE_STYLE_NAME = /^(?:--[A-Za-z0-9_-]+|[A-Za-z_][A-Za-z0-9_-]*)$/;
23
+ const SAFE_EVENT_NAME = /^on(?::[A-Za-z][A-Za-z0-9_.-]*|[A-Za-z][A-Za-z0-9_.-]*)$/;
24
+ const UNSAFE_ROOT_ATTRIBUTE = /^(?:dangerouslySetInnerHTML|innerHTML|innerText|textContent|children|href|xlinkHref|src|srcdoc)$/i;
25
+ const URL_VALUE_ATTRIBUTE = /^(?:fill|stroke|filter|clipPath|clip-path|mask|marker|markerStart|marker-start|markerMid|marker-mid|markerEnd|marker-end|cursor)$/;
26
+ const SCRIPT_SCHEME = /(?:java|vb)script\s*:/i;
27
+ const URL_FUNCTION = /url\s*\(/i;
28
+ const COMPLETE_URL_FUNCTION = /url\s*\(\s*(["']?)([^"')\s]+)\1\s*\)/gi;
29
+ const EXTERNAL_SCHEME = /(?:https?|data|blob|file|ftp)\s*:/i;
30
+ const CSS_ESCAPE = /\\/;
31
+ const CSS_RESOURCE_FUNCTION = /(?:image-set|cross-fade|image|src)\s*\(/i;
32
+ const CSS_IMPORT = /@import\b/i;
33
+ const MAX_ICON_DEPTH = 32;
34
+ const MAX_ICON_NODES = 1024;
35
+
36
+ function isRecord(value) {
37
+ return value !== null && typeof value === "object" && !Array.isArray(value);
38
+ }
39
+
40
+ function isSafeValue(name, value) {
41
+ if (value === undefined || value === null || typeof value === "function") return false;
42
+ if (typeof value === "object") return false;
43
+ if (typeof value === "string" && SCRIPT_SCHEME.test(value)) return false;
44
+ if ((name === "fill" || name === "stroke") && typeof value === "string" && hasExternalUrl(value)) return false;
45
+ return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
46
+ }
47
+
48
+ function hasExternalUrl(value) {
49
+ if (!URL_FUNCTION.test(value)) return false;
50
+ COMPLETE_URL_FUNCTION.lastIndex = 0;
51
+ let matched = false;
52
+ let match;
53
+ while ((match = COMPLETE_URL_FUNCTION.exec(value)) !== null) {
54
+ matched = true;
55
+ if (!match[2].startsWith("#")) return true;
56
+ }
57
+ return !matched;
58
+ }
59
+
60
+ function hasUnsafeStyleValue(value) {
61
+ return SCRIPT_SCHEME.test(value) || EXTERNAL_SCHEME.test(value) || CSS_ESCAPE.test(value) ||
62
+ CSS_RESOURCE_FUNCTION.test(value) || CSS_IMPORT.test(value) || hasExternalUrl(value);
63
+ }
64
+
65
+ function isSafeIconAttribute(tag, name) {
66
+ return SAFE_ATTRIBUTE_NAME.test(name) && (
67
+ GEOMETRY_ATTRIBUTES[tag]?.has(name) ||
68
+ PRESENTATION_ATTRIBUTES.has(name) ||
69
+ SAFE_DATA_ATTRIBUTE.test(name) ||
70
+ SAFE_ARIA_ATTRIBUTE.test(name)
71
+ );
72
+ }
73
+
74
+ function sanitizeStyle(value) {
75
+ if (typeof value === "string") return hasUnsafeStyleValue(value) ? undefined : value;
76
+ if (Array.isArray(value)) {
77
+ const safeValues = value.map(sanitizeStyle).filter((entry) => entry !== undefined);
78
+ return safeValues.length ? safeValues : undefined;
79
+ }
80
+ if (!isRecord(value)) return undefined;
81
+ const safeStyle = {};
82
+ for (const [name, entry] of Object.entries(value)) {
83
+ if (!SAFE_STYLE_NAME.test(name) || (typeof entry !== "string" && typeof entry !== "number")) continue;
84
+ if (typeof entry === "string" && hasUnsafeStyleValue(entry)) continue;
85
+ safeStyle[name] = entry;
86
+ }
87
+ return Object.keys(safeStyle).length ? safeStyle : undefined;
88
+ }
89
+
90
+ function safeRootValue(name, value) {
91
+ if (name === "style") return sanitizeStyle(value);
92
+ if (value === undefined || value === null || typeof value === "object" || typeof value === "function") return undefined;
93
+ if (typeof value === "string" && URL_VALUE_ATTRIBUTE.test(name) && (SCRIPT_SCHEME.test(value) || hasExternalUrl(value))) return undefined;
94
+ return value;
95
+ }
96
+
97
+ export function sanitizePaint(value, fallback = "currentColor") {
98
+ const safeValue = safeRootValue("stroke", value);
99
+ return typeof safeValue === "string" || typeof safeValue === "number" ? safeValue : fallback;
100
+ }
101
+
102
+ function sanitizeNodes(iconNode, activeArrays, state, depth) {
103
+ if (!Array.isArray(iconNode) || depth > MAX_ICON_DEPTH || activeArrays.has(iconNode)) return [];
104
+ activeArrays.add(iconNode);
105
+ const safeNodes = [];
106
+ for (const node of iconNode) {
107
+ state.count += 1;
108
+ if (state.count > MAX_ICON_NODES) break;
109
+ if (!Array.isArray(node) || typeof node[0] !== "string" || !SAFE_ICON_TAGS.has(node[0]) || !isRecord(node[1])) continue;
110
+ const [tag, attrs, children] = node;
111
+ const safeAttrs = {};
112
+ for (const [name, value] of Object.entries(attrs)) {
113
+ if (isSafeIconAttribute(tag, name) && isSafeValue(name, value)) safeAttrs[name] = value;
114
+ }
115
+ safeNodes.push([tag, safeAttrs]);
116
+ }
117
+ activeArrays.delete(iconNode);
118
+ return safeNodes;
119
+ }
120
+
121
+ export function sanitizeIconNode(iconNode) {
122
+ return sanitizeNodes(iconNode, new WeakSet(), { count: 0 }, 0);
123
+ }
124
+
125
+ export function sanitizeRootAttributes(attrs, allowEventFunctions = false) {
126
+ if (!isRecord(attrs)) return {};
127
+ const safeAttrs = {};
128
+ for (const name of Object.keys(attrs)) {
129
+ const value = attrs[name];
130
+ if (/^on/i.test(name)) {
131
+ if (allowEventFunctions && typeof value === "function" && SAFE_EVENT_NAME.test(name)) {
132
+ Object.defineProperty(safeAttrs, name, { enumerable: true, get: () => typeof attrs[name] === "function" ? attrs[name] : undefined });
133
+ }
134
+ continue;
135
+ }
136
+ if (!SAFE_ATTRIBUTE_NAME.test(name) || UNSAFE_ROOT_ATTRIBUTE.test(name) || /^prop/i.test(name)) continue;
137
+ if (safeRootValue(name, value) === undefined) continue;
138
+ Object.defineProperty(safeAttrs, name, { enumerable: true, get: () => safeRootValue(name, attrs[name]) });
139
+ }
140
+ return safeAttrs;
141
+ }
package/dist/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type * as React from "react";
2
- export type SVGElementType = "circle" | "ellipse" | "g" | "line" | "path" | "polygon" | "polyline" | "rect";
3
- export type IconNode = readonly [tag: SVGElementType, attrs: Record<string, string>][];
2
+ export type SVGElementType = "circle" | "ellipse" | "line" | "path" | "polygon" | "polyline" | "rect";
3
+ export type IconNode = readonly [tag: SVGElementType, attrs: Record<string, string | number | boolean>][];
4
4
  export type SVGAttributes = Partial<React.SVGProps<SVGSVGElement>>;
5
5
  export interface VadivamProps extends Omit<React.SVGProps<SVGSVGElement>, "color"> {
6
6
  size?: string | number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vadivam-react",
3
- "version": "0.0.42",
3
+ "version": "0.0.46",
4
4
  "description": "Pixel-perfect 24px outline icon components for React with tree-shakeable imports.",
5
5
  "license": "MIT",
6
6
  "author": "Praveen Juge",