Skip to content
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
3 changes: 2 additions & 1 deletion docs/issues/open/1347-overhaul-packages-testing/EPIC.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ Status values: `TODO`, `IN_PROGRESS`, `BLOCKED`, `DONE`.
| 9 | #2361 - Keep the report-only package coverage summary from failing on a discovery failure | `docs/issues/open/2361-1347-package-coverage-summary-discovery-failure/ISSUE.md` | TODO | Bug follow-up of #2347 (`review-finding:pr-2293-f1`): the #2222 summary check goes red with a JSON parse error when discovery fails. |
| 10 | #2637 - Add tests to the axum-rest-api-server package | `docs/issues/closed/2637-1347-add-tests-axum-rest-api-server/ISSUE.md` | DONE | S0 (spec and T1 baseline) merged through #2638; S1 (authentication and shared responses) merged through #2667; S2 (routes) merged through #2687; S3 (auth-key and whitelist contexts) merged through #2702; S4 (torrent and stats contexts) merged through #2718; S5 (server lifecycle) merged through #2744; S6 (crate root, wiring and final tasks) merged through #2772; the issue is closed. Replaces the former draft that was misfiled under #1349. |
| 11 | #2824 - Add tests to the primitives package | `docs/issues/open/2824-1347-add-tests-primitives/ISSUE.md` | IN_PROGRESS | Spec-only PR carries the specification and the T1 baseline and inventory; S0 (the spec, #2828) is merged and S1 (the `PeerRole` JSON casing fix, #2834) is in review; S2-S4 follow. Findings F1 (fixed through #2811), F3 (#2804) and the `AddAssign` and `peer::Id::new` follow-ups are handed off to their own issues. |
| 12 | Additional package-testing subissues | Create a folder-style spec when a concrete package need is identified | TODO | Permitted but not required upfront; retain scope in this EPIC. |
| 12 | #2838 - UDP tracker accepts announces with a negative left, uploaded or downloaded | `docs/issues/open/2838-1347-udp-reject-negative-announce-bytes/ISSUE.md` | IN_PROGRESS | Bug found by the #2824 primitives tests work; fix in PR #2839. |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Major][F10] The PR description describes a different change.

Outside the diff; anchored on the line that names this PR. The description as it stands records related issues of #2776 (docs/issues/closed/2776-2411-axum-server-0-9-timeouts/related-issues.md, absent at this head and at develop 388faf59a), says "Docs only" and ends with "Related to #324, #603, #612, #613, #2776"; it names neither #2838 nor the fix. The open-pull-request skill (SKILL.md:76) says the maintainer merge tool copies the PR body into the merge commit, as develop's merge commits show, so merging now would record a docs-only change for a code fix, and that cannot be corrected afterwards. The reply on F8 says the description's commit order was corrected, so two PR bodies look swapped. Restoring this PR's description (the fix and its placement, the tests, the evidence, the line for #2838) clears it.

| 13 | Additional package-testing subissues | Create a folder-style spec when a concrete package need is identified | TODO | Permitted but not required upfront; retain scope in this EPIC. |

## Package Coverage Tracking

