paivana-httpd_pay.c (49439B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2026 Taler Systems SA 4 5 Paivana is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Affero General Public License 7 as published by the Free Software Foundation; either version 8 3, or (at your option) any later version. 9 10 Paivana is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty 12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13 the GNU Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public 16 License along with Paivana; see the file COPYING. If not, 17 write to the Free Software Foundation, Inc., 51 Franklin 18 Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 */ 20 21 /** 22 * @author Christian Grothoff 23 * @file paivana-httpd_pay.c 24 * @brief payment processing logic 25 */ 26 #include "platform.h" 27 #include <curl/curl.h> 28 #include <microhttpd.h> 29 #include <gnunet/gnunet_curl_lib.h> 30 #include <gnunet/gnunet_util_lib.h> 31 #include <taler/taler_mhd_lib.h> 32 #include <taler/taler_json_lib.h> 33 #include <taler/taler_error_codes.h> 34 #include "paivana-httpd_cookie.h" 35 #include "paivana-httpd_helper.h" 36 #include "paivana-httpd_pay.h" 37 38 struct PayRequest; 39 #define TALER_MERCHANT_GET_PRIVATE_ORDER_RESULT_CLOSURE struct PayRequest 40 #include "taler/merchant/get-private-orders-ORDER_ID.h" 41 42 /** 43 * How long we give the merchant backend to answer the 44 * `GET /private/orders/$ORDER_ID' behind one client's redemption. 45 * 46 * A bound is needed at all because the endpoint is unauthenticated: 47 * every syntactically valid POST suspends an MHD connection and issues 48 * a backend query before any payment has been shown to exist, so 49 * without one, a wedged backend pins a suspended connection per 50 * request forever. MHD_OPTION_CONNECTION_TIMEOUT does not apply to 51 * suspended connections, so the deadline has to come from us. 52 * 53 * It is passed as TALER_MERCHANT_get_private_order_option_timeout(), 54 * which does two things at once: it caps the request client-side 55 * (CURLOPT_TIMEOUT_MS, so an unreachable or hung backend is bounded 56 * here too) and it sets `timeout_ms' on the URL, asking the backend to 57 * long-poll for that long before reporting an order as unpaid. 58 * 59 * The long poll is wanted, not merely tolerated. By the time the 60 * client posts here it has already seen the backend confirm the 61 * payment, so an order that still reads "unpaid" means either a client 62 * that is lying -- replaying an order ID it never paid -- or one that 63 * raced a state change that is about to land. Waiting a few seconds 64 * settles the race in the honest client's favour, and the dishonest 65 * one pays for it with a bounded wait and then a 409. Keep it short: 66 * this is the interval an attacker can pin a connection for. 67 */ 68 #define MERCHANT_ORDER_TIMEOUT \ 69 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) 70 71 /** 72 * Do not start a retry when too little of #MERCHANT_ORDER_TIMEOUT remains to 73 * make another connection useful. 74 */ 75 #define MERCHANT_RETRY_MIN_BUDGET \ 76 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250) 77 78 79 /** 80 * Handle for processing actual payment. 81 */ 82 struct PayRequest 83 { 84 85 /** 86 * Kept in a DLL while suspended. 87 */ 88 struct PayRequest *next; 89 90 /** 91 * Kept in a DLL while suspended. 92 */ 93 struct PayRequest *prev; 94 95 /** 96 * Connection we are handling. 97 */ 98 struct MHD_Connection *connection; 99 100 /** 101 * Buffer for TALER_MHD_parse_post_json(). 102 */ 103 void *buffer; 104 105 /** 106 * Uploaded JSON body, NULL if none yet. 107 */ 108 json_t *body; 109 110 /** 111 * Handle for our request to the merchant backend. This 112 * struct is in the #ph_head DLL as long as @e co is non-NULL. 113 */ 114 struct TALER_MERCHANT_GetPrivateOrderHandle *co; 115 116 /** 117 * When @e co was started. The merchant client API reports every 118 * transport failure as HTTP status zero, without the CURLcode that 119 * would distinguish a timeout from (for example) a refused stale 120 * pooled connection. The elapsed time still lets us distinguish a 121 * request that reached our deadline from one that failed before it. 122 */ 123 struct GNUNET_TIME_Absolute merchant_request_started; 124 125 /** 126 * Number of merchant lookup attempts started for this redemption. A GET 127 * that fails before receiving any HTTP response is retried once: the 128 * merchant connection pool can otherwise turn one stale idle connection 129 * into a user-visible payment failure. 130 */ 131 unsigned int merchant_lookup_attempts; 132 133 /** 134 * Response to return, NULL if not yet determined. 135 */ 136 struct MHD_Response *response; 137 138 /** 139 * ID of the order the client claims to have paid. Aliased 140 * from @e body. 141 */ 142 const char *order_id; 143 144 /** 145 * Website the order is supposed to have paid for. Aliased 146 * from @e body. 147 */ 148 const char *website; 149 150 /** 151 * Client-side nonce. 152 */ 153 struct PAIVANA_Nonce nonce; 154 155 /** 156 * Minimum acceptable cookie expiry bound into the session ID. 157 */ 158 struct GNUNET_TIME_Timestamp min_expiration; 159 160 /** 161 * HTTP status to return in combination with @e response to the 162 * client. 163 */ 164 unsigned int response_status; 165 166 }; 167 168 169 /** 170 * Head of DLL of suspended requests. 171 */ 172 static struct PayRequest *ph_head; 173 174 /** 175 * Tail of DLL of suspended requests. 176 */ 177 static struct PayRequest *ph_tail; 178 179 /** 180 * Number of consecutive merchant order lookups that ended without an 181 * HTTP response. This is diagnostic state only: a real response of 182 * any status resets it. In particular, a growing number here makes a 183 * persistent resolver/connection-pool/backend problem visible in the 184 * log instead of making each redemption look like an isolated timeout. 185 */ 186 static unsigned int merchant_transport_failures; 187 188 /** 189 * Number of merchant order requests currently in #ph_head. Maintaining the 190 * counter with the DLL makes diagnostics O(1) during a mass outage instead of 191 * walking every suspended request from every completion callback. 192 */ 193 static unsigned int active_merchant_lookups; 194 195 /** 196 * One forced-fresh request started after a sampled early transport failure. 197 * It is diagnostic only: the client's response is still determined by the 198 * two regular merchant API attempts. Keeping these in a DLL lets shutdown 199 * cancel both a not-yet-started scheduler task and a live curl job safely. 200 */ 201 struct MerchantTransportDiagnostic 202 { 203 struct MerchantTransportDiagnostic *next; 204 struct MerchantTransportDiagnostic *prev; 205 struct GNUNET_SCHEDULER_Task *task; 206 struct GNUNET_CURL_Job *job; 207 CURL *easy; 208 char *url; 209 char *order_id; 210 char error[CURL_ERROR_SIZE]; 211 }; 212 213 static struct MerchantTransportDiagnostic *diagnostic_head; 214 static struct MerchantTransportDiagnostic *diagnostic_tail; 215 216 /** 217 * Merchant transport diagnostics are useful immediately and then at most once 218 * per minute per failure class. A minute is short enough for an operator to 219 * see a persistent outage in routine monitoring, while reducing 32 concurrent 220 * five-second failures from hundreds of warnings per minute to two. 221 */ 222 #define MERCHANT_FAILURE_LOG_INTERVAL \ 223 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1) 224 225 /** 226 * Transport failures that need independently sampled explanations. 227 */ 228 enum MerchantFailureClass 229 { 230 MFC_UNUSABLE_REPLY, 231 MFC_TIMEOUT, 232 MFC_EARLY_TRANSPORT, 233 MFC_COUNT 234 }; 235 236 /** 237 * Sampling state for one #MerchantFailureClass. 238 */ 239 struct MerchantFailureLogState 240 { 241 /** Next time a warning may be emitted. */ 242 struct GNUNET_TIME_Absolute next_log; 243 244 /** Failures omitted since the previous emitted warning. */ 245 unsigned int suppressed; 246 247 /** Whether this class has emitted its first warning. */ 248 bool logged; 249 }; 250 251 /** 252 * Per-class warning sampling state. 253 */ 254 static struct MerchantFailureLogState failure_logs[MFC_COUNT]; 255 256 257 /** 258 * Discard a diagnostic response body without retaining contract data. 259 */ 260 static size_t 261 discard_diagnostic_body (void *cls, 262 const void *data, 263 size_t data_size) 264 { 265 (void) cls; 266 (void) data; 267 return data_size; 268 } 269 270 271 /** 272 * Make curl's fixed-size error buffer safe to embed in one log record. 273 */ 274 static void 275 sanitize_diagnostic_error (char *error) 276 { 277 for (char *p = error; '\0' != *p; p++) 278 if (iscntrl ((unsigned char) *p) || 279 ('"' == *p) || 280 ('\\' == *p)) 281 *p = ' '; 282 } 283 284 285 /** 286 * Release a completed diagnostic after GNUnet has removed and cleaned up its 287 * easy handle. In particular, CURLOPT_ERRORBUFFER requires @e md->error to 288 * remain alive until that cleanup is over, which happens after the completion 289 * callback returns. 290 */ 291 static void 292 free_completed_diagnostic (void *cls) 293 { 294 struct MerchantTransportDiagnostic *md = cls; 295 296 md->task = NULL; 297 GNUNET_CONTAINER_DLL_remove (diagnostic_head, 298 diagnostic_tail, 299 md); 300 GNUNET_free (md->url); 301 GNUNET_free (md->order_id); 302 GNUNET_free (md); 303 } 304 305 306 /** 307 * Report the result while the easy handle still exists, then release our 308 * closure on the next scheduler turn. GNUnet removes and destroys the curl 309 * job after this callback returns. 310 */ 311 static void 312 fresh_diagnostic_finished (void *cls, 313 long completed_http_status, 314 const void *body, 315 size_t body_size) 316 { 317 struct MerchantTransportDiagnostic *md = cls; 318 long observed_http_status = 0; 319 long new_connections = -1; 320 long os_errno = 0; 321 long http_version = 0; 322 const char *remote_ip = NULL; 323 const char *local_ip = NULL; 324 double dns_s = 0; 325 double tcp_s = 0; 326 double tls_s = 0; 327 double first_byte_s = 0; 328 double total_s = 0; 329 330 (void) body; 331 (void) body_size; 332 md->job = NULL; 333 (void) curl_easy_getinfo (md->easy, 334 CURLINFO_RESPONSE_CODE, 335 &observed_http_status); 336 (void) curl_easy_getinfo (md->easy, 337 CURLINFO_NUM_CONNECTS, 338 &new_connections); 339 (void) curl_easy_getinfo (md->easy, 340 CURLINFO_OS_ERRNO, 341 &os_errno); 342 (void) curl_easy_getinfo (md->easy, 343 CURLINFO_HTTP_VERSION, 344 &http_version); 345 (void) curl_easy_getinfo (md->easy, 346 CURLINFO_PRIMARY_IP, 347 &remote_ip); 348 (void) curl_easy_getinfo (md->easy, 349 CURLINFO_LOCAL_IP, 350 &local_ip); 351 (void) curl_easy_getinfo (md->easy, 352 CURLINFO_NAMELOOKUP_TIME, 353 &dns_s); 354 (void) curl_easy_getinfo (md->easy, 355 CURLINFO_CONNECT_TIME, 356 &tcp_s); 357 (void) curl_easy_getinfo (md->easy, 358 CURLINFO_APPCONNECT_TIME, 359 &tls_s); 360 (void) curl_easy_getinfo (md->easy, 361 CURLINFO_STARTTRANSFER_TIME, 362 &first_byte_s); 363 (void) curl_easy_getinfo (md->easy, 364 CURLINFO_TOTAL_TIME, 365 &total_s); 366 sanitize_diagnostic_error (md->error); 367 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 368 "Forced-fresh merchant diagnostic for order `%s': completed" 369 " HTTP status %ld, observed HTTP status %ld, curl error" 370 " \"%s\", new connections %ld, local address %s, remote" 371 " address %s, OS errno %ld, HTTP version %ld; timings in" 372 " seconds (TCP Fast Open enabled): DNS %.6f, TCP %.6f, TLS" 373 " %.6f, first byte %.6f, total %.6f%s\n", 374 md->order_id, 375 completed_http_status, 376 observed_http_status, 377 md->error, 378 new_connections, 379 (NULL != local_ip) ? local_ip : "<none>", 380 (NULL != remote_ip) ? remote_ip : "<none>", 381 os_errno, 382 http_version, 383 dns_s, 384 tcp_s, 385 tls_s, 386 first_byte_s, 387 total_s, 388 (0 != completed_http_status) 389 ? "; a fresh connection reached the merchant after the shared" 390 " merchant context failed" 391 : ""); 392 md->easy = NULL; 393 md->task = GNUNET_SCHEDULER_add_now (&free_completed_diagnostic, 394 md); 395 } 396 397 398 /** 399 * Start a forced-fresh request after the failed merchant job has completely 400 * left libcurl's multi handle. This is the useful A/B comparison with the 401 * two regular attempts, which share the long-lived connection pool. 402 */ 403 static void 404 start_fresh_diagnostic (void *cls) 405 { 406 struct MerchantTransportDiagnostic *md = cls; 407 struct GNUNET_CURL_StreamHandlers sh = { 408 .scb = &discard_diagnostic_body, 409 .scb_cls = md, 410 .jcc = &fresh_diagnostic_finished, 411 .jcc_cls = md 412 }; 413 CURLcode cc; 414 415 md->task = NULL; 416 md->easy = curl_easy_init (); 417 if (NULL == md->easy) 418 goto fail; 419 md->error[0] = '\0'; 420 #define SET_DIAGNOSTIC_OPTION(opt,val) do { \ 421 cc = curl_easy_setopt (md->easy, opt, val); \ 422 if (CURLE_OK != cc) \ 423 goto setopt_fail; \ 424 } while (0) 425 SET_DIAGNOSTIC_OPTION (CURLOPT_URL, 426 md->url); 427 SET_DIAGNOSTIC_OPTION (CURLOPT_ERRORBUFFER, 428 md->error); 429 SET_DIAGNOSTIC_OPTION (CURLOPT_CONNECTTIMEOUT_MS, 430 1500L); 431 SET_DIAGNOSTIC_OPTION (CURLOPT_TIMEOUT_MS, 432 2000L); 433 SET_DIAGNOSTIC_OPTION (CURLOPT_ACCEPT_ENCODING, 434 ""); 435 SET_DIAGNOSTIC_OPTION (CURLOPT_TCP_FASTOPEN, 436 1L); 437 SET_DIAGNOSTIC_OPTION (CURLOPT_FRESH_CONNECT, 438 1L); 439 SET_DIAGNOSTIC_OPTION (CURLOPT_FORBID_REUSE, 440 1L); 441 if (NULL != PH_merchant_unixpath) 442 SET_DIAGNOSTIC_OPTION (CURLOPT_UNIX_SOCKET_PATH, 443 PH_merchant_unixpath); 444 #undef SET_DIAGNOSTIC_OPTION 445 md->job = GNUNET_CURL_job_add_stream (PH_merchant_ctx, 446 md->easy, 447 NULL, 448 &sh); 449 if (NULL != md->job) 450 return; 451 md->easy = NULL; /* GNUNET_CURL_job_add_stream() released it. */ 452 goto fail; 453 454 setopt_fail: 455 #undef SET_DIAGNOSTIC_OPTION 456 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 457 "Could not configure forced-fresh merchant diagnostic for" 458 " order `%s': %s\n", 459 md->order_id, 460 curl_easy_strerror (cc)); 461 curl_easy_cleanup (md->easy); 462 md->easy = NULL; 463 fail: 464 GNUNET_CONTAINER_DLL_remove (diagnostic_head, 465 diagnostic_tail, 466 md); 467 GNUNET_free (md->url); 468 GNUNET_free (md->order_id); 469 GNUNET_free (md); 470 } 471 472 473 /** 474 * Queue one credential-safe diagnostic of the exact failed order lookup. 475 * The bearer token is attached by #PH_merchant_ctx and is never copied here. 476 */ 477 static void 478 queue_fresh_diagnostic (const struct PayRequest *ph) 479 { 480 struct MerchantTransportDiagnostic *md; 481 char *path; 482 char *paivana_id; 483 484 GNUNET_asprintf (&path, 485 "private/orders/%s", 486 ph->order_id); 487 paivana_id = PAIVANA_HTTPD_compute_paivana_id (ph->min_expiration, 488 ph->website, 489 &ph->nonce); 490 md = GNUNET_new (struct MerchantTransportDiagnostic); 491 md->url = TALER_url_join (PH_merchant_internal_url, 492 path, 493 "session_id", 494 paivana_id, 495 NULL); 496 GNUNET_free (paivana_id); 497 GNUNET_free (path); 498 if (NULL == md->url) 499 { 500 GNUNET_free (md); 501 return; 502 } 503 md->order_id = GNUNET_strdup (ph->order_id); 504 GNUNET_CONTAINER_DLL_insert (diagnostic_head, 505 diagnostic_tail, 506 md); 507 md->task = GNUNET_SCHEDULER_add_now (&start_fresh_diagnostic, 508 md); 509 } 510 511 512 /** 513 * Decide whether to emit a merchant failure warning now. 514 * 515 * @param fc failure class 516 * @param[out] suppressed number suppressed since the prior warning 517 * @return true if the caller should log 518 */ 519 static bool 520 merchant_failure_should_log (enum MerchantFailureClass fc, 521 unsigned int *suppressed) 522 { 523 struct MerchantFailureLogState *fl = &failure_logs[fc]; 524 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); 525 526 if ( (! fl->logged) || 527 GNUNET_TIME_absolute_cmp (now, 528 >=, 529 fl->next_log) ) 530 { 531 *suppressed = fl->suppressed; 532 fl->suppressed = 0; 533 fl->logged = true; 534 fl->next_log = GNUNET_TIME_absolute_add ( 535 now, 536 MERCHANT_FAILURE_LOG_INTERVAL); 537 return true; 538 } 539 if (UINT_MAX != fl->suppressed) 540 fl->suppressed++; 541 return false; 542 } 543 544 545 /** 546 * Clear sampling state after the backend returns an HTTP response. 547 * 548 * @return failures suppressed since the most recent emitted warnings 549 */ 550 static unsigned int 551 reset_merchant_failure_logs (void) 552 { 553 unsigned int suppressed = 0; 554 555 for (unsigned int i = 0; i < MFC_COUNT; i++) 556 { 557 if (UINT_MAX - suppressed < failure_logs[i].suppressed) 558 suppressed = UINT_MAX; 559 else 560 suppressed += failure_logs[i].suppressed; 561 } 562 memset (failure_logs, 563 0, 564 sizeof (failure_logs)); 565 return suppressed; 566 } 567 568 569 /** 570 * Log process descriptor usage while a merchant transport failure is 571 * live. Linux exposes this cheaply through /proc; elsewhere the 572 * directory may not exist and this diagnostic quietly stays absent. 573 * Failure to open it with EMFILE/ENFILE is itself useful evidence. 574 */ 575 static void 576 log_file_descriptor_usage (void) 577 { 578 DIR *dir; 579 struct dirent *entry; 580 unsigned int open_fds = 0; 581 582 dir = opendir ("/proc/self/fd"); 583 if (NULL == dir) 584 { 585 int ec = errno; 586 587 if ( (EMFILE == ec) || 588 (ENFILE == ec) ) 589 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 590 "Could not inspect open descriptors after merchant" 591 " transport failure: %s; descriptor exhaustion is" 592 " likely\n", 593 strerror (ec)); 594 else 595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 596 "Could not inspect /proc/self/fd after merchant" 597 " transport failure: %s\n", 598 strerror (ec)); 599 return; 600 } 601 while (NULL != (entry = readdir (dir))) 602 if ( (0 != strcmp (entry->d_name, 603 ".")) && 604 (0 != strcmp (entry->d_name, 605 "..")) ) 606 open_fds++; 607 GNUNET_break (0 == closedir (dir)); 608 /* The directory descriptor was present during the scan and is closed 609 now, so report the number that remains after this function. */ 610 if (0 != open_fds) 611 open_fds--; 612 #if HAVE_SYS_RESOURCE_H 613 { 614 struct rlimit lim; 615 616 if (0 == getrlimit (RLIMIT_NOFILE, 617 &lim)) 618 { 619 if (RLIM_INFINITY == lim.rlim_cur) 620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 621 "Process had %u open file descriptors at merchant" 622 " transport failure (soft limit is unlimited)\n", 623 open_fds); 624 else 625 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 626 "Process had %u open file descriptors at merchant" 627 " transport failure (soft limit %llu)\n", 628 open_fds, 629 (unsigned long long) lim.rlim_cur); 630 return; 631 } 632 } 633 #endif 634 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 635 "Process had %u open file descriptors at merchant" 636 " transport failure (soft limit unavailable)\n", 637 open_fds); 638 } 639 640 641 void 642 PAIVANA_HTTPD_payment_shutdown () 643 { 644 while (NULL != diagnostic_head) 645 { 646 struct MerchantTransportDiagnostic *md = diagnostic_head; 647 648 if (NULL != md->task) 649 GNUNET_SCHEDULER_cancel (md->task); 650 if (NULL != md->job) 651 GNUNET_CURL_job_cancel (md->job); 652 GNUNET_CONTAINER_DLL_remove (diagnostic_head, 653 diagnostic_tail, 654 md); 655 GNUNET_free (md->url); 656 GNUNET_free (md->order_id); 657 GNUNET_free (md); 658 } 659 while (NULL != ph_head) 660 { 661 struct PayRequest *ph = ph_head; 662 663 if (NULL != ph->co) 664 { 665 TALER_MERCHANT_get_private_order_cancel (ph->co); 666 ph->co = NULL; 667 } 668 GNUNET_CONTAINER_DLL_remove (ph_head, 669 ph_tail, 670 ph); 671 GNUNET_assert (active_merchant_lookups > 0); 672 active_merchant_lookups--; 673 MHD_resume_connection (ph->connection); 674 /* Note: PAIVANA_HTTPD_payment_destroy() 675 will be called by the owner of 'ph', 676 no need to do it here! */ 677 } 678 GNUNET_assert (0 == active_merchant_lookups); 679 } 680 681 682 struct PayRequest * 683 PAIVANA_HTTPD_payment_create (struct MHD_Connection *connection) 684 { 685 struct PayRequest *ph; 686 687 ph = GNUNET_new (struct PayRequest); 688 ph->connection = connection; 689 return ph; 690 } 691 692 693 /** 694 * Is @a website a URL below our own base URL? 695 * 696 * Used to bound where a client may send itself once it has paid for 697 * an order that carries no fulfillment URL of its own: without this 698 * the client picks the redirect target and the site the access cookie 699 * is minted for. 700 * 701 * The comparison is on whole path segments. A bare prefix test would 702 * accept "https://example.com.evil.net/" for a base URL of 703 * "https://example.com", because strip_trailing_slashes() has removed 704 * the '/' that used to terminate it. 705 * 706 * @param website candidate URL, from the client 707 * @return true if @a website is our base URL or something below it 708 */ 709 static bool 710 under_our_base_url (const char *website) 711 { 712 size_t blen; 713 714 if (NULL == PH_base_url) 715 { 716 /* BASE_URL is optional; without it we have nothing to compare 717 against and must not guess. Note that dereferencing it here 718 used to be an unconditional crash. */ 719 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 720 "Cannot check the target of an order without a" 721 " fulfillment URL: BASE_URL is not configured\n"); 722 return false; 723 } 724 blen = strlen (PH_base_url); 725 if (0 != strncmp (website, 726 PH_base_url, 727 blen)) 728 return false; 729 /* PH_base_url has no trailing '/' (strip_trailing_slashes()), so 730 require the boundary here rather than inheriting it. */ 731 return ('\0' == website[blen]) || 732 ('/' == website[blen]); 733 } 734 735 736 static void 737 order_status_cb (struct PayRequest *ph, 738 const struct TALER_MERCHANT_GetPrivateOrderResponse *osr); 739 740 741 /** 742 * Start one attempt to retrieve the order from the merchant backend. 743 * 744 * The caller owns the request's DLL membership and suspended MHD connection. 745 * On failure this function leaves @e ph->co NULL. 746 * 747 * @param ph payment request 748 * @param timeout remaining overall timeout for this attempt 749 * @return #TALER_EC_NONE on success, an error code otherwise 750 */ 751 static enum TALER_ErrorCode 752 start_merchant_order_lookup (struct PayRequest *ph, 753 struct GNUNET_TIME_Relative timeout) 754 { 755 char *paivana_id; 756 enum GNUNET_GenericReturnValue ret; 757 enum TALER_ErrorCode ec; 758 759 GNUNET_assert (NULL == ph->co); 760 ph->co = TALER_MERCHANT_get_private_order_create (PH_merchant_ctx, 761 PH_merchant_internal_url, 762 ph->order_id); 763 if (NULL == ph->co) 764 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 765 paivana_id = PAIVANA_HTTPD_compute_paivana_id (ph->min_expiration, 766 ph->website, 767 &ph->nonce); 768 ret = TALER_MERCHANT_get_private_order_set_options ( 769 ph->co, 770 TALER_MERCHANT_get_private_order_option_session_id (paivana_id), 771 TALER_MERCHANT_get_private_order_option_timeout (timeout)); 772 GNUNET_free (paivana_id); 773 if (GNUNET_OK != ret) 774 { 775 TALER_MERCHANT_get_private_order_cancel (ph->co); 776 ph->co = NULL; 777 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 778 } 779 ec = TALER_MERCHANT_get_private_order_start (ph->co, 780 &order_status_cb, 781 ph); 782 if (TALER_EC_NONE != ec) 783 { 784 TALER_MERCHANT_get_private_order_cancel (ph->co); 785 ph->co = NULL; 786 return ec; 787 } 788 ph->merchant_lookup_attempts++; 789 return TALER_EC_NONE; 790 } 791 792 793 /** 794 * Build a Paivana error response caused by a merchant order lookup. 795 * 796 * Keep the standard Taler error shape and its existing @e detail while adding 797 * the status of the merchant request. A zero status explicitly says that no 798 * HTTP response was received. 799 * 800 * @param ec Taler error code 801 * @param detail optional error detail 802 * @param merchant_http_status HTTP status returned by the merchant, or zero 803 * @return MHD response 804 */ 805 static struct MHD_Response * 806 make_merchant_error (enum TALER_ErrorCode ec, 807 const char *detail, 808 unsigned int merchant_http_status) 809 { 810 return TALER_MHD_MAKE_JSON_PACK ( 811 TALER_MHD_PACK_EC (ec), 812 GNUNET_JSON_pack_conditional ( 813 NULL != detail, 814 GNUNET_JSON_pack_string ("detail", 815 detail)), 816 GNUNET_JSON_pack_uint64 ("merchant_http_status", 817 merchant_http_status)); 818 } 819 820 821 /** 822 * Queue a Paivana error caused before a merchant lookup returned. 823 * 824 * @param connection client connection 825 * @param http_status status to return to the client 826 * @param ec Taler error code 827 * @param detail optional error detail 828 * @return MHD result 829 */ 830 static enum MHD_Result 831 reply_with_merchant_error (struct MHD_Connection *connection, 832 unsigned int http_status, 833 enum TALER_ErrorCode ec, 834 const char *detail) 835 { 836 struct MHD_Response *response; 837 enum MHD_Result ret; 838 839 response = make_merchant_error (ec, 840 detail, 841 0); 842 ret = MHD_queue_response (connection, 843 http_status, 844 response); 845 MHD_destroy_response (response); 846 return ret; 847 } 848 849 850 /** 851 * Check that the @a contract that was paid is reasonable for the 852 * request in @a ph, that is that we would indeed consider this 853 * contract to apply for the website and duration indicated 854 * in @a ph. If it does not apply, a response must be set in 855 * @a ph. 856 * 857 * @param[in,out] ph request to check 858 * @param contract contract to check 859 * @param[out] cookie_expiration access deadline of the paid contract 860 * @return true if the contract is good for the request, 861 * false if not and thus a response object was created in @a ph 862 */ 863 static bool 864 check_contract (struct PayRequest *ph, 865 const json_t *contract, 866 struct GNUNET_TIME_Timestamp *cookie_expiration) 867 { 868 struct GNUNET_TIME_Timestamp max_time 869 = GNUNET_TIME_UNIT_FOREVER_TS; 870 const char *target = NULL; 871 struct GNUNET_JSON_Specification spec[] = { 872 GNUNET_JSON_spec_mark_optional ( 873 TALER_JSON_spec_web_url ("fulfillment_url", 874 &target), 875 NULL), 876 GNUNET_JSON_spec_mark_optional ( 877 GNUNET_JSON_spec_timestamp ("max_pickup_time", 878 &max_time), 879 NULL), 880 GNUNET_JSON_spec_end () 881 }; 882 enum GNUNET_GenericReturnValue ret; 883 const char *ename; 884 unsigned int eline; 885 886 ret = GNUNET_JSON_parse (contract, 887 spec, 888 &ename, 889 &eline); 890 if (GNUNET_OK != ret) 891 { 892 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 893 "Encountered contract with unexpected fields: %s@%u\n", 894 ename, 895 eline); 896 /* Fail closed: returning true here would skip every check below -- 897 the fulfillment_url binding, the base-URL containment test and 898 the max_pickup_time deadline -- and mint an access cookie for 899 whatever website the client named. Tolerating unknown *extra* 900 fields is already what GNUNET_JSON_parse() does; a failure here 901 means a field we do look at was malformed. */ 902 GNUNET_break_op (0); 903 ph->response = make_merchant_error (TALER_EC_PAIVANA_WRONG_ORDER, 904 ph->order_id, 905 MHD_HTTP_OK); 906 ph->response_status = MHD_HTTP_CONFLICT; 907 return false; 908 } 909 if ( (NULL != target) && 910 (0 != strcmp (target, 911 ph->website)) ) 912 { 913 GNUNET_break_op (0); 914 ph->response = make_merchant_error (TALER_EC_PAIVANA_WRONG_ORDER, 915 ph->order_id, 916 MHD_HTTP_OK); 917 ph->response_status = MHD_HTTP_CONFLICT; 918 return false; 919 } 920 if ( ( (NULL == target) && 921 (! under_our_base_url (ph->website)) ) || 922 (! TALER_is_web_url (ph->website)) ) 923 { 924 /* Bad: the order has no fulfillment URL, and on top of that 925 the target given is not from our domain or not a well-formed 926 URL. Reject hard. */ 927 GNUNET_break_op (0); 928 ph->response = make_merchant_error (TALER_EC_PAIVANA_INVALID_TARGET, 929 ph->website, 930 MHD_HTTP_OK); 931 ph->response_status = MHD_HTTP_CONFLICT; 932 return false; 933 } 934 if (GNUNET_TIME_timestamp_cmp (max_time, 935 <=, 936 GNUNET_TIME_timestamp_get ()) || 937 GNUNET_TIME_timestamp_cmp (ph->min_expiration, 938 >, 939 max_time)) 940 { 941 GNUNET_break_op (0); 942 ph->response = make_merchant_error (TALER_EC_PAIVANA_TOO_LATE, 943 ph->order_id, 944 MHD_HTTP_OK); 945 ph->response_status = MHD_HTTP_GONE; 946 return false; 947 } 948 *cookie_expiration = max_time; 949 return true; 950 } 951 952 953 /** 954 * Handle response from the GET /private/orders/$ORDER_ID request. 955 * 956 * @param ph the payment request we are processing 957 * @param osr response details 958 */ 959 static void 960 order_status_cb (struct PayRequest *ph, 961 const struct TALER_MERCHANT_GetPrivateOrderResponse *osr) 962 { 963 struct GNUNET_TIME_Relative elapsed; 964 char *elapsed_s; 965 char *timeout_s; 966 unsigned int active_lookups; 967 968 elapsed = GNUNET_TIME_absolute_get_duration ( 969 ph->merchant_request_started); 970 /* GNUNET_STRINGS_relative_time_to_string() reuses one static buffer, 971 so keep copies before putting both values in one log message. */ 972 elapsed_s = GNUNET_strdup ( 973 GNUNET_STRINGS_relative_time_to_string (elapsed, 974 true)); 975 timeout_s = GNUNET_strdup ( 976 GNUNET_STRINGS_relative_time_to_string (MERCHANT_ORDER_TIMEOUT, 977 true)); 978 active_lookups = active_merchant_lookups; 979 ph->co = NULL; 980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 981 "Merchant order lookup attempt %u for `%s' completed with" 982 " HTTP status %u after %s\n", 983 ph->merchant_lookup_attempts, 984 ph->order_id, 985 osr->hr.http_status, 986 elapsed_s); 987 if ( (0 == osr->hr.http_status) && 988 (NULL == osr->hr.reply) && 989 (1 == ph->merchant_lookup_attempts) && 990 GNUNET_TIME_relative_cmp (elapsed, 991 <, 992 MERCHANT_ORDER_TIMEOUT) ) 993 { 994 struct GNUNET_TIME_Relative remaining 995 = GNUNET_TIME_relative_subtract (MERCHANT_ORDER_TIMEOUT, 996 elapsed); 997 998 if (GNUNET_TIME_relative_cmp (remaining, 999 >=, 1000 MERCHANT_RETRY_MIN_BUDGET)) 1001 { 1002 enum TALER_ErrorCode ec; 1003 1004 ec = start_merchant_order_lookup (ph, 1005 remaining); 1006 if (TALER_EC_NONE == ec) 1007 { 1008 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1009 "Merchant order lookup for `%s' received no HTTP" 1010 " response on its first attempt after %s; retrying once" 1011 " within the original %s deadline\n", 1012 ph->order_id, 1013 elapsed_s, 1014 timeout_s); 1015 GNUNET_free (elapsed_s); 1016 GNUNET_free (timeout_s); 1017 return; 1018 } 1019 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1020 "Could not start the retry of merchant order `%s': %d\n", 1021 ph->order_id, 1022 (int) ec); 1023 } 1024 } 1025 GNUNET_CONTAINER_DLL_remove (ph_head, 1026 ph_tail, 1027 ph); 1028 GNUNET_assert (active_merchant_lookups > 0); 1029 active_merchant_lookups--; 1030 MHD_resume_connection (ph->connection); 1031 TALER_MHD_daemon_trigger (); 1032 if (0 != osr->hr.http_status) 1033 { 1034 unsigned int suppressed = reset_merchant_failure_logs (); 1035 1036 if (0 != merchant_transport_failures) 1037 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1038 "Merchant backend at `%s' answered order `%s' after %u" 1039 " consecutive lookup%s without an HTTP response (%u" 1040 " repetitive diagnostic%s suppressed)\n", 1041 PH_merchant_internal_url, 1042 ph->order_id, 1043 merchant_transport_failures, 1044 (1 == merchant_transport_failures) ? "" : "s", 1045 suppressed, 1046 (1 == suppressed) ? "" : "s"); 1047 merchant_transport_failures = 0; 1048 } 1049 switch (osr->hr.http_status) 1050 { 1051 case MHD_HTTP_OK: 1052 /* "paid" survives a refund -- the merchant reports the refund in 1053 separate fields (api-merchant.rst, CheckPaymentPaidResponse) -- 1054 so testing the status alone would hand a fresh cookie to someone 1055 who has had their money back. */ 1056 if ( (TALER_MERCHANT_OSC_PAID != osr->details.ok.status) || 1057 (osr->details.ok.details.paid.refunded) || 1058 (osr->details.ok.details.paid.refund_pending) ) 1059 { 1060 GNUNET_break_op (0); 1061 ph->response = make_merchant_error (TALER_EC_PAIVANA_PAYMENT_MISSING, 1062 ph->order_id, 1063 MHD_HTTP_OK); 1064 ph->response_status = MHD_HTTP_CONFLICT; 1065 } 1066 else 1067 { 1068 void *ca = NULL; 1069 size_t ca_len = 0; 1070 char *cookie; 1071 struct MHD_Response *resp; 1072 struct GNUNET_TIME_Timestamp cookie_expiration; 1073 1074 if (! check_contract (ph, 1075 osr->details.ok.details.paid.contract_terms, 1076 &cookie_expiration)) 1077 { 1078 GNUNET_free (elapsed_s); 1079 GNUNET_free (timeout_s); 1080 return; 1081 } 1082 /* The client address is bound into the cookie MAC; computing 1083 the cookie over an empty address would produce a cookie that 1084 PAIVANA_HTTPD_check_cookie can never match, silently denying 1085 the access the client just paid for. Treat failure to obtain 1086 it as a hard error instead. 1087 1088 Note: This should become conditional once we add a 1089 configuration option to not include the client address in the 1090 cookie hash to allow one payment to be used from any IP 1091 address. */ 1092 if (! PAIVANA_HTTPD_get_client_address (ph->connection, 1093 &ca, 1094 &ca_len)) 1095 { 1096 GNUNET_break (0); 1097 ph->response = make_merchant_error ( 1098 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 1099 ph->order_id, 1100 MHD_HTTP_OK); 1101 ph->response_status = MHD_HTTP_INTERNAL_SERVER_ERROR; 1102 break; 1103 } 1104 cookie = PAIVANA_HTTPD_compute_cookie (cookie_expiration, 1105 ph->website, 1106 ca_len, 1107 ca); 1108 /* The cookie is the bearer credential proving payment; anyone 1109 who can read the log could replay it from the same address. */ 1110 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1111 "Client paid for `%s', setting access cookie\n", 1112 ph->website); 1113 GNUNET_free (ca); 1114 resp = MHD_create_response_from_buffer (0, 1115 NULL, 1116 MHD_RESPMEM_PERSISTENT); 1117 GNUNET_assert (NULL != resp); 1118 if ( (MHD_YES != 1119 MHD_add_response_header (resp, 1120 MHD_HTTP_HEADER_SET_COOKIE, 1121 cookie)) || 1122 (MHD_YES != 1123 MHD_add_response_header (resp, 1124 MHD_HTTP_HEADER_LOCATION, 1125 ph->website)) ) 1126 { 1127 /* Neither header is optional: without the `Set-Cookie' the 1128 client has paid and been sent back to a page that will 1129 paywall it again, and without the `Location' the 303 has no 1130 target at all. Answering 500 at least says so, and leaves 1131 the order paid and the redemption repeatable; sending the 1132 303 anyway does not. */ 1133 GNUNET_break (0); 1134 MHD_destroy_response (resp); 1135 GNUNET_free (cookie); 1136 ph->response = make_merchant_error ( 1137 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 1138 ph->website, 1139 MHD_HTTP_OK); 1140 ph->response_status = MHD_HTTP_INTERNAL_SERVER_ERROR; 1141 break; 1142 } 1143 GNUNET_free (cookie); 1144 TALER_MHD_add_global_headers (resp, 1145 false); 1146 ph->response = resp; 1147 ph->response_status = MHD_HTTP_SEE_OTHER; 1148 } 1149 break; 1150 case MHD_HTTP_UNAUTHORIZED: 1151 case MHD_HTTP_FORBIDDEN: 1152 /* Our `MERCHANT_ACCESS_TOKEN' is wrong: the operator's problem, not 1153 the client's, hence 500 and not a 4xx. UNAUTHORIZED is the case 1154 that actually fires -- taler-merchant-httpd_auth.c answers a bad 1155 bearer token with 401 -- and without it this landed in the 1156 default branch below, telling the operator that a protocol 1157 incompatibility should be reported to us. 1158 1159 Note that GANA has 9801 documented as a 502 and 9803 as a 500, 1160 i.e. the two the other way round from what is sent here and 1161 below. The statuses are right: RFC 9110 section 15.6.3 gives 1162 502 for "an invalid response from an inbound server", which is 1163 the unexpected-status case (9803), while a bearer token of ours 1164 that the backend will not take is our own misconfiguration and 1165 not the upstream misbehaving (9801). Fixing the registry is a 1166 change in gana, a different repository. */ 1167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1168 "Merchant backend at `%s' rejected our credentials (HTTP" 1169 " %u); check MERCHANT_ACCESS_TOKEN\n", 1170 PH_merchant_internal_url, 1171 osr->hr.http_status); 1172 ph->response = make_merchant_error (TALER_EC_PAIVANA_BACKEND_REFUSED, 1173 NULL, 1174 osr->hr.http_status); 1175 ph->response_status = MHD_HTTP_INTERNAL_SERVER_ERROR; 1176 break; 1177 case MHD_HTTP_NOT_FOUND: 1178 ph->response = make_merchant_error (TALER_EC_PAIVANA_ORDER_UNKNOWN, 1179 ph->order_id, 1180 osr->hr.http_status); 1181 ph->response_status = MHD_HTTP_NOT_FOUND; 1182 break; 1183 case 0: 1184 /* No HTTP status at all. The merchant client library reports this 1185 both for a request that never completed and for one whose reply 1186 it could not make sense of, with the same error code; @e reply is 1187 what tells them apart, being NULL only in the former case. */ 1188 if (NULL != osr->hr.reply) 1189 { 1190 unsigned int suppressed; 1191 1192 merchant_transport_failures = 0; 1193 GNUNET_break_op (0); 1194 if (merchant_failure_should_log (MFC_UNUSABLE_REPLY, 1195 &suppressed)) 1196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1197 "Merchant backend at `%s' sent an unusable reply for" 1198 " order `%s' after %s (%u similar diagnostic%s" 1199 " suppressed)\n", 1200 PH_merchant_internal_url, 1201 ph->order_id, 1202 elapsed_s, 1203 suppressed, 1204 (1 == suppressed) ? "" : "s"); 1205 ph->response = make_merchant_error (TALER_EC_PAIVANA_BACKEND_ERROR, 1206 ph->order_id, 1207 osr->hr.http_status); 1208 ph->response_status = MHD_HTTP_BAD_GATEWAY; 1209 break; 1210 } 1211 if (UINT_MAX != merchant_transport_failures) 1212 merchant_transport_failures++; 1213 if (GNUNET_TIME_relative_cmp (elapsed, 1214 >=, 1215 MERCHANT_ORDER_TIMEOUT)) 1216 { 1217 unsigned int suppressed; 1218 1219 if (merchant_failure_should_log (MFC_TIMEOUT, 1220 &suppressed)) 1221 { 1222 log_file_descriptor_usage (); 1223 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1224 "Merchant backend at `%s' returned no HTTP response for" 1225 " order `%s' by the %s deadline (elapsed %s; %u" 1226 " concurrent merchant lookup%s including this one; %u" 1227 " consecutive transport failure%s; %u similar" 1228 " diagnostic%s suppressed)\n", 1229 PH_merchant_internal_url, 1230 ph->order_id, 1231 timeout_s, 1232 elapsed_s, 1233 active_lookups, 1234 (1 == active_lookups) ? "" : "s", 1235 merchant_transport_failures, 1236 (1 == merchant_transport_failures) ? "" : "s", 1237 suppressed, 1238 (1 == suppressed) ? "" : "s"); 1239 } 1240 /* GENERIC_TIMEOUT's hint ("trying again might help") is the one 1241 that is true once our own deadline was actually reached. */ 1242 ph->response = make_merchant_error (TALER_EC_GENERIC_TIMEOUT, 1243 ph->order_id, 1244 osr->hr.http_status); 1245 ph->response_status = MHD_HTTP_GATEWAY_TIMEOUT; 1246 } 1247 else 1248 { 1249 unsigned int suppressed; 1250 1251 /* The merchant API does not expose CURLcode, so DNS failure, 1252 connection refusal, TLS failure and a dead reused connection 1253 are indistinguishable here. What they have in common is that 1254 they failed before our timeout. Calling that a timeout hid the 1255 most useful fact from both the operator and the client. */ 1256 if (merchant_failure_should_log (MFC_EARLY_TRANSPORT, 1257 &suppressed)) 1258 { 1259 log_file_descriptor_usage (); 1260 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1261 "Merchant backend at `%s' returned no HTTP response for" 1262 " order `%s' after %s, before the %s deadline; this is" 1263 " an early transport failure (for example DNS, TCP, TLS" 1264 " or a stale reused connection), not a Paivana timeout" 1265 " (%u concurrent merchant lookup%s including this one;" 1266 " %u consecutive transport failure%s; %u similar" 1267 " diagnostic%s suppressed)\n", 1268 PH_merchant_internal_url, 1269 ph->order_id, 1270 elapsed_s, 1271 timeout_s, 1272 active_lookups, 1273 (1 == active_lookups) ? "" : "s", 1274 merchant_transport_failures, 1275 (1 == merchant_transport_failures) ? "" : "s", 1276 suppressed, 1277 (1 == suppressed) ? "" : "s"); 1278 queue_fresh_diagnostic (ph); 1279 } 1280 ph->response = make_merchant_error (TALER_EC_PAIVANA_BACKEND_REFUSED, 1281 ph->order_id, 1282 osr->hr.http_status); 1283 ph->response_status = MHD_HTTP_BAD_GATEWAY; 1284 } 1285 break; 1286 default: 1287 { 1288 char code[20]; 1289 1290 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1291 "Unexpected status code %u from backend\n", 1292 osr->hr.http_status); 1293 GNUNET_snprintf (code, 1294 sizeof (code), 1295 "%u", 1296 osr->hr.http_status); 1297 ph->response = make_merchant_error (TALER_EC_PAIVANA_BACKEND_ERROR, 1298 code, 1299 osr->hr.http_status); 1300 ph->response_status = MHD_HTTP_BAD_GATEWAY; 1301 } 1302 break; 1303 } 1304 GNUNET_free (elapsed_s); 1305 GNUNET_free (timeout_s); 1306 } 1307 1308 1309 enum MHD_Result 1310 PAIVANA_HTTPD_payment_handle (struct PayRequest *ph, 1311 const char *upload_data, 1312 size_t *upload_data_size) 1313 { 1314 if (NULL == ph->body) 1315 { 1316 enum GNUNET_GenericReturnValue ret; 1317 1318 ret = TALER_MHD_parse_post_json (ph->connection, 1319 &ph->buffer, 1320 upload_data, 1321 upload_data_size, 1322 &ph->body); 1323 if (GNUNET_OK != ret) 1324 return (GNUNET_NO == ret) ? MHD_YES : MHD_NO; 1325 if (NULL == ph->body) 1326 return MHD_YES; 1327 } 1328 if (NULL != ph->response) 1329 { 1330 return MHD_queue_response (ph->connection, 1331 ph->response_status, 1332 ph->response); 1333 } 1334 if (NULL == ph->order_id) 1335 { 1336 struct GNUNET_JSON_Specification spec[] = { 1337 TALER_JSON_spec_slug ("order_id", 1338 &ph->order_id), 1339 TALER_JSON_spec_web_url ("website", 1340 &ph->website), 1341 GNUNET_JSON_spec_timestamp ("expiration", 1342 &ph->min_expiration), 1343 GNUNET_JSON_spec_fixed_auto ("nonce", 1344 &ph->nonce), 1345 GNUNET_JSON_spec_end () 1346 }; 1347 enum GNUNET_GenericReturnValue ret; 1348 1349 ret = TALER_MHD_parse_json_data (ph->connection, 1350 ph->body, 1351 spec); 1352 if (GNUNET_YES != ret) 1353 return (GNUNET_NO == ret) ? MHD_YES : MHD_NO; 1354 /* `website' is a URL like any other paivana handles, and every 1355 other one is refused past #PH_MAX_URL_LENGTH before anything 1356 looks at it. This one arrives in a JSON body instead of a 1357 request line, which is the only reason it escaped that: from 1358 here it goes into an HKDF, into a `Location' and into the 1359 `Path' of a `Set-Cookie', where three bytes of header are spent 1360 per byte of path. Bound it in the same place and at the same 1361 length. */ 1362 if (PH_MAX_URL_LENGTH < strlen (ph->website)) 1363 { 1364 GNUNET_break_op (0); 1365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1366 "Refusing %llu byte `website' in payment redemption\n", 1367 (unsigned long long) strlen (ph->website)); 1368 return TALER_MHD_reply_with_error ( 1369 ph->connection, 1370 MHD_HTTP_BAD_REQUEST, 1371 TALER_EC_GENERIC_PARAMETER_MALFORMED, 1372 "website"); 1373 } 1374 } 1375 GNUNET_CONTAINER_DLL_insert (ph_head, 1376 ph_tail, 1377 ph); 1378 active_merchant_lookups++; 1379 MHD_suspend_connection (ph->connection); 1380 { 1381 enum TALER_ErrorCode ec; 1382 1383 ph->merchant_request_started = GNUNET_TIME_absolute_get (); 1384 ec = start_merchant_order_lookup (ph, 1385 MERCHANT_ORDER_TIMEOUT); 1386 if (TALER_EC_NONE != ec) 1387 { 1388 /* Everything the callee can fail on here is a resource failure 1389 it recovers from by telling us: curl_easy_init() or 1390 curl_multi_add_handle() came back empty. That is one client's 1391 redemption going wrong, and asserting on it took the daemon 1392 down with every other request in flight, paid ones included. */ 1393 GNUNET_break (0); 1394 GNUNET_CONTAINER_DLL_remove (ph_head, 1395 ph_tail, 1396 ph); 1397 GNUNET_assert (active_merchant_lookups > 0); 1398 active_merchant_lookups--; 1399 MHD_resume_connection (ph->connection); 1400 return reply_with_merchant_error (ph->connection, 1401 MHD_HTTP_INTERNAL_SERVER_ERROR, 1402 TALER_EC_PAIVANA_GET_ORDER_FAILED, 1403 ph->order_id); 1404 } 1405 } 1406 return MHD_YES; 1407 } 1408 1409 1410 void 1411 PAIVANA_HTTPD_payment_destroy (struct PayRequest *ph) 1412 { 1413 TALER_MHD_parse_post_cleanup_callback (ph->buffer); 1414 if (NULL != ph->co) 1415 { 1416 TALER_MERCHANT_get_private_order_cancel (ph->co); 1417 GNUNET_CONTAINER_DLL_remove (ph_head, 1418 ph_tail, 1419 ph); 1420 GNUNET_assert (active_merchant_lookups > 0); 1421 active_merchant_lookups--; 1422 ph->co = NULL; 1423 } 1424 if (NULL != ph->response) 1425 { 1426 MHD_destroy_response (ph->response); 1427 ph->response = NULL; 1428 } 1429 json_decref (ph->body); 1430 GNUNET_free (ph); 1431 }