Skip to content

restrict volume names - #16140

Closed
xlgao-zju wants to merge 1 commit into
moby:masterfrom
ZJU-SEL:fix-16132
Closed

xlgao-zju wants to merge 1 commit into
moby:masterfrom
ZJU-SEL:fix-16132

Conversation

@xlgao-zju

Copy link
Copy Markdown
Contributor

Closes #16132

Agree if [a-zA-Z0-9][a-zA-Z0-9_.-]+ is what we want 

at first.

And if the definitive Regex gets changed, I'll update this pr.
@thaJeztah

@duglin

duglin commented Sep 8, 2015

Copy link
Copy Markdown
Contributor

please merge your commits.

please put Closes: #16132 in your first comment so that git closes the issue when this PR is merged.

ping @cpuguy83 since this is about volumes.

@cpuguy83

cpuguy83 commented Sep 9, 2015

Copy link
Copy Markdown
Member

I'm so bad with regexes I'm not going to comment much on that... other than +1 for a regex since it's weird to support relative paths.

@xlgao-zju
xlgao-zju force-pushed the fix-16132 branch 5 times, most recently from d70bb29 to 2433562 Compare September 9, 2015 03:37
@thaJeztah

Copy link
Copy Markdown
Member

I'm so bad with regexes I'm not going to comment much on that... other than +1 for a regex since it's weird to support relative paths.

There's an online tester here; https://regex-golang.appspot.com/assets/html/index.html if you want to check what's accepted and not.

To explain this Regex, it currently accepts volume names that;

  • Start with an alphanumeric character (a-z, A-Z, 0-9), followed by
  • At least one alphanumeric character (a-z, A-Z, 0-9), dot, underscore or dash

This means that single character volume names (e.g. A) are not accepted. I was thinking if we should, but I think it's better not to, to prevent docker run -v C:\Users\foo\ leading to a named volume C.

There's one concern I have; we accept both uppercase and lowercase characters. Given that the Daemon keeps reference to volumes by creating a directory in /var/lib/docker/volumes/<volumename>, this will cause problems if the daemon is running on a case-insensitive filesystem (read: docker daemon on Windows). On such system, MyVolume and myvolume will be the same volume, whereas on Linux, they will be different volumes.

I don't think the Windows daemon already supports this feature, but it may be added in some future, so we may need to think of that now.

TL;DR; if we want to be "safe" we should only support lowercased names, and change the regex to

^[a-z0-9][a-z0-9_.-]+$

@thaJeztah

Copy link
Copy Markdown
Member

ping @jhowardmsft @swernli @ahmetalpbalkan for input on the "Windows" side of this ^^

@lowenna

lowenna commented Sep 9, 2015

Copy link
Copy Markdown
Member

@jstarks I'm actually working on volume support in Docker for Windows right now. Obviously this change is in a _unix file right now and doesn't directly impact Windows.

On Windows we need to support any/all of (in case insensitive)
c:\foo
c:
\foo

So docker run -v c:\foo:d:\bar:wr would be an option. Obviously that is almost impossible to deterministically parse due to the use of colon being both the separator of the parameter and used as part of the path. The prototype I'm working on addresses this through the use of square brackets where a path with a colon is required. [c:\foo]:\bar:wr.

@thaJeztah

Copy link
Copy Markdown
Member

@jhowardmsft I think if we require a named volume to have a minimum length of two characters, that the brackets won't be necessary; C: is a single character, so won't be interpreted as a "named volume". Some smart regex should be able to match the cases you mentioned;

I just tried it a bit, and came with this quick & dirty regex (not including unc-paths yet);

^((?P<volumename>[a-zA-Z0-9][a-zA-Z0-9_.-]+)|(?P<path1>[a-zA-Z]:(\\[^\:\\]+)*\\?))(:(?P<path2>[a-zA-Z]:(\\[^\:\\]+)*\\?)(:(?P<readwrite>ro|rw))?(,(?P<relabel>z|Z))?)?$

