r/AskProgramming • • 8d ago

C# Per-tenant filesystem root confinement (traversal + symlinks), cross-platform

i am currently relying on string check to keep customers in own folders. Is it completely pointless on window? Or Force OS to look customers in isolation instead of filtering file names in c# & python

0 Upvotes

5 comments sorted by

2

u/snap-dev 8d ago

string checks on paths are worth keeping as a first pass but i wouldn't trust them alone, windows has too many ways to spell the same path (8.3 short names, \?\ prefixes, junctions, alternate data streams, case). what holds up better: resolve the full real path after following links (GetFullPath plus a reparse point check in c#, os.path.realpath in python), confirm it still sits under the tenant root, and do that right before you open the file, not earlier, or a symlink swap in between gets past you. if you can, let the os carry the real weight too. on windows run each tenant's file work under its own low privilege account with ACLs only on its folder, on linux a separate user or container. then a bug in your checks is an annoyance instead of a breach. can tenants create links themselves or only upload files?

2

u/Any-Appointment-814 8d ago

String checks on names are not pointless but they are the weakest layer, and on Windows they leak a lot. Case insensitivity, 8.3 short names (PROGRA~1), trailing dots and spaces, alternate data streams (file.txt:stream), UNC and \?\ prefixes, and junctions all get past naive checks.

What I'd do, in order of how much it buys you:

  1. Stop letting tenants send paths at all if you can. Give each file an id, keep a table of id to tenant to storage key, and build the real path on the server. Then there is nothing to traverse. This is the change that removes the whole class of bugs.
  2. If you must accept paths, resolve first and check after. Python: os.path.realpath on the joined path, then os.path.commonpath against the tenant root. C#: Path.GetFullPath, then resolve links (FileSystemInfo.ResolveLinkTarget) before comparing, and compare case insensitive on Windows. Still racy if someone can create symlinks between your check and your open.
  3. Close the race at the OS. On Linux, openat2 with RESOLVE_BENEATH does the confinement for you. On Windows, open the handle and then check its final path (GetFinalPathNameByHandle) before you read or write.
  4. Let the OS enforce it too. Run each tenant's work as its own user or container with ACLs only on its own folder, so a bug in your checks still can't reach another tenant.

Short version: do 1 if you can, otherwise 2 plus 3, and add 4 as the backstop.

0

u/Foreign_Career_454 8d ago

system does have background AI agents working inside local folders which is where symlinks comes & yes 1 to 4 ranking makes it simple.

1

u/Any-Appointment-814 8d ago

Makes sense. With agents writing in those folders, check the resolved real path is still under the tenant root on every open, not once up front. On Linux, openat2 with RESOLVE_BENEATH does that for you in the kernel. Glad the ranking helps.

1

u/YMK1234 8d ago

Have you heard of this thing called chroot and/or containers?