paivana

HTTP paywall reverse proxy
Log | Files | Refs | Submodules | README | LICENSE

commit 6bee010952d01e55ae24368af4d31c91b91c4c13
parent d59094c70e3c3e257ddbc4ba4dd09c5b4615bd2b
Author: Florian Dold <dold@taler.net>
Date:   Thu, 19 Nov 2026 19:15:00 +0100

redemption: issue the access cookie through the paid pickup deadline

Diffstat:
MREADME | 12+++++-------
Msrc/backend/paivana-httpd_cookie.c | 6+++---
Msrc/backend/paivana-httpd_cookie.h | 7+++----
Msrc/backend/paivana-httpd_pay.c | 46+++++++++++++++++-----------------------------
Msrc/tests/README | 10++++++----
Msrc/tests/test_paywall.sh | 32+++++++++++++++++++++++++++++---
6 files changed, 63 insertions(+), 50 deletions(-)

diff --git a/README b/README @@ -36,10 +36,9 @@ How it works with a reference to the unique payment identifier. 6. `paivana-httpd` verifies the payment with the merchant, sets an access cookie, and redirects the browser to the original URL. - The access ends at the `expiration` the browser named in step 3 and - repeated in step 5 — it is hashed into the payment identifier, so the - two must agree — bounded above by the contract's `max_pickup_time`, - which is what the merchant's `max_pickup_delay` on the template sets. + The `expiration` bound into the payment identifier is the minimum + acceptable cookie expiry. The proxy issues the cookie through the paid + order's `max_pickup_time`, which can be later than that minimum. 7. Requests with a valid cookie are forwarded to the upstream server via libcurl. Both directions are streamed: bytes are passed on as they arrive, so the size of a proxied body is not bounded by memory @@ -49,9 +48,8 @@ The cookie carries a keyed hash over `(expiration time, website, client address)` keyed by a `paivana_secret` derived from the configured `SECRET`. The construction is `GNUNET_CRYPTO_hkdf_gnunet()`, which is HMAC-based but is not itself an HMAC. `SECRET` is required whenever the -paywall is on: it is the only input to that hash the client does not -supply, so a key invented afresh at each start would invalidate every -access anyone had already paid for. Under `-n` no cookie is ever minted +paywall is on. Keeping it stable across restarts preserves the validity +of cookies already issued. Under `-n` no cookie is ever minted and none is needed. Keep the same `SECRET` on every `paivana-httpd` serving one site — a diff --git a/src/backend/paivana-httpd_cookie.c b/src/backend/paivana-httpd_cookie.c @@ -434,7 +434,7 @@ PAIVANA_HTTPD_compute_cookie (struct GNUNET_TIME_Timestamp expiration, char * -PAIVANA_HTTPD_compute_paivana_id (struct GNUNET_TIME_Timestamp expiration, +PAIVANA_HTTPD_compute_paivana_id (struct GNUNET_TIME_Timestamp min_expiration, const char *website, const struct PAIVANA_Nonce *nonce) { @@ -445,7 +445,7 @@ PAIVANA_HTTPD_compute_paivana_id (struct GNUNET_TIME_Timestamp expiration, char *cstr; size_t clen; - e = GNUNET_TIME_absolute_hton (expiration.abs_time); + e = GNUNET_TIME_absolute_hton (min_expiration.abs_time); GNUNET_assert (0 == gcry_md_open (&hd, GCRY_MD_SHA256, @@ -468,7 +468,7 @@ PAIVANA_HTTPD_compute_paivana_id (struct GNUNET_TIME_Timestamp expiration, GNUNET_asprintf ( &res, "%llu-%.*s", - (unsigned long long) (expiration.abs_time.abs_value_us / 1000LLU / 1000LLU), + (unsigned long long) (min_expiration.abs_time.abs_value_us / 1000LLU / 1000LLU), (int) clen, cstr); GNUNET_free (cstr); diff --git a/src/backend/paivana-httpd_cookie.h b/src/backend/paivana-httpd_cookie.h @@ -93,17 +93,16 @@ PAIVANA_HTTPD_compute_cookie (struct GNUNET_TIME_Timestamp expiration, /** - * Compute the Paivana ID for the given @a expiration, + * Compute the Paivana ID for the given @a min_expiration, * @a website and @a nonce. * - * @param expiration end of access, as chosen by the client and bounded - * by the contract's `max_pickup_time` + * @param min_expiration minimum acceptable cookie expiry * @param website website to be accessed * @param nonce client-selected nonce * @return corresponding Paivana ID. */ char * -PAIVANA_HTTPD_compute_paivana_id (struct GNUNET_TIME_Timestamp expiration, +PAIVANA_HTTPD_compute_paivana_id (struct GNUNET_TIME_Timestamp min_expiration, const char *website, const struct PAIVANA_Nonce *nonce); diff --git a/src/backend/paivana-httpd_pay.c b/src/backend/paivana-httpd_pay.c @@ -153,12 +153,9 @@ struct PayRequest struct PAIVANA_Nonce nonce; /** - * End of the access the client is redeeming: the expiration of the - * cookie we mint, and one of the three inputs the client hashed into - * the paivana_id the order was created under. Chosen by the client - * and bounded above by the contract's `max_pickup_time'. + * Minimum acceptable cookie expiry bound into the session ID. */ - struct GNUNET_TIME_Timestamp expiration; + struct GNUNET_TIME_Timestamp min_expiration; /** * HTTP status to return in combination with @e response to the @@ -487,7 +484,7 @@ queue_fresh_diagnostic (const struct PayRequest *ph) GNUNET_asprintf (&path, "private/orders/%s", ph->order_id); - paivana_id = PAIVANA_HTTPD_compute_paivana_id (ph->expiration, + paivana_id = PAIVANA_HTTPD_compute_paivana_id (ph->min_expiration, ph->website, &ph->nonce); md = GNUNET_new (struct MerchantTransportDiagnostic); @@ -765,7 +762,7 @@ start_merchant_order_lookup (struct PayRequest *ph, ph->order_id); if (NULL == ph->co) return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; - paivana_id = PAIVANA_HTTPD_compute_paivana_id (ph->expiration, + paivana_id = PAIVANA_HTTPD_compute_paivana_id (ph->min_expiration, ph->website, &ph->nonce); ret = TALER_MERCHANT_get_private_order_set_options ( @@ -859,12 +856,14 @@ reply_with_merchant_error (struct MHD_Connection *connection, * * @param[in,out] ph request to check * @param contract contract to check + * @param[out] cookie_expiration access deadline of the paid contract * @return true if the contract is good for the request, * false if not and thus a response object was created in @a ph */ static bool check_contract (struct PayRequest *ph, - const json_t *contract) + const json_t *contract, + struct GNUNET_TIME_Timestamp *cookie_expiration) { struct GNUNET_TIME_Timestamp max_time = GNUNET_TIME_UNIT_FOREVER_TS; @@ -932,7 +931,10 @@ check_contract (struct PayRequest *ph, ph->response_status = MHD_HTTP_CONFLICT; return false; } - if (GNUNET_TIME_timestamp_cmp (ph->expiration, + if (GNUNET_TIME_timestamp_cmp (max_time, + <=, + GNUNET_TIME_timestamp_get ()) || + GNUNET_TIME_timestamp_cmp (ph->min_expiration, >, max_time)) { @@ -943,6 +945,7 @@ check_contract (struct PayRequest *ph, ph->response_status = MHD_HTTP_GONE; return false; } + *cookie_expiration = max_time; return true; } @@ -1066,9 +1069,11 @@ order_status_cb (struct PayRequest *ph, size_t ca_len = 0; char *cookie; struct MHD_Response *resp; + struct GNUNET_TIME_Timestamp cookie_expiration; if (! check_contract (ph, - osr->details.ok.details.paid.contract_terms)) + osr->details.ok.details.paid.contract_terms, + &cookie_expiration)) { GNUNET_free (elapsed_s); GNUNET_free (timeout_s); @@ -1096,7 +1101,7 @@ order_status_cb (struct PayRequest *ph, ph->response_status = MHD_HTTP_INTERNAL_SERVER_ERROR; break; } - cookie = PAIVANA_HTTPD_compute_cookie (ph->expiration, + cookie = PAIVANA_HTTPD_compute_cookie (cookie_expiration, ph->website, ca_len, ca); @@ -1334,7 +1339,7 @@ PAIVANA_HTTPD_payment_handle (struct PayRequest *ph, TALER_JSON_spec_web_url ("website", &ph->website), GNUNET_JSON_spec_timestamp ("expiration", - &ph->expiration), + &ph->min_expiration), GNUNET_JSON_spec_fixed_auto ("nonce", &ph->nonce), GNUNET_JSON_spec_end () @@ -1366,23 +1371,6 @@ PAIVANA_HTTPD_payment_handle (struct PayRequest *ph, TALER_EC_GENERIC_PARAMETER_MALFORMED, "website"); } - /* `expiration' is the end of the access being bought, not a - statement about the client's clock: it is what the cookie's - Max-Age is computed from, what check_cookie() enforces, and what - is hashed into the paivana_id the order was created under, so we - cannot re-derive it here even if we wanted to. An expiration in - the past would mint a cookie that is dead on arrival; the upper - bound is the contract's `max_pickup_time', enforced in - check_contract() once we have the contract to compare against. */ - if (GNUNET_TIME_absolute_is_past (ph->expiration.abs_time)) - { - GNUNET_break_op (0); - return TALER_MHD_reply_with_error ( - ph->connection, - MHD_HTTP_BAD_REQUEST, - TALER_EC_GENERIC_PARAMETER_MALFORMED, - "expiration"); - } } GNUNET_CONTAINER_DLL_insert (ph_head, ph_tail, diff --git a/src/tests/README b/src/tests/README @@ -502,7 +502,7 @@ the client and "was it throttled" stops having a stable answer. The base64url cross-check (`test_base64url.sh`) ----------------------------------------------- -The paivana ID is `<expiration>-<base64url(sha256(...))>`. The daemon +The paivana ID is `<min_expiration_str>-<base64url(sha256(...))>`. The daemon builds it with `GNUNET_STRINGS_base64url_encode()`; the browser rebuilds it in `paywall.js` to recognise the payment it has just made. Nothing in either program forces the two encoders to agree, and if they do not, @@ -657,7 +657,7 @@ nowhere else. The `paivana_id` is pinned against a golden vector computed independently from the definition the paywall page implements (src/frontend/paywall.js, makePaivanaId()). Neither side ever sends -it; both derive it from their own copy of (nonce, website, expiration) +it; both derive it from their own copy of (nonce, website, min_expiration) and expect the other to have got the same string, so the two implementations agreeing IS the protocol, and a change on either side that this vector does not survive means every order is created under @@ -964,7 +964,7 @@ through `PAIVANA_PREFIX`, so a build tree is enough and `make install` is not required. Why the client half is written out by hand. The paywall page computes -a payment identifier from (nonce, website, expiration) and the daemon +a payment identifier from (nonce, website, min_expiration) and the daemon computes the same identifier independently; neither ever sends it to the other, so the two agreeing IS the protocol. `paivana_id.py` re-derives it -- and the Crockford base32 encoding of the nonce -- from @@ -1001,7 +1001,9 @@ What it covers, in order: * that redemption is repeatable from anywhere, which is deliberate (design document 076, "Payment buys access, not a seat"). The check is here so that a change of mind about it surfaces as a test - failure rather than as a silent change of policy. + failure rather than as a silent change of policy; + * that a fresh session can reuse the original order and receive a cookie + through its paid deadline, even when the session requested less time. The checks were verified not to be vacuous by breaking the code under them, one property at a time: dropping the `^(...)$` wrapping around diff --git a/src/tests/test_paywall.sh b/src/tests/test_paywall.sh @@ -310,7 +310,7 @@ STATUS=$(merchant_post private/templates \ "template_contract":{"template_type":"paivana", "summary":"Access to the article", "website_regex":".*", - "max_pickup_duration":{"d_us":3600000000}, + "max_pickup_duration":{"d_us":7200000000}, "choices":[{"amount":"TESTKUDOS:1"}]}}') [ "$STATUS" = "204" ] || exit_fail "creating the template: got $STATUS, $(cat "$RESP")" echo " OK" @@ -551,8 +551,7 @@ timeout 240 taler-wallet-cli --no-throttle --wallet-db="$WALLET" run-until-done echo " OK" WEBSITE="${BASE_URL}hello" -# The page computes this as `now + max_pickup_duration'; one hour is what -# the template above allows. +# The page asks for one hour. A new order grants two hours. EXPIRATION=$(( $(date +%s) + 3600 )) read -r NONCE PAIVANA_ID < <(python3 "$SRCDIR/paivana_id.py" "$EXPIRATION" "$WEBSITE") echo " paivana_id: $PAIVANA_ID" @@ -695,6 +694,16 @@ else fail "no Paivana-Cookie in the redemption response" fi +PAID_UNTIL=$(curl -fsS -H "$AUTH" "${MERCHANT_URL}private/orders/$ORDER_ID" | + jq -er '.contract_terms.max_pickup_time.t_s') +if [ "$PAID_UNTIL" -gt "$EXPIRATION" ] && + [[ "$COOKIE" == "Paivana-Cookie=$PAID_UNTIL-"* ]] +then + ok "the cookie grants access through the paid deadline beyond the requested hour" +else + fail "the cookie did not grant access through the contract's pickup deadline" +fi + if grep -qi '^set-cookie:.*HttpOnly' "$SCRATCH/redeem.hdr" then ok "the access cookie is HttpOnly" @@ -867,6 +876,23 @@ else fail "redeeming a second time gave $STATUS; if that is intended, 076 needs updating" fi +# The merchant now associates the paid order with the fresh browser session. +FRESH_MIN=$(( $(date +%s) + 3600 )) +read -r FRESH_NONCE FRESH_ID < <(python3 "$SRCDIR/paivana_id.py" "$FRESH_MIN" "$WEBSITE") +FRESH_URI="taler+http://pay-template/localhost:$MERCHANT_PORT/premium" +FRESH_URI="$FRESH_URI?session_id=$(urlenc "$FRESH_ID")&fulfillment_url=$(urlenc "$WEBSITE")" +if timeout 30 taler-wallet-cli --no-throttle --wallet-db="$WALLET" \ + handle-uri "$FRESH_URI" </dev/null > "$SCRATCH/replay.out" 2> "$SCRATCH/replay.err" && + curl -fsS "${MERCHANT_URL}sessions/$FRESH_ID?fulfillment_url=$(urlenc "$WEBSITE")&timeout_ms=1000" | + jq -e --arg original "$ORDER_ID" '.order_id == $original' >/dev/null && + [ "$(redeem "$WEBSITE" "$FRESH_MIN" "$FRESH_NONCE" "$ORDER_ID" "$SCRATCH/replay.hdr")" = 303 ] && + grep -qi "^Set-Cookie: Paivana-Cookie=$PAID_UNTIL-" "$SCRATCH/replay.hdr" +then + ok "a fresh QR recovers the original paid access without another payment" +else + fail "the fresh session did not redeem the original order through its paid deadline" +fi + echo "=== $CHECKS checks, $FAILS failure(s) ===" if [ "$FAILS" -gt 0 ] then