Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Level checking implementation and proof (WIP) #533

Draft
wants to merge 60 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
61a1442
Initial level impl
john-h-kastner-aws Jan 8, 2025
ded0e6f
proof for `if` at level `0`
john-h-kastner-aws Jan 13, 2025
c7ad783
iwp
john-h-kastner-aws Jan 14, 2025
937fc95
tweak ite
john-h-kastner-aws Jan 22, 2025
4206195
reorg
john-h-kastner-aws Jan 22, 2025
cd611a0
wip
john-h-kastner-aws Jan 22, 2025
a94d440
wip
john-h-kastner-aws Feb 4, 2025
ba9a17c
wip
john-h-kastner-aws Feb 5, 2025
e66599b
reorg files
john-h-kastner-aws Feb 5, 2025
d5261cc
tidy
john-h-kastner-aws Feb 5, 2025
ea104a4
hasattr
john-h-kastner-aws Feb 5, 2025
0b64945
work on reachable_attr_step
john-h-kastner-aws Feb 5, 2025
f18e418
comments
john-h-kastner-aws Feb 5, 2025
ae34a26
introduce lemma to clean up proof
john-h-kastner-aws Feb 6, 2025
383e6ba
more cases
john-h-kastner-aws Feb 7, 2025
a56607b
tidy
john-h-kastner-aws Feb 7, 2025
2c3c037
tidy
john-h-kastner-aws Feb 7, 2025
f2158f8
reorg
john-h-kastner-aws Feb 7, 2025
7c52720
use match instead of if
john-h-kastner-aws Feb 10, 2025
8970e0d
tidy: use `symm` a less
john-h-kastner-aws Feb 10, 2025
e4a5402
dont need that lemma anymore
john-h-kastner-aws Feb 10, 2025
6b780af
tidy
john-h-kastner-aws Feb 10, 2025
f5b05c5
work on not_entities_then_not_slice
john-h-kastner-aws Feb 10, 2025
fac2ad4
slightly simplify `lit` case
john-h-kastner-aws Feb 11, 2025
6670bd1
simplify var cae a little
john-h-kastner-aws Feb 11, 2025
2232933
move lit inversion into litvar.lean
john-h-kastner-aws Feb 11, 2025
44ff0df
unary app sound
john-h-kastner-aws Feb 11, 2025
3633668
split checkLevel and checkRoot
john-h-kastner-aws Feb 11, 2025
4b2413a
dead code
john-h-kastner-aws Feb 11, 2025
f195a31
rename checkRoot to notEntityLit and adjust lit to accept non-entity
john-h-kastner-aws Feb 12, 2025
27600a4
add most of an inductive definition for level checking
john-h-kastner-aws Feb 14, 2025
f1eb7ba
prototype better record handling
john-h-kastner-aws Feb 14, 2025
202a805
rename to `List.unionAll` to match emina's code
john-h-kastner-aws Feb 14, 2025
4904872
nicer termination
john-h-kastner-aws Feb 14, 2025
130813a
fix up proofs
john-h-kastner-aws Feb 14, 2025
1686f02
fill hole in termination proof
john-h-kastner-aws Feb 17, 2025
596bfe1
fill in record case for not_entity_lit_spec
john-h-kastner-aws Feb 17, 2025
758be7d
finish check_level_checked_succ using inductive def
john-h-kastner-aws Feb 17, 2025
49ef4b2
tweak
john-h-kastner-aws Feb 17, 2025
ce786bd
Show termination for record in checked_eval_entity_reachable
john-h-kastner-aws Feb 17, 2025
ba92360
fill in record details
john-h-kastner-aws Feb 18, 2025
54540ae
nicer match syntax
john-h-kastner-aws Feb 18, 2025
e5fb2cd
stronger record inversion lemma
john-h-kastner-aws Feb 19, 2025
83e7891
another record inversion lemma
john-h-kastner-aws Feb 19, 2025
a9d158f
fill in more record details
john-h-kastner-aws Feb 19, 2025
4f22ed2
finish proof for equiv of inductive defs
john-h-kastner-aws Feb 19, 2025
234a2d4
notes
john-h-kastner-aws Feb 19, 2025
5fb9211
incorperate emina proof
john-h-kastner-aws Feb 20, 2025
cfcee35
split checked_eval_entity_reachable
john-h-kastner-aws Feb 20, 2025
d116e43
move lemma
john-h-kastner-aws Feb 20, 2025
f388aac
dont need that anymore
john-h-kastner-aws Feb 20, 2025
ca78ccb
fill in lemmas
john-h-kastner-aws Feb 20, 2025
8a872f6
rename_inductive
john-h-kastner-aws Feb 20, 2025
af8e3ab
simp onlys
john-h-kastner-aws Feb 20, 2025
8044bef
Merge remote-tracking branch 'origin/main' into levels-checking
john-h-kastner-aws Feb 21, 2025
7b732d8
tidy
john-h-kastner-aws Feb 21, 2025
1ce31eb
qualify names
john-h-kastner-aws Feb 21, 2025
8ceece6
use mapUnion
john-h-kastner-aws Feb 21, 2025
afdc460
proof for `.and` and `.or`
john-h-kastner-aws Feb 21, 2025
873a178
simplify proof for `or`
john-h-kastner-aws Feb 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cedar-lean/Cedar/Spec.lean
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Cedar.Spec.Ext
import Cedar.Spec.Policy
import Cedar.Spec.Request
import Cedar.Spec.Response
import Cedar.Spec.Slice
import Cedar.Spec.Template
import Cedar.Spec.Value
import Cedar.Spec.Wildcard
53 changes: 53 additions & 0 deletions cedar-lean/Cedar/Spec/Slice.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/-
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.
-/

