@blocklet/xss 0.1.26 → 0.1.27
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/cjs/index.d.ts +5 -0
- package/cjs/index.js +3 -1
- package/cjs/utils.d.ts +5 -0
- package/cjs/utils.js +83 -2
- package/es/index.d.ts +5 -0
- package/es/index.js +4 -2
- package/es/utils.d.ts +5 -0
- package/es/utils.js +79 -0
- package/package.json +1 -1
package/cjs/index.d.ts
CHANGED
|
@@ -4,5 +4,10 @@ export declare function xss(options?: SanitizeOptions): (req: any, res: any, nex
|
|
|
4
4
|
declare const _default: {
|
|
5
5
|
xss: typeof xss;
|
|
6
6
|
initSanitize: (_options?: SanitizeOptions) => any;
|
|
7
|
+
isSvgFile: (svgContent: string, file?: {
|
|
8
|
+
name?: string;
|
|
9
|
+
type?: string;
|
|
10
|
+
}) => boolean;
|
|
11
|
+
sanitizeSvg: (svgContent: string) => string;
|
|
7
12
|
};
|
|
8
13
|
export default _default;
|
package/cjs/index.js
CHANGED
package/cjs/utils.d.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
1
|
import { SanitizeOptions } from './types';
|
|
2
2
|
export declare const initSanitize: (_options?: SanitizeOptions) => any;
|
|
3
|
+
export declare const sanitizeSvg: (svgContent: string) => string;
|
|
4
|
+
export declare const isSvgFile: (svgContent: string, file?: {
|
|
5
|
+
name?: string;
|
|
6
|
+
type?: string;
|
|
7
|
+
}) => boolean;
|
package/cjs/utils.js
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.initSanitize = void 0;
|
|
6
|
+
exports.sanitizeSvg = exports.isSvgFile = exports.initSanitize = void 0;
|
|
7
7
|
var xss = _interopRequireWildcard(require("xss"));
|
|
8
8
|
var _omit = _interopRequireDefault(require("lodash/omit"));
|
|
9
|
+
var _path = _interopRequireDefault(require("path"));
|
|
9
10
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
11
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
11
12
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
@@ -64,4 +65,84 @@ const initSanitize = (_options = {}) => {
|
|
|
64
65
|
};
|
|
65
66
|
return sanitize;
|
|
66
67
|
};
|
|
67
|
-
exports.initSanitize = initSanitize;
|
|
68
|
+
exports.initSanitize = initSanitize;
|
|
69
|
+
const svgWhiteList = {
|
|
70
|
+
svg: ["width", "height", "viewBox", "xmlns", "version", "preserveAspectRatio", "xml:space"],
|
|
71
|
+
circle: ["cx", "cy", "r", "fill", "stroke", "stroke-width", "fill-opacity", "stroke-opacity"],
|
|
72
|
+
ellipse: ["cx", "cy", "rx", "ry", "fill", "stroke", "stroke-width"],
|
|
73
|
+
line: ["x1", "y1", "x2", "y2", "stroke", "stroke-width"],
|
|
74
|
+
path: ["d", "fill", "stroke", "stroke-width", "fill-rule", "stroke-linecap", "stroke-linejoin"],
|
|
75
|
+
polygon: ["points", "fill", "stroke", "stroke-width"],
|
|
76
|
+
polyline: ["points", "fill", "stroke", "stroke-width"],
|
|
77
|
+
rect: ["x", "y", "width", "height", "rx", "ry", "fill", "stroke", "stroke-width"],
|
|
78
|
+
g: ["transform", "fill", "stroke"],
|
|
79
|
+
text: ["x", "y", "font-size", "font-family", "text-anchor", "fill"],
|
|
80
|
+
defs: [],
|
|
81
|
+
clipPath: ["id"],
|
|
82
|
+
mask: ["id"],
|
|
83
|
+
use: ["x", "y", "width", "height"],
|
|
84
|
+
linearGradient: ["id", "x1", "y1", "x2", "y2", "gradientUnits"],
|
|
85
|
+
radialGradient: ["id", "cx", "cy", "r", "fx", "fy", "gradientUnits"],
|
|
86
|
+
stop: ["offset", "stop-color", "stop-opacity"],
|
|
87
|
+
pattern: ["id", "width", "height", "patternUnits", "patternTransform"]
|
|
88
|
+
};
|
|
89
|
+
const svgSanitizeOptions = {
|
|
90
|
+
whiteList: svgWhiteList,
|
|
91
|
+
stripIgnoreTagBody: ["script", "style"],
|
|
92
|
+
onIgnoreTag: function (tag, html, options) {
|
|
93
|
+
return "";
|
|
94
|
+
},
|
|
95
|
+
onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) {
|
|
96
|
+
if (name.startsWith("on") || name === "href" || name === "xlink:href") {
|
|
97
|
+
return "";
|
|
98
|
+
}
|
|
99
|
+
if (name === "style") {
|
|
100
|
+
const safeValue = value.replace(/expression\(.*\)|javascript:|data:|@import|behavior|binding|moz-binding/gi, "");
|
|
101
|
+
if (safeValue !== value) {
|
|
102
|
+
return "";
|
|
103
|
+
}
|
|
104
|
+
return `${name}="${safeValue}"`;
|
|
105
|
+
}
|
|
106
|
+
if (tag === "use" && (name === "href" || name === "xlink:href")) {
|
|
107
|
+
if (value.startsWith("#") && !/[<>"']/.test(value)) {
|
|
108
|
+
return `${name}="${value}"`;
|
|
109
|
+
}
|
|
110
|
+
return "";
|
|
111
|
+
}
|
|
112
|
+
if (name === "id" || name === "class") {
|
|
113
|
+
return `${name}="${value}"`;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const sanitizeSvg = svgContent => {
|
|
118
|
+
const isSvg = isSvgFile(svgContent);
|
|
119
|
+
if (!isSvg) {
|
|
120
|
+
throw new Error("Invalid SVG content");
|
|
121
|
+
}
|
|
122
|
+
const xssInstance = new xss.FilterXSS(svgSanitizeOptions);
|
|
123
|
+
return xssInstance.process(svgContent);
|
|
124
|
+
};
|
|
125
|
+
exports.sanitizeSvg = sanitizeSvg;
|
|
126
|
+
const isSvgFile = (svgContent, file) => {
|
|
127
|
+
if (typeof svgContent !== "string") {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
const svgRegex = /<svg[^>]*?(?:>|\/>)|<\?xml[^>]*>\s*<svg[^>]*?(?:>|\/?>)/i;
|
|
131
|
+
const isSvg = svgRegex.test(svgContent);
|
|
132
|
+
if (!isSvg) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
if (file?.name) {
|
|
136
|
+
const ext = _path.default.extname(file.name).toLowerCase();
|
|
137
|
+
if (ext !== ".svg") {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (file?.type) {
|
|
142
|
+
if (!file.type.toLowerCase().includes("image/svg")) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return true;
|
|
147
|
+
};
|
|
148
|
+
exports.isSvgFile = isSvgFile;
|
package/es/index.d.ts
CHANGED
|
@@ -4,5 +4,10 @@ export declare function xss(options?: SanitizeOptions): (req: any, res: any, nex
|
|
|
4
4
|
declare const _default: {
|
|
5
5
|
xss: typeof xss;
|
|
6
6
|
initSanitize: (_options?: SanitizeOptions) => any;
|
|
7
|
+
isSvgFile: (svgContent: string, file?: {
|
|
8
|
+
name?: string;
|
|
9
|
+
type?: string;
|
|
10
|
+
}) => boolean;
|
|
11
|
+
sanitizeSvg: (svgContent: string) => string;
|
|
7
12
|
};
|
|
8
13
|
export default _default;
|
package/es/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { initSanitize } from "./utils.js";
|
|
1
|
+
import { initSanitize, isSvgFile, sanitizeSvg } from "./utils.js";
|
|
2
2
|
export * from "./utils.js";
|
|
3
3
|
export function xss(options = {}) {
|
|
4
4
|
const sanitize = initSanitize(options);
|
|
@@ -13,5 +13,7 @@ export function xss(options = {}) {
|
|
|
13
13
|
}
|
|
14
14
|
export default {
|
|
15
15
|
xss,
|
|
16
|
-
initSanitize
|
|
16
|
+
initSanitize,
|
|
17
|
+
isSvgFile,
|
|
18
|
+
sanitizeSvg
|
|
17
19
|
};
|
package/es/utils.d.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
1
|
import { SanitizeOptions } from './types';
|
|
2
2
|
export declare const initSanitize: (_options?: SanitizeOptions) => any;
|
|
3
|
+
export declare const sanitizeSvg: (svgContent: string) => string;
|
|
4
|
+
export declare const isSvgFile: (svgContent: string, file?: {
|
|
5
|
+
name?: string;
|
|
6
|
+
type?: string;
|
|
7
|
+
}) => boolean;
|
package/es/utils.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as xss from "xss";
|
|
2
2
|
import omit from "lodash/omit";
|
|
3
|
+
import path from "path";
|
|
3
4
|
const ignoreTagList = [
|
|
4
5
|
// here is a blacklist
|
|
5
6
|
"script",
|
|
@@ -79,3 +80,81 @@ export const initSanitize = (_options = {}) => {
|
|
|
79
80
|
};
|
|
80
81
|
return sanitize;
|
|
81
82
|
};
|
|
83
|
+
const svgWhiteList = {
|
|
84
|
+
svg: ["width", "height", "viewBox", "xmlns", "version", "preserveAspectRatio", "xml:space"],
|
|
85
|
+
circle: ["cx", "cy", "r", "fill", "stroke", "stroke-width", "fill-opacity", "stroke-opacity"],
|
|
86
|
+
ellipse: ["cx", "cy", "rx", "ry", "fill", "stroke", "stroke-width"],
|
|
87
|
+
line: ["x1", "y1", "x2", "y2", "stroke", "stroke-width"],
|
|
88
|
+
path: ["d", "fill", "stroke", "stroke-width", "fill-rule", "stroke-linecap", "stroke-linejoin"],
|
|
89
|
+
polygon: ["points", "fill", "stroke", "stroke-width"],
|
|
90
|
+
polyline: ["points", "fill", "stroke", "stroke-width"],
|
|
91
|
+
rect: ["x", "y", "width", "height", "rx", "ry", "fill", "stroke", "stroke-width"],
|
|
92
|
+
g: ["transform", "fill", "stroke"],
|
|
93
|
+
text: ["x", "y", "font-size", "font-family", "text-anchor", "fill"],
|
|
94
|
+
defs: [],
|
|
95
|
+
clipPath: ["id"],
|
|
96
|
+
mask: ["id"],
|
|
97
|
+
use: ["x", "y", "width", "height"],
|
|
98
|
+
linearGradient: ["id", "x1", "y1", "x2", "y2", "gradientUnits"],
|
|
99
|
+
radialGradient: ["id", "cx", "cy", "r", "fx", "fy", "gradientUnits"],
|
|
100
|
+
stop: ["offset", "stop-color", "stop-opacity"],
|
|
101
|
+
pattern: ["id", "width", "height", "patternUnits", "patternTransform"]
|
|
102
|
+
};
|
|
103
|
+
const svgSanitizeOptions = {
|
|
104
|
+
whiteList: svgWhiteList,
|
|
105
|
+
stripIgnoreTagBody: ["script", "style"],
|
|
106
|
+
onIgnoreTag: function(tag, html, options) {
|
|
107
|
+
return "";
|
|
108
|
+
},
|
|
109
|
+
onIgnoreTagAttr: function(tag, name, value, isWhiteAttr) {
|
|
110
|
+
if (name.startsWith("on") || name === "href" || name === "xlink:href") {
|
|
111
|
+
return "";
|
|
112
|
+
}
|
|
113
|
+
if (name === "style") {
|
|
114
|
+
const safeValue = value.replace(/expression\(.*\)|javascript:|data:|@import|behavior|binding|moz-binding/gi, "");
|
|
115
|
+
if (safeValue !== value) {
|
|
116
|
+
return "";
|
|
117
|
+
}
|
|
118
|
+
return `${name}="${safeValue}"`;
|
|
119
|
+
}
|
|
120
|
+
if (tag === "use" && (name === "href" || name === "xlink:href")) {
|
|
121
|
+
if (value.startsWith("#") && !/[<>"']/.test(value)) {
|
|
122
|
+
return `${name}="${value}"`;
|
|
123
|
+
}
|
|
124
|
+
return "";
|
|
125
|
+
}
|
|
126
|
+
if (name === "id" || name === "class") {
|
|
127
|
+
return `${name}="${value}"`;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
export const sanitizeSvg = (svgContent) => {
|
|
132
|
+
const isSvg = isSvgFile(svgContent);
|
|
133
|
+
if (!isSvg) {
|
|
134
|
+
throw new Error("Invalid SVG content");
|
|
135
|
+
}
|
|
136
|
+
const xssInstance = new xss.FilterXSS(svgSanitizeOptions);
|
|
137
|
+
return xssInstance.process(svgContent);
|
|
138
|
+
};
|
|
139
|
+
export const isSvgFile = (svgContent, file) => {
|
|
140
|
+
if (typeof svgContent !== "string") {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
const svgRegex = /<svg[^>]*?(?:>|\/>)|<\?xml[^>]*>\s*<svg[^>]*?(?:>|\/?>)/i;
|
|
144
|
+
const isSvg = svgRegex.test(svgContent);
|
|
145
|
+
if (!isSvg) {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
if (file?.name) {
|
|
149
|
+
const ext = path.extname(file.name).toLowerCase();
|
|
150
|
+
if (ext !== ".svg") {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (file?.type) {
|
|
155
|
+
if (!file.type.toLowerCase().includes("image/svg")) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
};
|