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

Use postcard to serialize cookie data #81

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions mendes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mendes"
version = "0.3.2"
version = "0.4.0"
edition = "2021"
rust-version = "1.63"
description = "Rust web toolkit for impatient perfectionists"
Expand All @@ -17,7 +17,7 @@ default = ["application"]
application = ["async-trait", "http", "mendes-macros", "percent-encoding", "serde", "serde_urlencoded"]
brotli = ["compression", "async-compression/brotli"]
compression = ["async-compression", "futures-util", "tokio-util"]
cookies = ["chrono", "http", "bincode", "data-encoding", "key", "mendes-macros", "serde-derive"]
cookies = ["chrono", "http", "data-encoding", "key", "postcard", "mendes-macros", "serde-derive"]
deflate = ["compression", "async-compression/deflate"]
forms = ["mendes-macros", "serde_urlencoded", "serde-derive"]
gzip = ["compression", "async-compression/gzip"]
Expand All @@ -32,7 +32,6 @@ serde-derive = ["serde/derive"]
[dependencies]
async-compression = { version = "0.4.0", features = ["tokio"], optional = true }
async-trait = { version = "0.1.24", optional = true }
bincode = { version = "1.3.1", optional = true }
bytes = { version = "1", optional = true }
chrono = { version = "0.4.23", optional = true, features = ["serde"] }
data-encoding = { version = "2.1.2", optional = true }
Expand All @@ -46,6 +45,7 @@ mendes-macros = { version = "0.2", path = "../mendes-macros", optional = true }
mime_guess = { version = "2.0.3", default-features = false, optional = true }
percent-encoding = { version = "2.1.0", default-features = false, optional = true }
pin-utils = { version = "0.1.0", optional = true }
postcard = { version = "1.0.6", default-features = false, features = ["use-std"], optional = true }
ring = { version = "0.16.11", optional = true }
serde = { version = "1.0.104", optional = true }
serde_json = { version = "1.0.48", optional = true }
Expand Down
7 changes: 3 additions & 4 deletions mendes/src/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use thiserror::Error;
use crate::key::{NONCE_LEN, TAG_LEN};

pub use crate::key::Key;
pub use bincode;
pub use mendes_macros::cookie;

#[cfg(feature = "application")]
Expand Down Expand Up @@ -126,7 +125,7 @@ pub trait CookieData: DeserializeOwned + Serialize {
let mut bytes = BASE64URL_NOPAD.decode(value.as_bytes()).ok()?;
let plain = key.decrypt(Self::NAME.as_bytes(), &mut bytes).ok()?;

let cookie = bincode::deserialize::<Cookie<Self>>(plain).ok()?;
let cookie = postcard::from_bytes::<Cookie<Self>>(plain).ok()?;
match SystemTime::now() < cookie.expires {
true => Some(cookie.data),
false => None,
Expand All @@ -150,7 +149,7 @@ impl<T: CookieData> Cookie<T> {
.checked_add(Duration::new(T::max_age() as u64, 0))
.ok_or(Error::ExpiryWindowTooLong)?;

let mut bytes = bincode::serialize(&Cookie { expires, data })?;
let mut bytes = postcard::to_stdvec(&Cookie { expires, data })?;
key.encrypt(T::NAME.as_bytes(), &mut bytes)?;
Ok(BASE64URL_NOPAD.encode(&bytes))
}
Expand Down Expand Up @@ -231,7 +230,7 @@ pub enum SameSite {
#[derive(Debug, Error)]
pub enum Error {
#[error("unstable to serialize cookie data")]
DataSerializationFailed(#[from] bincode::Error),
DataSerializationFailed(#[from] postcard::Error),
#[error("expiry window too long")]
ExpiryWindowTooLong,
#[error("non-ASCII cookie name")]
Expand Down