Skip to content

Commit 80ce513

Browse files
committed
fix(stack-encrypt): show EQL fields in a planned context, and copy the plan only when the caller adds to it
`caller_shapes` skipped the fields the resolver runs, so `contexts()` on a `Planned` and on a `QueryPlan` left an EQL field out, and the query plan reported no such field. Each is now a leaf under its label alone, which is the context the resolver seals it under. `under_caller` and `under_scopes` borrow the plan when the caller adds nothing and copy it once when they do, where every call copied it before. Refs #1486.
1 parent 409c918 commit 80ce513

4 files changed

Lines changed: 68 additions & 17 deletions

File tree

‎packages/stack-encrypt/src/contexts.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,17 @@ pub(crate) struct LeafShape {
11411141
/// The shapes of every leaf of a declaration, in order.
11421142
pub(crate) type Shapes = Vec<LeafShape>;
11431143

1144+
/// One named leaf under a context known when the declaration is written,
1145+
/// and extended by nothing: a data plan's EQL field, sealed under its label
1146+
/// by the resolver.
1147+
#[cfg_attr(not(feature = "dynamic"), allow(dead_code))]
1148+
pub(crate) fn known_leaf(name: &str, piece: ContextPiece<'static>) -> LeafShape {
1149+
LeafShape {
1150+
name: Some(Arc::from(name)),
1151+
shape: Shape::Known(piece),
1152+
}
1153+
}
1154+
11441155
/// One leaf that runs under the context it is handed.
11451156
pub(crate) fn caller_leaf() -> Shapes {
11461157
vec![LeafShape {

‎packages/stack-encrypt/src/dynamic/planned.rs‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
//! # Ok::<(), Box<dyn std::error::Error>>(())
6262
//! # }).unwrap();
6363
//! ```
64+
use std::borrow::Cow;
6465
use std::fmt;
6566

6667
use zeroize::Zeroize;
@@ -208,31 +209,40 @@ fn wrap(error: Error) -> crate::Error {
208209
impl Plan {
209210
/// This plan under the caller's context: its scope as one more part of
210211
/// every field's context, then its bindings after the plan's own.
211-
/// `None` leaves the plan as it is.
212-
fn under_caller(self, context: Option<CallerContext>) -> Result<Self, Error> {
212+
/// `None` leaves the plan as it is, borrowed: nothing is copied for a
213+
/// call with no caller context.
214+
fn under_caller(&self, context: Option<CallerContext>) -> Result<Cow<'_, Self>, Error> {
213215
let Some(context) = context else {
214-
return Ok(self);
216+
return Ok(Cow::Borrowed(self));
215217
};
216218
let (scopes, bindings) = context.checked()?.pieces();
217-
let plan = self.with_scopes(scopes)?;
219+
let plan = self.clone().with_scopes(scopes)?;
218220
if bindings.is_empty() {
219-
return Ok(plan);
221+
return Ok(Cow::Owned(plan));
220222
}
221223
let mut all = plan.bindings().to_vec();
222224
all.extend(bindings);
223-
plan.with_bindings(all)
225+
plan.with_bindings(all).map(Cow::Owned)
224226
}
225227

226-
/// This plan under a query's scopes, which hold no binding.
227-
fn under_scopes(self, scopes: &DeclaredContext, field: &str) -> Result<Self, Error> {
228+
/// This plan under a query's scopes, which hold no binding. No scope
229+
/// leaves the plan as it is, borrowed.
230+
fn under_scopes<'p>(
231+
&'p self,
232+
scopes: &DeclaredContext,
233+
field: &str,
234+
) -> Result<Cow<'p, Self>, Error> {
228235
if scopes.has_bindings() {
229236
return Err(Error::Plan {
230237
field: Some(field.to_owned()),
231238
reason: Reason::BindInQuery,
232239
});
233240
}
241+
if !scopes.is_extended() {
242+
return Ok(Cow::Borrowed(self));
243+
}
234244
let pieces = scopes.parts().iter().filter_map(|part| part.pieces().0);
235-
self.with_scopes(pieces)
245+
self.clone().with_scopes(pieces).map(Cow::Owned)
236246
}
237247

238248
/// A query plan for `field` and `operator`, with no EQL types: refused
@@ -366,7 +376,7 @@ fn encrypt_record<'a, K: 'static>(
366376
resolver: &(impl TargetResolver + ?Sized),
367377
context: OptionalContext<CallerContext>,
368378
) -> Pending<'a, StackCipherText, K> {
369-
let plan = match plan.clone().under_caller(context.into_inner()) {
379+
let plan = match plan.under_caller(context.into_inner()) {
370380
Ok(plan) => plan,
371381
Err(error) => return Pending::failed(cipher, wrap(error)),
372382
};
@@ -389,8 +399,10 @@ where
389399
{
390400
type ContextInput = OptionalContext<CallerContext>;
391401
fn decryption<K: 'static>(self, context: Self::ContextInput) -> Decryption<ValueFrame, K> {
392-
let plan = match self.plan.clone().under_caller(context.into_inner()) {
393-
Ok(plan) => plan,
402+
// The opening is a closure the cipher runs later, so it owns its
403+
// plan: one copy, with the caller's parts applied.
404+
let plan = match self.plan.under_caller(context.into_inner()) {
405+
Ok(plan) => plan.into_owned(),
394406
Err(error) => return Decryption::failed(wrap(error)),
395407
};
396408
let Planned {
@@ -460,7 +472,7 @@ impl QueryPlan {
460472
scopes: &DeclaredContext,
461473
resolver: &(impl TargetResolver + ?Sized),
462474
) -> Pending<'a, QueryTerm, K> {
463-
let plan = match self.plan.clone().under_scopes(scopes, &self.field) {
475+
let plan = match self.plan.under_scopes(scopes, &self.field) {
464476
Ok(plan) => plan,
465477
Err(error) => return Pending::failed(cipher, wrap(error)),
466478
};

‎packages/stack-encrypt/src/dynamic/record.rs‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ use crate::{
162162
CipherText, ContextPiece, KeysetCipher, Label, NonEmpty, PassthroughFrame, Pending,
163163
StackCipherText,
164164
};
165+
use vitaminc_aead::IntoContext as _;
165166

166167
/// The map key a target field's EQL value rides under in a stored record:
167168
/// wire format, like the output keys (see the
@@ -1147,10 +1148,18 @@ impl Plan {
11471148
/// record.
11481149
pub(super) fn caller_shapes(&self) -> Result<crate::contexts::Shapes, crate::Error> {
11491150
let plan = self.lower::<()>()?;
1150-
Ok(crate::contexts::under_declared_then_caller(
1151-
plan.shapes(),
1152-
&self.declared_context(),
1153-
))
1151+
let mut shapes =
1152+
crate::contexts::under_declared_then_caller(plan.shapes(), &self.declared_context());
1153+
// A target field is the resolver's, sealed under its label alone:
1154+
// an EQL value takes no extension and no binding, and the plan
1155+
// refuses a caller's parts for it before it runs.
1156+
for field in self.target_fields() {
1157+
shapes.push(crate::contexts::known_leaf(
1158+
&field.name,
1159+
field.label().clone().into_context(),
1160+
));
1161+
}
1162+
Ok(shapes)
11541163
}
11551164

11561165
/// The context every field's label is run under: the extension, as the

‎packages/stack-encrypt/tests/planned.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,25 @@ mod a_target_field {
634634
assert_eq!(through_planned, through_handle);
635635
}
636636

637+
#[test]
638+
fn shows_its_context_under_its_label_alone() {
639+
let plan = target_plan();
640+
// An EQL value is sealed under its label by the resolver, and takes
641+
// no caller part, so the view shows the label and no placeholder.
642+
assert_eq!(
643+
Planned::new(&plan, frame("a"))
644+
.contexts()
645+
.unwrap()
646+
.to_string(),
647+
"email\n context: users / email\n"
648+
);
649+
let by_email = plan.query_with("email", Op::Eq, &OneType).unwrap();
650+
assert_eq!(
651+
by_email.contexts().unwrap().to_string(),
652+
"context: users / email\n"
653+
);
654+
}
655+
637656
#[test]
638657
fn is_refused_without_its_resolver() {
639658
let plan = target_plan();

0 commit comments

Comments
 (0)