You can play around with that on https://regex-golang.appspot.com/assets/html/index.html. This gist breaks the regex down a bit: https://gist.github.com/thaJeztah/6185659e4978789fb2b2, and includes some example values for testing.

@jhowardmsft what are your thoughts on "case sensitivity" for named volumes on Windows? Will Window be able to have two volumes, one named Foobar and one named foobar? or should they be the same volume?

@xlgao-zju

Copy link
Copy Markdown
Contributor Author

uh, I think I should wait for your definitive Regex...

@thaJeztah

Copy link
Copy Markdown
Member

Lets ignore the Regex for Windows for now; the long Regex above is just a POC, not relevant to this PR.

For now we need to define "accepted" volume-names for Linux, but keep in mind that those names should not
cause problems once we introduce named volumes on Windows.

My thoughts are:

  1. Minimum length 2 characters (to disallow creating a volume named c, which cannot be used on Windows as c: is a drive-letter)
  2. Must start with an alphanumeric character (no ., -; we don't want volumes named --foo or .hidden)
  3. Only allow alphanumeric characters, _, . and -. No whitespace.
  4. Either only allow lowercase characters,

    OR
  5. Treat volume-names case-insensitive and normalize them (both --name MyVolume and --name mYvolUme would create a volume named myvolume)

Both 4. and 5. make sure that names behave the same on Windows and Linux. Out of those two, my preference goes to 4. as it's more explicit, and easier to "loosen up" in a later stage (if possible). Also changing upper to lowercase can be non-trivial if Unicode characters are accepted in the future.

ping @cpuguy83 @jhowardmsft @calavera can we agree on the above? which out of 4. and 5. would have your preference?

@cpuguy83

Copy link
Copy Markdown
Member

Also see $ sudo docker run -it --volume-driver sshfs --volume [email protected]#/data:/data busybox sh
From https://github.com/vieux/docker-volume-sshfs

@lowenna

lowenna commented Sep 10, 2015

Copy link
Copy Markdown
Member

@thaJeztah Re 4 or 5 above - I don't think it matters at all from a Windows perspective. This is validation being done in _unix.go files and is therefore not relevant at all for Windows. The Windows equivalent of parsing the (bind) mount path in platform semantics will be quite different.

@calavera

Copy link
Copy Markdown
Contributor

The regexp is fine, I would have used ^\w[\w_.-]+$, but it basically means the same.

There are no tests though 😔

@thaJeztah

Copy link
Copy Markdown
Member

@calavera I decided to not use \w here, because a-z was more explicit (and IIRC, used in other places), I'm good changing it though.

@jhowardmsft Yes, this is Linux only now; but I wanted to be sure we could give the same user experience on Windows (ie, being able to use the same name on Windows as on Linux) - not talking about bind-mounts here.

@cpuguy83 good one; I think we need to think of we still want to support that, or if --opts should be used in stead. @vieux any thoughts on that?

@jessfraz jessfraz changed the title Fix 16132 relative host-path volumes Sep 11, 2015
@jessfraz

Copy link
Copy Markdown
Contributor

can you make your commit message actually describe what is changing

@xlgao-zju

Copy link
Copy Markdown
Contributor Author

Ok, I'll change it tomorrow.
@jfrazelle

@xlgao-zju

Copy link
Copy Markdown
Contributor Author

So...what's the definitive Regex?
@thaJeztah

@thaJeztah

Copy link
Copy Markdown
Member

I think the current one is good, but would love to hear from @cpuguy83 and @vieux about #16140 (comment)

@xlgao-zju
xlgao-zju force-pushed the fix-16132 branch 2 times, most recently from 0536c0f to 725871f Compare September 15, 2015 08:32
@cpuguy83

cpuguy83 commented Oct 8, 2015

Copy link
Copy Markdown
Member

@duglin What does a relative path even mean in this regard? Most docker clients are remote clients. This is even harrier when you throw swarm into the mix.

@xlgao-zju

Copy link
Copy Markdown
Contributor Author

@duglin I think we can't use filepath.IsAbs. Since the name of volume will always get false with filepath.IsAbs.
Relative code:
https://github.com/docker/docker/blob/master/daemon/volumes_unix.go#L401

@thaJeztah thaJeztah added this to the 1.9.0 milestone Oct 14, 2015
Comment thread daemon/volumes_unix.go Outdated

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.

. and .. are problems as well... and I suspect anything that looks like a path would cause some issues.

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 seems to block out the bad stuff: if regexp.MatchString("(^.+[^0-9A-Za-z_]+$)|(/)", name)

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.

it seems not good enough...
. can't be blocked...

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.

ah crap, I must have changed without testing ....

@crosbymichael

Copy link
Copy Markdown
Contributor

Sorry for coming in late but did we ever support relative volumes? I didn't think so and we always required the source to be an ABS path.

@cpuguy83

Copy link
Copy Markdown
Member

@crosbymichael No, and the title is a misnomer.
This is about dealing (or has become about) with bad values for volume names.

@crosbymichael

Copy link
Copy Markdown
Contributor

Ok, i think anyone with write access can change the titles of PRs so that they are not confusing and misleading. I would suggest doing so in cases like this. ;)

