test_paywall.sh (34118B)
1 #!/usr/bin/env bash 2 # This file is part of GNU Taler 3 # Copyright (C) 2025, 2026 Taler Systems SA 4 # 5 # GNU Taler is free software; you can redistribute it and/or modify it under 6 # the terms of the GNU Affero General Public License as published by the Free 7 # Software Foundation; either version 3, or (at your option) any later 8 # version. 9 # 10 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 12 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for 13 # more details. 14 # 15 # You should have received a copy of the GNU Affero General Public License 16 # along with GNU Taler; see the file COPYING. If not, see 17 # <http://www.gnu.org/licenses/> 18 # 19 # 20 # The paywall, against a real merchant backend. 21 # 22 # test_reverse_proxy.sh runs paivana-httpd with `-n', which switches the 23 # paywall off before the first byte of a request is looked at, so it 24 # exercises the proxy and nothing else. Everything paivana exists for -- 25 # the paywall page, the payment, the access cookie -- is on the other side 26 # of that flag and had no automated coverage at all. 27 # 28 # This test puts a real GNU Taler system behind paivana-httpd with 29 # taler-unified-setup.sh (a fakebank, an exchange and a merchant backend, 30 # the same way merchant and anastasis test their own payment paths), buys 31 # access with taler-wallet-cli, and checks what the daemon does with it. 32 # 33 # The wallet payment uses a hand-written client. When Chromium and Playwright 34 # are available, a separate browser check exercises the page's polling UI. 35 # The paywall page's JavaScript computes a payment identifier from 36 # (nonce, website, expiration) and the daemon computes the same identifier 37 # independently, so the two agreeing IS the protocol -- it is never sent 38 # from one to the other. paivana_id.py re-derives it from the definition 39 # src/frontend/paywall.js implements, which is what makes this a test of 40 # both ends rather than of one end twice. 41 # 42 # Set PAIVANA_REPRO_MERCHANT_POOL_FAILURE=1 to pay a genuine order and then 43 # reproduce the production status-zero failure through merchant_fault_proxy.py 44 # instead of completing the successful redemption checks. 45 46 set -eu 47 48 # shellcheck source=src/tests/setup.sh 49 . "$(cd -- "$(dirname -- "$0")" && pwd)/setup.sh" 50 51 # Ports of the Taler system come from test_paywall.conf and are fixed; 52 # paivana's own move as a block, exactly as in test_reverse_proxy.sh. 53 PORT_BASE="${PAIVANA_PORT_BASE:-18400}" 54 PAIVANA_PORT=$((PORT_BASE + 110)) 55 ORIGIN_PORT=$((PORT_BASE + 111)) 56 MERCHANT_PROXY_PORT=$((PORT_BASE + 112)) 57 MERCHANT_PORT=9966 58 EXCHANGE_PORT=8081 59 BANK_PORT=8082 60 FAULT_REPRO="${PAIVANA_REPRO_MERCHANT_POOL_FAILURE:-0}" 61 62 function here() { 63 cd -- "$(dirname -- "$0")" && pwd 64 } 65 66 # Same convention as test_reverse_proxy.sh: meson passes both, and the 67 # defaults let the script be run by hand from either tree. 68 SRCDIR="${SRCDIR:-$(here)}" 69 BUILDDIR="${BUILDDIR:-$PWD}" 70 71 PAIVANA_HTTPD="${PAIVANA_HTTPD:-$BUILDDIR/../backend/paivana-httpd}" 72 UPSTREAM_MHD="${UPSTREAM_MHD:-$BUILDDIR/upstream_mhd}" 73 74 CHECKS=0 75 FAILS=0 76 77 function ok() { 78 CHECKS=$((CHECKS + 1)) 79 echo " OK: $1" 80 } 81 82 function fail() { 83 CHECKS=$((CHECKS + 1)) 84 FAILS=$((FAILS + 1)) 85 printf ' FAIL: %b\n' "$1" >&2 86 } 87 88 # Is a TCP port free? Same probe as test_reverse_proxy.sh: bash's 89 # /dev/tcp in a subshell, so a failed connect cannot kill the script. 90 function port_is_free() { 91 ! (exec 3<>"/dev/tcp/127.0.0.1/$1") 2>/dev/null 92 } 93 94 function wait_for_port() { 95 local port="$1" pid="${2:-}" tries=100 96 while [ "$tries" -gt 0 ] 97 do 98 if ! port_is_free "$port" 99 then 100 return 0 101 fi 102 # Probe first: a child that handed the listening socket on and 103 # exited is still a service that came up. 104 if [ -n "$pid" ] && ! kill -0 "$pid" 2>/dev/null 105 then 106 return 1 107 fi 108 sleep 0.1 109 tries=$((tries - 1)) 110 done 111 return 1 112 } 113 114 # -------------------------------------------------------------------- 115 # Preconditions. Every one of these is an environment problem rather 116 # than a regression, so each exits 77 naming what is missing. 117 # -------------------------------------------------------------------- 118 119 echo -n "Checking for curl ..." 120 curl --version >/dev/null 2>&1 || exit_skip "no curl" 121 echo " OK" 122 echo -n "Checking for jq ..." 123 jq --version >/dev/null 2>&1 || exit_skip "no jq" 124 echo " OK" 125 echo -n "Checking for python3 ..." 126 python3 --version >/dev/null 2>&1 || exit_skip "no python3" 127 echo " OK" 128 echo -n "Checking for taler-unified-setup.sh ..." 129 command -v taler-unified-setup.sh >/dev/null 2>&1 \ 130 || exit_skip "no taler-unified-setup.sh (the Taler stack is not installed)" 131 echo " OK" 132 echo -n "Checking for taler-wallet-cli ..." 133 command -v taler-wallet-cli >/dev/null 2>&1 || exit_skip "no taler-wallet-cli" 134 echo " OK" 135 echo -n "Checking for taler-merchant-httpd ..." 136 command -v taler-merchant-httpd >/dev/null 2>&1 || exit_skip "no taler-merchant-httpd" 137 echo " OK" 138 echo -n "Checking for PostgreSQL ..." 139 pg_isready >/dev/null 2>&1 || exit_skip "PostgreSQL is not accepting connections" 140 echo " OK" 141 echo -n "Checking for paivana-httpd ..." 142 [ -x "$PAIVANA_HTTPD" ] || exit_skip "no paivana-httpd at $PAIVANA_HTTPD" 143 echo " OK" 144 echo -n "Checking for the test upstream ..." 145 [ -x "$UPSTREAM_MHD" ] || exit_skip "no upstream_mhd at $UPSTREAM_MHD" 146 echo " OK" 147 148 # The paywall page is a template paivana loads from its installation 149 # prefix, so a build tree alone is not enough. Rather than demanding 150 # `make install', stage the one file into a prefix of our own and point 151 # PAIVANA_PREFIX at it -- the same variable paivana's project data 152 # declares for exactly this. 153 PAYWALL_TEMPLATE="" 154 for cand in "$BUILDDIR/../frontend/paywall.en.must" \ 155 "$SRCDIR/../frontend/paywall.en.must" 156 do 157 if [ -r "$cand" ] 158 then 159 PAYWALL_TEMPLATE="$cand" 160 break 161 fi 162 done 163 if [ -z "$PAYWALL_TEMPLATE" ] 164 then 165 exit_skip "the paywall template has not been built" 166 fi 167 168 echo -n "Checking that the ports are free ..." 169 BUSY="" 170 for p in "$PAIVANA_PORT" "$ORIGIN_PORT" "$MERCHANT_PORT" "$EXCHANGE_PORT" "$BANK_PORT" 171 do 172 port_is_free "$p" || BUSY="$BUSY $p" 173 done 174 if [ "$FAULT_REPRO" = 1 ] && ! port_is_free "$MERCHANT_PROXY_PORT" 175 then 176 BUSY="$BUSY $MERCHANT_PROXY_PORT" 177 fi 178 if [ -n "$BUSY" ] 179 then 180 # The Taler ports are the ones the merchant suite uses, so this is 181 # most often another Taler test running, not a leftover. paivana's 182 # own two move with PAIVANA_PORT_BASE. 183 exit_skip "port(s) already in use:$BUSY" 184 fi 185 echo " OK" 186 187 # -------------------------------------------------------------------- 188 # Scratch space. NOT named TMPDIR: that is the variable every child 189 # reads for its own temporary files, and cleanup removes this directory 190 # while the Taler services may still be running out of it. 191 # -------------------------------------------------------------------- 192 193 SCRATCH="$(mktemp -d -t paivana-paywall.XXXXXX)" 194 PIDS="" 195 196 function stop_children() { 197 local pid 198 for pid in $PIDS 199 do 200 kill -TERM "$pid" 2>/dev/null || true 201 done 202 for pid in $PIDS 203 do 204 wait "$pid" 2>/dev/null || true 205 done 206 PIDS="" 207 } 208 209 function paywall_cleanup() { 210 stop_children 211 # exit_cleanup (from setup.sh) stops taler-unified-setup.sh. 212 exit_cleanup 213 if [ -z "${KEEP_TMP:-}" ] 214 then 215 rm -rf "$SCRATCH" 216 else 217 echo "Keeping $SCRATCH" >&2 218 fi 219 } 220 trap paywall_cleanup EXIT 221 222 mkdir -p "$SCRATCH/prefix/share/paivana/templates" 223 cp "$PAYWALL_TEMPLATE" "$SCRATCH/prefix/share/paivana/templates/" 224 export PAIVANA_PREFIX="$SCRATCH/prefix/" 225 226 # -------------------------------------------------------------------- 227 # The Taler system. 228 # -------------------------------------------------------------------- 229 230 CONF="$SRCDIR/test_paywall.conf" 231 [ -r "$CONF" ] || exit_fail "cannot read $CONF" 232 233 # taler-unified-setup.sh resolves TALER_TEST_HOME relative to the working 234 # directory and writes into it, so run it from the scratch directory with 235 # an absolute path to the configuration. 236 CONF="$(cd "$(dirname "$CONF")" && pwd)/$(basename "$CONF")" 237 cd "$SCRATCH" 238 239 # Give the exchange and the merchant a database of their own. Reusing 240 # `talercheck' would have this test and the merchant's own tests 241 # clobbering each other's tables. 242 DB=paivanacheck 243 if ! psql -Aqt -c "SELECT 1" "$DB" >/dev/null 2>&1 244 then 245 createdb "$DB" >/dev/null 2>&1 \ 246 || exit_skip "cannot create the PostgreSQL database $DB" 247 fi 248 249 setup -c "$CONF" \ 250 -r "merchant-exchange-default" \ 251 -em \ 252 -f -d x-taler-bank -u exchange-account-2 253 254 MERCHANT_URL="http://localhost:$MERCHANT_PORT/" 255 PAIVANA_MERCHANT_URL="$MERCHANT_URL" 256 AUTH="Authorization: Bearer secret-token:super_secret" 257 RESP="$SCRATCH/response.json" 258 259 function merchant_post() { 260 local path="$1" body="$2" 261 curl -s -o "$RESP" -w "%{http_code}" \ 262 -H "Content-Type: application/json" \ 263 -H "$AUTH" \ 264 -X POST \ 265 "${MERCHANT_URL}${path}" \ 266 -d "$body" 267 } 268 269 echo -n "Configuring merchant instance ..." 270 STATUS=$(merchant_post management/instances \ 271 '{"auth":{"method":"token","token":"secret-token:super_secret"}, 272 "id":"admin","name":"default","user_type":"business", 273 "address":{},"jurisdiction":{},"use_stefan":true, 274 "default_wire_transfer_delay":{"d_us":50000000000}, 275 "default_pay_delay":{"d_us":60000000000}}') 276 [ "$STATUS" = "204" ] || exit_fail "creating the instance: got $STATUS, $(cat "$RESP")" 277 echo " OK" 278 279 if [ "$FAULT_REPRO" = 1 ] 280 then 281 echo -n "Starting the merchant fault frontend ..." 282 python3 "$SRCDIR/merchant_fault_proxy.py" \ 283 --listen-port "$MERCHANT_PROXY_PORT" \ 284 --upstream-port "$MERCHANT_PORT" \ 285 --control-file "$SCRATCH/merchant-fault.control" \ 286 > "$SCRATCH/merchant-proxy.log" 2>&1 & 287 MERCHANT_PROXY_PID=$! 288 PIDS="$PIDS $MERCHANT_PROXY_PID" 289 wait_for_port "$MERCHANT_PROXY_PORT" "$MERCHANT_PROXY_PID" \ 290 || exit_fail "the merchant fault frontend did not start" 291 PAIVANA_MERCHANT_URL="http://127.0.0.1:$MERCHANT_PROXY_PORT/" 292 echo " OK" 293 fi 294 295 echo -n "Configuring merchant bank account ..." 296 STATUS=$(merchant_post private/accounts \ 297 '{"payto_uri":"payto://x-taler-bank/localhost/fortythree?receiver-name=fortythree"}') 298 [ "$STATUS" = "200" ] || exit_fail "adding the account: got $STATUS, $(cat "$RESP")" 299 echo " OK" 300 301 # website_regex is `.*' so that every URL is paywalled and WHITELIST is 302 # the only exemption; that is what lets the whitelist cases below mean 303 # what they say. With a narrower expression a URL matching no template 304 # is served for free, which would make a whitelist miss indistinguishable 305 # from a template miss. 306 echo -n "Creating the Paivana template ..." 307 STATUS=$(merchant_post private/templates \ 308 '{"template_id":"premium", 309 "template_description":"Paywalled content", 310 "template_contract":{"template_type":"paivana", 311 "summary":"Access to the article", 312 "website_regex":".*", 313 "max_pickup_duration":{"d_us":7200000000}, 314 "choices":[{"amount":"TESTKUDOS:1"}]}}') 315 [ "$STATUS" = "204" ] || exit_fail "creating the template: got $STATUS, $(cat "$RESP")" 316 echo " OK" 317 318 # -------------------------------------------------------------------- 319 # The origin and paivana itself. 320 # -------------------------------------------------------------------- 321 322 BASE_URL="http://localhost:$PAIVANA_PORT/" 323 324 cat > "$SCRATCH/paivana.conf" <<EOF 325 [paivana] 326 DESTINATION_BASE_URL = http://localhost:$ORIGIN_PORT/ 327 BASE_URL = $BASE_URL 328 MERCHANT_BACKEND_URL = $PAIVANA_MERCHANT_URL 329 MERCHANT_ACCESS_TOKEN = secret-token:super_secret 330 # A fixed secret so that a cookie minted here can be reasoned about; a 331 # real deployment must not do this (see the manual on SECRET). 332 SECRET = paivana-integration-test-secret 333 WHITELIST = /echo-headers|/large/.* 334 SERVE = tcp 335 PORT = $PAIVANA_PORT 336 EOF 337 338 echo -n "Starting the origin ..." 339 "$UPSTREAM_MHD" "$ORIGIN_PORT" > "$SCRATCH/origin.log" 2>&1 & 340 ORIGIN_PID=$! 341 PIDS="$PIDS $ORIGIN_PID" 342 wait_for_port "$ORIGIN_PORT" "$ORIGIN_PID" \ 343 || exit_fail "the test origin did not start on port $ORIGIN_PORT" 344 echo " OK" 345 346 echo -n "Starting paivana-httpd ..." 347 "$PAIVANA_HTTPD" -c "$SCRATCH/paivana.conf" -L INFO > "$SCRATCH/paivana.log" 2>&1 & 348 PAIVANA_PID=$! 349 PIDS="$PIDS $PAIVANA_PID" 350 # paivana does not listen until it has fetched the templates, so this 351 # also asserts that the merchant handed them over. 352 wait_for_port "$PAIVANA_PORT" "$PAIVANA_PID" \ 353 || exit_fail "paivana-httpd did not start:\n$(cat "$SCRATCH/paivana.log")" 354 echo " OK" 355 356 # -------------------------------------------------------------------- 357 # What the paywall does before anyone has paid. 358 # -------------------------------------------------------------------- 359 360 echo "-- before payment --" 361 362 STATUS=$(curl -s -o /dev/null -w "%{http_code}" "${BASE_URL}hello") 363 if [ "$STATUS" = "302" ] 364 then 365 ok "an unpaid URL is redirected to the paywall" 366 else 367 fail "an unpaid URL should be 302, got $STATUS" 368 fi 369 370 curl -s -D "$SCRATCH/redirect.hdr" -o /dev/null "${BASE_URL}hello" 371 LOCATION=$(grep -i '^location:' "$SCRATCH/redirect.hdr" | tr -d '\r' | cut -d' ' -f2-) 372 case "$LOCATION" in 373 *"/.well-known/paivana/templates/premium#"*) 374 ok "the redirect names the template and carries the website in the fragment" 375 ;; 376 *) 377 fail "unexpected paywall redirect: $LOCATION" 378 ;; 379 esac 380 381 # The fragment is the website, base64url-encoded; the page's JavaScript 382 # atob()s it, so a '-' or '_' in it has to survive. 383 FRAGMENT="${LOCATION#*#}" 384 DECODED=$(python3 -c ' 385 import base64, sys 386 s = sys.argv[1] 387 print(base64.urlsafe_b64decode(s + "=" * (-len(s) % 4)).decode("utf-8"))' "$FRAGMENT") 388 if [ "$DECODED" = "${BASE_URL}hello" ] 389 then 390 ok "the fragment decodes to the URL that was asked for" 391 else 392 fail "fragment decodes to '$DECODED', want '${BASE_URL}hello'" 393 fi 394 395 PAGE_STATUS=$(curl -s -D "$SCRATCH/page.hdr" -o "$SCRATCH/page.html" \ 396 -w "%{http_code}" \ 397 "${BASE_URL}.well-known/paivana/templates/premium") 398 if [ "$PAGE_STATUS" = "402" ] 399 then 400 ok "the paywall page is served with 402 Payment Required" 401 else 402 fail "the paywall page should be 402, got $PAGE_STATUS" 403 fi 404 405 PAIVANA_HEADER=$(grep -i '^paivana:' "$SCRATCH/page.hdr" | tr -d '\r' | cut -d' ' -f2-) 406 if [ "$FAULT_REPRO" = 1 ] 407 then 408 EXPECTED_PAY_TEMPLATE="taler+http://pay-template/127.0.0.1:$MERCHANT_PROXY_PORT/premium" 409 else 410 EXPECTED_PAY_TEMPLATE="taler+http://pay-template/localhost:$MERCHANT_PORT/premium" 411 fi 412 case "$PAIVANA_HEADER" in 413 "$EXPECTED_PAY_TEMPLATE") 414 ok "the Paivana header carries the pay-template URI" 415 ;; 416 *) 417 fail "unexpected Paivana header: '$PAIVANA_HEADER'" 418 ;; 419 esac 420 421 if grep -qi "^content-security-policy:.*default-src 'none'" "$SCRATCH/page.hdr" && 422 ! grep -qi "^content-security-policy:.*img-src[^;]*data:" "$SCRATCH/page.hdr" 423 then 424 ok "the paywall CSP defaults to 'none' and blocks data: images" 425 else 426 fail "the paywall CSP must default to 'none' and block data: images" 427 fi 428 429 if python3 "$SRCDIR/test_paywall_poll.py" "${BASE_URL}hello"; then 430 ok "the browser recovers from temporary polling and redemption failures" 431 elif [ "$?" -ne 77 ]; then 432 fail "paywall polling and redemption in the browser" 433 fi 434 435 # -------------------------------------------------------------------- 436 # The whitelist, which only exists on this side of the paywall. 437 # 438 # README and paivana.conf(5) both promise that a WHITELIST expression is 439 # matched against the whole path and anchored at both ends, so that 440 # `/free/' waives nothing while `/free/.*' waives a subtree. The 441 # regexec that decides it sits behind the paywall `-n' switches off, so 442 # no other test in the tree can reach these. 443 # -------------------------------------------------------------------- 444 445 echo "-- the whitelist --" 446 447 function expect_forwarded() { 448 local path="$1" what="$2" status 449 status=$(curl -s -D "$SCRATCH/w.hdr" -o /dev/null -w "%{http_code}" "${BASE_URL}${path}") 450 if [ "302" = "$status" ] 451 then 452 fail "$what: /$path was paywalled (302)" 453 elif grep -qi '^x-upstream:' "$SCRATCH/w.hdr" 454 then 455 ok "$what" 456 else 457 fail "$what: /$path answered $status but not by the origin" 458 fi 459 } 460 461 function expect_paywalled() { 462 local path="$1" what="$2" status 463 status=$(curl -s -o /dev/null -w "%{http_code}" "${BASE_URL}${path}") 464 if [ "302" = "$status" ] 465 then 466 ok "$what" 467 else 468 fail "$what: /$path answered $status, want 302" 469 fi 470 } 471 472 expect_forwarded "echo-headers" "a whitelisted path is served without payment" 473 expect_forwarded "large/64" "an alternation branch is whitelisted too" 474 expect_paywalled "x/echo-headers" \ 475 "a path merely containing the whitelisted one is not waived (left anchor)" 476 expect_paywalled "echo-headers/x" \ 477 "a path extending the whitelisted one is not waived (right anchor)" 478 expect_paywalled "hello" "a path outside the whitelist is paywalled" 479 480 # -------------------------------------------------------------------- 481 # The payment endpoint's refusals, before we have anything valid to send. 482 # -------------------------------------------------------------------- 483 484 echo "-- the payment endpoint --" 485 486 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X POST \ 487 -H "Content-Type: application/json" \ 488 "${BASE_URL}.well-known/paivana" -d '{}') 489 if [ "$STATUS" = "400" ] 490 then 491 ok "a redemption without the required fields is refused with 400" 492 else 493 fail "an empty redemption should be 400, got $STATUS" 494 fi 495 496 # A nonce is 16 bytes in GNUnet's Crockford base32, which is 26 497 # characters; anything else must not reach the order lookup. 25 zeroes 498 # is the off-by-one an implementer would write. 499 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X POST \ 500 -H "Content-Type: application/json" \ 501 "${BASE_URL}.well-known/paivana" \ 502 -d "$(jq -n --arg w "${BASE_URL}hello" \ 503 '{order_id:"no-such-order",nonce:"0000000000000000000000000", 504 expiration:{t_s:2000000000},website:$w}')") 505 if [ "$STATUS" = "400" ] 506 then 507 ok "a redemption with a nonce of the wrong length is refused with 400" 508 else 509 fail "a short nonce should be 400, got $STATUS" 510 fi 511 512 # Well-formed, but naming an order the merchant has never heard of: this 513 # has to reach the backend and come back as a refusal rather than as a 514 # parse error, which is what tells the two apart in a log. 515 read -r JUNK_NONCE _ < <(python3 "$SRCDIR/paivana_id.py" 2000000000 "${BASE_URL}hello" \ 516 000102030405060708090a0b0c0d0e0f) 517 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X POST \ 518 -H "Content-Type: application/json" \ 519 "${BASE_URL}.well-known/paivana" \ 520 -d "$(jq -n --arg w "${BASE_URL}hello" --arg n "$JUNK_NONCE" \ 521 '{order_id:"no-such-order",nonce:$n, 522 expiration:{t_s:2000000000},website:$w}')") 523 if [ "$STATUS" = "404" ] || [ "$STATUS" = "409" ] 524 then 525 ok "a redemption naming an order that was never paid is refused ($STATUS)" 526 else 527 fail "an unknown order should be 404 or 409, got $STATUS" 528 fi 529 530 # -------------------------------------------------------------------- 531 # Buy access. 532 # -------------------------------------------------------------------- 533 534 echo "-- paying --" 535 536 WALLET="$SCRATCH/wallet.sqlite3" 537 538 echo -n "Withdrawing test coins ..." 539 taler-wallet-cli --no-throttle --wallet-db="$WALLET" \ 540 api --expect-success 'withdrawTestBalance' \ 541 "$(jq -n --arg bank "http://localhost:$BANK_PORT/" \ 542 --arg exchange "http://localhost:$EXCHANGE_PORT/" \ 543 '{amount:"TESTKUDOS:20",corebankApiBaseUrl:$bank,exchangeBaseUrl:$exchange}')" \ 544 > "$SCRATCH/withdraw.out" 2> "$SCRATCH/withdraw.err" \ 545 || exit_fail "withdrawTestBalance failed:\n$(cat "$SCRATCH/withdraw.err")" 546 taler-exchange-wirewatch -a exchange-account-2 -c "$CONF" -t \ 547 > "$SCRATCH/wirewatch.log" 2>&1 \ 548 || exit_fail "wirewatch failed:\n$(cat "$SCRATCH/wirewatch.log")" 549 timeout 240 taler-wallet-cli --no-throttle --wallet-db="$WALLET" run-until-done \ 550 > "$SCRATCH/withdraw2.out" 2> "$SCRATCH/withdraw2.err" \ 551 || exit_fail "the withdrawal did not finish:\n$(cat "$SCRATCH/withdraw2.err")" 552 echo " OK" 553 554 WEBSITE="${BASE_URL}hello" 555 # The page asks for one hour. A new order grants two hours. 556 EXPIRATION=$(( $(date +%s) + 3600 )) 557 read -r NONCE PAIVANA_ID < <(python3 "$SRCDIR/paivana_id.py" "$EXPIRATION" "$WEBSITE") 558 echo " paivana_id: $PAIVANA_ID" 559 560 function urlenc() { 561 python3 -c 'import sys,urllib.parse;print(urllib.parse.quote(sys.argv[1],safe=""))' "$1" 562 } 563 564 PAY_URI="taler+http://pay-template/localhost:$MERCHANT_PORT/premium" 565 PAY_URI="$PAY_URI?session_id=$(urlenc "$PAIVANA_ID")" 566 PAY_URI="$PAY_URI&fulfillment_url=$(urlenc "$WEBSITE")" 567 568 echo -n "Paying ..." 569 # "0" selects the first (and only) choice. 570 echo "0" | timeout 240 taler-wallet-cli --no-throttle --wallet-db="$WALLET" \ 571 handle-uri "$PAY_URI" -y > "$SCRATCH/pay.out" 2> "$SCRATCH/pay.err" \ 572 || exit_fail "paying failed:\n$(cat "$SCRATCH/pay.err")" 573 echo " OK" 574 575 # This is what the paywall page polls: the merchant reports the order it 576 # has seen paid under our session, which is the paivana ID. 577 SESSION_URL="${MERCHANT_URL}sessions/$(urlenc "$PAIVANA_ID")" 578 SESSION_URL="$SESSION_URL?fulfillment_url=$(urlenc "$WEBSITE")&timeout_ms=10000" 579 STATUS=$(curl -s -o "$RESP" -w "%{http_code}" "$SESSION_URL") 580 ORDER_ID=$(jq -r '.order_id // empty' < "$RESP") 581 if [ "$STATUS" = "200" ] && [ -n "$ORDER_ID" ] 582 then 583 ok "the merchant reports the order paid under the paivana ID we derived" 584 else 585 fail "the merchant did not report a paid order: $STATUS, $(cat "$RESP")" 586 echo "=== $CHECKS checks, $FAILS failures ===" 587 exit 1 588 fi 589 590 # -------------------------------------------------------------------- 591 # Redeem it. 592 # -------------------------------------------------------------------- 593 594 echo "-- redeeming --" 595 596 function redeem() { 597 local website="$1" expiration="$2" nonce="$3" order="$4" hdr="$5" 598 curl -s -D "$hdr" -o "$SCRATCH/redeem.body" -w "%{http_code}" -X POST \ 599 -H "Content-Type: application/json" \ 600 "${BASE_URL}.well-known/paivana" \ 601 -d "$(jq -n --arg o "$order" --arg n "$nonce" \ 602 --argjson e "$expiration" --arg w "$website" \ 603 '{order_id:$o,nonce:$n,expiration:{t_s:$e},website:$w}')" 604 } 605 606 if [ "$FAULT_REPRO" = 1 ] 607 then 608 # Atomic replacement keeps the proxy from observing a truncated control 609 # file. Three resets cover libcurl's retry of an idle reused connection 610 # and Paivana's one application-level retry; the forced-fresh diagnostic 611 # that follows is allowed through to the real merchant. 612 printf '/private/orders/%s 3\n' "$ORDER_ID" \ 613 > "$SCRATCH/merchant-fault.control.new" 614 mv "$SCRATCH/merchant-fault.control.new" \ 615 "$SCRATCH/merchant-fault.control" 616 fi 617 618 STATUS=$(redeem "$WEBSITE" "$EXPIRATION" "$NONCE" "$ORDER_ID" "$SCRATCH/redeem.hdr") 619 if [ "$FAULT_REPRO" = 1 ] 620 then 621 if [ "$STATUS" = 502 ] && \ 622 [ "$(jq -r '.code' < "$SCRATCH/redeem.body")" = 9801 ] && \ 623 [ "$(jq -r '.merchant_http_status' < "$SCRATCH/redeem.body")" = 0 ] 624 then 625 ok "Paivana reproduces 502/error 9801 with merchant_http_status zero" 626 else 627 fail "faulted redemption returned $STATUS: $(cat "$SCRATCH/redeem.body")" 628 fi 629 630 for _ in $(seq 1 50) 631 do 632 grep -q "Forced-fresh merchant diagnostic for order \`$ORDER_ID'.*completed HTTP status 200" \ 633 "$SCRATCH/paivana.log" && break 634 sleep 0.1 635 done 636 if grep -q "Forced-fresh merchant diagnostic for order \`$ORDER_ID'.*completed HTTP status 200" \ 637 "$SCRATCH/paivana.log" 638 then 639 ok "a forced-fresh request reaches the real merchant immediately afterwards" 640 else 641 fail "the forced-fresh merchant diagnostic did not recover" 642 fi 643 644 if grep -q "FAULT index=1 .*path=/private/orders/$ORDER_ID reused=yes" \ 645 "$SCRATCH/merchant-proxy.log" && \ 646 [ "$(grep -c "FAULT .*path=/private/orders/$ORDER_ID" \ 647 "$SCRATCH/merchant-proxy.log")" = 3 ] 648 then 649 ok "the first failed transport used an idle pooled connection and all retries were covered" 650 else 651 fail "fault frontend did not exercise the expected pooled/retry path" 652 fi 653 654 if [ "$(grep -c "ACCESS .*path=/private/orders/$ORDER_ID" \ 655 "$SCRATCH/merchant-proxy.log")" = 1 ] 656 then 657 ok "none of Paivana's failed attempts reached the frontend access log" 658 else 659 fail "a failed order lookup appeared in the frontend access log" 660 fi 661 662 FRESH_URL="${PAIVANA_MERCHANT_URL}private/orders/${ORDER_ID}" 663 FRESH_URL="$FRESH_URL?session_id=$(urlenc "$PAIVANA_ID")" 664 FRESH_OK=1 665 for _ in $(seq 1 5) 666 do 667 STATUS=$(curl -s -o "$RESP" -w "%{http_code}" -H "$AUTH" "$FRESH_URL") 668 [ "$STATUS" = 200 ] || FRESH_OK=0 669 done 670 if [ "$FRESH_OK" = 1 ] 671 then 672 ok "five fresh order-status curls all succeed through the same frontend" 673 else 674 fail "a fresh order-status curl failed after the Paivana incident" 675 fi 676 677 echo "=== $CHECKS checks, $FAILS failures ===" 678 [ "$FAILS" = 0 ] 679 exit 680 fi 681 682 if [ "$STATUS" = "303" ] 683 then 684 ok "a paid order is redeemed with 303" 685 else 686 fail "redemption should be 303, got $STATUS" 687 fi 688 689 COOKIE=$(grep -i '^set-cookie:' "$SCRATCH/redeem.hdr" | tr -d '\r' \ 690 | sed -n 's/.*\(Paivana-Cookie=[^;]*\).*/\1/p') 691 if [ -n "$COOKIE" ] 692 then 693 ok "the redemption sets an access cookie" 694 else 695 fail "no Paivana-Cookie in the redemption response" 696 fi 697 698 PAID_UNTIL=$(curl -fsS -H "$AUTH" "${MERCHANT_URL}private/orders/$ORDER_ID" | 699 jq -er '.contract_terms.max_pickup_time.t_s') 700 if [ "$PAID_UNTIL" -gt "$EXPIRATION" ] && 701 [[ "$COOKIE" == "Paivana-Cookie=$PAID_UNTIL-"* ]] 702 then 703 ok "the cookie grants access through the paid deadline beyond the requested hour" 704 else 705 fail "the cookie did not grant access through the contract's pickup deadline" 706 fi 707 708 if grep -qi '^set-cookie:.*HttpOnly' "$SCRATCH/redeem.hdr" 709 then 710 ok "the access cookie is HttpOnly" 711 else 712 fail "the access cookie is not HttpOnly" 713 fi 714 715 REDIRECT=$(grep -i '^location:' "$SCRATCH/redeem.hdr" | tr -d '\r' | cut -d' ' -f2-) 716 if [ "$REDIRECT" = "$WEBSITE" ] 717 then 718 ok "the redemption redirects back to the paid URL" 719 else 720 fail "redemption redirects to '$REDIRECT', want '$WEBSITE'" 721 fi 722 723 # Nothing the client sends is taken on trust. Naming a different website 724 # changes the paivana ID -- it is derived from the website -- so the 725 # merchant is asked about a session it has never seen paid. 726 STATUS=$(redeem "${BASE_URL}item" "$EXPIRATION" "$NONCE" "$ORDER_ID" "$SCRATCH/x.hdr") 727 if [ "$STATUS" = "303" ] 728 then 729 fail "the order was redeemed for a URL it did not pay for" 730 else 731 ok "another website derives another paivana ID, so the order is not found ($STATUS)" 732 fi 733 734 # That check passes because of the session binding, and would pass even 735 # if check_contract() never looked at the contract. This one reaches the 736 # fulfillment-URL comparison itself: buy an order whose session IS the one 737 # we will claim, but whose fulfillment URL is somewhere else entirely. 738 # The merchant will sell it -- website_regex is `.*' -- and the session 739 # lookup will succeed, so the only thing standing between this and a 740 # cookie for a page nobody paid for is paivana comparing the contract's 741 # fulfillment URL against the website being claimed. 742 echo "-- an order bought for somewhere else --" 743 744 TARGET="${BASE_URL}item" 745 ELSEWHERE="${BASE_URL}elsewhere" 746 EXP2=$(( $(date +%s) + 3600 )) 747 read -r NONCE2 PAIVANA_ID2 < <(python3 "$SRCDIR/paivana_id.py" "$EXP2" "$TARGET") 748 749 STATUS=$(curl -s -o "$RESP" -w "%{http_code}" \ 750 -H "Content-Type: application/json" \ 751 -X POST "${MERCHANT_URL}templates/premium" \ 752 -d "$(jq -n --arg w "$ELSEWHERE" --arg s "$PAIVANA_ID2" \ 753 '{template_type:"paivana",website:$w,paivana_id:$s}')") 754 if [ "$STATUS" != "200" ] 755 then 756 fail "could not create the mismatched order: $STATUS, $(cat "$RESP")" 757 else 758 ORDER2=$(jq -r '.order_id' < "$RESP") 759 STATUS=$(curl -s -o "$RESP" -w "%{http_code}" -H "$AUTH" \ 760 "${MERCHANT_URL}private/orders/${ORDER2}?session_id=$(urlenc "$PAIVANA_ID2")") 761 PAY2=$(jq -r '.taler_pay_uri // empty' < "$RESP") 762 if [ -z "$PAY2" ] 763 then 764 fail "no pay URI for the mismatched order: $STATUS, $(cat "$RESP")" 765 else 766 echo "0" | timeout 240 taler-wallet-cli --no-throttle --wallet-db="$WALLET" \ 767 handle-uri "$PAY2" -y > "$SCRATCH/pay2.out" 2> "$SCRATCH/pay2.err" \ 768 || fail "paying the mismatched order failed:\n$(cat "$SCRATCH/pay2.err")" 769 770 # Confirm the trap is armed: the merchant really does report this 771 # order as paid under the session we are about to claim. 772 STATUS=$(curl -s -o "$RESP" -w "%{http_code}" \ 773 "${MERCHANT_URL}sessions/$(urlenc "$PAIVANA_ID2")?fulfillment_url=$(urlenc "$ELSEWHERE")&timeout_ms=10000") 774 if [ "$STATUS" = "200" ] && [ -n "$(jq -r '.order_id // empty' < "$RESP")" ] 775 then 776 ok "the mismatched order is paid and bound to the session we will claim" 777 else 778 fail "the mismatched order was not paid: $STATUS, $(cat "$RESP")" 779 fi 780 781 STATUS=$(redeem "$TARGET" "$EXP2" "$NONCE2" "$ORDER2" "$SCRATCH/mm.hdr") 782 if [ "$STATUS" = "303" ] 783 then 784 fail "an order bought for $ELSEWHERE minted a cookie for $TARGET" 785 else 786 ok "an order whose contract names another URL is refused ($STATUS)" 787 fi 788 789 STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$TARGET") 790 if [ "$STATUS" = "302" ] 791 then 792 ok "and that URL is still paywalled afterwards" 793 else 794 fail "$TARGET answered $STATUS after the refused redemption" 795 fi 796 fi 797 fi 798 799 # The expiration is hashed into the paivana ID, so asking for a longer one 800 # than was paid for cannot match the session the merchant knows about. 801 STATUS=$(redeem "$WEBSITE" $((EXPIRATION + 86400)) "$NONCE" "$ORDER_ID" "$SCRATCH/x.hdr") 802 if [ "$STATUS" = "303" ] 803 then 804 fail "the order was redeemed for a later expiration than it paid for" 805 else 806 ok "redeeming for a later expiration is refused ($STATUS)" 807 fi 808 809 # -------------------------------------------------------------------- 810 # What the cookie is worth. 811 # -------------------------------------------------------------------- 812 813 echo "-- after payment --" 814 815 STATUS=$(curl -s -D "$SCRATCH/paid.hdr" -o "$SCRATCH/paid.body" \ 816 -w "%{http_code}" -H "Cookie: $COOKIE" "$WEBSITE") 817 if [ "$STATUS" = "200" ] && grep -qi '^x-upstream:' "$SCRATCH/paid.hdr" 818 then 819 ok "the paid URL is served from the origin" 820 else 821 fail "the paid URL answered $STATUS, and not from the origin" 822 fi 823 824 if grep -q "Hello" "$SCRATCH/paid.body" 825 then 826 ok "the origin's body arrives intact" 827 else 828 fail "the origin's body is not what it serves directly" 829 fi 830 831 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -H "Cookie: $COOKIE" "${BASE_URL}item") 832 if [ "$STATUS" = "302" ] 833 then 834 ok "the cookie does not open a URL it was not minted for" 835 else 836 fail "the cookie opened /item as well: $STATUS" 837 fi 838 839 STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$WEBSITE") 840 if [ "$STATUS" = "302" ] 841 then 842 ok "without the cookie the same URL is still paywalled" 843 else 844 fail "the paid URL is open to everyone: $STATUS" 845 fi 846 847 # The expiration is the KDF salt, so rewriting it in the value has to 848 # break the MAC rather than extend the access. 849 LATER=$(( EXPIRATION + 86400 )) 850 TAMPERED="Paivana-Cookie=${LATER}-${COOKIE#*-}" 851 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -H "Cookie: $TAMPERED" "$WEBSITE") 852 if [ "$STATUS" = "302" ] 853 then 854 ok "rewriting the expiration in the cookie invalidates it" 855 else 856 fail "a cookie with a rewritten expiration was accepted: $STATUS" 857 fi 858 859 STATUS=$(curl -s -o /dev/null -w "%{http_code}" \ 860 -H "Cookie: Paivana-Cookie=not-a-cookie" "$WEBSITE") 861 if [ "$STATUS" = "302" ] 862 then 863 ok "a malformed cookie is refused rather than mis-parsed" 864 else 865 fail "a malformed cookie answered $STATUS" 866 fi 867 868 # One payment can be redeemed again, from anywhere: this is deliberate 869 # (design document 076, "Payment buys access, not a seat") and the check 870 # is here so that a change of mind about it shows up as a test failure 871 # rather than as a silent change of policy. 872 STATUS=$(redeem "$WEBSITE" "$EXPIRATION" "$NONCE" "$ORDER_ID" "$SCRATCH/again.hdr") 873 if [ "$STATUS" = "303" ] 874 then 875 ok "the redemption is repeatable, as the design document says it is" 876 else 877 fail "redeeming a second time gave $STATUS; if that is intended, 076 needs updating" 878 fi 879 880 # The merchant now associates the paid order with the fresh browser session. 881 FRESH_MIN=$(( $(date +%s) + 3600 )) 882 read -r FRESH_NONCE FRESH_ID < <(python3 "$SRCDIR/paivana_id.py" "$FRESH_MIN" "$WEBSITE") 883 FRESH_URI="taler+http://pay-template/localhost:$MERCHANT_PORT/premium" 884 FRESH_URI="$FRESH_URI?session_id=$(urlenc "$FRESH_ID")&fulfillment_url=$(urlenc "$WEBSITE")" 885 if timeout 30 taler-wallet-cli --no-throttle --wallet-db="$WALLET" \ 886 handle-uri "$FRESH_URI" </dev/null > "$SCRATCH/replay.out" 2> "$SCRATCH/replay.err" && 887 curl -fsS "${MERCHANT_URL}sessions/$FRESH_ID?fulfillment_url=$(urlenc "$WEBSITE")&timeout_ms=1000" | 888 jq -e --arg original "$ORDER_ID" '.order_id == $original' >/dev/null && 889 [ "$(redeem "$WEBSITE" "$FRESH_MIN" "$FRESH_NONCE" "$ORDER_ID" "$SCRATCH/replay.hdr")" = 303 ] && 890 grep -qi "^Set-Cookie: Paivana-Cookie=$PAID_UNTIL-" "$SCRATCH/replay.hdr" 891 then 892 ok "a fresh QR recovers the original paid access without another payment" 893 else 894 fail "the fresh session did not redeem the original order through its paid deadline" 895 fi 896 897 echo "=== $CHECKS checks, $FAILS failure(s) ===" 898 if [ "$FAILS" -gt 0 ] 899 then 900 exit 1 901 fi 902 echo "=== all paywall tests passed ===" 903 exit 0