Merge commit from fork

232cf6d8255e772a55f64d965c6beb85cfbe931d

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
2 files changed, +17 -5Showing whitespace changes
src/endpoints/assets.js+15 -3
@@ -8,7 +8,8 @@ import sanitize from 'sanitize-filename';
88import fetch from 'node-fetch';
99
1010import { UNSAFE_EXTENSIONS } from '../constants.js';
1111import { clientRelativePath, isValidUrl } from '../util.js';
12+import { getHostFromUrl, isHostWhitelisted } from './content-manager.js';
1213
1314const VALID_CATEGORIES = ['bgm', 'ambient', 'blip', 'live2d', 'vrm', 'character', 'temp'];
1415
@@ -189,9 +190,21 @@ router.post('/get', async (request, response) => {
189190 * @returns {void}
190191 */
191192router.post('/download', async (request, response) => {
192- const url = request.body.url;
193+ try {
194+ if (!isValidUrl(request.body.url)) {
195+ console.warn('Asset download failed: Must be a valid URL');
196+ return response.sendStatus(400);
197+ }
198+
199+ const url = String(request.body.url);
193200 const inputCategory = request.body.category;
194201
202+ const host = getHostFromUrl(url);
203+ if (!isHostWhitelisted(host)) {
204+ console.error(`Received an import for "${host}", but site is not whitelisted. This domain must be added to the config key "whitelistImportDomains" to allow import from this source.`);
205+ return response.sendStatus(404);
206+ }
207+
195208 // Check category
196209 let category = null;
197210 for (let i of VALID_CATEGORIES)
@@ -213,7 +226,6 @@ router.post('/download', async (request, response) => {
213226 const file_path = path.join(request.user.directories.assets, category, request.body.filename);
214227 console.info('Request received to download', url, 'to', file_path);
215228
216- try {
217229 // Download to temp
218230 const res = await fetch(url);
219231 if (!res.ok || res.body === null) {
src/endpoints/content-manager.js+2 -2
@@ -872,7 +872,7 @@ function getUuidFromUrl(url) {
872872 * @param {String} url URL to strip
873873 * @returns {String} Domain name
874874 */
875875export function getHostFromUrl(url) {
876876 try {
877877 const urlObj = new URL(url);
878878 return urlObj.hostname;
@@ -886,7 +886,7 @@ function getHostFromUrl(url) {
886886 * @param {String} host Host to check
887887 * @returns {boolean} If the host is on the whitelist.
888888 */
889889export function isHostWhitelisted(host) {
890890 return WHITELIST_GENERIC_URL_DOWNLOAD_SOURCES.includes(host);
891891}
892892