Anyways, i think you can treat anything with a leading / as a volume path, everything else should follow the container name regex.

@thaJeztah

Copy link
Copy Markdown
Member

@crosbymichael that was my original proposal, however this would break #16140 (comment)

And @calavera wants users to give more freedom in naming their volumes, which could in turn lead to things we may not want to support (or cannot guarantee will work); #16140 (comment)

@cpuguy83 cpuguy83 changed the title relative host-path volumes restrict volume names Oct 15, 2015
@xlgao-zju
xlgao-zju force-pushed the fix-16132 branch 2 times, most recently from c6c93f7 to dad8d7e Compare October 16, 2015 08:44
@cpuguy83

Copy link
Copy Markdown
Member

I'm going to say +1 on using the container name regexp. If it breaks people doing silly things then fine.
A name should be a name, not encoded options.
See #16132 (comment) from early September.

But If not, we need to disallow anything that looks like a file path since this for sure messes up the local volume driver, and probably would not do good things to most other drivers. That means anything that has a / in it. Probably anything that has . as the prefix since that's probably an accident for people looking for a relative path.. and for certain ., .., etc. since that is just out right broken in both the client and the local volume driver.

ping @calavera who had reservations about restricting this much.

Also @xlgao-zju thanks for putting up with going back and forth on this.
LMK if you'd like for someone to carry this or not.

@calavera

Copy link
Copy Markdown
Contributor

I'm going to say +1 on using the container name regexp. If it breaks people doing silly things then fine.
A name should be a name, not encoded options.

using more than a-z0-9 characters is not doing silly things. We cannot restrict characters that the operating system accepts as valid for directory names only because we don't know how to differentiate absolute paths from relative paths.

@crosbymichael

Copy link
Copy Markdown
Contributor

@tiborvass

Copy link
Copy Markdown
Contributor

@calavera

Copy link
Copy Markdown
Contributor

@crosbymichael I'm fully aware. But what started as "we should not allow what looks like relative paths as volume names" has turned into "if we let people use emojis as names we all are going to die" 😸 :trollface:

@tiborvass

Copy link
Copy Markdown
Contributor

Closing in favor of #17213

Thanks for your contribution @xlgao-zju ! Really appreciate it!

@tiborvass tiborvass closed this Oct 20, 2015
@xlgao-zju
xlgao-zju deleted the fix-16132 branch October 21, 2015 02:11
@xlgao-zju

Copy link
Copy Markdown
Contributor Author

That's ok. I will carry on!

@thaJeztah

Copy link
Copy Markdown
Member

Thanks @xlgao-zju! Sorry for all the bike shedding here ❤️

@xlgao-zju

Copy link
Copy Markdown
Contributor Author

@thaJeztah That's all right. Working with you guys is so happy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Volumes: relative host-path creates named volumes