1use crate::{
2 canonical_json, digest, ApplyKind, Bound, Digest, Layer, RecordedValue, Refusal, RefusalCode,
3 PURPOSE_SCHEMA, PURPOSE_VALUE,
4};
5use regex::Regex;
6use serde::{Deserialize, Serialize};
7use serde_json::{Map, Value};
8use std::collections::{BTreeMap, BTreeSet};
9
10pub const VOCABULARY_META_SCHEMA: &str = include_str!("../vocabulary.schema.json");
11
12#[derive(Clone, Debug)]
15#[non_exhaustive]
16pub struct Schema {
17 normalized: Value,
18}
19
20#[derive(Clone, Debug, Default)]
21#[non_exhaustive]
22pub struct ValidationContext {
23 pub module_id: String,
24 pub project_id: Option<String>,
25 pub closed_options: BTreeMap<String, Vec<Value>>,
27 pub project_credentials: BTreeSet<Digest>,
29 pub credential_destinations: BTreeMap<String, Vec<String>>,
31}
32impl ValidationContext {
33 pub fn new(module_id: impl Into<String>, project_id: Option<String>) -> Self {
34 Self {
35 module_id: module_id.into(),
36 project_id,
37 ..Self::default()
38 }
39 }
40}
41
42#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
44#[serde(deny_unknown_fields)]
45#[non_exhaustive]
46pub struct LayerValue {
47 pub layer: Layer,
48 pub value: Value,
49}
50impl LayerValue {
51 pub fn new(layer: Layer, value: Value) -> Self {
52 Self { layer, value }
53 }
54}
55
56#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
57#[non_exhaustive]
58pub struct ReviewedTuple {
59 pub effective: Value,
60 pub sources: BTreeMap<String, Layer>,
61}
62impl ReviewedTuple {
63 pub fn new(effective: Value, sources: BTreeMap<String, Layer>) -> Self {
64 Self { effective, sources }
65 }
66}
67
68impl Schema {
69 pub fn new(schema: Value) -> Result<Self, Refusal> {
70 if schema.get("$schema").and_then(Value::as_str)
71 != Some("https://json-schema.org/draft/2020-12/schema")
72 {
73 return Err(schema_error(""));
74 }
75 let normalized = normalize(&schema, &schema, None, &mut Vec::new(), "")?;
76 if normalized.get("type").and_then(Value::as_str) != Some("object") {
77 return Err(schema_error(""));
78 }
79 let result = Self { normalized };
80 validate_defaults(&result.normalized, "")?;
83 Ok(result)
84 }
85 pub fn normalized(&self) -> &Value {
86 &self.normalized
87 }
88 pub fn digest(&self) -> Result<Digest, crate::CanonicalError> {
89 digest(PURPOSE_SCHEMA, &self.normalized)
90 }
91 pub fn defaults(&self) -> Value {
92 defaults(&self.normalized).unwrap_or_else(|| Value::Object(Map::new()))
93 }
94 pub fn validate_layer(
95 &self,
96 layer: Layer,
97 value: &Value,
98 context: &ValidationContext,
99 ) -> Result<(), Refusal> {
100 if layer == Layer::Default {
101 return Err(refusal(RefusalCode::LayerForbidden, "", context));
102 }
103 validate(&self.normalized, value, "", Some(layer), true, context)
104 }
105 pub fn review_tuple(
108 &self,
109 layers: &[LayerValue],
110 context: &ValidationContext,
111 ) -> Result<ReviewedTuple, Refusal> {
112 let mut effective = self.defaults();
113 let mut sources = BTreeMap::new();
114 mark_sources(&effective, "", Layer::Default, &mut sources);
115 let mut previous = Layer::Default;
116 let mut user_credentials = BTreeSet::new();
117 for proposal in layers {
118 if proposal.layer <= previous
119 || (proposal.layer == Layer::Project && context.project_id.is_none())
120 {
121 return Err(refusal(RefusalCode::LayerForbidden, "", context));
122 }
123 previous = proposal.layer;
124 validate(
125 &overlay_schema(&self.normalized, &effective, &proposal.value),
126 &proposal.value,
127 "",
128 Some(proposal.layer),
129 true,
130 context,
131 )?;
132 if proposal.layer == Layer::User {
133 collect_credentials(&self.normalized, &proposal.value, &mut user_credentials);
134 }
135 if matches!(proposal.layer, Layer::Project | Layer::Workspace) {
136 check_credentials(
137 &overlay_schema(&self.normalized, &effective, &proposal.value),
138 &proposal.value,
139 "",
140 &user_credentials,
141 context,
142 )?;
143 for (credential_path, destinations) in &context.credential_destinations {
144 if effective
145 .pointer(credential_path)
146 .is_some_and(is_vault_reference)
147 && sources.get(credential_path) == Some(&Layer::User)
148 && proposal.value.pointer(credential_path).is_none()
149 && destinations
150 .iter()
151 .any(|p| proposal.value.pointer(p).is_some())
152 {
153 return Err(refusal(
154 RefusalCode::CredentialShared,
155 credential_path,
156 context,
157 ));
158 }
159 }
160 }
161 merge(
162 &self.normalized,
163 &mut effective,
164 &proposal.value,
165 "",
166 proposal.layer,
167 &mut sources,
168 context,
169 )?;
170 }
171 validate(&self.normalized, &effective, "", None, false, context)?;
172 Ok(ReviewedTuple::new(effective, sources))
173 }
174 pub fn record(&self, value: &Value) -> Result<Value, crate::CanonicalError> {
177 record(&self.normalized, value)
178 }
179}
180
181fn schema_error(path: &str) -> Refusal {
182 Refusal::new(RefusalCode::SchemaInvalid, path, None)
183}
184fn refusal(code: RefusalCode, path: &str, c: &ValidationContext) -> Refusal {
185 Refusal::new(code, path, c.project_id.clone())
186}
187fn pointer(path: &str, key: &str) -> String {
188 format!("{path}/{}", key.replace('~', "~0").replace('/', "~1"))
189}
190fn credential(s: &Value) -> bool {
191 s.get("x-ck-kind").and_then(Value::as_str) == Some("credential_ref")
192}
193
194pub fn is_vault_reference(value: &Value) -> bool {
196 value
197 .as_object()
198 .filter(|o| o.len() == 1)
199 .and_then(|o| o.get("vault_ref"))
200 .and_then(Value::as_str)
201 .is_some_and(|s| {
202 !s.is_empty() && s.len() <= 256 && s.bytes().all(|b| (0x21..=0x7e).contains(&b))
203 })
204}
205
206fn normalize(
207 root: &Value,
208 input: &Value,
209 parent: Option<&Value>,
210 stack: &mut Vec<String>,
211 path: &str,
212) -> Result<Value, Refusal> {
213 let mut node = input
214 .as_object()
215 .cloned()
216 .ok_or_else(|| schema_error(path))?;
217 if let Some(reference) = node.remove("$ref") {
218 let reference = reference.as_str().ok_or_else(|| schema_error(path))?;
219 if !reference.starts_with("#/") || stack.iter().any(|s| s == reference) {
220 return Err(schema_error(path));
221 }
222 let target = root
223 .pointer(&reference[1..])
224 .ok_or_else(|| schema_error(path))?;
225 stack.push(reference.to_owned());
226 let resolved = normalize(root, target, parent, stack, path)?;
227 let mut expanded = resolved
228 .as_object()
229 .cloned()
230 .ok_or_else(|| schema_error(path))?;
231 for (key, value) in node {
232 if key == "x-ck-layers" {
233 if let Some(allowed) = expanded.get(&key).and_then(Value::as_array) {
234 if value
235 .as_array()
236 .is_none_or(|a| a.iter().any(|v| !allowed.contains(v)))
237 {
238 return Err(schema_error(path));
239 }
240 }
241 }
242 if key == "x-ck-apply" {
243 if let Some(existing) = expanded.get(&key) {
244 if existing != &value && existing != "live" && value != "daemon_restart" {
245 return Err(schema_error(path));
246 }
247 }
248 }
249 if expanded.get(&key).is_some_and(|v| v != &value)
250 && !key.starts_with("x-ck-")
251 && !["description", "default", "$schema"].contains(&key.as_str())
252 {
253 return Err(schema_error(path));
254 }
255 expanded.insert(key, value);
256 }
257 let result = normalize(root, &Value::Object(expanded), parent, stack, path);
258 stack.pop();
259 return result;
260 }
261 if ["allOf", "anyOf", "if", "then", "else", "$dynamicRef"]
262 .iter()
263 .any(|k| node.contains_key(*k))
264 {
265 return Err(schema_error(path));
267 }
268 if let Some(parent) = parent {
269 for key in ["x-ck-layers", "x-ck-apply"] {
270 if !node.contains_key(key) {
271 if let Some(v) = parent.get(key) {
272 node.insert(key.to_owned(), v.clone());
273 }
274 }
275 }
276 if let (Some(p), Some(c)) = (
277 parent.get("x-ck-layers").and_then(Value::as_array),
278 node.get("x-ck-layers").and_then(Value::as_array),
279 ) {
280 if c.iter().any(|v| !p.contains(v)) {
281 return Err(schema_error(path));
282 }
283 }
284 if let (Some(p), Some(c)) = (parent.get("x-ck-apply"), node.get("x-ck-apply")) {
285 if p != c && p != "live" && c != "daemon_restart" {
286 return Err(schema_error(path));
287 }
288 }
289 }
290 let policy = Value::Object(node.clone());
291 validate_vocabulary(&policy, path)?;
292 if let Some(layers) = node.get("x-ck-layers") {
293 let values = layers.as_array().ok_or_else(|| schema_error(path))?;
294 if values.is_empty()
295 || values.iter().any(|v| {
296 ![
297 Value::from("user"),
298 Value::from("workspace"),
299 Value::from("project"),
300 ]
301 .contains(v)
302 })
303 || values
304 .iter()
305 .enumerate()
306 .any(|(i, v)| values[..i].contains(v))
307 {
308 return Err(schema_error(path));
309 }
310 }
311 if let Some(apply) = node.get("x-ck-apply") {
312 serde_json::from_value::<ApplyKind>(apply.clone()).map_err(|_| schema_error(path))?;
313 if ["module_restart", "daemon_restart"].contains(&apply.as_str().unwrap_or_default())
314 && node.get("x-ck-layers") != Some(&serde_json::json!(["user"]))
315 {
316 return Err(schema_error(path));
317 }
318 }
319 if let Some(branches) = node.get_mut("oneOf") {
320 let branches = branches.as_array_mut().ok_or_else(|| schema_error(path))?;
321 if branches.len() < 2 {
322 return Err(schema_error(path));
323 }
324 for branch in branches.iter_mut() {
325 *branch = normalize(root, branch, Some(&policy), stack, path)?;
326 }
327 discriminator(branches).ok_or_else(|| schema_error(path))?;
328 return Ok(Value::Object(node));
329 }
330 let kind = node
331 .get("type")
332 .and_then(Value::as_str)
333 .ok_or_else(|| schema_error(path))?
334 .to_owned();
335 if !["object", "array", "string", "number", "integer", "boolean"].contains(&kind.as_str()) {
336 return Err(schema_error(path));
337 }
338 let structural = kind == "object" && !credential(&policy);
339 if !structural
340 && (node
341 .get("description")
342 .and_then(Value::as_str)
343 .is_none_or(str::is_empty)
344 || (!node.contains_key("default")
345 && node.get("x-ck-required") != Some(&Value::Bool(true)))
346 || !node.contains_key("x-ck-apply")
347 || !node.contains_key("x-ck-layers"))
348 {
349 return Err(schema_error(path));
350 }
351 if let Some(bound) = node.get("x-ck-bound") {
352 let bound: Bound = serde_json::from_value(bound.clone()).map_err(|_| schema_error(path))?;
353 if bound == Bound::SubsetOnly && kind != "array" {
354 return Err(schema_error(path));
355 }
356 if bound != Bound::SubsetOnly && !["number", "integer"].contains(&kind.as_str()) {
357 let order = node
358 .get("x-ck-ordering")
359 .and_then(Value::as_array)
360 .ok_or_else(|| schema_error(path))?;
361 let values = if kind == "boolean" {
362 serde_json::json!([false, true])
363 } else {
364 node.get("enum")
365 .cloned()
366 .ok_or_else(|| schema_error(path))?
367 };
368 let values = values.as_array().ok_or_else(|| schema_error(path))?;
369 if (kind == "boolean" && order != values)
370 || order.len() != values.len()
371 || order.iter().any(|v| !values.contains(v))
372 || order
373 .iter()
374 .enumerate()
375 .any(|(i, v)| order[..i].contains(v))
376 {
377 return Err(schema_error(path));
378 }
379 }
380 }
381 if structural {
382 if node.get("unevaluatedProperties") != Some(&Value::Bool(false)) {
383 return Err(schema_error(path));
384 }
385 if let Some(properties) = node.get_mut("properties") {
386 for (key, child) in properties
387 .as_object_mut()
388 .ok_or_else(|| schema_error(path))?
389 {
390 *child = normalize(root, child, Some(&policy), stack, &pointer(path, key))?;
391 }
392 }
393 if let Some(patterns) = node.get_mut("patternProperties") {
394 let patterns = patterns.as_object_mut().ok_or_else(|| schema_error(path))?;
395 if patterns.len() != 1 || node_has_open_map(&policy) {
396 return Err(schema_error(path));
397 }
398 for (pattern, child) in patterns {
399 Regex::new(pattern).map_err(|_| schema_error(path))?;
400 *child = normalize(root, child, Some(&policy), stack, &pointer(path, "*"))?;
401 }
402 }
403 if node
404 .get("additionalProperties")
405 .is_some_and(|v| v != &Value::Bool(false))
406 {
407 return Err(schema_error(path));
408 }
409 }
410 if kind == "array" {
411 let child = node.get_mut("items").ok_or_else(|| schema_error(path))?;
412 *child = normalize(root, child, Some(&policy), stack, &pointer(path, "*"))?;
413 }
414 if let Some(pattern) = node.get("pattern") {
415 Regex::new(pattern.as_str().ok_or_else(|| schema_error(path))?)
416 .map_err(|_| schema_error(path))?;
417 }
418 node.remove("$defs");
419 Ok(Value::Object(node))
420}
421fn node_has_open_map(node: &Value) -> bool {
422 node.get("additionalProperties")
423 .is_some_and(|v| v != &Value::Bool(false))
424}
425
426fn discriminator(branches: &[Value]) -> Option<String> {
427 let first = branches.first()?.get("properties")?.as_object()?;
428 let candidates: Vec<_> = first
429 .iter()
430 .filter(|(key, v)| {
431 v.get("const").is_some()
432 && branches.iter().all(|b| {
433 b.get("required")
434 .and_then(Value::as_array)
435 .is_some_and(|r| r.contains(&Value::String((*key).clone())))
436 })
437 })
438 .filter(|(key, _)| {
439 let constants: Vec<_> = branches
440 .iter()
441 .filter_map(|b| b.get("properties")?.get(*key)?.get("const"))
442 .collect();
443 constants.len() == branches.len()
444 && constants
445 .iter()
446 .enumerate()
447 .all(|(i, v)| !constants[..i].contains(v))
448 })
449 .collect();
450 if candidates.len() == 1 {
451 Some(candidates[0].0.clone())
452 } else {
453 None
454 }
455}
456fn variant<'a>(s: &'a Value, value: &Value) -> Option<&'a Value> {
457 let branches = s.get("oneOf")?.as_array()?;
458 let key = discriminator(branches)?;
459 branches.iter().find(|b| {
460 value
461 .get(&key)
462 .is_some_and(|v| b["properties"][&key]["const"] == *v)
463 })
464}
465fn child_schema<'a>(s: &'a Value, key: &str) -> Option<&'a Value> {
466 s.get("properties").and_then(|p| p.get(key)).or_else(|| {
467 s.get("patternProperties")?
468 .as_object()?
469 .iter()
470 .find(|(pattern, _)| Regex::new(pattern).is_ok_and(|r| r.is_match(key)))
471 .map(|(_, v)| v)
472 })
473}
474fn defaults(s: &Value) -> Option<Value> {
475 if let Some(v) = s.get("default") {
476 return Some(v.clone());
477 }
478 let properties = s.get("properties")?.as_object()?;
479 Some(Value::Object(
480 properties
481 .iter()
482 .filter_map(|(key, schema)| defaults(schema).map(|v| (key.clone(), v)))
483 .collect(),
484 ))
485}
486fn validate_defaults(s: &Value, path: &str) -> Result<(), Refusal> {
487 if let Some(value) = s.get("default") {
488 let mut declaration_schema = s.clone();
489 strip_options(&mut declaration_schema);
490 validate(
491 &declaration_schema,
492 value,
493 path,
494 None,
495 false,
496 &ValidationContext::default(),
497 )
498 .map_err(|_| schema_error(path))?;
499 }
500 if let Some(props) = s.get("properties").and_then(Value::as_object) {
501 for (key, child) in props {
502 validate_defaults(child, &pointer(path, key))?;
503 }
504 }
505 if let Some(patterns) = s.get("patternProperties").and_then(Value::as_object) {
506 for child in patterns.values() {
507 validate_defaults(child, path)?;
508 }
509 }
510 if let Some(items) = s.get("items") {
511 validate_defaults(items, path)?;
512 }
513 if let Some(branches) = s.get("oneOf").and_then(Value::as_array) {
514 for branch in branches {
515 validate_defaults(branch, path)?;
516 }
517 }
518 Ok(())
519}
520
521fn validate(
522 s: &Value,
523 value: &Value,
524 path: &str,
525 layer: Option<Layer>,
526 partial: bool,
527 c: &ValidationContext,
528) -> Result<(), Refusal> {
529 let invalid = || refusal(RefusalCode::ValueInvalid, path, c);
530 if value.is_null() || canonical_json(value).is_err() || contains_expansion(value) {
531 return Err(invalid());
532 }
533 if let Some(layer) = layer {
534 if let Some(layers) = s.get("x-ck-layers").and_then(Value::as_array) {
535 if !layers.contains(&serde_json::to_value(layer).expect("layer enum")) {
536 return Err(refusal(RefusalCode::LayerForbidden, path, c));
537 }
538 }
539 }
540 if credential(s) {
541 return if is_vault_reference(value) {
542 Ok(())
543 } else {
544 Err(invalid())
545 };
546 }
547 if s.get("oneOf").is_some() {
548 if let Some(branch) = variant(s, value) {
549 return validate(branch, value, path, layer, partial, c);
550 }
551 if partial {
552 let branches = s["oneOf"].as_array().expect("normalized union");
553 let key = discriminator(branches).expect("normalized discriminator");
554 if value.get(&key).is_none() {
555 for branch in branches {
559 validate(branch, value, path, layer, true, c)?;
560 }
561 return Ok(());
562 }
563 }
564 return Err(invalid());
565 }
566 let matches = match s["type"].as_str() {
567 Some("object") => value.is_object(),
568 Some("array") => value.is_array(),
569 Some("string") => value.is_string(),
570 Some("boolean") => value.is_boolean(),
571 Some("number") => value.is_number(),
572 Some("integer") => value.as_f64().is_some_and(|n| n.fract() == 0.0),
573 _ => false,
574 };
575 if !matches {
576 return Err(invalid());
577 }
578 if s.get("const").is_some_and(|v| !same_scalar(v, value))
579 || s.get("enum")
580 .and_then(Value::as_array)
581 .is_some_and(|a| !a.iter().any(|v| same_scalar(v, value)))
582 {
583 return Err(invalid());
584 }
585 if let Some(options) = s.get("x-ck-options") {
586 if options.get("closed") == Some(&Value::Bool(true)) {
587 let set = c.closed_options.get(path);
590 if set.is_none_or(|values| !values.iter().any(|v| same_scalar(v, value))) {
591 return Err(invalid());
592 }
593 }
594 }
595 if let Some(text) = value.as_str() {
596 if text.contains("{env:") || text.contains("{file:") {
597 return Err(invalid());
598 }
599 let len = text.chars().count() as u64;
600 if s.get("minLength")
601 .and_then(Value::as_u64)
602 .is_some_and(|n| len < n)
603 || s.get("maxLength")
604 .and_then(Value::as_u64)
605 .is_some_and(|n| len > n)
606 {
607 return Err(invalid());
608 }
609 if s.get("pattern")
610 .and_then(Value::as_str)
611 .is_some_and(|p| !Regex::new(p).expect("normalized pattern").is_match(text))
612 {
613 return Err(invalid());
614 }
615 }
616 if let Some(n) = value.as_f64() {
617 for (key, fails) in [
618 ("minimum", false),
619 ("maximum", false),
620 ("exclusiveMinimum", true),
621 ("exclusiveMaximum", true),
622 ] {
623 if let Some(bound) = s.get(key).and_then(Value::as_f64) {
624 let is_min = key.ends_with("Minimum") || key == "minimum";
625 if if is_min {
626 n < bound || (fails && n == bound)
627 } else {
628 n > bound || (fails && n == bound)
629 } {
630 return Err(invalid());
631 }
632 }
633 }
634 }
635 if let Some(object) = value.as_object() {
636 if object.keys().any(|k| k == "env" || k == "file") && object.len() == 1 {
637 return Err(invalid());
638 }
639 for (key, child) in object {
640 let child_path = pointer(path, key);
641 let schema = child_schema(s, key)
642 .ok_or_else(|| refusal(RefusalCode::ValueInvalid, &child_path, c))?;
643 validate(schema, child, &child_path, layer, partial, c)?;
644 }
645 if !partial {
646 if let Some(required) = s.get("required").and_then(Value::as_array) {
647 for key in required {
648 let key = key.as_str().ok_or_else(invalid)?;
649 if !object.contains_key(key) {
650 return Err(refusal(RefusalCode::ValueInvalid, &pointer(path, key), c));
651 }
652 }
653 }
654 if let Some(props) = s.get("properties").and_then(Value::as_object) {
655 for (key, schema) in props {
656 if schema.get("x-ck-required") == Some(&Value::Bool(true))
657 && !object.contains_key(key)
658 {
659 return Err(refusal(RefusalCode::ValueInvalid, &pointer(path, key), c));
660 }
661 }
662 }
663 }
664 }
665 if let Some(array) = value.as_array() {
666 if s.get("minItems")
667 .and_then(Value::as_u64)
668 .is_some_and(|n| (array.len() as u64) < n)
669 || s.get("maxItems")
670 .and_then(Value::as_u64)
671 .is_some_and(|n| array.len() as u64 > n)
672 {
673 return Err(invalid());
674 }
675 if s.get("uniqueItems") == Some(&Value::Bool(true))
676 && array
677 .iter()
678 .enumerate()
679 .any(|(i, v)| array[..i].iter().any(|x| same_scalar(x, v)))
680 {
681 return Err(invalid());
682 }
683 let ids_declared = s["items"]["properties"].get("id").is_some();
684 let mut ids = Vec::new();
685 for (i, child) in array.iter().enumerate() {
686 if ids_declared {
687 if let Some(id) = child.get("id") {
688 if ids.contains(id) {
689 return Err(invalid());
690 }
691 ids.push(id.clone());
692 }
693 }
694 validate(
695 &s["items"],
696 child,
697 &pointer(path, &i.to_string()),
698 layer,
699 false,
700 c,
701 )?;
702 }
703 }
704 Ok(())
705}
706fn same_scalar(a: &Value, b: &Value) -> bool {
707 match (canonical_json(a), canonical_json(b)) {
708 (Ok(a), Ok(b)) => a == b,
709 _ => false,
710 }
711}
712fn bound_ok(s: &Value, lower: &Value, higher: &Value) -> bool {
713 let Some(bound) = s.get("x-ck-bound") else {
714 return true;
715 };
716 if bound == "subset_only" {
717 let (Some(a), Some(b)) = (lower.as_array(), higher.as_array()) else {
718 return false;
719 };
720 return a
721 .iter()
722 .chain(b)
723 .all(|v| !v.is_object() && !v.is_array() && !v.is_null())
724 && b.iter().all(|v| a.iter().any(|x| same_scalar(x, v)));
725 }
726 let comparison = if let Some(order) = s.get("x-ck-ordering").and_then(Value::as_array) {
727 order
728 .iter()
729 .position(|v| same_scalar(v, higher))
730 .zip(order.iter().position(|v| same_scalar(v, lower)))
731 .map(|(h, l)| h.cmp(&l))
732 } else {
733 higher
734 .as_f64()
735 .zip(lower.as_f64())
736 .and_then(|(h, l)| h.partial_cmp(&l))
737 };
738 comparison.is_some_and(|cmp| {
739 if bound == "raise_only" {
740 !cmp.is_lt()
741 } else {
742 !cmp.is_gt()
743 }
744 })
745}
746fn merge(
747 s: &Value,
748 lower: &mut Value,
749 higher: &Value,
750 path: &str,
751 layer: Layer,
752 sources: &mut BTreeMap<String, Layer>,
753 c: &ValidationContext,
754) -> Result<(), Refusal> {
755 if !bound_ok(s, lower, higher) {
756 return Err(refusal(RefusalCode::BoundViolated, path, c));
757 }
758 if credential(s) {
759 *lower = higher.clone();
760 sources.insert(path.into(), layer);
761 return Ok(());
762 }
763 let schema = if let Some(branches) = s.get("oneOf").and_then(Value::as_array) {
764 let key = discriminator(branches).expect("normalized union");
765 if lower.get(&key).is_some()
766 && higher.get(&key).is_some()
767 && lower.get(&key) != higher.get(&key)
768 {
769 return Err(refusal(RefusalCode::ValueInvalid, &pointer(path, &key), c));
770 }
771 variant(s, higher)
772 .or_else(|| variant(s, lower))
773 .ok_or_else(|| refusal(RefusalCode::ValueInvalid, path, c))?
774 } else {
775 s
776 };
777 if let Some(object) = higher.as_object() {
778 if !lower.is_object() {
779 return Err(refusal(RefusalCode::ValueInvalid, path, c));
780 }
781 let target = lower.as_object_mut().expect("object");
782 for (key, value) in object {
783 let child = child_schema(schema, key)
784 .ok_or_else(|| refusal(RefusalCode::ValueInvalid, &pointer(path, key), c))?;
785 let child_path = pointer(path, key);
786 if let Some(existing) = target.get_mut(key) {
787 merge(child, existing, value, &child_path, layer, sources, c)?;
788 } else {
789 let mut initial = defaults(child).unwrap_or_else(|| {
790 if value.is_object() {
791 serde_json::json!({})
792 } else {
793 value.clone()
794 }
795 });
796 merge(child, &mut initial, value, &child_path, layer, sources, c)?;
797 target.insert(key.clone(), initial);
798 }
799 }
800 } else {
801 if lower.is_object() || lower.is_array() != higher.is_array() {
802 return Err(refusal(RefusalCode::ValueInvalid, path, c));
803 }
804 *lower = higher.clone();
805 sources.retain(|p, _| p != path && !p.starts_with(&format!("{path}/")));
806 mark_sources(higher, path, layer, sources);
807 }
808 Ok(())
809}
810fn mark_sources(v: &Value, path: &str, layer: Layer, sources: &mut BTreeMap<String, Layer>) {
811 if let Some(object) = v.as_object() {
812 if is_vault_reference(v) {
813 sources.insert(path.into(), layer);
814 } else {
815 for (key, value) in object {
816 mark_sources(value, &pointer(path, key), layer, sources);
817 }
818 }
819 } else {
820 sources.insert(path.into(), layer);
821 }
822}
823fn collect_credentials(s: &Value, value: &Value, out: &mut BTreeSet<Digest>) {
824 let s = variant(s, value).unwrap_or(s);
825 if credential(s) {
826 if let Ok(d) = digest(PURPOSE_VALUE, value) {
827 out.insert(d);
828 }
829 } else if let Some(o) = value.as_object() {
830 for (key, v) in o {
831 if let Some(child) = child_schema(s, key) {
832 collect_credentials(child, v, out);
833 }
834 }
835 } else if let Some(a) = value.as_array() {
836 for v in a {
837 collect_credentials(&s["items"], v, out);
838 }
839 }
840}
841fn check_credentials(
842 s: &Value,
843 value: &Value,
844 path: &str,
845 user: &BTreeSet<Digest>,
846 c: &ValidationContext,
847) -> Result<(), Refusal> {
848 let s = variant(s, value).unwrap_or(s);
849 if credential(s) {
850 let d = digest(PURPOSE_VALUE, value)
851 .map_err(|_| refusal(RefusalCode::ValueInvalid, path, c))?;
852 if c.project_id.is_none() || user.contains(&d) || !c.project_credentials.contains(&d) {
853 return Err(refusal(RefusalCode::CredentialShared, path, c));
854 }
855 } else if let Some(o) = value.as_object() {
856 for (key, v) in o {
857 if let Some(child) = child_schema(s, key) {
858 check_credentials(child, v, &pointer(path, key), user, c)?;
859 }
860 }
861 } else if let Some(a) = value.as_array() {
862 for (i, v) in a.iter().enumerate() {
863 check_credentials(&s["items"], v, &pointer(path, &i.to_string()), user, c)?;
864 }
865 }
866 Ok(())
867}
868fn record(s: &Value, value: &Value) -> Result<Value, crate::CanonicalError> {
869 if credential(s) {
870 return serde_json::to_value(RecordedValue::redacted(value)?)
871 .map_err(|_| crate::CanonicalError::Serialization);
872 }
873 let schema = variant(s, value).unwrap_or(s);
874 match value {
875 Value::Object(o) => Ok(Value::Object(
876 o.iter()
877 .map(|(key, v)| {
878 let v = if let Some(child) = child_schema(schema, key) {
881 record(child, v)?
882 } else {
883 serde_json::to_value(RecordedValue::redacted(v)?)
884 .map_err(|_| crate::CanonicalError::Serialization)?
885 };
886 Ok((key.clone(), v))
887 })
888 .collect::<Result<_, crate::CanonicalError>>()?,
889 )),
890 Value::Array(a) => Ok(Value::Array(
891 a.iter()
892 .map(|v| record(&schema["items"], v))
893 .collect::<Result<_, _>>()?,
894 )),
895 other => Ok(other.clone()),
896 }
897}
898
899fn strip_options(s: &mut Value) {
900 match s {
901 Value::Object(o) => {
902 o.remove("x-ck-options");
903 for value in o.values_mut() {
904 strip_options(value);
905 }
906 }
907 Value::Array(a) => {
908 for value in a {
909 strip_options(value);
910 }
911 }
912 _ => {}
913 }
914}
915fn validate_vocabulary(node: &Value, path: &str) -> Result<(), Refusal> {
916 let vocabulary: Value =
917 serde_json::from_str(VOCABULARY_META_SCHEMA).expect("committed vocabulary");
918 for (key, value) in node.as_object().expect("schema object") {
919 if !key.starts_with("x-ck-") {
920 continue;
921 }
922 let definition = vocabulary["properties"]
923 .get(key)
924 .ok_or_else(|| schema_error(path))?;
925 if let Some(values) = definition.get("enum").and_then(Value::as_array) {
926 if !values.contains(value) {
927 return Err(schema_error(path));
928 }
929 }
930 if let Some(t) = definition.get("type").and_then(Value::as_str) {
931 let valid = match t {
932 "object" => value.is_object(),
933 "array" => value.is_array(),
934 "string" => value.is_string(),
935 "boolean" => value.is_boolean(),
936 "number" => value.is_number(),
937 _ => false,
938 };
939 if !valid {
940 return Err(schema_error(path));
941 }
942 }
943 }
944 if let Some(options) = node.get("x-ck-options") {
945 let o = options.as_object().ok_or_else(|| schema_error(path))?;
946 if o.keys().any(|k| {
947 ![
948 "source",
949 "items_pointer",
950 "value_pointer",
951 "label_pointer",
952 "depends_on",
953 "closed",
954 "items_from_parent_pointer",
955 ]
956 .contains(&k.as_str())
957 }) {
958 return Err(schema_error(path));
959 }
960 for p in ["items_pointer", "value_pointer", "label_pointer"] {
961 if !options
962 .get(p)
963 .and_then(Value::as_str)
964 .is_some_and(valid_pointer)
965 {
966 return Err(schema_error(path));
967 }
968 }
969 if options
970 .get("items_from_parent_pointer")
971 .is_some_and(|v| !v.as_str().is_some_and(valid_pointer))
972 {
973 return Err(schema_error(path));
974 }
975 if !options.get("closed").is_some_and(Value::is_boolean) {
976 return Err(schema_error(path));
977 }
978 let deps = options
979 .get("depends_on")
980 .and_then(Value::as_array)
981 .ok_or_else(|| schema_error(path))?;
982 if deps.iter().any(|p| !p.as_str().is_some_and(valid_pointer)) {
983 return Err(schema_error(path));
984 }
985 let source = options
986 .get("source")
987 .and_then(Value::as_object)
988 .ok_or_else(|| schema_error(path))?;
989 if source
990 .keys()
991 .any(|k| !["module", "op", "params"].contains(&k.as_str()))
992 || !["module", "op"].iter().all(|k| {
993 source
994 .get(*k)
995 .and_then(Value::as_str)
996 .is_some_and(|s| !s.is_empty())
997 })
998 || !source.get("params").is_some_and(Value::is_object)
999 {
1000 return Err(schema_error(path));
1001 }
1002 }
1003 if let Some(order) = node.get("x-ck-ordering").and_then(Value::as_array) {
1004 if order.len() < 2
1005 || order
1006 .iter()
1007 .any(|v| v.is_null() || v.is_array() || v.is_object())
1008 || order
1009 .iter()
1010 .enumerate()
1011 .any(|(i, v)| order[..i].iter().any(|x| same_scalar(x, v)))
1012 {
1013 return Err(schema_error(path));
1014 }
1015 }
1016 Ok(())
1017}
1018fn valid_pointer(s: &str) -> bool {
1019 if !s.is_empty() && !s.starts_with('/') {
1020 return false;
1021 }
1022 let mut chars = s.chars();
1023 while let Some(c) = chars.next() {
1024 if c == '~' && !matches!(chars.next(), Some('0' | '1')) {
1025 return false;
1026 }
1027 }
1028 true
1029}
1030
1031fn overlay_schema(s: &Value, lower: &Value, higher: &Value) -> Value {
1032 let selected = variant(s, higher)
1033 .or_else(|| variant(s, lower))
1034 .unwrap_or(s);
1035 let mut result = selected.clone();
1036 if let Some(properties) = result.get_mut("properties").and_then(Value::as_object_mut) {
1037 for (key, child) in properties {
1038 *child = overlay_schema(child, &lower[key], &higher[key]);
1039 }
1040 }
1041 result
1042}
1043
1044fn contains_expansion(v: &Value) -> bool {
1045 match v {
1046 Value::String(s) => s.contains("{env:") || s.contains("{file:"),
1047 Value::Array(a) => a.iter().any(contains_expansion),
1048 Value::Object(o) => o
1049 .iter()
1050 .any(|(k, v)| k.contains("{env:") || k.contains("{file:") || contains_expansion(v)),
1051 _ => false,
1052 }
1053}
1054
1055#[cfg(test)]
1056mod tests {
1057 use super::*;
1058 use serde_json::json;
1059 fn field(kind: &str, default: Value) -> Value {
1060 json!({"type":kind,"description":"A configurable value", "default":default,"x-ck-apply":"live","x-ck-layers":["user","workspace","project"]})
1061 }
1062 fn object(properties: Value) -> Value {
1063 json!({"type":"object","properties":properties,"unevaluatedProperties":false})
1064 }
1065 fn schema(properties: Value) -> Schema {
1066 let mut value = object(properties);
1067 value["$schema"] = json!("https://json-schema.org/draft/2020-12/schema");
1068 Schema::new(value).unwrap()
1069 }
1070 fn context() -> ValidationContext {
1071 ValidationContext::new("module", Some("project-one".into()))
1072 }
1073 fn layers(user: Value, project: Value) -> Vec<LayerValue> {
1074 vec![
1075 LayerValue::new(Layer::User, user),
1076 LayerValue::new(Layer::Project, project),
1077 ]
1078 }
1079
1080 #[test]
1081 fn policy_contract_raise_only_golden_names_affected_project() {
1082 let mut n = field("number", json!(5));
1083 n["x-ck-bound"] = json!("raise_only");
1084 let s = schema(json!({"n":n}));
1085 let valid = s
1086 .review_tuple(&layers(json!({"n":10}), json!({"n":15})), &context())
1087 .unwrap();
1088 assert_eq!(valid.effective, json!({"n":15}));
1089 assert_eq!(valid.sources["/n"], Layer::Project);
1090 let refusal = s
1091 .review_tuple(&layers(json!({"n":20}), json!({"n":15})), &context())
1092 .unwrap_err();
1093 assert_eq!(refusal.code, RefusalCode::BoundViolated);
1094 assert_eq!(refusal.path, "/n");
1095 assert_eq!(refusal.project_id.as_deref(), Some("project-one"));
1096 assert!(s
1097 .review_tuple(&layers(json!({"n":20}), json!({"n":25})), &context())
1098 .is_ok());
1099 assert!(s
1100 .review_tuple(&layers(json!({"n":20}), json!({})), &context())
1101 .is_ok());
1102 }
1103 #[test]
1104 fn policy_contract_lower_subset_and_ordered_bounds() {
1105 let mut n = field("number", json!(20));
1106 n["x-ck-bound"] = json!("lower_only");
1107 let s = schema(json!({"n":n}));
1108 assert!(s
1109 .review_tuple(&layers(json!({"n":10}), json!({"n":8})), &context())
1110 .is_ok());
1111 assert_eq!(
1112 s.review_tuple(&layers(json!({"n":10}), json!({"n":12})), &context())
1113 .unwrap_err()
1114 .code,
1115 RefusalCode::BoundViolated
1116 );
1117 let mut list = field("array", json!(["a", "b"]));
1118 list["items"] = field("string", json!("a"));
1119 list["x-ck-bound"] = json!("subset_only");
1120 let s = schema(json!({"list":list}));
1121 assert!(s
1122 .review_tuple(
1123 &layers(json!({"list":["b","a"]}), json!({"list":["b"]})),
1124 &context()
1125 )
1126 .is_ok());
1127 assert_eq!(
1128 s.review_tuple(
1129 &layers(json!({"list":["b"]}), json!({"list":["a"]})),
1130 &context()
1131 )
1132 .unwrap_err()
1133 .code,
1134 RefusalCode::BoundViolated
1135 );
1136 let mut on = field("boolean", json!(false));
1137 on["x-ck-bound"] = json!("raise_only");
1138 on["x-ck-ordering"] = json!([false, true]);
1139 let s = schema(json!({"on":on}));
1140 assert_eq!(
1141 s.review_tuple(&layers(json!({"on":true}), json!({"on":false})), &context())
1142 .unwrap_err()
1143 .code,
1144 RefusalCode::BoundViolated
1145 );
1146 }
1147 #[test]
1148 fn policy_contract_picker_and_secret_redaction() {
1149 let mut choice = field("string", json!("small"));
1150 choice["enum"] = json!(["small", "large"]);
1151 let mut secret = field("object", json!({"vault_ref":"user-key"}));
1152 secret["x-ck-kind"] = json!("credential_ref");
1153 let s = schema(json!({"choice":choice,"key":secret}));
1154 assert_eq!(
1155 s.validate_layer(Layer::User, &json!({"choice":"huge"}), &context())
1156 .unwrap_err()
1157 .code,
1158 RefusalCode::ValueInvalid
1159 );
1160 let literal = json!({"key":"must-never-be-retained"});
1161 let error = s
1162 .validate_layer(Layer::User, &literal, &context())
1163 .unwrap_err();
1164 assert_eq!(error.code, RefusalCode::ValueInvalid);
1165 let recorded = s.record(&literal).unwrap();
1166 let text = serde_json::to_string(&(recorded.clone(), error)).unwrap();
1167 assert!(!text.contains("must-never-be-retained"));
1168 assert!(text.contains("redacted"));
1169 assert_eq!(
1170 recorded["key"]["digest"],
1171 json!(digest(PURPOSE_VALUE, &literal["key"]).unwrap())
1172 );
1173 let accepted =
1174 serde_json::to_string(&s.record(&json!({"key":{"vault_ref":"user-key"}})).unwrap())
1175 .unwrap();
1176 assert!(!accepted.contains("user-key"));
1177 for invalid in [
1178 json!({"vault_ref":""}),
1179 json!({"vault_ref":"has spaces"}),
1180 json!({"vault_ref":"ok","secret":"bad"}),
1181 json!({"env":"KEY"}),
1182 ] {
1183 assert!(!is_vault_reference(&invalid));
1184 }
1185 }
1186 #[test]
1187 fn policy_contract_closed_picker_requires_pinned_evidence() {
1188 let mut choice = field("string", json!("small"));
1189 choice["x-ck-options"] = json!({"source":{"module":"catalog","op":"list","params":{}},"items_pointer":"/items","value_pointer":"/id","label_pointer":"/label","depends_on":[],"closed":true});
1190 let s = schema(json!({"choice":choice}));
1191 assert!(s
1192 .validate_layer(Layer::User, &json!({"choice":"small"}), &context())
1193 .is_err());
1194 let mut c = context();
1195 c.closed_options
1196 .insert("/choice".into(), vec![json!("small")]);
1197 assert!(s
1198 .validate_layer(Layer::User, &json!({"choice":"small"}), &c)
1199 .is_ok());
1200 assert!(s
1201 .validate_layer(Layer::User, &json!({"choice":"huge"}), &c)
1202 .is_err());
1203 }
1204 #[test]
1205 fn policy_contract_credential_shared_is_not_entitlement() {
1206 let mut key = field("object", json!({"vault_ref":"user-key"}));
1207 key["x-ck-kind"] = json!("credential_ref");
1208 let s = schema(json!({"key":key,"endpoint":field("string",json!("approved"))}));
1209 let mut c = context();
1210 c.credential_destinations
1211 .insert("/key".into(), vec!["/endpoint".into()]);
1212 let copied = layers(
1213 json!({"key":{"vault_ref":"user-key"}}),
1214 json!({"key":{"vault_ref":"user-key"}}),
1215 );
1216 c.project_credentials
1217 .insert(digest(PURPOSE_VALUE, &json!({"vault_ref":"user-key"})).unwrap());
1218 assert_eq!(
1219 s.review_tuple(&copied, &c).unwrap_err().code,
1220 RefusalCode::CredentialShared
1221 );
1222 let redirected = layers(
1223 json!({"key":{"vault_ref":"user-key"}}),
1224 json!({"endpoint":"other"}),
1225 );
1226 assert_eq!(
1227 s.review_tuple(&redirected, &c).unwrap_err().code,
1228 RefusalCode::CredentialShared
1229 );
1230 let owned = layers(
1231 json!({"key":{"vault_ref":"user-key"}}),
1232 json!({"key":{"vault_ref":"project-key"}}),
1233 );
1234 assert_eq!(
1235 s.review_tuple(&owned, &c).unwrap_err().code,
1236 RefusalCode::CredentialShared
1237 );
1238 c.project_credentials
1239 .insert(digest(PURPOSE_VALUE, &json!({"vault_ref":"project-key"})).unwrap());
1240 assert!(s.review_tuple(&owned, &c).is_ok());
1241 }
1242 #[test]
1243 fn policy_contract_merge_absent_empty_arrays_maps_null_and_unknown() {
1244 let mut array = field("array", json!([1, 2]));
1245 array["items"] = field("number", json!(0));
1246 let mut map = object(json!({}));
1247 map["patternProperties"] = json!({"^[a-z]+$":field("string",json!(""))});
1248 let s = schema(
1249 json!({"n":field("number",json!(4)),"on":field("boolean",json!(true)),"section":object(json!({"a":field("number",json!(1)),"b":field("number",json!(2))})),"v":array,"tags":map}),
1250 );
1251 let result = s
1252 .review_tuple(
1253 &layers(
1254 json!({"tags":{"a":"x"}}),
1255 json!({"on":false,"section":{"a":3},"v":[],"tags":{"b":"y"}}),
1256 ),
1257 &context(),
1258 )
1259 .unwrap();
1260 assert_eq!(
1261 result.effective,
1262 json!({"n":4,"on":false,"section":{"a":3,"b":2},"v":[],"tags":{"a":"x","b":"y"}})
1263 );
1264 assert_eq!(result.sources["/n"], Layer::Default);
1265 for invalid in [
1266 json!({"n":null}),
1267 json!({"unknown":1}),
1268 json!({"n":"4"}),
1269 json!({"tags":{"BAD":"x"}}),
1270 ] {
1271 assert!(s
1272 .validate_layer(Layer::Project, &invalid, &context())
1273 .is_err());
1274 }
1275 }
1276 #[test]
1277 fn policy_contract_union_inheritance_and_complete_array_items() {
1278 let mut disk_kind = field("string", json!("disk"));
1279 disk_kind["const"] = json!("disk");
1280 let mut memory_kind = field("string", json!("memory"));
1281 memory_kind["const"] = json!("memory");
1282 let mut disk = object(json!({"kind":disk_kind,"path":field("string",json!("a"))}));
1283 disk["required"] = json!(["kind", "path"]);
1284 let mut memory = object(json!({"kind":memory_kind,"capacity":field("number",json!(1))}));
1285 memory["required"] = json!(["kind", "capacity"]);
1286 let union = json!({"oneOf":[disk,memory],"default":{"kind":"disk","path":"a"}});
1287 let s = schema(json!({"storage":union}));
1288 let result = s
1289 .review_tuple(
1290 &layers(json!({}), json!({"storage":{"path":"b"}})),
1291 &context(),
1292 )
1293 .unwrap();
1294 assert_eq!(
1295 result.effective,
1296 json!({"storage":{"kind":"disk","path":"b"}})
1297 );
1298 assert!(s
1299 .review_tuple(
1300 &layers(json!({}), json!({"storage":{"kind":"memory","capacity":2}})),
1301 &context()
1302 )
1303 .is_err());
1304 let mut item =
1305 object(json!({"id":field("string",json!("a")),"n":field("number",json!(1))}));
1306 item["required"] = json!(["id", "n"]);
1307 let mut list = field("array", json!([]));
1308 list["items"] = item;
1309 let s = schema(json!({"list":list}));
1310 assert!(s
1311 .validate_layer(Layer::Project, &json!({"list":[{"id":"a"}]}), &context())
1312 .is_err());
1313 assert!(s
1314 .validate_layer(
1315 Layer::Project,
1316 &json!({"list":[{"id":"a","n":1},{"id":"a","n":2}]}),
1317 &context()
1318 )
1319 .is_err());
1320 }
1321 #[test]
1322 fn policy_contract_references_narrowing_and_bad_schemas() {
1323 let mut root = object(json!({"n":{"$ref":"#/$defs/n","x-ck-layers":["user"]}}));
1324 root["$schema"] = json!("https://json-schema.org/draft/2020-12/schema");
1325 root["$defs"] = json!({"n":field("number",json!(5))});
1326 let s = Schema::new(root.clone()).unwrap();
1327 assert_eq!(
1328 s.validate_layer(Layer::Project, &json!({"n":6}), &context())
1329 .unwrap_err()
1330 .code,
1331 RefusalCode::LayerForbidden
1332 );
1333 root["properties"]["n"]["$ref"] = json!("https://example.invalid/schema");
1334 assert!(Schema::new(root.clone()).is_err());
1335 root["properties"]["n"]["$ref"] = json!("#/$defs/n");
1336 root["$defs"]["n"] = json!({"$ref":"#/$defs/n"});
1337 assert!(Schema::new(root).is_err());
1338 let mut boolean = field("boolean", json!(false));
1339 boolean["x-ck-bound"] = json!("raise_only");
1340 let mut root = object(json!({"on":boolean}));
1341 root["$schema"] = json!("https://json-schema.org/draft/2020-12/schema");
1342 assert!(Schema::new(root).is_err());
1343 let mut root = object(json!({"n":field("number",json!(5))}));
1344 root["$schema"] = json!("https://json-schema.org/draft/2020-12/schema");
1345 root["x-ck-layers"] = json!(["user"]);
1346 assert!(Schema::new(root).is_err());
1347 }
1348
1349 #[test]
1350 fn policy_contract_optional_annotation_values_are_checked() {
1351 for (keyword, value) in [
1352 ("x-ck-action", json!("not an operation object")),
1353 ("x-ck-ordering", json!([false, false])),
1354 ("x-ck-ordering", json!([null, true])),
1355 ("x-ck-kind", json!("unknown")),
1356 ("x-ck-required", json!("yes")),
1357 ] {
1358 let mut n = field("number", json!(5));
1359 n[keyword] = value;
1360 let mut root = object(json!({"n":n}));
1361 root["$schema"] = json!("https://json-schema.org/draft/2020-12/schema");
1362 assert_eq!(
1363 Schema::new(root).unwrap_err().code,
1364 RefusalCode::SchemaInvalid,
1365 "{keyword}"
1366 );
1367 }
1368 }
1369}