okigan / awscurl

curl-like access to AWS resources with AWS Signature Version 4 request signing.
MIT License
755 stars 94 forks source link

`--data` with UTF-8 data raises UnicodeDecodeError #48

Closed josb closed 5 years ago

josb commented 6 years ago

The default encoding in Python 2.x is ascii so we are seeing the following backtrace when supplying UTF-8 arguments to the --data flag:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/awscurl/awscurl.py", line 339, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/site-packages/awscurl/awscurl.py", line 334, in main
    args.security_token or args.session_token
  File "/usr/local/lib/python2.7/site-packages/awscurl/awscurl.py", line 178, in make_request
    payload_hash = sha256_hash(data)
  File "/usr/local/lib/python2.7/site-packages/awscurl/awscurl.py", line 104, in sha256_hash
    return hashlib.sha256(val.encode('utf-8')).hexdigest()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 93: ordinal not in range(128)

The fix seems to be to avoid the implicit call to decode(<default encoding>) when dealing with val being of type str, by explicitly decoding val as utf-8 first so we end of with a unicode object to feed to encode('utf-8'), like it expects.

Hence the fix looks like this: change

        return hashlib.sha256(val.encode('utf-8')).hexdigest()

to

        return hashlib.sha256(val.decode('utf-8').encode('utf-8')).hexdigest()
okigan commented 6 years ago

I think '--data-binary' option would solve that for you (thanks to @mariosotil).

Post back if you still see on issue.

okigan commented 6 years ago

This released in v0.13, which was just published to https://pypi.org/project/awscurl

josb commented 5 years ago

That worked, thanks @okigan !