From 2c8f4636309977b9d4660781eefbc30317b317a5 Mon Sep 17 00:00:00 2001 From: JimenezLi <75196426+JimenezLi@users.noreply.github.com> Date: Tue, 24 Oct 2023 21:35:42 +0800 Subject: [PATCH] fix(core): string-type categories only (#13590) --- lib/middleware/parameter.js | 16 ++++++++++------ lib/v2/test/index.js | 9 +++++++++ test/middleware/parameter.js | 8 ++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/middleware/parameter.js b/lib/middleware/parameter.js index edd0da8290ffae..f6a918cdb28466 100644 --- a/lib/middleware/parameter.js +++ b/lib/middleware/parameter.js @@ -136,6 +136,13 @@ module.exports = async (ctx, next) => { }); } } + + // handle category + if (item.category) { + // convert single string to array, and filter only string type category + Array.isArray(item.category) || (item.category = [item.category]); + item.category = item.category.filter((e) => typeof e === 'string'); + } return item; }; @@ -172,8 +179,7 @@ module.exports = async (ctx, next) => { const title = item.title || ''; const description = item.description || title; const author = item.author || ''; - const categoryArray = Array.isArray(item.category) ? item.category : [item.category]; - const category = item.category ? categoryArray : []; + const category = item.category || []; const isFilter = engine === 're2' ? regex.matcher(title).find() || regex.matcher(description).find() || regex.matcher(author).find() || category.some((c) => regex.matcher(c).find()) @@ -189,8 +195,7 @@ module.exports = async (ctx, next) => { const title = item.title || ''; const description = item.description || title; const author = item.author || ''; - const categoryArray = Array.isArray(item.category) ? item.category : [item.category]; - const category = item.category ? categoryArray : []; + const category = item.category || []; let isFilter = true; const titleRegex = makeRegex(ctx.query.filter_title); @@ -216,8 +221,7 @@ module.exports = async (ctx, next) => { const title = item.title; const description = item.description || title; const author = item.author || ''; - const categoryArray = Array.isArray(item.category) ? item.category : [item.category]; - const category = item.category ? categoryArray : []; + const category = item.category || []; let isFilter = true; const titleRegex = makeRegex(ctx.query.filterout_title); diff --git a/lib/v2/test/index.js b/lib/v2/test/index.js index 860dabc52a26fd..7c14d6c2b9f431 100644 --- a/lib/v2/test/index.js +++ b/lib/v2/test/index.js @@ -42,6 +42,15 @@ module.exports = async (ctx) => { category: 'Category3', }, ]; + } else if (ctx.params.id === 'filter-illegal-category') { + item.push({ + title: 'TitleIllegal', + description: 'DescriptionIllegal', + pubDate: new Date(`2019-3-1`).toUTCString(), + link: `https://github.com/DIYgod/RSSHub/issues/1`, + author: `DIYgod0`, + category: [1, 'CategoryIllegal', true, null, undefined, { type: 'object' }], + }); } else if (ctx.params.id === 'long') { item.push({ title: `Long Title `.repeat(50), diff --git a/test/middleware/parameter.js b/test/middleware/parameter.js index d0a1d6cd14f87b..b157ec38ca855b 100644 --- a/test/middleware/parameter.js +++ b/test/middleware/parameter.js @@ -112,6 +112,14 @@ describe('filter', () => { expect(parsed.items[0].title).toBe('Filter Title3'); }); + it(`filter_category illegal_category`, async () => { + const response = await request.get('/test/filter-illegal-category?filter_category=CategoryIllegal'); + const parsed = await parser.parseString(response.text); + expect(parsed.items.length).toBe(1); + expect(parsed.items[0].categories.length).toBe(1); + expect(parsed.items[0].categories[0]).toBe('CategoryIllegal'); + }); + it(`filter_time`, async () => { const response = await request.get('/test/current_time?filter_time=25'); const parsed = await parser.parseString(response.text);