Skip to content

Commit

Permalink
Enforce prefix in AssertionLogic Item src (#2601)
Browse files Browse the repository at this point in the history
  • Loading branch information
silva-fj committed Mar 21, 2024
1 parent 8147791 commit 7539aff
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions tee-worker/litentry/core/credentials/src/assertion_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ impl AssertionLogic {
}

pub fn new_item<T: ToString>(src: T, op: Op, dst: T) -> Self {
Self::Item { src: src.to_string(), op, dst: dst.to_string() }
let mut src_string = src.to_string();
let prefix = '$';
if !src_string.starts_with(prefix) {
log::warn!("AssertionLogic::new_item - src missing $ prefix: {}", src_string);
src_string.insert(0, prefix);
}
Self::Item { src: src_string, op, dst: dst.to_string() }
}
pub fn add_item(mut self, item: AssertionLogic) -> Self {
match &mut self {
Expand All @@ -90,27 +96,6 @@ impl AssertionLogic {
}
}

pub trait Logic {
fn eval(&self) -> bool;
}

impl Logic for AssertionLogic {
fn eval(&self) -> bool {
match self {
Self::Item { src, op, dst } => match op {
Op::GreaterThan => src > dst,
Op::LessThan => src < dst,
Op::GreaterEq => src >= dst,
Op::LessEq => src <= dst,
Op::Equal => src == dst,
Op::NotEq => src != dst,
},
Self::And { items } => items.iter().all(|item| item.eval()),
Self::Or { items } => items.iter().any(|item| item.eval()),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -145,11 +130,20 @@ mod tests {
}

#[test]
fn assertion_a1_eval_works() {
let web2_item = AssertionLogic::new_item("7", Op::GreaterEq, "7");
let web3_item = AssertionLogic::new_item("7", Op::GreaterThan, "3");
fn assertion_new_item_adds_dollar_prefix_if_missing() {
let item = AssertionLogic::new_item("some_src", Op::GreaterEq, "7");
assert_eq!(
item,
AssertionLogic::Item { src: "$some_src".into(), op: Op::GreaterEq, dst: "7".into() }
);
}

let a1 = AssertionLogic::new_or().add_item(web2_item).add_item(web3_item);
assert_eq!(a1.eval(), true);
#[test]
fn assertion_new_item_preserves_dollar_prefix_if_present() {
let item = AssertionLogic::new_item("$some_src", Op::GreaterEq, "7");
assert_eq!(
item,
AssertionLogic::Item { src: "$some_src".into(), op: Op::GreaterEq, dst: "7".into() }
);
}
}

0 comments on commit 7539aff

Please sign in to comment.