import Cedar.Spec.Value
import Cedar.Spec.Entities
import Cedar.Spec.Request
import Cedar.Data.SizeOf

/-!
This file defines entity slicing at a level
-/

namespace Cedar.Spec

open Cedar.Data

def Value.sliceEUIDs : Value → Set EntityUID
| .prim (.entityUID uid) => Set.singleton uid
| .record (Map.mk avs) => avs.attach₃.mapUnion λ e => e.val.snd.sliceEUIDs
| .prim _ | set _ | .ext _ => ∅

def EntityData.sliceEUIDs (ed : EntityData) : Set EntityUID :=
ed.attrs.values.mapUnion Value.sliceEUIDs ∪
ed.tags.values.mapUnion Value.sliceEUIDs

def Request.sliceEUIDs (r : Request) : Set EntityUID :=
Set.make [r.principal, r.action, r.resource] ∪
(Value.record r.context).sliceEUIDs

def Entities.sliceAtLevel (es : Entities) (r : Request) (level : Nat) : Option Entities := do
let slice ← sliceAtLevel r.sliceEUIDs level
let slice ← slice.elts.mapM λ e => do some (e, ←(es.find? e))
some (Map.make slice)
where
sliceAtLevel (work : Set EntityUID) : Nat → Option (Set EntityUID)
| 0 => some ∅
| level + 1 => do
let eds ← work.elts.mapM es.find?
let slice ← List.mapUnion id <$> eds.mapM (λ ed => sliceAtLevel ed.sliceEUIDs level)
some (work ∪ slice)
1 change: 1 addition & 0 deletions cedar-lean/Cedar/Thm/Validation.lean
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import Cedar.Spec
import Cedar.Data
import Cedar.Validation
import Cedar.Thm.Validation.Levels
import Cedar.Thm.Validation.Validator
import Cedar.Thm.Validation.RequestEntityValidation

Expand Down
73 changes: 73 additions & 0 deletions cedar-lean/Cedar/Thm/Validation/Levels.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/-
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.
-/

import Cedar.Spec
import Cedar.Data
import Cedar.Validation
import Cedar.Thm.Validation.Typechecker
import Cedar.Thm.Validation.Typechecker.Types

import Cedar.Thm.Validation.Levels.CheckLevel
import Cedar.Thm.Validation.Levels.IfThenElse
import Cedar.Thm.Validation.Levels.GetAttr
import Cedar.Thm.Validation.Levels.HasAttr
import Cedar.Thm.Validation.Levels.UnaryApp
import Cedar.Thm.Validation.Levels.And
import Cedar.Thm.Validation.Levels.Or

namespace Cedar.Thm

open Cedar.Data
open Cedar.Spec
open Cedar.Validation

theorem level_based_slicing_is_sound {e : Expr} {tx : TypedExpr} {n : Nat} {c c₁ : Capabilities} {env : Environment} {request : Request} {slice entities : Entities}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might also want to have another higher-level theorem (which calls this one) that doesn't mention capabilities. Something along these lines:

