mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-28 07:31:23 -07:00
Prevent data being truncated over persistent connection socket (#43885)
* Change how data is sent to the persistent connection socket. We can't rely on readline(), so send the size of the data first. We can then read that many bytes from the stream on the recieving end. * Set pty to noncanonical mode before sending * Now that we send data length, we don't need a sentinel anymore * Copy socket changes to persistent, too * Use os.write instead of fdopen()ing and using that. * Follow pickle with sha1sum of pickle * Swap order of vars and init being passed to ansible-connection
This commit is contained in:
parent
77bff99f3c
commit
f221105882
4 changed files with 77 additions and 56 deletions
|
@ -27,6 +27,7 @@
|
|||
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import os
|
||||
import hashlib
|
||||
import json
|
||||
import socket
|
||||
import struct
|
||||
|
@ -36,6 +37,30 @@ import uuid
|
|||
from functools import partial
|
||||
from ansible.module_utils._text import to_bytes, to_text
|
||||
from ansible.module_utils.six import iteritems
|
||||
from ansible.module_utils.six.moves import cPickle
|
||||
|
||||
|
||||
def write_to_file_descriptor(fd, obj):
|
||||
"""Handles making sure all data is properly written to file descriptor fd.
|
||||
|
||||
In particular, that data is encoded in a character stream-friendly way and
|
||||
that all data gets written before returning.
|
||||
"""
|
||||
# Need to force a protocol that is compatible with both py2 and py3.
|
||||
# That would be protocol=2 or less.
|
||||
# Also need to force a protocol that excludes certain control chars as
|
||||
# stdin in this case is a pty and control chars will cause problems.
|
||||
# that means only protocol=0 will work.
|
||||
src = cPickle.dumps(obj, protocol=0)
|
||||
|
||||
# raw \r characters will not survive pty round-trip
|
||||
# They should be rehydrated on the receiving end
|
||||
src = src.replace(b'\r', br'\r')
|
||||
data_hash = to_bytes(hashlib.sha1(src).hexdigest())
|
||||
|
||||
os.write(fd, b'%d\n' % len(src))
|
||||
os.write(fd, src)
|
||||
os.write(fd, b'%s\n' % data_hash)
|
||||
|
||||
|
||||
def send_data(s, data):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue