-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
108 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
###################################################################### | ||
# | ||
# File: b2sdk/utils/escape.py | ||
# | ||
# Copyright 2023 Backblaze Inc. All Rights Reserved. | ||
# | ||
# License https://www.backblaze.com/using_b2_code.html | ||
# | ||
###################################################################### | ||
|
||
import re | ||
import shlex | ||
|
||
# skip newline, tab | ||
UNPRINTABLE_PATTERN = re.compile(r'[\x00-\x08\x0e-\x1f\x7f-\x9f]') | ||
|
||
|
||
def unprintable_to_hex(s): | ||
""" | ||
Replace unprintable chars in string with a hex representation. | ||
:param string: an arbitrary string, possibly with unprintable characters. | ||
:return: the string, with unprintable characters changed to hex (e.g., "\x07") | ||
""" | ||
|
||
def hexify(match): | ||
return fr'\x{ord(match.group()):02x}' | ||
|
||
if s: | ||
return UNPRINTABLE_PATTERN.sub(hexify, s) | ||
return None | ||
|
||
|
||
def escape_control_chars(s): | ||
""" | ||
Replace unprintable chars in string with a hex representation AND shell quotes the string. | ||
:param string: an arbitrary string, possibly with unprintable characters. | ||
:return: the string, with unprintable characters changed to hex (e.g., "\x07") | ||
""" | ||
if s: | ||
return shlex.quote(unprintable_to_hex(s)) | ||
return None | ||
|
||
|
||
def substitute_control_chars(s): | ||
""" | ||
Replace unprintable chars in string with � unicode char | ||
:param string: an arbitrary string, possibly with unprintable characters. | ||
:return: tuple of the string with � replacements made and boolean indicated if chars were replaced | ||
""" | ||
match_result = UNPRINTABLE_PATTERN.search(s) | ||
s = UNPRINTABLE_PATTERN.sub('�', s) | ||
return (s, match_result is not None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added control character escaping for bucket and filenames. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
###################################################################### | ||
# | ||
# File: test/unit/utils/test_escape.py | ||
# | ||
# Copyright 2023 Backblaze Inc. All Rights Reserved. | ||
# | ||
# License https://www.backblaze.com/using_b2_code.html | ||
# | ||
###################################################################### | ||
|
||
from b2sdk.utils.escape import escape_control_chars, substitute_control_chars, unprintable_to_hex | ||
|
||
|
||
def test_unprintable_to_hex(): | ||
cases = [ | ||
(' abc-z', ' abc-z', "' abc-z'", (' abc-z', False)), | ||
('a\x7fb', 'a\\x7fb', "'a\\x7fb'", ('a�b', True)), | ||
('a\x00b a\x9fb ', 'a\\x00b a\\x9fb ', "'a\\x00b a\\x9fb '", ('a�b a�b ', True)), | ||
('a\x7fb\nc', 'a\\x7fb\nc', "'a\\x7fb\nc'", ('a�b\nc', True)), | ||
('\x9bT\x9bEtest', '\\x9bT\\x9bEtest', "'\\x9bT\\x9bEtest'", ('�T�Etest', True)), | ||
( | ||
'\x1b[32mC\x1b[33mC\x1b[34mI', '\\x1b[32mC\\x1b[33mC\\x1b[34mI', | ||
"'\\x1b[32mC\\x1b[33mC\\x1b[34mI'", ('�[32mC�[33mC�[34mI', True) | ||
) | ||
] | ||
for ( | ||
s, expected_unprintable_to_hex, expected_escape_control_chars, | ||
expected_substitute_control_chars | ||
) in cases: | ||
assert unprintable_to_hex(s) == expected_unprintable_to_hex | ||
assert escape_control_chars(s) == expected_escape_control_chars | ||
assert substitute_control_chars(s) == expected_substitute_control_chars |