(hs : slice = entities.sliceAtLevel request n)
(hc : CapabilitiesInvariant c request entities)
(hr : RequestAndEntitiesMatchEnvironment env request entities)
(ht : typeOf e c env = Except.ok (tx, c₁))
(hl : checkLevel tx n) :
evaluate e request entities = evaluate e request slice
:= by
cases e
case lit => simp [evaluate]
case var v => cases v <;> simp [evaluate]
case ite c t e =>
have ihc := @level_based_slicing_is_sound c
have iht := @level_based_slicing_is_sound t
have ihe := @level_based_slicing_is_sound e
exact level_based_slicing_is_sound_if hs hc hr ht hl ihc iht ihe
case and e₁ e₂ =>
have ih₁ := @level_based_slicing_is_sound e₁
have ih₂ := @level_based_slicing_is_sound e₂
exact level_based_slicing_is_sound_and hs hc hr ht hl ih₁ ih₂
case or e₁ e₂ =>
have ih₁ := @level_based_slicing_is_sound e₁
have ih₂ := @level_based_slicing_is_sound e₂
exact level_based_slicing_is_sound_or hs hc hr ht hl ih₁ ih₂
case unaryApp op e =>
have ihe := @level_based_slicing_is_sound e
exact level_based_slicing_is_sound_unary_app hs hc hr ht hl ihe
case binaryApp => sorry -- includes tags cases which should follow the attr cases and `in` case which might be tricky
case getAttr e _ =>
have ihe := @level_based_slicing_is_sound e
exact level_based_slicing_is_sound_get_attr hs hc hr ht hl ihe
case hasAttr e _ =>
have ihe := @level_based_slicing_is_sound e
exact level_based_slicing_is_sound_has_attr hs hc hr ht hl ihe
case set => sorry -- trivial recursive case maybe a little tricky
case record => sorry -- likely to be tricky. Record cases are always hard, and here there might be an odd interaction with attribute access
case call => sorry -- should be the same as set
80 changes: 80 additions & 0 deletions cedar-lean/Cedar/Thm/Validation/Levels/And.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

/-
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.
-/

import Cedar.Spec
import Cedar.Data
import Cedar.Validation
import Cedar.Thm.Validation.Typechecker
import Cedar.Thm.Validation.Typechecker.Basic
import Cedar.Thm.Validation.Typechecker.IfThenElse
import Cedar.Thm.Validation.Typechecker.Types
import Cedar.Thm.Validation.Levels.Basic

/-!
This file proves that level checking for `.and` expressions is sound.
-/

namespace Cedar.Thm

open Cedar.Data
open Cedar.Spec
open Cedar.Validation

theorem level_based_slicing_is_sound_and {e₁ e₂ : Expr} {n : Nat} {c₀ c₁: Capabilities} {env : Environment} {request : Request} {entities slice : Entities}
(hs : slice = entities.sliceAtLevel request n)
(hc : CapabilitiesInvariant c₀ request entities)
(hr : RequestAndEntitiesMatchEnvironment env request entities)
(ht : typeOf (.and e₁ e₂) c₀ env = Except.ok (tx, c₁))
(hl : checkLevel tx n = true)
(ih₁ : TypedAtLevelIsSound e₁)
(ih₂ : TypedAtLevelIsSound e₂)
: evaluate (.and e₁ e₂) request entities = evaluate (.and e₁ e₂) request slice
:= by
replace ⟨ tx₁, bty, c', htx₁, hty₁, ht ⟩ := type_of_and_inversion ht
have ⟨ hgc, v₁, he₁, hv₁⟩ := type_of_is_sound hc hr htx₁
rw [hty₁] at hv₁
split at ht
case isTrue =>
replace ⟨ ht, _ ⟩ := ht
subst tx bty
replace hv₁ := instance_of_ff_is_false hv₁
subst v₁
unfold EvaluatesTo at he₁
simp [evaluate]
simp [checkLevel] at hl
specialize ih₁ hs hc hr htx₁ hl
rw [←ih₁]
rcases he₁ with he₁ | he₁ | he₁ | he₁ <;>
simp [he₁, Result.as, Coe.coe, Value.asBool]
case isFalse =>
replace ⟨ tx₂, bty₂, c₂, htx₂, hty₂, ht ⟩ := ht
replace ⟨ b₁ , hv₁⟩ := instance_of_bool_is_bool hv₁
subst v₁
split at ht
all_goals
replace ⟨ ht, _ ⟩ := ht
subst tx
simp [evaluate]
simp [checkLevel] at hl
specialize ih₁ hs hc hr htx₁ (by simp [hl])
rw [←ih₁]
rcases he₁ with he₁ | he₁ | he₁ | he₁ <;>
simp [he₁, Result.as, Coe.coe, Value.asBool]
cases b₁ <;> simp
specialize hgc he₁
specialize ih₂ hs (capability_union_invariant hc hgc) hr htx₂ (by simp [hl])
simp [ih₂]
34 changes: 34 additions & 0 deletions cedar-lean/Cedar/Thm/Validation/Levels/Basic.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/-
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.
-/

import Cedar.Thm.Validation.Typechecker.Basic

namespace Cedar.Thm

/-!
Basic definitions for levels proof
-/

open Cedar.Spec
open Cedar.Validation

def TypedAtLevelIsSound (e : Expr) : Prop := ∀ {n : Nat} {tx : TypedExpr} {c c₁ : Capabilities} {env :Environment} {request : Request} {entities slice : Entities},
slice = entities.sliceAtLevel request n →
CapabilitiesInvariant c request entities →
RequestAndEntitiesMatchEnvironment env request entities →
typeOf e c env = Except.ok (tx, c₁) →
checkLevel tx n →
evaluate e request entities = evaluate e request slice
Loading