Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -295,24 +299,17 @@ async function startFetchProcess(restartTime: number): Promise<void> {
} else {
if (isErrorMaxTime(message)) {
const { feed, err } = message;
send(
bot,
`${feed.feed_title}: <a href="${feed.url}">${feed.url}</a> <%=i18n['ERROR_MANY_TIME']%> ${err.message}`,
feed
);
const [template, locals] = buildErrorNotification(feed, err);
send(bot, template, feed, locals);
}
if (isChangeFeedUrl(message)) {
const { feed, new_feed } = message;
const builder = [];
builder.push(
`${feed.feed_title}: <a href="${encodeUrl(
feed.url
)}"></a> <%=i18n['ERROR_MANY_TIME']%>`
); // feed is from database which not urlencoded
builder.push(`<b><%=i18n['FOUND_FEEDS']%></b>:`);
builder.push(...new_feed);
builder.push(`<%=i18n['FEED_CHANGE_TO']%> ${new_feed[0]}`);
send(bot, builder.join('\n'), feed);
const [template, locals] = buildFeedChangeNotification(
feed,
new_feed,
encodeUrl
);
send(bot, template, feed, locals);
}
}
});
Expand Down
63 changes: 49 additions & 14 deletions source/utils/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<TRSS>> {
try {
Expand All @@ -12,26 +13,60 @@ export async function isFeedValid(feedStr: string): Promise<Option<TRSS>> {
}
}

/**
* 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., &amp; -> &) 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<string[]> {
const reqURL = new URL(reqUrl);
const linksTag = html.match(/<link[^>]+rel="alternate"[^>]+\/?>/g);
if (!linksTag) {
const baseUrl = new URL(reqUrl);
const linkTags = html.match(/<link[^>]+rel="alternate"[^>]+\/?>/g);

if (!linkTags) {
return [];
}
return linksTag
.filter((t) => t.match(/rss|atom/) && t.includes('href'))
.map((linkTag) => {
const url = linkTag.match(/href="(.+?)"/)[1];
try {
return new URL(url).toString();
} catch {
reqURL.pathname = url;
return reqURL.toString();
}
});

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(
Expand Down
44 changes: 44 additions & 0 deletions source/utils/html-entities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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&amp;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<string, string> = {
amp: '&',
lt: '<',
gt: '>',
quot: '"',
apos: "'",
nbsp: '\u00A0'
};

/**
* Decode HTML entities in a string.
*
* Supports:
* - Named entities: &amp; &lt; &gt; &quot; &apos; &nbsp;
* - Numeric entities (decimal): &#38; &#60;
* - Numeric entities (hex): &#x26; &#x3C;
*
* @param text - String potentially containing HTML entities
* @returns String with entities decoded to their literal characters
*/
export function decodeHtmlEntities(text: string): string {
return text
.replace(/&([a-z]+);/gi, (match, entityName) => {
const normalized = entityName.toLowerCase();
return NAMED_ENTITIES[normalized] ?? match;
})
.replace(/&#(\d+);/g, (_match, decimal) => {
return String.fromCharCode(parseInt(decimal));
})
.replace(/&#x([0-9a-f]+);/gi, (_match, hexadecimal) => {
return String.fromCharCode(parseInt(hexadecimal, 16));
});
}
93 changes: 93 additions & 0 deletions source/utils/notification-messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Notification message templates for RSS feed errors and changes.
*
* 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 %>: ' +
'<a href="<%= url %>"><%= url %></a> ' +
'<%=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 %>: <a href="<%= url %>"></a> <%=i18n["ERROR_MANY_TIME"]%>\n' +
'<b><%=i18n["FOUND_FEEDS"]%></b>:\n' +
'<% newFeedUrls.forEach(function(feedUrl) { %><%= feedUrl %>\n<% }); %>' +
'<%=i18n["FEED_CHANGE_TO"]%> <%= newFeedUrls[0] %>';

/**
* Build error notification message template and locals.
*
* @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] {
return [
ERROR_NOTIFICATION_TEMPLATE,
{
title: feed.feed_title,
url: feed.url,
errorMessage: error.message
}
];
}

/**
* Build feed URL change notification message template and locals.
*
* @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] {
return [
FEED_CHANGE_NOTIFICATION_TEMPLATE,
{
title: feed.feed_title,
url: urlEncoder(feed.url),
newFeedUrls
}
];
}
8 changes: 6 additions & 2 deletions source/utils/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ async function handlerSendError(e: any, userId: number): Promise<boolean> {
const send = async (
bot: Telegraf<Context>,
toSend: NonNullable<string | FeedItem[]>,
feed: Feed
feed: Feed,
templateLocals?: Record<string, any>
): Promise<void> => {
const subscribers = await getSubscribersByFeedId(feed.feed_id);
if (typeof toSend === 'string') {
Expand All @@ -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 }
Expand Down
Loading
Loading