Skip to content

Commit

Permalink
restrict header fields
Browse files Browse the repository at this point in the history
  • Loading branch information
chunningham committed Jul 27, 2023
1 parent 80bfb2d commit 60530f4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ssi-ucan/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum Error {
VerificationMethodMismatch,
#[error("Missing UCAN field, expected: '{0}'")]
MissingUCANHeaderField(&'static str),
#[error("Header contains invalid fields")]
InvalidHeaderEntries(ssi_jws::Header),
#[error("Invalid DID URL")]
DIDURL,
#[error(transparent)]
Expand Down
17 changes: 17 additions & 0 deletions ssi-ucan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,27 @@ impl<F, A> Ucan<F, A> {
return Err(Error::MissingUCANHeaderField("type: JWT"));
}

// header can only contain 'typ' and 'alg' fields
if parts.header
!= (Header {
algorithm: parts.header.algorithm,
type_: Some("JWT".to_string()),
..Default::default()
})
{
return Err(Error::InvalidHeaderEntries(parts.header));
};

// aud must be a DID
if !payload.audience.starts_with("did:") {
return Err(Error::DIDURL);
}

// iss must be a DID
if !payload.issuer.starts_with("did:") {
return Err(Error::DIDURL);
}

Ok(Self {
header: parts.header,
payload,
Expand Down

0 comments on commit 60530f4

Please sign in to comment.