Include resolution in Pug 3.x remains unrestricted relative to template directory
0 reputation · 15 Dec 2024, 10:54 UTC
0 reputation · 15 Dec 2024, 10:54 UTC
Pug 3.x resolves include and extends relative to the template file’s directory by default. The goal is to understand the compatibility boundary of that resolution when templates are placed in a publicly served directory.
The documented behavior was retained for flexibility and was not revised to enforce a stricter basedir scope. This leaves an unresolved decision about whether relative resolution should be allowed to reach outside the template folder and what the safe default is for production use.
Is the current default considered intentional and stable across Pug 3.x? Does an explicit basedir change resolution semantics in a backward-compatible way? What configuration boundary prevents a public template from including private files?
32775 reputation · 15 Dec 2024, 12:44 UTC
In Pug 3.x, the default behavior where include and extends resolve relative to the current template's directory is intentional and stable. This design choice maintains full backward compatibility with Pug 2.x and provides flexibility for complex project structures that may not fit a strictly flat directory hierarchy.
By default, Pug does not enforce a 'sandbox' or a restricted root boundary. If a template is located in /public/views/, it can technically use include ../../private/config.pug to access files outside the public-facing folder, provided the Node.js process has the necessary file-system read permissions.
basedirThe basedir option is the primary mechanism for changing resolution semantics. When basedir is defined in the render options:
basedir path.Because Pug itself does not provide a configuration-level boundary to prevent reaching outside a directory, security must be handled at the application or infrastructure level. To prevent a publicly-served template from including private files, consider these strategies:
/views folder outside of /public).basedir: Set a strict basedir and ensure all includes start from that known root.../) before passing it to the Pug engine.Is your application passing user-provided strings directly into the render function? If so, manual path sanitization is your only definitive defense.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.