r/csharp • • 4d ago

Discussion Windows & Linux file access

Muti tenant self-hosted where each tenant has workspace directory at app level we resolve via

Path.GetFullPath(Path.Combine(tenantRoot, userPath))`, then require `resolved == tenantRoot || resolved.StartsWith(tenantRoot + Path.DirectorySeparatorChar)

os.path.realpath is cannoned for unresolved realpath. Wanted to know is `GetFullPath along with prefix-with-separator is enough? Does realpath needs O_NOFOLLOW`-style flags?

We volume-mount workspaces into containers is that ok or there should be OS level or app level canon? How to regress test in CI is there any fuzz list?

2 Upvotes

3 comments sorted by

2

u/binarycow 3d ago

Instead of your prefix check, look at Path.GetRelativePath

1

u/Comfortable-Row7105 3d ago

I'd trust Path.GetFullPath + your prefix check but still sleep better with extra symlink verification just to be safe.

1

u/mertsabinov 2d ago

the GetFullPath + prefix check is fine for .. stuff but it's just string manipulation, it doesn't look at the disk at all. so symlinks get through. if a tenant can get a symlink into their workspace (extracting a zip, git clone etc) pointing somewhere else, the check passes and the OS happily follows it

on linux openat2 with RESOLVE_BENEATH handles this properly at the kernel level but you need p/invoke for it. otherwise just reject symlinks when files come in

also use OrdinalIgnoreCase for the compare on windows since paths aren't case sensitive there