~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
61 lines · 2.3 KB Raw History
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.12"
# dependencies = []
# ///
"""Bodies past the server's lazy-read size must be refused, not crash it.

httpz buffers only part of a body at or past lazy_read_size yet reports its
full length; reading it with request.body() sliced past the buffered bytes and
panicked the whole server. Each route that reads a small JSON body gets one
sized and one chunked oversized body, then the server must still answer.
"""
import socket
import sys
import urllib.request

ROUTES = [
    "/xrpc/com.atproto.server.createSession",
    "/xrpc/com.atproto.server.createInviteCode",
    "/xrpc/com.atproto.server.createInviteCodes",
    "/xrpc/com.atproto.admin.updateSubjectStatus",
    "/xrpc/com.atproto.admin.deleteAccount",
    "/xrpc/com.atproto.server.deleteAccount",
    "/xrpc/com.atproto.server.confirmEmail",
    "/xrpc/com.atproto.server.updateEmail",
]


def post(host, port, path, size, chunked):
    body = b"{" + b" " * (size - 2) + b"}"
    s = socket.create_connection((host, port), timeout=10)
    if chunked:
        s.sendall(f"POST {path} HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n".encode())
        for i in range(0, len(body), 16384):
            part = body[i:i + 16384]
            s.sendall(f"{len(part):x}\r\n".encode() + part + b"\r\n")
        s.sendall(b"0\r\n\r\n")
    else:
        s.sendall(f"POST {path} HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\nContent-Length: {len(body)}\r\nConnection: close\r\n\r\n".encode())
        try:
            s.sendall(body)
        except OSError:
            pass  # the server may answer and close before reading it all
    status = s.recv(64).split(b"\r\n", 1)[0].decode()
    s.close()
    return status


def main():
    host, port = sys.argv[1], int(sys.argv[2])
    for path in ROUTES:
        for size in (70_000, 2_000_000):
            for chunked in (False, True):
                status = post(host, port, path, size, chunked)
                assert status.startswith("HTTP/1.1 4"), (path, size, chunked, status)
    with urllib.request.urlopen(f"http://{host}:{port}/xrpc/_health", timeout=10) as res:
        assert res.status == 200
    print(f"large bodies: {len(ROUTES) * 4} oversized bodies refused, server still up")


main()