9 Commits

Author SHA1 Message Date
c7d71a9ec2 Merge branch 'master' of https://github.com/misskey-dev/summaly 2023-04-20 04:02:59 +00:00
994f420b46 4.0.2 2023-04-20 04:02:55 +00:00
5a3321a04f fix: allow legacy allowfullscreen (#9) 2023-04-20 12:41:11 +09:00
1bab7afee6 Merge branch 'master' of https://github.com/misskey-dev/summaly 2023-03-16 03:26:10 +00:00
441e8c22f9 v4.0.1 2023-03-16 03:26:00 +00:00
376bba9c61 fix: give null when oEmbed access fails (#8) 2023-03-16 12:22:23 +09:00
028b2fed2f fix README.md 2023-03-13 18:03:16 +00:00
90d5d0f33b Fix README.md 2023-03-13 18:02:07 +00:00
9e955d8d04 fix readme 2023-03-13 17:57:37 +00:00
12 changed files with 836 additions and 759 deletions

View File

@ -1,3 +1,11 @@
4.0.2 / 2023-04-20
------------------
* YouTubeをフルスクリーンにできない問題を修正
4.0.1 / 2023-03-16
------------------
* oEmbedの読み込みでエラーが発生した際は、エラーにせずplayerの中身をnullにするように
4.0.0 / 2023-03-14 4.0.0 / 2023-03-14
------------------ ------------------
* oEmbed type=richの制限的なサポート * oEmbed type=richの制限的なサポート

View File

@ -98,27 +98,38 @@ See [Permissions Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Permi
### Example ### Example
``` javascript ```javascript
import { summaly } from 'summaly'; import { summaly } from 'summaly';
const summary = await summaly('https://www.youtube.com/watch?v=NMIEAhH_fTU'); const summary = await summaly('https://www.youtube.com/watch?v=NMIEAhH_fTU');
console.log(summary); // will be ... ↓ console.log(summary);
/* ```
will be ... ↓
```json
{ {
title: '【楽曲試聴】「Stage Bye Stage」(歌:島村卯月、渋谷凛、本田未央)', "title": "【アイドルマスター】「Stage Bye Stage」(歌:島村卯月、渋谷凛、本田未央)",
icon: 'https://s.ytimg.com/yts/img/favicon-vfl8qSV2F.ico', "icon": "https://www.youtube.com/s/desktop/9318de79/img/favicon.ico",
description: 'http://columbia.jp/idolmaster/ 2018年7月18日発売予定 THE IDOLM@STER CINDERELLA GIRLS CG STAR LIVE Stage Bye Stage 歌:島村卯月、渋谷凛、本田未央 COCC-17495CD1枚組 ¥1,200税 収録内容 Tr...', "description": "Website▶https://columbia.jp/idolmaster/Playlist▶https://www.youtube.com/playlist?list=PL83A2998CF3BBC86D2018年7月18日発売予定THE IDOLM@STER CINDERELLA GIRLS CG STAR...",
thumbnail: 'https://i.ytimg.com/vi/NMIEAhH_fTU/maxresdefault.jpg', "thumbnail": "https://i.ytimg.com/vi/NMIEAhH_fTU/maxresdefault.jpg",
player: { "player": {
url: 'https://www.youtube.com/embed/NMIEAhH_fTU', "url": "https://www.youtube.com/embed/NMIEAhH_fTU?feature=oembed",
width: 1280, "width": 200,
height: 720 "height": 113,
"allow": [
"autoplay",
"clipboard-write",
"encrypted-media",
"picture-in-picture",
"web-share"
]
}, },
sitename: 'YouTube', "sitename": "YouTube",
url: 'https://www.youtube.com/watch?v=NMIEAhH_fTU' "sensitive": false,
"url": "https://www.youtube.com/watch?v=NMIEAhH_fTU"
} }
*/
``` ```
Testing Testing
@ -129,5 +140,8 @@ License
---------------------------------------------------------------- ----------------------------------------------------------------
[MIT](LICENSE) [MIT](LICENSE)
[mit]: http://opensource.org/licenses/MIT
[mit-badge]: https://img.shields.io/badge/license-MIT-444444.svg?style=flat-square
[himasaku]: https://himasaku.net
[himawari-badge]: https://img.shields.io/badge/%E5%8F%A4%E8%B0%B7-%E5%90%91%E6%97%A5%E8%91%B5-1684c5.svg?style=flat-square [himawari-badge]: https://img.shields.io/badge/%E5%8F%A4%E8%B0%B7-%E5%90%91%E6%97%A5%E8%91%B5-1684c5.svg?style=flat-square
[sakurako-badge]: https://img.shields.io/badge/%E5%A4%A7%E5%AE%A4-%E6%AB%BB%E5%AD%90-efb02a.svg?style=flat-square [sakurako-badge]: https://img.shields.io/badge/%E5%A4%A7%E5%AE%A4-%E6%AB%BB%E5%AD%90-efb02a.svg?style=flat-square

View File

@ -15,7 +15,21 @@ async function getOEmbedPlayer($, pageUrl) {
if (!href) { if (!href) {
return null; return null;
} }
const oEmbed = await get((new URL(href, pageUrl)).href); const oEmbedUrl = (() => {
try {
return new URL(href, pageUrl);
}
catch {
return null;
}
})();
if (!oEmbedUrl) {
return null;
}
const oEmbed = await get(oEmbedUrl.href).catch(() => null);
if (!oEmbed) {
return null;
}
const body = (() => { const body = (() => {
try { try {
return JSON.parse(oEmbed); return JSON.parse(oEmbed);
@ -89,6 +103,9 @@ async function getOEmbedPlayer($, pageUrl) {
const allowedPermissions = (iframe.attr('allow') ?? '').split(/\s*;\s*/g) const allowedPermissions = (iframe.attr('allow') ?? '').split(/\s*;\s*/g)
.filter(s => s) .filter(s => s)
.filter(s => !ignoredList.includes(s)); .filter(s => !ignoredList.includes(s));
if (iframe.attr('allowfullscreen') === '') {
allowedPermissions.push('fullscreen');
}
if (allowedPermissions.some(allow => !safeList.includes(allow))) { if (allowedPermissions.some(allow => !safeList.includes(allow))) {
// This iframe is probably too powerful to be embedded // This iframe is probably too powerful to be embedded
return null; return null;

56
package-lock.json generated
View File

@ -1,17 +1,17 @@
{ {
"name": "summaly", "name": "summaly",
"version": "3.0.4", "version": "4.0.0",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "summaly", "name": "summaly",
"version": "3.0.4", "version": "4.0.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"cheerio": "^1.0.0-rc.12", "cheerio": "1.0.0-rc.12",
"escape-regexp": "0.0.1", "escape-regexp": "0.0.1",
"got": "^12.5.3", "got": "^12.6.0",
"html-entities": "2.3.2", "html-entities": "2.3.2",
"iconv-lite": "0.6.3", "iconv-lite": "0.6.3",
"jschardet": "3.0.0", "jschardet": "3.0.0",
@ -25,7 +25,6 @@
"@types/cheerio": "0.22.18", "@types/cheerio": "0.22.18",
"@types/debug": "4.1.7", "@types/debug": "4.1.7",
"@types/escape-regexp": "^0.0.1", "@types/escape-regexp": "^0.0.1",
"@types/html-entities": "1.3.4",
"@types/node": "16.11.12", "@types/node": "16.11.12",
"debug": "^4.3.4", "debug": "^4.3.4",
"fastify": "^4.13.0", "fastify": "^4.13.0",
@ -1393,16 +1392,6 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"node_modules/@types/html-entities": {
"version": "1.3.4",
"resolved": "https://registry.npmjs.org/@types/html-entities/-/html-entities-1.3.4.tgz",
"integrity": "sha512-Ut62LV90H9tgXwyhmfR8U6yCw/6xeo26IlsbAJJfqPomaqDN2zoLb2Z+cbmy5AycJFhwNJDdH0zqjQp7Ox/eXg==",
"deprecated": "This is a stub types definition. html-entities provides its own type definitions, so you do not need this installed.",
"dev": true,
"dependencies": {
"html-entities": "*"
}
},
"node_modules/@types/http-cache-semantics": { "node_modules/@types/http-cache-semantics": {
"version": "4.0.1", "version": "4.0.1",
"resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz", "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz",
@ -1838,9 +1827,9 @@
} }
}, },
"node_modules/cacheable-request": { "node_modules/cacheable-request": {
"version": "10.2.7", "version": "10.2.8",
"resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.7.tgz", "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.8.tgz",
"integrity": "sha512-I4SA6mKgDxcxVbSt/UmIkb9Ny8qSkg6ReBHtAAXnZHk7KOSx5g3DTiAOaYzcHCs6oOdHn+bip9T48E6tMvK9hw==", "integrity": "sha512-IDVO5MJ4LItE6HKFQTqT2ocAQsisOoCTUDu1ddCmnhyiwFQjXNPp4081Xj23N4tO+AFEFNzGuNEf/c8Gwwt15A==",
"dependencies": { "dependencies": {
"@types/http-cache-semantics": "^4.0.1", "@types/http-cache-semantics": "^4.0.1",
"get-stream": "^6.0.1", "get-stream": "^6.0.1",
@ -2780,14 +2769,14 @@
} }
}, },
"node_modules/got": { "node_modules/got": {
"version": "12.5.3", "version": "12.6.0",
"resolved": "https://registry.npmjs.org/got/-/got-12.5.3.tgz", "resolved": "https://registry.npmjs.org/got/-/got-12.6.0.tgz",
"integrity": "sha512-8wKnb9MGU8IPGRIo+/ukTy9XLJBwDiCpIf5TVzQ9Cpol50eMTpBq2GAuDsuDIz7hTYmZgMgC1e9ydr6kSDWs3w==", "integrity": "sha512-WTcaQ963xV97MN3x0/CbAriXFZcXCfgxVp91I+Ze6pawQOa7SgzwSx2zIJJsX+kTajMnVs0xcFD1TxZKFqhdnQ==",
"dependencies": { "dependencies": {
"@sindresorhus/is": "^5.2.0", "@sindresorhus/is": "^5.2.0",
"@szmarczak/http-timer": "^5.0.1", "@szmarczak/http-timer": "^5.0.1",
"cacheable-lookup": "^7.0.0", "cacheable-lookup": "^7.0.0",
"cacheable-request": "^10.2.1", "cacheable-request": "^10.2.8",
"decompress-response": "^6.0.0", "decompress-response": "^6.0.0",
"form-data-encoder": "^2.1.2", "form-data-encoder": "^2.1.2",
"get-stream": "^6.0.1", "get-stream": "^6.0.1",
@ -6322,15 +6311,6 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"@types/html-entities": {
"version": "1.3.4",
"resolved": "https://registry.npmjs.org/@types/html-entities/-/html-entities-1.3.4.tgz",
"integrity": "sha512-Ut62LV90H9tgXwyhmfR8U6yCw/6xeo26IlsbAJJfqPomaqDN2zoLb2Z+cbmy5AycJFhwNJDdH0zqjQp7Ox/eXg==",
"dev": true,
"requires": {
"html-entities": "*"
}
},
"@types/http-cache-semantics": { "@types/http-cache-semantics": {
"version": "4.0.1", "version": "4.0.1",
"resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz", "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz",
@ -6656,9 +6636,9 @@
"integrity": "sha512-+qJyx4xiKra8mZrcwhjMRMUhD5NR1R8esPkzIYxX96JiecFoxAXFuz/GpR3+ev4PE1WamHip78wV0vcmPQtp8w==" "integrity": "sha512-+qJyx4xiKra8mZrcwhjMRMUhD5NR1R8esPkzIYxX96JiecFoxAXFuz/GpR3+ev4PE1WamHip78wV0vcmPQtp8w=="
}, },
"cacheable-request": { "cacheable-request": {
"version": "10.2.7", "version": "10.2.8",
"resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.7.tgz", "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.8.tgz",
"integrity": "sha512-I4SA6mKgDxcxVbSt/UmIkb9Ny8qSkg6ReBHtAAXnZHk7KOSx5g3DTiAOaYzcHCs6oOdHn+bip9T48E6tMvK9hw==", "integrity": "sha512-IDVO5MJ4LItE6HKFQTqT2ocAQsisOoCTUDu1ddCmnhyiwFQjXNPp4081Xj23N4tO+AFEFNzGuNEf/c8Gwwt15A==",
"requires": { "requires": {
"@types/http-cache-semantics": "^4.0.1", "@types/http-cache-semantics": "^4.0.1",
"get-stream": "^6.0.1", "get-stream": "^6.0.1",
@ -7360,14 +7340,14 @@
"dev": true "dev": true
}, },
"got": { "got": {
"version": "12.5.3", "version": "12.6.0",
"resolved": "https://registry.npmjs.org/got/-/got-12.5.3.tgz", "resolved": "https://registry.npmjs.org/got/-/got-12.6.0.tgz",
"integrity": "sha512-8wKnb9MGU8IPGRIo+/ukTy9XLJBwDiCpIf5TVzQ9Cpol50eMTpBq2GAuDsuDIz7hTYmZgMgC1e9ydr6kSDWs3w==", "integrity": "sha512-WTcaQ963xV97MN3x0/CbAriXFZcXCfgxVp91I+Ze6pawQOa7SgzwSx2zIJJsX+kTajMnVs0xcFD1TxZKFqhdnQ==",
"requires": { "requires": {
"@sindresorhus/is": "^5.2.0", "@sindresorhus/is": "^5.2.0",
"@szmarczak/http-timer": "^5.0.1", "@szmarczak/http-timer": "^5.0.1",
"cacheable-lookup": "^7.0.0", "cacheable-lookup": "^7.0.0",
"cacheable-request": "^10.2.1", "cacheable-request": "^10.2.8",
"decompress-response": "^6.0.0", "decompress-response": "^6.0.0",
"form-data-encoder": "^2.1.2", "form-data-encoder": "^2.1.2",
"get-stream": "^6.0.1", "get-stream": "^6.0.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "summaly", "name": "summaly",
"version": "4.0.0", "version": "4.0.2",
"description": "Get web page's summary", "description": "Get web page's summary",
"author": "syuilo <syuilotan@yahoo.co.jp>", "author": "syuilo <syuilotan@yahoo.co.jp>",
"license": "MIT", "license": "MIT",
@ -9,6 +9,7 @@
"main": "./built/index.js", "main": "./built/index.js",
"type": "module", "type": "module",
"types": "./built/index.d.ts", "types": "./built/index.d.ts",
"packageManager": "pnpm@8.3.1",
"files": [ "files": [
"built", "built",
"LICENSE" "LICENSE"
@ -19,17 +20,17 @@
"serve": "fastify start ./built/index.js" "serve": "fastify start ./built/index.js"
}, },
"devDependencies": { "devDependencies": {
"@jest/globals": "^29.4.2", "@jest/globals": "^29.5.0",
"@swc/core": "^1.3.35", "@swc/core": "^1.3.52",
"@swc/jest": "^0.2.24", "@swc/jest": "^0.2.26",
"@types/cheerio": "0.22.18", "@types/cheerio": "0.22.18",
"@types/debug": "4.1.7", "@types/debug": "4.1.7",
"@types/escape-regexp": "^0.0.1", "@types/escape-regexp": "^0.0.1",
"@types/node": "16.11.12", "@types/node": "16.11.12",
"debug": "^4.3.4", "debug": "^4.3.4",
"fastify": "^4.13.0", "fastify": "^4.15.0",
"fastify-cli": "^5.7.1", "fastify-cli": "^5.7.1",
"jest": "^29.4.2", "jest": "^29.5.0",
"typescript": "4.5.3" "typescript": "4.5.3"
}, },
"dependencies": { "dependencies": {

1409
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -20,7 +20,20 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise<
return null; return null;
} }
const oEmbed = await get((new URL(href, pageUrl)).href); const oEmbedUrl = (() => {
try {
return new URL(href, pageUrl);
} catch { return null }
})();
if (!oEmbedUrl) {
return null;
}
const oEmbed = await get(oEmbedUrl.href).catch(() => null);
if (!oEmbed) {
return null;
}
const body = (() => { const body = (() => {
try { try {
return JSON.parse(oEmbed); return JSON.parse(oEmbed);
@ -102,6 +115,9 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise<
(iframe.attr('allow') ?? '').split(/\s*;\s*/g) (iframe.attr('allow') ?? '').split(/\s*;\s*/g)
.filter(s => s) .filter(s => s)
.filter(s => !ignoredList.includes(s)); .filter(s => !ignoredList.includes(s));
if (iframe.attr('allowfullscreen') === '') {
allowedPermissions.push('fullscreen');
}
if (allowedPermissions.some(allow => !safeList.includes(allow))) { if (allowedPermissions.some(allow => !safeList.includes(allow))) {
// This iframe is probably too powerful to be embedded // This iframe is probably too powerful to be embedded
return null; return null;

View File

@ -1,2 +1,3 @@
<!DOCTYPE html> <!DOCTYPE html>
<link type="application/json+oembed" href="http://localhost:3060/oembe.json" /> <link type="application/json+oembed" href="http://localhost:3060/oembe.json" />
<meta property="og:description" content="nonexistent">

View File

@ -0,0 +1,3 @@
<!DOCTYPE html>
<link type="application/json+oembed" href="http://localhost:+3060/oembed.json" />
<meta property="og:description" content="wrong url">

View File

@ -1,2 +0,0 @@
<!DOCTYPE html>
<link type="application/json+oembed" href="http://localhost+:3060/oembed.json" />

View File

@ -294,7 +294,14 @@ describe("oEmbed", () => {
await setUpFastify('oembed-allow-fullscreen.json'); await setUpFastify('oembed-allow-fullscreen.json');
const summary = await summaly(host); const summary = await summaly(host);
expect(summary.player.url).toBe('https://example.com/'); expect(summary.player.url).toBe('https://example.com/');
expect(summary.player.allow).toStrictEqual(['fullscreen']) expect(summary.player.allow).toStrictEqual(['fullscreen']);
});
test('allows legacy allowfullscreen', async () => {
await setUpFastify('oembed-allow-fullscreen-legacy.json');
const summary = await summaly(host);
expect(summary.player.url).toBe('https://example.com/');
expect(summary.player.allow).toStrictEqual(['fullscreen']);
}); });
test('allows safelisted permissions', async () => { test('allows safelisted permissions', async () => {
@ -322,12 +329,16 @@ describe("oEmbed", () => {
test('oEmbed with nonexistent path', async () => { test('oEmbed with nonexistent path', async () => {
await setUpFastify('oembed.json', 'htmls/oembed-nonexistent-path.html'); await setUpFastify('oembed.json', 'htmls/oembed-nonexistent-path.html');
await expect(summaly(host)).rejects.toThrow('404 Not Found'); const summary = await summaly(host);
expect(summary.player.url).toBe(null);
expect(summary.description).toBe('nonexistent');
}); });
test('oEmbed with wrong path', async () => { test('oEmbed with wrong path', async () => {
await setUpFastify('oembed.json', 'htmls/oembed-wrong-path.html'); await setUpFastify('oembed.json', 'htmls/oembed-wrong-path.html');
await expect(summaly(host)).rejects.toThrow(); const summary = await summaly(host);
expect(summary.player.url).toBe(null);
expect(summary.description).toBe('wrong url');
}); });
test('oEmbed with OpenGraph', async () => { test('oEmbed with OpenGraph', async () => {

View File

@ -0,0 +1,7 @@
{
"version": "1.0",
"type": "rich",
"html": "<iframe src='https://example.com/' allowfullscreen></iframe>",
"width": 500,
"height": 300
}