|
3 | 3 | const path = require('path');
|
4 | 4 | const nconf = require('nconf');
|
5 | 5 | const fs = require('fs');
|
| 6 | +const sanitizeHtml = require('sanitize-html'); |
6 | 7 |
|
7 | 8 | const meta = require('../../meta');
|
8 | 9 | const posts = require('../../posts');
|
@@ -121,11 +122,50 @@ uploadsController.uploadCategoryPicture = async function (req, res, next) {
|
121 | 122 | return next(new Error('[[error:invalid-json]]'));
|
122 | 123 | }
|
123 | 124 |
|
| 125 | + if (uploadedFile.path.endsWith('.svg')) { |
| 126 | + await sanitizeSvg(uploadedFile.path); |
| 127 | + } |
| 128 | + |
124 | 129 | await validateUpload(uploadedFile, allowedImageTypes);
|
125 | 130 | const filename = `category-${params.cid}${path.extname(uploadedFile.name)}`;
|
126 | 131 | await uploadImage(filename, 'category', uploadedFile, req, res, next);
|
127 | 132 | };
|
128 | 133 |
|
| 134 | +async function sanitizeSvg(filePath) { |
| 135 | + const dirty = await fs.promises.readFile(filePath, 'utf8'); |
| 136 | + const clean = sanitizeHtml(dirty, { |
| 137 | + allowedTags: [ |
| 138 | + 'svg', 'g', 'defs', 'linearGradient', 'radialGradient', 'stop', |
| 139 | + 'circle', 'ellipse', 'polygon', 'polyline', 'path', 'rect', |
| 140 | + 'line', 'text', 'tspan', 'use', 'symbol', 'clipPath', 'mask', 'pattern', |
| 141 | + 'filter', 'feGaussianBlur', 'feOffset', 'feBlend', 'feColorMatrix', 'feMerge', 'feMergeNode', |
| 142 | + ], |
| 143 | + allowedAttributes: { |
| 144 | + '*': [ |
| 145 | + // Geometry |
| 146 | + 'x', 'y', 'x1', 'x2', 'y1', 'y2', 'cx', 'cy', 'r', 'rx', 'ry', |
| 147 | + 'width', 'height', 'd', 'points', 'viewBox', 'transform', |
| 148 | + |
| 149 | + // Presentation |
| 150 | + 'fill', 'stroke', 'stroke-width', 'opacity', |
| 151 | + 'stop-color', 'stop-opacity', 'offset', 'style', 'class', |
| 152 | + |
| 153 | + // Text |
| 154 | + 'text-anchor', 'font-size', 'font-family', |
| 155 | + |
| 156 | + // Misc |
| 157 | + 'id', 'clip-path', 'mask', 'filter', 'gradientUnits', 'gradientTransform', |
| 158 | + 'xmlns', 'preserveAspectRatio', |
| 159 | + ], |
| 160 | + }, |
| 161 | + parser: { |
| 162 | + lowerCaseTags: false, |
| 163 | + lowerCaseAttributeNames: false, |
| 164 | + }, |
| 165 | + }); |
| 166 | + await fs.promises.writeFile(filePath, clean); |
| 167 | +} |
| 168 | + |
129 | 169 | uploadsController.uploadFavicon = async function (req, res, next) {
|
130 | 170 | const uploadedFile = req.files.files[0];
|
131 | 171 | const allowedTypes = ['image/x-icon', 'image/vnd.microsoft.icon'];
|
|
0 commit comments