forked from bevacqua/insane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanitizer.js
151 lines (134 loc) · 3.28 KB
/
sanitizer.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
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
"use strict";
var he = require("he");
var lowercase = require("./lowercase");
var attributes = require("./attributes");
var elements = require("./elements");
function sanitizer(buffer, options) {
var last;
var context;
var o = options || {};
reset();
return {
start: start,
end: end,
chars: chars,
};
function out(value) {
buffer.push(value);
}
function start(tag, attrs, unary) {
var low = lowercase(tag);
if (context.ignoring) {
ignore(low);
return;
}
if ((o.allowedTags || []).indexOf(low) === -1) {
ignore(low);
return;
}
if (o.filter && !o.filter({ tag: low, attrs: attrs })) {
ignore(low);
return;
}
out("<");
out(low);
Object.keys(attrs).forEach(parse);
out(unary ? "/>" : ">");
function parse(key) {
var value = attrs[key];
var classesOk = (o.allowedClasses || {})[low] || [];
var attrsOk = (o.allowedAttributes || {})[low] || [];
attrsOk = attrsOk.concat((o.allowedAttributes || {})["*"] || []);
var valid;
var lkey = lowercase(key);
if (lkey === "class" && attrsOk.indexOf(lkey) === -1) {
value = value.split(" ").filter(isValidClass).join(" ").trim();
valid = value.length;
} else {
valid =
attrsOk.indexOf(lkey) !== -1 &&
(attributes.uris[lkey] !== true || testUrl(value));
}
if (valid) {
out(" ");
out(key);
if (typeof value === "string") {
out('="');
out(he.encode(value));
out('"');
}
}
function isValidClass(className) {
return classesOk && classesOk.indexOf(className) !== -1;
}
}
}
function end(tag) {
var low = lowercase(tag);
var allowed = (o.allowedTags || []).indexOf(low) !== -1;
if (allowed) {
if (context.ignoring === false) {
out("</");
out(low);
out(">");
} else {
unignore(low);
}
} else {
unignore(low);
}
}
function testUrl(text) {
var start = text[0];
if (start === "#" || start === "/") {
return true;
}
var colon = text.indexOf(":");
if (colon === -1) {
return true;
}
var questionmark = text.indexOf("?");
if (questionmark !== -1 && colon > questionmark) {
return true;
}
var hash = text.indexOf("#");
if (hash !== -1 && colon > hash) {
return true;
}
return o.allowedSchemes.some(matches);
function matches(scheme) {
return text.indexOf(scheme + ":") === 0;
}
}
function chars(text) {
if (context.ignoring === false) {
out(o.transformText ? o.transformText(text) : text);
}
}
function ignore(tag) {
if (o.throwAsValidationError === true) {
throw new Error(
"This code is not fully sanitized. It has disallowed code within."
);
}
if (elements.voids[tag]) {
return;
}
if (context.ignoring === false) {
context = { ignoring: tag, depth: 1 };
} else if (context.ignoring === tag) {
context.depth++;
}
}
function unignore(tag) {
if (context.ignoring === tag) {
if (--context.depth <= 0) {
reset();
}
}
}
function reset() {
context = { ignoring: false, depth: 0 };
}
}
module.exports = sanitizer;