0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Handled bad URLs in oembed bookmark API

fixes #11636

- malformed URLs passed to oembed API would cause `got` or `metascraper`
  to throw an error and this would result in a 500 error from Ghost
- this commit catches the errors and returns a reasonable response
This commit is contained in:
Daniel Lockyer 2020-03-02 14:24:26 +00:00
parent f7bc233f4c
commit a851cdfc7b

View file

@ -16,15 +16,22 @@ async function fetchBookmarkData(url, html) {
require('metascraper-logo')()
]);
if (!html) {
const response = await request(url, {
headers: {
'user-agent': 'Ghost(https://github.com/TryGhost/Ghost)'
}
});
html = response.body;
let scraperResponse;
try {
if (!html) {
const response = await request(url, {
headers: {
'user-agent': 'Ghost(https://github.com/TryGhost/Ghost)'
}
});
html = response.body;
}
scraperResponse = await metascraper({html, url});
} catch (e) {
return Promise.reject();
}
const scraperResponse = await metascraper({html, url});
const metadata = Object.assign({}, scraperResponse, {
thumbnail: scraperResponse.image,
icon: scraperResponse.logo
@ -132,7 +139,8 @@ module.exports = {
let {url, type} = data;
if (type === 'bookmark') {
return fetchBookmarkData(url);
return fetchBookmarkData(url)
.catch(() => unknownProvider(url));
}
return fetchOembedData(url).then((response) => {