Skip to content

refs. #80: validate manifest and fetch path entries - #88

Merged
acdha merged 6 commits into
LibraryOfCongress:masterfrom
runderwood:master
Mar 28, 2017
Merged

acdha merged 6 commits into
LibraryOfCongress:masterfrom
runderwood:master

Conversation

@runderwood

Copy link
Copy Markdown
Contributor

...and raise error if path appears unsafe.

@runderwood

Copy link
Copy Markdown
Contributor Author

@acdha Is this what you had in mind re: #80?

Comment thread bagit.py Outdated
\\?\c:\, D:\sys32\cmd.exe
"""
# Disallow dots, slashes, and tildes as first character
disallow = r'.\/~'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread bagit.py Outdated
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):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me. I'll move that direction.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@acdha

acdha commented Mar 22, 2017

Copy link
Copy Markdown
Member

@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.

@runderwood

Copy link
Copy Markdown
Contributor Author

This passes on Windows and Linux -- the test cases are system-dependent.

@runderwood

Copy link
Copy Markdown
Contributor Author

@acdha Let me know where you're at on this when you get the time.

Comment thread bagit.py Outdated
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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like I had a reason for including it, but I can't imagine what it was. Removed!

Comment thread test.py Outdated
bad_paths = (
'../../../secrets.json',
'~/.pgp/id_rsa',
'/dev/null'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Agreed on all points. Should have been there from the start.

@runderwood

Copy link
Copy Markdown
Contributor Author

@acdha Passes on Win. 10, fyi.

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.

2 participants