Expand Down
261 changes: 261 additions & 0 deletions docs/issues/open/2838-1347-udp-reject-negative-announce-bytes/ISSUE.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
16:32:52.528 start: python3 -I evidence/udp_announce_probe.py 6868 1000
announce response: interval=120 leechers=1 seeders=0
16:32:52.540 end
16:32:52.543 start: python3 -I evidence/udp_announce_probe.py 6868 -5
action=3 message='tracker announce error: Invalid announce request: `left` must not be negative, got -5'
16:32:52.555 end
16:32:52.557 start: python3 -I evidence/udp_announce_probe.py 6868 0
announce response: interval=120 leechers=0 seeders=1
16:32:52.570 end
16:32:52.572 start: python3 -I evidence/udp_announce_probe.py 6868 0 -1 0
action=3 message='tracker announce error: Invalid announce request: `uploaded` must not be negative, got -1'
16:32:52.585 end
16:32:52.588 start: python3 -I evidence/udp_announce_probe.py 6868 0 0 -1
action=3 message='tracker announce error: Invalid announce request: `downloaded` must not be negative, got -1'
16:32:52.600 end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
17:28:55.118
left=1000
announce response: interval=120 leechers=1 seeders=0
17:28:55.132
left=-5
announce response: interval=120 leechers=0 seeders=1
17:28:55.145
left=0
announce response: interval=120 leechers=0 seeders=1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
thread 'handlers::announce::tests::announce_request::using_ipv4::it_should_reject_an_announce_with_a_negative_number_of_bytes_left' (1728527) panicked at packages/udp-server/src/handlers/announce.rs:595:17:
a negative `left` must be rejected, got: Ok(AnnounceIpv4(AnnounceResponse { fixed: AnnounceResponseFixedData { transaction_id: TransactionId(I32(0)), announce_interval: AnnounceInterval(I32(120)), leechers: NumberOfPeers(I32(0)), seeders: NumberOfPeers(I32(1)) }, peers: [] }))
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

--
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 223 filtered out; finished in 0.02s

