Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http_types panics on CheckoutSession call in production #566

Open
kraemahz opened this issue Jun 21, 2024 · 2 comments · Fixed by #576
Open

http_types panics on CheckoutSession call in production #566

kraemahz opened this issue Jun 21, 2024 · 2 comments · Fixed by #576
Labels
bug Something isn't working

Comments

@kraemahz
Copy link

kraemahz commented Jun 21, 2024

Describe the bug

I have code that works in stripe test mode but causes a panic when switching to live mode. This is a basic implementation of a checkout session largely transcribed from stripe's other examples for embedded checkout code.

Update: I discovered the problem to be a newline character getting into my API key from the environment due to the way my production server loads its secrets.

thread 'tokio-runtime-worker' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/http-types-2.12.0/src/hyperium_http.rs:162:71:
called `Result::unwrap()` on an `Err` value: InvalidHeaderValue
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::result::unwrap_failed
   3: http_types::hyperium_http::headers_to_hyperium_headers
   4: http_types::hyperium_http::<impl core::convert::From<http_types::request::Request> for http::request::Request<http_types::body::Body>>::from
   5: stripe::client::base::tokio::send_inner::{{closure}}
   6: stripe::client::base::tokio::TokioClient::execute::{{closure}}

I looked into http_types and it seems to be blindly doing an unwrap in this function, likely invoked by an into within send_inner

To Reproduce

  1. Create a checkout session using the provided code
  2. Include a newline character in the stripe API key

Expected behavior

Should not panic

Code snippets

let client = stripe::Client::new(stripe_key);
    let mut params = CreateCheckoutSession::new();
    let auto_tax = CreateCheckoutSessionAutomaticTax {
        enabled: true,
        liability: None,
    };
    let client_reference_id = org.add_checkout_reference(&mut conn).ok();
    params.client_reference_id = client_reference_id.as_deref();

    params.automatic_tax = Some(auto_tax);
    params.ui_mode = Some(stripe::CheckoutSessionUiMode::Embedded);
    params.mode = Some(stripe::CheckoutSessionMode::Subscription);
    params.line_items = Some(vec![stripe::CreateCheckoutSessionLineItems {
        price: Some(plan.price_id),
        quantity: Some(n_users as u64),
        ..Default::default()
    }]);
    let return_url = format!(
        "{}{}?session_id={{CHECKOUT_SESSION_ID}}",
        base_url, STRIPE_RETURN_URI
    );
    params.return_url = Some(&return_url);
    let stripe_session = stripe::CheckoutSession::create(&client, params)
        .await
        .map_err(|_| warp::reject::custom(ParseError {}))?;
    let secret = stripe_session
        .client_secret
        .ok_or_else(|| warp::reject::custom(ParseError {}))?;

OS

linux

Rust version

1.79

Library version

0.37.1

API version

2024-04-10

Additional context

This is happening before I get a tick in the 'api calls' section of Stripe.

@kraemahz kraemahz added the bug Something isn't working label Jun 21, 2024
@kraemahz
Copy link
Author

I figured out my issue, it was related to the configuration of my prod environment. A new line character was sneaking in to the environment variable holding my API key and that's what caused the panic.

I think this is still worth fixing with some checks or coercion, but feel free to close if you don't agree after you see this.

@arlyon
Copy link
Owner

arlyon commented Jul 23, 2024

I think a trim and a warning is probably reasonable. Will open a PR against the next branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants