-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathrls.rs
More file actions
716 lines (665 loc) · 23.7 KB
/
rls.rs
File metadata and controls
716 lines (665 loc) · 23.7 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
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
use std::rc::Rc;
use spacetimedb_lib::identity::AuthCtx;
use spacetimedb_primitives::TableId;
use spacetimedb_sql_parser::ast::BinOp;
use crate::{
check::{parse_and_type_sub, SchemaView},
expr::{AggType, Expr, FieldProject, LeftDeepJoin, ProjectList, ProjectName, RelExpr, Relvar},
};
/// The main driver of RLS resolution for subscription queries.
/// Mainly a wrapper around [resolve_views_for_expr].
pub fn resolve_views_for_sub(
tx: &impl SchemaView,
expr: ProjectName,
auth: &AuthCtx,
has_param: &mut bool,
) -> anyhow::Result<Vec<ProjectName>> {
// RLS does not apply to the database owner
if auth.is_owner() {
return Ok(vec![expr]);
}
let Some(return_name) = expr.return_name().map(|name| name.to_owned().into_boxed_str()) else {
anyhow::bail!("Could not determine return type during RLS resolution")
};
// Unwrap the underlying `RelExpr`
let expr = match expr {
ProjectName::None(expr) | ProjectName::Some(expr, _) => expr,
};
resolve_views_for_expr(
tx,
expr,
// Do not ignore the return table when checking for RLS rules
None,
// Resolve list is empty as we are not yet resolving any RLS rules
Rc::new(ResolveList::None),
has_param,
&mut 0,
auth,
)
.map(|fragments| {
fragments
.into_iter()
// The expanded fragments could be join trees,
// so wrap each of them in an outer project.
.map(|expr| ProjectName::Some(expr, return_name.clone()))
.collect()
})
}
/// The main driver of RLS resolution for sql queries.
/// Mainly a wrapper around [resolve_views_for_expr].
pub fn resolve_views_for_sql(tx: &impl SchemaView, expr: ProjectList, auth: &AuthCtx) -> anyhow::Result<ProjectList> {
// RLS does not apply to the database owner
if auth.is_owner() {
return Ok(expr);
}
// The subscription language is a subset of the sql language.
// Use the subscription helper if this is a compliant expression.
// Use the generic resolver otherwise.
let resolve_for_sub = |expr| resolve_views_for_sub(tx, expr, auth, &mut false);
let resolve_for_sql = |expr| {
resolve_views_for_expr(
// Use all default values
tx,
expr,
None,
Rc::new(ResolveList::None),
&mut false,
&mut 0,
auth,
)
};
match expr {
ProjectList::Limit(expr, n) => Ok(ProjectList::Limit(Box::new(resolve_views_for_sql(tx, *expr, auth)?), n)),
ProjectList::Name(exprs) => Ok(ProjectList::Name(
exprs
.into_iter()
.map(resolve_for_sub)
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.collect(),
)),
ProjectList::List(exprs, fields) => Ok(ProjectList::List(
exprs
.into_iter()
.map(resolve_for_sql)
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.collect(),
fields,
)),
ProjectList::Agg(exprs, AggType::Count, name, ty) => Ok(ProjectList::Agg(
exprs
.into_iter()
.map(resolve_for_sql)
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.collect(),
AggType::Count,
name,
ty,
)),
}
}
/// A list for detecting cycles during RLS resolution.
enum ResolveList {
None,
Some(TableId, Rc<ResolveList>),
}
impl ResolveList {
fn new(table_id: TableId, list: Rc<Self>) -> Rc<Self> {
Rc::new(Self::Some(table_id, list))
}
fn contains(&self, table_id: &TableId) -> bool {
match self {
Self::None => false,
Self::Some(id, suffix) if id != table_id => suffix.contains(table_id),
Self::Some(..) => true,
}
}
}
/// The main utility responsible for view resolution.
///
/// But what is view resolution and why do we need it?
///
/// A view is a named query that can be referenced as though it were just a regular table.
/// In SpacetimeDB, Row Level Security (RLS) is implemented using views.
/// We must resolve/expand these views in order to guarantee the correct access controls.
///
/// Before we discuss the implementation, a quick word on `return_table_id`.
///
/// Why do we care about it?
/// What does it mean for it to be `None`?
///
/// If this IS NOT a user query, it must be a view definition.
/// In SpacetimeDB this means we're expanding an RLS filter.
/// RLS filters cannot be self-referential, meaning that within a filter,
/// we cannot recursively expand references to its return table.
///
/// However, a `None` value implies that this expression is a user query,
/// and so we should attempt to expand references to the return table.
///
/// Now back to the implementation.
///
/// Take the following join tree as an example:
/// ```text
/// x
/// / \
/// x c
/// / \
/// a b
/// ```
///
/// Let's assume b is a view with the following structure:
/// ```text
/// x
/// / \
/// x f
/// / \
/// d e
/// ```
///
/// Logically we just want to expand the tree like so:
/// ```text
/// x
/// / \
/// x c
/// / \
/// a x
/// / \
/// x f
/// / \
/// d e
/// ```
///
/// However the join trees at this level are left deep.
/// To maintain this invariant, the correct expansion would be:
/// ```text
/// x
/// / \
/// x c
/// / \
/// x f
/// / \
/// x e
/// / \
/// a d
/// ```
///
/// That is, the subtree whose root is the left sibling of the node being expanded,
/// i.e. the subtree rooted at `a` in the above example,
/// must be pushed below the leftmost leaf node of the view expansion.
fn resolve_views_for_expr(
tx: &impl SchemaView,
view: RelExpr,
return_table_id: Option<TableId>,
resolving: Rc<ResolveList>,
has_param: &mut bool,
suffix: &mut usize,
auth: &AuthCtx,
) -> anyhow::Result<Vec<RelExpr>> {
let is_return_table = |relvar: &Relvar| return_table_id.is_some_and(|id| relvar.schema.table_id == id);
// Collect the table ids queried by this view.
// Ignore the id of the return table, since RLS views cannot be recursive.
let mut names = vec![];
view.visit(&mut |expr| match expr {
RelExpr::RelVar(rhs)
| RelExpr::LeftDeepJoin(LeftDeepJoin { rhs, .. })
| RelExpr::EqJoin(LeftDeepJoin { rhs, .. }, ..)
if !is_return_table(rhs) =>
{
names.push((rhs.schema.table_id, rhs.alias.clone()));
}
_ => {}
});
// Are we currently resolving any of them?
if let Some(table_id) = names
.iter()
.map(|(table_id, _)| table_id)
.find(|table_id| resolving.contains(table_id))
{
anyhow::bail!("Discovered cyclic dependency when resolving RLS rules for table id `{table_id}`");
}
let return_name = |expr: &ProjectName| {
expr.return_name()
.map(|name| name.to_owned())
.ok_or_else(|| anyhow::anyhow!("Could not resolve table reference in RLS filter"))
};
let mut view_def_fragments = vec![];
for (table_id, alias) in names {
let mut view_fragments = vec![];
for sql in tx.rls_rules_for_table(table_id)? {
// Parse and type check the RLS filter
let (expr, is_parameterized) = parse_and_type_sub(&sql, tx, auth)?;
// Are any of the RLS rules parameterized?
*has_param = *has_param || is_parameterized;
// We need to know which relvar is being returned for alpha-renaming
let return_name = return_name(&expr)?;
// Resolve views within the RLS filter itself
let fragments = resolve_views_for_expr(
tx,
expr.unwrap(),
Some(table_id),
ResolveList::new(table_id, resolving.clone()),
has_param,
suffix,
auth,
)?;
// Run alpha conversion on each view definition
alpha_rename_fragments(
// The revlar returned from the inner expression
&return_name,
// Its corresponding alias in the outer expression
&alias,
fragments,
&mut view_fragments,
suffix,
);
}
if !view_fragments.is_empty() {
view_def_fragments.push((table_id, alias, view_fragments));
}
}
/// After we collect all the necessary view definitions and run alpha conversion,
/// this function handles the actual replacement of the view with its definition.
fn expand_views(expr: RelExpr, view_def_fragments: &[(TableId, Box<str>, Vec<RelExpr>)], out: &mut Vec<RelExpr>) {
match view_def_fragments {
[] => out.push(expr),
[(table_id, alias, fragments), view_def_fragments @ ..] => {
for fragment in fragments {
let expanded = expand_leaf(expr.clone(), *table_id, alias, fragment);
expand_views(expanded, view_def_fragments, out);
}
}
}
}
let mut resolved = vec![];
expand_views(view, &view_def_fragments, &mut resolved);
Ok(resolved)
}
/// This is the main driver of alpha conversion.
///
/// For each expression that we alpha convert,
/// we append a unique suffix to the names in that expression,
/// with the one exception being the name of the return table.
/// The return table is aliased in the outer expression,
/// and so we use the same alias in the inner expression.
///
/// Ex.
///
/// Let `v` be a view defined as:
/// ```sql
/// SELECT r.* FROM r JOIN s ON r.id = s.id
/// ```
///
/// Take the following user query:
/// ```sql
/// SELECT t.* FROM v JOIN t ON v.id = t.id WHERE v.x = 0
/// ```
///
/// After alpha conversion, the expansion becomes:
/// ```sql
/// SELECT t.*
/// FROM r AS v
/// JOIN s AS s_1 ON v.id = s_1.id
/// JOIN t AS t ON t.id = v.id WHERE v.x = 0
/// ```
fn alpha_rename_fragments(
return_name: &str,
outer_alias: &str,
inputs: Vec<RelExpr>,
output: &mut Vec<RelExpr>,
suffix: &mut usize,
) {
for mut fragment in inputs {
*suffix += 1;
alpha_rename(&mut fragment, &mut |name: &str| {
if name == return_name {
return outer_alias.to_owned().into_boxed_str();
}
(name.to_owned() + "_" + &suffix.to_string()).into_boxed_str()
});
output.push(fragment);
}
}
/// When expanding a view, we must do an alpha conversion on the view definition.
/// This involves renaming the table aliases before replacing the view reference.
fn alpha_rename(expr: &mut RelExpr, f: &mut impl FnMut(&str) -> Box<str>) {
/// Helper for renaming a relvar
fn rename(relvar: &mut Relvar, f: &mut impl FnMut(&str) -> Box<str>) {
relvar.alias = f(&relvar.alias);
}
/// Helper for renaming a field reference
fn rename_field(field: &mut FieldProject, f: &mut impl FnMut(&str) -> Box<str>) {
field.table = f(&field.table);
}
expr.visit_mut(&mut |expr| match expr {
RelExpr::RelVar(rhs) | RelExpr::LeftDeepJoin(LeftDeepJoin { rhs, .. }) => {
rename(rhs, f);
}
RelExpr::EqJoin(LeftDeepJoin { rhs, .. }, a, b) => {
rename(rhs, f);
rename_field(a, f);
rename_field(b, f);
}
RelExpr::Select(_, expr) => {
expr.visit_mut(&mut |expr| {
if let Expr::Field(field) = expr {
rename_field(field, f);
}
});
}
});
}
/// Extends a left deep join tree with another.
///
/// Ex.
///
/// Assume `expr` is given by:
/// ```text
/// x
/// / \
/// x f
/// / \
/// d e
/// ```
///
/// Assume `with` is given by:
/// ```text
/// x
/// / \
/// x c
/// / \
/// a b
/// ```
///
/// This function extends `expr` by pushing `with` to the left-most leaf node:
/// ```text
/// x
/// / \
/// x f
/// / \
/// x e
/// / \
/// x d
/// / \
/// x c
/// / \
/// a b
/// ```
fn extend_lhs(expr: RelExpr, with: RelExpr) -> RelExpr {
match expr {
RelExpr::RelVar(rhs) => RelExpr::LeftDeepJoin(LeftDeepJoin {
lhs: Box::new(with),
rhs,
}),
RelExpr::Select(input, expr) => RelExpr::Select(Box::new(extend_lhs(*input, with)), expr),
RelExpr::LeftDeepJoin(join) => RelExpr::LeftDeepJoin(LeftDeepJoin {
lhs: Box::new(extend_lhs(*join.lhs, with)),
..join
}),
RelExpr::EqJoin(join, a, b) => RelExpr::EqJoin(
LeftDeepJoin {
lhs: Box::new(extend_lhs(*join.lhs, with)),
..join
},
a,
b,
),
}
}
/// Replaces the leaf node determined by `table_id` and `alias` with the subtree `with`.
/// Ensures the expanded tree stays left deep.
fn expand_leaf(expr: RelExpr, table_id: TableId, alias: &str, with: &RelExpr) -> RelExpr {
let ok = |relvar: &Relvar| relvar.schema.table_id == table_id && relvar.alias.as_ref() == alias;
match expr {
RelExpr::RelVar(relvar, ..) if ok(&relvar) => with.clone(),
RelExpr::RelVar(..) => expr,
RelExpr::Select(input, expr) => RelExpr::Select(Box::new(expand_leaf(*input, table_id, alias, with)), expr),
RelExpr::LeftDeepJoin(join) if ok(&join.rhs) => extend_lhs(with.clone(), *join.lhs),
RelExpr::LeftDeepJoin(LeftDeepJoin { lhs, rhs }) => RelExpr::LeftDeepJoin(LeftDeepJoin {
lhs: Box::new(expand_leaf(*lhs, table_id, alias, with)),
rhs,
}),
RelExpr::EqJoin(join, a, b) if ok(&join.rhs) => RelExpr::Select(
Box::new(extend_lhs(with.clone(), *join.lhs)),
Expr::BinOp(BinOp::Eq, Box::new(Expr::Field(a)), Box::new(Expr::Field(b))),
),
RelExpr::EqJoin(LeftDeepJoin { lhs, rhs }, a, b) => RelExpr::EqJoin(
LeftDeepJoin {
lhs: Box::new(expand_leaf(*lhs, table_id, alias, with)),
rhs,
},
a,
b,
),
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use pretty_assertions as pretty;
use spacetimedb_lib::{identity::AuthCtx, AlgebraicType, AlgebraicValue, Identity, ProductType};
use spacetimedb_primitives::TableId;
use spacetimedb_schema::{
def::ModuleDef,
schema::{Schema, TableSchema},
};
use spacetimedb_sql_parser::ast::BinOp;
use crate::{
check::{parse_and_type_sub, test_utils::build_module_def, SchemaView},
expr::{Expr, FieldProject, LeftDeepJoin, ProjectName, RelExpr, Relvar},
};
use super::resolve_views_for_sub;
pub struct SchemaViewer(pub ModuleDef);
impl SchemaView for SchemaViewer {
fn table_id(&self, name: &str) -> Option<TableId> {
match name {
"users" => Some(TableId(0)),
"admins" => Some(TableId(1)),
"player" => Some(TableId(2)),
_ => None,
}
}
fn schema_for_table(&self, table_id: TableId) -> Option<Arc<TableSchema>> {
match table_id.idx() {
0 => Some((TableId(0), "users")),
1 => Some((TableId(1), "admins")),
2 => Some((TableId(2), "player")),
_ => None,
}
.and_then(|(table_id, name)| {
self.0
.table(name)
.map(|def| Arc::new(TableSchema::from_module_def(&self.0, def, (), table_id)))
})
}
fn rls_rules_for_table(&self, table_id: TableId) -> anyhow::Result<Vec<Box<str>>> {
match table_id {
TableId(0) => Ok(vec!["select * from users where identity = :sender".into()]),
TableId(1) => Ok(vec!["select * from admins where identity = :sender".into()]),
TableId(2) => Ok(vec![
"select player.* from player join users u on player.id = u.id".into(),
"select player.* from player join admins".into(),
]),
_ => Ok(vec![]),
}
}
}
fn module_def() -> ModuleDef {
build_module_def(vec![
(
"users",
ProductType::from([("identity", AlgebraicType::identity()), ("id", AlgebraicType::U64)]),
),
(
"admins",
ProductType::from([("identity", AlgebraicType::identity()), ("id", AlgebraicType::U64)]),
),
(
"player",
ProductType::from([("id", AlgebraicType::U64), ("level_num", AlgebraicType::U64)]),
),
])
}
/// Parse, type check, and resolve RLS rules
fn resolve(sql: &str, tx: &impl SchemaView, auth: &AuthCtx) -> anyhow::Result<Vec<ProjectName>> {
let (expr, _) = parse_and_type_sub(sql, tx, auth)?;
resolve_views_for_sub(tx, expr, auth, &mut false)
}
#[test]
fn test_rls_for_owner() -> anyhow::Result<()> {
let tx = SchemaViewer(module_def());
let auth = AuthCtx::new(Identity::ONE, Identity::ONE);
let sql = "select * from users";
let resolved = resolve(sql, &tx, &auth)?;
let users_schema = tx.schema("users").unwrap();
pretty::assert_eq!(
resolved,
vec![ProjectName::None(RelExpr::RelVar(Relvar {
schema: users_schema,
alias: "users".into(),
delta: None,
}))]
);
Ok(())
}
#[test]
fn test_rls_for_non_owner() -> anyhow::Result<()> {
let tx = SchemaViewer(module_def());
let auth = AuthCtx::new(Identity::ZERO, Identity::ONE);
let sql = "select * from users";
let resolved = resolve(sql, &tx, &auth)?;
let users_schema = tx.schema("users").unwrap();
pretty::assert_eq!(
resolved,
vec![ProjectName::Some(
RelExpr::Select(
Box::new(RelExpr::RelVar(Relvar {
schema: users_schema,
alias: "users".into(),
delta: None,
})),
Expr::BinOp(
BinOp::Eq,
Box::new(Expr::Field(FieldProject {
table: "users".into(),
field: 0,
ty: AlgebraicType::identity(),
})),
Box::new(Expr::Value(Identity::ONE.into(), AlgebraicType::identity()))
)
),
"users".into()
)]
);
Ok(())
}
#[test]
fn test_multiple_rls_rules_for_table() -> anyhow::Result<()> {
let tx = SchemaViewer(module_def());
let auth = AuthCtx::new(Identity::ZERO, Identity::ONE);
let sql = "select * from player where level_num = 5";
let resolved = resolve(sql, &tx, &auth)?;
let users_schema = tx.schema("users").unwrap();
let admins_schema = tx.schema("admins").unwrap();
let player_schema = tx.schema("player").unwrap();
pretty::assert_eq!(
resolved,
vec![
ProjectName::Some(
RelExpr::Select(
Box::new(RelExpr::Select(
Box::new(RelExpr::Select(
Box::new(RelExpr::LeftDeepJoin(LeftDeepJoin {
lhs: Box::new(RelExpr::RelVar(Relvar {
schema: player_schema.clone(),
alias: "player".into(),
delta: None,
})),
rhs: Relvar {
schema: users_schema.clone(),
alias: "u_2".into(),
delta: None,
},
})),
Expr::BinOp(
BinOp::Eq,
Box::new(Expr::Field(FieldProject {
table: "u_2".into(),
field: 0,
ty: AlgebraicType::identity(),
})),
Box::new(Expr::Value(Identity::ONE.into(), AlgebraicType::identity())),
),
)),
Expr::BinOp(
BinOp::Eq,
Box::new(Expr::Field(FieldProject {
table: "player".into(),
field: 0,
ty: AlgebraicType::U64,
})),
Box::new(Expr::Field(FieldProject {
table: "u_2".into(),
field: 1,
ty: AlgebraicType::U64,
})),
),
)),
Expr::BinOp(
BinOp::Eq,
Box::new(Expr::Field(FieldProject {
table: "player".into(),
field: 1,
ty: AlgebraicType::U64,
})),
Box::new(Expr::Value(AlgebraicValue::U64(5), AlgebraicType::U64)),
),
),
"player".into(),
),
ProjectName::Some(
RelExpr::Select(
Box::new(RelExpr::Select(
Box::new(RelExpr::LeftDeepJoin(LeftDeepJoin {
lhs: Box::new(RelExpr::RelVar(Relvar {
schema: player_schema.clone(),
alias: "player".into(),
delta: None,
})),
rhs: Relvar {
schema: admins_schema.clone(),
alias: "admins_4".into(),
delta: None,
},
})),
Expr::BinOp(
BinOp::Eq,
Box::new(Expr::Field(FieldProject {
table: "admins_4".into(),
field: 0,
ty: AlgebraicType::identity(),
})),
Box::new(Expr::Value(Identity::ONE.into(), AlgebraicType::identity())),
),
)),
Expr::BinOp(
BinOp::Eq,
Box::new(Expr::Field(FieldProject {
table: "player".into(),
field: 1,
ty: AlgebraicType::U64,
})),
Box::new(Expr::Value(AlgebraicValue::U64(5), AlgebraicType::U64)),
),
),
"player".into(),
),
]
);
Ok(())
}
}