error: test failed, to rerun pass `-p torrust-tracker-udp-server --lib`
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2026-11-12T16:32:52.551837Z ERROR process_request:handle_packet{server_service_binding=ServiceBinding { protocol: UDP, bind_address: 127.0.0.1:6868 } connection_id_validation=Strict request_id="8c12f0e4-869e-41a1-9167-cd68cd05c118"}:handle_error{req_kind=Some(Announce { announce_request: AnnounceRequest { connection_id: ConnectionId(I64(6317256492440616017)), action_placeholder: AnnounceActionPlaceholder(I32(1)), transaction_id: TransactionId(I32(4321)), info_hash: InfoHash([17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), peer_id: PeerId([45, 88, 88, 48, 48, 48, 49, 45, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]), bytes_downloaded: NumberOfBytes(I64(0)), bytes_left: NumberOfBytes(I64(-5)), bytes_uploaded: NumberOfBytes(I64(0)), event: AnnounceEventBytes(I32(2)), ip_address: Ipv4AddrBytes([0, 0, 0, 0]), key: PeerKey(I32(7)), peers_wanted: NumberOfPeers(I32(-1)), port: Port(U16(6881)) } }) client_socket_addr=127.0.0.1:37317 server_service_binding=ServiceBinding { protocol: UDP, bind_address: 127.0.0.1:6868 } configuration_instance_id=ConfigurationInstanceId { service_role: UdpTracker, instance_index: 0 } public_url=None request_id=8c12f0e4-869e-41a1-9167-cd68cd05c118 cookie_valid_range=1794501051.551796..1794501173.551796 error=AnnounceFailed { source: NegativeNumberOfBytes { field: Left, value: -5 } } opt_transaction_id=Some(TransactionId(I32(4321)))}: UDP TRACKER: response error error=tracker announce error: Invalid announce request: `left` must not be negative, got -5 client_socket_addr=127.0.0.1:37317 server_socket_addr=127.0.0.1:6868 service_binding=udp://127.0.0.1:6868 request_id=8c12f0e4-869e-41a1-9167-cd68cd05c118 transaction_id=4321
2026-11-12T16:32:52.581471Z ERROR process_request:handle_packet{server_service_binding=ServiceBinding { protocol: UDP, bind_address: 127.0.0.1:6868 } connection_id_validation=Strict request_id="74334122-f76a-4914-88c1-90c4b5745cca"}:handle_error{req_kind=Some(Announce { announce_request: AnnounceRequest { connection_id: ConnectionId(I64(-712296129322009379)), action_placeholder: AnnounceActionPlaceholder(I32(1)), transaction_id: TransactionId(I32(4321)), info_hash: InfoHash([17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), peer_id: PeerId([45, 88, 88, 48, 48, 48, 49, 45, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]), bytes_downloaded: NumberOfBytes(I64(0)), bytes_left: NumberOfBytes(I64(0)), bytes_uploaded: NumberOfBytes(I64(-1)), event: AnnounceEventBytes(I32(2)), ip_address: Ipv4AddrBytes([0, 0, 0, 0]), key: PeerKey(I32(7)), peers_wanted: NumberOfPeers(I32(-1)), port: Port(U16(6881)) } }) client_socket_addr=127.0.0.1:43037 server_service_binding=ServiceBinding { protocol: UDP, bind_address: 127.0.0.1:6868 } configuration_instance_id=ConfigurationInstanceId { service_role: UdpTracker, instance_index: 0 } public_url=None request_id=74334122-f76a-4914-88c1-90c4b5745cca cookie_valid_range=1794501051.5814247..1794501173.5814247 error=AnnounceFailed { source: NegativeNumberOfBytes { field: Uploaded, value: -1 } } opt_transaction_id=Some(TransactionId(I32(4321)))}: UDP TRACKER: response error error=tracker announce error: Invalid announce request: `uploaded` must not be negative, got -1 client_socket_addr=127.0.0.1:43037 server_socket_addr=127.0.0.1:6868 service_binding=udp://127.0.0.1:6868 request_id=74334122-f76a-4914-88c1-90c4b5745cca transaction_id=4321
2026-11-12T16:32:52.596784Z ERROR process_request:handle_packet{server_service_binding=ServiceBinding { protocol: UDP, bind_address: 127.0.0.1:6868 } connection_id_validation=Strict request_id="fcf58cb0-9978-4132-989b-f4dfb1f58b3a"}:handle_error{req_kind=Some(Announce { announce_request: AnnounceRequest { connection_id: ConnectionId(I64(6588044239080337533)), action_placeholder: AnnounceActionPlaceholder(I32(1)), transaction_id: TransactionId(I32(4321)), info_hash: InfoHash([17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), peer_id: PeerId([45, 88, 88, 48, 48, 48, 49, 45, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]), bytes_downloaded: NumberOfBytes(I64(-1)), bytes_left: NumberOfBytes(I64(0)), bytes_uploaded: NumberOfBytes(I64(0)), event: AnnounceEventBytes(I32(2)), ip_address: Ipv4AddrBytes([0, 0, 0, 0]), key: PeerKey(I32(7)), peers_wanted: NumberOfPeers(I32(-1)), port: Port(U16(6881)) } }) client_socket_addr=127.0.0.1:37907 server_service_binding=ServiceBinding { protocol: UDP, bind_address: 127.0.0.1:6868 } configuration_instance_id=ConfigurationInstanceId { service_role: UdpTracker, instance_index: 0 } public_url=None request_id=fcf58cb0-9978-4132-989b-f4dfb1f58b3a cookie_valid_range=1794501051.5967395..1794501173.5967395 error=AnnounceFailed { source: NegativeNumberOfBytes { field: Downloaded, value: -1 } } opt_transaction_id=Some(TransactionId(I32(4321)))}: UDP TRACKER: response error error=tracker announce error: Invalid announce request: `downloaded` must not be negative, got -1 client_socket_addr=127.0.0.1:37907 server_socket_addr=127.0.0.1:6868 service_binding=udp://127.0.0.1:6868 request_id=fcf58cb0-9978-4132-989b-f4dfb1f58b3a transaction_id=4321
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# skill-link: run-tracker-locally
# skill-link: use-rest-api
[metadata]
app = "torrust-tracker"
purpose = "configuration"
schema_version = "3.0.0"

[logging]
trace_filter = "info"
trace_style = "full"

[core]
inactive_peer_cleanup_interval = 120
listed = false
private = false

[core.database]
driver = "sqlite3"
path = "/tmp/claude-1000/run/sqlite3.db"

[udp_tracker_server]
ip_bans_reset_interval_in_secs = 86400
max_connection_id_errors_per_ip = 10
connection_id_validation = "strict"

# How long the tracker takes to stop after SIGTERM or SIGINT. The values are the defaults.
# `max(http_drain_in_secs, udp_request_drain_in_secs) + event_listener_drain_in_secs` must be
# below `deadline_in_secs`; the tracker refuses to start otherwise.
[application.shutdown]
deadline_in_secs = 25
http_drain_in_secs = 15
udp_request_drain_in_secs = 5
event_listener_drain_in_secs = 5

[core.tracker_policy]
max_peer_timeout = 60
persistent_torrent_completed_stat = true
remove_peerless_torrents = true

[[udp_trackers]]
bind_address = "127.0.0.1:6868"
tracker_usage_statistics = true

[[udp_trackers]]
bind_address = "0.0.0.0:6969"
tracker_usage_statistics = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Disposable probe: send one UDP connect + one announce with a chosen signed `left`.

Usage: python3 -I udp_announce_probe.py <port> <left> [uploaded] [downloaded]
Prints the announce response (action, seeders, leechers) or the error message.
"""
import socket
import struct
import sys

port, left = int(sys.argv[1]), int(sys.argv[2])
uploaded = int(sys.argv[3]) if len(sys.argv) > 3 else 0
downloaded = int(sys.argv[4]) if len(sys.argv) > 4 else 0

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(3)
addr = ("127.0.0.1", port)

s.sendto(struct.pack(">qii", 0x41727101980, 0, 1234), addr)
action, tid, connection_id = struct.unpack(">iiq", s.recvfrom(64)[0][:16])

packet = struct.pack(
">qii20s20sqqqiIiiH", # cspell:disable-line
connection_id, 1, 4321,
b"\x11" * 20, b"-XX0001-000000000000",
downloaded, left, uploaded,
2, # event: started
0, 7, -1, 6881,
)
s.sendto(packet, addr)
data = s.recvfrom(2048)[0]
action, tid = struct.unpack(">ii", data[:8])
if action == 1:
interval, leechers, seeders = struct.unpack(">iii", data[8:20])
print(f"announce response: interval={interval} leechers={leechers} seeders={seeders}")
else:
print(f"action={action} message={data[8:].decode(errors='replace')!r}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
doc-type: manual-verification-evidence
issue-spec: docs/issues/open/2838-1347-udp-reject-negative-announce-bytes/ISSUE.md
last-updated-utc: 2026-11-12 16:40
---

# Manual Verification Evidence

## Environment and Prerequisites

- Date and time (UTC): 2026-11-11 17:28-17:30
- Artifact under test: `develop` plus the `test(udp-server): reject a negative left in an announce (red)` patch
(no production change); debug build of `torrust-tracker` (`cargo build --bin torrust-tracker`).
- Operating system: Linux 7.0.0-43-generic; stable Rust toolchain, rustc 1.99.1.
- Setup: tracker config [`evidence/tracker.toml`](evidence/tracker.toml) (the shipped
`tracker.development.sqlite3.toml` with a temp database, HTTP/API removed, UDP on
`127.0.0.1:6868`); probe [`evidence/udp_announce_probe.py`](evidence/udp_announce_probe.py).

## Verification Processes

### V1 - Negative `left` over UDP (before the fix)

- Goal: observe whether the UDP tracker accepts `left = -5` and counts the peer as a seeder.
- Initial state: fresh tracker, empty database.
- Status: `DONE`
- Outcome: **Reproduced** (the wrong outcome itself was observed).

#### Steps Performed

1. `TORRUST_TRACKER_CONFIG_TOML_PATH=<tracker.toml> ./target/debug/torrust-tracker`
2. For `left` in `1000`, `-5`, `0`:
`python3 -I evidence/udp_announce_probe.py 6868 <left>`
(same info hash, same peer id, `event = started`, so each announce replaces the peer).

#### Observed Result

Full output: [`evidence/probe-before-fix.txt`](evidence/probe-before-fix.txt).

```text
left=1000
announce response: interval=120 leechers=1 seeders=0
left=-5
announce response: interval=120 leechers=0 seeders=1
left=0
announce response: interval=120 leechers=0 seeders=1
```

#### Conclusion

`left = -5` is accepted and the peer is counted as a seeder, identically to `left = 0`. Expected
after the fix: an error response (action 3) and no peer registered.

### V1b - Red regression test at the UDP handler boundary

- Boundary: `udp-server` handler test; it is the smallest deterministic test at the UDP boundary
that runs the real announce service and in-memory repository.
- Command: `cargo test -p torrust-tracker-udp-server --lib negative_number_of_bytes_left`
(stable toolchain). Output: [`evidence/red-test-output.txt`](evidence/red-test-output.txt).

```text
a negative `left` must be rejected, got: Ok(AnnounceIpv4(AnnounceResponse { ... leechers: 0, seeders: 1 ... }))
test result: FAILED. 0 passed; 1 failed
```

### V2 - Final like-for-like recheck (after the fix)

- Status: `DONE`
- Date and time (UTC): 2026-11-12 16:32
- Build: a clean tree (`git status --short` printed nothing before the build) at the commit
`fix(udp-server): [#2838] classify a negative byte count as a bad request`, which is the last
production change and follows `fix(udp-core): [#2838] reject a negative number of bytes in an
announce` and `refactor(udp-core): [#2838] type the rejected byte-count field`;
`cargo build --bin torrust-tracker`, stable Rust toolchain, same config and probe as V1.
- An earlier run of this recheck (2026-11-12 09:12) was made on an uncommitted tree before the
review fixes; it is superseded by this one.

#### Steps Performed

1. `TORRUST_TRACKER_CONFIG_TOML_PATH=evidence/tracker.toml ./target/debug/torrust-tracker`
2. The five probe commands below, one after another (each announce replaces the same peer). The
probe arguments are `<port> <left> [uploaded] [downloaded]`.

#### Observed Result

Probe output with step times, verbatim from
[`evidence/probe-after-fix.txt`](evidence/probe-after-fix.txt):

```text
16:32:52.528 start: python3 -I evidence/udp_announce_probe.py 6868 1000
announce response: interval=120 leechers=1 seeders=0
16:32:52.540 end
16:32:52.543 start: python3 -I evidence/udp_announce_probe.py 6868 -5
action=3 message='tracker announce error: Invalid announce request: `left` must not be negative, got -5'
16:32:52.555 end
16:32:52.557 start: python3 -I evidence/udp_announce_probe.py 6868 0
announce response: interval=120 leechers=0 seeders=1
16:32:52.570 end
16:32:52.572 start: python3 -I evidence/udp_announce_probe.py 6868 0 -1 0
action=3 message='tracker announce error: Invalid announce request: `uploaded` must not be negative, got -1'
16:32:52.585 end
16:32:52.588 start: python3 -I evidence/udp_announce_probe.py 6868 0 0 -1
action=3 message='tracker announce error: Invalid announce request: `downloaded` must not be negative, got -1'
16:32:52.600 end
```

Tracker log: `grep -c "must not be negative"` on the tracker log printed `3`, one line per
rejection. The three lines are in
[`evidence/tracker-log-after-fix.txt`](evidence/tracker-log-after-fix.txt); the first, shortened
(the request dump is elided):

```text
2026-11-12T16:32:52.551837Z ERROR process_request:handle_packet{...}:handle_error{req_kind=Some(Announce { ... bytes_left: NumberOfBytes(I64(-5)) ... }) ... error=AnnounceFailed { source: NegativeNumberOfBytes { field: Left, value: -5 } } opt_transaction_id=Some(TransactionId(I32(4321)))}: UDP TRACKER: response error error=tracker announce error: Invalid announce request: `left` must not be negative, got -5 client_socket_addr=127.0.0.1:37317 server_socket_addr=127.0.0.1:6868 service_binding=udp://127.0.0.1:6868 request_id=8c12f0e4-869e-41a1-9167-cd68cd05c118 transaction_id=4321
```

Green tests on the same tree: `cargo test -p torrust-tracker-udp-core -p torrust-tracker-udp-server --lib`
(stable toolchain): 108 passed and 225 passed, 0 failed.

#### Conclusion

The symptom observed in V1 is gone; valid values (`left` 0 and 1000) behave as before. The UDP
error event classification (`ErrorKind::BadRequest`) is covered by a unit test, not by this run.

## Failures and Follow-up

None.
Loading
Loading