Fresh-handle manual cookies lose their documented priority over file imports

perfloop-oss/curl · Value Kind Confusion

https://perfloop.ai/t/oss/case_2vkh2zac7e

Verdict

VERIFIED · settled 2026-11-14

What happened: The check fails before this change and passes with it.

Hypothesis

A manually inserted cookie can be overwritten by a later file import even though the API documentation promises the manual cookie takes priority. A deterministic native test confirms this before the first transfer on a fresh handle. The prevalence of this operation order in applications is unknown.

The explicit contract is in docs/libcurl/opts/CURLOPT_COOKIELIST.md:110-117: a cookie imported immediately by COOKIELIST is considered live, so a file cookie with the same hostname, path and name is skipped. In cookielist, first use creates a jar through Curl_cookie_init and marks the cookie engine enabled, but the jar's running bit remains false. Curl_cookie_add derives both acceptance provenance and real->livecookie from that lifecycle bit at lib/cookie.c:1173-1182. A jar not yet running does not distinguish a manual cookie from a file cookie. During RELOAD, cookie_load sets running false for the file import, and cookie_accept protects the old cookie only if its livecookie bit is true. The fresh manual cookie therefore loses the promised protection.

The reduced counterexample is tests/unit/unit4996.c with tests/data/test4996, run alone in bash call call_HTiIwab5ulg6P4W28THt1eVv. Its complete deterministic bound is one fresh handle, one manually inserted Netscape-format session cookie, one file containing a cookie with the same identity and a different value, and one explicit RELOAD; it makes no transfer and injects no fault. The exact command was `(cd .hunt-cookie/tests && perl ../../tests/runtests.pl -n -v 4996)`. Output included `LIVE_READBACK example.com\tFALSE\t/\tFALSE\t0\tsid\tfrom-file` and `LIVE_REPLAY reload_rc=0 initial_running=0 initial_live=0 count=1 manual_preserved=0`, followed by `Assertion 'manual && count == 1' FAILED: manual cookie must keep documented priority over file import`. The native test returned 1 and the runner exited 1. It uses curl's native unit assertions, fixture-file section, bundle generator and test runner; no existing repository file was edited.

The requested property is preservation of explicit manual-cookie provenance from the first supported insertion, not an unconditional change to jar lifecycle. A provisional next check compares fresh and preactivated handles, both input formats, repeated manual replacements, and matching versus unrelated file imports. It should also retain deferred file loading and the existing no-transfer cookie-jar write guard: simply setting running early could change persistence behavior beyond this repair. Those checks and their placement are provisional. Successful manual updates should retain priority, while genuinely file-loaded cookies should retain file-import semantics.

The open-PR implementation review on 2026-11-14 found no ownership of this repair. https://github.com/curl/curl/pull/23660 adds SameSite/Partitioned attributes on an older implementation and does not change fresh-handle manual-cookie classification. https://github.com/curl/curl/pull/23394 concerns file-jar write and close errors instead. The bounded subject-index searches at https://curl.se/mail/lib-2026-10/ and https://curl.se/mail/lib-2026-11/ found no matching mechanism. Earlier merged-history coverage remains unknown because the checkout is shallow and remote path-history reads were refused.

Change to test: Preserve explicit manual-cookie provenance independently of whether a transfer has started, so the first COOKIELIST addition is live and keeps its documented priority over file imports. Keep genuine file imports non-live and preserve deferred loading and the no-transfer jar-write guard.

Where it lives

perfloop-oss/curl · lib/setopt.c

Evidence

The check fails before this change and passes with it: `On a fresh libcurl easy handle before any transfer, valid Netscape-format and Set-Cookie-format COOKIELIST session cookies, including repeated manual replacements, keep their latest manual value when RELOAD imports a file cookie with the same domain, path and name.`

Checks: 2 of 2 passed. Verification: no defect found.

Timeline