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

Raise ScramException for malformed messages #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions scramp/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,23 @@ def _get_client_first(username, c_nonce, channel_binding):


def _set_client_first(client_first, s_nonce, channel_binding):
first_comma = client_first.index(",")
second_comma = client_first.index(",", first_comma + 1)
try:
first_comma = client_first.index(",")
second_comma = client_first.index(",", first_comma + 1)
except ValueError:
raise ScramException(
"The client sent a malformed first message.",
SERVER_ERROR_OTHER_ERROR,
)
gs2_header = client_first[:second_comma].split(",")
gs2_cbind_flag = gs2_header[0]
gs2_char = gs2_cbind_flag[0]
try:
gs2_cbind_flag = gs2_header[0]
gs2_char = gs2_cbind_flag[0]
except IndexError:
raise ScramException(
"The client sent malformed gs2 data.",
SERVER_ERROR_OTHER_ERROR,
)

if gs2_char == "y":
if channel_binding is not None:
Expand Down Expand Up @@ -462,6 +474,14 @@ def _set_client_first(client_first, s_nonce, channel_binding):

client_first_bare = client_first[second_comma + 1 :]
msg = _parse_message(client_first_bare)

missing = [letter for letter in "rn" if letter not in msg]
if missing:
raise ScramException(
"The server returned a message without expected parameters. Missing: "
f"{', '.join(missing)}."
)

c_nonce = msg["r"]
nonce = c_nonce + s_nonce
user = msg["n"]
Expand All @@ -479,6 +499,14 @@ def _set_server_first(server_first, c_nonce, client_first_bare, channel_binding)
msg = _parse_message(server_first)
if "e" in msg:
raise ScramException(f"The server returned the error: {msg['e']}")

missing = [letter for letter in "rsi" if letter not in msg]
if missing:
raise ScramException(
"The server returned a message without expected parameters. Missing: "
f"{', '.join(missing)}."
)

nonce = msg["r"]
salt = msg["s"]
iterations = int(msg["i"])
Expand Down Expand Up @@ -532,6 +560,17 @@ def _set_client_final(
auth_msg = uenc(auth_msg_str)

msg = _parse_message(client_final)

missing = [letter for letter in "rpc" if letter not in msg]
if missing:
raise ScramException(
(
"The client sent a message without expected parameters. "
f"Missing: {', '.join(missing)}."
),
SERVER_ERROR_OTHER_ERROR,
)

nonce = msg["r"]
proof = msg["p"]
channel_binding = msg["c"]
Expand All @@ -558,6 +597,10 @@ def _set_server_final(message, server_signature):
msg = _parse_message(message)
if "e" in msg:
raise ScramException(f"The server returned the error: {msg['e']}")
if "v" not in msg:
raise ScramException(
f"The server returned a final message without the 'v' parameter."
)

if server_signature != msg["v"]:
raise ScramException(
Expand Down
72 changes: 72 additions & 0 deletions test/test_scramp.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,78 @@ def test_set_server_first_error():
c.set_server_first("e=other-error")


def test_set_server_first_missing_param():
c = ScramClient(["SCRAM-SHA-256"], "user", "pencil")
c.get_client_first()
with pytest.raises(
ScramException,
match="The server returned a message without expected parameters. Missing: r, s, i.",
):
c.set_server_first("junk")


def test_set_server_final_missing_param():
x = SCRAM_SHA_256_EXCHANGE
c = ScramClient(
["SCRAM-SHA-256"],
USERNAME,
PASSWORD,
c_nonce=x["c_nonce"],
)
c.get_client_first()
c.set_server_first(x["sfirst"])
c.get_client_final()
with pytest.raises(
ScramException,
match="The server returned a final message without the 'v' parameter.",
):
c.set_server_final("junk")


def test_set_client_first_nonsense():
m = ScramMechanism(mechanism="SCRAM-SHA-256")
s = m.make_server(lambda x: None)
with pytest.raises(
ScramException, match="The client sent a malformed first message."
):
s.set_client_first("junk")


def test_set_client_first_missing_param():
m = ScramMechanism(mechanism="SCRAM-SHA-256")
s = m.make_server(lambda x: None)
with pytest.raises(
ScramException,
match="The server returned a message without expected parameters. Missing: r, n.",
):
s.set_client_first("n,morejunk,bonusjunk")


def test_set_client_final_missing_param():
x = SCRAM_SHA_256_EXCHANGE
m = ScramMechanism(mechanism="SCRAM-SHA-256")

def auth_fn(username):
lookup = {
USERNAME: m.make_auth_info(
PASSWORD, salt=b64dec(x["salt"]), iteration_count=x["iterations"]
)
}
return lookup[username]

s = m.make_server(
auth_fn, channel_binding=x["channel_binding"], s_nonce=x["s_nonce"]
)

s.set_client_first(x["cfirst"])
s.get_server_first()
with pytest.raises(
ScramException,
match="The client sent a message without expected parameters. Missing: r, p, c.",
):
s.set_client_final("junk")


def test_make_channel_binding_tls_server_end_point(mocker):
ssl_socket = mocker.Mock()
ssl_socket.getpeercert = mocker.Mock(return_value=b"cafe")
Expand Down