Skip to content

The agent that grows with you

fix(desktop): validate code-copy URL reference hosts

Denis H9d4833979595
diff --git a/apps/desktop/src/components/chat/shiki-highlighter.test.ts b/apps/desktop/src/components/chat/shiki-highlighter.test.ts
index 15629ffe23a..1bd91e5261e 100644
--- a/apps/desktop/src/components/chat/shiki-highlighter.test.ts
+++ b/apps/desktop/src/components/chat/shiki-highlighter.test.ts
@@ -53,12 +53,10 @@ 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', () => {
- const http = '@url:http://'
- const https = '@url:https://'
-
- expect(copyableCodeText(http)).toBe(http)
- expect(copyableCodeText(https)).toBe(https)
+ it('keeps hostless and malformed-host HTTP(S) URL directives raw', () => {
+ for (const code of ['@url:http://', '@url:https://', '@url:http://?', '@url:https://#fragment', '@url:http://:80']) {
+ expect(copyableCodeText(code)).toBe(code)
+ }
})
it('leaves invalid URL directives and other reference kinds unchanged', () => {
diff --git a/apps/desktop/src/components/chat/shiki-highlighter.tsx b/apps/desktop/src/components/chat/shiki-highlighter.tsx
index 2a3088c09d9..bbca7c0bd00 100644
--- a/apps/desktop/src/components/chat/shiki-highlighter.tsx
+++ b/apps/desktop/src/components/chat/shiki-highlighter.tsx
@@ -126,15 +126,23 @@ const PlainCode: FC<{ code: string }> = ({ code }) => {
)
}
-// Keep code literals such as `@url:http://` raw: a copyable URL needs a host.
-const URL_REFERENCE_VALUE_RE = /^https?:\/\/[^/\s]\S*$/i
+/** Match the composer's contract: only HTTP(S) URLs with a parsed host become URL references. */
+function isHttpUrlWithHost(value: string): boolean {
+ try {
+ const url = new URL(value)
+
+ return (url.protocol === 'http:' || url.protocol === 'https:') && Boolean(url.hostname)
+ } catch {
+ return false
+ }
+}
/** Convert URL references back to literal URLs for code-block clipboard text. */
export function copyableCodeText(code: string): string {
return code.replace(referenceRe(), (directive, kind: string, value: string) => {
const url = unquoteReferenceValue(value)
- return kind === 'url' && URL_REFERENCE_VALUE_RE.test(url) ? url : directive
+ return kind === 'url' && isHttpUrlWithHost(url) ? url : directive
})
}