Secure development

Confining application file access to intended directories

Protected storage: document, database, key arranged to illustrate confining application file access to intended directories.

Confining application file access to intended directories connects a security requirement to the behaviour of a real feature. The relevant controls need to remain effective across custom code, integrations and the production configuration. Understanding application file access to intended directories helps establish which security property the feature needs and where that property is actually enforced.

What does this involve?

Input validation defines the structures, sizes and meanings an application expects to receive. It must happen before information influences a sensitive operation. Validation does not replace safe interpreter APIs, output encoding or permission checks, because those controls address different boundaries.

Path traversal happens when an application builds a file system path from user input and the input can make that path point outside the directory the application meant to use. Sequences such as ../, absolute paths, or symbolic links let an attacker read, write or include files such as configuration, credentials or source code (CWE-22).

When the application has to accept a name from the user, also restrict it to a simple allowlist of characters, for example letters, digits, - and _, with no path separators. This is an extra layer, not a replacement for the containment check below, because a character filter can't account for every way an operating system interprets a path (CWE-22).

Why it matters

Consider a realistic failure: a file operation accepts a path that resolves outside the intended directory after normalisation or link handling.

Untrusted input can change application behaviour, consume excessive resources or reach data and execution paths outside the intended feature. The practical result depends on where the value is used and which privileges are available there.

Implementation guidance should be applied to a real feature and its deployment. Framework defaults, custom handlers and integrated services all influence whether the intended protection remains effective.

How penetration testers check for it

Trace the relevant feature from its entry point to the data or operation it protects, including custom code and alternate paths.

  • Trace input from every relevant entry point to its use.
  • Check types, lengths, ranges and ambiguous representations.
  • Compare parsing across components and alternate request formats.
  • Use controlled invalid inputs to establish effects rather than assumptions.

How to fix it

Use the appropriate maintained mechanisms and verify the result in the actual production configuration.

  • If the user picks from a fixed set of files or templates, look the name up in an allowlist and use the stored path, not the input.
  • Check that the resolved path is inside the base directory. Compare path components, or compare strings only after making sure the base ends with a path separator, so /srv/uploads-old doesn't pass a check for /srv/uploads.
  • Use the path you checked. Don't rebuild it from the original input.
  • Prefer APIs that enforce the restriction while opening the file, such as Go's os.Root or Linux openat2 with RESOLVE_BENEATH (Linux 5.6 and later), which fails if any part of the resolution leaves the directory.
  • Reject unexpected structures before sensitive processing.
  • Apply resource bounds and predictable failure handling.

Blocking a few known attack strings is not a complete validation strategy. The intended data model and downstream interpretation determine which protections are needed.

Recheck the changed behaviour against the original failure conditions and the legitimate user journey. Keep enough evidence to show which release and configuration were verified, and include related entry points that depend on the same security decision.

Read about our methodology or contact pentested.com to discuss secure development and code review.

See your first test report this week.

Book a 30 minute demo. We will verify one of your targets with you and run a test against it, so you can judge the results on your own systems.

Book a demo