2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2019-08-09 17:11:24 +03:00
|
|
|
const {extract, hasProvider} = require('oembed-parser');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const cheerio = require('cheerio');
|
2020-03-03 15:47:18 +03:00
|
|
|
const _ = require('lodash');
|
2020-06-17 12:03:36 +03:00
|
|
|
const {CookieJar} = require('tough-cookie');
|
2020-06-15 20:38:43 +03:00
|
|
|
const config = require('../../../shared/config');
|
|
|
|
const {i18n} = require('../../lib/common');
|
|
|
|
const externalRequest = require('../../lib/request-external');
|
2019-08-27 17:01:02 +03:00
|
|
|
|
2020-10-06 10:44:03 +03:00
|
|
|
async function fetchBookmarkData(url) {
|
2019-11-06 04:31:11 +03:00
|
|
|
const metascraper = require('metascraper')([
|
|
|
|
require('metascraper-url')(),
|
|
|
|
require('metascraper-title')(),
|
|
|
|
require('metascraper-description')(),
|
|
|
|
require('metascraper-author')(),
|
|
|
|
require('metascraper-publisher')(),
|
|
|
|
require('metascraper-image')(),
|
|
|
|
require('metascraper-logo-favicon')(),
|
|
|
|
require('metascraper-logo')()
|
|
|
|
]);
|
|
|
|
|
2020-03-02 17:24:26 +03:00
|
|
|
let scraperResponse;
|
|
|
|
|
|
|
|
try {
|
2020-10-06 10:44:03 +03:00
|
|
|
const cookieJar = new CookieJar();
|
|
|
|
const response = await externalRequest(url, {cookieJar});
|
|
|
|
const html = response.body;
|
2020-03-02 17:24:26 +03:00
|
|
|
scraperResponse = await metascraper({html, url});
|
2020-04-15 12:35:22 +03:00
|
|
|
} catch (err) {
|
|
|
|
return Promise.reject(err);
|
2019-08-27 17:01:02 +03:00
|
|
|
}
|
2020-03-02 17:24:26 +03:00
|
|
|
|
2019-08-27 17:01:02 +03:00
|
|
|
const metadata = Object.assign({}, scraperResponse, {
|
|
|
|
thumbnail: scraperResponse.image,
|
|
|
|
icon: scraperResponse.logo
|
|
|
|
});
|
|
|
|
// We want to use standard naming for image and logo
|
|
|
|
delete metadata.image;
|
|
|
|
delete metadata.logo;
|
|
|
|
|
2020-06-05 19:27:01 +03:00
|
|
|
if (metadata.title) {
|
2019-08-27 17:01:02 +03:00
|
|
|
return Promise.resolve({
|
|
|
|
type: 'bookmark',
|
|
|
|
url,
|
|
|
|
metadata
|
|
|
|
});
|
|
|
|
}
|
2020-06-08 17:06:00 +03:00
|
|
|
|
|
|
|
return Promise.reject(new errors.ValidationError({
|
|
|
|
message: i18n.t('errors.api.oembed.insufficientMetadata'),
|
|
|
|
context: url
|
|
|
|
}));
|
2019-08-27 17:01:02 +03:00
|
|
|
}
|
2019-08-09 17:11:24 +03:00
|
|
|
|
|
|
|
const findUrlWithProvider = (url) => {
|
|
|
|
let provider;
|
|
|
|
|
|
|
|
// build up a list of URL variations to test against because the oembed
|
|
|
|
// providers list is not always up to date with scheme or www vs non-www
|
|
|
|
let baseUrl = url.replace(/^\/\/|^https?:\/\/(?:www\.)?/, '');
|
|
|
|
let testUrls = [
|
|
|
|
`http://${baseUrl}`,
|
|
|
|
`https://${baseUrl}`,
|
|
|
|
`http://www.${baseUrl}`,
|
|
|
|
`https://www.${baseUrl}`
|
|
|
|
];
|
|
|
|
|
|
|
|
for (let testUrl of testUrls) {
|
|
|
|
provider = hasProvider(testUrl);
|
|
|
|
if (provider) {
|
|
|
|
url = testUrl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {url, provider};
|
|
|
|
};
|
|
|
|
|
2019-08-27 17:01:02 +03:00
|
|
|
function unknownProvider(url) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.ValidationError({
|
|
|
|
message: i18n.t('errors.api.oembed.unknownProvider'),
|
2019-08-27 17:01:02 +03:00
|
|
|
context: url
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function knownProvider(url) {
|
2020-04-29 18:41:31 +03:00
|
|
|
return extract(url, {maxwidth: 1280}).catch((err) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.InternalServerError({
|
2019-08-27 17:01:02 +03:00
|
|
|
message: err.message
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-03 15:47:18 +03:00
|
|
|
function isIpOrLocalhost(url) {
|
|
|
|
try {
|
|
|
|
const IPV4_REGEX = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
|
|
|
const IPV6_REGEX = /:/; // fqdns will not have colons
|
|
|
|
const HTTP_REGEX = /^https?:/i;
|
|
|
|
|
2020-06-15 20:38:43 +03:00
|
|
|
const siteUrl = new URL(config.get('url'));
|
|
|
|
const {protocol, hostname, host} = new URL(url);
|
|
|
|
|
|
|
|
// allow requests to Ghost's own url through
|
|
|
|
if (siteUrl.host === host) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-03 15:47:18 +03:00
|
|
|
|
|
|
|
if (!HTTP_REGEX.test(protocol) || hostname === 'localhost' || IPV4_REGEX.test(hostname) || IPV6_REGEX.test(hostname)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} catch (e) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 10:44:03 +03:00
|
|
|
function fetchOembedData(_url, cardType) {
|
2020-03-03 15:47:18 +03:00
|
|
|
// parse the url then validate the protocol and host to make sure it's
|
|
|
|
// http(s) and not an IP address or localhost to avoid potential access to
|
|
|
|
// internal network endpoints
|
|
|
|
if (isIpOrLocalhost(_url)) {
|
|
|
|
return unknownProvider();
|
|
|
|
}
|
|
|
|
|
|
|
|
// check against known oembed list
|
|
|
|
let {url, provider} = findUrlWithProvider(_url);
|
2019-08-27 17:01:02 +03:00
|
|
|
if (provider) {
|
|
|
|
return knownProvider(url);
|
|
|
|
}
|
2020-03-03 15:47:18 +03:00
|
|
|
|
|
|
|
// url not in oembed list so fetch it in case it's a redirect or has a
|
|
|
|
// <link rel="alternate" type="application/json+oembed"> element
|
2020-06-17 12:03:36 +03:00
|
|
|
const cookieJar = new CookieJar();
|
2020-06-02 16:30:10 +03:00
|
|
|
return externalRequest(url, {
|
2019-08-27 17:01:02 +03:00
|
|
|
method: 'GET',
|
|
|
|
timeout: 2 * 1000,
|
2020-06-17 12:03:36 +03:00
|
|
|
followRedirect: true,
|
|
|
|
cookieJar
|
2020-06-08 14:51:59 +03:00
|
|
|
}).then((pageResponse) => {
|
2020-03-03 15:47:18 +03:00
|
|
|
// url changed after fetch, see if we were redirected to a known oembed
|
2020-06-08 14:51:59 +03:00
|
|
|
if (pageResponse.url !== url) {
|
|
|
|
({url, provider} = findUrlWithProvider(pageResponse.url));
|
2020-03-03 15:47:18 +03:00
|
|
|
if (provider) {
|
|
|
|
return knownProvider(url);
|
|
|
|
}
|
2019-08-27 17:01:02 +03:00
|
|
|
}
|
2020-03-03 15:47:18 +03:00
|
|
|
|
|
|
|
// check for <link rel="alternate" type="application/json+oembed"> element
|
|
|
|
let oembedUrl;
|
|
|
|
try {
|
2020-06-08 14:51:59 +03:00
|
|
|
oembedUrl = cheerio('link[type="application/json+oembed"]', pageResponse.body).attr('href');
|
2020-03-03 15:47:18 +03:00
|
|
|
} catch (e) {
|
|
|
|
return unknownProvider(url);
|
2019-08-27 17:01:02 +03:00
|
|
|
}
|
2020-03-03 15:47:18 +03:00
|
|
|
|
2019-08-27 17:01:02 +03:00
|
|
|
if (oembedUrl) {
|
2020-03-03 15:47:18 +03:00
|
|
|
// make sure the linked url is not an ip address or localhost
|
|
|
|
if (isIpOrLocalhost(oembedUrl)) {
|
|
|
|
return unknownProvider(oembedUrl);
|
|
|
|
}
|
|
|
|
|
2020-10-06 10:44:03 +03:00
|
|
|
// for standard WP oembed's we want to insert a bookmark card rather than their blockquote+script
|
|
|
|
// which breaks in the editor and most Ghost themes. Only fallback if card type was not explicitly chosen
|
|
|
|
if (!cardType && oembedUrl.match(/wp-json\/oembed/)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-03 15:47:18 +03:00
|
|
|
// fetch oembed response from embedded rel="alternate" url
|
2020-06-02 16:30:10 +03:00
|
|
|
return externalRequest(oembedUrl, {
|
2019-08-27 17:01:02 +03:00
|
|
|
method: 'GET',
|
2020-04-07 17:29:22 +03:00
|
|
|
json: true,
|
|
|
|
timeout: 2 * 1000,
|
2020-06-17 12:03:36 +03:00
|
|
|
followRedirect: true,
|
|
|
|
cookieJar
|
2020-06-08 14:51:59 +03:00
|
|
|
}).then((oembedResponse) => {
|
2020-03-03 15:47:18 +03:00
|
|
|
// validate the fetched json against the oembed spec to avoid
|
|
|
|
// leaking non-oembed responses
|
2020-06-08 14:51:59 +03:00
|
|
|
const body = oembedResponse.body;
|
2020-03-03 15:47:18 +03:00
|
|
|
const hasRequiredFields = body.type && body.version;
|
|
|
|
const hasValidType = ['photo', 'video', 'link', 'rich'].includes(body.type);
|
|
|
|
|
|
|
|
if (hasRequiredFields && hasValidType) {
|
|
|
|
// extract known oembed fields from the response to limit leaking of unrecognised data
|
|
|
|
const knownFields = [
|
|
|
|
'type',
|
|
|
|
'version',
|
|
|
|
'html',
|
|
|
|
'url',
|
|
|
|
'title',
|
|
|
|
'width',
|
|
|
|
'height',
|
|
|
|
'author_name',
|
|
|
|
'author_url',
|
|
|
|
'provider_name',
|
|
|
|
'provider_url',
|
|
|
|
'thumbnail_url',
|
|
|
|
'thumbnail_width',
|
|
|
|
'thumbnail_height'
|
|
|
|
];
|
|
|
|
const oembed = _.pick(body, knownFields);
|
|
|
|
|
|
|
|
// ensure we have required data for certain types
|
|
|
|
if (oembed.type === 'photo' && !oembed.url) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((oembed.type === 'video' || oembed.type === 'rich') && (!oembed.html || !oembed.width || !oembed.height)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the extracted object, don't pass through the response body
|
|
|
|
return oembed;
|
|
|
|
}
|
2019-08-27 17:01:02 +03:00
|
|
|
}).catch(() => {});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:06:00 +03:00
|
|
|
function errorHandler(url) {
|
|
|
|
return function (err) {
|
|
|
|
// allow specific validation errors through for better error messages
|
|
|
|
if (errors.utils.isIgnitionError(err) && err.errorType === 'ValidationError') {
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// default to unknown provider to avoid leaking any app specifics
|
|
|
|
return unknownProvider(url);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:11:24 +03:00
|
|
|
module.exports = {
|
|
|
|
docName: 'oembed',
|
|
|
|
|
|
|
|
read: {
|
|
|
|
permissions: false,
|
|
|
|
data: [
|
2019-08-27 17:01:02 +03:00
|
|
|
'url',
|
|
|
|
'type'
|
2019-08-09 17:11:24 +03:00
|
|
|
],
|
|
|
|
options: [],
|
|
|
|
query({data}) {
|
2019-08-27 17:01:02 +03:00
|
|
|
let {url, type} = data;
|
2019-08-09 17:11:24 +03:00
|
|
|
|
2019-08-27 17:01:02 +03:00
|
|
|
if (type === 'bookmark') {
|
2020-03-02 17:24:26 +03:00
|
|
|
return fetchBookmarkData(url)
|
2020-06-08 17:06:00 +03:00
|
|
|
.catch(errorHandler(url));
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
|
2019-08-27 17:01:02 +03:00
|
|
|
return fetchOembedData(url).then((response) => {
|
|
|
|
if (!response && !type) {
|
|
|
|
return fetchBookmarkData(url);
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
2019-08-27 17:01:02 +03:00
|
|
|
return response;
|
|
|
|
}).then((response) => {
|
|
|
|
if (!response) {
|
|
|
|
return unknownProvider(url);
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
2019-08-27 17:01:02 +03:00
|
|
|
return response;
|
2020-06-08 17:06:00 +03:00
|
|
|
}).catch(errorHandler(url));
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|