Skip to content

Commit

Permalink
Categorize v2 Utf-8 error as unreplyable
Browse files Browse the repository at this point in the history
Without the internal reply key, no reply can be made.
  • Loading branch information
DanGould committed Feb 5, 2025
1 parent bd9620e commit b09d837
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 4 additions & 0 deletions payjoin/src/receive/v2/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub(crate) enum InternalSessionError {
UnexpectedResponseSize(usize),
/// Unexpected status code
UnexpectedStatusCode(http::StatusCode),
/// Payload is not valid utf-8
Utf8(std::string::FromUtf8Error),
}

impl From<std::time::SystemTime> for Error {
Expand All @@ -52,6 +54,7 @@ impl From<HpkeError> for Error {
impl fmt::Display for SessionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.0 {
InternalSessionError::Utf8(e) => write!(f, "UTF-8 error: {}", e),
InternalSessionError::Expired(expiry) => write!(f, "Session expired at {:?}", expiry),
InternalSessionError::OhttpEncapsulation(e) =>
write!(f, "OHTTP Encapsulation Error: {}", e),
Expand All @@ -71,6 +74,7 @@ impl fmt::Display for SessionError {
impl error::Error for SessionError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match &self.0 {
InternalSessionError::Utf8(e) => Some(e),
InternalSessionError::Expired(_) => None,
InternalSessionError::OhttpEncapsulation(e) => Some(e),
InternalSessionError::Hpke(e) => Some(e),
Expand Down
8 changes: 5 additions & 3 deletions payjoin/src/receive/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use serde::de::Deserializer;
use serde::{Deserialize, Serialize};
use url::Url;

use super::error::{Error, InputContributionError};
use super::error::{Error, InputContributionError, UnreplyableError};
use super::{
v1, InternalPayloadError, JsonError, OutputSubstitutionError, ReplyableError, SelectionError,
v1, JsonError, OutputSubstitutionError, ReplyableError, SelectionError,
};
use crate::hpke::{decrypt_message_a, encrypt_message_b, HpkeKeyPair, HpkePublicKey};
use crate::ohttp::{ohttp_decapsulate, ohttp_encapsulate, OhttpEncapsulationError, OhttpKeys};
Expand Down Expand Up @@ -154,7 +154,7 @@ impl Receiver {
let (payload_bytes, e) = decrypt_message_a(&response, self.context.s.secret_key().clone())?;
self.context.e = Some(e);
let payload = String::from_utf8(payload_bytes)
.map_err(|e| Error::Replyable(InternalPayloadError::Utf8(e).into()))?;
.map_err(|e| Error::Unreplyable(UnreplyableError::V2(InternalSessionError::Utf8(e).into())))?;
self.unchecked_from_payload(payload).map_err(Error::Replyable)
}

Expand Down Expand Up @@ -580,6 +580,8 @@ mod test {
use ohttp::{KeyId, SymmetricSuite};
use once_cell::sync::Lazy;

use crate::receive::InternalPayloadError;

use super::*;

const KEY_ID: KeyId = 1;
Expand Down

0 comments on commit b09d837

Please sign in to comment.