Skip to content

Commit 6835d2d

Browse files
committed
fix: check path is string in pathToFsPath
There's a chance this function can be called with a path that is not a string. To catch that, we check if path is of a different type and throw an error if it is. This also adds a couple tests for this function.
1 parent 343cec5 commit 6835d2d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/node/util.ts

+7
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,13 @@ export function pathToFsPath(path: string, keepDriveLetterCasing = false): strin
461461
const isWindows = process.platform === "win32"
462462
const uri = { authority: undefined, path, scheme: "file" }
463463
let value: string
464+
465+
if (typeof uri.path !== "string") {
466+
throw new Error(
467+
`Could not computer fsPath from given uri. Expected path to be of type string, but was of type ${typeof uri.path}.`,
468+
)
469+
}
470+
464471
if (uri.authority && uri.path.length > 1 && uri.scheme === "file") {
465472
// unc path: file://shares/c$/far/boo
466473
value = `//${uri.authority}${uri.path}`

test/unit/node/util.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,34 @@ describe("escapeHtml", () => {
453453
)
454454
})
455455
})
456+
457+
describe("pathToFsPath", () => {
458+
it("should convert a path to a file system path", () => {
459+
expect(util.pathToFsPath("/foo/bar/baz")).toBe("/foo/bar/baz")
460+
})
461+
it("should lowercase drive letter casing by default", () => {
462+
expect(util.pathToFsPath("/C:/far/boo")).toBe("c:/far/boo")
463+
})
464+
it("should keep drive letter casing when set to true", () => {
465+
expect(util.pathToFsPath("/C:/far/bo", true)).toBe("C:/far/bo")
466+
})
467+
it("should throw an error if an array is passed in for path", () => {
468+
// @ts-expect-error We need to make sure it throws when an array is passed in.
469+
expect(() => util.pathToFsPath([])).toThrow(
470+
`Could not computer fsPath from given uri. Expected path to be of type string, but was of type object.`,
471+
)
472+
})
473+
it("should replace / with \\ on Windows", () => {
474+
let ORIGINAL_PLATFORM = process.platform
475+
476+
Object.defineProperty(process, "platform", {
477+
value: "win32",
478+
})
479+
480+
expect(util.pathToFsPath("/C:/far/boo")).toBe("c:\\far\\boo")
481+
482+
Object.defineProperty(process, "platform", {
483+
value: ORIGINAL_PLATFORM,
484+
})
485+
})
486+
})

0 commit comments

Comments
 (0)