Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(channel-reader): drop a polynomial regex from the profile link builder

Jose Ramirez147a77c4dd2d
diff --git a/channel-reader/src/__tests__/main.test.ts b/channel-reader/src/__tests__/main.test.ts
index 0907cfc45..0f5e5d9bc 100644
--- a/channel-reader/src/__tests__/main.test.ts
+++ b/channel-reader/src/__tests__/main.test.ts
@@ -1,7 +1,7 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { TeamsAdapter } from '../channels/teams'
import type { TeamsMessageHandoff } from '../handoffServer'
-import { ChannelReader, type ChannelReaderOptions } from '../main'
+import { ChannelReader, type ChannelReaderOptions, profileSocialChannelUrl } from '../main'
import { createProgressStream } from '../progressClient'
import type { MessageResponse } from '../rpcClient'
import type { ChannelAdapter, Message } from '../types'
@@ -1290,3 +1290,40 @@ describe('ChannelReader pollCycle provider guards', () => {
expect(messages).toHaveLength(1)
})
})
+
+describe('profileSocialChannelUrl', () => {
+ it('appends the provider tab path', () => {
+ expect(profileSocialChannelUrl('https://profile.example.com', 'teams')).toBe(
+ 'https://profile.example.com/settings/social/teams'
+ )
+ expect(profileSocialChannelUrl('https://profile.example.com', 'slack')).toBe(
+ 'https://profile.example.com/settings/social/slack'
+ )
+ })
+
+ it('falls back to the bare base URL for a provider with no tab', () => {
+ // Better than guessing a segment: profile-ui 404s on an unknown network.
+ expect(profileSocialChannelUrl('https://profile.example.com', 'telegram')).toBe(
+ 'https://profile.example.com'
+ )
+ })
+
+ // Regression guard for the /\/+$/ -> index-walk rewrite (CodeQL
+ // js/polynomial-redos). This asserts the rewrite preserved behaviour; it does
+ // NOT prove the ReDoS is gone, since the old regex produced the same string,
+ // only slowly. The linearity lives in the implementation, not in this test.
+ it('strips a run of trailing slashes, however long', () => {
+ expect(profileSocialChannelUrl('https://profile.example.com' + '/'.repeat(500), 'teams')).toBe(
+ 'https://profile.example.com/settings/social/teams'
+ )
+ expect(profileSocialChannelUrl(' https://profile.example.com// ', 'teams')).toBe(
+ 'https://profile.example.com/settings/social/teams'
+ )
+ })
+
+ it('leaves a URL with no trailing slash untouched', () => {
+ expect(profileSocialChannelUrl('https://p.example.com/base', 'teams')).toBe(
+ 'https://p.example.com/base/settings/social/teams'
+ )
+ })
+})
diff --git a/channel-reader/src/main.ts b/channel-reader/src/main.ts
index a5a8f4c05..bd6593f01 100644
--- a/channel-reader/src/main.ts
+++ b/channel-reader/src/main.ts
@@ -132,8 +132,15 @@ const PROFILE_SOCIAL_CHANNEL_PATH: Partial<Record<ChannelType, string>> = {
* degrades to today's behaviour rather than emitting a 404 path.
*/
export function profileSocialChannelUrl(baseUrl: string, channelType: ChannelType): string {
- const base = baseUrl.trim().replace(/\/+$/, '')
- return `${base}${PROFILE_SOCIAL_CHANNEL_PATH[channelType] ?? ''}`
+ const trimmed = baseUrl.trim()
+ // Trailing slashes are walked off by index rather than matched with /\/+$/.
+ // An end-anchored `+` over a run of the same character backtracks
+ // polynomially (CodeQL js/polynomial-redos), and profileUiUrl is deployment
+ // config that arrives here unvalidated. The walk is linear and needs no
+ // reasoning about the regex engine to stay that way.
+ let end = trimmed.length
+ while (end > 0 && trimmed.charCodeAt(end - 1) === 47 /* '/' */) end--
+ return `${trimmed.slice(0, end)}${PROFILE_SOCIAL_CHANNEL_PATH[channelType] ?? ''}`
}
// Telegram deliberately gets no Profile UI link and no product name: unlike Teams
// and Slack, anyone who discovers the bot handle can message it, so a reply must