bugger.
All challenges/Pythonhardthe-connection-that-trusted-too-much-v1
THE INVESTIGATION

The connection that trusted too much

A reusable HTTP session accepts an untrusted certificate only after an earlier diagnostic request.

Security boundariesConnection poolingState history

What’s going wrong

A billing client rejects an untrusted TLS certificate in a fresh session. In a long-lived session, running an unchecked diagnostic request first makes the same verified billing request succeed. Updating the request settings appears to work, but the result depends on connection history. Repair session reuse while preserving useful pooling.

This is a deterministic transport simulator: no real HTTP requests or TLS are performed. Stdin is JSON with `trust`, a map from trust-profile names to issuer-name arrays (including `default`), and `events`, processed in order. Every request event contains `op: "request"`, a globally unique string `id`, an HTTP(S) `url`, `verify`, an `issuer` string, and a `hostname_valid` boolean. `verify` is false (unchecked), true (verified with profile `default`), or an existing trust-profile name. URLs have no credentials, valid nonzero ports, and ASCII DNS hosts. Scheme and host are case insensitive; omitted ports mean 80 for HTTP and 443 for HTTPS. URL paths and queries do not identify connections.

For HTTPS, a new verified connection succeeds only when its issuer is in the selected profile and `hostname_valid` is true. Unchecked connections skip both checks. An established connection cannot change the policy of its completed handshake. Connections may be shared only by equal normalized origins AND equal effective TLS policies: unchecked, or verified under the same profile name. True and `"default"` mean the same policy. Distinct profile names stay separate even if their issuer arrays happen to match. For plain HTTP, ignore all TLS settings and share by origin.

A successful request holds its connection until an event `{"op":"release","id":"..."}` for that successful request; each held request is released at most once. Reuse the most recently released idle connection in the matching pool; create a new one only when that pool has none idle. A failed TLS handshake leaves no connection to hold or reuse. Number successfully created connections consecutively from 1 across the session; a rejected handshake does not consume a number. Reuse keeps the number. The issuer and hostname fields model the next handshake only, so reusing an established connection does not revalidate them. Inputs follow this contract.

Print one compact JSON array with one `[request_id, status, connection_number]` record per request. Status is `"ok"` or `"tls_error"`; rejected handshakes use null for the number. Release events produce no record. Empty events produce `[]`.

Expected behavior

A request can reuse only a connection established under its own origin and effective TLS policy. Legitimate reuse, active ownership, failed-handshake handling, and connection numbering remain deterministic.

Reproduction

01 An unchecked connection survives a policy change
{"trust":{"default":["public"],"blue":["blue-root"],"red":["red-root"]},"events":[{"op":"request","id":"probe","url":"https://api.example/data","verify":false,"issuer":"unknown","hostname_valid":true},{"op":"release","id":"probe"},{"op":"request","id":"payment","url":"https://api.example/data","verify":true,"issuer":"unknown","hostname_valid":true}]}
[["probe","ok",1],["payment","tls_error",null]]
02 Matching verified requests reuse their connection
{"trust":{"default":["public"],"blue":["blue-root"],"red":["red-root"]},"events":[{"op":"request","id":"first","url":"https://api.example/data","verify":true,"issuer":"public","hostname_valid":true},{"op":"release","id":"first"},{"op":"request","id":"second","url":"https://api.example/data","verify":true,"issuer":"public","hostname_valid":true}]}
[["first","ok",1],["second","ok",1]]
03 A different trust profile must perform its own handshake
{"trust":{"default":["public"],"blue":["blue-root"],"red":["red-root"]},"events":[{"op":"request","id":"blue","url":"https://api.example/data","verify":"blue","issuer":"blue-root","hostname_valid":true},{"op":"release","id":"blue"},{"op":"request","id":"red","url":"https://api.example/data","verify":"red","issuer":"blue-root","hostname_valid":true}]}
[["blue","ok",1],["red","tls_error",null]]

Before you begin

  • Python dictionaries and tuples
  • Connection pooling and resource ownership
  • TLS verification policy (simulated in the report)

Need a nudge?

Reveal one investigation hint at a time. Try each clue before opening the next.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.