Skip to content

fix(security): replace path traversal blocklist with canonical containment validation (CWE-22) - #589

Open
aryan4859 wants to merge 2 commits into
GZTimeWalker:developfrom
aryan4859:develop
Open

fix(security): replace path traversal blocklist with canonical containment validation (CWE-22)#589
aryan4859 wants to merge 2 commits into
GZTimeWalker:developfrom
aryan4859:develop

Conversation

@aryan4859

@aryan4859 aryan4859 commented Aug 3, 2026

Copy link
Copy Markdown

Summary

Fixes two path traversal issues found during security review, both stemming from
the same root cause: validating file paths against a substring blocklist or no
check at all, instead of canonicalizing and checking containment within an
intended base directory.

Changes

1. ComputeFileHashAsync

Previously rejected paths only if they contained the literal substring "..".
This is bypassed by absolute paths, alternate path separators, and extended-length
path prefixes - none of which contain ".." - allowing arbitrary file reads if
filePath is ever influenced by untrusted input.

Fix: Canonicalize with Path.GetFullPath() and verify the resolved path is a
descendant of an explicit allowedBaseDirectory parameter before opening the file.

2. ImportFileAsync

No path validation at all - filePath was built directly from Path.Combine(context.WorkDir, "files", hash)
with no check that hash couldn't escape that directory via traversal sequences
or a rooted/absolute value.

Fix: Same canonicalization pattern resolve the full path and confirm it
stays within context.WorkDir/files before checking existence or opening the file.

Why this approach

Blocklisting specific substrings ("..") doesn't validate the resolved location
of a path it only blocks one known technique. Path.GetFullPath() +
StartsWith(base + separator) validates the actual outcome regardless of how the
traversal is encoded.

Testing

  • Added/updated regression tests confirming absolute paths, ../ sequences,
    and alternate separators are rejected by both methods
  • Confirmed valid in-directory paths/hashes still succeed
  • Manual verification against PoC payloads from the associated security advisory

Related

Addresses path traversal reported in here (CWE-22 / CWE-23).

…locklisting ".."

The previous check only rejected paths containing the literal substring
"..", which is bypassed by absolute paths, alternate path separators,
and extended-length path prefixes — none of which contain "..".

Canonicalize the resolved path with Path.GetFullPath() and verify it
is a descendant of the intended base directory before opening the
file, rather than pattern-matching the raw input string.

Fixes path traversal / arbitrary file read (CWE-22, CWE-23) in
ComputeFileHashAsync when filePath originates from untrusted input.
…ngComparison.Ordinal))

{
    throw new ArgumentException("Invalid file path");
}
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

Successfully merging this pull request may close these issues.

1 participant