2018-11-02 11:32:26 +03:00
|
|
|
const nock = require('nock');
|
2020-06-02 16:30:10 +03:00
|
|
|
const sinon = require('sinon');
|
2018-11-02 11:32:26 +03:00
|
|
|
const should = require('should');
|
|
|
|
const supertest = require('supertest');
|
2019-09-20 18:02:45 +03:00
|
|
|
const testUtils = require('../../utils/index');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../core/shared/config/index');
|
2018-11-02 11:32:26 +03:00
|
|
|
const localUtils = require('./utils');
|
2023-03-07 15:20:28 +03:00
|
|
|
const {mockManager} = require('../../utils/e2e-framework');
|
2018-11-02 11:32:26 +03:00
|
|
|
|
2020-06-02 16:30:10 +03:00
|
|
|
// for sinon stubs
|
|
|
|
const dnsPromises = require('dns').promises;
|
|
|
|
|
2018-11-02 11:32:26 +03:00
|
|
|
describe('Oembed API', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let request;
|
2018-11-02 11:32:26 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
before(async function () {
|
2021-11-18 11:55:35 +03:00
|
|
|
await localUtils.startGhost();
|
2020-11-30 17:25:22 +03:00
|
|
|
request = supertest.agent(config.get('url'));
|
|
|
|
await localUtils.doAuth(request);
|
2018-11-02 11:32:26 +03:00
|
|
|
});
|
|
|
|
|
2020-12-01 00:44:15 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
// ensure sure we're not network dependent
|
2023-03-07 15:20:28 +03:00
|
|
|
mockManager.disableNetwork();
|
2020-12-01 00:44:15 +03:00
|
|
|
});
|
|
|
|
|
2020-06-02 16:30:10 +03:00
|
|
|
afterEach(function () {
|
2023-03-08 14:40:21 +03:00
|
|
|
mockManager.restore();
|
2020-06-02 16:30:10 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('can fetch an embed', async function () {
|
|
|
|
const requestMock = nock('https://www.youtube.com')
|
2019-02-04 17:16:24 +03:00
|
|
|
.get('/oembed')
|
|
|
|
.query(true)
|
|
|
|
.reply(200, {
|
|
|
|
html: '<iframe width="480" height="270" src="https://www.youtube.com/embed/E5yFcdPAGv0?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>',
|
|
|
|
thumbnail_width: 480,
|
|
|
|
width: 480,
|
|
|
|
author_url: 'https://www.youtube.com/user/gorillaz',
|
|
|
|
height: 270,
|
|
|
|
thumbnail_height: 360,
|
|
|
|
provider_name: 'YouTube',
|
|
|
|
title: 'Gorillaz - Humility (Official Video)',
|
|
|
|
provider_url: 'https://www.youtube.com/',
|
|
|
|
author_name: 'Gorillaz',
|
|
|
|
version: '1.0',
|
|
|
|
thumbnail_url: 'https://i.ytimg.com/vi/E5yFcdPAGv0/hqdefault.jpg',
|
|
|
|
type: 'video'
|
|
|
|
});
|
2018-11-02 11:32:26 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const res = await request.get(localUtils.API.getApiQuery('oembed/?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DE5yFcdPAGv0'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
2018-11-02 11:32:26 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
requestMock.isDone().should.be.true();
|
|
|
|
should.exist(res.body.html);
|
2018-11-02 11:32:26 +03:00
|
|
|
});
|
2020-03-03 15:47:18 +03:00
|
|
|
|
2024-01-18 17:42:28 +03:00
|
|
|
it('does not use http preferentially to https', async function () {
|
|
|
|
const httpMock = nock('https://odysee.com')
|
|
|
|
.get('/$/oembed')
|
|
|
|
.query({url: 'http://odysee.com/@BarnabasNagy:5/At-Last-(Playa):2', format: 'json'})
|
|
|
|
.reply(200, 'The URL is invalid or is not associated with any claim.');
|
|
|
|
|
|
|
|
const httpsMock = nock('https://odysee.com')
|
|
|
|
.get('/$/oembed')
|
|
|
|
.query({url: 'https://odysee.com/@BarnabasNagy:5/At-Last-(Playa):2', format: 'json'})
|
|
|
|
.reply(200, {
|
|
|
|
html: '<iframe></iframe>',
|
|
|
|
type: 'rich',
|
|
|
|
version: '1.0'
|
|
|
|
});
|
|
|
|
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery('oembed/?url=https%3A%2F%2Fodysee.com%2F%40BarnabasNagy%3A5%2FAt-Last-%28Playa%29%3A2'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private);
|
|
|
|
|
|
|
|
httpMock.isDone().should.be.false();
|
|
|
|
httpsMock.isDone().should.be.true();
|
|
|
|
should.exist(res.body.html);
|
|
|
|
});
|
|
|
|
|
2023-05-05 02:04:58 +03:00
|
|
|
it('errors with a useful message when embedding is disabled', async function () {
|
|
|
|
const requestMock = nock('https://www.youtube.com')
|
|
|
|
.get('/oembed')
|
|
|
|
.query(true)
|
|
|
|
.reply(401, {
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: 'Authorisation error, cannot read oembed.',
|
|
|
|
context: 'URL contains a private resource.',
|
|
|
|
type: 'UnauthorizedError',
|
|
|
|
details: null,
|
|
|
|
property: null,
|
|
|
|
help: null,
|
|
|
|
code: null,
|
|
|
|
id: 'c51228a0-921a-11ed-8abe-6babfda4d18a',
|
|
|
|
ghostErrorCode: null
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery('oembed/?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DE5yFcdPAGv0'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2024-03-12 12:36:34 +03:00
|
|
|
.expect(422);
|
2023-05-05 02:04:58 +03:00
|
|
|
|
|
|
|
requestMock.isDone().should.be.true();
|
|
|
|
should.exist(res.body.errors);
|
|
|
|
res.body.errors[0].context.should.match(/URL contains a private resource/i);
|
|
|
|
});
|
|
|
|
|
2020-06-08 17:06:00 +03:00
|
|
|
describe('type: bookmark', function () {
|
2020-11-30 17:25:22 +03:00
|
|
|
it('can fetch a bookmark with ?type=bookmark', async function () {
|
2020-12-04 00:46:01 +03:00
|
|
|
const pageMock = nock('http://example.com')
|
2020-06-08 17:06:00 +03:00
|
|
|
.get('/')
|
|
|
|
.reply(
|
|
|
|
200,
|
|
|
|
'<html><head><title>TESTING</title></head><body></body></html>',
|
|
|
|
{'content-type': 'text/html'}
|
|
|
|
);
|
|
|
|
|
2021-12-01 17:14:59 +03:00
|
|
|
const url = encodeURIComponent(' http://example.com\t '); // Whitespaces are to make sure urls are trimmed
|
2020-11-30 17:25:22 +03:00
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}&type=bookmark`))
|
2020-06-08 17:06:00 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
res.body.type.should.eql('bookmark');
|
2020-12-04 00:46:01 +03:00
|
|
|
res.body.url.should.eql('http://example.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
res.body.metadata.title.should.eql('TESTING');
|
2020-06-08 17:06:00 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('falls back to bookmark without ?type=embed and no oembed metatag', async function () {
|
2020-12-04 00:46:01 +03:00
|
|
|
const pageMock = nock('http://example.com')
|
2020-06-08 17:06:00 +03:00
|
|
|
.get('/')
|
2022-10-13 14:19:47 +03:00
|
|
|
.times(1) // url should not be fetched twice
|
2020-06-08 17:06:00 +03:00
|
|
|
.reply(
|
|
|
|
200,
|
|
|
|
'<html><head><title>TESTING</title></head><body></body></html>',
|
|
|
|
{'content-type': 'text/html'}
|
|
|
|
);
|
|
|
|
|
2020-12-04 00:46:01 +03:00
|
|
|
const url = encodeURIComponent('http://example.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-06-08 17:06:00 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
res.body.type.should.eql('bookmark');
|
2020-12-04 00:46:01 +03:00
|
|
|
res.body.url.should.eql('http://example.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
res.body.metadata.title.should.eql('TESTING');
|
2020-06-08 17:06:00 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('errors with useful message when title is unavailable', async function () {
|
2020-12-04 00:46:01 +03:00
|
|
|
const pageMock = nock('http://example.com')
|
2020-06-08 17:06:00 +03:00
|
|
|
.get('/')
|
|
|
|
.reply(
|
|
|
|
200,
|
|
|
|
'<html><head><title></title></head><body></body></html>',
|
|
|
|
{'content-type': 'text/html'}
|
|
|
|
);
|
|
|
|
|
2020-12-04 00:46:01 +03:00
|
|
|
const url = encodeURIComponent('http://example.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`oembed/?type=bookmark&url=${url}`))
|
2020-06-08 17:06:00 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(422);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
should.exist(res.body.errors);
|
|
|
|
res.body.errors[0].context.should.match(/insufficient metadata/i);
|
2021-09-14 11:36:12 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('errors when fetched url is an IP address', async function () {
|
2023-02-20 18:33:11 +03:00
|
|
|
// in order to follow the 302, we need to stub differently; externalRequest will block the internal IP
|
|
|
|
dnsPromises.lookup.restore();
|
|
|
|
let dnsStub = sinon.stub(dnsPromises, 'lookup');
|
|
|
|
dnsStub.onCall(0).returns(Promise.resolve({address: '123.123.123.123'}));
|
|
|
|
dnsStub.onCall(1).returns(Promise.resolve({address: '0.0.0.0'}));
|
|
|
|
|
2023-03-10 14:53:35 +03:00
|
|
|
nock('http://test.com/')
|
2021-09-14 11:36:12 +03:00
|
|
|
.get('/')
|
|
|
|
.reply(302, undefined, {Location: 'http://0.0.0.0:8080'});
|
|
|
|
|
|
|
|
const pageMock = nock('http://0.0.0.0:8080')
|
|
|
|
.get('/')
|
|
|
|
.reply(
|
|
|
|
200,
|
|
|
|
'<html><head><title>TESTING</title></head><body></body></html>',
|
|
|
|
{'content-type': 'text/html'}
|
|
|
|
);
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://test.com');
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`oembed/?type=bookmark&url=${url}`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(422);
|
|
|
|
|
2023-02-20 18:33:11 +03:00
|
|
|
pageMock.isDone().should.be.false(); // we shouldn't hit this; blocked by externalRequest
|
2021-09-14 11:36:12 +03:00
|
|
|
should.exist(res.body.errors);
|
2020-06-08 17:06:00 +03:00
|
|
|
});
|
2021-12-01 17:14:59 +03:00
|
|
|
|
|
|
|
it('errors when fetched url is incorrect', async function () {
|
|
|
|
const url = encodeURIComponent('example.com');
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`oembed/?type=bookmark&url=${url}`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(422);
|
|
|
|
|
|
|
|
should.exist(res.body.errors);
|
|
|
|
});
|
2024-02-29 19:09:34 +03:00
|
|
|
|
|
|
|
it('should replace icon URL when it returns 404', async function () {
|
|
|
|
// Mock the page so it contains a readable icon URL
|
|
|
|
const pageMock = nock('http://example.com')
|
|
|
|
.get('/page-with-icon')
|
|
|
|
.reply(
|
|
|
|
200,
|
|
|
|
'<html><head><title>TESTING</title><link rel="icon" href="http://example.com/icon.svg"></head><body></body></html>',
|
|
|
|
{'content-type': 'text/html'}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Mock the icon URL to return 404
|
|
|
|
nock('http://example.com/')
|
|
|
|
.head('/icon.svg')
|
|
|
|
.reply(404);
|
|
|
|
|
|
|
|
const url = encodeURIComponent(' http://example.com/page-with-icon\t '); // Whitespaces are to make sure urls are trimmed
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}&type=bookmark`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
// Check that the icon URL mock was loaded
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
|
|
|
|
// Check that the substitute icon URL is returned in place of the original
|
|
|
|
res.body.metadata.icon.should.eql('https://static.ghost.org/v5.0.0/images/link-icon.svg');
|
|
|
|
});
|
2020-06-08 17:06:00 +03:00
|
|
|
});
|
|
|
|
|
2020-03-03 15:47:18 +03:00
|
|
|
describe('with unknown provider', function () {
|
2020-11-30 17:25:22 +03:00
|
|
|
it('fetches url and follows redirects', async function () {
|
2020-06-02 16:30:10 +03:00
|
|
|
const redirectMock = nock('http://test.com/')
|
|
|
|
.get('/')
|
|
|
|
.reply(302, undefined, {Location: 'http://oembed.test.com'});
|
|
|
|
|
|
|
|
const pageMock = nock('http://oembed.test.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://oembed.test.com/my-embed"></head></html>');
|
|
|
|
|
|
|
|
const oembedMock = nock('http://oembed.test.com')
|
|
|
|
.get('/my-embed')
|
|
|
|
.reply(200, {
|
|
|
|
version: '1.0',
|
|
|
|
type: 'link'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://test.com/');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-06-02 16:30:10 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
redirectMock.isDone().should.be.true();
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
oembedMock.isDone().should.be.true();
|
2020-06-02 16:30:10 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('fetches url and follows <link rel="alternate">', async function () {
|
2020-03-03 15:47:18 +03:00
|
|
|
const pageMock = nock('http://test.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://test.com/oembed"></head></html>');
|
|
|
|
|
|
|
|
const oembedMock = nock('http://test.com')
|
|
|
|
.get('/oembed')
|
|
|
|
.reply(200, {
|
|
|
|
version: '1.0',
|
|
|
|
type: 'link'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://test.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-03-03 15:47:18 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
oembedMock.isDone().should.be.true();
|
2020-03-03 15:47:18 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('follows redirects when fetching <link rel="alternate">', async function () {
|
2020-06-02 16:30:10 +03:00
|
|
|
const pageMock = nock('http://test.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://test.com/oembed"></head></html>');
|
|
|
|
|
|
|
|
const alternateRedirectMock = nock('http://test.com')
|
|
|
|
.get('/oembed')
|
|
|
|
.reply(301, undefined, {Location: 'http://test.com/oembed-final'});
|
|
|
|
|
|
|
|
const alternateMock = nock('http://test.com')
|
|
|
|
.get('/oembed-final')
|
|
|
|
.reply(200, {
|
|
|
|
version: '1.0',
|
|
|
|
type: 'link'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://test.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-06-02 16:30:10 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
alternateRedirectMock.isDone().should.be.true();
|
|
|
|
alternateMock.isDone().should.be.true();
|
2020-06-02 16:30:10 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('rejects invalid oembed responses', async function () {
|
2020-03-03 15:47:18 +03:00
|
|
|
const pageMock = nock('http://test.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://test.com/oembed"></head></html>');
|
|
|
|
|
|
|
|
const oembedMock = nock('http://test.com')
|
|
|
|
.get('/oembed')
|
|
|
|
.reply(200, {
|
|
|
|
version: '1.0',
|
|
|
|
html: 'test'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://test.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-03-03 15:47:18 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(422);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
oembedMock.isDone().should.be.true();
|
2020-03-03 15:47:18 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('rejects unknown oembed types', async function () {
|
2020-03-03 15:47:18 +03:00
|
|
|
const pageMock = nock('http://test.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://test.com/oembed"></head></html>');
|
|
|
|
|
|
|
|
const oembedMock = nock('http://test.com')
|
|
|
|
.get('/oembed')
|
|
|
|
.reply(200, {
|
|
|
|
version: '1.0',
|
|
|
|
type: 'unknown'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://test.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-03-03 15:47:18 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(422);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
oembedMock.isDone().should.be.true();
|
2020-03-03 15:47:18 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('rejects invalid photo responses', async function () {
|
2020-03-03 15:47:18 +03:00
|
|
|
const pageMock = nock('http://test.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://test.com/oembed"></head></html>');
|
|
|
|
|
|
|
|
const oembedMock = nock('http://test.com')
|
|
|
|
.get('/oembed')
|
|
|
|
.reply(200, {
|
|
|
|
// no `url` field
|
|
|
|
version: '1.0',
|
|
|
|
type: 'photo',
|
|
|
|
thumbnail_url: 'https://test.com/thumbnail.jpg'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://test.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-03-03 15:47:18 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(422);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
oembedMock.isDone().should.be.true();
|
2020-03-03 15:47:18 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('rejects invalid video responses', async function () {
|
2020-03-03 15:47:18 +03:00
|
|
|
const pageMock = nock('http://test.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://test.com/oembed"></head></html>');
|
|
|
|
|
|
|
|
const oembedMock = nock('http://test.com')
|
|
|
|
.get('/oembed')
|
|
|
|
.reply(200, {
|
|
|
|
// no `html` field
|
|
|
|
version: '1.0',
|
|
|
|
type: 'video',
|
|
|
|
thumbnail_url: 'https://test.com/thumbnail.jpg'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://test.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-03-03 15:47:18 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(422);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
oembedMock.isDone().should.be.true();
|
2020-03-03 15:47:18 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('strips unknown response fields', async function () {
|
2020-03-03 15:47:18 +03:00
|
|
|
const pageMock = nock('http://test.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://test.com/oembed"></head></html>');
|
|
|
|
|
|
|
|
const oembedMock = nock('http://test.com')
|
|
|
|
.get('/oembed')
|
|
|
|
.reply(200, {
|
|
|
|
version: '1.0',
|
|
|
|
type: 'video',
|
|
|
|
html: '<p>Test</p>',
|
|
|
|
width: 200,
|
|
|
|
height: 100,
|
|
|
|
unknown: 'test'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://test.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-03-03 15:47:18 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
oembedMock.isDone().should.be.true();
|
|
|
|
|
|
|
|
res.body.should.deepEqual({
|
|
|
|
version: '1.0',
|
|
|
|
type: 'video',
|
|
|
|
html: '<p>Test</p>',
|
|
|
|
width: 200,
|
|
|
|
height: 100
|
|
|
|
});
|
|
|
|
should.not.exist(res.body.unknown);
|
2020-03-03 15:47:18 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('skips fetching IPv4 addresses', async function () {
|
2023-02-20 18:33:11 +03:00
|
|
|
dnsPromises.lookup.restore();
|
|
|
|
sinon.stub(dnsPromises, 'lookup').callsFake(function (hostname) {
|
|
|
|
if (hostname === '192.168.0.1') {
|
|
|
|
return Promise.resolve({address: '192.168.0.1'});
|
|
|
|
} else {
|
|
|
|
return Promise.resolve({address: '123.123.123.123'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-03 15:47:18 +03:00
|
|
|
const pageMock = nock('http://test.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://192.168.0.1/oembed"></head></html>');
|
|
|
|
|
|
|
|
const oembedMock = nock('http://192.168.0.1')
|
|
|
|
.get('/oembed')
|
|
|
|
.reply(200, {
|
|
|
|
version: '1.0',
|
|
|
|
type: 'link'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://test.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-03-03 15:47:18 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(422);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
oembedMock.isDone().should.be.false();
|
2020-03-03 15:47:18 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('skips fetching IPv6 addresses', async function () {
|
2023-02-20 18:33:11 +03:00
|
|
|
dnsPromises.lookup.restore();
|
|
|
|
sinon.stub(dnsPromises, 'lookup').callsFake(function (hostname) {
|
|
|
|
if (hostname === '[2607:f0d0:1002:51::4]') {
|
|
|
|
return Promise.resolve({address: '192.168.0.1'});
|
|
|
|
} else {
|
|
|
|
return Promise.resolve({address: '123.123.123.123'});
|
|
|
|
}
|
|
|
|
});
|
2023-03-07 15:20:28 +03:00
|
|
|
|
2020-03-03 15:47:18 +03:00
|
|
|
const pageMock = nock('http://test.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://[2607:f0d0:1002:51::4]:9999/oembed"></head></html>');
|
|
|
|
|
|
|
|
const oembedMock = nock('http://[2607:f0d0:1002:51::4]:9999')
|
|
|
|
.get('/oembed')
|
|
|
|
.reply(200, {
|
|
|
|
version: '1.0',
|
|
|
|
type: 'link'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://test.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-03-03 15:47:18 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(422);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
oembedMock.isDone().should.be.false();
|
2020-03-03 15:47:18 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('skips fetching localhost', async function () {
|
2023-02-20 18:33:11 +03:00
|
|
|
dnsPromises.lookup.restore();
|
|
|
|
sinon.stub(dnsPromises, 'lookup').callsFake(function (hostname) {
|
|
|
|
if (hostname === 'localhost') {
|
|
|
|
return Promise.resolve({address: '127.0.0.1'});
|
|
|
|
} else {
|
|
|
|
return Promise.resolve({address: '123.123.123.123'});
|
|
|
|
}
|
|
|
|
});
|
2020-03-03 15:47:18 +03:00
|
|
|
const pageMock = nock('http://test.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://localhost:9999/oembed"></head></html>');
|
|
|
|
|
|
|
|
const oembedMock = nock('http://localhost:9999')
|
|
|
|
.get('/oembed')
|
|
|
|
.reply(200, {
|
|
|
|
// no `html` field
|
|
|
|
version: '1.0',
|
|
|
|
type: 'video',
|
|
|
|
thumbnail_url: 'https://test.com/thumbnail.jpg'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://test.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-03-03 15:47:18 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(422);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
oembedMock.isDone().should.be.false();
|
2020-03-03 15:47:18 +03:00
|
|
|
});
|
2020-06-02 16:30:10 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('skips fetching url that resolves to private IP', async function () {
|
2020-12-01 00:44:15 +03:00
|
|
|
dnsPromises.lookup.restore();
|
2020-06-02 16:30:10 +03:00
|
|
|
sinon.stub(dnsPromises, 'lookup').callsFake(function (hostname) {
|
|
|
|
if (hostname === 'page.com') {
|
|
|
|
return Promise.resolve({address: '192.168.0.1'});
|
2020-12-01 00:44:15 +03:00
|
|
|
} else {
|
|
|
|
return Promise.resolve({address: '123.123.123.123'});
|
2020-06-02 16:30:10 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const pageMock = nock('http://page.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://oembed.com/oembed"></head></html>');
|
|
|
|
|
|
|
|
const oembedMock = nock('http://oembed.com')
|
|
|
|
.get('/oembed')
|
|
|
|
.reply(200, {
|
|
|
|
version: '1.0',
|
|
|
|
type: 'link'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://page.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-06-02 16:30:10 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(422);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.false();
|
|
|
|
oembedMock.isDone().should.be.false();
|
2020-06-02 16:30:10 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('aborts fetching if a redirect resolves to private IP', async function () {
|
2020-12-01 00:44:15 +03:00
|
|
|
dnsPromises.lookup.restore();
|
|
|
|
sinon.stub(dnsPromises, 'lookup').callsFake(async function (hostname) {
|
2020-06-02 16:30:10 +03:00
|
|
|
if (hostname === 'page.com') {
|
|
|
|
return Promise.resolve({address: '192.168.0.1'});
|
2020-12-01 00:44:15 +03:00
|
|
|
} else {
|
|
|
|
return Promise.resolve({address: '123.123.123.123'});
|
2020-06-02 16:30:10 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const redirectMock = nock('http://redirect.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(301, undefined, {Location: 'http://page.com'});
|
|
|
|
|
|
|
|
const pageMock = nock('http://page.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://oembed.com/oembed"></head></html>');
|
|
|
|
|
|
|
|
const oembedMock = nock('http://oembed.com')
|
|
|
|
.get('/oembed')
|
|
|
|
.reply(200, {
|
|
|
|
version: '1.0',
|
|
|
|
type: 'link'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://redirect.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-06-02 16:30:10 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(422);
|
|
|
|
|
|
|
|
redirectMock.isDone().should.be.true();
|
|
|
|
pageMock.isDone().should.be.false();
|
|
|
|
oembedMock.isDone().should.be.false();
|
2020-06-02 16:30:10 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('skips fetching <link rel="alternate"> if it resolves to a private IP', async function () {
|
2020-12-01 00:44:15 +03:00
|
|
|
dnsPromises.lookup.restore();
|
2020-06-02 16:30:10 +03:00
|
|
|
sinon.stub(dnsPromises, 'lookup').callsFake(function (hostname) {
|
|
|
|
if (hostname === 'oembed.com') {
|
|
|
|
return Promise.resolve({address: '192.168.0.1'});
|
2020-12-01 00:44:15 +03:00
|
|
|
} else {
|
|
|
|
return Promise.resolve({address: '123.123.123.123'});
|
2020-06-02 16:30:10 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const pageMock = nock('http://page.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(200, '<html><head><link rel="alternate" type="application/json+oembed" href="http://oembed.com/oembed"></head></html>');
|
|
|
|
|
|
|
|
const oembedMock = nock('http://oembed.com')
|
|
|
|
.get('/oembed')
|
|
|
|
.reply(200, {
|
|
|
|
version: '1.0',
|
|
|
|
type: 'link'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://page.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-06-02 16:30:10 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(422);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
oembedMock.isDone().should.be.false();
|
2020-06-02 16:30:10 +03:00
|
|
|
});
|
2020-10-06 10:44:03 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('falls back to bookmark card for WP oembeds', async function () {
|
2020-10-06 10:44:03 +03:00
|
|
|
const pageMock = nock('http://test.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(
|
|
|
|
200,
|
|
|
|
'<html><head><link rel="alternate" type="application/json+oembed" href="http://test.com/wp-json/oembed/embed?url=https%3A%2F%2Ftest.com%2F"><title>TESTING</title></head><body></body></html>',
|
|
|
|
{'content-type': 'text/html'}
|
|
|
|
);
|
|
|
|
|
|
|
|
const oembedMock = nock('http://test.com')
|
|
|
|
.get('/wp-json/oembed/embed')
|
|
|
|
.reply(200, {
|
|
|
|
version: '1.0',
|
|
|
|
type: 'link'
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = encodeURIComponent('http://test.com');
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}`))
|
2020-10-06 10:44:03 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
oembedMock.isDone().should.be.false();
|
2020-10-06 10:44:03 +03:00
|
|
|
});
|
2022-10-13 14:19:47 +03:00
|
|
|
|
|
|
|
it('decodes non utf-8 charsets', async function () {
|
|
|
|
const utfString = '中国abc';
|
|
|
|
const encodedBytes = [0xd6,0xd0,0xb9,0xfa,0x61,0x62,0x63];
|
|
|
|
const replyBuffer = Buffer.concat([
|
|
|
|
Buffer.from('<html><head><title>'),
|
|
|
|
Buffer.from(encodedBytes),
|
|
|
|
Buffer.from('</title><meta charset="gb2312"></head><body></body></html>')
|
|
|
|
]);
|
|
|
|
|
|
|
|
const pageMock = nock('http://example.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(
|
|
|
|
200,
|
|
|
|
replyBuffer,
|
|
|
|
{'content-type': 'text/html'}
|
|
|
|
);
|
|
|
|
|
|
|
|
const url = encodeURIComponent(' http://example.com\t '); // Whitespaces are to make sure urls are trimmed
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}&type=bookmark`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
res.body.type.should.eql('bookmark');
|
|
|
|
res.body.url.should.eql('http://example.com');
|
|
|
|
res.body.metadata.title.should.eql(utfString);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not fail on unknown charset', async function () {
|
|
|
|
const pageMock = nock('http://example.com')
|
|
|
|
.get('/')
|
|
|
|
.reply(
|
|
|
|
200,
|
|
|
|
'<html><head><title>TESTING</title><meta charset="notacharset"></head><body></body></html>',
|
|
|
|
{'content-type': 'text/html'}
|
|
|
|
);
|
|
|
|
|
|
|
|
const url = encodeURIComponent(' http://example.com\t '); // Whitespaces are to make sure urls are trimmed
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`oembed/?url=${url}&type=bookmark`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
pageMock.isDone().should.be.true();
|
|
|
|
res.body.type.should.eql('bookmark');
|
|
|
|
res.body.url.should.eql('http://example.com');
|
|
|
|
res.body.metadata.title.should.eql('TESTING');
|
|
|
|
});
|
2020-03-03 15:47:18 +03:00
|
|
|
});
|
2018-11-02 11:32:26 +03:00
|
|
|
});
|