| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import socket |
| 20 |
import subprocess |
| 21 |
import threading |
| 22 |
|
| 23 |
from rsyncfns import ( |
| 24 |
SCRATCHDIR, claim_ports, makepath, require_tcp, rmtree, rsync_argv, test_fail, |
| 25 |
) |
| 26 |
import rsync_proto as rp |
| 27 |
|
| 28 |
PORT = 12972 |
| 29 |
require_tcp("the pure-Python receiver needs a real TCP socket; run with --use-tcp") |
| 30 |
claim_ports(PORT) |
| 31 |
|
| 32 |
base = SCRATCHDIR / 'checksum-zero-blocklen' |
| 33 |
rmtree(base) |
| 34 |
src = base / 'src' |
| 35 |
makepath(src) |
| 36 |
(src / 'f').write_text('new data\n' * 4096) |
| 37 |
|
| 38 |
lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 39 |
lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 40 |
lsock.bind(('127.0.0.1', PORT)) |
| 41 |
lsock.listen(1) |
| 42 |
lsock.settimeout(30) |
| 43 |
|
| 44 |
state = {} |
| 45 |
|
| 46 |
|
| 47 |
def serve(): |
| 48 |
"""Accept the pushing client and, as the generator, send a zero-blocklen |
| 49 |
sum header for the single pushed file (index 0).""" |
| 50 |
try: |
| 51 |
csock, _ = lsock.accept() |
| 52 |
r = rp.DaemonReceiver(csock) |
| 53 |
r.handshake() |
| 54 |
r.send_sum_request(0, count=1, blength=0, s2length=0, remainder=0) |
| 55 |
r.drain() |
| 56 |
r.close() |
| 57 |
except Exception as exc: |
| 58 |
state['err'] = repr(exc) |
| 59 |
|
| 60 |
|
| 61 |
t = threading.Thread(target=serve, daemon=True) |
| 62 |
t.start() |
| 63 |
try: |
| 64 |
proc = subprocess.run( |
| 65 |
rsync_argv('--no-whole-file', f'{src}/f', f'rsync://127.0.0.1:{PORT}/mod/'), |
| 66 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=30) |
| 67 |
finally: |
| 68 |
t.join(timeout=10) |
| 69 |
lsock.close() |
| 70 |
|
| 71 |
out = (proc.stdout or '') + (proc.stderr or '') |
| 72 |
if state.get('err'): |
| 73 |
test_fail(f"the Python receiver failed before/while feeding the sender: {state['err']}\n" |
| 74 |
f"sender output:\n{out}") |
| 75 |
if proc.returncode == 0: |
| 76 |
test_fail("malicious receiver sent a count>0/blength=0 checksum header " |
| 77 |
f"but the sender completed successfully:\n{out}") |
| 78 |
if 'Invalid zero block length' not in out: |
| 79 |
test_fail("sender rejected the malicious receiver, but not via the zero-block " |
| 80 |
f"checksum guard. Output:\n{out}") |
| 81 |
|
| 82 |
print("checksum-zero-blocklen: sender rejects count>0 checksum headers with blength=0") |