-
Notifications
You must be signed in to change notification settings - Fork 79
One expiration error: Separate session replay & protocol operation #1036
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
Changes from all commits
ce55fe3
5e700f3
e3522c2
7bdc639
d9e7255
95a2bed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; | |
| use super::{ReceiveSession, SessionContext}; | ||
| use crate::output_substitution::OutputSubstitution; | ||
| use crate::persist::SessionPersister; | ||
| use crate::receive::v2::{extract_err_req, SessionError}; | ||
| use crate::receive::v2::{extract_err_req, InternalSessionError, SessionError}; | ||
| use crate::receive::{common, JsonReply, OriginalPayload, PsbtContext}; | ||
| use crate::{ImplementationError, IntoUrl, PjUri, Request}; | ||
|
|
||
|
|
@@ -17,7 +17,6 @@ impl std::fmt::Display for ReplayError { | |
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| use InternalReplayError::*; | ||
| match &self.0 { | ||
| SessionExpired(expiry) => write!(f, "Session expired at {expiry:?}"), | ||
| InvalidStateAndEvent(state, event) => write!( | ||
| f, | ||
| "Invalid combination of state ({state:?}) and event ({event:?}) during replay", | ||
|
|
@@ -34,8 +33,6 @@ impl From<InternalReplayError> for ReplayError { | |
|
|
||
| #[derive(Debug)] | ||
| pub(crate) enum InternalReplayError { | ||
| /// Session expired | ||
| SessionExpired(SystemTime), | ||
| /// Invalid combination of state and event | ||
| InvalidStateAndEvent(Box<ReceiveSession>, Box<SessionEvent>), | ||
| /// Application storage error | ||
|
|
@@ -48,6 +45,7 @@ pub fn replay_event_log<P>(persister: &P) -> Result<(ReceiveSession, SessionHist | |
| where | ||
| P: SessionPersister, | ||
| P::SessionEvent: Into<SessionEvent> + Clone, | ||
| P::SessionEvent: From<SessionEvent>, | ||
| { | ||
| let logs = persister | ||
| .load() | ||
|
|
@@ -68,6 +66,21 @@ where | |
| })?; | ||
| } | ||
|
|
||
| let ctx = | ||
| history.session_context().expect("Session context should be present after the first event"); | ||
| if SystemTime::now() > ctx.expiry { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I expect this needs a mutants exclusion too, see #1036 (review)
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. copy that. I excluded this pattern: ""replace > with >= in replay_event_log"," We'll see if that works. Thanks |
||
| // Session has expired: close the session and persist a fatal error | ||
| let err = SessionError(InternalSessionError::Expired(ctx.expiry)); | ||
| persister | ||
| .save_event(SessionEvent::SessionInvalid(err.to_string(), None).into()) | ||
| .map_err(|e| InternalReplayError::PersistenceFailure(ImplementationError::new(e)))?; | ||
| persister | ||
| .close() | ||
| .map_err(|e| InternalReplayError::PersistenceFailure(ImplementationError::new(e)))?; | ||
|
|
||
| return Ok((ReceiveSession::TerminalFailure, history)); | ||
| } | ||
|
|
||
| Ok((receiver, history)) | ||
| } | ||
|
|
||
|
|
@@ -193,6 +206,8 @@ pub enum SessionEvent { | |
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::time::Duration; | ||
|
|
||
| use payjoin_test_utils::{BoxError, EXAMPLE_URL}; | ||
|
|
||
| use super::*; | ||
|
|
@@ -324,34 +339,26 @@ mod tests { | |
| } | ||
|
|
||
| #[test] | ||
| fn test_replaying_unchecked_proposal() -> Result<(), BoxError> { | ||
| let session_context = SHARED_CONTEXT.clone(); | ||
| let original = original_from_test_vector(); | ||
| let reply_key = Some(crate::HpkeKeyPair::gen_keypair().1); | ||
|
|
||
| fn test_replaying_session_creation_with_expired_session() -> Result<(), BoxError> { | ||
| let session_context = SessionContext { | ||
| expiry: SystemTime::now() - Duration::from_secs(1), | ||
| ..SHARED_CONTEXT.clone() | ||
| }; | ||
| let test = SessionHistoryTest { | ||
| events: vec![ | ||
| SessionEvent::Created(session_context.clone()), | ||
| SessionEvent::UncheckedOriginalPayload((original.clone(), reply_key.clone())), | ||
| ], | ||
| events: vec![SessionEvent::Created(session_context.clone())], | ||
| expected_session_history: SessionHistoryExpectedOutcome { | ||
| psbt_with_fee_contributions: None, | ||
| fallback_tx: None, | ||
| }, | ||
| expected_receiver_state: ReceiveSession::UncheckedOriginalPayload(Receiver { | ||
| state: UncheckedOriginalPayload { | ||
| original, | ||
| session_context: SessionContext { reply_key, ..session_context }, | ||
| }, | ||
| }), | ||
| expected_receiver_state: ReceiveSession::TerminalFailure, | ||
| }; | ||
| // TODO: should check for the expired error message off the session history | ||
| run_session_history_test(test) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_replaying_unchecked_proposal_expiry() { | ||
| let now = SystemTime::now(); | ||
| let session_context = SessionContext { expiry: now, ..SHARED_CONTEXT.clone() }; | ||
| fn test_replaying_unchecked_proposal() -> Result<(), BoxError> { | ||
| let session_context = SHARED_CONTEXT.clone(); | ||
| let original = original_from_test_vector(); | ||
| let reply_key = Some(crate::HpkeKeyPair::gen_keypair().1); | ||
|
|
||
|
|
@@ -371,15 +378,7 @@ mod tests { | |
| }, | ||
| }), | ||
| }; | ||
| let session_history = run_session_history_test(test); | ||
|
|
||
| match session_history { | ||
| Err(error) => assert_eq!( | ||
| error.to_string(), | ||
| ReplayError::from(InternalReplayError::SessionExpired(now)).to_string() | ||
| ), | ||
| Ok(_) => panic!("Expected session expiry error, got success"), | ||
| } | ||
| run_session_history_test(test) | ||
| } | ||
|
|
||
| #[test] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this expect makes more sense after we remove uninitlized as a session state #1014