use minicbor::data::Type;
use minicbor::{Decode, Decoder, Encode, Encoder};
use crate::error::DecodeError;
use crate::identity::{DeviceId, IdentityKey};
use crate::strict;
use crate::value::{CanonicalMap, CborValue, CdeKey, CdeMapBuilder, HeaderLabel};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VerbError {
BadShape,
Malformed(String),
}
impl core::fmt::Display for VerbError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
VerbError::BadShape => write!(f, "a capability verb is '<subsystem>:<action>'"),
VerbError::Malformed(v) => write!(
f,
"verb {v:?} matches no core, namespaced, or private-use tier"
),
}
}
}
impl std::error::Error for VerbError {}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CapabilityVerb(pub String);
impl CapabilityVerb {
pub fn validate(verb: &str) -> Result<(), VerbError> {
let Some((head, action)) = verb.split_once(':') else {
return Err(VerbError::BadShape);
};
let action_ok = |a: &str| {
let bytes = a.as_bytes();
!bytes.is_empty()
&& bytes[0].is_ascii_lowercase()
&& bytes[1..]
.iter()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || *b == b'-')
};
let loose_ok = |a: &str| {
!a.is_empty()
&& a.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'.' || b == b'-')
};
let head_core_ok = |h: &str| {
let bytes = h.as_bytes();
!bytes.is_empty()
&& bytes[0].is_ascii_lowercase()
&& bytes[1..]
.iter()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || *b == b'-')
};
if head_core_ok(head) && action_ok(action) {
return Ok(());
}
if let Some((registrant, local)) = head.split_once('/') {
let registrant_ok = !registrant.is_empty()
&& registrant.bytes().all(|b| {
b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'.' || b == b'-'
});
if registrant_ok && loose_ok(local) && loose_ok(action) {
return Ok(());
}
}
if let Some(rest) = head.strip_prefix("x-") {
if loose_ok(rest) && loose_ok(action) {
return Ok(());
}
}
Err(VerbError::Malformed(verb.to_owned()))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScopeError {
EmptyKind,
EmptyPath,
}
impl core::fmt::Display for ScopeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ScopeError::EmptyKind => write!(f, "capability scope kind must be non-empty"),
ScopeError::EmptyPath => write!(f, "capability scope path must be non-empty when present (absent means the whole-scope root)"),
}
}
}
impl std::error::Error for ScopeError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CapabilityScope {
pub kind: String,
pub path: Option<String>,
}
impl CapabilityScope {
pub fn validate(&self) -> Result<(), ScopeError> {
if self.kind.is_empty() {
return Err(ScopeError::EmptyKind);
}
if self.path.as_deref() == Some("") {
return Err(ScopeError::EmptyPath);
}
Ok(())
}
pub fn is_within(&self, parent: &CapabilityScope) -> bool {
if self.kind != parent.kind {
return false;
}
let Some(parent_path) = &parent.path else {
return true;
};
let Some(path) = &self.path else {
return false;
};
if has_relative_segment(path) || has_relative_segment(parent_path) {
return false;
}
if path == parent_path {
return true;
}
if !path.starts_with(parent_path.as_str()) {
return false;
}
parent_path.ends_with('/') || path[parent_path.len()..].starts_with('/')
}
}
fn has_relative_segment(path: &str) -> bool {
path.split('/')
.any(|segment| segment == "." || segment == "..")
}
impl Encode<()> for CapabilityScope {
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut Encoder<W>,
_ctx: &mut (),
) -> Result<(), minicbor::encode::Error<W::Error>> {
let len = 1 + usize::from(self.path.is_some());
e.map(len as u64)?;
e.str("kind")?.str(&self.kind)?;
if let Some(path) = &self.path {
e.str("path")?.str(path)?;
}
e.ok()
}
}
impl Decode<'_, ()> for CapabilityScope {
fn decode(d: &mut Decoder<'_>, _ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
scope_from(d).map_err(minicbor::decode::Error::custom)
}
}
pub(crate) fn scope_from(d: &mut Decoder<'_>) -> Result<CapabilityScope, DecodeError> {
let mut map = strict::MapDecoder::new(d)?;
let mut kind: Option<String> = None;
let mut path: Option<String> = None;
while let Some(key) = map.next_key(d)? {
match key {
"kind" => strict::set_once(&mut kind, strict::text_value(d)?)?,
"path" => strict::set_once(&mut path, strict::text_value(d)?)?,
other => return Err(DecodeError::UnknownKey(other.to_owned())),
}
}
Ok(CapabilityScope {
kind: kind.ok_or(DecodeError::MissingField("kind"))?,
path,
})
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CoseTokenHeaders {
pub alg: Option<i64>,
pub kid: Option<Vec<u8>>,
pub extra: CanonicalMap<HeaderLabel, CborValue>,
}
impl CoseTokenHeaders {
pub fn new() -> Self {
Self::default()
}
pub fn alg(&self) -> Option<i64> {
if let Some(alg) = self.alg {
return Some(alg);
}
match self.extra.get(&HeaderLabel::Text("1".to_owned())) {
Some(CborValue::Int(v)) => Some(*v),
Some(CborValue::UInt(v)) => i64::try_from(*v).ok(),
_ => None,
}
}
pub fn kid(&self) -> Option<&[u8]> {
if let Some(kid) = &self.kid {
return Some(kid);
}
match self.extra.get(&HeaderLabel::Text("4".to_owned())) {
Some(CborValue::Bytes(b)) => Some(b),
_ => None,
}
}
}
impl Encode<()> for CoseTokenHeaders {
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut Encoder<W>,
_ctx: &mut (),
) -> Result<(), minicbor::encode::Error<W::Error>> {
let mut builder = CdeMapBuilder::new();
if let Some(alg) = self.alg {
builder.push(&HeaderLabel::Int(HeaderLabel::ALG), &alg);
}
if let Some(kid) = &self.kid {
builder.push_bytes(&HeaderLabel::Int(HeaderLabel::KID), kid);
}
for (label, value) in self.extra.iter() {
let mut key_buf = Vec::new();
let mut key_enc = Encoder::new(&mut key_buf);
label
.write_key(&mut key_enc)
.unwrap_or_else(|_| unreachable!("Vec<u8> writes are infallible"));
let mut value_buf = Vec::new();
let mut value_enc = Encoder::new(&mut value_buf);
value
.encode(&mut value_enc, &mut ())
.unwrap_or_else(|_| unreachable!("Vec<u8> writes are infallible"));
builder.push_raw(key_buf, value_buf);
}
builder.write(e)
}
}
impl Decode<'_, ()> for CoseTokenHeaders {
fn decode(d: &mut Decoder<'_>, _ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
headers_from(d).map_err(minicbor::decode::Error::custom)
}
}
pub(crate) fn headers_from(d: &mut Decoder<'_>) -> Result<CoseTokenHeaders, DecodeError> {
let n = strict::definite_map(d)?;
let mut order = strict::KeyOrder::new();
let mut headers = CoseTokenHeaders::new();
for _ in 0..n {
let label = HeaderLabel::decode_strict(d)?;
order.push(&label.encoded())?;
match label {
HeaderLabel::Int(HeaderLabel::ALG) => {
strict::set_once(&mut headers.alg, strict::int_value(d)?)?;
}
HeaderLabel::Int(HeaderLabel::KID) => {
strict::set_once(&mut headers.kid, strict::bytes_value(d)?)?;
}
label => {
let value = CborValue::decode_strict(d)?;
headers.extra.insert(label, value)?;
}
}
}
Ok(headers)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CoseSign1 {
pub protected: Vec<u8>,
pub unprotected: CoseTokenHeaders,
pub payload: Option<Vec<u8>>,
pub signature: Vec<u8>,
}
impl CoseSign1 {
pub fn decode_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
let mut d = Decoder::new(bytes);
let value = cose_sign1_from(&mut d)?;
if d.position() != bytes.len() {
return Err(DecodeError::TrailingBytes {
consumed: d.position(),
total: bytes.len(),
});
}
Ok(value)
}
pub fn encode_to_vec(&self) -> Vec<u8> {
minicbor::to_vec(self).unwrap_or_else(|_| unreachable!("Vec<u8> writes are infallible"))
}
pub fn decode_protected(&self) -> Result<CoseTokenHeaders, DecodeError> {
let mut d = Decoder::new(&self.protected);
let headers = headers_from(&mut d)?;
if d.position() != self.protected.len() {
return Err(DecodeError::TrailingBytes {
consumed: d.position(),
total: self.protected.len(),
});
}
Ok(headers)
}
pub fn decode_claims(&self) -> Result<TokenClaims, DecodeError> {
let payload = self
.payload
.as_deref()
.ok_or(DecodeError::Constraint("token payload is nil"))?;
TokenClaims::decode_bytes(payload)
}
pub fn decode_handle_claims(&self) -> Result<crate::discovery::HandleClaims, DecodeError> {
let payload = self
.payload
.as_deref()
.ok_or(DecodeError::Constraint("handle-record payload is nil"))?;
crate::discovery::HandleClaims::decode_bytes(payload)
}
pub fn decode_room_notice_claims(&self) -> Result<crate::room::RoomNoticeClaims, DecodeError> {
let payload = self
.payload
.as_deref()
.ok_or(DecodeError::Constraint("room-notice payload is nil"))?;
crate::room::RoomNoticeClaims::decode_bytes(payload)
}
}
impl Encode<()> for CoseSign1 {
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut Encoder<W>,
_ctx: &mut (),
) -> Result<(), minicbor::encode::Error<W::Error>> {
e.array(4)?;
e.bytes(&self.protected)?;
self.unprotected.encode(e, &mut ())?;
match &self.payload {
Some(payload) => e.bytes(payload)?,
None => e.null()?,
};
e.bytes(&self.signature)?;
e.ok()
}
}
impl Decode<'_, ()> for CoseSign1 {
fn decode(d: &mut Decoder<'_>, _ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
cose_sign1_from(d).map_err(minicbor::decode::Error::custom)
}
}
pub(crate) fn cose_sign1_from(d: &mut Decoder<'_>) -> Result<CoseSign1, DecodeError> {
let arity = strict::definite_array(d)?;
if arity != 4 {
return Err(DecodeError::WrongArity {
expected: 4,
found: arity,
});
}
let protected = strict::bytes_value(d)?;
let unprotected = headers_from(d)?;
let payload = match d.datatype().map_err(DecodeError::from_minicbor)? {
Type::Bytes => Some(strict::bytes_value(d)?),
Type::Null => {
d.null().map_err(DecodeError::from_minicbor)?;
None
}
other => {
return Err(DecodeError::UnexpectedType {
expected: "byte string or null payload",
found: other.to_string(),
})
}
};
let signature = strict::bytes_value(d)?;
Ok(CoseSign1 {
protected,
unprotected,
payload,
signature,
})
}
pub type CapabilityToken = CoseSign1;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TokenClaims {
pub token_id: Vec<u8>,
pub issuer: DeviceId,
pub issuer_key: IdentityKey,
pub bearer: DeviceId,
pub capability: CapabilityVerb,
pub scope: CapabilityScope,
pub expires: u64,
pub not_before: Option<u64>,
pub valid_until: Option<u64>,
pub conditions: Option<Vec<u8>>,
pub parent: Option<Vec<u8>>,
pub delegations_remaining: Option<u64>,
pub authorised_by: Option<Vec<u8>>,
pub grants_capability: Option<CapabilityVerb>,
pub requests_capability: Option<CapabilityVerb>,
pub extra: CanonicalMap<String, CborValue>,
}
impl TokenClaims {
pub fn validate_verbs(&self) -> Result<(), VerbError> {
CapabilityVerb::validate(&self.capability.0)?;
for verb in [&self.grants_capability, &self.requests_capability]
.into_iter()
.flatten()
{
CapabilityVerb::validate(&verb.0)?;
}
Ok(())
}
pub fn decode_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
let mut d = Decoder::new(bytes);
let claims = token_claims_from(&mut d)?;
if d.position() != bytes.len() {
return Err(DecodeError::TrailingBytes {
consumed: d.position(),
total: bytes.len(),
});
}
Ok(claims)
}
pub fn encode_to_vec(&self) -> Vec<u8> {
minicbor::to_vec(self).unwrap_or_else(|_| unreachable!("Vec<u8> writes are infallible"))
}
}
impl Encode<()> for TokenClaims {
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut Encoder<W>,
_ctx: &mut (),
) -> Result<(), minicbor::encode::Error<W::Error>> {
let mut builder = CdeMapBuilder::new();
builder.push_bytes("token-id", &self.token_id);
builder.push("issuer", &self.issuer);
builder.push("issuer-key", &self.issuer_key);
builder.push("bearer", &self.bearer);
builder.push("capability", &self.capability.0.as_str());
builder.push("scope", &self.scope);
builder.push("expires", &self.expires);
if let Some(not_before) = self.not_before {
builder.push("not-before", ¬_before);
}
if let Some(valid_until) = self.valid_until {
builder.push("valid-until", &valid_until);
}
if let Some(conditions) = &self.conditions {
builder.push_bytes("conditions", conditions);
}
if let Some(parent) = &self.parent {
builder.push_bytes("parent", parent);
}
if let Some(remaining) = self.delegations_remaining {
builder.push("delegations-remaining", &remaining);
}
if let Some(authorised_by) = &self.authorised_by {
builder.push_bytes("authorised-by", authorised_by);
}
if let Some(grants) = &self.grants_capability {
builder.push("grants-capability", &grants.0.as_str());
}
if let Some(requests) = &self.requests_capability {
builder.push("requests-capability", &requests.0.as_str());
}
for (key, value) in self.extra.iter() {
let mut value_buf = Vec::new();
let mut value_enc = Encoder::new(&mut value_buf);
value
.encode(&mut value_enc, &mut ())
.unwrap_or_else(|_| unreachable!("Vec<u8> writes are infallible"));
builder.push_raw(key.encoded(), value_buf);
}
builder.write(e)
}
}
impl Decode<'_, ()> for TokenClaims {
fn decode(d: &mut Decoder<'_>, _ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
token_claims_from(d).map_err(minicbor::decode::Error::custom)
}
}
pub(crate) fn token_claims_from(d: &mut Decoder<'_>) -> Result<TokenClaims, DecodeError> {
let mut map = strict::MapDecoder::new(d)?;
let mut token_id: Option<Vec<u8>> = None;
let mut issuer: Option<DeviceId> = None;
let mut issuer_key: Option<IdentityKey> = None;
let mut bearer: Option<DeviceId> = None;
let mut capability: Option<String> = None;
let mut scope: Option<CapabilityScope> = None;
let mut expires: Option<u64> = None;
let mut not_before: Option<u64> = None;
let mut valid_until: Option<u64> = None;
let mut conditions: Option<Vec<u8>> = None;
let mut parent: Option<Vec<u8>> = None;
let mut delegations_remaining: Option<u64> = None;
let mut authorised_by: Option<Vec<u8>> = None;
let mut grants_capability: Option<String> = None;
let mut requests_capability: Option<String> = None;
let mut extra = CanonicalMap::new();
while let Some(key) = map.next_key(d)? {
match key {
"token-id" => strict::set_once(&mut token_id, strict::bytes_value(d)?)?,
"issuer" => strict::set_once(&mut issuer, crate::identity::device_id_from(d)?)?,
"issuer-key" => {
strict::set_once(&mut issuer_key, crate::identity::identity_key_from(d)?)?
}
"bearer" => strict::set_once(&mut bearer, crate::identity::device_id_from(d)?)?,
"capability" => strict::set_once(&mut capability, strict::text_value(d)?)?,
"scope" => strict::set_once(&mut scope, scope_from(d)?)?,
"expires" => strict::set_once(&mut expires, strict::uint_value(d)?)?,
"not-before" => strict::set_once(&mut not_before, strict::uint_value(d)?)?,
"valid-until" => strict::set_once(&mut valid_until, strict::uint_value(d)?)?,
"conditions" => strict::set_once(&mut conditions, strict::bytes_value(d)?)?,
"parent" => strict::set_once(&mut parent, strict::bytes_value(d)?)?,
"delegations-remaining" => {
strict::set_once(&mut delegations_remaining, strict::uint_value(d)?)?
}
"authorised-by" => strict::set_once(&mut authorised_by, strict::bytes_value(d)?)?,
"grants-capability" => {
strict::set_once(&mut grants_capability, strict::text_value(d)?)?
}
"requests-capability" => {
strict::set_once(&mut requests_capability, strict::text_value(d)?)?
}
other => {
let value = CborValue::decode_strict(d)?;
extra.insert(other.to_owned(), value)?;
}
}
}
Ok(TokenClaims {
token_id: token_id.ok_or(DecodeError::MissingField("token-id"))?,
issuer: issuer.ok_or(DecodeError::MissingField("issuer"))?,
issuer_key: issuer_key.ok_or(DecodeError::MissingField("issuer-key"))?,
bearer: bearer.ok_or(DecodeError::MissingField("bearer"))?,
capability: CapabilityVerb(capability.ok_or(DecodeError::MissingField("capability"))?),
scope: scope.ok_or(DecodeError::MissingField("scope"))?,
expires: expires.ok_or(DecodeError::MissingField("expires"))?,
not_before,
valid_until,
conditions,
parent,
delegations_remaining,
authorised_by,
grants_capability: grants_capability.map(CapabilityVerb),
requests_capability: requests_capability.map(CapabilityVerb),
extra,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn token_claims_rejects_duplicate_typed_key() {
let bytes = [
0xa2, 0x67, b'e', b'x', b'p', b'i', b'r', b'e', b's', 0x01, 0x67, b'e', b'x', b'p',
b'i', b'r', b'e', b's', 0x02,
];
assert_eq!(
TokenClaims::decode_bytes(&bytes),
Err(DecodeError::DuplicateKey)
);
}
#[test]
fn token_claims_rejects_unsorted_keys() {
let id = [0x11u8; 32];
let mut bytes = vec![0xa2, 0x66];
bytes.extend_from_slice(b"issuer");
bytes.push(0x58);
bytes.push(32);
bytes.extend_from_slice(&id);
bytes.push(0x66);
bytes.extend_from_slice(b"bearer");
bytes.push(0x58);
bytes.push(32);
bytes.extend_from_slice(&id);
assert_eq!(
TokenClaims::decode_bytes(&bytes),
Err(DecodeError::UnsortedMapKeys)
);
}
#[test]
fn token_claims_rejects_non_minimal_integer_head() {
let mut bytes = vec![0xa2, 0x63];
bytes.extend_from_slice(b"alg");
bytes.extend_from_slice(&[0x38, 0x06]);
bytes.push(0x6a);
bytes.extend_from_slice(b"public-key");
bytes.extend_from_slice(&[0x58, 0x41]);
bytes.extend_from_slice(&[0x04; 65]);
let mut d = Decoder::new(&bytes);
assert_eq!(
crate::identity::identity_key_from(&mut d),
Err(DecodeError::NonMinimalHead)
);
}
#[test]
fn verb_tiers() {
for verb in [
"pin:write",
"exec:pty",
"exec:proc",
"a-b:c-d",
"example.com/rooms:list",
"exa.dev/thing.v2:run",
"a.b/c:x",
"x-private:thing",
"x-a_.-b:x_y.-z",
] {
assert_eq!(CapabilityVerb::validate(verb), Ok(()), "{verb}");
}
for verb in [
"nolons",
"two:colons:here",
"EXEC:pty",
"exec:",
":exec",
"exa.dev:run", "x-bad verb:thing",
"-lead:action",
"exec:1",
"exec:-x",
"exec:1pty",
] {
assert_ne!(CapabilityVerb::validate(verb), Ok(()), "{verb}");
}
}
#[test]
fn capability_verb_edges_match_the_grammar() {
for verb in [
"",
" exec:pty",
"exec:pty ",
"exec:pty\n",
"exec:pty\0",
"exec\0:pty",
"exec:pty\r",
"exec:p\u{e9}ty",
"ex\u{e9}c:pty",
"exec:pty\u{2028}",
"Exec:pty",
"example.com/Tool:run:more",
"Example.com/tool:run",
"example.com/tool:run\n",
"example.com/tool:",
"example.com/:run",
"/tool:run",
"x-local:run\n",
"x-local:",
"X-local:run",
] {
assert_ne!(CapabilityVerb::validate(verb), Ok(()), "{verb:?}");
}
let long_action = "a".repeat(10_000);
assert_eq!(
CapabilityVerb::validate(&format!("exec:{long_action}")),
Ok(())
);
}
#[test]
fn claims_verbs_are_all_validated() {
let ok = CapabilityVerb("exec:pty".to_owned());
let bad = CapabilityVerb("two:colons:here".to_owned());
let claims = |capability: &CapabilityVerb,
grants: Option<&CapabilityVerb>,
requests: Option<&CapabilityVerb>| TokenClaims {
token_id: vec![0; 16],
issuer: crate::identity::DeviceId([1; 32]),
issuer_key: crate::identity::IdentityKey {
alg: -8,
public_key: vec![2; 32],
},
bearer: crate::identity::DeviceId([3; 32]),
capability: capability.clone(),
scope: CapabilityScope {
kind: "folder".to_owned(),
path: None,
},
expires: 1,
not_before: None,
valid_until: None,
conditions: None,
parent: None,
delegations_remaining: None,
authorised_by: None,
grants_capability: grants.cloned(),
requests_capability: requests.cloned(),
extra: CanonicalMap::new(),
};
assert_eq!(claims(&ok, Some(&ok), Some(&ok)).validate_verbs(), Ok(()));
assert_eq!(claims(&ok, None, None).validate_verbs(), Ok(()));
assert!(claims(&bad, None, None).validate_verbs().is_err());
assert!(claims(&ok, Some(&bad), None).validate_verbs().is_err());
assert!(claims(&ok, None, Some(&bad)).validate_verbs().is_err());
}
#[test]
fn scope_within_agrees_with_the_typescript_rule_at_every_edge() {
let within = |child: &str, parent: &str| {
let scope = |path: &str| CapabilityScope {
kind: "folder".to_owned(),
path: Some(path.to_owned()),
};
scope(child).is_within(&scope(parent))
};
assert!(within("/work/x", "/"));
assert!(within("/", "/"));
assert!(within("/work/sub", "/work/"));
assert!(within("/work/", "/work/"));
assert!(
within("/work//x", "/work/"),
"doubled slashes are not collapsed"
);
assert!(
!within("/work", "/work/"),
"the parent's own name without its slash is outside it"
);
assert!(!within("/other", "/work/"));
assert!(within("/work", "/work"));
assert!(within("/work/sub", "/work"));
assert!(within("/work/", "/work"));
assert!(
!within("/workshop", "/work"),
"a shared prefix is not a shared segment"
);
assert!(!within("/work-other", "/work"));
assert!(!within("/work/./x", "/work"));
assert!(!within("/work/../x", "/work"));
assert!(!within("/work", "/work/.."));
assert!(!within("/work/..", "/work/.."));
assert!(!within("/.", "/"));
assert!(!within("", "/work"));
assert!(within("/x", ""));
assert!(!within("x", ""));
}
#[test]
fn scope_within_narrows() {
let root = CapabilityScope {
kind: "folder".to_owned(),
path: None,
};
let work = CapabilityScope {
kind: "folder".to_owned(),
path: Some("/work".to_owned()),
};
let subdir = CapabilityScope {
kind: "folder".to_owned(),
path: Some("/work/subdir".to_owned()),
};
let sibling = CapabilityScope {
kind: "folder".to_owned(),
path: Some("/work-other".to_owned()),
};
let other_kind = CapabilityScope {
kind: "node".to_owned(),
path: Some("/work".to_owned()),
};
assert!(subdir.is_within(&work));
assert!(work.is_within(&work));
assert!(work.is_within(&root));
assert!(
!sibling.is_within(&work),
"prefix must respect the segment boundary"
);
let escape = CapabilityScope {
kind: "folder".to_owned(),
path: Some("/work/../org".to_owned()),
};
assert!(
!escape.is_within(&work),
"a relative segment normalises outside the parent: fail closed, not lexically"
);
let dot = CapabilityScope {
kind: "folder".to_owned(),
path: Some("/work/./sub".to_owned()),
};
assert!(!dot.is_within(&work), "'.' segments never narrow either");
assert!(
!root.is_within(&work),
"root is wider than a path-scoped parent"
);
assert!(!subdir.is_within(&other_kind));
}
#[test]
fn scope_validation() {
assert_eq!(
CapabilityScope {
kind: "folder".to_owned(),
path: None
}
.validate(),
Ok(())
);
assert_eq!(
CapabilityScope {
kind: String::new(),
path: None
}
.validate(),
Err(ScopeError::EmptyKind)
);
assert_eq!(
CapabilityScope {
kind: "folder".to_owned(),
path: Some(String::new())
}
.validate(),
Err(ScopeError::EmptyPath)
);
}
#[test]
fn headers_accept_both_label_forms_but_reencode_only_what_was_there() {
let headers = CoseTokenHeaders {
alg: Some(-7),
kid: Some(vec![0x11; 32]),
extra: CanonicalMap::new(),
};
let bytes = minicbor::to_vec(&headers).expect("encode");
let back: CoseTokenHeaders = minicbor::decode(&bytes).expect("decode");
assert_eq!(back, headers);
assert_eq!(bytes[1], 0x01, "alg encoded under integer label 1");
let mut text_form: Vec<u8> = vec![0xa2, 0x61, b'1', 0x26, 0x61, b'4', 0x58, 0x20];
text_form.extend(core::iter::repeat_n(0x11, 32));
let decoded: CoseTokenHeaders = minicbor::decode(&text_form).expect("decode");
assert_eq!(decoded.alg(), Some(-7));
assert_eq!(decoded.kid(), Some(vec![0x11; 32].as_slice()));
assert_eq!(minicbor::to_vec(&decoded).expect("re-encode"), text_form);
}
#[test]
fn cose_sign1_nil_payload_round_trip() {
let cose = CoseSign1 {
protected: vec![0xa0],
unprotected: CoseTokenHeaders::new(),
payload: None,
signature: vec![0xff; 64],
};
let bytes = cose.encode_to_vec();
assert_eq!(bytes[0], 0x84, "four-element array");
let back = CoseSign1::decode_bytes(&bytes).expect("decode");
assert_eq!(back, cose);
assert!(bytes.contains(&0xf6));
}
#[test]
fn cose_sign1_rejects_wrong_arity() {
let bytes = [0x83, 0x41, 0x01, 0xa0, 0x41, 0x02];
assert!(CoseSign1::decode_bytes(&bytes).is_err());
let mut with_trailer = CoseSign1 {
protected: vec![0xa0],
unprotected: CoseTokenHeaders::new(),
payload: None,
signature: vec![],
}
.encode_to_vec();
with_trailer.push(0x01);
assert!(CoseSign1::decode_bytes(&with_trailer).is_err());
}
#[test]
fn token_claims_encode_matches_cde_interleaving() {
let mut extra = CanonicalMap::new();
extra
.insert("zz".to_owned(), CborValue::UInt(1))
.expect("unique");
let claims = TokenClaims {
token_id: vec![1; 16],
issuer: DeviceId([2; 32]),
issuer_key: IdentityKey {
alg: -8,
public_key: vec![3; 32],
},
bearer: DeviceId([4; 32]),
capability: CapabilityVerb("exec:pty".to_owned()),
scope: CapabilityScope {
kind: "folder".to_owned(),
path: Some("/work".to_owned()),
},
expires: 42,
not_before: None,
valid_until: None,
conditions: None,
parent: None,
delegations_remaining: None,
authorised_by: None,
grants_capability: None,
requests_capability: None,
extra,
};
let bytes = claims.encode_to_vec();
let decoded = TokenClaims::decode_bytes(&bytes).expect("decode");
assert_eq!(decoded, claims);
assert_eq!(bytes[0], 0xa8);
assert_eq!(&bytes[1..4], &[0x62, b'z', b'z']);
}
#[test]
fn token_claims_round_trips_the_grant_permission_fields() {
let claims = TokenClaims {
token_id: vec![1; 16],
issuer: DeviceId([2; 32]),
issuer_key: IdentityKey {
alg: -7,
public_key: vec![3; 32],
},
bearer: DeviceId([4; 32]),
capability: CapabilityVerb("manage:grant".to_owned()),
scope: CapabilityScope {
kind: "folder".to_owned(),
path: Some("/work".to_owned()),
},
expires: 42,
not_before: None,
valid_until: None,
conditions: Some(vec![0x81, 0xf6]),
parent: None,
delegations_remaining: Some(3),
authorised_by: Some(vec![5; 48]),
grants_capability: Some(CapabilityVerb("exec:pty".to_owned())),
extra: CanonicalMap::new(),
requests_capability: None,
};
let bytes = claims.encode_to_vec();
let decoded = TokenClaims::decode_bytes(&bytes).expect("decode");
assert_eq!(decoded, claims);
}
}