Skip to content

Commit

Permalink
avoid ambiguous username-servername boundary
Browse files Browse the repository at this point in the history
two hyphens prevents collisions,
which were possible if usernames could contain hyphens and servernames could start with an escape sequence,
since our separator was the escape character
  • Loading branch information
minrk committed Jul 2, 2020
1 parent a6c3ea8 commit b7f55ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
10 changes: 7 additions & 3 deletions kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,8 +1315,12 @@ def _expand_user_properties(self, template):

# Set servername based on whether named-server initialised
if self.name:
servername = '-{}'.format(self.name)
safe_servername = '-{}'.format(escapism.escape(self.name, safe=safe_chars, escape_char='-').lower())
# use two -- to ensure no collision possibilities
# are created by an ambiguous boundary between username and
# servername.
# -- cannot occur in a string where - is the escape char.
servername = '--{}'.format(self.name)
safe_servername = '--{}'.format(escapism.escape(self.name, safe=safe_chars, escape_char='-').lower())
else:
servername = ''
safe_servername = ''
Expand All @@ -1329,7 +1333,7 @@ def _expand_user_properties(self, template):
unescaped_username=self.user.name,
legacy_escape_username=legacy_escaped_username,
servername=safe_servername,
unescaped_servername=servername
unescaped_servername=servername,
)

def _expand_all(self, src):
Expand Down
30 changes: 23 additions & 7 deletions tests/test_spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def test_pod_name_named_servers():

spawner = KubeSpawner(config=c, user=user, orm_spawner=orm_spawner, _mock=True)

assert spawner.pod_name == "jupyter-user-server"
assert spawner.pod_name == "jupyter-user--server"


def test_pod_name_escaping():
Expand All @@ -296,18 +296,34 @@ def test_pod_name_escaping():

spawner = KubeSpawner(config=c, user=user, orm_spawner=orm_spawner, _mock=True)

assert spawner.pod_name == "jupyter-some-5fuser-test-2dserver-21"
assert spawner.pod_name == "jupyter-some-5fuser--test-2dserver-21"


def test_pod_name_custom_template():
c = Config()
c.JupyterHub.allow_named_servers = False

user = Config()
user = MockUser()
user.name = "some_user"

pod_name_template = "prefix-{username}-suffix"

spawner = KubeSpawner(config=c, user=user, pod_name_template=pod_name_template, _mock=True)
spawner = KubeSpawner(user=user, pod_name_template=pod_name_template, _mock=True)

assert spawner.pod_name == "prefix-some-5fuser-suffix"


def test_pod_name_collision():
user1 = MockUser()
user1.name = "user-has-dash"

orm_spawner1 = Spawner()
orm_spawner1.name = ""

user2 = MockUser()
user2.name = "user-has"
orm_spawner2 = Spawner()
orm_spawner2.name = "2ddash"

spawner = KubeSpawner(user=user1, orm_spawner=orm_spawner1, _mock=True)
assert spawner.pod_name == "jupyter-user-2dhas-2ddash"
named_spawner = KubeSpawner(user=user2, orm_spawner=orm_spawner2, _mock=True)
assert named_spawner.pod_name == "jupyter-user-2dhas--2ddash"
assert spawner.pod_name != named_spawner.pod_name

0 comments on commit b7f55ea

Please sign in to comment.