billboard.js 3.18.0-next.2 → 3.18.0
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/dist/billboard.css +1 -1
- package/dist/billboard.js +319 -100
- package/dist/billboard.min.css +1 -1
- package/dist/billboard.min.js +10 -9
- package/dist/billboard.pkgd.js +2059 -1625
- package/dist/billboard.pkgd.min.js +23 -22
- package/dist/plugin/billboardjs-plugin-bubblecompare.js +2 -2
- package/dist/plugin/billboardjs-plugin-bubblecompare.min.js +2 -2
- package/dist/plugin/billboardjs-plugin-sparkline.js +305 -79
- package/dist/plugin/billboardjs-plugin-sparkline.min.js +4 -4
- package/dist/plugin/billboardjs-plugin-stanford.js +305 -79
- package/dist/plugin/billboardjs-plugin-stanford.min.js +11 -11
- package/dist/plugin/billboardjs-plugin-tableview.js +305 -79
- package/dist/plugin/billboardjs-plugin-tableview.min.js +6 -5
- package/dist/plugin/billboardjs-plugin-textoverlap.js +305 -79
- package/dist/plugin/billboardjs-plugin-textoverlap.min.js +4 -4
- package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.js +1743 -1528
- package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.min.js +8 -8
- package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.js +2047 -1606
- package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.min.js +10 -10
- package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.js +2048 -1607
- package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.min.js +17 -17
- package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.js +2049 -1608
- package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.min.js +13 -12
- package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.js +2050 -1609
- package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.min.js +10 -10
- package/dist/theme/dark.css +1 -1
- package/dist/theme/dark.min.css +1 -1
- package/dist/theme/datalab.css +1 -1
- package/dist/theme/datalab.min.css +1 -1
- package/dist/theme/graph.css +1 -1
- package/dist/theme/graph.min.css +1 -1
- package/dist/theme/insight.css +1 -1
- package/dist/theme/insight.min.css +1 -1
- package/dist/theme/modern.css +1 -1
- package/dist/theme/modern.min.css +1 -1
- package/dist-esm/billboard.js +441 -135
- package/dist-esm/plugin/billboardjs-plugin-bubblecompare.js +2 -2
- package/dist-esm/plugin/billboardjs-plugin-sparkline.js +2 -2
- package/dist-esm/plugin/billboardjs-plugin-stanford.js +2 -2
- package/dist-esm/plugin/billboardjs-plugin-tableview.js +416 -102
- package/dist-esm/plugin/billboardjs-plugin-textoverlap.js +2 -2
- package/package.json +1 -1
- package/src/ChartInternal/ChartInternal.ts +1 -1
- package/src/ChartInternal/internals/tooltip.ts +11 -9
- package/src/config/Options/axis/y2.ts +0 -13
- package/src/config/Options/common/legend.ts +1 -0
- package/src/config/Options/common/point.ts +1 -1
- package/src/config/Options/common/tooltip.ts +3 -1
- package/src/module/sanitize.ts +471 -0
- package/src/module/util.ts +1 -131
|
@@ -91,9 +91,11 @@ export default {
|
|
|
91
91
|
const $$ = this;
|
|
92
92
|
const {api, config} = $$;
|
|
93
93
|
|
|
94
|
-
return
|
|
95
|
-
config.tooltip_contents
|
|
96
|
-
|
|
94
|
+
return sanitize(
|
|
95
|
+
isFunction(config.tooltip_contents) ?
|
|
96
|
+
config.tooltip_contents.bind(api)(...args) :
|
|
97
|
+
$$.getTooltipContent(...args)
|
|
98
|
+
);
|
|
97
99
|
},
|
|
98
100
|
|
|
99
101
|
/**
|
|
@@ -116,9 +118,9 @@ export default {
|
|
|
116
118
|
return isFunction(fn) ? fn.bind(api) : fn;
|
|
117
119
|
});
|
|
118
120
|
|
|
119
|
-
// determine
|
|
120
|
-
const titleFormat = (...arg) =>
|
|
121
|
-
const nameFormat = (...arg) =>
|
|
121
|
+
// determine formatter function
|
|
122
|
+
const titleFormat = (...arg) => (titleFn || defaultTitleFormat)(...arg);
|
|
123
|
+
const nameFormat = (...arg) => (nameFn || (name => name))(...arg);
|
|
122
124
|
const valueFormat = (v, ratio, id, index) => {
|
|
123
125
|
let fn = valueFn;
|
|
124
126
|
|
|
@@ -135,7 +137,7 @@ export default {
|
|
|
135
137
|
}
|
|
136
138
|
}
|
|
137
139
|
|
|
138
|
-
return
|
|
140
|
+
return fn(v, ratio, id, index);
|
|
139
141
|
};
|
|
140
142
|
|
|
141
143
|
const order = config.tooltip_order;
|
|
@@ -533,12 +535,12 @@ export default {
|
|
|
533
535
|
|
|
534
536
|
// set tooltip content
|
|
535
537
|
tooltip
|
|
536
|
-
.html(
|
|
538
|
+
.html($$.getTooltipHTML(
|
|
537
539
|
selectedData, // data
|
|
538
540
|
$$.axis ? $$.axis.getXAxisTickFormat() : $$.categoryName.bind($$), // defaultTitleFormat
|
|
539
541
|
$$.getDefaultValueFormat(), // defaultValueFormat
|
|
540
542
|
$$.color // color
|
|
541
|
-
))
|
|
543
|
+
))
|
|
542
544
|
.style("display", null)
|
|
543
545
|
.style("visibility", null) // for IE9
|
|
544
546
|
.datum(datum = {
|
|
@@ -6,19 +6,6 @@
|
|
|
6
6
|
* y2 Axis config options
|
|
7
7
|
*/
|
|
8
8
|
export default {
|
|
9
|
-
/**
|
|
10
|
-
* Set clip-path attribute for y2 axis element
|
|
11
|
-
* - **NOTE**: `clip-path` attribute for y2 Axis is set only when `axis.y2.inner` option is true.
|
|
12
|
-
* @name axis․y2․clipPath
|
|
13
|
-
* @memberof Options
|
|
14
|
-
* @type {boolean}
|
|
15
|
-
* @default true
|
|
16
|
-
* @example
|
|
17
|
-
* // don't set 'clip-path' attribute
|
|
18
|
-
* clipPath: false
|
|
19
|
-
*/
|
|
20
|
-
axis_y2_clipPath: true,
|
|
21
|
-
|
|
22
9
|
/**
|
|
23
10
|
* Show or hide y2 axis.
|
|
24
11
|
* - **NOTE**:
|
|
@@ -18,6 +18,7 @@ export default {
|
|
|
18
18
|
* @property {string|HTMLElement} [legend.contents.bindto=undefined] Set CSS selector or element reference to bind legend items.
|
|
19
19
|
* - **NOTE:** Should be used along with `legend.contents.template`.
|
|
20
20
|
* @property {string|function} [legend.contents.template="<span style='color:#fff;padding:5px;background-color:{=COLOR}'>{=TITLE}</span>"] Set item's template.<br>
|
|
21
|
+
* - **NOTE:** Only common HTML tags are allowed to prevent XSS attacks. If creating charts from user input, it is recommended to sanitize input values to avoid potential vulnerabilities.
|
|
21
22
|
* - If set `string` value, within template the 'color' and 'title' can be replaced using template-like syntax string:
|
|
22
23
|
* - {=COLOR}: data color value
|
|
23
24
|
* - {=TITLE}: data title value
|
|
@@ -59,7 +59,7 @@ export default {
|
|
|
59
59
|
* - This is an `experimental` feature and can have some unexpected behaviors.
|
|
60
60
|
* - If chart has 'bubble' type, only circle can be used.
|
|
61
61
|
* - For IE, non circle point expansions are not supported due to lack of transform support.
|
|
62
|
-
* -
|
|
62
|
+
* - Only common SVG tags are allowed to prevent XSS attacks. If creating charts from user input, it is recommended to sanitize input values to avoid potential vulnerabilities.
|
|
63
63
|
* - **Available Values:**
|
|
64
64
|
* - circle
|
|
65
65
|
* - rectangle
|
|
@@ -40,6 +40,7 @@ export default {
|
|
|
40
40
|
* If tooltip.grouped is true, data includes multiple data points.<br><br>
|
|
41
41
|
* Specified function receives `data` array and `defaultTitleFormat`, `defaultValueFormat` and `color` functions of the data point to show.
|
|
42
42
|
* - **Note:**
|
|
43
|
+
* - Only common HTML tags are allowed to prevent XSS attacks. If creating charts from user input, it is recommended to sanitize input values to avoid potential vulnerabilities.<br><br>
|
|
43
44
|
* - defaultTitleFormat:
|
|
44
45
|
* - if `axis.x.tick.format` option will be used if set.
|
|
45
46
|
* - otherwise, will return function based on tick format type(category, timeseries).
|
|
@@ -49,7 +50,8 @@ export default {
|
|
|
49
50
|
* - otherwise, will parse value and return as number.
|
|
50
51
|
* @property {string|HTMLElement} [tooltip.contents.bindto=undefined] Set CSS selector or element reference to bind tooltip.
|
|
51
52
|
* - **NOTE:** When is specified, will not be updating tooltip's position.
|
|
52
|
-
* @property {string} [tooltip.contents.template=undefined] Set tooltip's template
|
|
53
|
+
* @property {string} [tooltip.contents.template=undefined] Set tooltip's template.
|
|
54
|
+
* - **NOTE:** Only common HTML tags are allowed to prevent XSS attacks. If creating charts from user input, it is recommended to sanitize input values to avoid potential vulnerabilities.<br><br>
|
|
53
55
|
* Within template, below syntax will be replaced using template-like syntax string:
|
|
54
56
|
* - **{{ ... }}**: the doubly curly brackets indicate loop block for data rows.
|
|
55
57
|
* - **{=CLASS_TOOLTIP}**: default tooltip class name `bb-tooltip`.
|
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2017 ~ present NAVER Corp.
|
|
3
|
+
* billboard.js project is licensed under the MIT license
|
|
4
|
+
* @ignore
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* HTML/SVG Sanitization module
|
|
9
|
+
* Pure whitelist approach - only explicitly allowed tags, attributes, and protocols pass through
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// Whitelist of allowed HTML/SVG tags
|
|
13
|
+
const ALLOWED_TAGS = new Set([
|
|
14
|
+
// HTML tags for tooltip/legend templates
|
|
15
|
+
"span",
|
|
16
|
+
"div",
|
|
17
|
+
"p",
|
|
18
|
+
"br",
|
|
19
|
+
"b",
|
|
20
|
+
"i",
|
|
21
|
+
"em",
|
|
22
|
+
"strong",
|
|
23
|
+
"u",
|
|
24
|
+
"s",
|
|
25
|
+
"sub",
|
|
26
|
+
"sup",
|
|
27
|
+
"ul",
|
|
28
|
+
"ol",
|
|
29
|
+
"li",
|
|
30
|
+
"dl",
|
|
31
|
+
"dt",
|
|
32
|
+
"dd",
|
|
33
|
+
"table",
|
|
34
|
+
"thead",
|
|
35
|
+
"tbody",
|
|
36
|
+
"tfoot",
|
|
37
|
+
"tr",
|
|
38
|
+
"th",
|
|
39
|
+
"td",
|
|
40
|
+
"caption",
|
|
41
|
+
"colgroup",
|
|
42
|
+
"col",
|
|
43
|
+
"hr",
|
|
44
|
+
"pre",
|
|
45
|
+
"code",
|
|
46
|
+
"blockquote",
|
|
47
|
+
"a",
|
|
48
|
+
"img",
|
|
49
|
+
// SVG tags for point patterns
|
|
50
|
+
"svg",
|
|
51
|
+
"g",
|
|
52
|
+
"path",
|
|
53
|
+
"circle",
|
|
54
|
+
"ellipse",
|
|
55
|
+
"rect",
|
|
56
|
+
"line",
|
|
57
|
+
"polyline",
|
|
58
|
+
"polygon",
|
|
59
|
+
"text",
|
|
60
|
+
"tspan",
|
|
61
|
+
"textPath",
|
|
62
|
+
"use",
|
|
63
|
+
"defs",
|
|
64
|
+
"symbol",
|
|
65
|
+
"clipPath",
|
|
66
|
+
"mask",
|
|
67
|
+
"linearGradient",
|
|
68
|
+
"radialGradient",
|
|
69
|
+
"stop",
|
|
70
|
+
"pattern",
|
|
71
|
+
"marker",
|
|
72
|
+
"title",
|
|
73
|
+
"desc"
|
|
74
|
+
]);
|
|
75
|
+
|
|
76
|
+
// Whitelist of allowed attributes
|
|
77
|
+
const ALLOWED_ATTRS = new Set([
|
|
78
|
+
// Common attributes
|
|
79
|
+
"class",
|
|
80
|
+
"id",
|
|
81
|
+
"style",
|
|
82
|
+
"title",
|
|
83
|
+
"lang",
|
|
84
|
+
"dir",
|
|
85
|
+
// HTML specific
|
|
86
|
+
"href",
|
|
87
|
+
"src",
|
|
88
|
+
"alt",
|
|
89
|
+
"width",
|
|
90
|
+
"height",
|
|
91
|
+
"colspan",
|
|
92
|
+
"rowspan",
|
|
93
|
+
"scope",
|
|
94
|
+
"headers",
|
|
95
|
+
// SVG presentation attributes
|
|
96
|
+
"d",
|
|
97
|
+
"points",
|
|
98
|
+
"x",
|
|
99
|
+
"y",
|
|
100
|
+
"x1",
|
|
101
|
+
"x2",
|
|
102
|
+
"y1",
|
|
103
|
+
"y2",
|
|
104
|
+
"cx",
|
|
105
|
+
"cy",
|
|
106
|
+
"r",
|
|
107
|
+
"rx",
|
|
108
|
+
"ry",
|
|
109
|
+
"dx",
|
|
110
|
+
"dy",
|
|
111
|
+
"viewBox",
|
|
112
|
+
"preserveAspectRatio",
|
|
113
|
+
"transform",
|
|
114
|
+
"fill",
|
|
115
|
+
"fill-opacity",
|
|
116
|
+
"fill-rule",
|
|
117
|
+
"stroke",
|
|
118
|
+
"stroke-width",
|
|
119
|
+
"stroke-opacity",
|
|
120
|
+
"stroke-linecap",
|
|
121
|
+
"stroke-linejoin",
|
|
122
|
+
"stroke-dasharray",
|
|
123
|
+
"stroke-dashoffset",
|
|
124
|
+
"opacity",
|
|
125
|
+
"clip-path",
|
|
126
|
+
"clip-rule",
|
|
127
|
+
"mask",
|
|
128
|
+
"font-family",
|
|
129
|
+
"font-size",
|
|
130
|
+
"font-weight",
|
|
131
|
+
"font-style",
|
|
132
|
+
"text-anchor",
|
|
133
|
+
"dominant-baseline",
|
|
134
|
+
"offset",
|
|
135
|
+
"stop-color",
|
|
136
|
+
"stop-opacity",
|
|
137
|
+
"gradientUnits",
|
|
138
|
+
"gradientTransform",
|
|
139
|
+
"spreadMethod",
|
|
140
|
+
"patternUnits",
|
|
141
|
+
"patternTransform",
|
|
142
|
+
"marker-start",
|
|
143
|
+
"marker-mid",
|
|
144
|
+
"marker-end",
|
|
145
|
+
"markerWidth",
|
|
146
|
+
"markerHeight",
|
|
147
|
+
"refX",
|
|
148
|
+
"refY",
|
|
149
|
+
"xlink:href"
|
|
150
|
+
]);
|
|
151
|
+
|
|
152
|
+
// Whitelist of allowed URI protocols
|
|
153
|
+
const ALLOWED_URI_PROTOCOLS = new Set([
|
|
154
|
+
"http:",
|
|
155
|
+
"https:",
|
|
156
|
+
"mailto:"
|
|
157
|
+
]);
|
|
158
|
+
|
|
159
|
+
// Attributes that contain URIs
|
|
160
|
+
const URI_ATTRS = new Set(["href", "src", "xlink:href"]);
|
|
161
|
+
|
|
162
|
+
// Pre-compiled regex patterns for performance
|
|
163
|
+
const TAG_NAME_REGEX = /^<\/?([a-zA-Z][a-zA-Z0-9]*)/;
|
|
164
|
+
const CLOSING_TAG_REGEX = /^<\/([a-zA-Z][a-zA-Z0-9]*)\s*>$/;
|
|
165
|
+
const OPENING_TAG_REGEX = /^<([a-zA-Z][a-zA-Z0-9]*)([\s\S]*?)(\/?)>$/;
|
|
166
|
+
const ATTR_REGEX = /([a-zA-Z][\w:-]*)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/g;
|
|
167
|
+
const URL_IN_STYLE_REGEX = /url\s*\(\s*["']?([^"')]+)["']?\s*\)/gi;
|
|
168
|
+
|
|
169
|
+
// Dangerous CSS patterns
|
|
170
|
+
const DANGEROUS_CSS_PATTERNS = [
|
|
171
|
+
"expression(",
|
|
172
|
+
"behavior:",
|
|
173
|
+
"binding:",
|
|
174
|
+
"@import",
|
|
175
|
+
"@charset",
|
|
176
|
+
"-moz-binding:"
|
|
177
|
+
];
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Decode HTML entities in a string
|
|
181
|
+
* @param {string} str String with potential HTML entities
|
|
182
|
+
* @returns {string} Decoded string
|
|
183
|
+
* @private
|
|
184
|
+
*/
|
|
185
|
+
function decodeHTMLEntities(str: string): string {
|
|
186
|
+
return str
|
|
187
|
+
// Named entities
|
|
188
|
+
.replace(/:/gi, ":")
|
|
189
|
+
.replace(/&newline;/gi, "\n")
|
|
190
|
+
.replace(/&tab;/gi, "\t")
|
|
191
|
+
.replace(/ /gi, " ")
|
|
192
|
+
.replace(/</gi, "<")
|
|
193
|
+
.replace(/>/gi, ">")
|
|
194
|
+
.replace(/&/gi, "&")
|
|
195
|
+
.replace(/"/gi, "\"")
|
|
196
|
+
.replace(/'/gi, "'")
|
|
197
|
+
// Numeric entities (decimal)
|
|
198
|
+
.replace(/&#(\d+);/gi, (_, code) => String.fromCharCode(parseInt(code, 10)))
|
|
199
|
+
// Numeric entities (hex)
|
|
200
|
+
.replace(/&#x([0-9a-f]+);/gi, (_, code) => String.fromCharCode(parseInt(code, 16)));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Check if a URI is safe (whitelist approach)
|
|
205
|
+
* @param {string} uri URI to check
|
|
206
|
+
* @returns {boolean} Whether the URI is safe
|
|
207
|
+
* @private
|
|
208
|
+
*/
|
|
209
|
+
function isSafeURI(uri: string): boolean {
|
|
210
|
+
// Decode HTML entities first to prevent bypass
|
|
211
|
+
const decoded = decodeHTMLEntities(uri).trim();
|
|
212
|
+
|
|
213
|
+
// Remove any whitespace/control characters that could be used for bypass
|
|
214
|
+
// eslint-disable-next-line no-control-regex
|
|
215
|
+
const normalized = decoded.replace(/[\s\u0000-\u001f]/g, "").toLowerCase();
|
|
216
|
+
|
|
217
|
+
// Empty or fragment-only URIs are safe
|
|
218
|
+
if (!normalized || normalized.startsWith("#")) {
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Relative paths are safe
|
|
223
|
+
if (
|
|
224
|
+
normalized.startsWith("/") ||
|
|
225
|
+
normalized.startsWith("./") ||
|
|
226
|
+
normalized.startsWith("../") ||
|
|
227
|
+
!normalized.includes(":")
|
|
228
|
+
) {
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Check if protocol is in whitelist
|
|
233
|
+
const colonIndex = normalized.indexOf(":");
|
|
234
|
+
if (colonIndex > 0) {
|
|
235
|
+
const protocol = normalized.substring(0, colonIndex + 1);
|
|
236
|
+
return ALLOWED_URI_PROTOCOLS.has(protocol);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Check if a style value is safe (whitelist approach)
|
|
244
|
+
* @param {string} style Style attribute value
|
|
245
|
+
* @returns {string|null} Sanitized style or null if unsafe
|
|
246
|
+
* @private
|
|
247
|
+
*/
|
|
248
|
+
function sanitizeStyleValue(style: string): string | null {
|
|
249
|
+
// Decode HTML entities first
|
|
250
|
+
const decoded = decodeHTMLEntities(style);
|
|
251
|
+
|
|
252
|
+
// Remove any control characters
|
|
253
|
+
// eslint-disable-next-line no-control-regex
|
|
254
|
+
const cleaned = decoded.replace(/[\u0000-\u001f]/g, "");
|
|
255
|
+
|
|
256
|
+
// Check for url() - only allow safe URIs inside
|
|
257
|
+
URL_IN_STYLE_REGEX.lastIndex = 0;
|
|
258
|
+
let match;
|
|
259
|
+
|
|
260
|
+
while ((match = URL_IN_STYLE_REGEX.exec(cleaned)) !== null) {
|
|
261
|
+
if (!isSafeURI(match[1])) {
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Check for dangerous CSS patterns (expression, behavior, etc.)
|
|
267
|
+
const normalizedLower = cleaned.toLowerCase().replace(/\s/g, "");
|
|
268
|
+
|
|
269
|
+
for (const pattern of DANGEROUS_CSS_PATTERNS) {
|
|
270
|
+
if (normalizedLower.includes(pattern)) {
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return style;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Lookup table for encoding dangerous characters in attribute values
|
|
279
|
+
const ATTR_ENCODE_MAP: Record<string, string> = {
|
|
280
|
+
"\"": """,
|
|
281
|
+
"'": "'",
|
|
282
|
+
"`": "`"
|
|
283
|
+
};
|
|
284
|
+
const ATTR_ENCODE_REGEX = /["'`]/g;
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Encode dangerous characters in attribute values to HTML entities
|
|
288
|
+
* This prevents attribute injection attacks where quotes/backticks break out of the attribute context
|
|
289
|
+
* @param {string} value Attribute value
|
|
290
|
+
* @returns {string} Encoded value
|
|
291
|
+
* @private
|
|
292
|
+
*/
|
|
293
|
+
function encodeAttrValue(value: string): string {
|
|
294
|
+
return value.replace(ATTR_ENCODE_REGEX, char => ATTR_ENCODE_MAP[char]);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Sanitize attribute value using whitelist approach
|
|
299
|
+
* @param {string} name Attribute name
|
|
300
|
+
* @param {string} value Attribute value
|
|
301
|
+
* @param {boolean} wasUnquoted Whether the value was originally unquoted
|
|
302
|
+
* @returns {string|null} Sanitized value if safe, null if should be removed
|
|
303
|
+
* @private
|
|
304
|
+
*/
|
|
305
|
+
function sanitizeAttrValue(name: string, value: string, wasUnquoted: boolean = false):
|
|
306
|
+
| string
|
|
307
|
+
| null {
|
|
308
|
+
// Check URI attributes with whitelist
|
|
309
|
+
if (URI_ATTRS.has(name)) {
|
|
310
|
+
if (!isSafeURI(value)) {
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
// Encode dangerous characters in URI values to prevent attribute injection
|
|
314
|
+
return wasUnquoted ? encodeAttrValue(value) : value;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Check style attribute
|
|
318
|
+
if (name === "style") {
|
|
319
|
+
const sanitizedStyle = sanitizeStyleValue(value);
|
|
320
|
+
if (sanitizedStyle === null) {
|
|
321
|
+
return null;
|
|
322
|
+
}
|
|
323
|
+
// Encode dangerous characters in style values
|
|
324
|
+
return wasUnquoted ? encodeAttrValue(sanitizedStyle) : sanitizedStyle;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// For other attributes, check for embedded event handlers
|
|
328
|
+
const decoded = decodeHTMLEntities(value).toLowerCase().replace(/\s/g, "");
|
|
329
|
+
if (/\bon\w+=/.test(decoded)) {
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Encode dangerous characters to prevent attribute injection
|
|
334
|
+
return wasUnquoted ? encodeAttrValue(value) : value;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Extract tag name from a tag string
|
|
339
|
+
* Returns null if not a valid tag format
|
|
340
|
+
* @param {string} tag Tag string starting with <
|
|
341
|
+
* @returns {string|null} Lowercase tag name or null
|
|
342
|
+
* @private
|
|
343
|
+
*/
|
|
344
|
+
function extractTagName(tag: string): string | null {
|
|
345
|
+
// Must start with < followed immediately by letter (no spaces allowed)
|
|
346
|
+
const match = tag.match(TAG_NAME_REGEX);
|
|
347
|
+
return match ? match[1].toLowerCase() : null;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Check if a tag is in the whitelist
|
|
352
|
+
* @param {string} tag Tag string
|
|
353
|
+
* @returns {boolean} Whether tag is allowed
|
|
354
|
+
* @private
|
|
355
|
+
*/
|
|
356
|
+
function isAllowedTag(tag: string): boolean {
|
|
357
|
+
const tagName = extractTagName(tag);
|
|
358
|
+
return tagName !== null && ALLOWED_TAGS.has(tagName);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Sanitize a single HTML/SVG tag (only called for allowed tags)
|
|
363
|
+
* @param {string} fullTag The full tag string including < and >
|
|
364
|
+
* @returns {string} Sanitized tag
|
|
365
|
+
* @private
|
|
366
|
+
*/
|
|
367
|
+
function sanitizeTag(fullTag: string): string {
|
|
368
|
+
// Closing tag
|
|
369
|
+
const closingMatch = fullTag.match(CLOSING_TAG_REGEX);
|
|
370
|
+
if (closingMatch) {
|
|
371
|
+
return `</${closingMatch[1].toLowerCase()}>`;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Opening tag
|
|
375
|
+
const openingMatch = fullTag.match(OPENING_TAG_REGEX);
|
|
376
|
+
if (!openingMatch) {
|
|
377
|
+
return "";
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const [, tagName, attrString, selfClose] = openingMatch;
|
|
381
|
+
const lowerTagName = tagName.toLowerCase();
|
|
382
|
+
|
|
383
|
+
// Parse and filter attributes, preserving original quote style
|
|
384
|
+
const allowedAttrs: string[] = [];
|
|
385
|
+
ATTR_REGEX.lastIndex = 0;
|
|
386
|
+
let attrMatch;
|
|
387
|
+
|
|
388
|
+
while ((attrMatch = ATTR_REGEX.exec(attrString)) !== null) {
|
|
389
|
+
const attrName = attrMatch[1].toLowerCase();
|
|
390
|
+
const doubleQuotedValue = attrMatch[2];
|
|
391
|
+
const singleQuotedValue = attrMatch[3];
|
|
392
|
+
const unquotedValue = attrMatch[4];
|
|
393
|
+
|
|
394
|
+
// Skip event handlers (on*)
|
|
395
|
+
if (attrName.startsWith("on")) {
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Determine original quote style and value
|
|
400
|
+
let attrValue: string;
|
|
401
|
+
let quoteChar: string;
|
|
402
|
+
|
|
403
|
+
if (doubleQuotedValue !== undefined) {
|
|
404
|
+
attrValue = doubleQuotedValue;
|
|
405
|
+
quoteChar = "\"";
|
|
406
|
+
} else if (singleQuotedValue !== undefined) {
|
|
407
|
+
attrValue = singleQuotedValue;
|
|
408
|
+
quoteChar = "'";
|
|
409
|
+
} else if (unquotedValue !== undefined) {
|
|
410
|
+
attrValue = unquotedValue;
|
|
411
|
+
quoteChar = "\"";
|
|
412
|
+
} else {
|
|
413
|
+
// Boolean attribute (no value)
|
|
414
|
+
if (ALLOWED_ATTRS.has(attrName)) {
|
|
415
|
+
allowedAttrs.push(attrName);
|
|
416
|
+
}
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (ALLOWED_ATTRS.has(attrName)) {
|
|
421
|
+
const wasUnquoted = unquotedValue !== undefined;
|
|
422
|
+
const sanitizedValue = sanitizeAttrValue(attrName, attrValue, wasUnquoted);
|
|
423
|
+
if (sanitizedValue !== null) {
|
|
424
|
+
allowedAttrs.push(`${attrName}=${quoteChar}${sanitizedValue}${quoteChar}`);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const attrsStr = allowedAttrs.length > 0 ? ` ${allowedAttrs.join(" ")}` : "";
|
|
430
|
+
const selfCloseStr = selfClose ? "/>" : ">";
|
|
431
|
+
|
|
432
|
+
return `<${lowerTagName}${attrsStr}${selfCloseStr}`;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Sanitize HTML string to prevent XSS attacks
|
|
437
|
+
* Pure whitelist approach - allowed tags are sanitized, others are escaped
|
|
438
|
+
* @param {string} str Target string value
|
|
439
|
+
* @returns {string} Sanitized string with only allowed elements
|
|
440
|
+
* @private
|
|
441
|
+
*/
|
|
442
|
+
export function sanitize(str: string): string {
|
|
443
|
+
if (typeof str !== "string" || !str || str.indexOf("<") === -1) {
|
|
444
|
+
return str;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Single pass: sanitize allowed tags, escape disallowed ones
|
|
448
|
+
// Also match orphaned fragments like "ipt>" from broken tags
|
|
449
|
+
return str.replace(
|
|
450
|
+
/<\/?[^>]*>|[^<>\s]+>/g,
|
|
451
|
+
match => {
|
|
452
|
+
// Remove HTML comments
|
|
453
|
+
if (match.startsWith("<!--")) {
|
|
454
|
+
return "";
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// Orphaned fragment (e.g., "ipt>") → escape '>'
|
|
458
|
+
if (!match.startsWith("<")) {
|
|
459
|
+
return match.slice(0, -1) + ">";
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// Allowed tag → sanitize attributes
|
|
463
|
+
if (isAllowedTag(match)) {
|
|
464
|
+
return sanitizeTag(match);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// Disallowed tag → escape all '<' to prevent execution
|
|
468
|
+
return match.replace(/</g, "<");
|
|
469
|
+
}
|
|
470
|
+
);
|
|
471
|
+
}
|