1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2024-12-27 18:28:42 +00:00

Merge pull request #7288 from ThomasWaldmann/host-user-group-names-encoding

hostname / username encoding
This commit is contained in:
TW 2023-01-19 21:48:28 +01:00 committed by GitHub
commit 4a94097193
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -75,8 +75,7 @@ def text_to_json(key, value):
value.encode(coding, errors="strict") # check if pure unicode
except UnicodeEncodeError:
# value has surrogate escape sequences
value_replace_encoded = value.encode(coding, errors="replace")
data[key] = value_replace_encoded.decode(coding, errors="strict")
data[key] = remove_surrogates(value)
value_bytes = value.encode(coding, errors="surrogateescape")
data.update(binary_to_json(key, value_bytes))
else:
@ -715,9 +714,9 @@ def __init__(self, format, repository, manifest, key, *, json=False, iec=False):
self.format = partial_format(format, static_keys)
self.format_keys = {f[1] for f in Formatter().parse(format)}
self.call_keys = {
"hostname": partial(self.get_meta, "hostname", rs=True),
"username": partial(self.get_meta, "username", rs=True),
"comment": partial(self.get_meta, "comment", rs=False),
"hostname": partial(self.get_meta, "hostname"),
"username": partial(self.get_meta, "username"),
"comment": partial(self.get_meta, "comment"),
"end": self.get_ts_end,
"command_line": self.get_cmdline,
}
@ -747,6 +746,12 @@ def get_item_data(self, archive_info):
)
for key in self.used_call_keys:
item_data[key] = self.call_keys[key]()
# Note: name and comment are validated, should never contain surrogate escapes.
# But unsure whether hostname, username could contain surrogate escapes, play safe:
for key in "hostname", "username":
if key in item_data:
item_data.update(text_to_json(key, item_data[key]))
return item_data
@property
@ -758,9 +763,8 @@ def archive(self):
self._archive = Archive(self.manifest, self.name, iec=self.iec)
return self._archive
def get_meta(self, key, rs):
value = self.archive.metadata.get(key, "")
return remove_surrogates(value) if rs else value
def get_meta(self, key):
return self.archive.metadata.get(key, "")
def get_cmdline(self):
cmdline = map(remove_surrogates, self.archive.metadata.get("cmdline", []))