2021-01-12 20:16:05 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-10-08 18:12:19 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2021-11-17 16:31:10 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2021-12-01 19:46:19 +03:00
|
|
|
const sentry = require('../../shared/sentry');
|
2021-01-12 20:16:05 +03:00
|
|
|
const {extract, hasProvider} = require('oembed-parser');
|
|
|
|
const cheerio = require('cheerio');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const {CookieJar} = require('tough-cookie');
|
|
|
|
|
2021-10-08 17:32:16 +03:00
|
|
|
const messages = {
|
|
|
|
noUrlProvided: 'No url provided.',
|
2022-03-21 12:07:54 +03:00
|
|
|
insufficientMetadata: 'URL contains insufficient metadata.',
|
|
|
|
unknownProvider: 'No provider found for supplied URL.'
|
2021-10-08 17:32:16 +03:00
|
|
|
};
|
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
* @returns {{url: string, provider: boolean}}
|
|
|
|
*/
|
2021-01-12 20:16:05 +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};
|
|
|
|
};
|
|
|
|
|
2021-01-14 20:01:43 +03:00
|
|
|
/**
|
|
|
|
* @typedef {Object} IConfig
|
|
|
|
* @prop {(key: string) => string} get
|
|
|
|
*/
|
2021-01-12 20:16:05 +03:00
|
|
|
|
2021-01-14 20:01:43 +03:00
|
|
|
/**
|
|
|
|
* @typedef {(url: string, config: Object) => Promise} IExternalRequest
|
|
|
|
*/
|
2021-01-12 20:16:05 +03:00
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
/**
|
|
|
|
* @typedef {object} ICustomProvider
|
|
|
|
* @prop {(url: URL) => Promise<boolean>} canSupportRequest
|
|
|
|
* @prop {(url: URL, externalRequest: IExternalRequest) => Promise<import('oembed-parser').OembedData>} getOEmbedData
|
|
|
|
*/
|
|
|
|
|
2021-01-14 20:01:43 +03:00
|
|
|
class OEmbed {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Object} dependencies
|
|
|
|
* @param {IConfig} dependencies.config
|
|
|
|
* @param {IExternalRequest} dependencies.externalRequest
|
|
|
|
*/
|
2021-10-08 18:12:19 +03:00
|
|
|
constructor({config, externalRequest}) {
|
2021-01-14 20:01:43 +03:00
|
|
|
this.config = config;
|
2021-11-23 13:58:38 +03:00
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
/** @type {IExternalRequest} */
|
|
|
|
this.externalRequest = async (url, requestConfig) => {
|
|
|
|
if (this.isIpOrLocalhost(url)) {
|
|
|
|
return this.unknownProvider(url);
|
|
|
|
}
|
|
|
|
const response = await externalRequest(url, requestConfig);
|
|
|
|
if (this.isIpOrLocalhost(response.url)) {
|
|
|
|
return this.unknownProvider(url);
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
};
|
2021-11-23 13:58:38 +03:00
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
/** @type {ICustomProvider[]} */
|
|
|
|
this.customProviders = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ICustomProvider} provider
|
|
|
|
*/
|
|
|
|
registerProvider(provider) {
|
|
|
|
this.customProviders.push(provider);
|
2021-01-12 20:16:05 +03:00
|
|
|
}
|
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
*/
|
|
|
|
async unknownProvider(url) {
|
|
|
|
throw new errors.ValidationError({
|
2021-10-08 18:12:19 +03:00
|
|
|
message: tpl(messages.unknownProvider),
|
2021-01-14 20:01:43 +03:00
|
|
|
context: url
|
2021-11-10 17:14:04 +03:00
|
|
|
});
|
2021-01-12 20:16:05 +03:00
|
|
|
}
|
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
*/
|
|
|
|
async knownProvider(url) {
|
|
|
|
try {
|
|
|
|
return await extract(url);
|
|
|
|
} catch (err) {
|
|
|
|
throw new errors.InternalServerError({
|
2021-01-14 20:01:43 +03:00
|
|
|
message: err.message
|
2021-11-10 17:14:04 +03:00
|
|
|
});
|
|
|
|
}
|
2021-01-12 20:16:05 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 20:01:43 +03:00
|
|
|
async fetchBookmarkData(url) {
|
|
|
|
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')()
|
|
|
|
]);
|
|
|
|
|
|
|
|
let scraperResponse;
|
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
const cookieJar = new CookieJar();
|
|
|
|
const response = await this.externalRequest(url, {cookieJar});
|
2021-09-14 11:36:12 +03:00
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
const html = response.body;
|
2021-12-01 19:46:19 +03:00
|
|
|
try {
|
|
|
|
scraperResponse = await metascraper({html, url});
|
|
|
|
} catch (err) {
|
|
|
|
// Log to avoid being blind to errors happenning in metascraper
|
|
|
|
sentry.captureException(err);
|
|
|
|
logging.error(err);
|
|
|
|
return this.unknownProvider(url);
|
|
|
|
}
|
2021-01-12 20:16:05 +03:00
|
|
|
|
2021-01-14 20:01:43 +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;
|
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
if (!metadata.title) {
|
|
|
|
throw new errors.ValidationError({
|
|
|
|
message: tpl(messages.insufficientMetadata),
|
|
|
|
context: url
|
2021-01-14 20:01:43 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
return {
|
|
|
|
version: '1.0',
|
|
|
|
type: 'bookmark',
|
|
|
|
url,
|
|
|
|
metadata
|
|
|
|
};
|
2021-01-14 20:01:43 +03:00
|
|
|
}
|
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2021-01-14 20:01:43 +03:00
|
|
|
isIpOrLocalhost(url) {
|
2021-01-12 20:16:05 +03:00
|
|
|
try {
|
2021-01-14 20:01:43 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
const siteUrl = new URL(this.config.get('url'));
|
|
|
|
const {protocol, hostname, host} = new URL(url);
|
|
|
|
|
|
|
|
// allow requests to Ghost's own url through
|
|
|
|
if (siteUrl.host === host) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HTTP_REGEX.test(protocol) || hostname === 'localhost' || IPV4_REGEX.test(hostname) || IPV6_REGEX.test(hostname)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2021-01-12 20:16:05 +03:00
|
|
|
} catch (e) {
|
2021-01-14 20:01:43 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 09:36:18 +03:00
|
|
|
/**
|
|
|
|
* @param {string} _url
|
2021-08-23 09:53:44 +03:00
|
|
|
* @param {string} [cardType]
|
2021-08-23 09:36:18 +03:00
|
|
|
*
|
|
|
|
* @returns {Promise<Object>}
|
|
|
|
*/
|
2021-11-10 17:14:04 +03:00
|
|
|
async fetchOembedData(_url, cardType) {
|
2021-01-14 20:01:43 +03:00
|
|
|
// check against known oembed list
|
|
|
|
let {url, provider} = findUrlWithProvider(_url);
|
|
|
|
if (provider) {
|
|
|
|
return this.knownProvider(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
const cookieJar = new CookieJar();
|
2021-11-10 17:14:04 +03:00
|
|
|
const pageResponse = await this.externalRequest(url, {
|
2021-01-14 20:01:43 +03:00
|
|
|
method: 'GET',
|
|
|
|
timeout: 2 * 1000,
|
|
|
|
followRedirect: true,
|
|
|
|
cookieJar
|
2021-11-10 17:14:04 +03:00
|
|
|
});
|
|
|
|
// url changed after fetch, see if we were redirected to a known oembed
|
|
|
|
if (pageResponse.url !== url) {
|
|
|
|
({url, provider} = findUrlWithProvider(pageResponse.url));
|
|
|
|
if (provider) {
|
|
|
|
return this.knownProvider(url);
|
2021-01-12 20:16:05 +03:00
|
|
|
}
|
2021-11-10 17:14:04 +03:00
|
|
|
}
|
2021-01-12 20:16:05 +03:00
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
// check for <link rel="alternate" type="application/json+oembed"> element
|
|
|
|
let oembedUrl;
|
|
|
|
try {
|
|
|
|
oembedUrl = cheerio('link[type="application/json+oembed"]', pageResponse.body).attr('href');
|
|
|
|
} catch (e) {
|
|
|
|
return this.unknownProvider(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oembedUrl) {
|
|
|
|
// 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;
|
2021-01-12 20:16:05 +03:00
|
|
|
}
|
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
// fetch oembed response from embedded rel="alternate" url
|
|
|
|
const oembedResponse = await this.externalRequest(oembedUrl, {
|
|
|
|
method: 'GET',
|
|
|
|
json: true,
|
|
|
|
timeout: 2 * 1000,
|
|
|
|
followRedirect: true,
|
|
|
|
cookieJar
|
|
|
|
});
|
|
|
|
// validate the fetched json against the oembed spec to avoid
|
|
|
|
// leaking non-oembed responses
|
|
|
|
const body = oembedResponse.body;
|
|
|
|
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;
|
2021-01-14 20:01:43 +03:00
|
|
|
}
|
2021-11-10 17:14:04 +03:00
|
|
|
if ((oembed.type === 'video' || oembed.type === 'rich') && (!oembed.html || !oembed.width || !oembed.height)) {
|
2021-01-14 20:01:43 +03:00
|
|
|
return;
|
2021-01-12 20:16:05 +03:00
|
|
|
}
|
2021-01-14 20:01:43 +03:00
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
// return the extracted object, don't pass through the response body
|
|
|
|
return oembed;
|
2021-01-14 20:01:43 +03:00
|
|
|
}
|
2021-11-10 17:14:04 +03:00
|
|
|
}
|
2021-01-14 20:01:43 +03:00
|
|
|
}
|
2021-08-23 09:36:18 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} url - oembed URL
|
|
|
|
* @param {string} type - card type
|
|
|
|
*
|
|
|
|
* @returns {Promise<Object>}
|
|
|
|
*/
|
|
|
|
async fetchOembedDataFromUrl(url, type) {
|
2021-08-23 09:53:44 +03:00
|
|
|
try {
|
2021-11-10 17:14:04 +03:00
|
|
|
const urlObject = new URL(url);
|
2021-11-23 13:58:38 +03:00
|
|
|
|
2021-12-01 17:14:59 +03:00
|
|
|
// Trimming solves the difference of url validation between `new URL(url)`
|
|
|
|
// and metascraper.
|
|
|
|
url = url.trim();
|
|
|
|
|
2021-11-10 17:14:04 +03:00
|
|
|
for (const provider of this.customProviders) {
|
|
|
|
if (await provider.canSupportRequest(urlObject)) {
|
|
|
|
const result = await provider.getOEmbedData(urlObject, this.externalRequest);
|
|
|
|
if (result !== null) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 13:58:38 +03:00
|
|
|
// fetch only bookmark when explicitly requested
|
2021-08-23 09:53:44 +03:00
|
|
|
if (type === 'bookmark') {
|
2021-08-23 09:36:18 +03:00
|
|
|
return this.fetchBookmarkData(url);
|
|
|
|
}
|
2021-08-23 09:53:44 +03:00
|
|
|
|
2021-11-23 13:58:38 +03:00
|
|
|
// attempt to fetch oembed
|
|
|
|
let data = await this.fetchOembedData(url);
|
2021-08-23 09:53:44 +03:00
|
|
|
|
2021-11-23 13:58:38 +03:00
|
|
|
// fallback to bookmark when we can't get oembed
|
2021-08-23 09:53:44 +03:00
|
|
|
if (!data && !type) {
|
|
|
|
data = await this.fetchBookmarkData(url);
|
2021-08-23 09:36:18 +03:00
|
|
|
}
|
2021-08-23 09:53:44 +03:00
|
|
|
|
2021-11-23 13:58:38 +03:00
|
|
|
// couldn't get anything, throw a validation error
|
2021-08-23 09:53:44 +03:00
|
|
|
if (!data) {
|
2021-11-23 13:58:38 +03:00
|
|
|
return this.unknownProvider(url);
|
2021-08-23 09:53:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2021-11-23 13:58:38 +03:00
|
|
|
} catch (err) {
|
|
|
|
// allow specific validation errors through for better error messages
|
2021-12-01 13:22:01 +03:00
|
|
|
if (errors.utils.isGhostError(err) && err.errorType === 'ValidationError') {
|
2021-11-23 13:58:38 +03:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// log the real error because we're going to throw a generic "Unknown provider" error
|
2021-12-01 13:22:01 +03:00
|
|
|
logging.error(new errors.InternalServerError({
|
2021-11-23 13:58:38 +03:00
|
|
|
message: 'Encountered error when fetching oembed',
|
|
|
|
err
|
|
|
|
}));
|
|
|
|
|
|
|
|
// default to unknown provider to avoid leaking any app specifics
|
|
|
|
return this.unknownProvider(url);
|
2021-08-23 09:53:44 +03:00
|
|
|
}
|
2021-08-23 09:36:18 +03:00
|
|
|
}
|
2021-01-12 20:16:05 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 20:01:43 +03:00
|
|
|
module.exports = OEmbed;
|