From cdba6e5f1af50c626a65550b5a427eddef3a50ea Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 22 Oct 2026 04:02:02 +0000 Subject: [PATCH 1/5] Harden error message rendering against template injection - Pass untrusted data (feed titles, URLs, error messages) as EJS locals instead of concatenating into template source - Add stripEjsDelimiters() function for defense-in-depth sanitization - Apply EJS delimiter sanitization when storing feed titles - Add comprehensive security tests covering template injection scenarios - Update send() function to accept optional template locals parameter This change prevents untrusted remote content from being evaluated as code when rendering error messages and feed change notifications. Co-authored-by: fengkx --- source/index.ts | 27 +++-- source/proxies/rss-feed.ts | 3 +- source/utils/sanitize.ts | 7 ++ source/utils/send.ts | 8 +- test/sanitize.spec.ts | 40 +++++++- test/template-injection.spec.ts | 177 ++++++++++++++++++++++++++++++++ 6 files changed, 249 insertions(+), 13 deletions(-) create mode 100644 test/template-injection.spec.ts diff --git a/source/index.ts b/source/index.ts index 67f0d805214..e3a554f5ddb 100644 --- a/source/index.ts +++ b/source/index.ts @@ -297,22 +297,31 @@ async function startFetchProcess(restartTime: number): Promise { const { feed, err } = message; send( bot, - `${feed.feed_title}: ${feed.url} <%=i18n['ERROR_MANY_TIME']%> ${err.message}`, - feed + '<%= title %>: <%= url %> <%=i18n["ERROR_MANY_TIME"]%> <%= msg %>', + feed, + { + title: feed.feed_title, + url: feed.url, + msg: err.message + } ); } if (isChangeFeedUrl(message)) { const { feed, new_feed } = message; const builder = []; builder.push( - `${feed.feed_title}: <%=i18n['ERROR_MANY_TIME']%>` - ); // feed is from database which not urlencoded - builder.push(`<%=i18n['FOUND_FEEDS']%>:`); + '<%= title %>: <%=i18n["ERROR_MANY_TIME"]%>' + ); + builder.push('<%=i18n["FOUND_FEEDS"]%>:'); builder.push(...new_feed); - builder.push(`<%=i18n['FEED_CHANGE_TO']%> ${new_feed[0]}`); - send(bot, builder.join('\n'), feed); + builder.push( + '<%=i18n["FEED_CHANGE_TO"]%> <%= first_new_feed %>' + ); + send(bot, builder.join('\n'), feed, { + title: feed.feed_title, + url: encodeUrl(feed.url), + first_new_feed: new_feed[0] + }); } } }); diff --git a/source/proxies/rss-feed.ts b/source/proxies/rss-feed.ts index f9f0b7b5224..48d6b49cce6 100644 --- a/source/proxies/rss-feed.ts +++ b/source/proxies/rss-feed.ts @@ -5,6 +5,7 @@ import { Subscribe } from '../types/subscribe'; import { isSome, Option, Optional, Some } from '../types/option'; import { decodeUrl } from '../utils/urlencode'; import arrayShuffle from '../utils/shuffle'; +import { stripEjsDelimiters } from '../utils/sanitize'; export async function sub( userId: number, @@ -37,7 +38,7 @@ export async function sub( .insert( { url: feedUrl, - feed_title: feedTitle, + feed_title: stripEjsDelimiters(feedTitle), ttl: ttl }, 'feed_id' diff --git a/source/utils/sanitize.ts b/source/utils/sanitize.ts index 3896fc29d77..da8a3d8824e 100644 --- a/source/utils/sanitize.ts +++ b/source/utils/sanitize.ts @@ -9,3 +9,10 @@ export default function (s: string): string { .replace(/ +/g, ' ') // display like html ); } + +// Strip EJS template delimiters from untrusted input as defense-in-depth +// This prevents template injection even if the input is incorrectly concatenated +// into a template string elsewhere in the codebase +export function stripEjsDelimiters(s: string): string { + return s.replace(/<%/g, '<%').replace(/%>/g, '%>'); +} diff --git a/source/utils/send.ts b/source/utils/send.ts index 1cc2b284407..ea79a71b18c 100644 --- a/source/utils/send.ts +++ b/source/utils/send.ts @@ -142,7 +142,8 @@ async function handlerSendError(e: any, userId: number): Promise { const send = async ( bot: Telegraf, toSend: NonNullable, - feed: Feed + feed: Feed, + templateLocals?: Record ): Promise => { const subscribers = await getSubscribersByFeedId(feed.feed_id); if (typeof toSend === 'string') { @@ -156,7 +157,10 @@ const send = async ( if (isSome(user)) { lang = user.value.lang; } - const text = ejs.render(tpl, { i18n: i18n[lang] }); + const text = ejs.render(tpl, { + i18n: i18n[lang], + ...templateLocals + }); await sendMessageWithLimit(bot, userId, text, { parse_mode: 'HTML', link_preview_options: { is_disabled: true } diff --git a/test/sanitize.spec.ts b/test/sanitize.spec.ts index e59112c45dc..c681d0440e9 100644 --- a/test/sanitize.spec.ts +++ b/test/sanitize.spec.ts @@ -1,4 +1,4 @@ -import sanitize from '../source/utils/sanitize'; +import sanitize, { stripEjsDelimiters } from '../source/utils/sanitize'; test('escape html character', () => { const cs = '<>'; @@ -14,3 +14,41 @@ test('escape multi space', () => { const cs = 'a\n\n\n\n\n b'; expect(sanitize(cs)).toEqual('a b'); }); + +describe('stripEjsDelimiters', () => { + test('should strip EJS opening delimiter', () => { + const input = '<% code %>'; + expect(stripEjsDelimiters(input)).toBe('<% code %>'); + }); + + test('should strip EJS output delimiter', () => { + const input = '<%= value %>'; + expect(stripEjsDelimiters(input)).toBe('<%= value %>'); + }); + + test('should strip EJS unescaped output delimiter', () => { + const input = '<%- value %>'; + expect(stripEjsDelimiters(input)).toBe('<%- value %>'); + }); + + test('should strip multiple EJS delimiters', () => { + const input = 'Title <%= process.exit(1) %> and <%= "more" %>'; + expect(stripEjsDelimiters(input)).toBe( + 'Title <%= process.exit(1) %> and <%= "more" %>' + ); + }); + + test('should handle text without EJS delimiters', () => { + const input = 'Normal feed title'; + expect(stripEjsDelimiters(input)).toBe('Normal feed title'); + }); + + test('should prevent RCE payload', () => { + const rcePayload = + '<%= require("child_process").execSync("malicious").toString() %>'; + const result = stripEjsDelimiters(rcePayload); + expect(result).toBe( + '<%= require("child_process").execSync("malicious").toString() %>' + ); + }); +}); diff --git a/test/template-injection.spec.ts b/test/template-injection.spec.ts new file mode 100644 index 00000000000..a809019cd29 --- /dev/null +++ b/test/template-injection.spec.ts @@ -0,0 +1,177 @@ +import * as ejs from 'ejs'; +import i18n from '../source/i18n'; + +/** + * Test for Server-Side Template Injection (SSTI) vulnerability + * + * This test reproduces the vulnerability where untrusted data (feed titles, URLs, error messages) + * are concatenated into EJS template strings and then evaluated, allowing code execution. + */ +describe('Template Injection Security Tests', () => { + const lang = 'en'; + + describe('Vulnerability Reproduction - ERROR_MANY_TIME path', () => { + it('should NOT execute code from malicious feed title (old vulnerable code path)', () => { + // Simulate a malicious feed title with EJS code injection + const maliciousFeedTitle = + '<%= global.TEMPLATE_INJECTION_TEST = "EXPLOITED" %>'; + const feedUrl = 'https://example.com/feed.xml'; + const errorMessage = 'Connection timeout'; + + // This simulates the VULNERABLE code pattern from index.ts:300 + // OLD CODE: `${feed.feed_title}: ${feed.url} <%=i18n['ERROR_MANY_TIME']%> ${err.message}` + const vulnerableTemplate = `${maliciousFeedTitle}: ${feedUrl} <%=i18n['ERROR_MANY_TIME']%> ${errorMessage}`; + + // Clear any previous value + delete (global as any).TEMPLATE_INJECTION_TEST; + + // Render the template (this is what happens in send.ts:159) + const result = ejs.render(vulnerableTemplate, { i18n: i18n[lang] }); + + // If the code executed, the global variable will be set + // This demonstrates the vulnerability exists + if ((global as any).TEMPLATE_INJECTION_TEST === 'EXPLOITED') { + // The vulnerability IS present - the malicious code executed + // This test documents the vulnerability before the fix + expect((global as any).TEMPLATE_INJECTION_TEST).toBe( + 'EXPLOITED' + ); + + // Clean up + delete (global as any).TEMPLATE_INJECTION_TEST; + } else { + // The vulnerability is fixed - the code did NOT execute + // After the fix, this should be the case + expect((global as any).TEMPLATE_INJECTION_TEST).toBeUndefined(); + } + }); + + it('should demonstrate RCE potential with process execution attempt', () => { + // A more severe payload that attempts process manipulation + const maliciousFeedTitle = '<%= process.pid %>'; + const feedUrl = 'https://example.com/feed.xml'; + const errorMessage = 'Failed'; + + // Vulnerable pattern + const vulnerableTemplate = `${maliciousFeedTitle}: ${feedUrl} <%=i18n['ERROR_MANY_TIME']%> ${errorMessage}`; + + const result = ejs.render(vulnerableTemplate, { i18n: i18n[lang] }); + + // If process.pid appears in the output, code execution occurred + if (result.includes(String(process.pid))) { + // Vulnerability present - the process.pid was evaluated + expect(result).toContain(String(process.pid)); + } else { + // Vulnerability fixed - should show escaped or literal text instead + expect(result).not.toContain(String(process.pid)); + } + }); + }); + + describe('Vulnerability Reproduction - isChangeFeedUrl path', () => { + it('should NOT execute code from malicious feed title in feed change path', () => { + const maliciousFeedTitle = + '<%= global.FEED_CHANGE_EXPLOITED = "EXPLOITED" %>'; + const feedUrl = 'https://example.com/feed.xml'; + const newFeed = 'https://example.com/newfeed.xml'; + + // This simulates the VULNERABLE code from index.ts:308-314 + const builder = []; + builder.push( + `${maliciousFeedTitle}: <%=i18n['ERROR_MANY_TIME']%>` + ); + builder.push(`<%=i18n['FOUND_FEEDS']%>:`); + builder.push(newFeed); + builder.push(`<%=i18n['FEED_CHANGE_TO']%> ${newFeed}`); + const vulnerableTemplate = builder.join('\n'); + + delete (global as any).FEED_CHANGE_EXPLOITED; + + const result = ejs.render(vulnerableTemplate, { i18n: i18n[lang] }); + + if ((global as any).FEED_CHANGE_EXPLOITED === 'EXPLOITED') { + expect((global as any).FEED_CHANGE_EXPLOITED).toBe('EXPLOITED'); + delete (global as any).FEED_CHANGE_EXPLOITED; + } else { + expect((global as any).FEED_CHANGE_EXPLOITED).toBeUndefined(); + } + }); + }); + + describe('Fixed Implementation - Passing untrusted data as EJS locals', () => { + it('should safely render feed title when passed as EJS local', () => { + const maliciousFeedTitle = + '<%= global.SAFE_TEST = "SHOULD_NOT_EXECUTE" %>'; + const feedUrl = 'https://example.com/feed.xml'; + const errorMessage = 'Connection timeout'; + + // FIXED CODE: Pass untrusted values as locals, reference them with escaped output + const safeTemplate = + '<%= title %>: <%= url %> <%=i18n["ERROR_MANY_TIME"]%> <%= msg %>'; + + delete (global as any).SAFE_TEST; + + const result = ejs.render(safeTemplate, { + title: maliciousFeedTitle, + url: feedUrl, + msg: errorMessage, + i18n: i18n[lang] + }); + + // The malicious code should NOT execute + expect((global as any).SAFE_TEST).toBeUndefined(); + + // The result should contain the escaped/literal title text + expect(result).toContain('<%= global.SAFE_TEST'); + expect(result).toContain(feedUrl); + expect(result).toContain('Unable to fetch for many times'); + }); + + it('should safely render feed change with all untrusted data as locals', () => { + const maliciousFeedTitle = '<%= process.exit(1) %>'; + const feedUrl = 'https://example.com/feed.xml'; + const newFeed = 'https://example.com/newfeed.xml'; + + // FIXED CODE: Safe template with locals + const safeTemplate = + '<%= title %>: <%=i18n["ERROR_MANY_TIME"]%>\n<%=i18n["FOUND_FEEDS"]%>:\n<%= new_feed %>\n<%=i18n["FEED_CHANGE_TO"]%> <%= new_feed %>'; + + const result = ejs.render(safeTemplate, { + title: maliciousFeedTitle, + url: feedUrl, + new_feed: newFeed, + i18n: i18n[lang] + }); + + // Process should still be running (not exited) + expect(process.pid).toBeGreaterThan(0); + + // The result should contain escaped content + expect(result).toContain('<%= process.exit(1) %>'); + expect(result).toContain(newFeed); + }); + + it('should handle malicious error messages safely', () => { + const feedTitle = 'Legitimate Feed'; + const feedUrl = 'https://example.com/feed.xml'; + // err.message could also be attacker-controlled (from remote server responses) + const maliciousErrorMessage = + '<%= require("child_process").execSync("id").toString() %>'; + + const safeTemplate = + '<%= title %>: <%= url %> <%=i18n["ERROR_MANY_TIME"]%> <%= msg %>'; + + const result = ejs.render(safeTemplate, { + title: feedTitle, + url: feedUrl, + msg: maliciousErrorMessage, + i18n: i18n[lang] + }); + + // Should NOT execute the command - verify the template code is escaped + expect(result).toContain('<%='); + expect(result).toContain('require'); + // The result should contain the HTML-escaped template code, not the output of the command + }); + }); +}); From f1b9cb75ae43cce29d72db06f77d1e5091c140d6 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 22 Oct 2026 04:38:20 +0000 Subject: [PATCH 2/5] Address Codex feedback: use raw output and pass all new_feed as locals Key changes: - Use <%- %> (raw/unescaped) instead of <%= %> for untrusted data to preserve original output behavior for benign inputs - Pass entire new_feed array as local and loop in EJS template (no concatenation) - Remove storage-time stripEjsDelimiters (inconsistent, breaks legitimate titles) - Rewrite tests to use production code paths with DB/Telegram mocks - Add regression guards that verify the fix works correctly - Add benign input compatibility tests to ensure output remains identical All template sources now use fixed strings with all untrusted data passed as locals. Co-authored-by: fengkx --- source/index.ts | 24 +- source/proxies/rss-feed.ts | 3 +- source/utils/sanitize.ts | 7 - test/sanitize.spec.ts | 40 +-- test/template-injection.spec.ts | 617 +++++++++++++++++++++++++------- 5 files changed, 492 insertions(+), 199 deletions(-) diff --git a/source/index.ts b/source/index.ts index e3a554f5ddb..630d6f2777d 100644 --- a/source/index.ts +++ b/source/index.ts @@ -297,7 +297,7 @@ async function startFetchProcess(restartTime: number): Promise { const { feed, err } = message; send( bot, - '<%= title %>: <%= url %> <%=i18n["ERROR_MANY_TIME"]%> <%= msg %>', + '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>', feed, { title: feed.feed_title, @@ -308,20 +308,16 @@ async function startFetchProcess(restartTime: number): Promise { } if (isChangeFeedUrl(message)) { const { feed, new_feed } = message; - const builder = []; - builder.push( - '<%= title %>: <%=i18n["ERROR_MANY_TIME"]%>' - ); - builder.push('<%=i18n["FOUND_FEEDS"]%>:'); - builder.push(...new_feed); - builder.push( - '<%=i18n["FEED_CHANGE_TO"]%> <%= first_new_feed %>' + send( + bot, + '<%- title %>: <%=i18n["ERROR_MANY_TIME"]%>\n<%=i18n["FOUND_FEEDS"]%>:\n<% new_feeds.forEach(function(feed_url) { %><%- feed_url %>\n<% }); %><%=i18n["FEED_CHANGE_TO"]%> <%- new_feeds[0] %>', + feed, + { + title: feed.feed_title, + url: encodeUrl(feed.url), + new_feeds: new_feed + } ); - send(bot, builder.join('\n'), feed, { - title: feed.feed_title, - url: encodeUrl(feed.url), - first_new_feed: new_feed[0] - }); } } }); diff --git a/source/proxies/rss-feed.ts b/source/proxies/rss-feed.ts index 48d6b49cce6..f9f0b7b5224 100644 --- a/source/proxies/rss-feed.ts +++ b/source/proxies/rss-feed.ts @@ -5,7 +5,6 @@ import { Subscribe } from '../types/subscribe'; import { isSome, Option, Optional, Some } from '../types/option'; import { decodeUrl } from '../utils/urlencode'; import arrayShuffle from '../utils/shuffle'; -import { stripEjsDelimiters } from '../utils/sanitize'; export async function sub( userId: number, @@ -38,7 +37,7 @@ export async function sub( .insert( { url: feedUrl, - feed_title: stripEjsDelimiters(feedTitle), + feed_title: feedTitle, ttl: ttl }, 'feed_id' diff --git a/source/utils/sanitize.ts b/source/utils/sanitize.ts index da8a3d8824e..3896fc29d77 100644 --- a/source/utils/sanitize.ts +++ b/source/utils/sanitize.ts @@ -9,10 +9,3 @@ export default function (s: string): string { .replace(/ +/g, ' ') // display like html ); } - -// Strip EJS template delimiters from untrusted input as defense-in-depth -// This prevents template injection even if the input is incorrectly concatenated -// into a template string elsewhere in the codebase -export function stripEjsDelimiters(s: string): string { - return s.replace(/<%/g, '<%').replace(/%>/g, '%>'); -} diff --git a/test/sanitize.spec.ts b/test/sanitize.spec.ts index c681d0440e9..e59112c45dc 100644 --- a/test/sanitize.spec.ts +++ b/test/sanitize.spec.ts @@ -1,4 +1,4 @@ -import sanitize, { stripEjsDelimiters } from '../source/utils/sanitize'; +import sanitize from '../source/utils/sanitize'; test('escape html character', () => { const cs = '<>'; @@ -14,41 +14,3 @@ test('escape multi space', () => { const cs = 'a\n\n\n\n\n b'; expect(sanitize(cs)).toEqual('a b'); }); - -describe('stripEjsDelimiters', () => { - test('should strip EJS opening delimiter', () => { - const input = '<% code %>'; - expect(stripEjsDelimiters(input)).toBe('<% code %>'); - }); - - test('should strip EJS output delimiter', () => { - const input = '<%= value %>'; - expect(stripEjsDelimiters(input)).toBe('<%= value %>'); - }); - - test('should strip EJS unescaped output delimiter', () => { - const input = '<%- value %>'; - expect(stripEjsDelimiters(input)).toBe('<%- value %>'); - }); - - test('should strip multiple EJS delimiters', () => { - const input = 'Title <%= process.exit(1) %> and <%= "more" %>'; - expect(stripEjsDelimiters(input)).toBe( - 'Title <%= process.exit(1) %> and <%= "more" %>' - ); - }); - - test('should handle text without EJS delimiters', () => { - const input = 'Normal feed title'; - expect(stripEjsDelimiters(input)).toBe('Normal feed title'); - }); - - test('should prevent RCE payload', () => { - const rcePayload = - '<%= require("child_process").execSync("malicious").toString() %>'; - const result = stripEjsDelimiters(rcePayload); - expect(result).toBe( - '<%= require("child_process").execSync("malicious").toString() %>' - ); - }); -}); diff --git a/test/template-injection.spec.ts b/test/template-injection.spec.ts index a809019cd29..d8fe8d75631 100644 --- a/test/template-injection.spec.ts +++ b/test/template-injection.spec.ts @@ -1,177 +1,520 @@ -import * as ejs from 'ejs'; -import i18n from '../source/i18n'; - /** - * Test for Server-Side Template Injection (SSTI) vulnerability + * Template Injection Security Tests + * + * These tests verify that untrusted data (feed titles, URLs, error messages) + * cannot be used to inject and execute arbitrary code via EJS template processing. * - * This test reproduces the vulnerability where untrusted data (feed titles, URLs, error messages) - * are concatenated into EJS template strings and then evaluated, allowing code execution. + * Tests use production code paths (mocking only DB and Telegram API) to ensure + * the fix is effective in the actual runtime environment. */ -describe('Template Injection Security Tests', () => { - const lang = 'en'; - - describe('Vulnerability Reproduction - ERROR_MANY_TIME path', () => { - it('should NOT execute code from malicious feed title (old vulnerable code path)', () => { - // Simulate a malicious feed title with EJS code injection - const maliciousFeedTitle = - '<%= global.TEMPLATE_INJECTION_TEST = "EXPLOITED" %>'; - const feedUrl = 'https://example.com/feed.xml'; - const errorMessage = 'Connection timeout'; - // This simulates the VULNERABLE code pattern from index.ts:300 - // OLD CODE: `${feed.feed_title}: ${feed.url} <%=i18n['ERROR_MANY_TIME']%> ${err.message}` - const vulnerableTemplate = `${maliciousFeedTitle}: ${feedUrl} <%=i18n['ERROR_MANY_TIME']%> ${errorMessage}`; - - // Clear any previous value - delete (global as any).TEMPLATE_INJECTION_TEST; - - // Render the template (this is what happens in send.ts:159) - const result = ejs.render(vulnerableTemplate, { i18n: i18n[lang] }); - - // If the code executed, the global variable will be set - // This demonstrates the vulnerability exists - if ((global as any).TEMPLATE_INJECTION_TEST === 'EXPLOITED') { - // The vulnerability IS present - the malicious code executed - // This test documents the vulnerability before the fix - expect((global as any).TEMPLATE_INJECTION_TEST).toBe( - 'EXPLOITED' - ); - - // Clean up - delete (global as any).TEMPLATE_INJECTION_TEST; - } else { - // The vulnerability is fixed - the code did NOT execute - // After the fix, this should be the case - expect((global as any).TEMPLATE_INJECTION_TEST).toBeUndefined(); +import { Telegraf } from 'telegraf'; +import send from '../source/utils/send'; +import { Feed } from '../source/types/feed'; +import * as subscribesProxy from '../source/proxies/subscribes'; +import * as usersProxy from '../source/proxies/users'; +import { Optional } from '../source/types/option'; + +// Mock database and Telegram +jest.mock('../source/proxies/subscribes'); +jest.mock('../source/proxies/users'); + +describe('Template Injection Security - Production Code Paths', () => { + let bot: Telegraf; + let sentMessages: Array<{ + userId: number; + text: string; + options: any; + }>; + const mockUserId = 12345; + + beforeEach(() => { + // Reset mocks + sentMessages = []; + + // Create bot with mocked telegram API + bot = new Telegraf('mock-token'); + bot.telegram.sendMessage = jest.fn( + async (userId: number, text: string, options?: any) => { + sentMessages.push({ userId, text, options }); + return {} as any; } + ); + + // Mock database responses + (subscribesProxy.getSubscribersByFeedId as jest.Mock).mockResolvedValue( + [{ user_id: mockUserId, feed_id: 1 }] + ); + + (usersProxy.getUserById as jest.Mock).mockResolvedValue( + Optional({ user_id: mockUserId, lang: 'en', username: 'test' }) + ); + }); + + describe('ERROR_MANY_TIME Path - Template Injection Prevention', () => { + it('should block template injection in feed title', async () => { + const maliciousFeed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: + '<% global.INJECTED_TITLE = "EXPLOITED" %>Malicious', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; + + const errorMessage = 'Connection timeout'; + + // Use production template from index.ts:300 + const template = + '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; + + delete (global as any).INJECTED_TITLE; + + await send(bot, template, maliciousFeed, { + title: maliciousFeed.feed_title, + url: maliciousFeed.url, + msg: errorMessage + }); + + // Template injection should be blocked + expect((global as any).INJECTED_TITLE).toBeUndefined(); + + // Message should be sent with literal EJS code visible as text + expect(sentMessages).toHaveLength(1); + expect(sentMessages[0].text).toContain( + '<% global.INJECTED_TITLE = "EXPLOITED" %>' + ); + expect(sentMessages[0].text).toContain( + 'Unable to fetch for many times' + ); }); - it('should demonstrate RCE potential with process execution attempt', () => { - // A more severe payload that attempts process manipulation - const maliciousFeedTitle = '<%= process.pid %>'; - const feedUrl = 'https://example.com/feed.xml'; - const errorMessage = 'Failed'; + it('should block template injection in error message', async () => { + const feed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: 'Legitimate Feed', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; - // Vulnerable pattern - const vulnerableTemplate = `${maliciousFeedTitle}: ${feedUrl} <%=i18n['ERROR_MANY_TIME']%> ${errorMessage}`; + // err.message is also attacker-controlled (comes from remote servers) + const maliciousError = '<%= global.INJECTED_ERROR = "EXPLOITED" %>'; - const result = ejs.render(vulnerableTemplate, { i18n: i18n[lang] }); + const template = + '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; - // If process.pid appears in the output, code execution occurred - if (result.includes(String(process.pid))) { - // Vulnerability present - the process.pid was evaluated - expect(result).toContain(String(process.pid)); - } else { - // Vulnerability fixed - should show escaped or literal text instead - expect(result).not.toContain(String(process.pid)); - } + delete (global as any).INJECTED_ERROR; + + await send(bot, template, feed, { + title: feed.feed_title, + url: feed.url, + msg: maliciousError + }); + + expect((global as any).INJECTED_ERROR).toBeUndefined(); + expect(sentMessages[0].text).toContain( + '<%= global.INJECTED_ERROR = "EXPLOITED" %>' + ); + }); + + it('should block template injection in URL', async () => { + const maliciousFeed: Feed = { + feed_id: 1, + url: 'https://evil.com/<%=global.INJECTED_URL="EXPLOITED"%>', + feed_title: 'Test Feed', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; + + const template = + '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; + + delete (global as any).INJECTED_URL; + + await send(bot, template, maliciousFeed, { + title: maliciousFeed.feed_title, + url: maliciousFeed.url, + msg: 'Error' + }); + + expect((global as any).INJECTED_URL).toBeUndefined(); }); }); - describe('Vulnerability Reproduction - isChangeFeedUrl path', () => { - it('should NOT execute code from malicious feed title in feed change path', () => { - const maliciousFeedTitle = - '<%= global.FEED_CHANGE_EXPLOITED = "EXPLOITED" %>'; - const feedUrl = 'https://example.com/feed.xml'; - const newFeed = 'https://example.com/newfeed.xml'; - - // This simulates the VULNERABLE code from index.ts:308-314 - const builder = []; - builder.push( - `${maliciousFeedTitle}: <%=i18n['ERROR_MANY_TIME']%>` + describe('isChangeFeedUrl Path - Template Injection Prevention', () => { + it('should block template injection in feed title (feed change path)', async () => { + const maliciousFeed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: + '<%= global.INJECTED_FEED_CHANGE = "EXPLOITED" %>Feed', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; + + const newFeeds = [ + 'https://example.com/newfeed1.xml', + 'https://example.com/newfeed2.xml' + ]; + + // Production template from index.ts:308-314 + const template = + '<%- title %>: <%=i18n["ERROR_MANY_TIME"]%>\n<%=i18n["FOUND_FEEDS"]%>:\n<% new_feeds.forEach(function(feed_url) { %><%- feed_url %>\n<% }); %><%=i18n["FEED_CHANGE_TO"]%> <%- new_feeds[0] %>'; + + delete (global as any).INJECTED_FEED_CHANGE; + + await send(bot, template, maliciousFeed, { + title: maliciousFeed.feed_title, + url: maliciousFeed.url, + new_feeds: newFeeds + }); + + expect((global as any).INJECTED_FEED_CHANGE).toBeUndefined(); + expect(sentMessages[0].text).toContain( + '<%= global.INJECTED_FEED_CHANGE = "EXPLOITED" %>' ); - builder.push(`<%=i18n['FOUND_FEEDS']%>:`); - builder.push(newFeed); - builder.push(`<%=i18n['FEED_CHANGE_TO']%> ${newFeed}`); - const vulnerableTemplate = builder.join('\n'); + }); - delete (global as any).FEED_CHANGE_EXPLOITED; + it('should block template injection in new_feed URLs', async () => { + const feed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: 'Test Feed', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; - const result = ejs.render(vulnerableTemplate, { i18n: i18n[lang] }); + const maliciousNewFeeds = [ + '<%= global.INJECTED_NEWFEED = "EXPLOITED" %>', + 'https://example.com/feed2.xml' + ]; - if ((global as any).FEED_CHANGE_EXPLOITED === 'EXPLOITED') { - expect((global as any).FEED_CHANGE_EXPLOITED).toBe('EXPLOITED'); - delete (global as any).FEED_CHANGE_EXPLOITED; - } else { - expect((global as any).FEED_CHANGE_EXPLOITED).toBeUndefined(); - } + const template = + '<%- title %>: <%=i18n["ERROR_MANY_TIME"]%>\n<%=i18n["FOUND_FEEDS"]%>:\n<% new_feeds.forEach(function(feed_url) { %><%- feed_url %>\n<% }); %><%=i18n["FEED_CHANGE_TO"]%> <%- new_feeds[0] %>'; + + delete (global as any).INJECTED_NEWFEED; + + await send(bot, template, feed, { + title: feed.feed_title, + url: feed.url, + new_feeds: maliciousNewFeeds + }); + + expect((global as any).INJECTED_NEWFEED).toBeUndefined(); + expect(sentMessages[0].text).toContain( + '<%= global.INJECTED_NEWFEED = "EXPLOITED" %>' + ); + }); + + it('should render all new_feed entries correctly', async () => { + const feed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: 'Test Feed', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; + + const newFeeds = [ + 'https://example.com/feed1.xml', + 'https://example.com/feed2.xml', + 'https://example.com/feed3.xml' + ]; + + const template = + '<%- title %>: <%=i18n["ERROR_MANY_TIME"]%>\n<%=i18n["FOUND_FEEDS"]%>:\n<% new_feeds.forEach(function(feed_url) { %><%- feed_url %>\n<% }); %><%=i18n["FEED_CHANGE_TO"]%> <%- new_feeds[0] %>'; + + await send(bot, template, feed, { + title: feed.feed_title, + url: feed.url, + new_feeds: newFeeds + }); + + const text = sentMessages[0].text; + expect(text).toContain('https://example.com/feed1.xml'); + expect(text).toContain('https://example.com/feed2.xml'); + expect(text).toContain('https://example.com/feed3.xml'); }); }); - describe('Fixed Implementation - Passing untrusted data as EJS locals', () => { - it('should safely render feed title when passed as EJS local', () => { - const maliciousFeedTitle = - '<%= global.SAFE_TEST = "SHOULD_NOT_EXECUTE" %>'; - const feedUrl = 'https://example.com/feed.xml'; - const errorMessage = 'Connection timeout'; + describe('Benign Input Compatibility - Output Must Be Identical', () => { + it('should preserve HTML special characters in feed titles', async () => { + const feed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: 'Tech News & Updates', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; - // FIXED CODE: Pass untrusted values as locals, reference them with escaped output - const safeTemplate = - '<%= title %>: <%= url %> <%=i18n["ERROR_MANY_TIME"]%> <%= msg %>'; + const template = + '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; - delete (global as any).SAFE_TEST; + await send(bot, template, feed, { + title: feed.feed_title, + url: feed.url, + msg: 'Timeout' + }); - const result = ejs.render(safeTemplate, { - title: maliciousFeedTitle, - url: feedUrl, - msg: errorMessage, - i18n: i18n[lang] + // Output should contain raw HTML characters (same as original behavior) + expect(sentMessages[0].text).toContain( + 'Tech News & Updates' + ); + }); + + it('should preserve legitimate EJS-like strings in titles', async () => { + const feed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: 'Programming Tips: <%= best practices %>', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; + + const template = + '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; + + await send(bot, template, feed, { + title: feed.feed_title, + url: feed.url, + msg: 'Error' }); - // The malicious code should NOT execute - expect((global as any).SAFE_TEST).toBeUndefined(); + // Legitimate <%= %> in title should appear as-is + expect(sentMessages[0].text).toContain( + 'Programming Tips: <%= best practices %>' + ); + }); + + it('should handle URLs with query parameters correctly', async () => { + const feed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml?key=value&foo=bar', + feed_title: 'Test Feed', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; - // The result should contain the escaped/literal title text - expect(result).toContain('<%= global.SAFE_TEST'); - expect(result).toContain(feedUrl); - expect(result).toContain('Unable to fetch for many times'); + const template = + '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; + + await send(bot, template, feed, { + title: feed.feed_title, + url: feed.url, + msg: 'Error' + }); + + expect(sentMessages[0].text).toContain( + 'https://example.com/feed.xml?key=value&foo=bar' + ); }); - it('should safely render feed change with all untrusted data as locals', () => { - const maliciousFeedTitle = '<%= process.exit(1) %>'; - const feedUrl = 'https://example.com/feed.xml'; - const newFeed = 'https://example.com/newfeed.xml'; + it('should produce correct message format for benign error case', async () => { + const feed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: 'My RSS Feed', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; - // FIXED CODE: Safe template with locals - const safeTemplate = - '<%= title %>: <%=i18n["ERROR_MANY_TIME"]%>\n<%=i18n["FOUND_FEEDS"]%>:\n<%= new_feed %>\n<%=i18n["FEED_CHANGE_TO"]%> <%= new_feed %>'; + const template = + '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; - const result = ejs.render(safeTemplate, { - title: maliciousFeedTitle, - url: feedUrl, - new_feed: newFeed, - i18n: i18n[lang] + await send(bot, template, feed, { + title: feed.feed_title, + url: feed.url, + msg: 'Connection timeout' }); - // Process should still be running (not exited) - expect(process.pid).toBeGreaterThan(0); + const text = sentMessages[0].text; - // The result should contain escaped content - expect(result).toContain('<%= process.exit(1) %>'); - expect(result).toContain(newFeed); + // Verify structure matches expected output + expect(text).toMatch( + /^My RSS Feed: https:\/\/example\.com\/feed\.xml<\/a> Unable to fetch for many times, you might want to unsub it Connection timeout$/ + ); }); - it('should handle malicious error messages safely', () => { - const feedTitle = 'Legitimate Feed'; - const feedUrl = 'https://example.com/feed.xml'; - // err.message could also be attacker-controlled (from remote server responses) - const maliciousErrorMessage = - '<%= require("child_process").execSync("id").toString() %>'; - - const safeTemplate = - '<%= title %>: <%= url %> <%=i18n["ERROR_MANY_TIME"]%> <%= msg %>'; - - const result = ejs.render(safeTemplate, { - title: feedTitle, - url: feedUrl, - msg: maliciousErrorMessage, - i18n: i18n[lang] + it('should work with different languages', async () => { + // Mock Chinese user + (usersProxy.getUserById as jest.Mock).mockResolvedValue( + Optional({ + user_id: mockUserId, + lang: 'zh-cn', + username: 'test' + }) + ); + + const feed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: 'Test Feed', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; + + const template = + '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; + + await send(bot, template, feed, { + title: feed.feed_title, + url: feed.url, + msg: 'Error' }); - // Should NOT execute the command - verify the template code is escaped - expect(result).toContain('<%='); - expect(result).toContain('require'); - // The result should contain the HTML-escaped template code, not the output of the command + const text = sentMessages[0].text; + // Should contain Chinese translation + expect(text).toMatch(/多次/); // Part of Chinese error message + }); + }); + + describe('Regression Guard - Must Use Fixed Pattern', () => { + it('GUARD: production pattern must pass title as local (not concatenated)', async () => { + const feed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: '<% global.GUARD_TITLE = "VULNERABLE" %>Test', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; + + // FIXED pattern - title passed as local + const fixedTemplate = + '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; + + delete (global as any).GUARD_TITLE; + + await send(bot, fixedTemplate, feed, { + title: feed.feed_title, + url: feed.url, + msg: 'Error' + }); + + // With proper locals, code should NOT execute + expect((global as any).GUARD_TITLE).toBeUndefined(); + + // The literal EJS code should appear in the message + expect(sentMessages[0].text).toContain('<% global.GUARD_TITLE'); + }); + + it('GUARD: production pattern must pass err.message as local (not concatenated)', async () => { + const feed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: 'Test', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; + + const maliciousError = '<% global.GUARD_MSG = "VULNERABLE" %>'; + + // FIXED pattern - error message passed as local + const fixedTemplate = + '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; + + delete (global as any).GUARD_MSG; + + await send(bot, fixedTemplate, feed, { + title: feed.feed_title, + url: feed.url, + msg: maliciousError + }); + + // With proper locals, code should NOT execute + expect((global as any).GUARD_MSG).toBeUndefined(); + + // The literal EJS code should appear in the message + expect(sentMessages[0].text).toContain('<% global.GUARD_MSG'); + }); + + it('GUARD: new_feed array must be passed as local and looped in template', async () => { + const feed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: 'Test', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; + + const maliciousNewFeeds = [ + '<% global.GUARD_NEWFEED = "VULNERABLE" %>', + 'https://example.com/feed2.xml' + ]; + + // FIXED pattern - new_feeds passed as local and looped + const fixedTemplate = + '<%- title %>: \n<% new_feeds.forEach(function(feed_url) { %><%- feed_url %>\n<% }); %>'; + + delete (global as any).GUARD_NEWFEED; + + await send(bot, fixedTemplate, feed, { + title: feed.feed_title, + url: feed.url, + new_feeds: maliciousNewFeeds + }); + + // With proper locals, code should NOT execute + expect((global as any).GUARD_NEWFEED).toBeUndefined(); + + // The literal EJS code should appear in the message + expect(sentMessages[0].text).toContain('<% global.GUARD_NEWFEED'); }); }); }); From fdc87a3944fdf4cc1b20cf1f7002d3a34fbb1725 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 22 Oct 2026 04:53:11 +0000 Subject: [PATCH 3/5] Use escaped output for feed data in notification templates - Switch from <%- %> to <%= %> for all user/feed-controlled data - Extract notification construction into testable functions - Rewrite tests to use production message builders - Remove executable code from tests (use inert HTML chars) - Add full-text assertions for complete message verification Behavior changes: - HTML special characters (<, >, &, quotes) in feed titles, URLs, and error messages are now escaped - Literal EJS delimiters in titles are preserved (not interpreted) - Plain inputs without special characters render identically to before Co-authored-by: fengkx --- source/index.ts | 28 +- source/utils/notification-messages.ts | 64 ++++ test/template-injection.spec.ts | 465 +++++++++++--------------- 3 files changed, 266 insertions(+), 291 deletions(-) create mode 100644 source/utils/notification-messages.ts diff --git a/source/index.ts b/source/index.ts index 630d6f2777d..727e523c23f 100644 --- a/source/index.ts +++ b/source/index.ts @@ -53,6 +53,10 @@ import { } from './types/message'; import { migrateUser } from './proxies/users'; import { encodeUrl } from './utils/urlencode'; +import { + buildErrorNotification, + buildFeedChangeNotification +} from './utils/notification-messages'; const bot = new Telegraf(token, { telegram: { @@ -295,29 +299,17 @@ async function startFetchProcess(restartTime: number): Promise { } else { if (isErrorMaxTime(message)) { const { feed, err } = message; - send( - bot, - '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>', - feed, - { - title: feed.feed_title, - url: feed.url, - msg: err.message - } - ); + const [template, locals] = buildErrorNotification(feed, err); + send(bot, template, feed, locals); } if (isChangeFeedUrl(message)) { const { feed, new_feed } = message; - send( - bot, - '<%- title %>: <%=i18n["ERROR_MANY_TIME"]%>\n<%=i18n["FOUND_FEEDS"]%>:\n<% new_feeds.forEach(function(feed_url) { %><%- feed_url %>\n<% }); %><%=i18n["FEED_CHANGE_TO"]%> <%- new_feeds[0] %>', + const [template, locals] = buildFeedChangeNotification( feed, - { - title: feed.feed_title, - url: encodeUrl(feed.url), - new_feeds: new_feed - } + new_feed, + encodeUrl ); + send(bot, template, feed, locals); } } }); diff --git a/source/utils/notification-messages.ts b/source/utils/notification-messages.ts new file mode 100644 index 00000000000..f51c029db8f --- /dev/null +++ b/source/utils/notification-messages.ts @@ -0,0 +1,64 @@ +/** + * Notification message templates for RSS feed errors and changes. + * All user-controlled data (titles, URLs, error messages) is passed as EJS locals + * and rendered with escaped output (<%= %>) to prevent injection of Telegram HTML. + */ + +import { Feed } from '../types/feed'; +import { Messenger } from '../types/message'; + +export interface ErrorNotificationData { + title: string; + url: string; + errorMessage: string; +} + +export interface FeedChangeNotificationData { + title: string; + url: string; + newFeedUrls: string[]; +} + +/** + * Build error notification message template and locals. + * Used when a feed fails to fetch multiple times (ERROR_MANY_TIME). + */ +export function buildErrorNotification( + feed: Feed, + error: Messenger +): [string, ErrorNotificationData] { + const template = + '<%= title %>: <%= url %> <%=i18n["ERROR_MANY_TIME"]%> <%= errorMessage %>'; + + const locals: ErrorNotificationData = { + title: feed.feed_title, + url: feed.url, + errorMessage: error.message + }; + + return [template, locals]; +} + +/** + * Build feed URL change notification message template and locals. + * Used when RSS feed URL changes are detected (isChangeFeedUrl). + */ +export function buildFeedChangeNotification( + feed: Feed, + newFeedUrls: string[], + urlEncoder: (url: string) => string +): [string, FeedChangeNotificationData] { + const template = + '<%= title %>: <%=i18n["ERROR_MANY_TIME"]%>\n' + + '<%=i18n["FOUND_FEEDS"]%>:\n' + + '<% newFeedUrls.forEach(function(feed_url) { %><%= feed_url %>\n<% }); %>' + + '<%=i18n["FEED_CHANGE_TO"]%> <%= newFeedUrls[0] %>'; + + const locals: FeedChangeNotificationData = { + title: feed.feed_title, + url: urlEncoder(feed.url), + newFeedUrls: newFeedUrls + }; + + return [template, locals]; +} diff --git a/test/template-injection.spec.ts b/test/template-injection.spec.ts index d8fe8d75631..6078c3a0f9f 100644 --- a/test/template-injection.spec.ts +++ b/test/template-injection.spec.ts @@ -1,11 +1,11 @@ /** - * Template Injection Security Tests + * Notification Template Security Tests * - * These tests verify that untrusted data (feed titles, URLs, error messages) - * cannot be used to inject and execute arbitrary code via EJS template processing. + * These tests verify that feed titles, URLs, and error messages in notification + * templates are properly escaped and cannot inject Telegram HTML or malformed markup. * - * Tests use production code paths (mocking only DB and Telegram API) to ensure - * the fix is effective in the actual runtime environment. + * Tests use the actual production message construction functions to ensure the fix + * is effective at the real call sites. */ import { Telegraf } from 'telegraf'; @@ -14,12 +14,17 @@ import { Feed } from '../source/types/feed'; import * as subscribesProxy from '../source/proxies/subscribes'; import * as usersProxy from '../source/proxies/users'; import { Optional } from '../source/types/option'; +import { + buildErrorNotification, + buildFeedChangeNotification +} from '../source/utils/notification-messages'; +import { encodeUrl } from '../source/utils/urlencode'; // Mock database and Telegram jest.mock('../source/proxies/subscribes'); jest.mock('../source/proxies/users'); -describe('Template Injection Security - Production Code Paths', () => { +describe('Notification Template Security - Production Functions', () => { let bot: Telegraf; let sentMessages: Array<{ userId: number; @@ -51,13 +56,12 @@ describe('Template Injection Security - Production Code Paths', () => { ); }); - describe('ERROR_MANY_TIME Path - Template Injection Prevention', () => { - it('should block template injection in feed title', async () => { - const maliciousFeed: Feed = { + describe('Error Notification - buildErrorNotification()', () => { + it('should escape HTML special characters in feed title', async () => { + const feed: Feed = { feed_id: 1, url: 'https://example.com/feed.xml', - feed_title: - '<% global.INJECTED_TITLE = "EXPLOITED" %>Malicious', + feed_title: 'Tech News & "Updates"', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -66,38 +70,33 @@ describe('Template Injection Security - Production Code Paths', () => { next_fetch_time: new Date() }; - const errorMessage = 'Connection timeout'; - - // Use production template from index.ts:300 - const template = - '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; - - delete (global as any).INJECTED_TITLE; - - await send(bot, template, maliciousFeed, { - title: maliciousFeed.feed_title, - url: maliciousFeed.url, - msg: errorMessage - }); + const error = new Error('Connection timeout'); + const [template, locals] = buildErrorNotification(feed, error); - // Template injection should be blocked - expect((global as any).INJECTED_TITLE).toBeUndefined(); + await send(bot, template, feed, locals); - // Message should be sent with literal EJS code visible as text expect(sentMessages).toHaveLength(1); - expect(sentMessages[0].text).toContain( - '<% global.INJECTED_TITLE = "EXPLOITED" %>' + const text = sentMessages[0].text; + + // Title should be HTML-escaped (EJS uses " for quotes) + expect(text).toContain( + 'Tech News <Daily> & "Updates"' ); - expect(sentMessages[0].text).toContain( - 'Unable to fetch for many times' + // Link markup should remain literal + expect(text).toContain(''); + // i18n text should be present + expect(text).toContain( + 'Unable to fetch for many times, you might want to unsub it' ); + // Error message should be present + expect(text).toContain('Connection timeout'); }); - it('should block template injection in error message', async () => { + it('should escape HTML special characters in URL', async () => { const feed: Feed = { feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: 'Legitimate Feed', + url: 'https://example.com/feed?a=1&b=2', + feed_title: 'Test Feed', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -106,30 +105,20 @@ describe('Template Injection Security - Production Code Paths', () => { next_fetch_time: new Date() }; - // err.message is also attacker-controlled (comes from remote servers) - const maliciousError = '<%= global.INJECTED_ERROR = "EXPLOITED" %>'; + const error = new Error('Timeout'); + const [template, locals] = buildErrorNotification(feed, error); - const template = - '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; + await send(bot, template, feed, locals); - delete (global as any).INJECTED_ERROR; - - await send(bot, template, feed, { - title: feed.feed_title, - url: feed.url, - msg: maliciousError - }); - - expect((global as any).INJECTED_ERROR).toBeUndefined(); - expect(sentMessages[0].text).toContain( - '<%= global.INJECTED_ERROR = "EXPLOITED" %>' - ); + const text = sentMessages[0].text; + // URL with & should be escaped + expect(text).toContain('https://example.com/feed?a=1&b=2'); }); - it('should block template injection in URL', async () => { - const maliciousFeed: Feed = { + it('should escape HTML special characters in error message', async () => { + const feed: Feed = { feed_id: 1, - url: 'https://evil.com/<%=global.INJECTED_URL="EXPLOITED"%>', + url: 'https://example.com/feed.xml', feed_title: 'Test Feed', error_count: 0, ttl: 60, @@ -139,28 +128,25 @@ describe('Template Injection Security - Production Code Paths', () => { next_fetch_time: new Date() }; - const template = - '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; - - delete (global as any).INJECTED_URL; + const error = new Error( + 'Error: returned "Invalid" & failed' + ); + const [template, locals] = buildErrorNotification(feed, error); - await send(bot, template, maliciousFeed, { - title: maliciousFeed.feed_title, - url: maliciousFeed.url, - msg: 'Error' - }); + await send(bot, template, feed, locals); - expect((global as any).INJECTED_URL).toBeUndefined(); + const text = sentMessages[0].text; + // Error message should be HTML-escaped (EJS uses " for quotes) + expect(text).toContain( + 'Error: <Server> returned "Invalid" & failed' + ); }); - }); - describe('isChangeFeedUrl Path - Template Injection Prevention', () => { - it('should block template injection in feed title (feed change path)', async () => { - const maliciousFeed: Feed = { + it('should render literal EJS delimiters in feed title', async () => { + const feed: Feed = { feed_id: 1, url: 'https://example.com/feed.xml', - feed_title: - '<%= global.INJECTED_FEED_CHANGE = "EXPLOITED" %>Feed', + feed_title: 'Programming Tips: use <%= variable %> syntax', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -169,34 +155,23 @@ describe('Template Injection Security - Production Code Paths', () => { next_fetch_time: new Date() }; - const newFeeds = [ - 'https://example.com/newfeed1.xml', - 'https://example.com/newfeed2.xml' - ]; - - // Production template from index.ts:308-314 - const template = - '<%- title %>: <%=i18n["ERROR_MANY_TIME"]%>\n<%=i18n["FOUND_FEEDS"]%>:\n<% new_feeds.forEach(function(feed_url) { %><%- feed_url %>\n<% }); %><%=i18n["FEED_CHANGE_TO"]%> <%- new_feeds[0] %>'; + const error = new Error('Timeout'); + const [template, locals] = buildErrorNotification(feed, error); - delete (global as any).INJECTED_FEED_CHANGE; + await send(bot, template, feed, locals); - await send(bot, template, maliciousFeed, { - title: maliciousFeed.feed_title, - url: maliciousFeed.url, - new_feeds: newFeeds - }); - - expect((global as any).INJECTED_FEED_CHANGE).toBeUndefined(); - expect(sentMessages[0].text).toContain( - '<%= global.INJECTED_FEED_CHANGE = "EXPLOITED" %>' + const text = sentMessages[0].text; + // EJS delimiters should be escaped and appear literally + expect(text).toContain( + 'Programming Tips: use <%= variable %> syntax' ); }); - it('should block template injection in new_feed URLs', async () => { + it('should produce complete correct message for plain input', async () => { const feed: Feed = { feed_id: 1, url: 'https://example.com/feed.xml', - feed_title: 'Test Feed', + feed_title: 'My RSS Feed', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -205,33 +180,27 @@ describe('Template Injection Security - Production Code Paths', () => { next_fetch_time: new Date() }; - const maliciousNewFeeds = [ - '<%= global.INJECTED_NEWFEED = "EXPLOITED" %>', - 'https://example.com/feed2.xml' - ]; - - const template = - '<%- title %>: <%=i18n["ERROR_MANY_TIME"]%>\n<%=i18n["FOUND_FEEDS"]%>:\n<% new_feeds.forEach(function(feed_url) { %><%- feed_url %>\n<% }); %><%=i18n["FEED_CHANGE_TO"]%> <%- new_feeds[0] %>'; + const error = new Error('Connection timeout'); + const [template, locals] = buildErrorNotification(feed, error); - delete (global as any).INJECTED_NEWFEED; + await send(bot, template, feed, locals); - await send(bot, template, feed, { - title: feed.feed_title, - url: feed.url, - new_feeds: maliciousNewFeeds - }); + const text = sentMessages[0].text; - expect((global as any).INJECTED_NEWFEED).toBeUndefined(); - expect(sentMessages[0].text).toContain( - '<%= global.INJECTED_NEWFEED = "EXPLOITED" %>' + // Complete message structure (plain inputs, no escaping needed) + expect(text).toBe( + 'My RSS Feed: https://example.com/feed.xml ' + + 'Unable to fetch for many times, you might want to unsub it Connection timeout' ); }); + }); - it('should render all new_feed entries correctly', async () => { + describe('Feed Change Notification - buildFeedChangeNotification()', () => { + it('should escape HTML special characters in feed title', async () => { const feed: Feed = { feed_id: 1, url: 'https://example.com/feed.xml', - feed_title: 'Test Feed', + feed_title: 'News & Updates', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -240,34 +209,30 @@ describe('Template Injection Security - Production Code Paths', () => { next_fetch_time: new Date() }; - const newFeeds = [ - 'https://example.com/feed1.xml', - 'https://example.com/feed2.xml', - 'https://example.com/feed3.xml' + const newFeedUrls = [ + 'https://example.com/newfeed1.xml', + 'https://example.com/newfeed2.xml' ]; - const template = - '<%- title %>: <%=i18n["ERROR_MANY_TIME"]%>\n<%=i18n["FOUND_FEEDS"]%>:\n<% new_feeds.forEach(function(feed_url) { %><%- feed_url %>\n<% }); %><%=i18n["FEED_CHANGE_TO"]%> <%- new_feeds[0] %>'; + const [template, locals] = buildFeedChangeNotification( + feed, + newFeedUrls, + encodeUrl + ); - await send(bot, template, feed, { - title: feed.feed_title, - url: feed.url, - new_feeds: newFeeds - }); + await send(bot, template, feed, locals); const text = sentMessages[0].text; - expect(text).toContain('https://example.com/feed1.xml'); - expect(text).toContain('https://example.com/feed2.xml'); - expect(text).toContain('https://example.com/feed3.xml'); + + // Title should be HTML-escaped + expect(text).toContain('News <Daily> & Updates'); }); - }); - describe('Benign Input Compatibility - Output Must Be Identical', () => { - it('should preserve HTML special characters in feed titles', async () => { + it('should escape HTML special characters in all new feed URLs', async () => { const feed: Feed = { feed_id: 1, url: 'https://example.com/feed.xml', - feed_title: 'Tech News & Updates', + feed_title: 'Test Feed', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -276,53 +241,34 @@ describe('Template Injection Security - Production Code Paths', () => { next_fetch_time: new Date() }; - const template = - '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; - - await send(bot, template, feed, { - title: feed.feed_title, - url: feed.url, - msg: 'Timeout' - }); + const newFeedUrls = [ + 'https://example.com/feed1?a=1&b=2', + 'https://example.com/feed2?x=&y="test"', + 'https://example.com/feed3?z=3' + ]; - // Output should contain raw HTML characters (same as original behavior) - expect(sentMessages[0].text).toContain( - 'Tech News & Updates' + const [template, locals] = buildFeedChangeNotification( + feed, + newFeedUrls, + encodeUrl ); - }); - it('should preserve legitimate EJS-like strings in titles', async () => { - const feed: Feed = { - feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: 'Programming Tips: <%= best practices %>', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - - const template = - '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; + await send(bot, template, feed, locals); - await send(bot, template, feed, { - title: feed.feed_title, - url: feed.url, - msg: 'Error' - }); + const text = sentMessages[0].text; - // Legitimate <%= %> in title should appear as-is - expect(sentMessages[0].text).toContain( - 'Programming Tips: <%= best practices %>' + // All URLs should be HTML-escaped (EJS uses " for quotes) + expect(text).toContain('https://example.com/feed1?a=1&b=2'); + expect(text).toContain( + 'https://example.com/feed2?x=<value>&y="test"' ); + expect(text).toContain('https://example.com/feed3?z=3'); }); - it('should handle URLs with query parameters correctly', async () => { + it('should render all new feed URLs in the list', async () => { const feed: Feed = { feed_id: 1, - url: 'https://example.com/feed.xml?key=value&foo=bar', + url: 'https://example.com/feed.xml', feed_title: 'Test Feed', error_count: 0, ttl: 60, @@ -332,64 +278,37 @@ describe('Template Injection Security - Production Code Paths', () => { next_fetch_time: new Date() }; - const template = - '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; - - await send(bot, template, feed, { - title: feed.feed_title, - url: feed.url, - msg: 'Error' - }); + const newFeedUrls = [ + 'https://example.com/feed1.xml', + 'https://example.com/feed2.xml', + 'https://example.com/feed3.xml' + ]; - expect(sentMessages[0].text).toContain( - 'https://example.com/feed.xml?key=value&foo=bar' + const [template, locals] = buildFeedChangeNotification( + feed, + newFeedUrls, + encodeUrl ); - }); - - it('should produce correct message format for benign error case', async () => { - const feed: Feed = { - feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: 'My RSS Feed', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - - const template = - '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; - await send(bot, template, feed, { - title: feed.feed_title, - url: feed.url, - msg: 'Connection timeout' - }); + await send(bot, template, feed, locals); const text = sentMessages[0].text; - // Verify structure matches expected output - expect(text).toMatch( - /^My RSS Feed: https:\/\/example\.com\/feed\.xml<\/a> Unable to fetch for many times, you might want to unsub it Connection timeout$/ - ); - }); + // All three URLs should appear in order + const feed1Idx = text.indexOf('https://example.com/feed1.xml'); + const feed2Idx = text.indexOf('https://example.com/feed2.xml'); + const feed3Idx = text.indexOf('https://example.com/feed3.xml'); - it('should work with different languages', async () => { - // Mock Chinese user - (usersProxy.getUserById as jest.Mock).mockResolvedValue( - Optional({ - user_id: mockUserId, - lang: 'zh-cn', - username: 'test' - }) - ); + expect(feed1Idx).toBeGreaterThan(-1); + expect(feed2Idx).toBeGreaterThan(feed1Idx); + expect(feed3Idx).toBeGreaterThan(feed2Idx); + }); + it('should render literal EJS delimiters in new feed URLs', async () => { const feed: Feed = { feed_id: 1, url: 'https://example.com/feed.xml', - feed_title: 'Test Feed', + feed_title: 'Test', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -398,27 +317,29 @@ describe('Template Injection Security - Production Code Paths', () => { next_fetch_time: new Date() }; - const template = - '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; + const newFeedUrls = ['https://example.com/<%= id %>/feed.xml']; + + const [template, locals] = buildFeedChangeNotification( + feed, + newFeedUrls, + encodeUrl + ); - await send(bot, template, feed, { - title: feed.feed_title, - url: feed.url, - msg: 'Error' - }); + await send(bot, template, feed, locals); const text = sentMessages[0].text; - // Should contain Chinese translation - expect(text).toMatch(/多次/); // Part of Chinese error message + + // EJS delimiters should be escaped and appear literally + expect(text).toContain( + 'https://example.com/<%= id %>/feed.xml' + ); }); - }); - describe('Regression Guard - Must Use Fixed Pattern', () => { - it('GUARD: production pattern must pass title as local (not concatenated)', async () => { + it('should produce complete correct message structure', async () => { const feed: Feed = { feed_id: 1, url: 'https://example.com/feed.xml', - feed_title: '<% global.GUARD_TITLE = "VULNERABLE" %>Test', + feed_title: 'Test Feed', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -427,26 +348,35 @@ describe('Template Injection Security - Production Code Paths', () => { next_fetch_time: new Date() }; - // FIXED pattern - title passed as local - const fixedTemplate = - '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; + const newFeedUrls = [ + 'https://example.com/newfeed1.xml', + 'https://example.com/newfeed2.xml' + ]; - delete (global as any).GUARD_TITLE; + const [template, locals] = buildFeedChangeNotification( + feed, + newFeedUrls, + encodeUrl + ); - await send(bot, fixedTemplate, feed, { - title: feed.feed_title, - url: feed.url, - msg: 'Error' - }); + await send(bot, template, feed, locals); - // With proper locals, code should NOT execute - expect((global as any).GUARD_TITLE).toBeUndefined(); + const text = sentMessages[0].text; - // The literal EJS code should appear in the message - expect(sentMessages[0].text).toContain('<% global.GUARD_TITLE'); + // Verify complete structure with newlines + expect(text).toBe( + 'Test Feed: ' + + 'Unable to fetch for many times, you might want to unsub it\n' + + 'Found feeds:\n' + + 'https://example.com/newfeed1.xml\n' + + 'https://example.com/newfeed2.xml\n' + + 'Feed Change to https://example.com/newfeed1.xml' + ); }); + }); - it('GUARD: production pattern must pass err.message as local (not concatenated)', async () => { + describe('No Double Escaping', () => { + it('should not double-escape already-encoded URLs', async () => { const feed: Feed = { feed_id: 1, url: 'https://example.com/feed.xml', @@ -459,32 +389,34 @@ describe('Template Injection Security - Production Code Paths', () => { next_fetch_time: new Date() }; - const maliciousError = '<% global.GUARD_MSG = "VULNERABLE" %>'; - - // FIXED pattern - error message passed as local - const fixedTemplate = - '<%- title %>: <%- url %> <%=i18n["ERROR_MANY_TIME"]%> <%- msg %>'; - - delete (global as any).GUARD_MSG; + const error = new Error('Timeout'); + const [template, locals] = buildErrorNotification(feed, error); - await send(bot, fixedTemplate, feed, { - title: feed.feed_title, - url: feed.url, - msg: maliciousError - }); + await send(bot, template, feed, locals); - // With proper locals, code should NOT execute - expect((global as any).GUARD_MSG).toBeUndefined(); + const text = sentMessages[0].text; - // The literal EJS code should appear in the message - expect(sentMessages[0].text).toContain('<% global.GUARD_MSG'); + // URL should appear with & escaped exactly once + // If double-escaped, it would be &amp; + expect(text).not.toContain('&amp;'); }); + }); + + describe('Multi-Language Support', () => { + it('should work with Chinese language', async () => { + // Mock Chinese user + (usersProxy.getUserById as jest.Mock).mockResolvedValue( + Optional({ + user_id: mockUserId, + lang: 'zh-cn', + username: 'test' + }) + ); - it('GUARD: new_feed array must be passed as local and looped in template', async () => { const feed: Feed = { feed_id: 1, url: 'https://example.com/feed.xml', - feed_title: 'Test', + feed_title: 'Test Feed', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -493,28 +425,15 @@ describe('Template Injection Security - Production Code Paths', () => { next_fetch_time: new Date() }; - const maliciousNewFeeds = [ - '<% global.GUARD_NEWFEED = "VULNERABLE" %>', - 'https://example.com/feed2.xml' - ]; - - // FIXED pattern - new_feeds passed as local and looped - const fixedTemplate = - '<%- title %>: \n<% new_feeds.forEach(function(feed_url) { %><%- feed_url %>\n<% }); %>'; - - delete (global as any).GUARD_NEWFEED; + const error = new Error('Connection timeout'); + const [template, locals] = buildErrorNotification(feed, error); - await send(bot, fixedTemplate, feed, { - title: feed.feed_title, - url: feed.url, - new_feeds: maliciousNewFeeds - }); + await send(bot, template, feed, locals); - // With proper locals, code should NOT execute - expect((global as any).GUARD_NEWFEED).toBeUndefined(); + const text = sentMessages[0].text; - // The literal EJS code should appear in the message - expect(sentMessages[0].text).toContain('<% global.GUARD_NEWFEED'); + // Should contain Chinese translation + expect(text).toMatch(/多次/); // Part of Chinese error message }); }); }); From 9048ac158fa8ab615b433f407d7654b8fcadefe3 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 22 Oct 2026 05:26:32 +0000 Subject: [PATCH 4/5] Fix double-escaping and add comprehensive full-text tests 1. HTML entity decoding in feed discovery: - Created html-entities decoder for href attributes - findFeed() now decodes & etc. before URL parsing - Prevents double-escaping in notification output 2. Comprehensive test coverage: - Full-text (toBe) assertions cover all escaped outputs - Tests verify href attribute, link text, list items, trailing URL - Added tests for &, quotes, <>, Chinese text in all positions - Any change from <%= to <%- will fail at least one test 3. Feed discovery tests: - Test entity decoding from HTML through to URL output - Cover named, numeric, and hex entities Test results: 17 suites, 76 tests, all passed Co-authored-by: fengkx --- source/utils/feed.ts | 11 +- source/utils/html-entities.ts | 27 ++++ test/feed-discovery.spec.ts | 114 ++++++++++++++ test/html-entities.spec.ts | 47 ++++++ test/template-injection.spec.ts | 253 +++++++++++++++++++------------- 5 files changed, 346 insertions(+), 106 deletions(-) create mode 100644 source/utils/html-entities.ts create mode 100644 test/feed-discovery.spec.ts create mode 100644 test/html-entities.spec.ts diff --git a/source/utils/feed.ts b/source/utils/feed.ts index abb37da1239..e83e1dfd74d 100644 --- a/source/utils/feed.ts +++ b/source/utils/feed.ts @@ -2,6 +2,7 @@ import { none, Option, Optional } from '../types/option'; import { parseString, TRSS } from '../parser/parse'; import { FeedItem } from '../types/feed'; import hashFeed from './hash-feed'; +import { decodeHtmlEntities } from './html-entities'; export async function isFeedValid(feedStr: string): Promise> { try { @@ -24,14 +25,20 @@ export async function findFeed( return linksTag .filter((t) => t.match(/rss|atom/) && t.includes('href')) .map((linkTag) => { - const url = linkTag.match(/href="(.+?)"/)[1]; + const hrefMatch = linkTag.match(/href="(.+?)"/); + if (!hrefMatch) { + return ''; + } + // Decode HTML entities from href attribute value + const url = decodeHtmlEntities(hrefMatch[1]); try { return new URL(url).toString(); } catch { reqURL.pathname = url; return reqURL.toString(); } - }); + }) + .filter((url) => url !== ''); } export async function getNewItems( diff --git a/source/utils/html-entities.ts b/source/utils/html-entities.ts new file mode 100644 index 00000000000..e11ee895ba9 --- /dev/null +++ b/source/utils/html-entities.ts @@ -0,0 +1,27 @@ +/** + * Decode HTML entities in a string. + * Handles common entities found in HTML href attributes. + */ +export function decodeHtmlEntities(text: string): string { + // Map of named entities to characters + const entities: Record = { + amp: '&', + lt: '<', + gt: '>', + quot: '"', + apos: "'", + nbsp: '\u00A0' + }; + + return text + .replace(/&([a-z]+);/gi, (match, entity) => { + const lower = entity.toLowerCase(); + return entities[lower] || match; + }) + .replace(/&#(\d+);/g, (_match, dec) => { + return String.fromCharCode(parseInt(dec)); + }) + .replace(/&#x([0-9a-f]+);/gi, (_match, hex) => { + return String.fromCharCode(parseInt(hex, 16)); + }); +} diff --git a/test/feed-discovery.spec.ts b/test/feed-discovery.spec.ts new file mode 100644 index 00000000000..959cd47bcbe --- /dev/null +++ b/test/feed-discovery.spec.ts @@ -0,0 +1,114 @@ +import { findFeed } from '../source/utils/feed'; + +describe('Feed Discovery - findFeed()', () => { + it('should decode HTML entities in href attributes', async () => { + const html = ` + + + + + + `; + + const feeds = await findFeed(html, 'https://example.com/'); + + expect(feeds).toHaveLength(1); + // Should be decoded: & -> & + expect(feeds[0]).toBe('https://example.com/rss?a=1&b=2&c=3'); + }); + + it('should decode multiple types of entities', async () => { + const html = ` + + + + + + `; + + const feeds = await findFeed(html, 'https://example.com/'); + + expect(feeds).toHaveLength(1); + // Entities are decoded, then URL constructor normalizes/encodes special chars + expect(feeds[0]).toBe( + 'https://example.com/rss?x=%3Cvalue%3E&y=%22test%22' + ); + }); + + it('should decode numeric entities', async () => { + const html = ` + + + + + + `; + + const feeds = await findFeed(html, 'https://example.com/'); + + expect(feeds).toHaveLength(1); + expect(feeds[0]).toBe('https://example.com/rss?a=1&b=2'); + }); + + it('should decode hex entities', async () => { + const html = ` + + + + + + `; + + const feeds = await findFeed(html, 'https://example.com/'); + + expect(feeds).toHaveLength(1); + expect(feeds[0]).toBe('https://example.com/rss?a=1&b=2'); + }); + + it('should handle multiple feeds with entities', async () => { + const html = ` + + + + + + + `; + + const feeds = await findFeed(html, 'https://example.com/'); + + expect(feeds).toHaveLength(2); + expect(feeds[0]).toBe('https://example.com/rss1?a=1&b=2'); + expect(feeds[1]).toBe('https://example.com/atom?x=1&y=2&z=3'); + }); + + it('should leave plain URLs unchanged', async () => { + const html = ` + + + + + + `; + + const feeds = await findFeed(html, 'https://example.com/'); + + expect(feeds).toHaveLength(1); + expect(feeds[0]).toBe('https://example.com/feed.xml'); + }); + + it('should handle absolute path URLs with entities', async () => { + const html = ` + + + + + + `; + + const feeds = await findFeed(html, 'https://example.com/page'); + + expect(feeds).toHaveLength(1); + expect(feeds[0]).toBe('https://example.com/rss?a=1&b=2'); + }); +}); diff --git a/test/html-entities.spec.ts b/test/html-entities.spec.ts new file mode 100644 index 00000000000..be09cc0352a --- /dev/null +++ b/test/html-entities.spec.ts @@ -0,0 +1,47 @@ +import { decodeHtmlEntities } from '../source/utils/html-entities'; + +describe('HTML Entity Decoder', () => { + it('should decode common named entities', () => { + expect(decodeHtmlEntities('&')).toBe('&'); + expect(decodeHtmlEntities('<')).toBe('<'); + expect(decodeHtmlEntities('>')).toBe('>'); + expect(decodeHtmlEntities('"')).toBe('"'); + expect(decodeHtmlEntities(''')).toBe("'"); + }); + + it('should decode numeric entities (decimal)', () => { + expect(decodeHtmlEntities('&')).toBe('&'); + expect(decodeHtmlEntities('<')).toBe('<'); + expect(decodeHtmlEntities('>')).toBe('>'); + }); + + it('should decode numeric entities (hexadecimal)', () => { + expect(decodeHtmlEntities('&')).toBe('&'); + expect(decodeHtmlEntities('<')).toBe('<'); + expect(decodeHtmlEntities('>')).toBe('>'); + expect(decodeHtmlEntities('"')).toBe('"'); + }); + + it('should decode multiple entities in a string', () => { + expect(decodeHtmlEntities('a=1&b=2&c=3')).toBe('a=1&b=2&c=3'); + expect(decodeHtmlEntities('<tag>')).toBe(''); + }); + + it('should handle URLs with entities', () => { + const url = 'https://example.com/feed?a=1&b=2&c=3'; + expect(decodeHtmlEntities(url)).toBe( + 'https://example.com/feed?a=1&b=2&c=3' + ); + }); + + it('should leave plain text unchanged', () => { + expect(decodeHtmlEntities('https://example.com/feed.xml')).toBe( + 'https://example.com/feed.xml' + ); + expect(decodeHtmlEntities('Plain text')).toBe('Plain text'); + }); + + it('should leave unrecognized entities unchanged', () => { + expect(decodeHtmlEntities('&unknownentity;')).toBe('&unknownentity;'); + }); +}); diff --git a/test/template-injection.spec.ts b/test/template-injection.spec.ts index 6078c3a0f9f..8a3d49a07c6 100644 --- a/test/template-injection.spec.ts +++ b/test/template-injection.spec.ts @@ -57,10 +57,10 @@ describe('Notification Template Security - Production Functions', () => { }); describe('Error Notification - buildErrorNotification()', () => { - it('should escape HTML special characters in feed title', async () => { + it('should escape HTML special characters in all positions (full message)', async () => { const feed: Feed = { feed_id: 1, - url: 'https://example.com/feed.xml', + url: 'https://example.com/feed?a=1&b=2', feed_title: 'Tech News & "Updates"', error_count: 0, ttl: 60, @@ -70,7 +70,7 @@ describe('Notification Template Security - Production Functions', () => { next_fetch_time: new Date() }; - const error = new Error('Connection timeout'); + const error = { message: 'Error: & "Bad"' }; const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); @@ -78,25 +78,20 @@ describe('Notification Template Security - Production Functions', () => { expect(sentMessages).toHaveLength(1); const text = sentMessages[0].text; - // Title should be HTML-escaped (EJS uses " for quotes) - expect(text).toContain( - 'Tech News <Daily> & "Updates"' - ); - // Link markup should remain literal - expect(text).toContain(''); - // i18n text should be present - expect(text).toContain( - 'Unable to fetch for many times, you might want to unsub it' + // Full-text assertion - changing any <%= to <%- would fail this + expect(text).toBe( + 'Tech News <Daily> & "Updates": ' + + 'https://example.com/feed?a=1&b=2 ' + + 'Unable to fetch for many times, you might want to unsub it ' + + 'Error: <Server> & "Bad"' ); - // Error message should be present - expect(text).toContain('Connection timeout'); }); - it('should escape HTML special characters in URL', async () => { + it('should escape single quotes in title and error', async () => { const feed: Feed = { feed_id: 1, - url: 'https://example.com/feed?a=1&b=2', - feed_title: 'Test Feed', + url: 'https://example.com/feed.xml', + feed_title: "It's News & 'Updates'", error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -105,21 +100,27 @@ describe('Notification Template Security - Production Functions', () => { next_fetch_time: new Date() }; - const error = new Error('Timeout'); + const error = { message: "Can't connect & 'timeout'" }; const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); const text = sentMessages[0].text; - // URL with & should be escaped - expect(text).toContain('https://example.com/feed?a=1&b=2'); + + // Full-text assertion with single quotes + expect(text).toBe( + 'It's News & 'Updates': ' + + 'https://example.com/feed.xml ' + + 'Unable to fetch for many times, you might want to unsub it ' + + 'Can't connect & 'timeout'' + ); }); - it('should escape HTML special characters in error message', async () => { + it('should escape all special characters in URL query params', async () => { const feed: Feed = { feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: 'Test Feed', + url: 'https://example.com/feed?x=&y="test"&z=\'quote\'', + feed_title: 'Test', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -128,17 +129,19 @@ describe('Notification Template Security - Production Functions', () => { next_fetch_time: new Date() }; - const error = new Error( - 'Error: returned "Invalid" & failed' - ); + const error = { message: 'Timeout' }; const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); const text = sentMessages[0].text; - // Error message should be HTML-escaped (EJS uses " for quotes) + + // URL with complex query string - both in href and link text expect(text).toContain( - 'Error: <Server> returned "Invalid" & failed' + '' + ); + expect(text).toContain( + 'https://example.com/feed?x=<val>&y="test"&z='quote'' ); }); @@ -146,7 +149,7 @@ describe('Notification Template Security - Production Functions', () => { const feed: Feed = { feed_id: 1, url: 'https://example.com/feed.xml', - feed_title: 'Programming Tips: use <%= variable %> syntax', + feed_title: 'Tips: <%= var %> and <% code %>', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -155,19 +158,20 @@ describe('Notification Template Security - Production Functions', () => { next_fetch_time: new Date() }; - const error = new Error('Timeout'); + const error = { message: 'Timeout' }; const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); const text = sentMessages[0].text; - // EJS delimiters should be escaped and appear literally + + // EJS delimiters should be escaped expect(text).toContain( - 'Programming Tips: use <%= variable %> syntax' + 'Tips: <%= var %> and <% code %>' ); }); - it('should produce complete correct message for plain input', async () => { + it('should produce correct message for plain input', async () => { const feed: Feed = { feed_id: 1, url: 'https://example.com/feed.xml', @@ -180,14 +184,14 @@ describe('Notification Template Security - Production Functions', () => { next_fetch_time: new Date() }; - const error = new Error('Connection timeout'); + const error = { message: 'Connection timeout' }; const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); const text = sentMessages[0].text; - // Complete message structure (plain inputs, no escaping needed) + // Plain inputs - byte-identical to master expect(text).toBe( 'My RSS Feed: https://example.com/feed.xml ' + 'Unable to fetch for many times, you might want to unsub it Connection timeout' @@ -196,11 +200,11 @@ describe('Notification Template Security - Production Functions', () => { }); describe('Feed Change Notification - buildFeedChangeNotification()', () => { - it('should escape HTML special characters in feed title', async () => { + it('should escape HTML in all positions including repeated URL (full message)', async () => { const feed: Feed = { feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: 'News & Updates', + url: 'https://example.com/old?a=1&b=2', + feed_title: 'News & "Updates"', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -210,8 +214,8 @@ describe('Notification Template Security - Production Functions', () => { }; const newFeedUrls = [ - 'https://example.com/newfeed1.xml', - 'https://example.com/newfeed2.xml' + 'https://example.com/new1?x=1&y=2', + 'https://example.com/new2?p=&q="test"' ]; const [template, locals] = buildFeedChangeNotification( @@ -224,15 +228,23 @@ describe('Notification Template Security - Production Functions', () => { const text = sentMessages[0].text; - // Title should be HTML-escaped - expect(text).toContain('News <Daily> & Updates'); + // Full-text assertion - any <%- would break this + expect(text).toBe( + 'News <Daily> & "Updates": ' + + ' ' + + 'Unable to fetch for many times, you might want to unsub it\n' + + 'Found feeds:\n' + + 'https://example.com/new1?x=1&y=2\n' + + 'https://example.com/new2?p=<val>&q="test"\n' + + 'Feed Change to https://example.com/new1?x=1&y=2' + ); }); - it('should escape HTML special characters in all new feed URLs', async () => { + it('should escape special chars in all new feed URLs', async () => { const feed: Feed = { feed_id: 1, url: 'https://example.com/feed.xml', - feed_title: 'Test Feed', + feed_title: 'Test', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -242,9 +254,9 @@ describe('Notification Template Security - Production Functions', () => { }; const newFeedUrls = [ - 'https://example.com/feed1?a=1&b=2', - 'https://example.com/feed2?x=&y="test"', - 'https://example.com/feed3?z=3' + 'https://ex.com/f1?a=1&b=2&c=3', + 'https://ex.com/f2?x=<>&y="\'', + 'https://ex.com/f3?z=3' ]; const [template, locals] = buildFeedChangeNotification( @@ -257,51 +269,17 @@ describe('Notification Template Security - Production Functions', () => { const text = sentMessages[0].text; - // All URLs should be HTML-escaped (EJS uses " for quotes) - expect(text).toContain('https://example.com/feed1?a=1&b=2'); + // Each URL should appear escaped in the list + expect(text).toContain('https://ex.com/f1?a=1&b=2&c=3\n'); expect(text).toContain( - 'https://example.com/feed2?x=<value>&y="test"' + 'https://ex.com/f2?x=<>&y="'\n' ); - expect(text).toContain('https://example.com/feed3?z=3'); - }); + expect(text).toContain('https://ex.com/f3?z=3\n'); - it('should render all new feed URLs in the list', async () => { - const feed: Feed = { - feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: 'Test Feed', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - - const newFeedUrls = [ - 'https://example.com/feed1.xml', - 'https://example.com/feed2.xml', - 'https://example.com/feed3.xml' - ]; - - const [template, locals] = buildFeedChangeNotification( - feed, - newFeedUrls, - encodeUrl + // First URL also appears at the end (repeated) - must be escaped there too + expect(text).toContain( + 'Feed Change to https://ex.com/f1?a=1&b=2&c=3' ); - - await send(bot, template, feed, locals); - - const text = sentMessages[0].text; - - // All three URLs should appear in order - const feed1Idx = text.indexOf('https://example.com/feed1.xml'); - const feed2Idx = text.indexOf('https://example.com/feed2.xml'); - const feed3Idx = text.indexOf('https://example.com/feed3.xml'); - - expect(feed1Idx).toBeGreaterThan(-1); - expect(feed2Idx).toBeGreaterThan(feed1Idx); - expect(feed3Idx).toBeGreaterThan(feed2Idx); }); it('should render literal EJS delimiters in new feed URLs', async () => { @@ -317,7 +295,7 @@ describe('Notification Template Security - Production Functions', () => { next_fetch_time: new Date() }; - const newFeedUrls = ['https://example.com/<%= id %>/feed.xml']; + const newFeedUrls = ['https://ex.com/<%= id %>/feed.xml']; const [template, locals] = buildFeedChangeNotification( feed, @@ -329,13 +307,14 @@ describe('Notification Template Security - Production Functions', () => { const text = sentMessages[0].text; - // EJS delimiters should be escaped and appear literally + // EJS delimiters should be escaped in both list and trailing URL + expect(text).toContain('https://ex.com/<%= id %>/feed.xml\n'); expect(text).toContain( - 'https://example.com/<%= id %>/feed.xml' + 'Feed Change to https://ex.com/<%= id %>/feed.xml' ); }); - it('should produce complete correct message structure', async () => { + it('should produce correct structure for plain inputs', async () => { const feed: Feed = { feed_id: 1, url: 'https://example.com/feed.xml', @@ -363,7 +342,7 @@ describe('Notification Template Security - Production Functions', () => { const text = sentMessages[0].text; - // Verify complete structure with newlines + // Plain inputs - byte-identical to expected expect(text).toBe( 'Test Feed: ' + 'Unable to fetch for many times, you might want to unsub it\n' + @@ -376,10 +355,10 @@ describe('Notification Template Security - Production Functions', () => { }); describe('No Double Escaping', () => { - it('should not double-escape already-encoded URLs', async () => { + it('should escape ampersands exactly once in URLs with query params', async () => { const feed: Feed = { feed_id: 1, - url: 'https://example.com/feed.xml', + url: 'https://example.com/feed?a=1&b=2&c=3', feed_title: 'Test', error_count: 0, ttl: 60, @@ -389,21 +368,51 @@ describe('Notification Template Security - Production Functions', () => { next_fetch_time: new Date() }; - const error = new Error('Timeout'); + const error = { message: 'Timeout' }; const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); const text = sentMessages[0].text; - // URL should appear with & escaped exactly once - // If double-escaped, it would be &amp; + // Should contain & not &amp; (would indicate double-escaping) + expect(text).toContain('?a=1&b=2&c=3'); + expect(text).not.toContain('&amp;'); + }); + + it('should not double-escape already-escaped entities in feed change URLs', async () => { + const feed: Feed = { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: 'Test', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; + + const newFeedUrls = ['https://example.com/rss?a=1&b=2']; + + const [template, locals] = buildFeedChangeNotification( + feed, + newFeedUrls, + encodeUrl + ); + + await send(bot, template, feed, locals); + + const text = sentMessages[0].text; + + // Should be escaped exactly once + expect(text).toContain('?a=1&b=2'); expect(text).not.toContain('&amp;'); }); }); describe('Multi-Language Support', () => { - it('should work with Chinese language', async () => { + it('should work with Chinese language (full message with special chars)', async () => { // Mock Chinese user (usersProxy.getUserById as jest.Mock).mockResolvedValue( Optional({ @@ -415,8 +424,8 @@ describe('Notification Template Security - Production Functions', () => { const feed: Feed = { feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: 'Test Feed', + url: 'https://example.com/feed?a=1&b=2', + feed_title: '科技新闻 <每日> & "更新"', error_count: 0, ttl: 60, recent_hash_list: '[]', @@ -425,15 +434,51 @@ describe('Notification Template Security - Production Functions', () => { next_fetch_time: new Date() }; - const error = new Error('Connection timeout'); + const error = { message: 'Connection timeout' }; const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); const text = sentMessages[0].text; - // Should contain Chinese translation - expect(text).toMatch(/多次/); // Part of Chinese error message + // Full message with Chinese title and i18n text, special chars escaped + expect(text).toBe( + '科技新闻 <每日> & "更新": ' + + 'https://example.com/feed?a=1&b=2 ' + + '多次抓取失败,建议取消订阅 Connection timeout' + ); + }); + }); + + describe('Feed Discovery Integration', () => { + it('should handle entity-encoded URLs from HTML href attributes', async () => { + // This tests the complete flow: HTML with encoded entities -> + // findFeed decodes -> notification builders escape for Telegram + const feed: Feed = { + feed_id: 1, + // Simulate URL that came from HTML: href="...?a=1&b=2" + // After findFeed decoding, it's raw: ...?a=1&b=2 + // Then notification escapes it once for output + url: 'https://example.com/rss?a=1&b=2', + feed_title: 'Discovered Feed', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date() + }; + + const error = { message: 'Failed' }; + const [template, locals] = buildErrorNotification(feed, error); + + await send(bot, template, feed, locals); + + const text = sentMessages[0].text; + + // Should be escaped exactly once (not double-escaped) + expect(text).toContain('https://example.com/rss?a=1&b=2'); + expect(text).not.toContain('&amp;'); }); }); }); From 753287f573ec07637f226844c7f034f605db9497 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 22 Oct 2026 07:47:13 +0000 Subject: [PATCH 5/5] Improve readability of notification template code Refactoring for clarity - no behavior changes: notification-messages.ts: - Extract templates as named constants for readability - Improve JSDoc with parameter descriptions - Use clearer variable names (feedUrl vs feed_url) html-entities.ts: - Extract NAMED_ENTITIES as top-level constant - Better file-level documentation explaining purpose - Use descriptive parameter names (entityName, decimal, hexadecimal) - Use nullish coalescing for cleaner fallback feed.ts (findFeed): - Extract href parsing into extractHref() helper function - Convert chained map/filter to clear for loop with early continues - Better variable names (linkTags, baseUrl) - Add JSDoc for both functions template-injection.spec.ts: - Add createTestFeed() helper to reduce boilerplate - More concise test descriptions - Simplified mock setup - Better test organization with clearer describe blocks All tests pass (76/76), build clean, lint clean, behavior identical Co-authored-by: fengkx --- source/utils/feed.ts | 68 +++-- source/utils/html-entities.ts | 53 ++-- source/utils/notification-messages.ts | 81 ++++-- test/template-injection.spec.ts | 348 +++++++------------------- 4 files changed, 228 insertions(+), 322 deletions(-) diff --git a/source/utils/feed.ts b/source/utils/feed.ts index e83e1dfd74d..f21b3965639 100644 --- a/source/utils/feed.ts +++ b/source/utils/feed.ts @@ -13,32 +13,60 @@ export async function isFeedValid(feedStr: string): Promise> { } } +/** + * Extract and decode href attribute from a link tag. + * HTML entities in the href value are decoded to prevent double-escaping. + */ +function extractHref(linkTag: string): string | null { + const hrefMatch = linkTag.match(/href="(.+?)"/); + if (!hrefMatch) { + return null; + } + // Decode HTML entities (e.g., & -> &) before URL parsing + return decodeHtmlEntities(hrefMatch[1]); +} + +/** + * Discover RSS/Atom feed URLs from HTML link tags. + * + * @param html - HTML content to search + * @param reqUrl - Base URL for resolving relative paths + * @returns Array of discovered feed URLs + */ export async function findFeed( html: string, reqUrl: string ): Promise { - const reqURL = new URL(reqUrl); - const linksTag = html.match(/]+rel="alternate"[^>]+\/?>/g); - if (!linksTag) { + const baseUrl = new URL(reqUrl); + const linkTags = html.match(/]+rel="alternate"[^>]+\/?>/g); + + if (!linkTags) { return []; } - return linksTag - .filter((t) => t.match(/rss|atom/) && t.includes('href')) - .map((linkTag) => { - const hrefMatch = linkTag.match(/href="(.+?)"/); - if (!hrefMatch) { - return ''; - } - // Decode HTML entities from href attribute value - const url = decodeHtmlEntities(hrefMatch[1]); - try { - return new URL(url).toString(); - } catch { - reqURL.pathname = url; - return reqURL.toString(); - } - }) - .filter((url) => url !== ''); + + const feedUrls: string[] = []; + + for (const linkTag of linkTags) { + // Only process RSS/Atom feeds + if (!linkTag.match(/rss|atom/) || !linkTag.includes('href')) { + continue; + } + + const href = extractHref(linkTag); + if (!href) { + continue; + } + + // Try to parse as absolute URL, fall back to relative path + try { + feedUrls.push(new URL(href).toString()); + } catch { + baseUrl.pathname = href; + feedUrls.push(baseUrl.toString()); + } + } + + return feedUrls; } export async function getNewItems( diff --git a/source/utils/html-entities.ts b/source/utils/html-entities.ts index e11ee895ba9..a06450e42d9 100644 --- a/source/utils/html-entities.ts +++ b/source/utils/html-entities.ts @@ -1,27 +1,44 @@ +/** + * HTML entity decoder for href attribute values. + * + * When discovering RSS feeds from HTML, href attributes may contain entity-encoded + * characters (e.g., href="...?a=1&b=2"). This decoder converts them to their + * literal characters so URLs can be properly parsed and stored. + */ + +/** + * Common named HTML entities and their character equivalents. + */ +const NAMED_ENTITIES: Record = { + amp: '&', + lt: '<', + gt: '>', + quot: '"', + apos: "'", + nbsp: '\u00A0' +}; + /** * Decode HTML entities in a string. - * Handles common entities found in HTML href attributes. + * + * Supports: + * - Named entities: & < > " '   + * - Numeric entities (decimal): & < + * - Numeric entities (hex): & < + * + * @param text - String potentially containing HTML entities + * @returns String with entities decoded to their literal characters */ export function decodeHtmlEntities(text: string): string { - // Map of named entities to characters - const entities: Record = { - amp: '&', - lt: '<', - gt: '>', - quot: '"', - apos: "'", - nbsp: '\u00A0' - }; - return text - .replace(/&([a-z]+);/gi, (match, entity) => { - const lower = entity.toLowerCase(); - return entities[lower] || match; + .replace(/&([a-z]+);/gi, (match, entityName) => { + const normalized = entityName.toLowerCase(); + return NAMED_ENTITIES[normalized] ?? match; }) - .replace(/&#(\d+);/g, (_match, dec) => { - return String.fromCharCode(parseInt(dec)); + .replace(/&#(\d+);/g, (_match, decimal) => { + return String.fromCharCode(parseInt(decimal)); }) - .replace(/&#x([0-9a-f]+);/gi, (_match, hex) => { - return String.fromCharCode(parseInt(hex, 16)); + .replace(/&#x([0-9a-f]+);/gi, (_match, hexadecimal) => { + return String.fromCharCode(parseInt(hexadecimal, 16)); }); } diff --git a/source/utils/notification-messages.ts b/source/utils/notification-messages.ts index f51c029db8f..d89e5011227 100644 --- a/source/utils/notification-messages.ts +++ b/source/utils/notification-messages.ts @@ -1,64 +1,93 @@ /** * Notification message templates for RSS feed errors and changes. - * All user-controlled data (titles, URLs, error messages) is passed as EJS locals + * + * All feed-controlled data (titles, URLs, error messages) is passed as EJS locals * and rendered with escaped output (<%= %>) to prevent injection of Telegram HTML. + * This applies only to error and feed-change notifications; normal feed items are + * handled elsewhere. */ import { Feed } from '../types/feed'; import { Messenger } from '../types/message'; +/** + * Template locals for error notifications (ERROR_MANY_TIME). + */ export interface ErrorNotificationData { title: string; url: string; errorMessage: string; } +/** + * Template locals for feed URL change notifications (isChangeFeedUrl). + */ export interface FeedChangeNotificationData { title: string; url: string; newFeedUrls: string[]; } +/** + * Template for error notification messages. + * Shows feed title, URL link, i18n error text, and error message. + */ +const ERROR_NOTIFICATION_TEMPLATE = + '<%= title %>: ' + + '<%= url %> ' + + '<%=i18n["ERROR_MANY_TIME"]%> ' + + '<%= errorMessage %>'; + +/** + * Template for feed URL change notifications. + * Shows old feed info, lists discovered feeds, and indicates the new URL. + */ +const FEED_CHANGE_NOTIFICATION_TEMPLATE = + '<%= title %>: <%=i18n["ERROR_MANY_TIME"]%>\n' + + '<%=i18n["FOUND_FEEDS"]%>:\n' + + '<% newFeedUrls.forEach(function(feedUrl) { %><%= feedUrl %>\n<% }); %>' + + '<%=i18n["FEED_CHANGE_TO"]%> <%= newFeedUrls[0] %>'; + /** * Build error notification message template and locals. - * Used when a feed fails to fetch multiple times (ERROR_MANY_TIME). + * + * @param feed - The feed that failed to fetch + * @param error - The error that occurred + * @returns Tuple of [template string, locals object] */ export function buildErrorNotification( feed: Feed, error: Messenger ): [string, ErrorNotificationData] { - const template = - '<%= title %>: <%= url %> <%=i18n["ERROR_MANY_TIME"]%> <%= errorMessage %>'; - - const locals: ErrorNotificationData = { - title: feed.feed_title, - url: feed.url, - errorMessage: error.message - }; - - return [template, locals]; + return [ + ERROR_NOTIFICATION_TEMPLATE, + { + title: feed.feed_title, + url: feed.url, + errorMessage: error.message + } + ]; } /** * Build feed URL change notification message template and locals. - * Used when RSS feed URL changes are detected (isChangeFeedUrl). + * + * @param feed - The feed whose URL changed + * @param newFeedUrls - Array of discovered feed URLs + * @param urlEncoder - Function to encode URLs (typically encodeUrl) + * @returns Tuple of [template string, locals object] */ export function buildFeedChangeNotification( feed: Feed, newFeedUrls: string[], urlEncoder: (url: string) => string ): [string, FeedChangeNotificationData] { - const template = - '<%= title %>: <%=i18n["ERROR_MANY_TIME"]%>\n' + - '<%=i18n["FOUND_FEEDS"]%>:\n' + - '<% newFeedUrls.forEach(function(feed_url) { %><%= feed_url %>\n<% }); %>' + - '<%=i18n["FEED_CHANGE_TO"]%> <%= newFeedUrls[0] %>'; - - const locals: FeedChangeNotificationData = { - title: feed.feed_title, - url: urlEncoder(feed.url), - newFeedUrls: newFeedUrls - }; - - return [template, locals]; + return [ + FEED_CHANGE_NOTIFICATION_TEMPLATE, + { + title: feed.feed_title, + url: urlEncoder(feed.url), + newFeedUrls + } + ]; } diff --git a/test/template-injection.spec.ts b/test/template-injection.spec.ts index 8a3d49a07c6..756d7c4625c 100644 --- a/test/template-injection.spec.ts +++ b/test/template-injection.spec.ts @@ -1,11 +1,10 @@ /** * Notification Template Security Tests * - * These tests verify that feed titles, URLs, and error messages in notification - * templates are properly escaped and cannot inject Telegram HTML or malformed markup. + * Verify that feed-controlled data (titles, URLs, error messages) in error and + * feed-change notifications is properly escaped to prevent Telegram HTML injection. * - * Tests use the actual production message construction functions to ensure the fix - * is effective at the real call sites. + * All tests use production message builders to ensure real code paths are covered. */ import { Telegraf } from 'telegraf'; @@ -20,33 +19,39 @@ import { } from '../source/utils/notification-messages'; import { encodeUrl } from '../source/utils/urlencode'; -// Mock database and Telegram jest.mock('../source/proxies/subscribes'); jest.mock('../source/proxies/users'); -describe('Notification Template Security - Production Functions', () => { +// Test feed factory +function createTestFeed(overrides: Partial = {}): Feed { + return { + feed_id: 1, + url: 'https://example.com/feed.xml', + feed_title: 'Test Feed', + error_count: 0, + ttl: 60, + recent_hash_list: '[]', + etag_header: '', + last_modified_header: null, + next_fetch_time: new Date(), + ...overrides + }; +} + +describe('Notification Template Security', () => { let bot: Telegraf; - let sentMessages: Array<{ - userId: number; - text: string; - options: any; - }>; + let sentMessages: Array<{ userId: number; text: string; options: any }>; const mockUserId = 12345; beforeEach(() => { - // Reset mocks sentMessages = []; - // Create bot with mocked telegram API bot = new Telegraf('mock-token'); - bot.telegram.sendMessage = jest.fn( - async (userId: number, text: string, options?: any) => { - sentMessages.push({ userId, text, options }); - return {} as any; - } - ); + bot.telegram.sendMessage = jest.fn(async (userId, text, options) => { + sentMessages.push({ userId, text, options }); + return {} as any; + }); - // Mock database responses (subscribesProxy.getSubscribersByFeedId as jest.Mock).mockResolvedValue( [{ user_id: mockUserId, feed_id: 1 }] ); @@ -56,30 +61,18 @@ describe('Notification Template Security - Production Functions', () => { ); }); - describe('Error Notification - buildErrorNotification()', () => { - it('should escape HTML special characters in all positions (full message)', async () => { - const feed: Feed = { - feed_id: 1, + describe('Error Notifications', () => { + it('escapes HTML chars in title, URL, and error message', async () => { + const feed = createTestFeed({ url: 'https://example.com/feed?a=1&b=2', - feed_title: 'Tech News & "Updates"', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - + feed_title: 'Tech News & "Updates"' + }); const error = { message: 'Error: & "Bad"' }; - const [template, locals] = buildErrorNotification(feed, error); + const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); - expect(sentMessages).toHaveLength(1); - const text = sentMessages[0].text; - - // Full-text assertion - changing any <%= to <%- would fail this - expect(text).toBe( + expect(sentMessages[0].text).toBe( 'Tech News <Daily> & "Updates": ' + 'https://example.com/feed?a=1&b=2 ' + 'Unable to fetch for many times, you might want to unsub it ' + @@ -87,28 +80,16 @@ describe('Notification Template Security - Production Functions', () => { ); }); - it('should escape single quotes in title and error', async () => { - const feed: Feed = { - feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: "It's News & 'Updates'", - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - + it('escapes single quotes in title and error', async () => { + const feed = createTestFeed({ + feed_title: "It's News & 'Updates'" + }); const error = { message: "Can't connect & 'timeout'" }; - const [template, locals] = buildErrorNotification(feed, error); + const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); - const text = sentMessages[0].text; - - // Full-text assertion with single quotes - expect(text).toBe( + expect(sentMessages[0].text).toBe( 'It's News & 'Updates': ' + 'https://example.com/feed.xml ' + 'Unable to fetch for many times, you might want to unsub it ' + @@ -116,27 +97,16 @@ describe('Notification Template Security - Production Functions', () => { ); }); - it('should escape all special characters in URL query params', async () => { - const feed: Feed = { - feed_id: 1, - url: 'https://example.com/feed?x=&y="test"&z=\'quote\'', - feed_title: 'Test', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - + it('escapes special chars in URL query params (href and link text)', async () => { + const feed = createTestFeed({ + url: 'https://example.com/feed?x=&y="test"&z=\'quote\'' + }); const error = { message: 'Timeout' }; - const [template, locals] = buildErrorNotification(feed, error); + const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); const text = sentMessages[0].text; - - // URL with complex query string - both in href and link text expect(text).toContain( '' ); @@ -145,74 +115,40 @@ describe('Notification Template Security - Production Functions', () => { ); }); - it('should render literal EJS delimiters in feed title', async () => { - const feed: Feed = { - feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: 'Tips: <%= var %> and <% code %>', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - + it('renders literal EJS delimiters as text', async () => { + const feed = createTestFeed({ + feed_title: 'Tips: <%= var %> and <% code %>' + }); const error = { message: 'Timeout' }; - const [template, locals] = buildErrorNotification(feed, error); + const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); - const text = sentMessages[0].text; - - // EJS delimiters should be escaped - expect(text).toContain( + expect(sentMessages[0].text).toContain( 'Tips: <%= var %> and <% code %>' ); }); - it('should produce correct message for plain input', async () => { - const feed: Feed = { - feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: 'My RSS Feed', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - + it('renders plain inputs identically to previous behavior', async () => { + const feed = createTestFeed({ feed_title: 'My RSS Feed' }); const error = { message: 'Connection timeout' }; - const [template, locals] = buildErrorNotification(feed, error); + const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); - const text = sentMessages[0].text; - - // Plain inputs - byte-identical to master - expect(text).toBe( + expect(sentMessages[0].text).toBe( 'My RSS Feed: https://example.com/feed.xml ' + 'Unable to fetch for many times, you might want to unsub it Connection timeout' ); }); }); - describe('Feed Change Notification - buildFeedChangeNotification()', () => { - it('should escape HTML in all positions including repeated URL (full message)', async () => { - const feed: Feed = { - feed_id: 1, + describe('Feed Change Notifications', () => { + it('escapes HTML in title, URLs, and repeated trailing URL', async () => { + const feed = createTestFeed({ url: 'https://example.com/old?a=1&b=2', - feed_title: 'News & "Updates"', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - + feed_title: 'News & "Updates"' + }); const newFeedUrls = [ 'https://example.com/new1?x=1&y=2', 'https://example.com/new2?p=&q="test"' @@ -223,13 +159,9 @@ describe('Notification Template Security - Production Functions', () => { newFeedUrls, encodeUrl ); - await send(bot, template, feed, locals); - const text = sentMessages[0].text; - - // Full-text assertion - any <%- would break this - expect(text).toBe( + expect(sentMessages[0].text).toBe( 'News <Daily> & "Updates": ' + ' ' + 'Unable to fetch for many times, you might want to unsub it\n' + @@ -240,19 +172,8 @@ describe('Notification Template Security - Production Functions', () => { ); }); - it('should escape special chars in all new feed URLs', async () => { - const feed: Feed = { - feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: 'Test', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - + it('escapes special chars in all list items and trailing URL', async () => { + const feed = createTestFeed(); const newFeedUrls = [ 'https://ex.com/f1?a=1&b=2&c=3', 'https://ex.com/f2?x=<>&y="\'', @@ -264,37 +185,20 @@ describe('Notification Template Security - Production Functions', () => { newFeedUrls, encodeUrl ); - await send(bot, template, feed, locals); const text = sentMessages[0].text; - - // Each URL should appear escaped in the list expect(text).toContain('https://ex.com/f1?a=1&b=2&c=3\n'); expect(text).toContain( 'https://ex.com/f2?x=<>&y="'\n' ); - expect(text).toContain('https://ex.com/f3?z=3\n'); - - // First URL also appears at the end (repeated) - must be escaped there too expect(text).toContain( 'Feed Change to https://ex.com/f1?a=1&b=2&c=3' ); }); - it('should render literal EJS delimiters in new feed URLs', async () => { - const feed: Feed = { - feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: 'Test', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - + it('renders literal EJS delimiters in feed URLs', async () => { + const feed = createTestFeed(); const newFeedUrls = ['https://ex.com/<%= id %>/feed.xml']; const [template, locals] = buildFeedChangeNotification( @@ -302,31 +206,17 @@ describe('Notification Template Security - Production Functions', () => { newFeedUrls, encodeUrl ); - await send(bot, template, feed, locals); const text = sentMessages[0].text; - - // EJS delimiters should be escaped in both list and trailing URL expect(text).toContain('https://ex.com/<%= id %>/feed.xml\n'); expect(text).toContain( 'Feed Change to https://ex.com/<%= id %>/feed.xml' ); }); - it('should produce correct structure for plain inputs', async () => { - const feed: Feed = { - feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: 'Test Feed', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - + it('renders plain inputs with correct structure', async () => { + const feed = createTestFeed(); const newFeedUrls = [ 'https://example.com/newfeed1.xml', 'https://example.com/newfeed2.xml' @@ -337,13 +227,9 @@ describe('Notification Template Security - Production Functions', () => { newFeedUrls, encodeUrl ); - await send(bot, template, feed, locals); - const text = sentMessages[0].text; - - // Plain inputs - byte-identical to expected - expect(text).toBe( + expect(sentMessages[0].text).toBe( 'Test Feed: ' + 'Unable to fetch for many times, you might want to unsub it\n' + 'Found feeds:\n' + @@ -355,44 +241,21 @@ describe('Notification Template Security - Production Functions', () => { }); describe('No Double Escaping', () => { - it('should escape ampersands exactly once in URLs with query params', async () => { - const feed: Feed = { - feed_id: 1, - url: 'https://example.com/feed?a=1&b=2&c=3', - feed_title: 'Test', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - + it('escapes ampersands exactly once in error URLs', async () => { + const feed = createTestFeed({ + url: 'https://example.com/feed?a=1&b=2&c=3' + }); const error = { message: 'Timeout' }; - const [template, locals] = buildErrorNotification(feed, error); + const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); - const text = sentMessages[0].text; - - // Should contain & not &amp; (would indicate double-escaping) - expect(text).toContain('?a=1&b=2&c=3'); - expect(text).not.toContain('&amp;'); + expect(sentMessages[0].text).toContain('?a=1&b=2&c=3'); + expect(sentMessages[0].text).not.toContain('&amp;'); }); - it('should not double-escape already-escaped entities in feed change URLs', async () => { - const feed: Feed = { - feed_id: 1, - url: 'https://example.com/feed.xml', - feed_title: 'Test', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - + it('escapes ampersands exactly once in feed change URLs', async () => { + const feed = createTestFeed(); const newFeedUrls = ['https://example.com/rss?a=1&b=2']; const [template, locals] = buildFeedChangeNotification( @@ -400,20 +263,15 @@ describe('Notification Template Security - Production Functions', () => { newFeedUrls, encodeUrl ); - await send(bot, template, feed, locals); - const text = sentMessages[0].text; - - // Should be escaped exactly once - expect(text).toContain('?a=1&b=2'); - expect(text).not.toContain('&amp;'); + expect(sentMessages[0].text).toContain('?a=1&b=2'); + expect(sentMessages[0].text).not.toContain('&amp;'); }); }); - describe('Multi-Language Support', () => { - it('should work with Chinese language (full message with special chars)', async () => { - // Mock Chinese user + describe('Internationalization', () => { + it('works with Chinese language and special chars', async () => { (usersProxy.getUserById as jest.Mock).mockResolvedValue( Optional({ user_id: mockUserId, @@ -422,27 +280,16 @@ describe('Notification Template Security - Production Functions', () => { }) ); - const feed: Feed = { - feed_id: 1, + const feed = createTestFeed({ url: 'https://example.com/feed?a=1&b=2', - feed_title: '科技新闻 <每日> & "更新"', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - + feed_title: '科技新闻 <每日> & "更新"' + }); const error = { message: 'Connection timeout' }; - const [template, locals] = buildErrorNotification(feed, error); + const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); - const text = sentMessages[0].text; - - // Full message with Chinese title and i18n text, special chars escaped - expect(text).toBe( + expect(sentMessages[0].text).toBe( '科技新闻 <每日> & "更新": ' + 'https://example.com/feed?a=1&b=2 ' + '多次抓取失败,建议取消订阅 Connection timeout' @@ -451,32 +298,17 @@ describe('Notification Template Security - Production Functions', () => { }); describe('Feed Discovery Integration', () => { - it('should handle entity-encoded URLs from HTML href attributes', async () => { - // This tests the complete flow: HTML with encoded entities -> - // findFeed decodes -> notification builders escape for Telegram - const feed: Feed = { - feed_id: 1, - // Simulate URL that came from HTML: href="...?a=1&b=2" - // After findFeed decoding, it's raw: ...?a=1&b=2 - // Then notification escapes it once for output - url: 'https://example.com/rss?a=1&b=2', - feed_title: 'Discovered Feed', - error_count: 0, - ttl: 60, - recent_hash_list: '[]', - etag_header: '', - last_modified_header: null, - next_fetch_time: new Date() - }; - + it('handles entity-decoded URLs from HTML discovery', async () => { + // URL decoded from HTML href="...?a=1&b=2" by findFeed() + const feed = createTestFeed({ + url: 'https://example.com/rss?a=1&b=2' + }); const error = { message: 'Failed' }; - const [template, locals] = buildErrorNotification(feed, error); + const [template, locals] = buildErrorNotification(feed, error); await send(bot, template, feed, locals); const text = sentMessages[0].text; - - // Should be escaped exactly once (not double-escaped) expect(text).toContain('https://example.com/rss?a=1&b=2'); expect(text).not.toContain('&amp;'); });