-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
321 lines (306 loc) · 8.24 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* Copyright (c) 2019 Environmental Systems Research Institute, Inc.
* Apache-2.0
*
* js-xss
* Copyright (c) 2012-2017 Zongmin Lei(雷宗民) <leizongmin@gmail.com>
* http://ucdok.com
* The MIT License, see
* https://github.com/leizongmin/js-xss/blob/master/LICENSE for details
*
* Lodash/isPlainObject
* Copyright (c) JS Foundation and other contributors <https://js.foundation/>
* MIT License, see https://raw.githubusercontent.com/lodash/lodash/4.17.10-npm/LICENSE for details
* */
import isPlainObject from 'lodash.isplainobject';
import xss from 'xss';
/**
* The response from the validate method
*
* @export
* @interface IValidationResponse
*/
export interface IValidationResponse {
isValid: boolean;
sanitized: any;
}
/**
* The Sanitizer Class
*
* @export
* @class Sanitizer
*/
export class Sanitizer {
// Supported HTML Spec: https://doc.arcgis.com/en/arcgis-online/reference/supported-html.htm
public readonly arcgisWhiteList: XSS.IWhiteList = {
a: ['href', 'target', 'style'],
img: ['src', 'width', 'height', 'border', 'alt', 'style'],
video: [
'autoplay',
'controls',
'height',
'loop',
'muted',
'poster',
'preload',
'src',
'width'
],
audio: ['autoplay', 'controls', 'loop', 'muted', 'preload', 'src'],
span: ['style'],
table: ['width', 'height', 'cellpadding', 'cellspacing', 'border', 'style'],
div: ['style', 'align'],
font: ['size', 'color', 'style'],
tr: ['height', 'valign', 'align', 'style'],
td: [
'height',
'width',
'valign',
'align',
'colspan',
'rowspan',
'nowrap',
'style'
],
th: [
'height',
'width',
'valign',
'align',
'colspan',
'rowspan',
'nowrap',
'style'
],
p: ['style'],
b: [],
strong: [],
i: [],
em: [],
u: [],
br: [],
li: [],
ul: [],
ol: [],
hr: [],
tbody: []
};
public readonly allowedProtocols: string[] = [
'http',
'https',
'mailto',
'iform',
'tel',
'flow',
'lfmobile',
'arcgis-navigator',
'arcgis-appstudio-player',
'arcgis-survey123',
'arcgis-collector',
'arcgis-workforce',
'arcgis-explorer',
'arcgis-trek2there',
'mspbi',
'comgooglemaps',
'pdfefile',
'pdfehttp',
'pdfehttps',
'boxapp',
'boxemm',
'awb',
'awbs',
'gropen',
'radarscope'
];
public readonly arcgisFilterOptions: XSS.IFilterXSSOptions = {
allowCommentTag: true,
safeAttrValue: (
tag: string,
name: string,
value: string,
cssFilter: XSS.ICSSFilter
): string => {
// Take over safe attribute filtering for `a` `href`, `img` `src`,
// `audio` `src`, and `video` `src` attributes, otherwise pass onto the
// default `XSS.safeAttrValue` method.
if (
(tag === 'a' && name === 'href') ||
((tag === 'img' || tag === 'audio' || tag === 'video') && name === 'src')
) {
return this.sanitizeUrl(value);
}
return xss.safeAttrValue(tag, name, value, cssFilter);
}
};
public readonly xssFilterOptions: XSS.IFilterXSSOptions;
private _xssFilter: XSS.ICSSFilter;
constructor(filterOptions?: XSS.IFilterXSSOptions, extendDefaults?: boolean) {
let xssFilterOptions: XSS.IFilterXSSOptions;
if (filterOptions && !extendDefaults) {
// Override the defaults
xssFilterOptions = filterOptions;
} else if (filterOptions && extendDefaults) {
// Extend the defaults
xssFilterOptions = Object.create(this.arcgisFilterOptions);
Object.keys(filterOptions).forEach(key => {
if (key === 'whiteList') {
// Extend the whitelist by concatenating arrays
xssFilterOptions.whiteList = this._extendObjectOfArrays([
this.arcgisWhiteList,
filterOptions.whiteList || {}
]);
} else {
xssFilterOptions[key] = filterOptions[key];
}
});
} else {
// Only use the defaults
xssFilterOptions = Object.create(this.arcgisFilterOptions);
xssFilterOptions.whiteList = this.arcgisWhiteList;
}
this.xssFilterOptions = xssFilterOptions;
// Make this readable to tests
this._xssFilter = new xss.FilterXSS(xssFilterOptions);
}
/**
* Sanitizes value to remove invalid HTML tags.
*
* Note: If the value passed does not contain a valid JSON data type (String,
* Number, JSON Object, Array, Boolean, or null), the value will be nullified.
*
* @param {any} value The value to sanitize.
* @returns {any} The sanitized value.
* @memberof Sanitizer
*/
public sanitize(value: any): any {
switch (typeof value) {
case 'number':
if (isNaN(value) || !isFinite(value)) {
return null;
}
return value;
case 'boolean':
return value;
case 'string':
return this._xssFilter.process(value);
case 'object':
return this._iterateOverObject(value);
default:
return null;
}
}
/**
* Sanitizes a URL string following the allowed protocols and sanitization rules.
*
* @param {string} value The URL to sanitize.
* @returns {string} The sanitized URL.
*/
public sanitizeUrl(value: string): string {
const protocol = this._trim(value.substring(0, value.indexOf(':')));
if (
!(
value === '/' ||
value === '#' ||
value[0] === '#' ||
this.allowedProtocols.indexOf(protocol.toLowerCase()) > -1
)
) {
return '';
} else {
return xss.escapeAttrValue(value);
}
}
/**
* Checks if a value only contains valid HTML.
*
* @param {any} value The value to validate.
* @returns {boolean}
* @memberof Sanitizer
*/
public validate(value: any): IValidationResponse {
const sanitized = this.sanitize(value);
return {
isValid: value === sanitized,
sanitized
};
}
/**
* Extends an object of arrays by by concatenating arrays of the same object
* keys. If the if the previous key's value is not an array, the next key's
* value will replace the previous key. This method is used for extending the
* whiteList in the XSS filter options.
*
* @private
* @param {Array<{}>} objects An array of objects.
* @returns {{}} The extended object.
* @memberof Sanitizer
*/
private _extendObjectOfArrays(objects: Array<{}>): {} {
const finalObj = {};
objects.forEach(obj => {
Object.keys(obj).forEach(key => {
if (Array.isArray(obj[key]) && Array.isArray(finalObj[key])) {
finalObj[key] = finalObj[key].concat(obj[key]);
} else {
finalObj[key] = obj[key];
}
});
});
return finalObj;
}
/**
* Iterate over a plain object or array to deeply sanitize each value.
*
* @private
* @param {object} obj The object to iterate over.
* @returns {(object | null)} The sanitized object.
* @memberof Sanitizer
*/
private _iterateOverObject(obj: object): object | null {
try {
let hasChanged = false;
let changedObj;
if (Array.isArray(obj)) {
changedObj = obj.reduce((prev, value) => {
const validation = this.validate(value);
if (validation.isValid) {
return prev.concat([value]);
} else {
hasChanged = true;
return prev.concat([validation.sanitized]);
}
}, []);
} else if (!isPlainObject(obj)) {
return null;
} else {
const keys = Object.keys(obj);
changedObj = keys.reduce((prev, key) => {
const value = obj[key];
const validation = this.validate(value);
if (validation.isValid) {
prev[key] = value;
} else {
hasChanged = true;
prev[key] = validation.sanitized;
}
return prev;
}, {});
}
if (hasChanged) {
return changedObj;
}
return obj;
} catch (err) {
return null;
}
}
/**
* Trim whitespace from the start and ends of a string.
* @param {string} val The string to trim.
* @returns {string} The trimmed string.
*/
private _trim(val: string): string {
return String.prototype.trim
? val.trim()
: val.replace(/(^\s*)|(\s*$)/g, '');
}
}