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

proposal: path: add IsLocal #63716

Closed
earthboundkid opened this issue Oct 24, 2023 · 9 comments
Closed

proposal: path: add IsLocal #63716

earthboundkid opened this issue Oct 24, 2023 · 9 comments
Labels
Milestone

Comments

@earthboundkid
Copy link
Contributor

It would be nice to have a platform independent version of filepath.IsLocal. It would be the same the existing filepath.isLocalUnix, but usable from Windows.

@gopherbot gopherbot added this to the Proposal milestone Oct 24, 2023
@apparentlymart
Copy link

I think it would be nice to have a straightforward way to do this in the standard library, just as proposed.


There are already some building blocks in stdlib that can do roughly this in a more roundabout way:

func LocalPath(s string) bool {
	return fs.ValidPath(path.Clean(s))
}

fs.ValidPath is essentially an extra-strict version of "IsLocal" which also prohibits ../ and ./ segments. path.Clean removes any ../ or ./ segments that are redundant, but leaves behind those that would traverse upwards, therefore making fs.ValidPath return false for those.

@seankhliao
Copy link
Member

The path package doesn't operate on the filesystem, this seems in conflict with the first point of filepath.IsLocal: "is within the subtree rooted at the directory in which path is evaluated"

@earthboundkid
Copy link
Contributor Author

The path package doesn't operate on the filesystem, this seems in conflict with the first point of filepath.IsLocal: "is within the subtree rooted at the directory in which path is evaluated"

It’s a purely lexical consideration though. It’s not looking at CWD to decide this. I think path is an appropriate package.

@earthboundkid
Copy link
Contributor Author

fs.ValidPath is essentially an extra-strict version of "IsLocal" which also prohibits ../ and ./ segments. path.Clean removes any ../ or ./ segments that are redundant, but leaves behind those that would traverse upwards, therefore making fs.ValidPath return false for those.

If you take a possible path and clean it then test that it’s local, it will pass fs.ValidPath.

@bcmills
Copy link
Contributor

bcmills commented Oct 25, 2023

(CC @neild)

@earthboundkid
Copy link
Contributor Author

If you take a possible path and clean it then test that it’s local, it will pass fs.ValidPath.

fs.ValidPath also checks for a trailing slash, so it's not totally equivalent after all.

@neild
Copy link
Contributor

neild commented Oct 25, 2023

fs.ValidPath also checks for a trailing slash, so it's not totally equivalent after all.

path.Clean removes trailing slashes, though.

@neild
Copy link
Contributor

neild commented Oct 25, 2023

I personally wouldn't object to path.Local, for consistency with filepath if nothing else. However, it's much less necessary; filepath.Local exists because it's difficult to answer the question "does this path refer to a file in the current directory" in a general sense. (Mostly because of Windows, which has a variety of surprising path constructs, although Plan 9 does have its # prefix.)

I believe path.IsLocal would be equivalent to:

func IsLocal(p string) bool {
  p = path.Clean()
  return !strings.HasPrefix(p, "/") && !strings.HasPrefix(p, "../")
}

fs.ValidPath(path.Clean(p)) will reject paths containing invalid UTF-8, while the above will not.

@earthboundkid
Copy link
Contributor Author

earthboundkid commented Feb 12, 2024

There is now filepath.Localize #57151, so I guess this issue is obsolete? You could do filepath.IsLocal(filepath.Localize(s)) instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants