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

Add initial support for resolving relative paths #343

Merged
merged 11 commits into from
Jun 24, 2019

Conversation

liamrharwood
Copy link
Contributor

This PR adds support for resolving resource locations, such when using relative paths in a file system.

  • This adds a new LocationResolver interface, which resolves a string to the absolute location of a resource (i.e. resolves a relative path to an absolute path).
  • Each ResourceLocator can have an optional LocationResolver. I decided to have this live in the ResourceLocator since the behavior should be fairly linked.
  • This also adds the resolveResourceLocation() method to JinjavaInterpreter which uses the LocationResolver of the current ResourceLocator to resolve the location of a resource.
  • I modified the include, import, extends, and from [..] import tags to resolve the location before retrieving a resource and before adding it to the currentPathStack (this is to make sure all paths in the stack are absolute).
  • I added a basic RelativePathResolver which is a LocationResolver that resolves relative paths beginning with ./ based on the currentPathStack and the current_path context value. I also added tests for each of the tags mentioned above to test this behavior using the RelativePathResolver. (I wasn't sure if this class should live in Jinjava or not, but felt it was a good basic example of a LocationResolver.)


Path templatePath = Paths.get(parentPath);
Path folderPath = templatePath.getParent() != null ? templatePath.getParent() : Paths.get("");
if (folderPath != null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think that folderPath would ever be null, but the build was failing because Travis CI was saying there was a possible null dereference. 🤷‍♂

Copy link
Collaborator

@mattcoley mattcoley left a comment

Choose a reason for hiding this comment

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

Some nits but I think this looks good.


@Override
public String resolve(String path, JinjavaInterpreter interpreter) {
if (path.startsWith("./")) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we allow ../ straight up without having to do ./../ since that is a bit redundant?

}
return path;
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: extra line

default Optional<LocationResolver> getLocationResolver() {
return Optional.empty();
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here 🙃

Copy link
Contributor

@boulter boulter left a comment

Choose a reason for hiding this comment

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

The only thing I worry about here is the possibility that it could allow an arbitrary directory traversal exploit. I think that's only possible when using the FileLocator, but could you verify?

@liamrharwood
Copy link
Contributor Author

liamrharwood commented Jun 21, 2019

That should only be possible using FileLocator, which incidentally already had its own relative path resolution logic. If necessary, we could update the FileLocator to use a LocationResolver and add logic which explicitly blocks such an exploit, though this PR is not changing FileLocator behavior.

@liamrharwood liamrharwood merged commit 1cf7126 into master Jun 24, 2019
@liamrharwood liamrharwood deleted the support-relative-paths branch June 24, 2019 13:54
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.

5 participants