-
I use this project to append csrf cookie in my response header within the middleware function, started from the example, but I found the function would fail to update the header, the code as below: pub async fn append_csrf_token(req: ServiceRequest, next: Next<impl MessageBody + 'static>) -> core::result::Result<ServiceResponse<impl MessageBody + 'static>, actix_web::error::Error> {
let mut response = next.call(req).await?;
let http_req = response.request();
if let Ok(identity) = Identity::from_request(&http_req, &mut Payload::None).into_inner() {
if let Ok(user_json) = identity.id() {
if let Ok(user) = serde_json::from_str::<UserIdentity>(&user_json) {
//generate csrf token based on user token
if let Some(protect_key) = http_req.app_data::<web::Data<SecVec<u8>>>() {
if let Ok(protect_key_array) = protect_key.clone().unsecure().try_into() {
if let Ok(csrf_token) = user.generate_new_csrf_cookie(protect_key_array, 600) {
let http_header = response.headers_mut();
//code crashed here.
http_header.insert(
HeaderName::from_static("XSRF-TOKEN"),
HeaderValue::from_str(&csrf_token).unwrap(),
);
} else {
warn!("failed to generate csrf token in middleware")
}
}
}
}
}
}
Ok(response)
} and the stack information [2023-07-05T12:06:49Z INFO sqlx::query] SELECT * FROM user …; rows affected: 0, rows returned: 1, elapsed: 6.509ms
SELECT
*
FROM
user
WHERE
email = ?
thread 'actix-server worker 2' panicked at 'index out of bounds: the len is 0 but the index is 0', /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/http-0.2.9/src/header/name.rs:1270:13
stack backtrace:
0: 0x111515d73 - std::backtrace_rs::backtrace::libunwind::trace::hae6efe039b3fd59a
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
1: 0x111515d73 - std::backtrace_rs::backtrace::trace_unsynchronized::hdcd38835c997b122
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
2: 0x111515d73 - std::sys_common::backtrace::_print_fmt::hc4a546b1903dc7d6
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/sys_common/backtrace.rs:65:5
3: 0x111515d73 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hfe6f44aff11c179e
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/sys_common/backtrace.rs:44:22
4: 0x11153586b - core::fmt::rt::Argument::fmt::hfa69b0060701a7ed
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/core/src/fmt/rt.rs:138:9
5: 0x11153586b - core::fmt::write::h3bc6e7c5ab093964
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/core/src/fmt/mod.rs:1094:21
6: 0x111511fa8 - std::io::Write::write_fmt::ha0b68ab4c15f0bc6
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/io/mod.rs:1712:15
7: 0x111515b6a - std::sys_common::backtrace::_print::h796f55fec72c897e
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/sys_common/backtrace.rs:47:5
8: 0x111515b6a - std::sys_common::backtrace::print::h18ece7990e53029f
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/sys_common/backtrace.rs:34:9
9: 0x111516fe5 - std::panicking::default_hook::{{closure}}::h55ac5a157221e609
10: 0x111516def - std::panicking::default_hook::hac2381dcb7cdb29f
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/panicking.rs:288:9
11: 0x11151746e - std::panicking::rust_panic_with_hook::h11885c0070d88355
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/panicking.rs:691:13
12: 0x111517393 - std::panicking::begin_panic_handler::{{closure}}::he04c85739230a2b0
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/panicking.rs:582:13
13: 0x111516189 - std::sys_common::backtrace::__rust_end_short_backtrace::h0cf32c4b3dbfd4f6
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/sys_common/backtrace.rs:150:18
14: 0x1115170dd - rust_begin_unwind
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/panicking.rs:578:5
15: 0x111569063 - core::panicking::panic_fmt::h5f1768fb380922f0
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/core/src/panicking.rs:67:14
16: 0x1115691b6 - core::panicking::panic_bounds_check::h2cb4edcf4f6d0db4
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/core/src/panicking.rs:162:5
17: 0x110d4b5bd - http::header::name::HeaderName::from_static::h1104acea9f713b28
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/http-0.2.9/src/header/name.rs:1270:13
18: 0x10fd0f51f - control_server::presentation::handler::control::model::user::dto::UserIdentity::append_csrf_token::{{closure}}::hd4e9b67ed30dde00
at /Users/tommylike/Work/codes/rust-projects/gitee-signatrust/signatrust/src/presentation/handler/control/model/user/dto.rs:86:38
19: 0x10fe669ff - <actix_session::middleware::InnerSessionMiddleware<S,Store> as actix_service::Service<actix_web::service::ServiceRequest>>::call::{{closure}}::hdbf678bc122ccaac
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-session-0.7.2/src/middleware.rs:223:45
20: 0x10fb51682 - <core::pin::Pin<P> as core::future::future::Future>::poll::h4f51628a30b3d23e
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/core/src/future/future.rs:125:9
21: 0x10fc04e13 - <actix_service::map_err::MapErrFuture<A,Req,F,E> as core::future::future::Future>::poll::h2bf74de4f71847ef
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-service-2.0.2/src/map_err.rs:99:9
22: 0x10fe74abd - <actix_http::h2::dispatcher::Dispatcher<T,S,B,X,U> as core::future::future::Future>::poll::{{closure}}::h08950f7e295ff158
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-http-3.3.0/src/h2/dispatcher.rs:138:45
23: 0x10fc99617 - tokio::runtime::task::core::Core<T,S>::poll::{{closure}}::h8eb2019c5590e0b7
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/task/core.rs:223:17
24: 0x10fe1d691 - tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut::h12a4b4a9e3e100d4
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/loom/std/unsafe_cell.rs:14:9
25: 0x10fc978d7 - tokio::runtime::task::core::Core<T,S>::poll::h2c1ed7c0bc211f33
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/task/core.rs:212:13
26: 0x10fead3fd - tokio::runtime::task::harness::poll_future::{{closure}}::he28bad7421f2e372
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/task/harness.rs:476:19
27: 0x10fd2b765 - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h16f6f95a5ae1c1d6
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/core/src/panic/unwind_safe.rs:271:9
28: 0x10fc34d37 - std::panicking::try::do_call::h5cde804d6b409b70
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/panicking.rs:485:40
29: 0x10fc39f4d - ___rust_try
30: 0x10fc31bff - std::panicking::try::h75a1346babc9acfa
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/panicking.rs:449:19
31: 0x10fb80b0c - std::panic::catch_unwind::h9e781f7f9b9ddc41
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/panic.rs:140:14
32: 0x10fea8e82 - tokio::runtime::task::harness::poll_future::h2c548b71e36f41da
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/task/harness.rs:464:18
33: 0x10feaf69a - tokio::runtime::task::harness::Harness<T,S>::poll_inner::he175d0475dae75d0
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/task/harness.rs:198:27
34: 0x10feb2e85 - tokio::runtime::task::harness::Harness<T,S>::poll::h2da5aafdd694a950
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/task/harness.rs:152:15
35: 0x10fd7dced - tokio::runtime::task::raw::poll::hffd2afab02392c6e
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/task/raw.rs:255:5
36: 0x111159f16 - tokio::runtime::task::raw::RawTask::poll::h27c6e8f3de8b1068
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/task/raw.rs:200:18
37: 0x1111b6a82 - tokio::runtime::task::LocalNotified<S>::run::h05f29e26b28ddac7
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/task/mod.rs:394:9
38: 0x1111b7a81 - tokio::task::local::LocalSet::tick::{{closure}}::hda05f723cf079092
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/task/local.rs:615:63
39: 0x1111b79ef - tokio::runtime::coop::with_budget::hc5c16a707e088459
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/coop.rs:107:5
40: 0x1111b79ef - tokio::runtime::coop::budget::h452f8777170344cf
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/coop.rs:73:5
41: 0x1111b79ef - tokio::task::local::LocalSet::tick::h642e0e1d08be04e1
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/task/local.rs:615:31
42: 0x110459c3b - <tokio::task::local::RunUntil<T> as core::future::future::Future>::poll::{{closure}}::h2e06c4f09a1b0cf5
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/task/local.rs:927:16
43: 0x110458e26 - tokio::task::local::LocalSet::with::{{closure}}::hd2c6c26ca936e132
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/task/local.rs:684:13
44: 0x11046e736 - std::thread::local::LocalKey<T>::try_with::h938db2f30a7e74f6
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/thread/local.rs:270:16
45: 0x11046d741 - std::thread::local::LocalKey<T>::with::hce7f784e49ae613e
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/thread/local.rs:246:9
46: 0x110458c57 - tokio::task::local::LocalSet::with::h734dbc79120d0b90
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/task/local.rs:667:9
47: 0x110459afc - <tokio::task::local::RunUntil<T> as core::future::future::Future>::poll::h1f40860cc1a6a185
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/task/local.rs:913:9
48: 0x1104590d4 - tokio::task::local::LocalSet::run_until::{{closure}}::h108fc08874ed98a2
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/task/local.rs:573:19
49: 0x110459646 - <core::pin::Pin<P> as core::future::future::Future>::poll::hc9a50666e6a5b5f5
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/core/src/future/future.rs:125:9
50: 0x110487462 - tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}::{{closure}}::h48431fd5862eed62
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/scheduler/current_thread.rs:541:57
51: 0x110487375 - tokio::runtime::coop::with_budget::h6a0f2b681be5cdb9
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/coop.rs:107:5
52: 0x110487375 - tokio::runtime::coop::budget::h697f647d21335389
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/coop.rs:73:5
53: 0x110487375 - tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}::h7d4f19e1e8684e87
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/scheduler/current_thread.rs:541:25
54: 0x1104861b1 - tokio::runtime::scheduler::current_thread::Context::enter::h54af24cdbd86d2ba
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/scheduler/current_thread.rs:350:19
55: 0x110486a63 - tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::h79f41b4641663abb
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/scheduler/current_thread.rs:540:36
56: 0x11048681b - tokio::runtime::scheduler::current_thread::CoreGuard::enter::{{closure}}::hc02d83861c9b4863
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/scheduler/current_thread.rs:615:57
57: 0x110447965 - tokio::macros::scoped_tls::ScopedKey<T>::set::h5fc0d7eaf0c9743c
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/macros/scoped_tls.rs:61:9
58: 0x110486648 - tokio::runtime::scheduler::current_thread::CoreGuard::enter::hede518f857712d05
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/scheduler/current_thread.rs:615:27
59: 0x11048684d - tokio::runtime::scheduler::current_thread::CoreGuard::block_on::hb2e9195e444d48af
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/scheduler/current_thread.rs:530:19
60: 0x110485b0e - tokio::runtime::scheduler::current_thread::CurrentThread::block_on::h869164e2b395c132
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/scheduler/current_thread.rs:154:24
61: 0x11044b3b2 - tokio::runtime::runtime::Runtime::block_on::h7854303ba3f4d967
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/runtime.rs:302:47
62: 0x110476537 - actix_server::worker::ServerWorker::start::{{closure}}::ha21218e30e4540ec
at /Users/tommylike/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-server-2.2.0/src/worker.rs:402:29
63: 0x1104844ad - std::sys_common::backtrace::__rust_begin_short_backtrace::h4a3742090008581b
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/sys_common/backtrace.rs:134:18
64: 0x1104a404b - std::thread::Builder::spawn_unchecked_::{{closure}}::{{closure}}::h274240eee78284fc
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/thread/mod.rs:529:17
65: 0x11044617b - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h75bab6b6ddfa2ef6
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/core/src/panic/unwind_safe.rs:271:9
66: 0x11045106f - std::panicking::try::do_call::h7ffa196837292a71
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/panicking.rs:485:40
67: 0x110453d5d - ___rust_try
68: 0x11045094c - std::panicking::try::h61dc6daa54d42d70
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/panicking.rs:449:19
69: 0x1104a3767 - std::panic::catch_unwind::h159d1a1e6b7cb986
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/panic.rs:140:14
70: 0x1104a3767 - std::thread::Builder::spawn_unchecked_::{{closure}}::h6acbe680ce62d0d2
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/thread/mod.rs:528:30
71: 0x11049ad01 - core::ops::function::FnOnce::call_once{{vtable.shim}}::hbd72094bf8852eb1
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/core/src/ops/function.rs:250:5
72: 0x111519bf9 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h1f2ab9862b396f43
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/alloc/src/boxed.rs:1985:9
73: 0x111519bf9 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h9fb41ef341cdd807
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/alloc/src/boxed.rs:1985:9
74: 0x111519bf9 - std::sys::unix::thread::Thread::new::thread_start::h8d71b3b0aaa4f421
at /rustc/74c4821045c68d42bb8b8a7c998bdb5c2a72bd0d/library/std/src/sys/unix/thread.rs:108:17
75: 0x7ff80157c1d3 - __pthread_start
|
Beta Was this translation helpful? Give feedback.
Answered by
robjtede
Jul 5, 2023
Replies: 1 comment 1 reply
-
- https://docs.rs/http/latest/http/header/struct.HeaderName.html#method.from_static so - HeaderName::from_static("XSRF-TOKEN"),
+ HeaderName::from_static("xsrf-token"), |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
robjtede
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
- https://docs.rs/http/latest/http/header/struct.HeaderName.html#method.from_static
so