Repository navigation
refs. #80: validate manifest and fetch path entries - #88
Conversation
… raise error if the path appears unsafe
| \\?\c:\, D:\sys32\cmd.exe | ||
| """ | ||
| # Disallow dots, slashes, and tildes as first character | ||
| disallow = r'.\/~' |
There was a problem hiding this comment.
I think the approach of trying to blacklist known bad things is error prone – I would probably rewrite the regex as if re.match(r'^[.\\/~]', path) to make that clear but I'm wondering whether the better approach would be to use the Python os.path functions to fully expand the path and then confirm that it is still relative to the bag. I'd want to think carefully through the permutations but that would be something like calling expanduser and abspath on the input path, joining it with the bag base directory, and then testing that it still starts with the bag's base directory.
| return True | ||
| # Disallow drive letters at beginning of path, e.g c:, D: | ||
| drive_pat = re.compile(r'^[A-Za-z]:') | ||
| if drive_pat.match(path): |
There was a problem hiding this comment.
I would probably remove the separate compile step and just write this as re.match(…) since there module caches both calls to re.compile and the module-level match, sub, etc. functions. It's possible that in a large program this might exceed the re cache size and in that case we could see a modest win by making a module-level DRIVE_PATTERN_RE compiled constant and using that but I'd want to see a benchmark before not going with the simplest code
There was a problem hiding this comment.
Also, looking at the prose updates we've been adding to the spec – specifically https://github.com/loc-rdc/bagitspec/pull/1/commits/6673683d31473318b7d03f803b44336fc6f31319 – I think this would also want to have tests for COM1:, etc. Looking at https://msdn.microsoft.com/en-us/library/aa365247.aspx suggests we should also have some checks for the various reserved characters and the various tolerated path options on Windows (e.g. C:/bad/but/not/uncommon/practice)
There was a problem hiding this comment.
So focus on checking whether the resolved path is actually a child of the bag directory and avoid inspecting the path as a string altogether?
There was a problem hiding this comment.
I'm leaning in that direction - it seems safer than hoping we enumerate all of the different ways someone could try to sneak something through
There was a problem hiding this comment.
Makes sense to me. I'll move that direction.
There was a problem hiding this comment.
So, you're cool with these errors being system-specific? Such that an unsafe pathname on Windows doesn't necessarily look unsafe on a Unix-like OS?
There are pathnames that resolve outside the bagging directory on Windows, for example, which resolve as valid (but non-existent) relative paths on Linux machines.
|
@runderwood This is a great idea - and thanks for taking the initiative on it. Getting the tests in early is the most important part since I want to make sure the list of things which should be expected to fail is as wide as possible. |
|
This passes on Windows and Linux -- the test cases are system-dependent. |
|
@acdha Let me know where you're at on this when you get the time. |
| if os.path.expandvars(path) != path: | ||
| return True | ||
| real_path = os.path.realpath(os.path.join(self.path, path)) | ||
| real_path = os.path.normcase(os.path.normpath(real_path)) |
There was a problem hiding this comment.
This update looks great but I'm wondering about whether we should include the normcase call – that's documented as converting the path to lowercase on case-insensitive filesystems and I'm not sure there's a case where we want to change it.
There was a problem hiding this comment.
I feel like I had a reason for including it, but I can't imagine what it was. Removed!
| bad_paths = ( | ||
| '../../../secrets.json', | ||
| '~/.pgp/id_rsa', | ||
| '/dev/null' |
There was a problem hiding this comment.
The other one I'd add – and I think the .. tests should be cross-platform – would be something which starts relative and uses .. to escape like data/../../../../../etc/passwd.
There was a problem hiding this comment.
Yeah. Agreed on all points. Should have been there from the start.
…rmalize case in bad path checks
|
@acdha Passes on Win. 10, fyi. |
...and raise error if path appears unsafe.