1use std::collections::HashMap;
6
7use serde_json::{Map, Value};
8use ssp2::primitives::{RawJson, Reader, Writer};
9use ssp2::segment::{
10 decode_row, encode_row, encode_sparse_row, Column, ColumnType, ColumnValue, Row, SparseRow,
11 SparseSlot,
12};
13
14use crate::schema::TableSchema;
15use crate::TransportError;
16
17#[derive(Clone, Default)]
22pub struct EncryptionConfig {
23 pub keys: HashMap<String, Vec<u8>>,
24 pub key_id_columns: HashMap<String, String>,
27}
28
29impl std::fmt::Debug for EncryptionConfig {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 struct Redacted(usize);
34 impl std::fmt::Debug for Redacted {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 write!(f, "<{} bytes>", self.0)
37 }
38 }
39 let keys: std::collections::BTreeMap<&String, Redacted> = self
40 .keys
41 .iter()
42 .map(|(id, key)| (id, Redacted(key.len())))
43 .collect();
44 f.debug_struct("EncryptionConfig")
45 .field("keys", &keys)
46 .field("key_id_columns", &self.key_id_columns)
47 .finish()
48 }
49}
50
51impl Drop for EncryptionConfig {
52 fn drop(&mut self) {
53 for key in self.keys.values_mut() {
56 key.fill(0);
57 }
58 }
59}
60
61impl EncryptionConfig {
62 pub fn is_empty(&self) -> bool {
63 self.keys.is_empty()
64 }
65
66 pub fn key_id_for(&self, table: &TableSchema, row: &Row) -> Result<String, String> {
71 let Some(column_name) = self.key_id_columns.get(&table.name) else {
72 return Ok(table.name.clone());
73 };
74 let Some(index) = table
75 .columns
76 .iter()
77 .position(|column| &column.name == column_name)
78 else {
79 return Err(format!(
80 "client.encrypt_failed: encryption key-id column {column_name:?} is not present on table {:?}",
81 table.name
82 ));
83 };
84 if table
85 .encrypted_columns
86 .iter()
87 .any(|column| column.index == index)
88 {
89 return Err(format!(
90 "client.encrypt_failed: encryption key-id column {column_name:?} on table {:?} must not be encrypted",
91 table.name
92 ));
93 }
94 match row.get(index).and_then(|value| value.as_ref()) {
95 Some(ColumnValue::String(key_id)) if !key_id.is_empty() => Ok(key_id.clone()),
96 _ => Err(format!(
97 "client.encrypt_failed: encryption key-id column {column_name:?} on table {:?} must contain a non-empty string",
98 table.name
99 )),
100 }
101 }
102}
103
104pub fn snake_to_camel(name: &str) -> String {
109 let is_mappable = {
110 let bare = name.trim_start_matches('_');
111 !bare.is_empty()
112 && bare.chars().next().is_some_and(|c| c.is_ascii_alphabetic())
113 && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
114 };
115 if !is_mappable {
116 return name.to_owned();
117 }
118 let lead_len = name.len() - name.trim_start_matches('_').len();
119 let (lead, bare) = name.split_at(lead_len);
120 let trail_len = bare.len() - bare.trim_end_matches('_').len();
121 let (middle, trail) = bare.split_at(bare.len() - trail_len);
122 let mut segments = middle.split('_').filter(|s| !s.is_empty());
123 let Some(first) = segments.next() else {
124 return name.to_owned();
125 };
126 let mut out = String::with_capacity(name.len());
127 out.push_str(lead);
128 out.push_str(first);
129 for segment in segments {
130 let mut chars = segment.chars();
131 if let Some(head) = chars.next() {
132 out.push(head.to_ascii_uppercase());
133 out.push_str(chars.as_str());
134 }
135 }
136 out.push_str(trail);
137 out
138}
139
140pub fn normalize_values_casing(
148 table: &TableSchema,
149 mut values: Map<String, Value>,
150) -> Result<Map<String, Value>, String> {
151 for column in &table.columns {
152 let camel = snake_to_camel(&column.name);
153 if camel == column.name {
154 continue;
155 }
156 if table.columns.iter().any(|c| c.name == camel) {
159 continue;
160 }
161 if table
162 .columns
163 .iter()
164 .filter(|c| snake_to_camel(&c.name) == camel)
165 .count()
166 > 1
167 {
168 continue;
169 }
170 if let Some(value) = values.remove(&camel) {
171 if values.contains_key(&column.name) {
172 return Err(format!(
173 "sync.invalid_request: table {:?}: column {:?} appears twice in mutation values (as both snake_case and camelCase) — pass it once",
174 table.name, column.name
175 ));
176 }
177 values.insert(column.name.clone(), value);
178 }
179 }
180 for key in values.keys() {
181 if !table.columns.iter().any(|column| column.name == *key) {
182 if key.starts_with("_sync_") {
183 return Err(format!(
184 "sync.invalid_request: table {:?}: {:?} is an internal sync column and cannot appear in mutation values",
185 table.name, key
186 ));
187 }
188 return Err(format!(
189 "sync.invalid_request: table {:?}: unknown column {:?} in mutation values (snake_case and camelCase keys are accepted)",
190 table.name, key
191 ));
192 }
193 }
194 Ok(values)
195}
196
197pub fn bytes_to_hex(bytes: &[u8]) -> String {
198 let mut out = String::with_capacity(bytes.len() * 2);
199 for b in bytes {
200 out.push(char::from(b"0123456789abcdef"[usize::from(b >> 4)]));
201 out.push(char::from(b"0123456789abcdef"[usize::from(b & 15)]));
202 }
203 out
204}
205
206pub fn hex_to_bytes(hex: &str) -> Result<Vec<u8>, String> {
207 if !hex.len().is_multiple_of(2) {
208 return Err("odd-length hex string".to_owned());
209 }
210 let mut out = Vec::with_capacity(hex.len() / 2);
211 let nibble = |byte| match byte {
212 b'0'..=b'9' => byte - b'0',
213 b'a'..=b'f' => byte - b'a' + 10,
214 b'A'..=b'F' => byte - b'A' + 10,
215 _ => u8::MAX,
216 };
217 for pair in hex.as_bytes().as_chunks::<2>().0 {
218 let high = if pair[0] == b'+' { 0 } else { nibble(pair[0]) };
221 let low = nibble(pair[1]);
222 if high == u8::MAX || low == u8::MAX {
223 return Err(if std::str::from_utf8(pair).is_err() {
224 "non-ASCII hex".to_owned()
225 } else {
226 "bad hex: invalid digit found in string".to_owned()
227 });
228 }
229 out.push((high << 4) | low);
230 }
231 Ok(out)
232}
233
234pub fn json_to_column_value(
237 column: &Column,
238 value: Option<&Value>,
239) -> Result<Option<ColumnValue>, String> {
240 let value = match value {
241 None | Some(Value::Null) => return Ok(None),
242 Some(v) => v,
243 };
244 let fail = |expected: &str| {
245 Err(format!(
246 "column {:?}: expected {expected}, got {value}",
247 column.name
248 ))
249 };
250 match column.ty {
251 ColumnType::String => match value.as_str() {
252 Some(s) => Ok(Some(ColumnValue::String(s.to_owned()))),
253 None => fail("a string"),
254 },
255 ColumnType::Integer => match value.as_i64() {
256 Some(i)
257 if (-ssp2::primitives::I64_SAFE_MAX..=ssp2::primitives::I64_SAFE_MAX)
258 .contains(&i) =>
259 {
260 Ok(Some(ColumnValue::Integer(i)))
261 }
262 _ => fail("an integer within the JavaScript safe-integer range"),
263 },
264 ColumnType::Float => match value.as_f64() {
265 Some(f) => Ok(Some(ColumnValue::Float(f))),
266 None => fail("a number"),
267 },
268 ColumnType::Boolean => match value.as_bool() {
269 Some(b) => Ok(Some(ColumnValue::Boolean(b))),
270 None => fail("a boolean"),
271 },
272 ColumnType::Json => match value.as_str() {
274 Some(s) => Ok(Some(ColumnValue::Json(RawJson(s.to_owned())))),
275 None => fail("a raw JSON string"),
276 },
277 ColumnType::BlobRef => match value.as_str() {
279 Some(s) => Ok(Some(ColumnValue::BlobRef(RawJson(s.to_owned())))),
280 None => fail("a raw BlobRef JSON string"),
281 },
282 ColumnType::Bytes => match value.get("$bytes").and_then(Value::as_str) {
283 Some(hex) => Ok(Some(ColumnValue::Bytes(hex_to_bytes(hex)?))),
284 None => fail("a {\"$bytes\": hex} object"),
285 },
286 ColumnType::Crdt => match value.get("$bytes").and_then(Value::as_str) {
288 Some(hex) => Ok(Some(ColumnValue::Crdt(hex_to_bytes(hex)?))),
289 None => fail("a {\"$bytes\": hex} object"),
290 },
291 }
292}
293
294pub fn column_value_to_json(value: &Option<ColumnValue>) -> Value {
296 match value {
297 None => Value::Null,
298 Some(ColumnValue::String(s)) => Value::from(s.clone()),
299 Some(ColumnValue::Integer(i)) => Value::from(*i),
300 Some(ColumnValue::Float(f)) => {
301 serde_json::Number::from_f64(*f).map_or(Value::Null, Value::Number)
302 }
303 Some(ColumnValue::Boolean(b)) => Value::from(*b),
304 Some(ColumnValue::Json(raw)) => Value::from(raw.0.clone()),
305 Some(ColumnValue::BlobRef(raw)) => Value::from(raw.0.clone()),
306 Some(ColumnValue::Bytes(bytes)) => {
307 let mut map = Map::new();
308 map.insert("$bytes".to_owned(), Value::from(bytes_to_hex(bytes)));
309 Value::Object(map)
310 }
311 Some(ColumnValue::Crdt(bytes)) => {
312 let mut map = Map::new();
313 map.insert("$bytes".to_owned(), Value::from(bytes_to_hex(bytes)));
314 Value::Object(map)
315 }
316 }
317}
318
319pub fn full_row_values(
323 table: &TableSchema,
324 mut values: Map<String, Value>,
325) -> Result<Map<String, Value>, String> {
326 for column in &table.columns {
327 if values.contains_key(&column.name) {
328 continue;
329 }
330 if !column.nullable {
331 return Err(format!(
332 "sync.invalid_request: table {:?}: column {:?} is not nullable (§6.1 full-row payloads)",
333 table.name, column.name
334 ));
335 }
336 values.insert(column.name.clone(), Value::Null);
337 }
338 Ok(values)
339}
340
341pub(crate) fn is_canonical_bytes_envelope(value: &Value) -> bool {
344 value
345 .as_object()
346 .filter(|map| map.len() == 1)
347 .and_then(|map| map.get("$bytes"))
348 .and_then(Value::as_str)
349 .is_some_and(|hex| {
350 hex.len().is_multiple_of(2) && hex.bytes().all(|byte| byte.is_ascii_hexdigit())
351 })
352}
353
354fn author_bytes_envelope_error(column: &Column, value: &Value) -> Option<String> {
360 if value.is_null() || !matches!(column.ty, ColumnType::Bytes | ColumnType::Crdt) {
364 return None;
365 }
366 if is_canonical_bytes_envelope(value) {
367 return None;
368 }
369 Some(format!(
370 "sync.invalid_request: column {:?}: expected exactly {{\"$bytes\": hex}} with an even number of hexadecimal digits, got {value}",
371 column.name
372 ))
373}
374
375pub fn encode_row_json(
380 table: &TableSchema,
381 row_id: &str,
382 values: &Map<String, Value>,
383 encryption: &EncryptionConfig,
384) -> Result<Vec<u8>, String> {
385 let mut row: Row = Vec::with_capacity(table.columns.len());
387 for column in &table.columns {
388 if let Some(raw) = values.get(&column.name) {
389 if let Some(error) = author_bytes_envelope_error(column, raw) {
390 return Err(error);
391 }
392 }
393 let value = json_to_column_value(column, values.get(&column.name))
397 .map_err(|error| format!("sync.invalid_request: {error}"))?;
398 if value.is_none() && !column.nullable {
399 return Err(format!(
400 "sync.invalid_request: table {:?}: column {:?} is not nullable (§6.1 full-row payloads)",
401 table.name, column.name
402 ));
403 }
404 row.push(value);
405 }
406 if table.has_encrypted_columns() {
407 encrypt_row(table, row_id, &mut row, encryption)?;
408 }
409 let mut w = Writer::new();
411 encode_row(&mut w, &table.wire_columns, &row);
412 Ok(w.into_bytes())
413}
414
415pub fn encode_sparse_row_json(
421 table: &TableSchema,
422 row_id: &str,
423 values: &Map<String, Value>,
424 encryption: &EncryptionConfig,
425 fallback: Option<&Row>,
426) -> Result<Vec<u8>, String> {
427 let mut row: SparseRow = Vec::with_capacity(table.columns.len());
428 for column in &table.columns {
429 let Some(raw) = values.get(&column.name) else {
430 row.push(SparseSlot::Absent);
431 continue;
432 };
433 if let Some(error) = author_bytes_envelope_error(column, raw) {
434 return Err(error);
435 }
436 let refused = match (column.ty, raw) {
439 (ColumnType::String, Value::String(text)) => text.contains('\0'),
440 (ColumnType::Json, Value::String(text)) => {
441 ssp2::primitives::parse_json_document(text, "json value").is_err()
442 }
443 (ColumnType::BlobRef, Value::String(text)) => {
444 ssp2::blob_ref::validate_blob_ref(text).is_err()
445 }
446 _ => false,
447 };
448 if refused {
449 return Err(format!(
450 "sync.invalid_request: column {:?}: the value is not a push admission value (§2.4)",
451 column.name
452 ));
453 }
454 let value = json_to_column_value(column, Some(raw))
455 .map_err(|error| format!("sync.invalid_request: {error}"))?;
456 if value.is_none() && !column.nullable {
457 return Err(format!(
458 "sync.invalid_request: table {:?}: column {:?} is not nullable (§6.1)",
459 table.name, column.name
460 ));
461 }
462 row.push(value.map_or(SparseSlot::Null, SparseSlot::Value));
463 }
464 if table.has_encrypted_columns() {
465 encrypt_sparse_row(table, row_id, &mut row, encryption, fallback)?;
466 }
467 Ok(encode_sparse_row(&table.wire_columns, table.pk_index, &row))
468}
469
470pub fn decode_row_bytes(
474 table: &TableSchema,
475 payload: &[u8],
476 encryption: &EncryptionConfig,
477) -> Result<Row, TransportError> {
478 let mut r = Reader::new(payload);
479 let mut row = decode_row(&mut r, &table.wire_columns)
481 .map_err(|e| TransportError::new("sync.invalid_request", e.to_string()))?;
482 if !r.is_empty() {
483 return Err(TransportError::new(
484 "sync.invalid_request",
485 "row payload has trailing bytes",
486 ));
487 }
488 if table.has_encrypted_columns() {
489 decrypt_row(table, &mut row, encryption)?;
490 }
491 Ok(row)
492}
493
494pub fn decrypt_segment_row(
498 table: &TableSchema,
499 row: &mut Row,
500 encryption: &EncryptionConfig,
501) -> Result<(), TransportError> {
502 decrypt_row(table, row, encryption)
503}
504
505#[cfg(feature = "e2ee")]
508fn encrypt_row(
509 table: &TableSchema,
510 _row_id: &str,
511 row: &mut Row,
512 encryption: &EncryptionConfig,
513) -> Result<(), String> {
514 use rand_core::RngCore;
515 use ssp2::crypto::{encrypt_value, NONCE_LENGTH};
516 for enc in &table.encrypted_columns {
517 let Some(value) = row.get(enc.index).and_then(|v| v.as_ref()) else {
518 continue; };
520 let plain = column_value_to_plain(value)?;
521 let key_id = encryption.key_id_for(table, row)?;
522 let key = encryption
523 .keys
524 .get(&key_id)
525 .ok_or_else(|| format!("client.encrypt_failed: no key for keyId {key_id:?}"))?;
526 let mut nonce = [0u8; NONCE_LENGTH];
527 rand_core::OsRng.fill_bytes(&mut nonce);
528 let envelope = encrypt_value(&plain, &key_id, key, nonce)?;
529 row[enc.index] = Some(ColumnValue::Bytes(envelope));
530 }
531 Ok(())
532}
533
534#[cfg(feature = "e2ee")]
535fn encrypt_sparse_row(
536 table: &TableSchema,
537 _row_id: &str,
538 row: &mut SparseRow,
539 encryption: &EncryptionConfig,
540 fallback: Option<&Row>,
541) -> Result<(), String> {
542 use rand_core::RngCore;
543 use ssp2::crypto::{encrypt_value, NONCE_LENGTH};
544 let has_present_encrypted = table
547 .encrypted_columns
548 .iter()
549 .any(|enc| matches!(row.get(enc.index), Some(SparseSlot::Value(_))));
550 if !has_present_encrypted {
551 return Ok(());
552 }
553 let mut dense: Row = row
557 .iter()
558 .map(|slot| match slot {
559 SparseSlot::Value(value) => Some(value.clone()),
560 _ => None,
561 })
562 .collect();
563 if let Some(selector) = encryption.key_id_columns.get(&table.name) {
564 if let Some(index) = table
565 .columns
566 .iter()
567 .position(|column| &column.name == selector)
568 {
569 if matches!(row.get(index), None | Some(SparseSlot::Absent)) {
570 let stored: Option<&str> = match fallback
571 .and_then(|row| row.get(index))
572 .and_then(|value| value.as_ref())
573 {
574 Some(ColumnValue::String(key_id)) => Some(key_id),
575 _ => None,
576 };
577 if let Some(key_id) = stored.filter(|key_id| !key_id.is_empty()) {
578 dense[index] = Some(ColumnValue::String(key_id.to_owned()));
579 }
580 }
581 }
582 }
583 let key_id = encryption.key_id_for(table, &dense)?;
584 for enc in &table.encrypted_columns {
585 let Some(SparseSlot::Value(value)) = row.get(enc.index) else {
586 continue; };
588 let plain = column_value_to_plain(value)?;
589 let key = encryption
590 .keys
591 .get(&key_id)
592 .ok_or_else(|| format!("client.encrypt_failed: no key for keyId {key_id:?}"))?;
593 let mut nonce = [0u8; NONCE_LENGTH];
594 rand_core::OsRng.fill_bytes(&mut nonce);
595 let envelope = encrypt_value(&plain, &key_id, key, nonce)?;
596 row[enc.index] = SparseSlot::Value(ColumnValue::Bytes(envelope));
597 }
598 Ok(())
599}
600
601#[cfg(not(feature = "e2ee"))]
602fn encrypt_sparse_row(
603 table: &TableSchema,
604 _row_id: &str,
605 row: &mut SparseRow,
606 _encryption: &EncryptionConfig,
607 _fallback: Option<&Row>,
608) -> Result<(), String> {
609 if !table
610 .encrypted_columns
611 .iter()
612 .any(|enc| matches!(row.get(enc.index), Some(SparseSlot::Value(_))))
613 {
614 return Ok(());
615 }
616 Err(format!(
617 "table {:?} has encrypted columns but this build lacks the e2ee feature (§5.11)",
618 table.name
619 ))
620}
621
622#[cfg(feature = "e2ee")]
626fn decrypt_row(
627 table: &TableSchema,
628 row: &mut Row,
629 encryption: &EncryptionConfig,
630) -> Result<(), TransportError> {
631 use ssp2::crypto::{decrypt_value, DeclaredType, DecryptValueError};
632 let failed = |message: String| TransportError::new("client.decrypt_failed", message);
633 for enc in &table.encrypted_columns {
634 let Some(value) = row.get(enc.index).and_then(|v| v.as_ref()) else {
635 continue;
636 };
637 let ColumnValue::Bytes(envelope) = value else {
638 return Err(failed(format!(
639 "encrypted column at index {} is not bytes",
640 enc.index
641 )));
642 };
643 let declared = DeclaredType::from_name(&enc.declared_type)
644 .ok_or_else(|| failed(format!("unknown declaredType {:?}", enc.declared_type)))?;
645 let keys = &encryption.keys;
646 let plain =
647 decrypt_value(declared, envelope, |id| keys.get(id).cloned()).map_err(|error| {
648 match error {
649 DecryptValueError::KeyMissing { key_id } => TransportError {
650 code: "client.encryption_key_missing".to_owned(),
651 message: "no key for the envelope keyId".to_owned(),
652 details: Some(serde_json::json!({ "table": table.name, "keyId": key_id })),
653 },
654 DecryptValueError::Failed(error) => failed(error.0),
655 }
656 })?;
657 row[enc.index] = Some(plain_to_column_value(plain));
658 }
659 Ok(())
660}
661
662#[cfg(not(feature = "e2ee"))]
665fn encrypt_row(
666 table: &TableSchema,
667 _row_id: &str,
668 _row: &mut Row,
669 _encryption: &EncryptionConfig,
670) -> Result<(), String> {
671 Err(format!(
672 "table {:?} has encrypted columns but this build lacks the e2ee feature (§5.11)",
673 table.name
674 ))
675}
676
677#[cfg(not(feature = "e2ee"))]
678fn decrypt_row(
679 table: &TableSchema,
680 _row: &mut Row,
681 _encryption: &EncryptionConfig,
682) -> Result<(), TransportError> {
683 Err(TransportError::new(
684 "client.decrypt_failed",
685 format!(
686 "table {:?} has encrypted columns but this build lacks the e2ee feature (§5.11)",
687 table.name
688 ),
689 ))
690}
691
692#[cfg(feature = "e2ee")]
693fn column_value_to_plain(value: &ColumnValue) -> Result<ssp2::crypto::PlainValue, String> {
694 use ssp2::crypto::PlainValue;
695 Ok(match value {
696 ColumnValue::String(s) => PlainValue::String(s.clone()),
697 ColumnValue::Integer(i) => PlainValue::Integer(*i),
698 ColumnValue::Float(f) => PlainValue::Float(*f),
699 ColumnValue::Boolean(b) => PlainValue::Boolean(*b),
700 ColumnValue::Json(j) => PlainValue::Json(j.0.clone()),
701 ColumnValue::BlobRef(j) => PlainValue::BlobRef(j.0.clone()),
702 ColumnValue::Bytes(b) => PlainValue::Bytes(b.clone()),
703 ColumnValue::Crdt(_) => return Err("crdt columns cannot be encrypted (§5.11)".to_owned()),
704 })
705}
706
707#[cfg(feature = "e2ee")]
708fn plain_to_column_value(value: ssp2::crypto::PlainValue) -> ColumnValue {
709 use ssp2::crypto::PlainValue;
710 match value {
711 PlainValue::String(s) => ColumnValue::String(s),
712 PlainValue::Integer(i) => ColumnValue::Integer(i),
713 PlainValue::Float(f) => ColumnValue::Float(f),
714 PlainValue::Boolean(b) => ColumnValue::Boolean(b),
715 PlainValue::Json(s) => ColumnValue::Json(RawJson(s)),
716 PlainValue::BlobRef(s) => ColumnValue::BlobRef(RawJson(s)),
717 PlainValue::Bytes(b) => ColumnValue::Bytes(b),
718 }
719}
720
721pub fn render_row_id(value: &Option<ColumnValue>) -> Result<String, String> {
723 match value {
724 Some(ColumnValue::String(s)) => Ok(s.clone()),
725 Some(ColumnValue::Integer(i)) => Ok(i.to_string()),
726 Some(ColumnValue::Float(f)) => Ok(f.to_string()),
727 Some(ColumnValue::Boolean(b)) => Ok(b.to_string()),
728 Some(ColumnValue::Json(raw)) => Ok(raw.0.clone()),
729 Some(ColumnValue::BlobRef(_)) => Err("blob_ref column cannot be a rowId".to_owned()),
730 Some(ColumnValue::Bytes(_)) => Err("bytes column cannot be a rowId".to_owned()),
731 Some(ColumnValue::Crdt(_)) => Err("crdt column cannot be a rowId".to_owned()),
732 None => Err("primary key value is missing".to_owned()),
733 }
734}
735
736pub fn render_row_id_json(value: Option<&Value>) -> Result<String, String> {
738 match value {
739 Some(Value::String(s)) => Ok(s.clone()),
740 Some(Value::Number(n)) => Ok(n.to_string()),
741 Some(Value::Bool(b)) => Ok(b.to_string()),
742 _ => Err("sync.invalid_request: primary key value is missing or not renderable".to_owned()),
743 }
744}
745
746pub fn scope_map_to_json(scopes: &[(String, Vec<String>)]) -> Value {
748 let mut map = Map::new();
749 for (key, values) in scopes {
750 map.insert(
751 key.clone(),
752 Value::Array(values.iter().map(|v| Value::from(v.clone())).collect()),
753 );
754 }
755 Value::Object(map)
756}
757
758pub fn json_to_scope_map(value: &Value) -> Result<Vec<(String, Vec<String>)>, String> {
760 let object = value
761 .as_object()
762 .ok_or_else(|| "scope map must be an object".to_owned())?;
763 let mut out = Vec::with_capacity(object.len());
764 for (key, values) in object {
765 let list = values
766 .as_array()
767 .ok_or_else(|| format!("scope values for {key:?} must be a list (§0)"))?;
768 let mut strings = Vec::with_capacity(list.len());
769 for v in list {
770 strings.push(
771 v.as_str()
772 .ok_or_else(|| format!("scope value for {key:?} is not a string"))?
773 .to_owned(),
774 );
775 }
776 out.push((key.clone(), strings));
777 }
778 Ok(out)
779}
780
781#[cfg(test)]
782mod naming_tests {
783 use serde_json::{json, Map, Value};
784 use ssp2::segment::{Column, ColumnType, ColumnValue, Row};
785
786 use super::{
787 bytes_to_hex, hex_to_bytes, normalize_values_casing, snake_to_camel, EncryptionConfig,
788 };
789 use crate::schema::{EncryptedColumn, TableSchema};
790
791 #[test]
792 fn authored_integer_cells_respect_the_wire_safe_range() {
793 let column = Column {
794 name: "n".into(),
795 ty: ColumnType::Integer,
796 nullable: false,
797 };
798 for value in [
799 9007199254740992_i64,
800 9007199254740993,
801 i64::MAX,
802 -9007199254740992,
803 i64::MIN,
804 ] {
805 assert!(super::json_to_column_value(&column, Some(&json!(value))).is_err());
806 }
807 for value in [-9007199254740991_i64, 0, 9007199254740991] {
808 assert!(super::json_to_column_value(&column, Some(&json!(value))).is_ok());
809 }
810 }
811
812 #[test]
813 fn byte_envelopes_preserve_canonical_hex_for_every_byte() {
814 let bytes: Vec<u8> = (0..=255).collect();
815 let expected: String = bytes.iter().map(|byte| format!("{byte:02x}")).collect();
816 assert_eq!(bytes_to_hex(&bytes), expected);
817 assert_eq!(hex_to_bytes(&expected).expect("decode"), bytes);
818 assert_eq!(bytes_to_hex(&[]), "");
819 assert_eq!(bytes_to_hex(&[0, 15, 16, 255]), "000f10ff");
820 let large = bytes.repeat(8192);
821 assert_eq!(bytes_to_hex(&large), expected.repeat(8192));
822 assert_eq!(
823 hex_to_bytes(&bytes_to_hex(&large)).expect("large decode"),
824 large
825 );
826 }
827
828 #[test]
829 fn byte_decoder_preserves_radix_parser_inputs_and_errors() {
830 for first in 0_u8..=127 {
831 for second in 0_u8..=127 {
832 let pair = [first, second];
833 let input = std::str::from_utf8(&pair).unwrap();
834 let expected = u8::from_str_radix(input, 16)
835 .map(|byte| vec![byte])
836 .map_err(|error| format!("bad hex: {error}"));
837 assert_eq!(hex_to_bytes(input), expected, "ASCII pair {pair:?}");
838 }
839 }
840 assert_eq!(hex_to_bytes("+fA0+100"), Ok(vec![15, 160, 1, 0]));
841 for input in [
842 "", "0", "abc", "é", "雪", "😀", "0é0", "é00", "00é", "00😀", "😀00", "\0\0",
843 ] {
844 let expected = if !input.len().is_multiple_of(2) {
845 Err("odd-length hex string".to_owned())
846 } else {
847 input
848 .as_bytes()
849 .chunks(2)
850 .map(|pair| {
851 let text =
852 std::str::from_utf8(pair).map_err(|_| "non-ASCII hex".to_owned())?;
853 u8::from_str_radix(text, 16).map_err(|error| format!("bad hex: {error}"))
854 })
855 .collect::<Result<Vec<_>, _>>()
856 };
857 assert_eq!(hex_to_bytes(input), expected, "input {input:?}");
858 }
859 }
860
861 #[test]
862 fn snake_to_camel_pinned_vectors() {
863 for (input, expected) in [
864 ("created_at", "createdAt"),
865 ("col_2", "col2"),
866 ("user_id", "userId"),
867 ("_internal", "_internal"),
868 ("__foo_bar", "__fooBar"),
869 ("row_", "row_"),
870 ("id_url", "idUrl"),
871 ("api_key", "apiKey"),
872 ("title", "title"),
873 ("alreadyCamel", "alreadyCamel"),
874 ("a__b", "aB"),
875 ("_lead_and_trail_", "_leadAndTrail_"),
876 ("count(*)", "count(*)"),
877 ] {
878 assert_eq!(snake_to_camel(input), expected, "input {input:?}");
879 }
880 }
881
882 #[test]
883 fn portable_key_selector_reads_a_non_encrypted_string_column() {
884 let columns = vec![
885 Column {
886 name: "id".to_owned(),
887 ty: ColumnType::String,
888 nullable: false,
889 },
890 Column {
891 name: "encryption_key_id".to_owned(),
892 ty: ColumnType::String,
893 nullable: false,
894 },
895 Column {
896 name: "note".to_owned(),
897 ty: ColumnType::String,
898 nullable: false,
899 },
900 ];
901 let table = TableSchema {
902 name: "patients".to_owned(),
903 columns: columns.clone(),
904 wire_columns: columns,
905 primary_key: "id".to_owned(),
906 pk_index: 0,
907 scope_variables: Vec::new(),
908 indexes: Vec::new(),
909 fts_indexes: Vec::new(),
910 encrypted_columns: vec![EncryptedColumn {
911 index: 2,
912 declared_type: "string".to_owned(),
913 }],
914 };
915 let mut config = EncryptionConfig::default();
916 config
917 .key_id_columns
918 .insert("patients".to_owned(), "encryption_key_id".to_owned());
919 let row: Row = vec![
920 Some(ColumnValue::String("patient-1".to_owned())),
921 Some(ColumnValue::String("practice-key-v1".to_owned())),
922 Some(ColumnValue::String("Identity".to_owned())),
923 ];
924 assert_eq!(
925 config.key_id_for(&table, &row).expect("selects key"),
926 "practice-key-v1"
927 );
928 }
929
930 #[test]
931 fn debug_render_redacts_key_material() {
932 let mut config = EncryptionConfig::default();
933 config
934 .keys
935 .insert("practice-key-v1".to_owned(), vec![0xAB; 32]);
936 config
937 .key_id_columns
938 .insert("patients".to_owned(), "encryption_key_id".to_owned());
939 let rendered = format!("{config:?}");
940 assert!(rendered.contains("practice-key-v1"), "{rendered}");
941 assert!(rendered.contains("<32 bytes>"), "{rendered}");
942 assert!(rendered.contains("encryption_key_id"), "{rendered}");
943 assert!(!rendered.contains("171"), "{rendered}");
945 assert!(!rendered.contains("0xAB"), "{rendered}");
946 }
947
948 fn table(names: &[&str]) -> TableSchema {
949 let columns: Vec<Column> = names
950 .iter()
951 .map(|n| Column {
952 name: (*n).to_owned(),
953 ty: ColumnType::String,
954 nullable: true,
955 })
956 .collect();
957 TableSchema {
958 name: "t".to_owned(),
959 columns: columns.clone(),
960 wire_columns: columns,
961 primary_key: "id".to_owned(),
962 pk_index: 0,
963 scope_variables: Vec::new(),
964 indexes: Vec::new(),
965 fts_indexes: Vec::new(),
966 encrypted_columns: Vec::new(),
967 }
968 }
969
970 fn map(entries: &[(&str, &str)]) -> Map<String, Value> {
971 entries
972 .iter()
973 .map(|(k, v)| ((*k).to_owned(), json!(v)))
974 .collect()
975 }
976
977 #[test]
978 fn camel_keys_normalize_to_sql_names() {
979 let t = table(&["id", "list_id", "updated_at_ms"]);
980 let out = normalize_values_casing(
981 &t,
982 map(&[("id", "x"), ("listId", "l"), ("updatedAtMs", "9")]),
983 )
984 .expect("normalizes");
985 assert_eq!(out.get("list_id"), Some(&json!("l")));
986 assert_eq!(out.get("updated_at_ms"), Some(&json!("9")));
987 assert!(!out.contains_key("listId"));
988 }
989
990 #[test]
991 fn snake_keys_pass_through() {
992 let t = table(&["id", "list_id"]);
993 let out = normalize_values_casing(&t, map(&[("id", "x"), ("list_id", "l")])).expect("ok");
994 assert_eq!(out.get("list_id"), Some(&json!("l")));
995 }
996
997 #[test]
998 fn both_casings_for_one_column_is_an_error() {
999 let t = table(&["id", "list_id"]);
1000 let err = normalize_values_casing(&t, map(&[("list_id", "a"), ("listId", "b")]))
1001 .expect_err("rejects");
1002 assert!(err.contains("both snake_case and camelCase"), "{err}");
1003 }
1004
1005 #[test]
1006 fn an_alias_colliding_with_a_real_column_never_steals_it() {
1007 let t = table(&["id", "col_2", "col2"]);
1009 let out = normalize_values_casing(&t, map(&[("id", "x"), ("col2", "v")])).expect("ok");
1010 assert_eq!(out.get("col2"), Some(&json!("v")));
1011 assert!(!out.contains_key("col_2"));
1012 }
1013
1014 #[test]
1015 fn unknown_and_internal_columns_fail_loud() {
1016 let t = table(&["id", "title"]);
1017 let unknown = normalize_values_casing(&t, map(&[("id", "x"), ("typo", "v")]))
1018 .expect_err("unknown field");
1019 assert!(unknown.contains("unknown column"), "{unknown}");
1020 let internal = normalize_values_casing(&t, map(&[("id", "x"), ("_sync_version", "1")]))
1021 .expect_err("internal field");
1022 assert!(internal.contains("internal sync column"), "{internal}");
1023 }
1024}
1025
1026#[cfg(test)]
1027mod sparse_key_tests {
1028 use ssp2::segment::{Column, ColumnType, ColumnValue, Row};
1029
1030 use super::EncryptionConfig;
1031 use crate::schema::{EncryptedColumn, TableSchema};
1032
1033 #[cfg(feature = "e2ee")]
1034 use super::encode_sparse_row_json;
1035 #[cfg(feature = "e2ee")]
1036 use serde_json::{json, Map, Value};
1037
1038 fn keyed_table() -> TableSchema {
1039 let columns = vec![
1040 Column {
1041 name: "id".to_owned(),
1042 ty: ColumnType::String,
1043 nullable: false,
1044 },
1045 Column {
1046 name: "encryption_key_id".to_owned(),
1047 ty: ColumnType::String,
1048 nullable: true,
1049 },
1050 Column {
1051 name: "note".to_owned(),
1052 ty: ColumnType::String,
1053 nullable: true,
1054 },
1055 ];
1056 let wire_columns = vec![
1057 Column {
1058 name: "id".to_owned(),
1059 ty: ColumnType::String,
1060 nullable: false,
1061 },
1062 Column {
1063 name: "encryption_key_id".to_owned(),
1064 ty: ColumnType::String,
1065 nullable: true,
1066 },
1067 Column {
1068 name: "note".to_owned(),
1069 ty: ColumnType::Bytes,
1070 nullable: true,
1071 },
1072 ];
1073 TableSchema {
1074 name: "t".to_owned(),
1075 columns,
1076 wire_columns,
1077 primary_key: "id".to_owned(),
1078 pk_index: 0,
1079 scope_variables: Vec::new(),
1080 indexes: Vec::new(),
1081 fts_indexes: Vec::new(),
1082 encrypted_columns: vec![EncryptedColumn {
1083 index: 2,
1084 declared_type: "string".to_owned(),
1085 }],
1086 }
1087 }
1088
1089 fn keyed_config() -> EncryptionConfig {
1090 let mut config = EncryptionConfig::default();
1091 config.keys.insert("k1".to_owned(), vec![0x2A; 32]);
1092 config
1093 .key_id_columns
1094 .insert("t".to_owned(), "encryption_key_id".to_owned());
1095 config
1096 }
1097
1098 #[cfg(feature = "e2ee")]
1099 fn values(entries: &[(&str, Value)]) -> Map<String, Value> {
1100 entries
1101 .iter()
1102 .map(|(key, value)| ((*key).to_owned(), value.clone()))
1103 .collect()
1104 }
1105
1106 #[test]
1107 fn key_selection_failures_carry_the_encode_seam_code() {
1108 let table = keyed_table();
1109 let config = keyed_config();
1110 let row: Row = vec![
1112 Some(ColumnValue::String("r1".to_owned())),
1113 None,
1114 Some(ColumnValue::String("hi".to_owned())),
1115 ];
1116 let error = config.key_id_for(&table, &row).expect_err("rejects");
1117 assert!(error.starts_with("client.encrypt_failed"), "{error}");
1118 assert!(!error.starts_with("client.decrypt_failed"), "{error}");
1119 }
1120
1121 #[cfg(feature = "e2ee")]
1122 #[test]
1123 fn sparse_patch_without_an_encrypted_slot_needs_no_key() {
1124 let table = keyed_table();
1125 let config = EncryptionConfig::default();
1127 let payload =
1128 encode_sparse_row_json(&table, "r1", &values(&[("id", json!("r1"))]), &config, None)
1129 .expect("encodes without key resolution");
1130 assert!(!payload.is_empty());
1131 }
1132
1133 #[cfg(feature = "e2ee")]
1134 #[test]
1135 fn sparse_patch_falls_back_to_the_stored_key_id() {
1136 let table = keyed_table();
1137 let config = keyed_config();
1138 let sparse = values(&[("id", json!("r1")), ("note", json!("hi"))]);
1140 let error = encode_sparse_row_json(&table, "r1", &sparse, &config, None)
1141 .expect_err("absent selector with no fallback fails");
1142 assert!(error.contains("client.encrypt_failed"), "{error}");
1143 let fallback: Row = vec![
1144 Some(ColumnValue::String("r1".to_owned())),
1145 Some(ColumnValue::String("k1".to_owned())),
1146 None,
1147 ];
1148 encode_sparse_row_json(&table, "r1", &sparse, &config, Some(&fallback))
1149 .expect("stored key id encrypts the patch");
1150 }
1151
1152 #[cfg(feature = "e2ee")]
1153 #[test]
1154 fn sparse_patch_keeps_a_present_null_selector() {
1155 let table = keyed_table();
1156 let config = keyed_config();
1157 let sparse = values(&[
1161 ("id", json!("r1")),
1162 ("encryption_key_id", json!(null)),
1163 ("note", json!("hi")),
1164 ]);
1165 let fallback: Row = vec![
1166 Some(ColumnValue::String("r1".to_owned())),
1167 Some(ColumnValue::String("k1".to_owned())),
1168 None,
1169 ];
1170 let error = encode_sparse_row_json(&table, "r1", &sparse, &config, Some(&fallback))
1171 .expect_err("a present NULL selector must not read the stored fallback");
1172 assert!(error.contains("client.encrypt_failed"), "{error}");
1173 }
1174}