mirror of
https://github.com/calibrain/shelfmark.git
synced 2026-11-25 22:41:15 +00:00
Searches and downloads share one IRC connection and run side by side, which broke in three ways: - wait_for_dcc took the first search-results file to arrive, so a late file for an earlier query was cached under the current one. It now skips a results file whose name is for a different query. - A download failing on the shared socket closed it under a search in flight. The search then cached an empty answer, hiding the book for a day. Searches and downloads now hold one lock for the whole exchange. - A search that lost its connection returned [] and counted as a no-match pass, pushing the request into backoff. It now raises SourceUnavailableError, which auto-download already treats as an error, and caches nothing. This was written with AI assistance. I reviewed the changes and ran the tests above and verified that this fixed my issues with searching and downloading using IRC. *Generated with Claude Sonnet 5.5* <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Search results are matched against the requested title, ignoring punctuation and accents while rejecting partial-title matches. Filenames without the usual suffix are also recognized. * Results and “no results” notices from earlier searches are skipped, and denied searches report an error. * Connection drops and DCC read errors are reported as failures rather than empty results or timeouts. * Repeated identical searches are rate-limited before sending, and search time limits apply while waiting for results or downloads. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com> Co-authored-by: CaliBrain <calibrain@l4n.xyz>
101 lines
2.9 KiB
Python
101 lines
2.9 KiB
Python
import socket
|
|
|
|
import pytest
|
|
|
|
from shelfmark.release_sources.irc.dcc import (
|
|
DCCOffer,
|
|
DCCParseError,
|
|
DCCSecurityError,
|
|
download_dcc,
|
|
parse_dcc_send,
|
|
safe_dcc_filename,
|
|
validate_dcc_endpoint,
|
|
)
|
|
|
|
|
|
def test_safe_dcc_filename_allows_plain_filenames() -> None:
|
|
assert safe_dcc_filename("results.txt") == "results.txt"
|
|
assert safe_dcc_filename("Author - Title.epub") == "Author - Title.epub"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"filename",
|
|
[
|
|
"",
|
|
".",
|
|
"..",
|
|
"../outside.txt",
|
|
"/tmp/outside.txt",
|
|
r"..\outside.txt",
|
|
r"C:\temp\outside.txt",
|
|
],
|
|
)
|
|
def test_safe_dcc_filename_rejects_paths(filename: str) -> None:
|
|
with pytest.raises(DCCSecurityError):
|
|
safe_dcc_filename(filename)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"ip",
|
|
[
|
|
"127.0.0.1",
|
|
"10.0.0.1",
|
|
"172.16.0.1",
|
|
"192.168.1.1",
|
|
"169.254.169.254",
|
|
"0.0.0.0",
|
|
],
|
|
)
|
|
def test_validate_dcc_endpoint_rejects_non_public_ips(ip: str) -> None:
|
|
with pytest.raises(DCCSecurityError):
|
|
validate_dcc_endpoint(DCCOffer(filename="book.epub", ip=ip, port=1234, size=1))
|
|
|
|
|
|
@pytest.mark.parametrize("port", [0, 65536])
|
|
def test_validate_dcc_endpoint_rejects_invalid_ports(port: int) -> None:
|
|
with pytest.raises(DCCSecurityError):
|
|
validate_dcc_endpoint(DCCOffer(filename="book.epub", ip="8.8.8.8", port=port, size=1))
|
|
|
|
|
|
def test_validate_dcc_endpoint_allows_public_endpoint() -> None:
|
|
validate_dcc_endpoint(DCCOffer(filename="book.epub", ip="8.8.8.8", port=443, size=1))
|
|
|
|
|
|
def test_parse_dcc_send_rejects_out_of_range_ip_integer() -> None:
|
|
with pytest.raises(DCCParseError):
|
|
parse_dcc_send('DCC SEND "book.epub" 999999999999999999 443 1')
|
|
|
|
|
|
def test_download_dcc_rejects_private_endpoint_before_socket_connect(
|
|
monkeypatch,
|
|
tmp_path,
|
|
) -> None:
|
|
def fail_socket(*_args: object, **_kwargs: object) -> socket.socket:
|
|
raise AssertionError("download should reject the endpoint before opening a socket")
|
|
|
|
monkeypatch.setattr(socket, "socket", fail_socket)
|
|
|
|
with pytest.raises(DCCSecurityError):
|
|
download_dcc(
|
|
DCCOffer(filename="book.epub", ip="127.0.0.1", port=1234, size=1),
|
|
tmp_path / "book.epub",
|
|
)
|
|
|
|
|
|
def test_download_dcc_turns_a_reset_mid_transfer_into_a_dcc_error(monkeypatch, tmp_path) -> None:
|
|
"""A reset on the DCC socket must not look like the IRC connection failing."""
|
|
from shelfmark.release_sources.irc.dcc import DCCError
|
|
|
|
class ResetSocket:
|
|
def settimeout(self, _t: float) -> None: ...
|
|
def connect(self, _addr: object) -> None: ...
|
|
def close(self) -> None: ...
|
|
def recv(self, _n: int) -> bytes:
|
|
raise ConnectionResetError
|
|
|
|
monkeypatch.setattr(socket, "socket", lambda *_a: ResetSocket())
|
|
offer = DCCOffer(filename="a.epub", ip="8.8.8.8", port=443, size=10)
|
|
|
|
with pytest.raises(DCCError):
|
|
download_dcc(offer, tmp_path / "a.epub", timeout=1.0)
|