From a851cdfc7ba089bf111a9abb60b814cbd5c047b2 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Mon, 2 Mar 2020 14:24:26 +0000 Subject: [PATCH] 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 --- core/server/api/canary/oembed.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/core/server/api/canary/oembed.js b/core/server/api/canary/oembed.js index 59f3bbc780..de45e58f10 100644 --- a/core/server/api/canary/oembed.js +++ b/core/server/api/canary/oembed.js @@ -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) => {