Repository navigation
Expand file tree
/
Copy pathsecurity.rs
More file actions
144 lines (133 loc) · 4.68 KB
/
Copy pathsecurity.rs
File metadata and controls
144 lines (133 loc) · 4.68 KB
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
//! Central, fail-closed security properties for managed Diamond types.
use crate::hir::CheckedProgram;
use crate::value::Type;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum MessageSafety {
Allowed,
Forbidden,
}
/// Security properties needed by compiler and runtime sinks.
///
/// These properties are derived from the complete type rather than annotations
/// supplied at individual call sites. Unknown or excessively recursive shapes
/// are treated as unsafe.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct TypeSecurity {
pub(crate) contains_secret: bool,
pub(crate) browser_managed: bool,
pub(crate) client_wire_safe: bool,
component_messages: MessageSafety,
}
impl TypeSecurity {
const fn public_wire() -> Self {
Self {
contains_secret: false,
browser_managed: true,
client_wire_safe: true,
component_messages: MessageSafety::Allowed,
}
}
const fn server_only() -> Self {
Self {
contains_secret: false,
browser_managed: false,
client_wire_safe: false,
component_messages: MessageSafety::Forbidden,
}
}
const fn secret() -> Self {
Self {
contains_secret: true,
browser_managed: false,
client_wire_safe: false,
component_messages: MessageSafety::Forbidden,
}
}
const fn browser_only() -> Self {
Self {
contains_secret: false,
browser_managed: true,
client_wire_safe: false,
component_messages: MessageSafety::Forbidden,
}
}
const fn combine(self, other: Self) -> Self {
Self {
contains_secret: self.contains_secret || other.contains_secret,
browser_managed: self.browser_managed && other.browser_managed,
client_wire_safe: self.client_wire_safe && other.client_wire_safe,
component_messages: if self.component_message_safe() && other.component_message_safe() {
MessageSafety::Allowed
} else {
MessageSafety::Forbidden
},
}
}
pub(crate) const fn component_message_safe(self) -> bool {
matches!(self.component_messages, MessageSafety::Allowed)
}
}
/// Derive the security properties of a managed type.
pub(crate) fn type_security(ty: &Type, program: &CheckedProgram) -> TypeSecurity {
analyze(ty, program, 0)
}
fn analyze(ty: &Type, program: &CheckedProgram, depth: usize) -> TypeSecurity {
if depth > 32 {
return TypeSecurity::secret();
}
match ty {
Type::Int
| Type::Decimal
| Type::Timestamp
| Type::Duration
| Type::Bool
| Type::Text
| Type::BoundedText(..)
| Type::Unit
| Type::Id(_)
| Type::Never => TypeSecurity::public_wire(),
Type::Secret(_) => TypeSecurity::secret(),
Type::Blob(_) | Type::Stream(_) | Type::DataError => TypeSecurity::browser_only(),
Type::List(inner) | Type::Option(inner) | Type::Result(inner) | Type::Success(inner) => {
analyze(inner, program, depth + 1)
}
Type::AppResult(value, error) => {
analyze(value, program, depth + 1).combine(analyze(error, program, depth + 1))
}
Type::Action(value, error) => {
let mut security =
analyze(value, program, depth + 1).combine(analyze(error, program, depth + 1));
// Actions use their dedicated dispatcher; no generic wire/storage escape.
security.client_wire_safe = false;
security
}
Type::Record(name) => {
program
.records
.get(name)
.map_or_else(TypeSecurity::secret, |record| {
record
.fields
.values()
.fold(TypeSecurity::public_wire(), |security, field| {
security.combine(analyze(&field.ty, program, depth + 1))
})
})
}
Type::Enum(name) => {
program
.enums
.get(name)
.map_or_else(TypeSecurity::secret, |enumeration| {
enumeration
.variants
.values()
.flatten()
.fold(TypeSecurity::public_wire(), |security, (_, field)| {
security.combine(analyze(field, program, depth + 1))
})
})
}
Type::Html | Type::Url | Type::Response => TypeSecurity::server_only(),
}
}