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