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

Redis storage: return an error if a non serializable value is sent. #3597

Merged
merged 5 commits into from
Aug 17, 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: 6 additions & 0 deletions .changesets/fix_igni_safely_deal_with_invalid_redis_keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Redis storage: return an error instead if a non serializable value is sent. ([#3594](https://github.com/apollographql/router/issues/3594))

This changeset returns an error if a value couldn't be serialized before being sent to the redis storage backend.
It also logs the error in console and prompts you to open an issue (This message showing up would be a router bug!).

By [@o0Ignition0o](https://github.com/o0Ignition0o) in https://github.com/apollographql/router/pull/3597
31 changes: 29 additions & 2 deletions apollo-router/src/cache/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ where
type Error = RedisError;

fn try_into(self) -> Result<fred::types::RedisValue, Self::Error> {
let v = serde_json::to_vec(&self.0)
.expect("JSON serialization should not fail for redis values");
let v = serde_json::to_vec(&self.0).map_err(|e| {
tracing::error!("couldn't serialize value to redis {}. This is a bug in the router, please file an issue: https://github.com/apollographql/router/issues/new", e);
RedisError::new(
RedisErrorKind::Parse,
format!("couldn't serialize value to redis {}", e),
)
})?;

Ok(fred::types::RedisValue::Bytes(v.into()))
}
Expand Down Expand Up @@ -332,3 +337,25 @@ impl RedisCacheStorage {
tracing::trace!("insert result {:?}", r);
}
}

#[cfg(test)]
mod test {
use std::time::SystemTime;

#[test]
fn ensure_invalid_payload_serialization_doesnt_fail() {
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
struct Stuff {
time: SystemTime,
}

let invalid_json_payload = super::RedisValue(Stuff {
// this systemtime is invalid, serialization will fail
time: std::time::UNIX_EPOCH - std::time::Duration::new(1, 0),
});

let as_value: Result<fred::types::RedisValue, _> = invalid_json_payload.try_into();

assert!(as_value.is_err());
}
}