Skip to content

Commit

Permalink
Fixed panic when parsing reserved ids (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronjeline authored Feb 28, 2024
1 parent fb09925 commit 20fce06
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
7 changes: 3 additions & 4 deletions cedar-policy-validator/src/human_schema/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub enum UserError {
EmptyList,
#[error("Invalid escape codes")]
StringEscape(NonEmpty<UnescapeError>),
#[error("`{0}` is a reserved identifier")]
ReservedIdentifierUsed(SmolStr),
}

pub(crate) type RawLocation = usize;
Expand Down Expand Up @@ -99,10 +101,7 @@ impl Display for ParseError {
} => write!(f, "extra token `{token}`"),
OwnedRawParseError::User {
error: Node { node, .. },
} => match node {
UserError::EmptyList => write!(f, "expected a non-empty list"),
UserError::StringEscape(unescape_errs) => write!(f, "{}", unescape_errs.first()),
},
} => write!(f, "{node}"),
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion cedar-policy-validator/src/human_schema/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@ Ident: Node<Id> = {
<l:@L> STRING <r:@R>
=> Node::with_source_loc("String".parse().unwrap(), Loc::new(l..r, Arc::clone(src))),
<l:@L> <i:IDENTIFIER> <r:@R>
=> Node::with_source_loc(i.parse().unwrap(), Loc::new(l..r, Arc::clone(src))),
=>? Id::from_str(i)
.map(|id : Id| Node::with_source_loc(id, Loc::new(l..r, Arc::clone(src))))
.map_err(|err : cedar_policy_core::parser::err::ParseErrors|
ParseError::User {
error: Node::with_source_loc(UserError::ReservedIdentifierUsed(i.to_smolstr()), Loc::new(l..r, Arc::clone(src)))
}
)
}

STR: Node<SmolStr> = {
Expand Down
50 changes: 50 additions & 0 deletions cedar-policy-validator/src/human_schema/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,4 +1422,54 @@ mod translator_tests {
let foo = ns.entity_types.get("Foo").unwrap();
assert_eq!(foo.member_of_types, vec!["String".to_smolstr()]);
}

#[test]
fn entity_named_if() {
let src = r#"
entity if = {};
entity Foo in [if] = {};
"#;

assert!(SchemaFragment::from_str_natural(src).is_err());
}

#[test]
fn entity_named_like() {
let src = r#"
entity like = {};
entity Foo in [like] = {};
"#;

assert!(SchemaFragment::from_str_natural(src).is_err());
}

#[test]
fn entity_named_true() {
let src = r#"
entity true = {};
entity Foo in [true] = {};
"#;

assert!(SchemaFragment::from_str_natural(src).is_err());
}

#[test]
fn entity_named_false() {
let src = r#"
entity false = {};
entity Foo in [false] = {};
"#;

assert!(SchemaFragment::from_str_natural(src).is_err());
}

#[test]
fn entity_named_has() {
let src = r#"
entity has = {};
entity Foo in [has] = {};
"#;

assert!(SchemaFragment::from_str_natural(src).is_err());
}
}

0 comments on commit 20fce06

Please sign in to comment.