Skip to content

Commit ff1ec50

Browse files
committed
fix(stack-encrypt): check the label on every prepare, whatever check did
A chain checked and then extended kept its verifier, and the cached branch of `prepare` skipped `check_label` for the changed context. The label check is about the context; the verifier is about the value. So `prepare` checks the label on every call and keeps only the verifier. `a_chain_extended_after_its_check_is_checked_again` pins it against an index that takes no extension. Refs #1279.
1 parent 6123bba commit ff1ec50

2 files changed

Lines changed: 94 additions & 9 deletions

File tree

‎packages/stack-encrypt/src/plan/chain.rs‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -971,18 +971,19 @@ where
971971
Ok(label) => label,
972972
Err(error) => return Pending::failed(keyset, error.into()),
973973
};
974-
// `check` ran before any keyset was resolved and kept the verifier.
975-
// A chain prepared on its own is held to the same refusals, so it
976-
// prepares here instead: no term is derived under a context or from
977-
// a value the index refuses, and no query is prepared twice.
974+
// The context is checked here whatever `check` did: a chain can be
975+
// extended between the two, and the label check is about the
976+
// context. `check` ran before any keyset was resolved and kept the
977+
// verifier, which is about the value alone. A chain prepared on its
978+
// own prepares here instead: no term is derived under a context or
979+
// from a value the index refuses, and no query is prepared twice.
978980
let extended = self.common.extend.is_extended();
981+
if let Err(error) = self.index.check_label(&label, extended) {
982+
return Pending::failed(keyset, error);
983+
}
979984
let verifier = match self.verifier {
980985
Some(verifier) => verifier,
981-
None => match self
982-
.index
983-
.check_label(&label, extended)
984-
.and_then(|()| self.index.prepare(&self.value))
985-
{
986+
None => match self.index.prepare(&self.value) {
986987
Ok(verifier) => verifier,
987988
Err(error) => return Pending::failed(keyset, error),
988989
},

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,90 @@ fn refused_as_not_a_column<T: std::fmt::Debug>(result: Result<T, Error>) {
420420
);
421421
}
422422

423+
/// A target whose index refuses an extended context: what an EQL column
424+
/// is, stored under its table and column alone.
425+
struct Column;
426+
427+
impl EncryptFrom<String> for Column {
428+
type Context = NonEmpty<Label>;
429+
fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context>
430+
where
431+
String: 's,
432+
{
433+
<EqualityTerm as EncryptFrom<String>>::encryption()
434+
.accepting::<NonEmpty<Label>>()
435+
.map(|_| Column)
436+
}
437+
fn indexes() -> Vec<IndexSpec> {
438+
vec![IndexSpec::Equality]
439+
}
440+
}
441+
442+
impl op::LabelledTarget for Column {}
443+
444+
/// The equality index that refuses an extended context.
445+
#[derive(Default)]
446+
struct Unextendable;
447+
448+
impl Index<String> for Unextendable {
449+
type Term = EqualityTerm;
450+
fn spec(&self) -> IndexSpec {
451+
Index::<String>::spec(&Equality)
452+
}
453+
fn operation<'s, K: 'static, M: ConsumeSource<'s, String>>(
454+
&self,
455+
) -> Encryption<'s, String, EqualityTerm, K, CallerContext, M> {
456+
Index::<String>::operation(&Equality)
457+
}
458+
}
459+
460+
impl op::Supports<op::Eq, String> for Unextendable {
461+
type Query = EqualityTerm;
462+
type Verifier = ();
463+
fn prepare(&self, value: &String) -> Result<(), Error> {
464+
<Equality as op::Supports<op::Eq, String>>::prepare(&Equality, value)
465+
}
466+
fn check_label(&self, _: &Label, extended: bool) -> Result<(), Error> {
467+
if extended {
468+
Err(Error::Other("no extension".into()))
469+
} else {
470+
Ok(())
471+
}
472+
}
473+
fn query((): (), term: EqualityTerm, _: op::QueryToken) -> EqualityTerm {
474+
term
475+
}
476+
}
477+
478+
impl op::Answers<op::Eq, String> for Column {
479+
type Via = Unextendable;
480+
}
481+
482+
/// A chain checked and then extended is checked again when it is prepared:
483+
/// the label check is about the context, which a later `.extend(..)`
484+
/// changes, so a kept verifier does not carry a stale acceptance.
485+
#[tokio::test]
486+
async fn a_chain_extended_after_its_check_is_checked_again() {
487+
use stack_encrypt::plan::Operation;
488+
let cipher = stack_cipher().await;
489+
let plan = Plan::value::<String>()
490+
.context("users/email")
491+
.encrypt_into::<Column>()
492+
.build()
493+
.unwrap();
494+
let mut query = cipher.query(op::Eq, "bob@example.com").on(&plan);
495+
query.check().unwrap();
496+
let refused = query.extend(7u64).await;
497+
assert!(
498+
matches!(&refused, Err(Error::Other(reason)) if reason.to_string() == "no extension"),
499+
"{refused:?}"
500+
);
501+
// Unextended, the checked chain derives its term.
502+
let mut query = cipher.query(op::Eq, "bob@example.com").on(&plan);
503+
query.check().unwrap();
504+
assert!(query.await.is_ok());
505+
}
506+
423507
/// A `LabelledTarget` is queried through its plan though nothing can be
424508
/// encrypted through it, and its query term reads the context its terms
425509
/// were derived under from the token: the plan's label or the call's, and

0 commit comments

Comments
 (0)