Summary
A path traversal flaw in Mako's TemplateLookup.get_template() allows attackers to bypass template directory restrictions by supplying URIs that begin with double slashes (e.g., //../../../secret.txt).
When applications pass untrusted input directly into get_template(), any file readable by the process may be returned as rendered template content, exposing sensitive data.
Affected context
This vulnerability appears in Python applications that:
- Use Mako's TemplateLookup.get_template() to load templates by name or path
- Accept template identifiers or view names derived from user-controlled input (e.g., query parameters, route variables, headers)
- Rely on Mako's lookup directories or prefix-stripping behavior to constrain templates to specific folders
In such setups, URIs starting with // may be normalized incorrectly, allowing traversal out of the intended template root and exposing arbitrary readable files.
Exploit preconditions
- Attack surface: HTTP endpoints, RPC calls, CLI tools, or any interface where a user-controlled value is passed into
TemplateLookup.get_template(). - Required access: Ability to influence the template name/URI (directly or indirectly) used by the application.
- Trigger point: A call to
TemplateLookup.get_template(user_supplied_value)where the value can start with//and include traversal segments like../. - Why exploitation is possible: An inconsistency between two slash-stripping implementations causes paths beginning with
//to bypass intended directory normalization, enabling directory traversal outside configured template roots.
Why this happens
The underlying bug maps to CWE-22: Path Traversal, where user-supplied path components are not consistently normalized and constrained.
Mako employs multiple mechanisms to strip or normalize leading slashes in template URIs, but these mechanisms behave differently when the path begins with //.
This inconsistency means that certain crafted URIs can escape the expected template lookup root and be resolved against the filesystem more broadly.
Because the application treats the resolved path as safe template input, any readable file can be returned as rendered content, violating directory isolation assumptions.
Potential impact
- Exposure of configuration files containing secrets, API keys, or database credentials
- Disclosure of application source code, templates, or business logic
- Disclosure of environment files (e.g.,
.env) and other sensitive operational data - Assistance in chaining with other vulnerabilities (e.g., leaking credentials or internal endpoints used in further attacks)
- Leakage of user data stored in files accessible to the application process
General mitigation guidance
Effective mitigation focuses on enforcing strict path handling and reducing trust in template identifiers:
- Treat all template names derived from user input as untrusted and disallow direct passthrough into
TemplateLookup.get_template()without validation. - Apply allowlists of valid template identifiers (e.g., fixed route-to-template maps) instead of accepting arbitrary paths.
- Normalize and canonicalize paths before resolution, rejecting any template identifier containing traversal sequences (e.g.,
.., backslashes on Windows) or unexpected leading slashes. - Configure template directories such that they contain only non-sensitive templates; avoid colocating secrets or configuration files within or above template roots.
- Run the application with least-privilege filesystem access, ensuring the process user can read only what is strictly necessary for normal operation.
How teams detect issues like this
Detection efforts can focus on both code and runtime behavior:
- Static analysis / code search:
- Identify calls to
TemplateLookup.get_template()where the argument is derived from request data (URL parameters, form fields, JSON bodies, headers, or cookies). - Flag string concatenation or formatting that combines user input with path segments to build template names.
- Enforce internal policies that forbid direct user-controlled values as template identifiers without explicit validation.
- Code review signals:
- Routes or controllers that accept a template name, theme, or view parameter and pass it directly to Mako.
- Lack of explicit mapping from route names to fixed template paths.
- Runtime monitoring:
- Log requested template names and alert on patterns with
//,../, or unusual directory components. - Monitor access to unexpected filesystem paths by the application process, especially outside the configured template directories.
- Automated SAST/IAST or custom linters can be configured to detect these patterns and block risky changes in CI/CD pipelines.

