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: Don't follow circular symlinks #126

Merged
merged 2 commits into from
Apr 8, 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
22 changes: 20 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ function isFound(glob) {
return isGlob(glob);
}

function getSymlinkInfo(filepath, cb) {
fs.realpath(filepath, function (err, realPath) {
if (err) return cb(err);

fs.lstat(realPath, function (err, lstat) {
if (err) return cb(err);

cb(null, {
destinationPath: realPath,
destinationStat: lstat,
});
});
});
}

function walkdir() {
var readdirOpts = {
withFileTypes: true,
Expand Down Expand Up @@ -95,12 +110,15 @@ function walkdir() {

if (dirent.isSymbolicLink()) {
// If it's a symlink, check if the symlink points to a directory
fs.stat(nextpath, function (err, stats) {
getSymlinkInfo(nextpath, function (err, info) {
if (err) {
return cb(err);
}

if (stats.isDirectory()) {
if (
info.destinationStat.isDirectory() &&
!nextpath.startsWith(info.destinationPath + path.sep) // don't follow circular symlinks
) {
cb(null, nextpath);
} else {
cb();
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/symlinks/circular-symlink
1 change: 1 addition & 0 deletions test/fixtures/symlinks/folder-a/folder-a-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello a
1 change: 1 addition & 0 deletions test/fixtures/symlinks/folder-a/link-to-b
1 change: 1 addition & 0 deletions test/fixtures/symlinks/folder-b/folder-b-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
world b
1 change: 1 addition & 0 deletions test/fixtures/symlinks/folder-b/link-to-a
38 changes: 37 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,12 +765,48 @@ function suite(moduleName) {
dir +
'/fixtures/symlinks/symlink-dest/hey/isaidhey/whatsgoingon/test.txt',
},

{
cwd: dir,
base: dir + '/fixtures/symlinks',
path: dir + '/fixtures/symlinks/folder-a/folder-a-file.txt',
},
{
cwd: dir,
base: dir + '/fixtures/symlinks',
path: dir + '/fixtures/symlinks/folder-b/folder-b-file.txt',
},

// It should follow these circular symlinks, but not infinitely
{
cwd: dir,
base: dir + '/fixtures/symlinks',
path: dir + '/fixtures/symlinks/folder-a/link-to-b/folder-b-file.txt',
},
{
cwd: dir,
base: dir + '/fixtures/symlinks',
path: dir + '/fixtures/symlinks/folder-b/link-to-a/folder-a-file.txt',
},

// And it should follow a symlink to a parent directory (circular symlink) without blowing up
{
cwd: dir,
base: dir + '/fixtures/symlinks',
path:
dir +
'/fixtures/symlinks/symlink-dest/hey/isaidhey/whatsgoingon/test.txt',
},
Comment on lines +792 to +799
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to check if this is a duplicate glob getting through because we wouldn't want to emit it twice.

];

function assert(pathObjs) {
expect(pathObjs.length).toBe(2);
expect(pathObjs.length).toBe(6);
expect(pathObjs).toContainEqual(expected[0]);
expect(pathObjs).toContainEqual(expected[1]);
expect(pathObjs).toContainEqual(expected[2]);
expect(pathObjs).toContainEqual(expected[3]);
expect(pathObjs).toContainEqual(expected[4]);
expect(pathObjs).toContainEqual(expected[5]);
}

stream.pipeline(
Expand Down
Loading