r/AskProgramming • u/Foreign_Career_454 • 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
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:
Short version: do 1 if you can, otherwise 2 plus 3, and add 4 as the backstop.
1
u/YMK1234 8d ago
Have you heard of this thing called chroot and/or containers?