Repository navigation
fix(file): accept absolute paths and show the server's reason for missing source #69
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,14 @@ import ora from "ora"; | |
| import ansis from "ansis"; | ||
| import pluralize from "pluralize"; | ||
| import { repositoryTokenOption, resolveAccountAuth } from "../utils/auth"; | ||
| import { handleError } from "../utils/error"; | ||
| import { errorReason, handleError } from "../utils/error"; | ||
| import { createTable, getOutputFormat, pickDeep, printJson } from "../utils/output"; | ||
| import { sanitizeText } from "../utils/sanitize"; | ||
| import { resolveRepoArgs } from "../utils/resolve-repo-args"; | ||
| import { | ||
| fetchAllFiles, | ||
| getCwdRepoRelativePath, | ||
| getGitRepoRoot, | ||
| normalizeRepoPath, | ||
| } from "../utils/repo-tree"; | ||
| import { | ||
|
|
@@ -77,13 +78,25 @@ export function parseLineRange(value: string): LineRange { | |
|
|
||
| /** | ||
| * Turn the user's path argument into a repository-relative path. With an | ||
| * auto-detected repository the path is relative to the current directory (like | ||
| * a local file path); with an explicit repository it is relative to the root. | ||
| * auto-detected repository the path is a local one: relative to the current | ||
| * directory, or absolute (what shell completion and editors hand over), which is | ||
| * resolved against the git root. With an explicit repository it is relative to | ||
| * the repository root. A local path that lands outside the repository is | ||
| * refused before any request is made. | ||
| */ | ||
| export function resolveFilePath(input: string, autoDetected: boolean): string { | ||
| const joined = autoDetected | ||
| ? path.posix.join(getCwdRepoRelativePath(), input.trim()) | ||
| : input; | ||
| if (!autoDetected) return normalizeRepoPath(input); | ||
| const trimmed = input.trim(); | ||
| let joined: string; | ||
| if (path.isAbsolute(trimmed)) { | ||
| const root = getGitRepoRoot(); | ||
| joined = root ? path.relative(root, trimmed).split(path.sep).join("/") : ".."; | ||
| } else { | ||
| joined = path.posix.join(getCwdRepoRelativePath(), trimmed); | ||
| } | ||
| if (joined === ".." || joined.startsWith("../")) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π‘ MEDIUM RISK On Windows, an absolute path on another drive is not rejected as outside the repository because
|
||
| throw new Error(`Path '${trimmed}' is outside the repository.`); | ||
| } | ||
| return normalizeRepoPath(joined); | ||
| } | ||
|
|
||
|
|
@@ -198,7 +211,9 @@ async function fetchFileData( | |
| ); | ||
| source = content.data; | ||
| } catch (err) { | ||
| sourceError = err instanceof Error ? err.message : String(err); | ||
| // errorReason, not err.message: for an ApiError the latter is the generated | ||
| // client's static status name, which hides the server's explanation. | ||
| sourceError = errorReason(err); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
π‘ MEDIUM RISK
An absolute path through a symlink to the repository is rejected even when it names a file inside the repository. Consider resolving symlink aliases before comparing the path with the git root.
TIPReact π if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.