00:00

#0402

Template Loader

Easy+50 XPA05:2021 Security MisconfigurationCWE-22
Path TraversalLFITemplate Engine

Scenario

A web application dynamically loads HTML templates by name. The template loader concatenates a base templates directory with the user-supplied template name.

No path sanitisation is applied. An attacker passes ../../config/database.yml as the template name.

The server reads and returns the database credentials file instead of a template.

Template loaders are a common vector for LFI. Exposing arbitrary file reads via template names can leak credentials, private keys, and application source code.

Your Tasks

  1. Fix loadTemplate so it rejects template names that escape the templates directory.
  2. Throw an error with the message 'Invalid template path' when traversal is detected.
  3. Return the resolved path string on success (no filesystem reads required for testing).

Examples

Example 1Blocked — traversal attempt

loadTemplate('/app/templates', '../../config/db.yml')
// → throws Error('Invalid template path')

Example 2Allowed — valid template name

loadTemplate('/app/templates', 'email/welcome.html')
// → returns '/app/templates/email/welcome.html'

Constraints

  • Only edit the function body — do not change the function signature.
  • Treat paths as pure strings; resolve '..' segments and verify the result starts with templatesDir.
  • The check must be applied before any file read operation.

Hint

References

solution.js
Ln 1, Col 1UTF-8JavaScript
Sandbox ready
0/0/0not run