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

fs: fix existsSync for invalid symlink at win32 #30556

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 10 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,16 @@ function existsSync(path) {
return false;
}
const ctx = { path };
binding.access(pathModule.toNamespacedPath(path), F_OK, undefined, ctx);
const nPath = pathModule.toNamespacedPath(path);
binding.access(nPath, F_OK, undefined, ctx);

// In case of an invalid symlink, `binding.access()` on win32
// will **not** return an error and is therefore not enough.
// Double check with `binding.stat()`.
if (isWindows && ctx.errno === undefined) {
binding.stat(nPath, false, undefined, ctx);
}

return ctx.errno === undefined;
}

Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-fs-symlink-dir-junction.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,20 @@ fs.symlink(linkData, linkPath, 'junction', common.mustCall(function(err) {
}));
}));
}));

// Test invalid symlink
{
const linkData = fixtures.path('/not/exists/dir');
const linkPath = path.join(tmpdir.path, 'invalid_junction_link');

fs.symlink(linkData, linkPath, 'junction', common.mustCall(function(err) {
assert.ifError(err);

assert(!fs.existsSync(linkPath));

fs.unlink(linkPath, common.mustCall(function(err) {
assert.ifError(err);
assert(!fs.existsSync(linkPath));
}));
}));
}
22 changes: 22 additions & 0 deletions test/parallel/test-fs-symlink-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,25 @@ for (const linkTarget of linkTargets) {
testAsync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-async`);
}
}

// Test invalid symlink
{
function testSync(target, path) {
fs.symlinkSync(target, path);
assert(!fs.existsSync(path));
}

function testAsync(target, path) {
fs.symlink(target, path, common.mustCall((err) => {
assert.ifError(err);
assert(!fs.existsSync(path));
}));
}

for (const linkTarget of linkTargets.map((p) => p + '-broken')) {
for (const linkPath of linkPaths) {
testSync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-sync`);
testAsync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-async`);
}
}
}
12 changes: 12 additions & 0 deletions test/parallel/test-fs-symlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ fs.symlink(linkData, linkPath, common.mustCall(function(err) {
}));
}));

// Test invalid symlink
{
const linkData = fixtures.path('/not/exists/file');
const linkPath = path.join(tmpdir.path, 'symlink2.js');

fs.symlink(linkData, linkPath, common.mustCall(function(err) {
assert.ifError(err);

assert(!fs.existsSync(linkPath));
}));
}

[false, 1, {}, [], null, undefined].forEach((input) => {
const errObj = {
code: 'ERR_INVALID_ARG_TYPE',
Expand Down