Skip to content

The agent that grows with you

fix(desktop): reject malformed URL directives

Denis Hcee781a62fc9
diff --git a/apps/desktop/src/app/chat/composer/url-refs.ts b/apps/desktop/src/app/chat/composer/url-refs.ts
index bff92a98a2b..c1ba2e3c9c5 100644
--- a/apps/desktop/src/app/chat/composer/url-refs.ts
+++ b/apps/desktop/src/app/chat/composer/url-refs.ts
@@ -134,8 +134,16 @@ function splitUrlTail(raw: string) {
return { trailing: raw.slice(url.length), url }
}
-/** A URL needs a host past the scheme to be worth treating as a URL reference. */
-export const hasHttpUrlHost = (url: string) => /^https?:\/\/[^/\s]/i.test(splitUrlTail(url).url)
+/** A URL needs a valid HTTP(S) authority and host to be worth treating as a URL reference. */
+export const hasHttpUrlHost = (url: string) => {
+ try {
+ const parsed = new URL(splitUrlTail(url).url)
+
+ return (parsed.protocol === 'http:' || parsed.protocol === 'https:') && Boolean(parsed.hostname)
+ } catch {
+ return false
+ }
+}
/** Rewrite bare links in `text` as `@url:` directives, leaving links that are
* already part of a directive alone. Returns `text` unchanged when there are
diff --git a/apps/desktop/src/components/chat/shiki-highlighter.test.ts b/apps/desktop/src/components/chat/shiki-highlighter.test.ts
index 81198cbb8c6..36e9f8d6cc5 100644
--- a/apps/desktop/src/components/chat/shiki-highlighter.test.ts
+++ b/apps/desktop/src/components/chat/shiki-highlighter.test.ts
@@ -53,8 +53,15 @@ second @url:"https://two.example" @file:\`src/app.ts\``
expect(copyableCodeText(code)).toBe('first https://one.example\nsecond https://two.example @file:`src/app.ts`')
})
- it('keeps hostless HTTP(S) URL directives raw', () => {
- for (const code of ['@url:http://', '@url:https://', '@url:http://?', '@url:https://?']) {
+ it('keeps hostless or malformed HTTP(S) URL directives raw', () => {
+ for (const code of [
+ '@url:http://',
+ '@url:https://',
+ '@url:http://?',
+ '@url:https://?',
+ '@url:http://:80',
+ '@url:https://#fragment',
+ ]) {
expect(copyableCodeText(code)).toBe(code)
}
})