feat: handle description in parameter middleware
This commit is contained in:
+124
-77
@@ -2,7 +2,6 @@ const he = require('he');
|
||||
const mercury_parser = require('@postlight/mercury-parser');
|
||||
const cheerio = require('cheerio');
|
||||
const got = require('@/utils/got');
|
||||
const utils = require('@/utils/common-utils');
|
||||
|
||||
module.exports = async (ctx, next) => {
|
||||
await next();
|
||||
@@ -11,31 +10,79 @@ module.exports = async (ctx, next) => {
|
||||
throw Error('wrong path');
|
||||
}
|
||||
|
||||
if (ctx.state.data && (!ctx.state.data.item || ctx.state.data.item.length === 0) && !ctx.state.data.allowEmpty) {
|
||||
throw Error('this route is empty, please check the original site or create an issue on https://github.com/DIYgod/RSSHub/issues/new/choose');
|
||||
}
|
||||
|
||||
if (ctx.state.data && ctx.query && ctx.query.mode && ctx.query.mode.toLowerCase() === 'fulltext') {
|
||||
const tasks = ctx.state.data.item.map(async (item) => {
|
||||
const { link, author, description } = item;
|
||||
const parsed_result = await ctx.cache.tryGet(`mercury-cache-${link}`, async () => {
|
||||
const res = await got(link);
|
||||
const $ = cheerio.load(res.data);
|
||||
utils.resolveLazyLoadImage($);
|
||||
const result = await mercury_parser.parse(link, {
|
||||
html: $.html(),
|
||||
});
|
||||
return result;
|
||||
});
|
||||
|
||||
item.author = author || parsed_result.author;
|
||||
item.description = parsed_result.content || description;
|
||||
});
|
||||
await Promise.all(tasks);
|
||||
}
|
||||
|
||||
// decode HTML entities
|
||||
if (ctx.state.data) {
|
||||
if ((!ctx.state.data.item || ctx.state.data.item.length === 0) && !ctx.state.data.allowEmpty) {
|
||||
throw Error('this route is empty, please check the original site or create an issue on https://github.com/DIYgod/RSSHub/issues/new/choose');
|
||||
}
|
||||
|
||||
if (ctx.query && ctx.query.mode && ctx.query.mode.toLowerCase() === 'fulltext') {
|
||||
const tasks = ctx.state.data.item.map(async (item) => {
|
||||
const { link, author, description } = item;
|
||||
const parsed_result = await ctx.cache.tryGet(`mercury-cache-${link}`, async () => {
|
||||
const res = await got(link);
|
||||
const $ = cheerio.load(res.data);
|
||||
const result = await mercury_parser.parse(link, {
|
||||
html: $.html(),
|
||||
});
|
||||
return result;
|
||||
});
|
||||
|
||||
item.author = author || parsed_result.author;
|
||||
item.description = parsed_result.content || description;
|
||||
});
|
||||
await Promise.all(tasks);
|
||||
}
|
||||
|
||||
// handle description
|
||||
if (ctx.state.data.item && ctx.state.data.item.length) {
|
||||
ctx.state.data.item.forEach((item) => {
|
||||
if (item.description) {
|
||||
const $ = cheerio.load(item.description);
|
||||
|
||||
$('script').remove();
|
||||
|
||||
$('a').each((_, ele) => {
|
||||
const $ele = $(ele);
|
||||
|
||||
// absolute link
|
||||
if (ctx.state.data.link) {
|
||||
$ele.attr('href', new URL($ele.attr('href'), ctx.state.data.link).href);
|
||||
}
|
||||
});
|
||||
|
||||
$('img').each((_, ele) => {
|
||||
const $ele = $(ele);
|
||||
|
||||
// fix lazyload
|
||||
if (!$ele.attr('src')) {
|
||||
for (const key in ele.attribs) {
|
||||
const value = ele.attribs[key].trim();
|
||||
if (['.gif', '.png', '.jpg', '.webp'].some((suffix) => value.includes(suffix))) {
|
||||
$ele.attr('src', value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// absolute link
|
||||
if (ctx.state.data.link) {
|
||||
$ele.attr('src', new URL($ele.attr('src'), ctx.state.data.link).href);
|
||||
}
|
||||
|
||||
// referrerpolicy
|
||||
$ele.attr('referrerpolicy', 'no-referrer');
|
||||
|
||||
// redundant attributes
|
||||
['onclick', 'onerror', 'onload'].forEach((e) => {
|
||||
$ele.removeAttr(e);
|
||||
});
|
||||
});
|
||||
item.description = $('body').html();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// decode HTML entities
|
||||
ctx.state.data.title && (ctx.state.data.title = he.decode(ctx.state.data.title + ''));
|
||||
ctx.state.data.description && (ctx.state.data.description = he.decode(ctx.state.data.description + ''));
|
||||
ctx.state.data.item &&
|
||||
@@ -43,61 +90,61 @@ module.exports = async (ctx, next) => {
|
||||
item.title && (item.title = he.decode(item.title + ''));
|
||||
item.description && (item.description = he.decode(item.description + ''));
|
||||
});
|
||||
}
|
||||
|
||||
// filter
|
||||
if (ctx.state.data && ctx.query && (ctx.query.filter || ctx.query.filter_title || ctx.query.filter_description || ctx.query.filter_author)) {
|
||||
ctx.state.data.item = ctx.state.data.item.filter((item) => {
|
||||
const title = item.title || '';
|
||||
const description = item.description || title;
|
||||
const author = item.author || '';
|
||||
return !(
|
||||
(ctx.query.filter && !title.match(ctx.query.filter) && !description.match(ctx.query.filter)) ||
|
||||
(ctx.query.filter_title && !title.match(ctx.query.filter_title)) ||
|
||||
(ctx.query.filter_description && !description.match(ctx.query.filter_description)) ||
|
||||
(ctx.query.filter_author && !author.match(ctx.query.filter_author))
|
||||
);
|
||||
});
|
||||
}
|
||||
if (ctx.state.data && ctx.query && (ctx.query.filterout || ctx.query.filterout_title || ctx.query.filterout_description || ctx.query.filterout_author)) {
|
||||
ctx.state.data.item = ctx.state.data.item.filter((item) => {
|
||||
const title = item.title;
|
||||
const description = item.description || title;
|
||||
const author = item.author || '';
|
||||
return (
|
||||
(ctx.query.filterout && !title.match(ctx.query.filterout) && !description.match(ctx.query.filterout)) ||
|
||||
(ctx.query.filterout_title && !title.match(ctx.query.filterout_title)) ||
|
||||
(ctx.query.filterout_description && !description.match(ctx.query.filterout_description)) ||
|
||||
(ctx.query.filterout_author && !author.match(ctx.query.filterout_author))
|
||||
);
|
||||
});
|
||||
}
|
||||
if (ctx.state.data && ctx.query && ctx.query.filter_time) {
|
||||
const now = Date.now();
|
||||
ctx.state.data.item = ctx.state.data.item.filter(({ pubDate }) => {
|
||||
if (!pubDate) {
|
||||
return true;
|
||||
}
|
||||
// filter
|
||||
if (ctx.query && (ctx.query.filter || ctx.query.filter_title || ctx.query.filter_description || ctx.query.filter_author)) {
|
||||
ctx.state.data.item = ctx.state.data.item.filter((item) => {
|
||||
const title = item.title || '';
|
||||
const description = item.description || title;
|
||||
const author = item.author || '';
|
||||
return !(
|
||||
(ctx.query.filter && !title.match(ctx.query.filter) && !description.match(ctx.query.filter)) ||
|
||||
(ctx.query.filter_title && !title.match(ctx.query.filter_title)) ||
|
||||
(ctx.query.filter_description && !description.match(ctx.query.filter_description)) ||
|
||||
(ctx.query.filter_author && !author.match(ctx.query.filter_author))
|
||||
);
|
||||
});
|
||||
}
|
||||
if (ctx.query && (ctx.query.filterout || ctx.query.filterout_title || ctx.query.filterout_description || ctx.query.filterout_author)) {
|
||||
ctx.state.data.item = ctx.state.data.item.filter((item) => {
|
||||
const title = item.title;
|
||||
const description = item.description || title;
|
||||
const author = item.author || '';
|
||||
return (
|
||||
(ctx.query.filterout && !title.match(ctx.query.filterout) && !description.match(ctx.query.filterout)) ||
|
||||
(ctx.query.filterout_title && !title.match(ctx.query.filterout_title)) ||
|
||||
(ctx.query.filterout_description && !description.match(ctx.query.filterout_description)) ||
|
||||
(ctx.query.filterout_author && !author.match(ctx.query.filterout_author))
|
||||
);
|
||||
});
|
||||
}
|
||||
if (ctx.query && ctx.query.filter_time) {
|
||||
const now = Date.now();
|
||||
ctx.state.data.item = ctx.state.data.item.filter(({ pubDate }) => {
|
||||
if (!pubDate) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
return now - new Date(pubDate).getTime() <= parseInt(ctx.query.filter_time) * 1000;
|
||||
} catch (err) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
try {
|
||||
return now - new Date(pubDate).getTime() <= parseInt(ctx.query.filter_time) * 1000;
|
||||
} catch (err) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// limit
|
||||
if (ctx.state.data && ctx.query && ctx.query.limit) {
|
||||
ctx.state.data.item = ctx.state.data.item.slice(0, parseInt(ctx.query.limit));
|
||||
}
|
||||
// limit
|
||||
if (ctx.query && ctx.query.limit) {
|
||||
ctx.state.data.item = ctx.state.data.item.slice(0, parseInt(ctx.query.limit));
|
||||
}
|
||||
|
||||
// telegram instant view
|
||||
if (ctx.state.data && ctx.query && ctx.query.tgiv) {
|
||||
ctx.state.data.item.map((item) => {
|
||||
const encodedlink = encodeURIComponent(item.link);
|
||||
item.link = `https://t.me/iv?url=${encodedlink}&rhash=${ctx.query.tgiv}`;
|
||||
return item;
|
||||
});
|
||||
// telegram instant view
|
||||
if (ctx.query && ctx.query.tgiv) {
|
||||
ctx.state.data.item.map((item) => {
|
||||
const encodedlink = encodeURIComponent(item.link);
|
||||
item.link = `https://t.me/iv?url=${encodedlink}&rhash=${ctx.query.tgiv}`;
|
||||
return item;
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -29,6 +29,22 @@ module.exports = async (ctx) => {
|
||||
link: `https://github.com/DIYgod/RSSHub/issues/0`,
|
||||
author: `DIYgod0`,
|
||||
});
|
||||
} else if (ctx.params.id === 'complicated') {
|
||||
item.push({
|
||||
title: `Complicated Title`,
|
||||
description: `<script>alert(1);</script>
|
||||
<a href="https://github.com/DIYgod/RSSHub/issues/0"></a>
|
||||
<a href="/DIYgod/RSSHub/issues/0"></a>
|
||||
<img src="/DIYgod/RSSHub.jpg" data-src="/DIYgod/RSSHub0.jpg">
|
||||
<img data-src="/DIYgod/RSSHub.jpg">
|
||||
<img data-mock="/DIYgod/RSSHub.png">
|
||||
<img mock="/DIYgod/RSSHub.gif">
|
||||
<img src="https://github.com/DIYgod/DIYgod/RSSHub">
|
||||
<img src="/DIYgod/RSSHub.jpg" onclick="alert(1);" onerror="alert(1);" onload="alert(1);">`,
|
||||
pubDate: new Date(`2018-3-1`).toUTCString(),
|
||||
link: `https://github.com/DIYgod/RSSHub/issues/0`,
|
||||
author: `DIYgod0`,
|
||||
});
|
||||
}
|
||||
|
||||
for (let i = 1; i < 6; i++) {
|
||||
|
||||
@@ -6,67 +6,6 @@ const toTitleCase = (str) =>
|
||||
.map((word) => word.replace(word[0], word[0].toUpperCase()))
|
||||
.join(' ');
|
||||
|
||||
// add no-referrer attribute to images
|
||||
// $: cheerio selector
|
||||
// source: source selector, string
|
||||
// target: target attribute name, typically for lazyload imgs, string
|
||||
// srcPrefix: prefix for src, string
|
||||
// removeAttr: attributes to remove, array: ['attrA','attrA']
|
||||
|
||||
const addNoReferrer = ($, source, target, srcPrefix, removeAttr) => {
|
||||
$(`${source} img`).each((index, e) => {
|
||||
const $e = $(e);
|
||||
let src = target ? $e.attr(target) : $e.attr('src');
|
||||
|
||||
if (src) {
|
||||
if (srcPrefix) {
|
||||
src = srcPrefix + src;
|
||||
}
|
||||
|
||||
if (target) {
|
||||
$e.removeAttr(target);
|
||||
}
|
||||
|
||||
if (removeAttr) {
|
||||
removeAttr.forEach((e) => {
|
||||
$e.removeAttr(e);
|
||||
});
|
||||
}
|
||||
|
||||
$e.attr('src', src);
|
||||
$e.attr('referrerpolicy', 'no-referrer');
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const resolveLazyLoadImage = ($) => {
|
||||
const imgs_suffix = ['gif', 'png', 'jpg', 'webp'];
|
||||
$('img')
|
||||
.each((_, ele) => {
|
||||
const $ele = $(ele);
|
||||
if ($ele.attr('src')) {
|
||||
$ele.attr('referrerpolicy', 'no-referrer');
|
||||
} else {
|
||||
let valid_src = '';
|
||||
Object.values(ele.attribs).some((attr_value) => {
|
||||
attr_value = attr_value.trim();
|
||||
const is_matched = /^(http:|https:)?\/\//.test(attr_value) && imgs_suffix.some((suffix) => attr_value.includes(suffix));
|
||||
if (is_matched) {
|
||||
valid_src = attr_value;
|
||||
}
|
||||
return is_matched;
|
||||
});
|
||||
if (valid_src) {
|
||||
$ele.attr('src', valid_src);
|
||||
$ele.attr('referrerpolicy', 'no-referrer');
|
||||
}
|
||||
}
|
||||
})
|
||||
.get();
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
toTitleCase,
|
||||
addNoReferrer,
|
||||
resolveLazyLoadImage,
|
||||
};
|
||||
|
||||
@@ -144,3 +144,19 @@ describe('fulltext_mode', () => {
|
||||
expect(parsed.items[0].content).not.toBe(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('complicated_description', () => {
|
||||
it(`complicated_description`, async () => {
|
||||
const response = await request.get('/test/complicated');
|
||||
expect(response.status).toBe(200);
|
||||
const parsed = await parser.parseString(response.text);
|
||||
expect(parsed.items[0].content).toBe(`<a href="https://github.com/DIYgod/RSSHub/issues/0"></a>
|
||||
<a href="https://github.com/DIYgod/RSSHub/issues/0"></a>
|
||||
<img src="https://github.com/DIYgod/RSSHub.jpg" data-src="/DIYgod/RSSHub0.jpg" referrerpolicy="no-referrer">
|
||||
<img data-src="/DIYgod/RSSHub.jpg" src="https://github.com/DIYgod/RSSHub.jpg" referrerpolicy="no-referrer">
|
||||
<img data-mock="/DIYgod/RSSHub.png" src="https://github.com/DIYgod/RSSHub.png" referrerpolicy="no-referrer">
|
||||
<img mock="/DIYgod/RSSHub.gif" src="https://github.com/DIYgod/RSSHub.gif" referrerpolicy="no-referrer">
|
||||
<img src="https://github.com/DIYgod/DIYgod/RSSHub" referrerpolicy="no-referrer">
|
||||
<img src="https://github.com/DIYgod/RSSHub.jpg" referrerpolicy="no-referrer">`);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,18 +6,7 @@ describe('common-utils', () => {
|
||||
expect(utils.toTitleCase('RSSHub IS AS aweSOme aS henry')).toBe('Rsshub Is As Awesome As Henry');
|
||||
});
|
||||
|
||||
it('addNoReferrer', async () => {
|
||||
const data = "<div><img data-src='user-images.githubusercontent.com/5896343/60762836-02746d80-a060-11e9-8947-76b3b3d3b731.png' remove='remove' remove2='remove2'></div>";
|
||||
|
||||
const $ = cheerio.load(data);
|
||||
|
||||
utils.addNoReferrer($, 'div', 'data-src', 'https://', ['remove', 'remove2']);
|
||||
const result = $('div').html();
|
||||
|
||||
expect(result).toBe('<img src="https://user-images.githubusercontent.com/5896343/60762836-02746d80-a060-11e9-8947-76b3b3d3b731.png" referrerpolicy="no-referrer">');
|
||||
});
|
||||
|
||||
it('resolveLazyLoadImage', async () => {
|
||||
it('handleImg', async () => {
|
||||
const testSet = [
|
||||
'<div><img lazyload="http://user-images.test.com/6b3b3d3b731.png" remove="http://githubusercontent.com/b3b3d3b731.png"></div>',
|
||||
'<div><img data-src="https://user-images.test.com/3d3b731.webp" remove="remove" remove2="remove2"></div>',
|
||||
@@ -30,7 +19,7 @@ describe('common-utils', () => {
|
||||
];
|
||||
testSet.forEach((htmlStr, index) => {
|
||||
const $ = cheerio.load(htmlStr);
|
||||
utils.resolveLazyLoadImage($);
|
||||
utils.handleImg($);
|
||||
const result = $('div').html();
|
||||
expect(result).toBe(expectList[index]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user