@capillarytech/cap-ui-utils 1.4.24 → 1.4.25-alpha.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/package.json
CHANGED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { JSDOM } from 'jsdom';
|
|
2
|
+
import createDOMPurify from 'dompurify';
|
|
3
|
+
import isEmpty from 'lodash/isEmpty';
|
|
4
|
+
|
|
5
|
+
export const sanitizeTemplateWithDomPurify = (content) => {
|
|
6
|
+
const window = new JSDOM('<textarea id="a1" />').window;
|
|
7
|
+
const textElement = window.document.getElementById("a1");
|
|
8
|
+
textElement.innerHTML = content;
|
|
9
|
+
const DOMPurify = createDOMPurify(window);
|
|
10
|
+
return DOMPurify.sanitize(content);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export const sanitizeTemplateWithRegexp = (content) => {
|
|
15
|
+
const forbiddenTags = process.env.TMPL_SANITIZE_FORBIDDEN_TAGS ? JSON.parse(process.env.TMPL_SANITIZE_FORBIDDEN_TAGS) : ['script'];
|
|
16
|
+
const forbiddenAttrs = process.env.TMPL_SANITIZE_FORBIDDEN_ATTRS ? JSON.parse(process.env.TMPL_SANITIZE_FORBIDDEN_ATTRS) : ['onclick', 'onmouseover', 'onmouseout'];
|
|
17
|
+
const forbiddenHrefJavaScript = ["javascript:"];
|
|
18
|
+
const tagsJoined = forbiddenTags.join('|');
|
|
19
|
+
const attrsJoined = forbiddenAttrs.join('|');
|
|
20
|
+
const hrefsJoined = forbiddenHrefJavaScript.join('|');
|
|
21
|
+
const fullTagRegex = new RegExp(`<${tagsJoined}\\b[^<]*(?:(?!<\/${tagsJoined}>)<[^<]*)*<\/${tagsJoined}>`, 'gi');
|
|
22
|
+
const emptyTagRegex = new RegExp(`<${tagsJoined}.*\/?>`, 'gi');
|
|
23
|
+
const attrRegex = new RegExp(`\\b(${attrsJoined})\\s*=\\s*(\"[^\"]*\"|'[^']*'|[^>|&|\/\\s]+)`, 'gi');
|
|
24
|
+
const hrefJavaScriptRegex = new RegExp(`href\\s*=\\s*(["\'])(?:\\*?.)*?(?:\\?\\")?${hrefsJoined}(?:\\\\?.)*?\\1`, 'gi');
|
|
25
|
+
const hrefRegexForJsonString = new RegExp(`href=\\\\["\']${hrefsJoined}.*?\\\\["\']`, 'gi'); // This is to find the href in json format for BEE editor
|
|
26
|
+
const encodedScriptTagRegex = new RegExp(`(?:<|<)${tagsJoined}(?:\s[^>]*|)[^*]*>`, 'gi');
|
|
27
|
+
|
|
28
|
+
return content
|
|
29
|
+
.replaceAll(fullTagRegex, '')
|
|
30
|
+
.replaceAll(encodedScriptTagRegex, '')
|
|
31
|
+
.replaceAll(emptyTagRegex, '')
|
|
32
|
+
.replaceAll(attrRegex, '')
|
|
33
|
+
.replaceAll(hrefJavaScriptRegex, 'href="#"')
|
|
34
|
+
.replaceAll(hrefRegexForJsonString, 'href=\\\"#\\\"'); // This will ensure to remain the content in json format for BEE editor
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const sanitizeEmailTemplateAndSaveInData = data => {
|
|
38
|
+
var content = data?.versions?.base?.[data?.versions?.base?.activeTab]['template-content'];
|
|
39
|
+
var sanitizedContent = sanitizeTemplateWithRegexp(content);
|
|
40
|
+
const newData = {
|
|
41
|
+
...data,
|
|
42
|
+
versions: {
|
|
43
|
+
...data.versions,
|
|
44
|
+
base: {
|
|
45
|
+
...data.versions.base,
|
|
46
|
+
[data.versions.base.activeTab]: {
|
|
47
|
+
...data.versions.base[data.versions.base.activeTab],
|
|
48
|
+
'template-content': sanitizedContent
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
history: [{
|
|
52
|
+
...data.versions.history[0],
|
|
53
|
+
[data.versions.history[0].activeTab]: {
|
|
54
|
+
...data.versions.history[0][data.versions.history[0].activeTab],
|
|
55
|
+
'template-content': sanitizedContent
|
|
56
|
+
}
|
|
57
|
+
}]
|
|
58
|
+
},
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// To handle in BEE editor. // is_drag_drop=true means BEE editor is enabled
|
|
62
|
+
if (data?.versions?.base?.[data?.versions?.base?.activeTab]?.is_drag_drop) {
|
|
63
|
+
const jsonContent = data?.versions?.base?.[data?.versions?.base?.activeTab]?.['json-content'];
|
|
64
|
+
if (!isEmpty(jsonContent)) {
|
|
65
|
+
const sanitizedJsonContent = sanitizeTemplateWithRegexp(jsonContent);
|
|
66
|
+
newData.versions.base[newData?.versions?.base?.activeTab]['json-content'] = sanitizedJsonContent;
|
|
67
|
+
newData.versions.history[0][newData?.versions?.history?.[0]?.activeTab]['json-content'] = sanitizedJsonContent;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return { sanitizedContent, newData };
|
|
71
|
+
};
|