-
Notifications
You must be signed in to change notification settings - Fork 29
fix(web): open assistant workspace links in-app #351
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
Show all changes
14 commits
Select commit
Hold shift + click to select a range
59ccc1f
todo: Draft and approve the evidence-link handling design
rsolmano 378177b
todo: Implement and test safe workspace-file navigation
rsolmano a991cd0
todo: Review the full diff and capture UI proof
rsolmano 8e522df
Merge branch 'main' into fix/chat-workspace-links
rsolmano dc2b6e6
Merge branch 'main' into fix/chat-workspace-links
rsolmano c7659a9
Merge branch 'main' into fix/chat-workspace-links
rsolmano 0215fcb
fix(web): address assistant link review findings
rsolmano b821621
fix(web): match Windows paths case-insensitively
rsolmano ca828e1
Merge branch 'main' into fix/chat-workspace-links
danyaberezun bb009b7
Merge branch 'main' into fix/chat-workspace-links
danyaberezun f1652c5
Merge branch 'main' into fix/chat-workspace-links
danyaberezun b49a9fe
Merge branch 'main' into fix/chat-workspace-links
danyaberezun 19e63fc
Merge branch 'main' into fix/chat-workspace-links
rsolmano e9ae7da
todo: Adjust tests to current behavior
rsolmano 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { renderToStaticMarkup } from "react-dom/server"; | ||
| import { AssistantMarkdown, assistantFileTarget, assistantUrlTransform } from "./assistantLinks"; | ||
|
|
||
| describe("assistantFileTarget", () => { | ||
| test("accepts normalized relative paths and absolute paths inside the workspace", () => { | ||
| expect(assistantFileTarget(".thinkrail/context/report.md", "/repo")).toBe( | ||
| ".thinkrail/context/report.md", | ||
| ); | ||
| expect(assistantFileTarget("docs/../README.md#usage", "/repo")).toBe("README.md"); | ||
| expect(assistantFileTarget("reports/manual%20report.md", "/repo")).toBe( | ||
| "reports/manual report.md", | ||
| ); | ||
| expect(assistantFileTarget("/repo/docs/report.md?raw=1", "/repo")).toBe("docs/report.md"); | ||
| expect(assistantFileTarget("c:/repo/docs/Report.md", "C:/Repo")).toBe("docs/Report.md"); | ||
| }); | ||
|
|
||
| test("rejects external, fragment, outside-workspace, and context-free targets", () => { | ||
| for (const href of [ | ||
| "https://example.com/report", | ||
| "mailto:owner@example.com", | ||
| "//example.com/report", | ||
| "#report", | ||
| "../report.md", | ||
| "%2e%2e/report.md", | ||
| "/tmp/report.md", | ||
| "report%E0%A4%A.md", | ||
| ]) { | ||
| expect(assistantFileTarget(href, "/repo")).toBeNull(); | ||
| } | ||
| expect(assistantFileTarget("README.md", undefined)).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| test("assistant URL transforms preserve only anchor-shaped Windows paths", () => { | ||
| const anchor = { tagName: "a" }; | ||
| expect(assistantUrlTransform("C:/repo/report.md", "href", anchor)).toBe("C:/repo/report.md"); | ||
| expect(assistantUrlTransform("C:%5Crepo%5Creport.md", "href", anchor)).toBe( | ||
| "C:%5Crepo%5Creport.md", | ||
| ); | ||
| expect(assistantUrlTransform("C:/repo/image.png", "src", { tagName: "img" })).toBe(""); | ||
| expect(assistantUrlTransform("C:/repo/style.css", "href", { tagName: "link" })).toBe(""); | ||
| expect(assistantUrlTransform("javascript:alert(1)", "href", anchor)).toBe(""); | ||
| expect(assistantUrlTransform("https://example.com/report", "href", anchor)).toBe( | ||
| "https://example.com/report", | ||
| ); | ||
| }); | ||
|
|
||
| test("assistant Markdown distinguishes workspace files from ordinary anchors", () => { | ||
| const html = renderToStaticMarkup( | ||
| <AssistantMarkdown | ||
| text={[ | ||
| "[report](<.thinkrail/context/manual report.md>)", | ||
| "[site](https://example.com/report)", | ||
| "[outside](../report.md)", | ||
| ].join("\n\n")} | ||
| workspaceRoot="/repo" | ||
| onOpenFile={() => {}} | ||
| />, | ||
| ); | ||
|
|
||
| expect(html).toContain('<button type="button" data-testid="chat-file-link"'); | ||
| expect(html).toContain('data-path=".thinkrail/context/manual report.md"'); | ||
| expect(html).not.toContain('href=".thinkrail/context/manual%20report.md"'); | ||
| expect(html.match(/target="_blank"/g)).toHaveLength(2); | ||
| expect(html).toContain('href="https://example.com/report"'); | ||
| expect(html).toContain('href="../report.md"'); | ||
| }); | ||
|
|
||
| test("assistant Markdown opens contained Windows paths without exposing unsafe URLs", () => { | ||
| const html = renderToStaticMarkup( | ||
| <AssistantMarkdown | ||
| text={[ | ||
| "[inside](c:/repo/docs/Report.md)", | ||
| "[inside backslash](c:\\REPO\\docs\\backslash.md)", | ||
| "[outside](D:/other/report.md)", | ||
| "[unsafe](javascript:alert(1))", | ||
| ].join("\n\n")} | ||
| workspaceRoot="C:/Repo" | ||
| onOpenFile={() => {}} | ||
| />, | ||
| ); | ||
|
|
||
| expect(html.match(/data-testid="chat-file-link"/g)).toHaveLength(2); | ||
| expect(html).toContain('data-path="docs/Report.md"'); | ||
| expect(html).toContain('data-path="docs/backslash.md"'); | ||
| expect(html).not.toContain('href="c:/repo/docs/Report.md"'); | ||
| expect(html).not.toContain('href="c:%5CREPO%5Cdocs%5Cbackslash.md"'); | ||
| expect(html).not.toContain('href="D:/other/report.md"'); | ||
| expect(html).not.toContain('href="javascript:alert(1)"'); | ||
| }); |
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 @@ | ||
| import { type ReactNode, useMemo } from "react"; | ||
| import { type Components, defaultUrlTransform } from "react-markdown"; | ||
| import { isWindowsAbsolutePath, workspaceFileTarget } from "./fileTargets"; | ||
| import { Markdown } from "./Markdown"; | ||
|
|
||
| export function assistantUrlTransform( | ||
| value: string, | ||
| property: string, | ||
| node: { tagName: string }, | ||
| ): string { | ||
| if (property === "href" && node.tagName === "a") { | ||
| try { | ||
| if (isWindowsAbsolutePath(decodeURIComponent(value))) return value; | ||
| } catch { | ||
| return defaultUrlTransform(value); | ||
| } | ||
| } | ||
| return defaultUrlTransform(value); | ||
| } | ||
|
|
||
| export function assistantFileTarget( | ||
| href: string | undefined, | ||
| workspaceRoot: string | undefined, | ||
| ): string | null { | ||
| const candidate = href?.trim(); | ||
| if (!candidate || !workspaceRoot || candidate.startsWith("#") || candidate.startsWith("//")) { | ||
| return null; | ||
| } | ||
| const encodedPath = candidate.split(/[?#]/, 1)[0]; | ||
| if (!encodedPath) return null; | ||
| try { | ||
| return workspaceFileTarget(decodeURIComponent(encodedPath), workspaceRoot); | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function AssistantLink({ | ||
| href, | ||
| children, | ||
| workspaceRoot, | ||
| onOpenFile, | ||
| }: { | ||
| href?: string | undefined; | ||
| children?: ReactNode; | ||
| workspaceRoot?: string | undefined; | ||
| onOpenFile?: ((path: string) => void) | undefined; | ||
| }) { | ||
| const target = assistantFileTarget(href, workspaceRoot); | ||
| if (!target || !onOpenFile) { | ||
| const safeHref = href === undefined ? undefined : defaultUrlTransform(href); | ||
| return ( | ||
| <a href={safeHref} target="_blank" rel="noopener noreferrer"> | ||
| {children} | ||
| </a> | ||
| ); | ||
| } | ||
| return ( | ||
| <button | ||
| type="button" | ||
| data-testid="chat-file-link" | ||
| data-path={target} | ||
| onClick={() => onOpenFile(target)} | ||
| className="cursor-pointer text-left text-primary underline" | ||
| > | ||
| {children} | ||
| </button> | ||
| ); | ||
| } | ||
|
|
||
| export function AssistantMarkdown({ | ||
| text, | ||
| workspaceRoot, | ||
| onOpenFile, | ||
| }: { | ||
| text: string; | ||
| workspaceRoot?: string | undefined; | ||
| onOpenFile?: ((path: string) => void) | undefined; | ||
| }) { | ||
| const components = useMemo<Components>( | ||
| () => ({ | ||
| a: ({ href, children }) => ( | ||
| <AssistantLink href={href} workspaceRoot={workspaceRoot} onOpenFile={onOpenFile}> | ||
| {children} | ||
| </AssistantLink> | ||
| ), | ||
| }), | ||
| [workspaceRoot, onOpenFile], | ||
| ); | ||
| return <Markdown text={text} urlTransform={assistantUrlTransform} components={components} />; | ||
| } |
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,19 @@ | ||
| import { describe, expect, it } from "bun:test"; | ||
| import { workspaceFileTarget } from "./fileTargets"; | ||
|
|
||
| describe("workspaceFileTarget", () => { | ||
| it("canonicalizes only paths contained by the active worktree", () => { | ||
| for (const [path, root, expected] of [ | ||
| ["module-a/../README.md", "/repo", "README.md"], | ||
| ["/repo/module-a/SPEC.md", "/repo", "module-a/SPEC.md"], | ||
| ["C:\\repo\\module-a\\SPEC.md", "C:\\repo", "module-a/SPEC.md"], | ||
| ["/repo-other/SPEC.md", "/repo", null], | ||
| ["../outside.md", "/repo", null], | ||
| ["https://example.com/a.md", "/repo", null], | ||
| ["file:///repo/a.md", "/repo", null], | ||
| ["", "/repo", null], | ||
| ] as const) { | ||
| expect(workspaceFileTarget(path, root)).toBe(expected); | ||
| } | ||
| }); | ||
| }); |
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,26 @@ | ||
| import { isAbsolutePath, projectRelativePath } from "@/lib"; | ||
|
|
||
| const URI_SCHEME = /^[A-Za-z][A-Za-z0-9+.-]*:/; | ||
| const WINDOWS_ABSOLUTE_PATH = /^[A-Za-z]:[\\/]/; | ||
|
|
||
| export function hasUriScheme(value: string): boolean { | ||
| return URI_SCHEME.test(value); | ||
| } | ||
|
|
||
| export function isWindowsAbsolutePath(value: string): boolean { | ||
| return WINDOWS_ABSOLUTE_PATH.test(value); | ||
| } | ||
|
|
||
| export function workspaceFileTarget( | ||
| path: string, | ||
| workspaceRoot?: string | undefined, | ||
| ): string | null { | ||
| const candidate = path.trim(); | ||
| if (!candidate) return null; | ||
| if (!isAbsolutePath(candidate) && hasUriScheme(candidate)) return null; | ||
| const relative = projectRelativePath(candidate, workspaceRoot); | ||
| if (!relative || isAbsolutePath(relative) || relative === ".." || relative.startsWith("../")) { | ||
| return null; | ||
| } | ||
| return relative; | ||
| } | ||
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.
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.