1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00

Warned about names a peer doesn't take, Refused a NUL in Python's strings

- Replication: what a link's roles here may send but the peer doesn't
  take (not in its roles' accept_* there) is skipped as before, and now
  logged as a warning (the first ten "topic:node" named) once per link,
  again only when that changes.
- Python: every string going to the DLL as a C string (a message, a
  name, SQL, a path) with a NUL is refused (INVALID_ARGUMENT) rather
  than cut there, as C# already did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 12:38:56 +01:00
parent 1bae8ee182
commit fda21a986b
7 changed files with 261 additions and 39 deletions
+15 -7
View File
@@ -202,10 +202,18 @@ class NamesList:
truncated: bool # True if they didn't all fit in one answer
def _text(value: str) -> bytes:
"""value as the DLL's C string: one with a NUL is refused rather than
cut there."""
if "\0" in value:
raise BakaShrotError("A string with a NUL character", ErrorCode.INVALID_ARGUMENT)
return value.encode("utf-8")
def _name(value: str) -> bytes:
if not isinstance(value, str):
raise TypeError(f"a topic or node is a name, not {value!r}")
return value.encode("utf-8")
return _text(value)
def _find_dll(dll_path: Optional[str]) -> str:
@@ -388,9 +396,9 @@ class Client:
raise self._failed("Answer timeout")
for setter, value in ((dll.SetCaCertificateA, self.ca), (dll.SetCertificateA, self.certificate),
(dll.SetPrivateKeyA, self.key), (dll.SetCrlA, self.crl or "")):
if not setter(handle, value.encode("utf-8")):
if not setter(handle, _text(value)):
raise self._failed("Certificates")
if not dll.ConnectA(handle, self.host.encode("utf-8"), self.port):
if not dll.ConnectA(handle, _text(self.host), self.port):
raise self._failed(f"Connect to {self.host}:{self.port}", connecting=True)
self.node = dll.string(dll.GetNodeA, handle)
return self
@@ -496,7 +504,7 @@ class Client:
message is only stored once all of it came."""
if not isinstance(data, str):
data = json.dumps(data, separators=(",", ":"), ensure_ascii=False)
encoded = data.encode("utf-8")
encoded = _text(data)
if len(encoded) > MAX_MESSAGE_SIZE:
raise BakaShrotError(f"The message is too big: {len(encoded)} bytes, at most {MAX_MESSAGE_SIZE}", ErrorCode.INVALID_ARGUMENT)
with self._lock:
@@ -507,7 +515,7 @@ class Client:
raw = bytes(attachment)
message_id = self._dll.PushMessageWithDataA(self._handle, _name(topic), encoded, raw, len(raw))
else:
path = os.fspath(attachment).encode("utf-8")
path = _text(os.fspath(attachment))
message_id = self._dll.PushMessageWithFileA(self._handle, _name(topic), encoded, path)
if message_id < 1:
raise self._failed("Push")
@@ -536,7 +544,7 @@ class Client:
with self._lock:
self._check_connected()
size = self._dll.SaveAttachmentA(self._handle, _name(topic), _name(node), int(message_id),
os.fspath(path).encode("utf-8"))
_text(os.fspath(path)))
if size < 0:
raise self._failed("Attachment")
return size
@@ -642,7 +650,7 @@ class Client:
query is refused: RefusedError "malformed JSON"."""
with self._lock:
self._check_connected()
if not self._dll.QueryA(self._handle, sql.encode("utf-8")):
if not self._dll.QueryA(self._handle, _text(sql)):
raise self._failed("Query")
return self._dll.string(self._dll.GetQueryResultAsJsonA, self._handle)
+4
View File
@@ -138,6 +138,10 @@ with kitchen:
ok, detail = raises(lambda: kitchen.push("x" * (bakashrot.MAX_MESSAGE_SIZE + 1), "logs"), BakaShrotError, "too big",
ErrorCode.INVALID_ARGUMENT)
check("A message over the limit: refused before sending", ok, detail)
ok, detail = raises(lambda: kitchen.push("a\0b", "logs"), BakaShrotError, "NUL", ErrorCode.INVALID_ARGUMENT)
check("A message with a NUL: refused rather than cut", ok, detail)
ok, detail = raises(lambda: kitchen.query("SELECT 1\0; DELETE"), BakaShrotError, "NUL", ErrorCode.INVALID_ARGUMENT)
check("SQL with a NUL: refused too", ok and kitchen.connected, detail)
secret_id = kitchen.push({"s": 1}, "secret")
check("Push to another allowed topic", secret_id == 1, secret_id)
with lounge: