-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(copilot): validate credential-link URL scheme before rendering #5416
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
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: 0 additions & 58 deletions
58
apps/sim/app/(interfaces)/chat/components/message/components/file-download.test.tsx
This file was deleted.
Oops, something went wrong.
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
91 changes: 91 additions & 0 deletions
91
...orkspaceId]/home/components/message-content/components/special-tags/special-tags.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,91 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import { act } from 'react' | ||
| import { createRoot, type Root } from 'react-dom/client' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockUseUserPermissionsContext } = vi.hoisted(() => ({ | ||
| mockUseUserPermissionsContext: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/app/workspace/[workspaceId]/providers/workspace-permissions-provider', () => ({ | ||
| useUserPermissionsContext: mockUseUserPermissionsContext, | ||
| })) | ||
|
|
||
| vi.mock('next/navigation', () => ({ | ||
| useParams: () => ({ workspaceId: 'workspace-1' }), | ||
| })) | ||
|
|
||
| import type { CredentialTagData } from '@/app/workspace/[workspaceId]/home/components/message-content/components/special-tags/special-tags' | ||
| import { SpecialTags } from '@/app/workspace/[workspaceId]/home/components/message-content/components/special-tags/special-tags' | ||
|
|
||
| /** | ||
| * Minimal dependency-free render harness (the repo has no `@testing-library/react`). Mounts the | ||
| * component in a real React 19 root under jsdom, matching the pattern in `use-autosave.test.tsx`. | ||
| */ | ||
| function renderCredentialLink(data: CredentialTagData): { container: HTMLDivElement; root: Root } { | ||
| ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true | ||
| const container = document.createElement('div') | ||
| const root: Root = createRoot(container) | ||
| act(() => { | ||
| root.render(<SpecialTags segment={{ type: 'credential', data }} />) | ||
| }) | ||
| return { container, root } | ||
| } | ||
|
|
||
| describe('CredentialDisplay link tag', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockUseUserPermissionsContext.mockReturnValue({ canEdit: true }) | ||
| }) | ||
|
|
||
| it('does not render an anchor for a javascript: scheme value', () => { | ||
| const { container, root } = renderCredentialLink({ | ||
| type: 'link', | ||
| provider: 'github', | ||
| value: 'javascript:alert(1)', | ||
| }) | ||
|
|
||
| expect(container.querySelector('a')).toBeNull() | ||
| act(() => root.unmount()) | ||
| }) | ||
|
|
||
| it('does not render an anchor for a data: scheme value', () => { | ||
| const { container, root } = renderCredentialLink({ | ||
| type: 'link', | ||
| provider: 'github', | ||
| value: 'data:text/html,<script>alert(1)</script>', | ||
| }) | ||
|
|
||
| expect(container.querySelector('a')).toBeNull() | ||
| act(() => root.unmount()) | ||
| }) | ||
|
|
||
| it('renders a working link for a real http(s) connect URL', () => { | ||
| const url = 'https://github.com/login/oauth/authorize?client_id=abc&scope=repo' | ||
| const { container, root } = renderCredentialLink({ | ||
| type: 'link', | ||
| provider: 'github', | ||
| value: url, | ||
| }) | ||
|
|
||
| const link = container.querySelector('a') | ||
| expect(link).not.toBeNull() | ||
| expect(link?.getAttribute('href')).toBe(url) | ||
| expect(container.textContent).toContain('Connect github') | ||
| act(() => root.unmount()) | ||
| }) | ||
|
|
||
| it('renders nothing when the user cannot edit, regardless of URL safety', () => { | ||
| mockUseUserPermissionsContext.mockReturnValue({ canEdit: false }) | ||
| const { container, root } = renderCredentialLink({ | ||
| type: 'link', | ||
| provider: 'github', | ||
| value: 'https://github.com/login/oauth/authorize', | ||
| }) | ||
|
|
||
| expect(container.querySelector('a')).toBeNull() | ||
| act(() => root.unmount()) | ||
| }) | ||
| }) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.