-
Notifications
You must be signed in to change notification settings - Fork 1
/
com_zimbra_dompurify.js
81 lines (75 loc) · 2.37 KB
/
com_zimbra_dompurify.js
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
/**
* com_zimbra_dompurify Zimlet
*/
function com_zimbra_dompurify_handler () {
/**
* Return the first element named elem starting with root.
* This is a recursive function.
* Returns: undefined if cannot locate.
*/
var findElement = function (root, elem) {
var val = undefined;
Object.keys(root).some(function (k) {
if (k === elem) {
val = root[k];
return true;
}
else if (typeof root[k] === "object") {
val = findElement(root[k], elem);
return val !== undefined;
}
return false;
});
return val;
};
var sanitizeMessageParts = function (mp) {
return mp.map(function (v, i, a) {
if (v.ct === "text/html" && v.content) {
if (v.content._content) {
v.content._content = DOMPurify.sanitize(v.content._content);
}
else {
v.content = DOMPurify.sanitize(v.content);
}
}
else if (v.mp) {
v.mp = sanitizeMessageParts(v.mp);
}
return v;
});
};
/**
* Recursively search through the supplied argument and
* look for any mp arrays that are embedded in an m or
* inside another mp array. Sanitize any elements whose
* ct is "text.html"
*/
var sanitizeTextHtml = function (j) {
var message = findElement(j, "m");
if (message !== undefined) {
var messageParts = findElement(message, "mp");
if (messageParts !== undefined) {
messageParts = sanitizeMessageParts(messageParts);
}
}
return j;
};
var saveRun = AjxCallback.prototype.run;
AjxCallback.prototype.run = function (args) {
if (args && args.text) {
try {
var v = JSON.parse(args.text);
if (v.Body) {
v.Body = sanitizeTextHtml(v.Body);
args.text = JSON.stringify(v);
}
}
catch (err) {
console.log(err);
}
}
saveRun.call(this, args);
};
}
com_zimbra_dompurify_handler.prototype = new ZmZimletBase();
com_zimbra_dompurify_handler.prototype.constructor = com_zimbra_dompurify_handler;