Skip to content
Open
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
4 changes: 4 additions & 0 deletions pymemcache/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def check_key_helper(
raise MemcacheIllegalInputError("Key contains whitespace: %r" % key)
elif b"\00" in key:
raise MemcacheIllegalInputError("Key contains null: %r" % key)
elif any(
(byte < 0x20 and byte not in b" \t\n\r\v\f") or byte == 0x7F for byte in key
):
raise MemcacheIllegalInputError("Key contains control characters: %r" % key)

return key

Expand Down
30 changes: 30 additions & 0 deletions pymemcache/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ def is_ipv6(address):
("b" * 251, True, b"", MemcacheIllegalInputError, "Key is too long"),
("foo bar", True, b"", MemcacheIllegalInputError, "Key contains whitespace"),
("\00", True, b"", MemcacheIllegalInputError, "Key contains null"),
(
b"foo\x01",
False,
b"",
MemcacheIllegalInputError,
"Key contains control characters",
),
(
b"foo\x7f",
True,
b"",
MemcacheIllegalInputError,
"Key contains control characters",
),
(
"\x01",
True,
b"",
MemcacheIllegalInputError,
"Key contains control characters",
),
(None, True, b"", TypeError, None),
],
)
Expand All @@ -82,6 +103,7 @@ def test_check_key_helper():
assert check_key_helper("key", True) == b"key"
assert isinstance(check_key_helper(b"key", True), bytes)
assert check_key_helper("", True) == b""
assert check_key_helper(b"key\x80", False) == b"key\x80"


class MockSocket:
Expand Down Expand Up @@ -931,6 +953,14 @@ def _set():
with pytest.raises(MemcacheIllegalInputError):
_set()

def test_set_key_with_control_character(self):
client = self.make_client([])

with pytest.raises(MemcacheIllegalInputError):
client.set(b"key\x01", b"value", noreply=False)

client._connect.assert_not_called()

def test_set_key_with_noninteger_expire(self):
client = self.make_client([b""])

Expand Down