Skip to content

Implement a name encoding scheme using punycode. **PREVIEW** #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: edge
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
convert to hex digits for substitutions of unsafe characters
  • Loading branch information
albu-diku committed Jul 19, 2024
commit 96a9c304f518b13a8efc2ddbb17b5054de63e1fe
23 changes: 15 additions & 8 deletions mig/shared/sanitize.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# -- END_HEADER ---
#

import base64
import codecs
import os
import string
Expand All @@ -38,17 +39,22 @@
INDICATOR_CH = ':'
INVALID_INSERTION_POINT = -2
MARKER = INDICATOR_CH * 2
MARKER_HEXDIGIT_WIDTH = 2
UNSAFE_CHARS = sorted(list(set(string.printable) - set(username_charset)))
UNSAFE_CHARS_ORD = list(ord(c) for c in UNSAFE_CHARS)
UNSAFE_CHARS_NAMES = list(str(o).zfill(3) for o in UNSAFE_CHARS_ORD)
UNSAFE_SUBSTIUTIONS = dict(zip(UNSAFE_CHARS_ORD, UNSAFE_CHARS_NAMES))
UNSAFE_CHARS_HEXDIGITS = None
UNSAFE_SUBSTIUTIONS = None
PY2 = sys.version_info[0] == 2

if PY2:
def _as_ascii_string(value): return value
else:
def _as_ascii_string(value): return codecs.decode(value, 'ascii')

def _as_hexdigit(ch):
return _as_ascii_string(base64.b16encode(bytes(ch, 'ascii')))

UNSAFE_CHARS_HEXDIGITS = list(_as_hexdigit(c) for c in UNSAFE_CHARS)
UNSAFE_SUBSTIUTIONS = dict(zip(UNSAFE_CHARS, UNSAFE_CHARS_HEXDIGITS))

# TODO
# - swap to converting the ord char value to hex as a way to save bytes
Expand Down Expand Up @@ -80,8 +86,7 @@ def safename_encode(value):
characters = list(punycoded)

for index, character in enumerate(characters):
character_ordinal = ord(character)
character_substitute = UNSAFE_SUBSTIUTIONS.get(character_ordinal, None)
character_substitute = UNSAFE_SUBSTIUTIONS.get(character, None)
if character_substitute is not None:
characters[index] = "%s%s" % (INDICATOR_CH, character_substitute)

Expand All @@ -100,7 +105,8 @@ def safename_decode(value):
value_to_decode = None
try:
idx = value.rindex(MARKER)
value_to_decode = ''.join((value[:idx + 1], '045', value[idx + 2:]))
character_substitute = _as_hexdigit('-')
value_to_decode = ''.join((value[:idx + 1], character_substitute, value[idx + 2:]))
except ValueError:
raise RuntimeError()

Expand All @@ -114,8 +120,9 @@ def safename_decode(value):
if chunk == '':
index += 1
continue
character_substitute = chr(int(chunk[0:3]))
chunked[index] = "%s%s" % (character_substitute, chunk[3:])
hexdigit = chunk[0:MARKER_HEXDIGIT_WIDTH]
character_substitute = _as_ascii_string(base64.b16decode(hexdigit))
chunked[index] = chunked[index].replace(hexdigit, character_substitute, 1)
index += 1

try:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_mig_shared_sanitize.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_encode_exotic(self):
encoded = safename_encode(DUMMY_EXOTIC)

self.assertEqual(
encoded, "UniCode123@:036::lna3a4dm6e3ftgua80ewlwka88boszo7i7iv930g")
encoded, "UniCode123@:24::lna3a4dm6e3ftgua80ewlwka88boszo7i7iv930g")

def test_decode_basic(self):
safename_decode("")
Expand All @@ -39,7 +39,7 @@ def test_decode_ascii(self):
self.assertEqual(decoded, DUMMY_ASCII)

def test_decode_exotic(self):
decoded = safename_decode("UniCode123@:036::lna3a4dm6e3ftgua80ewlwka88boszo7i7iv930g")
decoded = safename_decode("UniCode123@:24::lna3a4dm6e3ftgua80ewlwka88boszo7i7iv930g")

self.assertEqual(decoded, DUMMY_EXOTIC)

Expand Down