Repository navigation
Render notification templates with fixed templates and escaped locals - #2250
Merged
Merged
Conversation
- 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 <liangkx8237@gmail.com>
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 <liangkx8237@gmail.com>
- 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 <liangkx8237@gmail.com>
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 <liangkx8237@gmail.com>
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 <liangkx8237@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR updates error and feed-change notification message rendering to use fixed EJS template sources with all feed-controlled data passed as escaped locals. This prevents feed data from injecting arbitrary Telegram HTML markup or malformed tags.
Scope: This PR addresses error notifications (ERROR_MANY_TIME) and feed URL change notifications (isChangeFeedUrl). Other Telegram HTML outputs (normal feed items, subscription confirmations, RSS list) are out of scope for this PR and will be addressed separately if needed.
Changes
Notification Template Construction
Created
source/utils/notification-messages.tswith message builder functions:buildErrorNotification()- constructs error notification templates and localsbuildFeedChangeNotification()- constructs feed URL change templates and localsUpdated
source/index.tsto use the builder functions:buildErrorNotification()buildFeedChangeNotification()HTML Entity Decoding in Feed Discovery
source/utils/html-entities.tsto decode HTML entities from href attributesfindFeed()insource/utils/feed.tsto decode entities before URL parsinghref="...?a=1&b=2") flow through to notificationsTemplate Rendering
<%= %>) to prevent injection of Telegram HTML<a href="...">tags) remains literal in the template sourceExtended send() Function
templateLocalsparameter to pass untrusted data safelyBehavior Changes
Escaping of Special Characters
Feed titles, URLs, and error messages now have HTML special characters escaped in error and feed-change notifications:
<becomes<>becomes>&becomes&"becomes"'becomes'Before: Feed title
Tech News <Daily> & Updateswould render with raw HTMLAfter: Title renders as
Tech News <Daily> & Updates(displays literally in Telegram)Literal EJS Delimiter Preservation
Feed titles or URLs containing literal
<%or%>text are now preserved:Before: Title
Use <%= variable %>might cause rendering issuesAfter: Title renders as
Use <%= variable %>(displays literally)Plain Input Compatibility
Plain feed titles without special characters render byte-identically to the previous implementation.
Testing
Test Coverage (13 test suites, 76 tests)
Notification Security Tests (11 tests in
template-injection.spec.ts):&, quotes,<>in all positionsbuildErrorNotification()andbuildFeedChangeNotification()functions<%= %>to<%- %>in any output position will fail at least one testHTML Entity Decoding Tests (7 tests in
html-entities.spec.ts):&,<,>,",')&)&)Feed Discovery Tests (6 tests in
feed-discovery.spec.ts):Test Results
Build & Lint
Technical Details
Why Escaped Output?
Using
<%= %>(escaped) instead of<%- %>(raw) ensures feed data cannot inject:<a href="malicious">)<b>,<i>,<code>)HTML Entity Decoding Flow
href="https://example.com/rss?a=1&b=2"(entity-encoded)findFeed()decodes:https://example.com/rss?a=1&b=2(raw URL)https://example.com/rss?a=1&b=2(output)Template Construction Pattern
Fixed template source:
'<%= title %>: <a href="<%= url %>"><%= url %></a> <%=i18n["ERROR_MANY_TIME"]%> <%= errorMessage %>'Data passed as locals:
This separation ensures data is never evaluated as template code.
Out of Scope
This PR's escaping guarantee is limited to error and feed-change notifications. The following Telegram HTML outputs are NOT changed by this PR:
send.ts(line ~174)sub-multi-url.tsimport-from-opml.tscontrolers/rss.tsThese may be addressed in future work if needed.
Commit History
cdba6e5- Initial hardening with escaped localsf1b9cb7- Address Codex feedback on raw vs escaped outputfdc87a3- Extract production functions, rewrite tests9048ac1- Fix double-escaping, add comprehensive full-text testsFiles Changed
source/index.ts- Use builder functions for notificationssource/utils/notification-messages.ts- Message construction functionssource/utils/html-entities.ts- HTML entity decodersource/utils/feed.ts- Decode entities in findFeed()source/utils/send.ts- Added templateLocals parametertest/template-injection.spec.ts- 11 comprehensive security teststest/html-entities.spec.ts- 7 entity decoder teststest/feed-discovery.spec.ts- 6 feed discovery tests