Skip to content

Commit

Permalink
[stable-2.15] Additional Unsafe fixes (#82376) (#82380)
Browse files Browse the repository at this point in the history
* Allow older pickle protocols to pickle unsafe classes. Fixes #82356

* Address issues when iterating or getting single index from AnsibleUnsafeBytes. Fixes #82375

* clog frag
(cherry picked from commit afe3fc1)
  • Loading branch information
sivel committed Dec 11, 2023
1 parent a79e410 commit b19cfb1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/unsafe-fixes-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bugfixes:
- unsafe data - Address an incompatibility with ``AnsibleUnsafeText`` and ``AnsibleUnsafeBytes`` when pickling with ``protocol=0``
- unsafe data - Address an incompatibility when iterating or getting a single index from ``AnsibleUnsafeBytes``
12 changes: 8 additions & 4 deletions lib/ansible/utils/unsafe_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class AnsibleUnsafeBytes(bytes, AnsibleUnsafe):
def _strip_unsafe(self):
return super().__bytes__()

def __reduce__(self, /):
return (self.__class__, (self._strip_unsafe(),))

def __str__(self, /): # pylint: disable=invalid-str-returned
return self.decode()

Expand All @@ -84,12 +87,10 @@ def __format__(self, format_spec, /): # pylint: disable=invalid-format-returned
return AnsibleUnsafeText(super().__format__(format_spec))

def __getitem__(self, key, /):
if isinstance(key, int):
return super().__getitem__(key)
return self.__class__(super().__getitem__(key))

def __iter__(self, /):
cls = self.__class__
return (cls(c) for c in super().__iter__())

def __reversed__(self, /):
return self[::-1]

Expand Down Expand Up @@ -192,6 +193,9 @@ class AnsibleUnsafeText(str, AnsibleUnsafe):
def _strip_unsafe(self, /):
return super().__str__()

def __reduce__(self, /):
return (self.__class__, (self._strip_unsafe(),))

def __str__(self, /): # pylint: disable=invalid-str-returned
return self

Expand Down
7 changes: 7 additions & 0 deletions test/integration/targets/filter_urls/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
- "{'foo': 'bar', 'baz': 'buz'}|urlencode == 'foo=bar&baz=buz'"
- "()|urlencode == ''"

- name: verify urlencode works for unsafe strings
assert:
that:
- thing|urlencode == 'foo%3Abar'
vars:
thing: !unsafe foo:bar

# Needed (temporarily) due to coverage reports not including the last task.
- assert:
that: true

0 comments on commit b19cfb1

Please sign in to comment.