From c3e78ee7dfbab1e5a96793c750998c5f4b02abbc Mon Sep 17 00:00:00 2001 From: Rongrong Date: Mon, 28 Nov 2022 03:37:43 +0800 Subject: [PATCH] fix(route/weibo/*): sinaimg anti-hotlinking (#11320) * fix(route/weibo/*): sinaimg anti-hotlinking Signed-off-by: Rongrong * refactor: migrate to v2 * Fix CodeQL warning Signed-off-by: Rongrong * Fix CodeQL warning again Signed-off-by: Rongrong * Narrow sinaimg URL regex Signed-off-by: Rongrong * Fix CodeQL warning again and again Signed-off-by: Rongrong * Fix CodeQL warning again, again and again Signed-off-by: Rongrong * More accurate regex, dismiss CodeQL plz Signed-off-by: Rongrong Signed-off-by: Rongrong --- assets/radar-rules.js | 21 ---------- lib/radar-rules.js | 46 -------------------- lib/router.js | 14 +++---- lib/routes/weibo/oasis/user.js | 17 -------- lib/{routes => v2}/weibo/keyword.js | 4 +- lib/v2/weibo/maintainer.js | 8 ++++ lib/v2/weibo/oasis/user.js | 20 +++++++++ lib/v2/weibo/radar.js | 48 +++++++++++++++++++++ lib/v2/weibo/router.js | 8 ++++ lib/{routes => v2}/weibo/search/hot.js | 2 + lib/{routes => v2}/weibo/super_index.js | 4 +- lib/{routes => v2}/weibo/timeline.js | 4 +- lib/{routes => v2}/weibo/user.js | 4 +- lib/{routes => v2}/weibo/utils.js | 56 ++++++++++++++++++------- 14 files changed, 143 insertions(+), 113 deletions(-) delete mode 100644 lib/routes/weibo/oasis/user.js rename lib/{routes => v2}/weibo/keyword.js (97%) create mode 100644 lib/v2/weibo/maintainer.js create mode 100644 lib/v2/weibo/oasis/user.js create mode 100644 lib/v2/weibo/radar.js create mode 100644 lib/v2/weibo/router.js rename lib/{routes => v2}/weibo/search/hot.js (91%) rename lib/{routes => v2}/weibo/super_index.js (97%) rename lib/{routes => v2}/weibo/timeline.js (99%) rename lib/{routes => v2}/weibo/user.js (99%) rename lib/{routes => v2}/weibo/utils.js (92%) diff --git a/assets/radar-rules.js b/assets/radar-rules.js index 19221c272..cc3690f48 100644 --- a/assets/radar-rules.js +++ b/assets/radar-rules.js @@ -13,27 +13,6 @@ }, 'https://www.icac.org.hk': { _name: '廉政公署', '.': [{ title: '新闻公布', docs: 'https://docs.rsshub.app/government.html#xiang-gang-lian-zheng-gong-shu', source: ['/:lang/press/index.html'], target: '/icac/news/:lang' }] }, 'trow.cc': { _name: 'The Ring of Wonder', '.': [{ title: '首页更新', docs: 'https://docs.rsshub.app/bbs.html#the-ring-of-wonder', source: ['/'], target: '/portal' }] }, - 'weibo.com': { - _name: '微博', - '.': [ - { - title: '博主', - docs: 'https://docs.rsshub.app/social-media.html#wei-bo', - source: ['/u/:id', '/:id'], - target: (params, url, document) => { - let uid = document?.documentElement.innerHTML.match(/\$CONFIG\['oid']='(\d+)'/)?.[1]; - if (!uid && !isNaN(params.id)) { - uid = params.id; - } - return uid ? `/weibo/user/${uid}` : ''; - }, - }, - { title: '关键词', docs: 'https://docs.rsshub.app/social-media.html#wei-bo' }, - { title: '超话', docs: 'https://docs.rsshub.app/social-media.html#wei-bo', source: '/p/:id/super_index', target: '/weibo/super_index/:id' }, - ], - s: [{ title: '热搜榜', docs: 'https://docs.rsshub.app/social-media.html#wei-bo', source: '/top/summary', target: '/weibo/search/hot' }], - }, - 'weibo.cn': { _name: '微博', m: [{ title: '博主', docs: 'https://docs.rsshub.app/social-media.html#wei-bo', source: ['/u/:uid', '/profile/:uid'], target: '/weibo/user/:uid' }] }, 'github.com': { _name: 'GitHub', '.': [ diff --git a/lib/radar-rules.js b/lib/radar-rules.js index c920902cc..bc8324a08 100644 --- a/lib/radar-rules.js +++ b/lib/radar-rules.js @@ -1,50 +1,4 @@ module.exports = { - 'weibo.com': { - _name: '微博', - '.': [ - { - title: '博主', - docs: 'https://docs.rsshub.app/social-media.html#wei-bo', - source: ['/u/:id', '/:id'], - target: (params, url, document) => { - let uid = document?.documentElement.innerHTML.match(/\$CONFIG\['oid']='(\d+)'/)?.[1]; - if (!uid && !isNaN(params.id)) { - uid = params.id; - } - return uid ? `/weibo/user/${uid}` : ''; - }, - }, - { - title: '关键词', - docs: 'https://docs.rsshub.app/social-media.html#wei-bo', - }, - { - title: '超话', - docs: 'https://docs.rsshub.app/social-media.html#wei-bo', - source: '/p/:id/super_index', - target: '/weibo/super_index/:id', - }, - ], - s: [ - { - title: '热搜榜', - docs: 'https://docs.rsshub.app/social-media.html#wei-bo', - source: '/top/summary', - target: '/weibo/search/hot', - }, - ], - }, - 'weibo.cn': { - _name: '微博', - m: [ - { - title: '博主', - docs: 'https://docs.rsshub.app/social-media.html#wei-bo', - source: ['/u/:uid', '/profile/:uid'], - target: '/weibo/user/:uid', - }, - ], - }, 'ximalaya.com': { _name: '喜马拉雅', '.': [ diff --git a/lib/router.js b/lib/router.js index 7c3594fb2..89f3a0420 100644 --- a/lib/router.js +++ b/lib/router.js @@ -47,12 +47,12 @@ router.get('/benedictevans', lazyloadRouteHandler('./routes/benedictevans/recent // router.get('/twreporter/photography', lazyloadRouteHandler('./routes/twreporter/photography')); // router.get('/twreporter/category/:cid', lazyloadRouteHandler('./routes/twreporter/category')); -// 微博 -router.get('/weibo/user/:uid/:routeParams?', lazyloadRouteHandler('./routes/weibo/user')); -router.get('/weibo/keyword/:keyword/:routeParams?', lazyloadRouteHandler('./routes/weibo/keyword')); -router.get('/weibo/search/hot', lazyloadRouteHandler('./routes/weibo/search/hot')); -router.get('/weibo/super_index/:id/:type?/:routeParams?', lazyloadRouteHandler('./routes/weibo/super_index')); -router.get('/weibo/oasis/user/:userid', lazyloadRouteHandler('./routes/weibo/oasis/user')); +// 微博 migrated to v2 +// router.get('/weibo/user/:uid/:routeParams?', lazyloadRouteHandler('./routes/weibo/user')); +// router.get('/weibo/keyword/:keyword/:routeParams?', lazyloadRouteHandler('./routes/weibo/keyword')); +// router.get('/weibo/search/hot', lazyloadRouteHandler('./routes/weibo/search/hot')); +// router.get('/weibo/super_index/:id/:type?/:routeParams?', lazyloadRouteHandler('./routes/weibo/super_index')); +// router.get('/weibo/oasis/user/:userid', lazyloadRouteHandler('./routes/weibo/oasis/user')); // 贴吧 migrated to v2 // router.get('/tieba/forum/:kw', lazyloadRouteHandler('./routes/tieba/forum')); @@ -2086,7 +2086,7 @@ router.get('/haimaoba/:id?', lazyloadRouteHandler('./routes/haimaoba/comics')); router.get('/pgyer/:app?', lazyloadRouteHandler('./routes/pgyer/app')); // 微博个人时间线 -router.get('/weibo/timeline/:uid/:feature?/:routeParams?', lazyloadRouteHandler('./routes/weibo/timeline')); +// router.get('/weibo/timeline/:uid/:feature?/:routeParams?', lazyloadRouteHandler('./routes/weibo/timeline')); // TAPTAP migrated to v2 // router.get('/taptap/topic/:id/:label?', lazyloadRouteHandler('./routes/taptap/topic')); diff --git a/lib/routes/weibo/oasis/user.js b/lib/routes/weibo/oasis/user.js deleted file mode 100644 index 529645264..000000000 --- a/lib/routes/weibo/oasis/user.js +++ /dev/null @@ -1,17 +0,0 @@ -const buildData = require('@/utils/common-config'); - -module.exports = async (ctx) => { - const userid = ctx.params.userid; - const link = `https://oasis.weibo.cn/v1/h5/share?uid=${userid}`; - ctx.state.data = await buildData({ - link, - url: link, - title: `$('.name-main').text().trim() + ' - 用户 - 绿洲'`, - description: `$('.desc').text().trim()`, - item: { - item: '.container .item', - title: `$('.status-desc').text()`, - description: `$('.status-desc').text() + '
' + $('.media-wrapper').html()`, - }, - }); -}; diff --git a/lib/routes/weibo/keyword.js b/lib/v2/weibo/keyword.js similarity index 97% rename from lib/routes/weibo/keyword.js rename to lib/v2/weibo/keyword.js index 5afe45481..2e8209c58 100644 --- a/lib/routes/weibo/keyword.js +++ b/lib/v2/weibo/keyword.js @@ -28,7 +28,7 @@ module.exports = async (ctx) => { ); const routeParams = querystring.parse(ctx.params.routeParams); - ctx.state.data = { + ctx.state.data = weiboUtils.sinaimgTvax({ title: `又有人在微博提到${keyword}了`, link: `http://s.weibo.com/weibo/${encodeURIComponent(keyword)}&b=1&nodup=1`, description: `又有人在微博提到${keyword}了`, @@ -51,5 +51,5 @@ module.exports = async (ctx) => { link: `https://weibo.com/${item.mblog.user.id}/${item.mblog.bid}`, }; }), - }; + }); }; diff --git a/lib/v2/weibo/maintainer.js b/lib/v2/weibo/maintainer.js new file mode 100644 index 000000000..1d7d2a244 --- /dev/null +++ b/lib/v2/weibo/maintainer.js @@ -0,0 +1,8 @@ +module.exports = { + '/keyword/:keyword/:routeParams?': ['DIYgod'], + '/oasis/user/:userid': ['kt286'], + '/search/hot': ['xyqfer'], + '/super_index/:id/:type?/:routeParams?': ['zengxs'], + '/timeline/:uid/:feature?/:routeParams?': ['zytomorrow', 'DIYgod ', 'Rongronggg9'], + '/user/:uid/:routeParams?': ['DIYgod', 'iplusx', 'Rongronggg9'], +}; diff --git a/lib/v2/weibo/oasis/user.js b/lib/v2/weibo/oasis/user.js new file mode 100644 index 000000000..547f01666 --- /dev/null +++ b/lib/v2/weibo/oasis/user.js @@ -0,0 +1,20 @@ +const buildData = require('@/utils/common-config'); +const weiboUtils = require('../utils'); + +module.exports = async (ctx) => { + const userid = ctx.params.userid; + const link = `https://oasis.weibo.cn/v1/h5/share?uid=${userid}`; + ctx.state.data = weiboUtils.sinaimgTvax( + await buildData({ + link, + url: link, + title: `$('.name-main').text().trim() + ' - 用户 - 绿洲'`, + description: `$('.desc').text().trim()`, + item: { + item: '.container .item', + title: `$('.status-desc').text()`, + description: `$('.status-desc').text() + '
' + $('.media-wrapper').html()`, + }, + }) + ); +}; diff --git a/lib/v2/weibo/radar.js b/lib/v2/weibo/radar.js new file mode 100644 index 000000000..89d4adf73 --- /dev/null +++ b/lib/v2/weibo/radar.js @@ -0,0 +1,48 @@ +module.exports = { + 'weibo.com': { + _name: '微博', + '.': [ + { + title: '博主', + docs: 'https://docs.rsshub.app/social-media.html#wei-bo', + source: ['/u/:id', '/:id'], + target: (params, url, document) => { + let uid = document?.documentElement.innerHTML.match(/\$CONFIG\['oid']='(\d+)'/)?.[1]; + if (!uid && !isNaN(params.id)) { + uid = params.id; + } + return uid ? `/weibo/user/${uid}` : ''; + }, + }, + { + title: '关键词', + docs: 'https://docs.rsshub.app/social-media.html#wei-bo', + }, + { + title: '超话', + docs: 'https://docs.rsshub.app/social-media.html#wei-bo', + source: '/p/:id/super_index', + target: '/weibo/super_index/:id', + }, + ], + s: [ + { + title: '热搜榜', + docs: 'https://docs.rsshub.app/social-media.html#wei-bo', + source: '/top/summary', + target: '/weibo/search/hot', + }, + ], + }, + 'weibo.cn': { + _name: '微博', + m: [ + { + title: '博主', + docs: 'https://docs.rsshub.app/social-media.html#wei-bo', + source: ['/u/:uid', '/profile/:uid'], + target: '/weibo/user/:uid', + }, + ], + }, +}; diff --git a/lib/v2/weibo/router.js b/lib/v2/weibo/router.js new file mode 100644 index 000000000..2936afe92 --- /dev/null +++ b/lib/v2/weibo/router.js @@ -0,0 +1,8 @@ +module.exports = (router) => { + router.get('/keyword/:keyword/:routeParams?', require('./keyword')); + router.get('/oasis/user/:userid', require('./oasis/user')); + router.get('/search/hot', require('./search/hot')); + router.get('/super_index/:id/:type?/:routeParams?', require('./super_index')); + router.get('/timeline/:uid/:feature?/:routeParams?', require('./timeline')); + router.get('/user/:uid/:routeParams?', require('./user')); +}; diff --git a/lib/routes/weibo/search/hot.js b/lib/v2/weibo/search/hot.js similarity index 91% rename from lib/routes/weibo/search/hot.js rename to lib/v2/weibo/search/hot.js index 03d9a3690..0f0f46cb0 100644 --- a/lib/routes/weibo/search/hot.js +++ b/lib/v2/weibo/search/hot.js @@ -1,4 +1,5 @@ const got = require('@/utils/got'); +// const weiboUtils = require('../utils'); module.exports = async (ctx) => { const { @@ -30,4 +31,5 @@ module.exports = async (ctx) => { }; }), }; + // ctx.state.data = weiboUtils.sinaimgTvax(ctx.state.data); // no image in the route }; diff --git a/lib/routes/weibo/super_index.js b/lib/v2/weibo/super_index.js similarity index 97% rename from lib/routes/weibo/super_index.js rename to lib/v2/weibo/super_index.js index cd8a7e715..4cf15c14c 100644 --- a/lib/routes/weibo/super_index.js +++ b/lib/v2/weibo/super_index.js @@ -53,10 +53,10 @@ module.exports = async (ctx) => { } } - ctx.state.data = { + ctx.state.data = weiboUtils.sinaimgTvax({ title: `微博超话 - ${containerData.pageInfo.page_title}`, link: `https://weibo.com/p/${id}/super_index`, description: `#${containerData.pageInfo.page_title}# 的超话`, item: resultItems, - }; + }); }; diff --git a/lib/routes/weibo/timeline.js b/lib/v2/weibo/timeline.js similarity index 99% rename from lib/routes/weibo/timeline.js rename to lib/v2/weibo/timeline.js index 90682fbe2..06bc749d7 100644 --- a/lib/routes/weibo/timeline.js +++ b/lib/v2/weibo/timeline.js @@ -125,13 +125,13 @@ module.exports = async (ctx) => { }) ); - ctx.state.data = { + ctx.state.data = weiboUtils.sinaimgTvax({ title: `个人微博时间线--${name}`, link: `http://weibo.com/${uid}/`, description, image: profileImageUrl, item: resultItem, - }; + }); } else if (uid === '0' || ctx.querystring) { const { app_key = '', redirect_url = ctx.request.origin + '/weibo/timeline/0', app_secret = '' } = config.weibo; diff --git a/lib/routes/weibo/user.js b/lib/v2/weibo/user.js similarity index 99% rename from lib/routes/weibo/user.js rename to lib/v2/weibo/user.js index f1b0de2ca..9b3edd945 100644 --- a/lib/routes/weibo/user.js +++ b/lib/v2/weibo/user.js @@ -145,11 +145,11 @@ module.exports = async (ctx) => { resultItems = ordinaryItems; } - ctx.state.data = { + ctx.state.data = weiboUtils.sinaimgTvax({ title: `${name}的微博`, link: `https://weibo.com/${uid}/`, description, image: profileImageUrl, item: resultItems, - }; + }); }; diff --git a/lib/routes/weibo/utils.js b/lib/v2/weibo/utils.js similarity index 92% rename from lib/routes/weibo/utils.js rename to lib/v2/weibo/utils.js index 16d515509..2d657903f 100644 --- a/lib/routes/weibo/utils.js +++ b/lib/v2/weibo/utils.js @@ -4,6 +4,16 @@ const cheerio = require('cheerio'); const { fallback, queryToBoolean, queryToInteger } = require('@/utils/readable-social'); const weiboUtils = { + formatTitle: (html) => + html + .replace(/]*?alt=["']?([^>]+?)["']?\s[^>]*?\/?><\/span>/g, '$1') // 表情转换 + .replace(/(]*>)<\/span>/g, '') // 去掉所有图标 + .replace(//g, '[图片]') + // impossible to have inline script in weibo posts, but CodeQL complains about it + // Dismiss it through the UI: https://github.com/github/codeql/issues/11427 + .replace(/<[^<]*>/g, '') + .replace(/\n/g, ' ') + .trim(), formatExtended: (ctx, status, params = {}, picsPrefixes = []) => { // undefined and strings like "1" is also safely parsed, so no if branch is needed const routeParams = querystring.parse(ctx.params.routeParams); @@ -200,23 +210,11 @@ const weiboUtils = { title += status.user.screen_name + ': '; } if (!status.retweeted_status || showRetweetTextInTitle) { - title += htmlNewLineUnreplaced - .replace(/]*?alt=["']?([^>]+?)["']?\s[^>]*?\/><\/span>/g, '$1') // 表情转换 - .replace(/(]*?>)<\/span>/g, '') // 去掉所有图标 - .replace(//g, '[图片]') - .replace(/<.*?>/g, '') - .replace('\n', '') - .trim(); + title += weiboUtils.formatTitle(htmlNewLineUnreplaced); } if (status.retweeted_status) { title += showEmojiForRetweet ? '🔁 ' : ' - 转发 '; - title += retweeted - .replace(/]*?alt=["']?([^>]+?)["']?\s[^>]*?\/><\/span>/g, '$1') // 表情转换 - .replace(/(]*?>)<\/span>/g, '') // 去掉所有图标 - .replace(//g, '[图片]') - .replace(/<.*?>/g, '') - .replace('\n', '') - .trim(); + title += weiboUtils.formatTitle(retweeted); } if (livePhotoCount > 0) { title += ' '; @@ -417,6 +415,36 @@ const weiboUtils = { } return itemDesc; }, + sinaimgTvax: (() => { + // https://datatracker.ietf.org/doc/html/rfc1808#section-2.4.3 + const regex = /(?<=\/\/)wx(?=[1-4]\.sinaimg\.cn\/)/gi; + const prefixes = ['tva', 'tvax']; + let cnt = 0; + const replace = (html) => { + cnt = (cnt + 1) % 2; + return html.replace(regex, prefixes[cnt]); + }; + const replaceKV = (obj, keys) => { + keys.forEach((key) => { + if (obj[key]) { + obj[key] = replace(obj[key]); + } + }); + }; + const dataKeys = ['description', 'image']; + const itemKeys = ['description']; + return (data) => { + if (data) { + replaceKV(data, dataKeys); + if (data.item) { + data.item.forEach((item) => { + replaceKV(item, itemKeys); + }); + } + } + return data; + }; + })(), }; module.exports = weiboUtils;