-
Notifications
You must be signed in to change notification settings - Fork 5.9k
fix: check uri.path is string in pathToFsPath #3751
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 |
---|---|---|
|
@@ -453,3 +453,44 @@ describe("escapeHtml", () => { | |
) | ||
}) | ||
}) | ||
|
||
describe("pathToFsPath", () => { | ||
it("should convert a path to a file system path", () => { | ||
expect(util.pathToFsPath("/foo/bar/baz")).toBe("/foo/bar/baz") | ||
}) | ||
it("should lowercase drive letter casing by default", () => { | ||
expect(util.pathToFsPath("/C:/far/boo")).toBe("c:/far/boo") | ||
}) | ||
it("should keep drive letter casing when set to true", () => { | ||
expect(util.pathToFsPath("/C:/far/bo", true)).toBe("C:/far/bo") | ||
}) | ||
it("should throw an error if a non-string is passed in for path", () => { | ||
expect(() => | ||
util | ||
// @ts-expect-error We need to check other types | ||
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. If this function can take an object we should update the types on it I think. 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. It can't. I was just testing this logic if (typeof uri.path !== "string") {
throw new Error(
`Could not compute fsPath from given uri. Expected path to be of type string, but was of type ${typeof uri.path}.`,
)
} 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. Whoops I mean remove
Or in other words if Although I don't think we should make it accept a object so it probably makes more sense to remove this test. We don't currently have anywhere in the code where this arg might be an object (I think! I could be wrong.) 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. Sorry! I think my first comment was confusing. I added this piece of logic if (typeof uri.path !== "string") {
throw new Error(
`Could not compute fsPath from given uri. Expected path to be of type string, but was of type ${typeof uri.path}.`,
)
} to make sure we handled the scenario where something other than a string was being passed in. I think there is somewhere in the codebase where we're passing in something that we typecast as a In order to test that, I need to make 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. Right! What I mean is that there are two scenarios:
If the first is true then the types in the function signature should reflect this (with If the second is true then the code that is calling In our case the data comes in as So why do we get an error? I'm not 100% sure but I think what's happening is the tool is thinking that since TypeScript is able to detect this and show an error but whatever tool reported this apparently can't. If that's the case then we'll need to extract it to a constant first, e.g.: const path = getFirstString(req.query.path)
if (path) {
res.set("Content-Type", getMediaMime(path))
res.send(await fs.readFile(pathToFsPath(path))) Then our |
||
.pathToFsPath({}), | ||
).toThrow( | ||
`Could not compute fsPath from given uri. Expected path to be of type string, but was of type undefined.`, | ||
) | ||
}) | ||
it("should not throw an error for a string array", () => { | ||
// @ts-expect-error We need to check other types | ||
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. Can remove. |
||
expect(() => util.pathToFsPath(["/hello/foo", "/hello/bar"]).not.toThrow()) | ||
}) | ||
it("should use the first string in a string array", () => { | ||
expect(util.pathToFsPath(["/hello/foo", "/hello/bar"])).toBe("/hello/foo") | ||
}) | ||
it("should replace / with \\ on Windows", () => { | ||
let ORIGINAL_PLATFORM = process.platform | ||
|
||
Object.defineProperty(process, "platform", { | ||
value: "win32", | ||
}) | ||
|
||
expect(util.pathToFsPath("/C:/far/boo")).toBe("c:\\far\\boo") | ||
|
||
Object.defineProperty(process, "platform", { | ||
value: ORIGINAL_PLATFORM, | ||
}) | ||
}) | ||
}) |
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.
getFirstString
can only return a string or undefined so this could be something like: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.
Will do