Skip to content
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

fix(node/fs): add missing stat path argument validation #27086

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ext/node/polyfills/_fs/_fs_stat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { denoErrorToNodeError } from "ext:deno_node/internal/errors.ts";
import { promisify } from "ext:deno_node/internal/util.mjs";
import { primordials } from "ext:core/mod.js";
import { getValidatedPath } from "ext:deno_node/internal/fs/utils.mjs";

const { ObjectCreate, ObjectAssign } = primordials;

Expand Down Expand Up @@ -379,6 +380,7 @@ export function stat(
? optionsOrCallback
: { bigint: false };

path = getValidatedPath(path).toString();
if (!callback) throw new Error("No callback function supplied");

Deno.stat(path).then(
Expand Down Expand Up @@ -409,6 +411,8 @@ export function statSync(
path: string | URL,
options: statOptions = { bigint: false, throwIfNoEntry: true },
): Stats | BigIntStats | undefined {
path = getValidatedPath(path).toString();

try {
const origin = Deno.statSync(path);
return CFISBIS(origin, options.bigint);
Expand Down
37 changes: 36 additions & 1 deletion tests/unit_node/_fs/_fs_stat_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertCallbackErrorUncaught } from "../_test_utils.ts";
import { BigIntStats, stat, Stats, statSync } from "node:fs";
import { assertEquals, fail } from "@std/assert";
import { assert, assertEquals, fail } from "@std/assert";

export function assertStats(actual: Stats, expected: Deno.FileInfo) {
assertEquals(actual.dev, expected.dev);
Expand Down Expand Up @@ -152,3 +152,38 @@ Deno.test({
assertEquals(stats.isSocket(), false);
},
});

Deno.test({
name: "[node/fs] stat invalid path error",
async fn() {
try {
await new Promise<Stats>((resolve, reject) => {
stat(
// deno-lint-ignore no-explicit-any
undefined as any,
(err, stats) => err ? reject(err) : resolve(stats),
);
});
fail();
} catch (err) {
assert(err instanceof TypeError);
// deno-lint-ignore no-explicit-any
assertEquals((err as any).code, "ERR_INVALID_ARG_TYPE");
}
},
});

Deno.test({
name: "[node/fs] statSync invalid path error",
fn() {
try {
// deno-lint-ignore no-explicit-any
statSync(undefined as any);
fail();
} catch (err) {
assert(err instanceof TypeError);
// deno-lint-ignore no-explicit-any
assertEquals((err as any).code, "ERR_INVALID_ARG_TYPE");
}
},
});
5 changes: 4 additions & 1 deletion tests/unit_node/fs_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// <reference lib="deno.ns" />
import { assert, assertEquals, assertRejects, assertThrows } from "@std/assert";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { tmpdir } from "node:os";
import {
closeSync,
Expand Down Expand Up @@ -160,7 +161,9 @@ Deno.test(
} catch (error: unknown) {
assertEquals(
`${error}`,
`Error: ENOENT: no such file or directory, stat '${fileUrl.pathname}'`,
`Error: ENOENT: no such file or directory, stat '${
fileURLToPath(fileUrl)
}'`,
);
}
},
Expand Down