Skip to content

[BUG] binary return data causes tracebacks in many returners #59012

Open

Description

@dhiltonp

Description
Some returners return binary data. The default returner uses msgpack, which can handle binary data.

19/40 returners in salt/returners use salt.utils.json.dumps, which cannot handle binary data.

Maybe binary data should be b64 encoded instead of the return data being dropped?


grep 'json.dumps' salt/returners | awk -F : '{print $1}' | uniq:

cassandra_cql_return.py
couchbase_return.py
couchdb_return.py
elasticsearch_return.py
etcd_return.py
highstate_return.py
influxdb_return.py
kafka_return.py
mattermost_returner.py
memcache_return.py
mysql.py
odbc.py
postgres.py
postgres_local_cache.py
redis_return.py
slack_webhook_return.py
splunk.py
sqlite3_return.py
syslog_return.py

Activity

  1. added
    bug
    on Nov 23, 2020
  2. OrangeDog commented on Nov 30, 2020

    @OrangeDog
    Contributor

    Maybe binary data should be b64 encoded instead of the return data being dropped?

    It's probably because the returners were all written for Python 2, where str and bytes are interchangeable, and never updated.
    Any state that is still returning bytes should be fixed to return str, but it wouldn't hurt to also allow the returners to handle both.

  3. added
    python3
    severity-high
    and removed on Jan 28, 2021
  4. added this to the Aluminium milestone on Jan 28, 2021
  5. added
    Aluminium
    on Jan 28, 2021
  6. sagetherage commented on Mar 22, 2021

    @sagetherage
    Contributor

    Joe was pulled off of some work in AL for other priorities, moving this to Si release cycle

  7. added
    Silicon v3004.0
    and removed
    Aluminium
    on Mar 22, 2021
  8. modified the milestones: Aluminium, Silicon on Mar 22, 2021
  9. sagetherage commented on Mar 22, 2021

    @sagetherage
    Contributor

    @dhiltonp @xeacott @danielrobbins there are several of these issues where Python2 expected either strings or bytes and some modules were written with one or the other and likely not both, I created a label, if anyone can create PRs please do. I hope to get most of these work done in Silicon release cycle or at least planned and worked in Silicon and soon after, if there is a global approach (doubtful) we may want to explore it.

  10. modified the milestones: Silicon, Approved on Aug 12, 2021
  11. removed
    Silicon v3004.0
    on Oct 25, 2021
  12. amalaguti commented on Jul 5, 2023

    @amalaguti

    This issue still present in 3006.1 right ? Trying to use odbc returner and getting errors like
    [WARNING ] Returner unavailable: the JSON object must be str, bytes or bytearray, not 'Row'
    TypeError: the JSON object must be str, bytes or bytearray, not 'Row'

  13. Orlando-Richards commented on Jun 16, 2025

    @Orlando-Richards
    Contributor

    A recent salt release (I went from 3006.9 -> 3006.12) added the tok return to all minion payloads, which is encoded, that caused me problems when using the API.

    I implemented a local patch on the salt-master to sanitize the data:

    --- /opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/json.py.dist	2025-06-16 17:08:27.484890052 +0100
    +++ /opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/json.py	2025-06-16 17:19:28.804754435 +0100
    @@ -152,6 +152,22 @@
             kwargs["ensure_ascii"] = False
         return json_module.dump(obj, fp, **kwargs)
    
    +def _sanitize_for_json(obj):
    +    """
    +    Recursively clean up an object to make it safe for JSON serialization.
    +    Non-serializable types are converted to strings.
    +    """
    +    if isinstance(obj, dict):
    +        return {_sanitize_for_json(k): _sanitize_for_json(v) for k, v in obj.items()}
    +    elif isinstance(obj, list):
    +        return [_sanitize_for_json(v) for v in obj]
    +    elif isinstance(obj, tuple):
    +        return tuple(_sanitize_for_json(v) for v in obj)
    +    elif isinstance(obj, (str, int, float, bool)) or obj is None:
    +        return obj
    +    else:
    +        # Convert non-serializable object to string
    +        return str(obj)
    
     def dumps(obj, **kwargs):
         """
    @@ -171,4 +187,4 @@
         json_module = kwargs.pop("_json_module", json)
         if "ensure_ascii" not in kwargs:
             kwargs["ensure_ascii"] = False
    -    return json_module.dumps(obj, **kwargs)
    +    return json_module.dumps(_sanitize_for_json(obj), **kwargs)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpython3severity-high

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions