cedar-policy-core 4.10.0

Core implementation of the Cedar policy language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*
 * Copyright Cedar Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! Validator for Cedar policies
#![deny(
    missing_docs,
    rustdoc::broken_intra_doc_links,
    rustdoc::private_intra_doc_links,
    rustdoc::invalid_codeblock_attributes,
    rustdoc::invalid_html_tags,
    rustdoc::invalid_rust_codeblocks,
    rustdoc::bare_urls,
    clippy::doc_markdown
)]
#![cfg_attr(
    feature = "wasm",
    allow(
        non_snake_case,
        reason = "Wasm/TypeScript doesn't use snake case identifiers by convention"
    )
)]

use crate::ast::{Policy, PolicySet, Template};
use std::collections::HashSet;
mod level_validate;

mod coreschema;
#[cfg(feature = "entity-manifest")]
pub mod entity_manifest;
pub use coreschema::*;
mod diagnostics;
pub use diagnostics::*;
mod expr_iterator;
mod extension_schema;
mod extensions;
mod rbac;
mod schema;
pub use schema::err::*;
pub use schema::*;
mod deprecated_schema_compat;
pub mod json_schema;
mod str_checks;
pub use str_checks::confusable_string_checks;
pub mod cedar_schema;
pub mod typecheck;
use typecheck::Typechecker;
mod partition_nonempty;
pub mod types;

/// Used to select how a policy will be validated.
#[derive(Default, Eq, PartialEq, Copy, Clone, Debug)]
pub enum ValidationMode {
    /// Strict mode
    #[default]
    Strict,
    /// Permissive mode
    Permissive,
    /// Partial validation, allowing you to use an incomplete schema, but
    /// providing no formal guarantees
    #[cfg(feature = "partial-validate")]
    Partial,
}

impl ValidationMode {
    /// Does this mode use partial validation. We could conceivably have a
    /// strict/partial validation mode.
    fn is_partial(self) -> bool {
        match self {
            ValidationMode::Strict | ValidationMode::Permissive => false,
            #[cfg(feature = "partial-validate")]
            ValidationMode::Partial => true,
        }
    }

    /// Does this mode apply strict validation rules.
    fn is_strict(self) -> bool {
        match self {
            ValidationMode::Strict => true,
            ValidationMode::Permissive => false,
            #[cfg(feature = "partial-validate")]
            ValidationMode::Partial => false,
        }
    }
}

/// Structure containing the context needed for policy validation. This is
/// currently only the `EntityType`s and `ActionType`s from a single schema.
#[derive(Debug, Clone)]
pub struct Validator {
    schema: ValidatorSchema,
}

impl Validator {
    /// Construct a new Validator from a schema file.
    pub fn new(schema: ValidatorSchema) -> Validator {
        Self { schema }
    }

    /// Get the `ValidatorSchema` this `Validator` is using.
    pub fn schema(&self) -> &ValidatorSchema {
        &self.schema
    }

    /// Validate all templates, links, and static policies in a policy set.
    /// Return a `ValidationResult`.
    pub fn validate(&self, policies: &PolicySet, mode: ValidationMode) -> ValidationResult {
        let validate_policy_results: (Vec<_>, Vec<_>) = policies
            .all_templates()
            .map(|p| self.validate_policy(p, mode))
            .unzip();
        let template_and_static_policy_errs = validate_policy_results.0.into_iter().flatten();
        let template_and_static_policy_warnings = validate_policy_results.1.into_iter().flatten();
        let link_errs = policies
            .policies()
            .filter_map(|p| self.validate_slots(p, mode))
            .flatten();
        ValidationResult::new(
            template_and_static_policy_errs.chain(link_errs),
            template_and_static_policy_warnings
                .chain(confusable_string_checks(policies.all_templates())),
        )
    }

    /// Validate all templates, links, and static policies in a policy set.
    /// If validation passes, also run level validation with `max_deref_level`
    /// (see RFC 76).
    /// Return a `ValidationResult`.
    pub fn validate_with_level(
        &self,
        policies: &PolicySet,
        mode: ValidationMode,
        max_deref_level: u32,
    ) -> ValidationResult {
        let validate_policy_results: (Vec<_>, Vec<_>) = policies
            .all_templates()
            .map(|p| self.validate_policy_with_level(p, mode, max_deref_level))
            .unzip();
        let template_and_static_policy_errs = validate_policy_results.0.into_iter().flatten();
        let template_and_static_policy_warnings = validate_policy_results.1.into_iter().flatten();
        let link_errs = policies
            .policies()
            .filter_map(|p| self.validate_slots(p, mode))
            .flatten();
        ValidationResult::new(
            template_and_static_policy_errs.chain(link_errs),
            template_and_static_policy_warnings
                .chain(confusable_string_checks(policies.all_templates())),
        )
    }

    /// Run all validations against a single static policy or template (note
    /// that Core `Template` includes static policies as well), gathering all
    /// validation errors and warnings in the returned iterators.
    fn validate_policy<'a>(
        &'a self,
        p: &'a Template,
        mode: ValidationMode,
    ) -> (
        impl Iterator<Item = ValidationError> + 'a,
        impl Iterator<Item = ValidationWarning> + 'a,
    ) {
        let validation_errors = if mode.is_partial() {
            // We skip `validate_entity_types`, `validate_action_ids`, and
            // `validate_action_application` passes for partial schema
            // validation because there may be arbitrary extra entity types and
            // actions, so we can never claim that one doesn't exist.
            None
        } else {
            Some(
                Validator::validate_entity_types(&self.schema, p)
                    .chain(Validator::validate_enum_entity(&self.schema, p))
                    .chain(Validator::validate_action_ids(&self.schema, p))
                    // We could usefully update this pass to apply to partial
                    // schema if it only failed when there is a known action
                    // applied to known principal/resource entity types that are
                    // not in its `appliesTo`.
                    .chain(self.validate_template_action_application(p)),
            )
        }
        .into_iter()
        .flatten();
        let (errors, warnings) = self.typecheck_policy(p, mode);
        (validation_errors.chain(errors), warnings)
    }

    /// Check that all entity types are defined in the schema, and each entity
    /// literal that is an action or enum type is defined in the schema. These
    /// checks are notably not performed by [`Typechecker::typecheck_by_single_request_env`]
    /// so callers of that function will typically need to call this as well.
    pub fn validate_entity_types_and_literals<'a>(
        schema: &'a ValidatorSchema,
        p: &'a Template,
    ) -> impl Iterator<Item = ValidationError> + 'a {
        Validator::validate_entity_types(schema, p)
            .chain(Validator::validate_enum_entity(schema, p))
            .chain(Validator::validate_action_ids(schema, p))
    }

    /// Run relevant validations against a single template-linked policy,
    /// gathering all validation errors together in the returned iterator.
    fn validate_slots<'a>(
        &'a self,
        p: &'a Policy,
        mode: ValidationMode,
    ) -> Option<impl Iterator<Item = ValidationError> + 'a> {
        // Ignore static policies since they are already handled by `validate_policy`
        if p.is_static() {
            return None;
        }
        // In partial validation, there may be arbitrary extra entity types and
        // actions, so we can never claim that one doesn't exist or that the
        // action application is invalid.
        if mode.is_partial() {
            return None;
        }
        // For template-linked policies `Policy::principal_constraint()` and
        // `Policy::resource_constraint()` return a copy of the constraint with
        // the slot filled by the appropriate value.
        Some(
            self.validate_entity_types_in_slots(p.id(), p.env())
                .chain(self.validate_linked_action_application(p)),
        )
    }

    /// Construct a Typechecker instance and use it to detect any type errors in
    /// the argument static policy or template (note that Core `Template`
    /// includes static policies as well) in the context of the schema for this
    /// validator. Any detected type errors are wrapped and returned as
    /// `ValidationErrorKind`s.
    fn typecheck_policy<'a>(
        &'a self,
        t: &'a Template,
        mode: ValidationMode,
    ) -> (
        impl Iterator<Item = ValidationError> + 'a,
        impl Iterator<Item = ValidationWarning> + 'a,
    ) {
        let typecheck = Typechecker::new(&self.schema, mode);
        let mut errors = HashSet::new();
        let mut warnings = HashSet::new();
        typecheck.typecheck_policy(t, &mut errors, &mut warnings);
        (errors.into_iter(), warnings.into_iter())
    }
}

#[cfg(test)]
mod test {
    use itertools::Itertools;
    use std::{collections::HashMap, sync::Arc};

    use crate::validator::types::Type;
    use crate::validator::validation_errors::UnrecognizedActionIdHelp;
    use crate::validator::Result;

    use super::*;
    use crate::{
        ast::{self, PolicyID},
        est::Annotations,
        parser::{self, Loc},
    };

    use similar_asserts::assert_eq;

    #[test]
    fn top_level_validate() -> Result<()> {
        let mut set = PolicySet::new();
        let foo_type = "foo_type";
        let bar_type = "bar_type";
        let action_name = "action";
        let schema_file = json_schema::NamespaceDefinition::new(
            [
                (
                    foo_type.parse().unwrap(),
                    json_schema::StandardEntityType {
                        member_of_types: vec![],
                        shape: json_schema::AttributesOrContext::default(),
                        tags: None,
                    }
                    .into(),
                ),
                (
                    bar_type.parse().unwrap(),
                    json_schema::StandardEntityType {
                        member_of_types: vec![],
                        shape: json_schema::AttributesOrContext::default(),
                        tags: None,
                    }
                    .into(),
                ),
            ],
            [(
                action_name.into(),
                json_schema::ActionType {
                    applies_to: Some(json_schema::ApplySpec {
                        principal_types: vec!["foo_type".parse().unwrap()],
                        resource_types: vec!["bar_type".parse().unwrap()],
                        context: json_schema::AttributesOrContext::default(),
                    }),
                    member_of: None,
                    attributes: None,
                    annotations: Annotations::new(),
                    loc: None,
                    #[cfg(feature = "extended-schema")]
                    defn_loc: None,
                },
            )],
        );
        let schema = schema_file.try_into().unwrap();
        let validator = Validator::new(schema);

        let policy_a_src = r#"permit(principal in foo_type::"a", action == Action::"actin", resource == bar_type::"b");"#;
        let policy_a = parser::parse_policy(Some(PolicyID::from_string("pola")), policy_a_src)
            .expect("Test Policy Should Parse");
        set.add_static(policy_a)
            .expect("Policy already present in PolicySet");

        let policy_b_src = r#"permit(principal in foo_tye::"a", action == Action::"action", resource == br_type::"b");"#;
        let policy_b = parser::parse_policy(Some(PolicyID::from_string("polb")), policy_b_src)
            .expect("Test Policy Should Parse");
        set.add_static(policy_b)
            .expect("Policy already present in PolicySet");

        let result = validator.validate(&set, ValidationMode::default());
        let principal_err = ValidationError::unrecognized_entity_type(
            Some(Loc::new(20..27, Arc::from(policy_b_src))),
            PolicyID::from_string("polb"),
            "foo_tye".to_string(),
            Some("foo_type".to_string()),
        );
        let resource_err = ValidationError::unrecognized_entity_type(
            Some(Loc::new(74..81, Arc::from(policy_b_src))),
            PolicyID::from_string("polb"),
            "br_type".to_string(),
            Some("bar_type".to_string()),
        );
        let action_err = ValidationError::unrecognized_action_id(
            Some(Loc::new(45..60, Arc::from(policy_a_src))),
            PolicyID::from_string("pola"),
            "Action::\"actin\"".to_string(),
            Some(UnrecognizedActionIdHelp::SuggestAlternative(
                "Action::\"action\"".to_string(),
            )),
        );

        assert!(!result.validation_passed());
        assert!(
            result.validation_errors().contains(&principal_err),
            "{result:?}"
        );
        assert!(
            result.validation_errors().contains(&resource_err),
            "{result:?}"
        );
        assert!(
            result.validation_errors().contains(&action_err),
            "{result:?}"
        );
        Ok(())
    }

    #[test]
    fn top_level_validate_with_links() -> Result<()> {
        let mut set = PolicySet::new();
        let schema: ValidatorSchema = json_schema::Fragment::from_json_str(
            r#"
            {
                "some_namespace": {
                    "entityTypes": {
                        "User": {
                            "shape": {
                                "type": "Record",
                                "attributes": {
                                    "department": {
                                        "type": "String"
                                    },
                                    "jobLevel": {
                                        "type": "Long"
                                    }
                                }
                            },
                            "memberOfTypes": [
                                "UserGroup"
                            ]
                        },
                        "UserGroup": {},
                        "Photo" : {}
                    },
                    "actions": {
                        "view": {
                            "appliesTo": {
                                "resourceTypes": [
                                    "Photo"
                                ],
                                "principalTypes": [
                                    "User"
                                ]
                            }
                        }
                    }
                }
            }
        "#,
        )
        .expect("Schema parse error.")
        .try_into()
        .expect("Expected valid schema.");
        let validator = Validator::new(schema);

        let t = parser::parse_policy_or_template(
            Some(PolicyID::from_string("template")),
            r#"permit(principal == some_namespace::User::"Alice", action, resource in ?resource);"#,
        )
        .expect("Parse Error");
        let loc = t.loc().cloned();
        set.add_template(t)
            .expect("Template already present in PolicySet");

        // the template is valid by itself
        let result = validator.validate(&set, ValidationMode::default());
        assert_eq!(
            result.validation_errors().collect::<Vec<_>>(),
            Vec::<&ValidationError>::new()
        );

        // a valid link is valid
        let mut values = HashMap::new();
        values.insert(
            ast::SlotId::resource(),
            ast::EntityUID::from_components(
                "some_namespace::Photo".parse().unwrap(),
                ast::Eid::new("foo"),
                None,
            ),
        );
        set.link(
            ast::PolicyID::from_string("template"),
            ast::PolicyID::from_string("link1"),
            values,
        )
        .expect("Linking failed!");
        let result = validator.validate(&set, ValidationMode::default());
        assert!(result.validation_passed());

        // an invalid link results in an error
        let mut values = HashMap::new();
        values.insert(
            ast::SlotId::resource(),
            ast::EntityUID::from_components(
                "some_namespace::Undefined".parse().unwrap(),
                ast::Eid::new("foo"),
                None,
            ),
        );
        set.link(
            ast::PolicyID::from_string("template"),
            ast::PolicyID::from_string("link2"),
            values,
        )
        .expect("Linking failed!");
        let result = validator.validate(&set, ValidationMode::default());
        assert!(!result.validation_passed());
        assert_eq!(result.validation_errors().count(), 2);
        let undefined_err = ValidationError::unrecognized_entity_type(
            None,
            PolicyID::from_string("link2"),
            "some_namespace::Undefined".to_string(),
            Some("some_namespace::User".to_string()),
        );
        let invalid_action_err = ValidationError::invalid_action_application(
            loc.clone(),
            PolicyID::from_string("link2"),
            false,
            false,
        );

        let actual_undef_error = result
            .validation_errors()
            .find(|e| matches!(e, ValidationError::UnrecognizedEntityType(_)))
            .unwrap();
        assert_eq!(actual_undef_error, &undefined_err);

        let actual_action_error = result
            .validation_errors()
            .find(|e| matches!(e, ValidationError::InvalidActionApplication(_)))
            .unwrap();
        assert_eq!(actual_action_error, &invalid_action_err);

        // this is also an invalid link (not a valid resource type for any action in the schema)
        let mut values = HashMap::new();
        values.insert(
            ast::SlotId::resource(),
            ast::EntityUID::from_components(
                "some_namespace::User".parse().unwrap(),
                ast::Eid::new("foo"),
                None,
            ),
        );
        set.link(
            ast::PolicyID::from_string("template"),
            ast::PolicyID::from_string("link3"),
            values,
        )
        .expect("Linking failed!");
        let result = validator.validate(&set, ValidationMode::default());
        assert!(!result.validation_passed());
        // `result` contains the two prior error messages plus one new one
        assert_eq!(result.validation_errors().count(), 3);
        let invalid_action_err = ValidationError::invalid_action_application(
            loc,
            PolicyID::from_string("link3"),
            false,
            false,
        );
        assert!(result.validation_errors().contains(&invalid_action_err));

        Ok(())
    }

    #[test]
    fn validate_finds_warning_and_error() {
        let schema: ValidatorSchema = json_schema::Fragment::from_json_str(
            r#"
            {
                "": {
                    "entityTypes": {
                        "User": { }
                    },
                    "actions": {
                        "view": {
                            "appliesTo": {
                                "resourceTypes": [ "User" ],
                                "principalTypes": [ "User" ]
                            }
                        }
                    }
                }
            }
        "#,
        )
        .expect("Schema parse error.")
        .try_into()
        .expect("Expected valid schema.");
        let validator = Validator::new(schema);

        let mut set = PolicySet::new();
        let src = r#"permit(principal == User::"һenry", action, resource) when {1 > true};"#;
        let p = parser::parse_policy(None, src).unwrap();
        set.add_static(p).unwrap();

        let result = validator.validate(&set, ValidationMode::default());
        assert_eq!(
            result.validation_errors().collect::<Vec<_>>(),
            vec![&ValidationError::expected_type(
                typecheck::test::test_utils::get_loc(src, "true"),
                PolicyID::from_string("policy0"),
                Type::primitive_long(),
                Type::singleton_boolean(true),
                None,
            )]
        );
        assert_eq!(
            result.validation_warnings().collect::<Vec<_>>(),
            vec![&ValidationWarning::mixed_script_identifier(
                None,
                PolicyID::from_string("policy0"),
                "һenry"
            )]
        );
    }
}

#[cfg(test)]
#[expect(clippy::cognitive_complexity, reason = "unit test code")]
mod enumerated_entity_types {
    use std::collections::HashMap;

    use crate::{
        ast::{Eid, EntityUID, ExprBuilder, PolicyID, PolicySet, SlotId, Template},
        expr_builder::ExprBuilder as _,
        extensions::Extensions,
        parser::parse_policy_or_template,
    };
    use cool_asserts::assert_matches;
    use itertools::Itertools;

    use crate::validator::{
        typecheck::test::test_utils::get_loc,
        types::{EntityLUB, Type},
        validation_errors::AttributeAccess,
        ValidationError, ValidationWarning, Validator, ValidatorSchema,
    };

    #[track_caller]
    fn schema() -> ValidatorSchema {
        ValidatorSchema::from_json_value(
            serde_json::json!(
                {
                    "":  {  "entityTypes": {
                             "Foo": {
                                "enum": [ "foo" ],
                            },
                            "Bar": {
                                "memberOfTypes": ["Foo"],
                            },
                            "Other": { },
                        },
                        "actions": {
                            "a": {
                                "appliesTo": {
                                    "principalTypes": ["Foo"],
                                    "resourceTypes": ["Bar"],
                                }
                            }
                        }
                }
            }
            ),
            Extensions::none(),
        )
        .unwrap()
    }

    #[test]
    fn basic() {
        let schema = schema();
        let template = parse_policy_or_template(None, r#"permit(principal, action == Action::"a", resource) when { principal == Foo::"foo" };"#).unwrap();
        let validator = Validator::new(schema);
        let (errors, warnings) =
            validator.validate_policy(&template, crate::validator::ValidationMode::Strict);
        assert!(warnings.collect_vec().is_empty());
        assert!(errors.collect_vec().is_empty());
    }

    #[test]
    fn link() {
        let schema = schema();
        let template = parse_policy_or_template(
            None,
            r#"permit(principal in ?principal, action == Action::"a", resource);"#,
        )
        .unwrap();
        let policy = Template::link(
            std::sync::Arc::new(template),
            PolicyID::from_string("test"),
            HashMap::from_iter([(SlotId::principal(), r#"Other::"foo""#.parse().unwrap())]),
        )
        .unwrap();
        let mut policy_set = PolicySet::new();
        let _ = policy_set.add(policy);
        let validator = Validator::new(schema);
        let result = validator.validate(&policy_set, crate::validator::ValidationMode::Strict);

        assert_eq!(result.validation_errors().collect_vec().len(), 1);
    }

    #[test]
    fn basic_invalid() {
        let schema = schema();
        let template = parse_policy_or_template(None, r#"permit(principal, action == Action::"a", resource) when { principal == Foo::"fo" };"#).unwrap();
        let validator = Validator::new(schema.clone());
        let (errors, warnings) =
            validator.validate_policy(&template, crate::validator::ValidationMode::Strict);
        assert!(warnings.collect_vec().is_empty());
        assert_matches!(&errors.collect_vec(), [ValidationError::InvalidEnumEntity(err)] => {
            assert_eq!(err.err.choices, vec![Eid::new("foo")]);
            assert_eq!(err.err.uid, EntityUID::with_eid_and_type("Foo", "fo").unwrap());
        });

        let template = parse_policy_or_template(
            None,
            r#"permit(principal == Foo::"🏈", action == Action::"a", resource);"#,
        )
        .unwrap();
        let validator = Validator::new(schema.clone());
        let (errors, warnings) =
            validator.validate_policy(&template, crate::validator::ValidationMode::Strict);
        assert!(warnings.collect_vec().is_empty());
        assert_matches!(&errors.collect_vec(), [ValidationError::InvalidEnumEntity(err)] => {
            assert_eq!(err.err.choices, vec![Eid::new("foo")]);
            assert_eq!(err.err.uid, EntityUID::with_eid_and_type("Foo", "🏈").unwrap());
        });

        let template = parse_policy_or_template(
            None,
            r#"permit(principal in Foo::"🏈", action == Action::"a", resource);"#,
        )
        .unwrap();
        let validator = Validator::new(schema.clone());
        let (errors, warnings) =
            validator.validate_policy(&template, crate::validator::ValidationMode::Strict);
        assert!(warnings.collect_vec().is_empty());
        assert_matches!(&errors.collect_vec(), [ValidationError::InvalidEnumEntity(err)] => {
            assert_eq!(err.err.choices, vec![Eid::new("foo")]);
            assert_eq!(err.err.uid, EntityUID::with_eid_and_type("Foo", "🏈").unwrap());
        });

        let template = parse_policy_or_template(
            None,
            r#"permit(principal, action == Action::"a", resource)
            when { {"🏈": Foo::"🏈"} has "🏈" };
        "#,
        )
        .unwrap();
        let validator = Validator::new(schema.clone());
        let (errors, warnings) =
            validator.validate_policy(&template, crate::validator::ValidationMode::Strict);
        assert!(warnings.collect_vec().is_empty());
        assert_matches!(&errors.collect_vec(), [ValidationError::InvalidEnumEntity(err)] => {
            assert_eq!(err.err.choices, vec![Eid::new("foo")]);
            assert_eq!(err.err.uid, EntityUID::with_eid_and_type("Foo", "🏈").unwrap());
        });

        let template = parse_policy_or_template(
            None,
            r#"permit(principal, action == Action::"a", resource)
            when { [Foo::"🏈"].isEmpty() };
        "#,
        )
        .unwrap();
        let validator = Validator::new(schema.clone());
        let (errors, warnings) =
            validator.validate_policy(&template, crate::validator::ValidationMode::Strict);
        assert!(warnings.collect_vec().is_empty());
        assert_matches!(&errors.collect_vec(), [ValidationError::InvalidEnumEntity(err)] => {
            assert_eq!(err.err.choices, vec![Eid::new("foo")]);
            assert_eq!(err.err.uid, EntityUID::with_eid_and_type("Foo", "🏈").unwrap());
        });

        let template = parse_policy_or_template(
            None,
            r#"permit(principal, action == Action::"a", resource)
            when { [{"🏈": Foo::"🏈"}].isEmpty() };
        "#,
        )
        .unwrap();
        let validator = Validator::new(schema);
        let (errors, warnings) =
            validator.validate_policy(&template, crate::validator::ValidationMode::Strict);
        assert!(warnings.collect_vec().is_empty());
        assert_matches!(&errors.collect_vec(), [ValidationError::InvalidEnumEntity(err)] => {
            assert_eq!(err.err.choices, vec![Eid::new("foo")]);
            assert_eq!(err.err.uid, EntityUID::with_eid_and_type("Foo", "🏈").unwrap());
        });
    }

    #[test]
    fn no_attrs_allowed() {
        let schema = schema();
        let src = r#"permit(principal, action == Action::"a", resource) when { principal.foo == "foo" };"#;
        let template = parse_policy_or_template(None, src).unwrap();
        let validator = Validator::new(schema);
        let (errors, warnings) =
            validator.validate_policy(&template, crate::validator::ValidationMode::Strict);
        assert!(warnings.collect_vec().is_empty());
        assert_eq!(
            errors.collect_vec(),
            [ValidationError::unsafe_attribute_access(
                get_loc(src, "principal.foo"),
                PolicyID::from_string("policy0"),
                AttributeAccess::EntityLUB(
                    EntityLUB::single_entity("Foo".parse().unwrap()),
                    vec!["foo".into()],
                ),
                None,
                false,
            )]
        );
    }

    #[test]
    fn no_ancestors() {
        let schema = schema();
        let src = r#"permit(principal, action == Action::"a", resource) when { principal in Bar::"bar" };"#;
        let template = parse_policy_or_template(None, src).unwrap();
        let validator = Validator::new(schema);
        let (errors, warnings) =
            validator.validate_policy(&template, crate::validator::ValidationMode::Strict);
        assert_eq!(
            warnings.collect_vec(),
            [ValidationWarning::impossible_policy(
                get_loc(src, src),
                PolicyID::from_string("policy0")
            )]
        );
        assert!(errors.collect_vec().is_empty());
    }

    #[test]
    fn no_tags_allowed() {
        let schema = schema();
        let src = r#"permit(principal, action == Action::"a", resource) when { principal.getTag("foo") == "foo" };"#;
        let template = parse_policy_or_template(None, src).unwrap();
        let validator = Validator::new(schema);
        let (errors, warnings) =
            validator.validate_policy(&template, crate::validator::ValidationMode::Strict);
        assert!(warnings.collect_vec().is_empty());
        assert_eq!(
            errors.collect_vec(),
            [ValidationError::unsafe_tag_access(
                get_loc(src, r#"principal.getTag("foo")"#),
                PolicyID::from_string("policy0"),
                Some(EntityLUB::single_entity("Foo".parse().unwrap()),),
                {
                    let builder = ExprBuilder::new();
                    let mut expr = builder.val("foo");
                    expr.set_data(Some(Type::primitive_string()));
                    expr
                },
            )]
        );
    }
}