Skip to content

Add a helper that relativizes and resolves child. #6522

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

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
27 changes: 27 additions & 0 deletions api/src/org/labkey/vfs/FileSystemLike.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,33 @@ static File toFile(FileLike f)
return p.toFile();
}

static FileLike resolveChildWithRelativeURI(File file)
{
URI fileURI = file.toURI();
FileLike allowedRoot = new FileSystemLike.Builder(fileURI).root();
Copy link
Contributor

Choose a reason for hiding this comment

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

allowedRoot==file???
If this were not static then allowed root would be the getRoot()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, thought that the root() was getting the root directory from a file. I will change that line to
FileLike allowedRoot = new FileSystemLike.Builder(file.getParentFile()).root();

Copy link
Contributor

Choose a reason for hiding this comment

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

If we already "trust" file we can simply use FileSystemLike.wrapFile(). This method helper is for when we don't trust the file yet, and want to verify it is within a known directory and then "adopt" it (e.g. turn a URI/File/Path into a FileLike).


// Converts an absolute URI to a relative one if it's within the base URI.
// If fileURI is outside rootURI, the result remains fileURI/unchanged.
// Examples:
// root "/a/b/c/", file "/x/y/z.txt" -> "/x/y/z.txt"
// root "/a/b/c/", file "/a/b/c/d.txt" -> "d.txt"
URI relativeURI = URIUtil.relativize(allowedRoot.toURI(), fileURI);

if (relativeURI == null)
{
throw new IllegalArgumentException("File '" + fileURI.getPath() + "' is outside the root '" + allowedRoot.toURI().getPath() + "'");
}

//explicitly check whether the given file path is within the allowed root directory
if (allowedRoot.getFileSystem().isDescendant(allowedRoot, fileURI))
{
return allowedRoot.resolveChild(relativeURI.getPath());
}
else
{
throw new IllegalArgumentException("File '" + relativeURI.getPath() + "' is not a descendant of '" + allowedRoot.getPath() + "'");
}
}

/* More efficient version of wrap when many files may be from the same directory */
static List<FileLike> wrapFiles(List<File> files)
Expand Down