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

Regression of file loading outside of template location #29

Closed
codemonstur opened this issue Aug 13, 2024 · 4 comments
Closed

Regression of file loading outside of template location #29

codemonstur opened this issue Aug 13, 2024 · 4 comments

Comments

@codemonstur
Copy link

The bug described in issue 3 has come back. Every version from 2.0.5 onwards now fails with the 'The template [../webinc/skeleton.pug] could not be opened. Maybe outside template path.' error.

@chbloemer
Copy link
Contributor

Pug4J needs a Base path, so that it can determine the parent path. By default the basePath is empty.
To configure a basePath. You need to use the FileTemplateLoader or ClassPathTemplateLoader and configure setBase("").
Example for template location: /root/dir/base/path/template.pug

FileTemplateLoader fileLoader = new FileTemplateLoader("/root/dir/")
fileLoader.setBase("base/path")

PugConfiguration config = new PugConfiguration();
config.setTemplateLoader(fileLoader);
PugTemplate template = config.getTemplate("template");

Map<String, Object> model = new HashMap<String, Object>();

config.renderTemplate(template, model);

Pug is now allowed to read all files below /root/dir/ but not in / and /root
So this is more a security feature than a bug ;-) I will update the documentation.

chbloemer added a commit that referenced this issue Dec 16, 2024
chbloemer added a commit that referenced this issue Dec 16, 2024
@codemonstur
Copy link
Author

You can't set a base path if you use Pug4J.render(path, model);. My expectation when using that call would be that there are no restrictions on which files you can point to.

There are many confusing things about the API (The code has a templateLoaderPath and a basePath and wants to load a template with a name that appears to just be a filename but without an extension?? You ask a config object to get and render templates? The rootpath must be absolute?), but the real issue is that your code example appears to just not work at all. With your code I now get a NoSuchFileException for a path that doesn't include the basepath. The code appears to ignore the basepath when looking for files.

Code is here. This is the error:

Exception in thread "main" java.nio.file.NoSuchFileException: C:\Projects\public\pug4j-bug\src\main\skeleton.pug
	at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
	at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
	at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
	at java.base/sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(WindowsFileAttributeViews.java:53)
	at java.base/sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(WindowsFileAttributeViews.java:38)
	at java.base/sun.nio.fs.WindowsFileSystemProvider.readAttributes(WindowsFileSystemProvider.java:197)
	at java.base/java.nio.file.Files.readAttributes(Files.java:1848)
	at java.base/java.nio.file.Files.getLastModifiedTime(Files.java:2391)
	at de.neuland.pug4j.template.FileTemplateLoader.getLastModified(FileTemplateLoader.java:65)
	at de.neuland.pug4j.PugConfiguration.getTemplate(PugConfiguration.java:57)
	at bug.Main.loadTemplate(Main.java:45)
	at bug.Main.main(Main.java:22)

@chbloemer chbloemer reopened this Dec 27, 2024
@chbloemer
Copy link
Contributor

chbloemer commented Dec 27, 2024

Ok. I had the documentation wrong. What I found out, is you need to add a "/" in front of our "index" template name. This will select the base folder. Otherwise only the templateLoaderPath is used.
If you update your toBaseName method to the following code it should work:

    private static String toBaseName(final Path file) {
        final String filename = file.getFileName().toString();
        return "/"+filename;
    }

Then it prints out:

<html lang="en-US"><head><title>Skeleton</title><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"/><link rel="stylesheet" href="../webinc/style.css" inline="inline" compress="compress"/><script id="page" type="text/html" compress="compress"><p>I am an html page</p></script></head><body id="canvas"><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script><script src="../webinc/js/before.js" inline="inline" compress="compress"></script><script type="text/javascript" compress="compress">function page() {
    console.log('Page!');
}</script><script src="../webinc/js/after.js" inline="inline" compress="compress"></script></body></html>

See also https://pugjs.org/language/includes.html

@codemonstur
Copy link
Author

Yeah the forward slash works. The bug-repo is now out of date since the problem is solved so I'm going to set it as private.

Thanks.

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

No branches or pull requests

2 participants