François de Metz 49ce4649cc
Encode the URL before fetching it. (#7934)
Signed-off-by: François de Metz <francois@2metz.fr>
2020-09-18 10:05:34 +02:00

43 lines
988 B
JavaScript

'use strict';
const _ = require('lodash');
const fetch = require('node-fetch');
const isValidDomain = require('is-valid-domain');
module.exports = {
async uploadProxy(ctx) {
try {
const url = new URL(ctx.query.url);
if (!['http:', 'https:'].includes(url.protocol)) {
throw new Error('Unexpected url protocol');
}
if (!isValidDomain(url.hostname)) {
throw new Error('Invalid url hostname');
}
} catch (err) {
ctx.status = 400;
ctx.body = 'Invalid URL';
return;
}
try {
const res = await fetch(new URL(ctx.query.url), {
headers: _.omit(ctx.request.headers, ['origin', 'host', 'authorization']),
});
for (const [key, value] of res.headers.entries()) {
ctx.set(key, value);
}
ctx.status = res.status;
ctx.body = res.body;
} catch (err) {
strapi.log.error(err);
ctx.status = 500;
ctx.body = 'Internal Server Error';
}
},
};