Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove newlines from base64.encodestring output when using long basic auth #30

Closed
wants to merge 2 commits into from

Conversation

thelan
Copy link

@thelan thelan commented Jan 16, 2018

According to python documentation:

base64.encodestring(s)
Encode the string s, which can contain arbitrary binary data, and return a string containing one or more lines of base64-encoded data. encodestring() returns a string containing one or more lines of base64-encoded data always including an extra trailing newline ('\n').

Having extra newline within the header will break the authentication process as the server will read only the first part of the encoded 'user:password'

@paskal
Copy link

paskal commented Jan 16, 2018

Isn't it related to #23? Also, could you please write the scenario with the problem, which this patch solve?

@thelan
Copy link
Author

thelan commented Jan 16, 2018

Apparently I didn't remember making the old push request. My bad.

I think this would be more efficient to use only one call to base64.b64encode instead of this fix.

When logging with user long-zabbix-username and password reallylongpasswordKzSmxbpckS0sJ1tQWQNq7Jvif
Using python 2.7 here is the result of the 2 encoded string:

teststr='long-zabbix-username:reallylongpasswordKzSmxbpckS0sJ1tQWQNq7Jvif'

base64.encodestring(teststr)
'bG9uZy16YWJiaXgtdXNlcm5hbWU6cmVhbGx5bG9uZ3Bhc3N3b3JkS3pTbXhicGNrUzBzSjF0UVdR\nTnE3SnZpZg==\n'

string.strip(base64.encodestring(teststr))
'bG9uZy16YWJiaXgtdXNlcm5hbWU6cmVhbGx5bG9uZ3Bhc3N3b3JkS3pTbXhicGNrUzBzSjF0UVdR\nTnE3SnZpZg=='

base64.b64encode(teststr)
'bG9uZy16YWJiaXgtdXNlcm5hbWU6cmVhbGx5bG9uZ3Bhc3N3b3JkS3pTbXhicGNrUzBzSjF0UVdRTnE3SnZpZg=='

string.strip only remove the last \n leaving a newline in the encoded string.
When sending the header with urllib2 the newline is kept thus breaking the header data

@thelan
Copy link
Author

thelan commented Jan 16, 2018

i'll close the old PR for this one.

@thelan
Copy link
Author

thelan commented Jan 16, 2018

Test code:

#!/usr/bin/env python
from zabbix_api import ZabbixAPI, ZabbixAPISubClass, Already_Exists
import logging

http_login_user='long-zabbix-username'
http_login_password='reallylongpasswordKzSmxbpckS0sJ1tQWQNq7Jvif'
zbx = ZabbixAPI('https://zabbix/api_jsonrpc.php', timeout=600, user=http_login_user, passwd=http_login_password)
zbx.set_log_level(logging.DEBUG)
zbx.login(http_login_user, http_login_password)

Output with the patch:

10: Trying to login with 'long-zabbix-username':'md5(77af21a2334d999603639e351da9b29d)'
10: json_obj: {'params': {'password': 'reallylongpasswordKzSmxbpckS0sJ1tQWQNq7Jvif', 'user': 'long-zabbix-username'}, 'jsonrpc': '2.0', 'method': 'user.login', 'id': 0}
20: HTTP Auth enabled
20: Sending: {"params": {"password": "reallylongpasswordKzSmxbpckS0sJ1tQWQNq7Jvif", "user": "long-zabbix-username"}, "jsonrpc": "2.0", "method": "user.login", "id": 0}
10: Sending headers: {'Content-Type': 'application/json-rpc', 'Authorization': 'Basic bG9uZy16YWJiaXgtdXNlcm5hbWU6cmVhbGx5bG9uZ3Bhc3N3b3JkS3pTbXhicGNrUzBzSjF0UVdRTnE3SnZpZg==', 'User-Agent': 'python/zabbix_api'}

Output without patch: (see \n in the Authorization heaer)

10: Trying to login with 'long-zabbix-username':'md5(77af21a2334d999603639e351da9b29d)'
10: json_obj: {'params': {'password': 'reallylongpasswordKzSmxbpckS0sJ1tQWQNq7Jvif', 'user': 'long-zabbix-username'}, 'jsonrpc': '2.0', 'method': 'user.login', 'id': 0}
20: HTTP Auth enabled
20: Sending: {"params": {"password": "reallylongpasswordKzSmxbpckS0sJ1tQWQNq7Jvif", "user": "long-zabbix-username"}, "jsonrpc": "2.0", "method": "user.login", "id": 0}
10: Sending headers: {'Content-Type': 'application/json-rpc', 'Authorization': 'Basic bG9uZy16YWJiaXgtdXNlcm5hbWU6cmVhbGx5bG9uZ3Bhc3N3b3JkS3pTbXhicGNrUzBzSjF0UVdR\nTnE3SnZpZg==', 'User-Agent': 'python/zabbix_api'}
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    zbx.login(http_login_user, http_login_password)
  File "/home/thelan/git/py-zbxapi/zabbix/zabbix_api.py", line 207, in login
    result = self.do_request(obj)
  File "/home/thelan/git/py-zbxapi/zabbix/zabbix_api.py", line 249, in do_request
    response = opener.open(request, timeout=self.timeout)
  File "/usr/lib64/python2.7/urllib2.py", line 429, in open
    response = self._open(req, data)
  File "/usr/lib64/python2.7/urllib2.py", line 447, in _open
    '_open', req)
  File "/usr/lib64/python2.7/urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.7/urllib2.py", line 1230, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib64/python2.7/urllib2.py", line 1197, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/usr/lib64/python2.7/httplib.py", line 1042, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python2.7/httplib.py", line 1081, in _send_request
    self.putheader(hdr, value)
  File "/usr/lib64/python2.7/httplib.py", line 1020, in putheader
    raise ValueError('Invalid header value %r' % (one_value,))
ValueError: Invalid header value 'Basic bG9uZy16YWJiaXgtdXNlcm5hbWU6cmVhbGx5bG9uZ3Bhc3N3b3JkS3pTbXhicGNrUzBzSjF0UVdR\nTnE3SnZpZg=='

@gescheit
Copy link
Owner

gescheit commented Feb 2, 2018

Thanks for your contributing! Unfortunately, http-auth had been broken for python3. Can you also make a fix for py3? Problem with py3 in b64encode() function output type. It is bytes. So you need to check python version and depend on it decode b64 result or not.
Also, string.strip is not needed anymore.

@empire
Copy link

empire commented Dec 26, 2018

Thanks for your contributing! Unfortunately, http-auth had been broken for python3. Can you also make a fix for py3? Problem with py3 in b64encode() function output type. It is bytes. So you need to check python version and depend on it decode b64 result or not.
Also, string.strip is not needed anymore.

@gescheit, I fixed it with 32 pull request.

@empire
Copy link

empire commented Apr 6, 2019

This patch is not needed any more (see #32)

@thelan thelan closed this Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants