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

🐛 Fixes oembed bookmark with whitespaces

refs https://github.com/TryGhost/Team/issues/1200

- The leading/trailing whitespaces are trimmed by `new URL()` but are considered invalid in metascraper. Trimming solves this edge case.
This commit is contained in:
Thibaut Patel 2021-12-01 15:14:59 +01:00
parent 57de4aca71
commit a1421c2380
2 changed files with 16 additions and 1 deletions

View file

@ -284,6 +284,10 @@ class OEmbed {
try {
const urlObject = new URL(url);
// Trimming solves the difference of url validation between `new URL(url)`
// and metascraper.
url = url.trim();
for (const provider of this.customProviders) {
if (await provider.canSupportRequest(urlObject)) {
const result = await provider.getOEmbedData(urlObject, this.externalRequest);

View file

@ -70,7 +70,7 @@ describe('Oembed API', function () {
{'content-type': 'text/html'}
);
const url = encodeURIComponent('http://example.com');
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/)
@ -150,6 +150,17 @@ describe('Oembed API', function () {
pageMock.isDone().should.be.true();
should.exist(res.body.errors);
});
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);
});
});
describe('with unknown provider', function () {