Skip to content

Add logic to detect recursive symlink - #2252

Closed
A (ealexjordan) wants to merge 3 commits into
PowerShell:masterfrom
ealexjordan:recursesymlink
Closed

A (ealexjordan) wants to merge 3 commits into
PowerShell:masterfrom
ealexjordan:recursesymlink

Conversation

@ealexjordan

Copy link
Copy Markdown
Contributor

Quick fix that detects when symlink recursively links back to parent directory and skips recursing into that symlink rather than recursing until the limit is hit.

Does not fix more complicated symlink loops, as in sys.

@msftclas

Hi A (@ealexjordan), I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!


It looks like you're a Microsoft contributor (Alex Jordan). If you're full-time, we DON'T require a Contribution License Agreement. If you are a vendor, please DO sign the electronic Contribution License Agreement. It will take 2 minutes and there's no faxing! https://cla.microsoft.com.

TTYL, MSBOT;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we have the same problem in windows, for example:

 Directory of e:\psTest\test\foo

09/13/2016  04:26 PM    <DIR>          .
09/13/2016  04:26 PM    <DIR>          ..
09/13/2016  04:26 PM    <SYMLINKD>     foo [e:\psTest\test]

Can you please solve it for both win and unix?

@daxian-dbw

Copy link
Copy Markdown
Member

Test GetFullyQualifiedNameTest failed. It's not related to the PR. Andy Jordan (@andschwa) can you please take a look to see if this is just a flaky test?

[----------] 1 test from GetFullyQualifiedNameTest
[ RUN      ] GetFullyQualifiedNameTest.ValidateLinuxGetFullyQualifiedDomainName
/Users/travis/build/PowerShell/PowerShell/src/libpsl-native/test/test-getfullyqualifiedname.cpp:37: Failure
Value of: actual
Actual: NULL
Expected: info->ai_canonname
Which is: "traviss-mac-1117.local"

@daxian-dbw

Copy link
Copy Markdown
Member

A (@ealexjordan) could you please also add test along with the code change?

Detects when symlink recursively links back to parent directory and skips recursing into that symlink rather than recursing until the limit is hit. Does not fix more complicated symlink loops, as in sys

try
{
readSymLink = InternalSymbolicLinkLinkCodeMethods.GetTarget(obj).First().ToString();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like InternalSymbolicLinkLinkCodeMethods.GetTarget will take care of both win and unix platforms -- it calls into Platform.NonWindowsInternalGetTarget for non-windows platforms. So you don't need to have if (win) .. else ... anymore.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

GetTarget(obj).First().ToString();

InternalSymbolicLinkLinkCodeMethods.GetTarget returns IEnumerable<string>, so the ToString() is unnecessary.

}

//if the read symlink is not null or empty, and what it points to is the parent directory then it is recursive
if (!String.IsNullOrEmpty(readSymLink) && path.Parent.FullName.Contains(readSymLink))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

path.Parent.FullName.Contains(readSymLink))

I think path.FullName.StartsWith is a better check. Considering the following case:

path = /tmp/home/pi/foo
path.Parent.FullName = /tmp/home/pi
readSymLink = /home/pi

in this case, path is pointing to its parent recursively, but this method would return true.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And also, please use the StartsWith overload with the culture comparison parameter.

//Platform is Linux or Windows
else
{
readSymLink = Platform.NonWindowsInternalGetTarget(path.FullName);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

you catch all exception for win-plat above, but not for unix-plat. I think maybe it's better to not catch exception when calling into GetTarget. It shouldn't throw any exception normally, but if anything went wrong, it's probably better to let the user know.

@@ -1,3 +1,5 @@


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can you please remove the extra new lines?

{
recurse=false;
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the current directory is recursive, I think we should just skip this one and carry on with other directories. Return back without processing the rest of directories doesn't look right.

@daxian-dbw

Copy link
Copy Markdown
Member

A (@ealexjordan) could you please also add test along with the code change?

@daxian-dbw

Copy link
Copy Markdown
Member

Aditya Patwardhan (@adityapatwardhan), since you're more familiar with the symbolic link on windows, could you please also review in case I miss anything?

@daxian-dbw

Copy link
Copy Markdown
Member

A (@ealexjordan) Thanks for adding the corresponding test changes. We are currently still waiting on you for 2 things:

  1. Resolve the CI failures.
  2. Address the review comments.

@DHowett

Copy link
Copy Markdown

I think this is a good start, but I'm worried it sets us off on the wrong foot for eventually supporting extended symlink chains (the likes of which don't link to directories directly under our own prefix).

@daxian-dbw

Copy link
Copy Markdown
Member

Thanks Dustin L. Howett (@DHowett) for your feedback. Can you plase elaborate it a bit more?
One more issue with the current fix is that it takes care of the direct recursive symbolic chains, but not indirect ones.

@DHowett

Copy link
Copy Markdown

Dongbo Wang (@daxian-dbw) That's what I mean; I don't think the solution in this pull request is scalable for indirect recursive symbolic link chains. The solution to that one would also fix direct chains, however.


It "Should be able to detect a recursive symlink" {

New-Item -ItemType directory -Name $parentfolder -Path $tmpDirectory

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this doesn't seem to be defined

}
}

Describe "New-Item" -Tags "CI" {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

"CI" [](start = 26, length = 4)

this also needs the tag "RequireAdminOnWindows" since you can't create symlinks on windows unless you're elevated

@daxian-dbw

Copy link
Copy Markdown
Member

I'm closing this PR because the solution in this pull request fixes the direct symbolic chain, but not the indirect recursive symbolic link chains.

@iSazonov Ilya (iSazonov) removed the Review - Needed The PR is being reviewed label Apr 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants