@voxgig/sdkgen 4.49.1 → 4.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/package.json +1 -1
  3. package/project/.sdk/model/feature/cache.aontu +6 -4
  4. package/project/.sdk/model/target/lua.aontu +6 -0
  5. package/project/.sdk/src/cmp/c/Config_c.ts +3 -134
  6. package/project/.sdk/src/cmp/c/TestClean_c.ts +14 -0
  7. package/project/.sdk/src/cmp/clojure/TestClean_clojure.ts +10 -5
  8. package/project/.sdk/src/cmp/cpp/TestClean_cpp.ts +13 -0
  9. package/project/.sdk/src/cmp/csharp/TestClean_csharp.ts +9 -0
  10. package/project/.sdk/src/cmp/elixir/TestClean_elixir.ts +11 -3
  11. package/project/.sdk/src/cmp/go/TestClean_go.ts +39 -3
  12. package/project/.sdk/src/cmp/java/TestClean_java.ts +8 -0
  13. package/project/.sdk/src/cmp/js/TestClean_js.ts +13 -5
  14. package/project/.sdk/src/cmp/kotlin/TestClean_kotlin.ts +22 -0
  15. package/project/.sdk/src/cmp/lua/TestClean_lua.ts +12 -3
  16. package/project/.sdk/src/cmp/ocaml/TestClean_ocaml.ts +18 -4
  17. package/project/.sdk/src/cmp/perl/TestClean_perl.ts +12 -3
  18. package/project/.sdk/src/cmp/php/TestClean_php.ts +10 -3
  19. package/project/.sdk/src/cmp/py/TestClean_py.ts +10 -3
  20. package/project/.sdk/src/cmp/rb/TestClean_rb.ts +10 -3
  21. package/project/.sdk/src/cmp/rust/TestClean_rust.ts +10 -0
  22. package/project/.sdk/src/cmp/scala/TestClean_scala.ts +34 -0
  23. package/project/.sdk/src/cmp/swift/TestClean_swift.ts +20 -10
  24. package/project/.sdk/src/cmp/ts/TestClean_ts.ts +13 -5
  25. package/project/.sdk/src/cmp/zig/TestClean_zig.ts +12 -0
  26. package/project/.sdk/tm/c/core/context.c +1 -0
  27. package/project/.sdk/tm/c/core/sdk.h +7 -4
  28. package/project/.sdk/tm/c/feature/cache.c +243 -13
  29. package/project/.sdk/tm/c/feature/debug.c +1 -0
  30. package/project/.sdk/tm/c/feature/secrets.c +27 -38
  31. package/project/.sdk/tm/c/feature/telemetry.c +1 -0
  32. package/project/.sdk/tm/c/tests/feature_test.c +142 -0
  33. package/project/.sdk/tm/c/utility/clean.c +26 -33
  34. package/project/.sdk/tm/c/utility/make_options.c +9 -8
  35. package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +14 -11
  36. package/project/.sdk/tm/clojure/src/sdk/client.clj +3 -1
  37. package/project/.sdk/tm/clojure/src/sdk/core.clj +19 -11
  38. package/project/.sdk/tm/clojure/src/sdk/features.clj +83 -10
  39. package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +47 -0
  40. package/project/.sdk/tm/clojure/test/sdk/test/pipeline.clj +1 -0
  41. package/project/.sdk/tm/cpp/Makefile +5 -0
  42. package/project/.sdk/tm/cpp/core/types.hpp +11 -4
  43. package/project/.sdk/tm/cpp/feature/audit.hpp +6 -7
  44. package/project/.sdk/tm/cpp/feature/cache.hpp +188 -9
  45. package/project/.sdk/tm/cpp/feature/debug.hpp +1 -0
  46. package/project/.sdk/tm/cpp/feature/secrets.hpp +12 -0
  47. package/project/.sdk/tm/cpp/feature/telemetry.hpp +1 -0
  48. package/project/.sdk/tm/cpp/test/feature_test.cpp +128 -0
  49. package/project/.sdk/tm/cpp/utility/pipeline.hpp +9 -7
  50. package/project/.sdk/tm/csharp/core/Context.cs +4 -0
  51. package/project/.sdk/tm/csharp/feature/CacheFeature.cs +126 -12
  52. package/project/.sdk/tm/csharp/feature/DebugFeature.cs +1 -0
  53. package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +9 -27
  54. package/project/.sdk/tm/csharp/feature/TelemetryFeature.cs +1 -0
  55. package/project/.sdk/tm/csharp/test/FeatureTest.cs +101 -0
  56. package/project/.sdk/tm/csharp/utility/Clean.cs +5 -10
  57. package/project/.sdk/tm/csharp/utility/Fetcher.cs +7 -1
  58. package/project/.sdk/tm/csharp/utility/MakeOptions.cs +5 -12
  59. package/project/.sdk/tm/elixir/lib/projectname/context.ex +2 -0
  60. package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +146 -30
  61. package/project/.sdk/tm/elixir/lib/projectname/feature/debug.ex +1 -0
  62. package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +10 -56
  63. package/project/.sdk/tm/elixir/lib/projectname/feature/telemetry.ex +1 -0
  64. package/project/.sdk/tm/elixir/lib/projectname/utility.ex +18 -12
  65. package/project/.sdk/tm/elixir/test/feature_test.exs +84 -0
  66. package/project/.sdk/tm/go/core/context.go +6 -0
  67. package/project/.sdk/tm/go/feature/cache_feature.go +131 -4
  68. package/project/.sdk/tm/go/feature/debug_feature.go +3 -2
  69. package/project/.sdk/tm/go/feature/secrets_feature.go +17 -71
  70. package/project/.sdk/tm/go/feature/telemetry_feature.go +5 -4
  71. package/project/.sdk/tm/go/test/feature_corpus_test.go +150 -2
  72. package/project/.sdk/tm/go/test/feature_test.go +101 -0
  73. package/project/.sdk/tm/go/utility/clean.go +5 -7
  74. package/project/.sdk/tm/go/utility/fetcher.go +11 -0
  75. package/project/.sdk/tm/go/utility/make_options.go +5 -7
  76. package/project/.sdk/tm/java/core/Context.java +9 -0
  77. package/project/.sdk/tm/java/feature/CacheFeature.java +123 -17
  78. package/project/.sdk/tm/java/feature/DebugFeature.java +1 -0
  79. package/project/.sdk/tm/java/feature/SecretsFeature.java +9 -92
  80. package/project/.sdk/tm/java/feature/TelemetryFeature.java +1 -0
  81. package/project/.sdk/tm/java/test/FeatureCorpusTest.java +141 -2
  82. package/project/.sdk/tm/java/test/FeatureTest.java +88 -0
  83. package/project/.sdk/tm/java/utility/Clean.java +6 -10
  84. package/project/.sdk/tm/java/utility/Fetcher.java +5 -1
  85. package/project/.sdk/tm/java/utility/MakeOptions.java +3 -1
  86. package/project/.sdk/tm/js/src/Context.js +4 -0
  87. package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +118 -11
  88. package/project/.sdk/tm/js/src/feature/cost/CostFeature.js +3 -2
  89. package/project/.sdk/tm/js/src/feature/debug/DebugFeature.js +1 -0
  90. package/project/.sdk/tm/js/src/feature/metrics/MetricsFeature.js +1 -1
  91. package/project/.sdk/tm/js/src/feature/paging/PagingFeature.js +4 -4
  92. package/project/.sdk/tm/js/src/feature/rbac/RbacFeature.js +4 -4
  93. package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +16 -6
  94. package/project/.sdk/tm/js/src/feature/telemetry/TelemetryFeature.js +1 -0
  95. package/project/.sdk/tm/js/src/feature/validate/ValidateFeature.js +2 -1
  96. package/project/.sdk/tm/js/src/utility/CleanUtility.js +1 -1
  97. package/project/.sdk/tm/js/src/utility/MakeOptionsUtility.js +6 -6
  98. package/project/.sdk/tm/js/test/feature/Corpus.test.js +100 -2
  99. package/project/.sdk/tm/js/test/feature/harness.js +3 -3
  100. package/project/.sdk/tm/js/test/feature.test.js +17 -0
  101. package/project/.sdk/tm/js/test/pipeline.test.js +1 -1
  102. package/project/.sdk/tm/kotlin/core/Context.kt +3 -0
  103. package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +102 -7
  104. package/project/.sdk/tm/kotlin/feature/DebugFeature.kt +1 -0
  105. package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +10 -20
  106. package/project/.sdk/tm/kotlin/feature/TelemetryFeature.kt +1 -0
  107. package/project/.sdk/tm/kotlin/test/FeatureTest.kt +79 -0
  108. package/project/.sdk/tm/kotlin/utility/CoreUtil.kt +5 -5
  109. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +4 -0
  110. package/project/.sdk/tm/kotlin/utility/MakeOptions.kt +3 -1
  111. package/project/.sdk/tm/lua/core/context.lua +6 -0
  112. package/project/.sdk/tm/lua/feature/cache_feature.lua +212 -15
  113. package/project/.sdk/tm/lua/feature/debug_feature.lua +1 -0
  114. package/project/.sdk/tm/lua/feature/secrets_feature.lua +15 -80
  115. package/project/.sdk/tm/lua/feature/telemetry_feature.lua +1 -0
  116. package/project/.sdk/tm/lua/test/feature_test.lua +108 -0
  117. package/project/.sdk/tm/lua/utility/clean.lua +6 -9
  118. package/project/.sdk/tm/lua/utility/fetcher.lua +9 -0
  119. package/project/.sdk/tm/lua/utility/make_options.lua +5 -5
  120. package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +21 -6
  121. package/project/.sdk/tm/ocaml/sdk_features.ml +108 -8
  122. package/project/.sdk/tm/ocaml/sdk_runtime.ml +13 -10
  123. package/project/.sdk/tm/ocaml/sdk_types.ml +2 -0
  124. package/project/.sdk/tm/ocaml/test/t_feature.ml +51 -1
  125. package/project/.sdk/tm/perl/core/context.pm +3 -0
  126. package/project/.sdk/tm/perl/feature/audit_feature.pm +4 -7
  127. package/project/.sdk/tm/perl/feature/cache_feature.pm +111 -15
  128. package/project/.sdk/tm/perl/feature/debug_feature.pm +1 -0
  129. package/project/.sdk/tm/perl/feature/secrets_feature.pm +13 -18
  130. package/project/.sdk/tm/perl/feature/telemetry_feature.pm +1 -0
  131. package/project/.sdk/tm/perl/t/feature.t +86 -0
  132. package/project/.sdk/tm/perl/t/feature_corpus.t +80 -4
  133. package/project/.sdk/tm/perl/utility/clean.pm +6 -8
  134. package/project/.sdk/tm/perl/utility/fetcher.pm +4 -0
  135. package/project/.sdk/tm/perl/utility/make_options.pm +5 -5
  136. package/project/.sdk/tm/php/core/Context.php +4 -0
  137. package/project/.sdk/tm/php/feature/CacheFeature.php +98 -13
  138. package/project/.sdk/tm/php/feature/DebugFeature.php +1 -0
  139. package/project/.sdk/tm/php/feature/SecretsFeature.php +18 -15
  140. package/project/.sdk/tm/php/feature/TelemetryFeature.php +1 -0
  141. package/project/.sdk/tm/php/test/FeatureCorpusTest.php +113 -3
  142. package/project/.sdk/tm/php/test/FeatureTest.php +90 -0
  143. package/project/.sdk/tm/php/utility/Clean.php +3 -10
  144. package/project/.sdk/tm/php/utility/Fetcher.php +8 -3
  145. package/project/.sdk/tm/php/utility/MakeOptions.php +5 -7
  146. package/project/.sdk/tm/py/pkg/core/context.py +4 -0
  147. package/project/.sdk/tm/py/pkg/feature/cache_feature.py +79 -13
  148. package/project/.sdk/tm/py/pkg/feature/debug_feature.py +1 -0
  149. package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +15 -66
  150. package/project/.sdk/tm/py/pkg/feature/telemetry_feature.py +1 -0
  151. package/project/.sdk/tm/py/pkg/utility/clean.py +4 -10
  152. package/project/.sdk/tm/py/pkg/utility/make_options.py +8 -9
  153. package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +23 -9
  154. package/project/.sdk/tm/py/test/test_feature.py +73 -0
  155. package/project/.sdk/tm/py/test/test_feature_corpus.py +88 -3
  156. package/project/.sdk/tm/rb/core/context.rb +4 -1
  157. package/project/.sdk/tm/rb/feature/audit_feature.rb +6 -5
  158. package/project/.sdk/tm/rb/feature/cache_feature.rb +75 -11
  159. package/project/.sdk/tm/rb/feature/debug_feature.rb +1 -0
  160. package/project/.sdk/tm/rb/feature/secrets_feature.rb +9 -64
  161. package/project/.sdk/tm/rb/feature/telemetry_feature.rb +1 -0
  162. package/project/.sdk/tm/rb/test/feature_corpus_test.rb +85 -3
  163. package/project/.sdk/tm/rb/test/feature_test.rb +93 -0
  164. package/project/.sdk/tm/rb/utility/clean.rb +6 -7
  165. package/project/.sdk/tm/rb/utility/fetcher.rb +6 -0
  166. package/project/.sdk/tm/rb/utility/make_options.rb +5 -5
  167. package/project/.sdk/tm/rust/core/context.rs +8 -0
  168. package/project/.sdk/tm/rust/feature/cache.rs +120 -3
  169. package/project/.sdk/tm/rust/feature/debug.rs +1 -0
  170. package/project/.sdk/tm/rust/feature/secrets.rs +36 -98
  171. package/project/.sdk/tm/rust/feature/telemetry.rs +1 -0
  172. package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +3 -3
  173. package/project/.sdk/tm/rust/tests/feature_test.rs +141 -0
  174. package/project/.sdk/tm/rust/utility/clean.rs +35 -43
  175. package/project/.sdk/tm/rust/utility/fetcher.rs +6 -0
  176. package/project/.sdk/tm/rust/utility/make_options.rs +8 -11
  177. package/project/.sdk/tm/scala/core/Context.scala +4 -0
  178. package/project/.sdk/tm/scala/feature/CacheFeature.scala +117 -11
  179. package/project/.sdk/tm/scala/feature/DebugFeature.scala +1 -0
  180. package/project/.sdk/tm/scala/feature/SecretsFeature.scala +11 -24
  181. package/project/.sdk/tm/scala/feature/TelemetryFeature.scala +1 -0
  182. package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +52 -1
  183. package/project/.sdk/tm/scala/utility/Fetcher.scala +5 -0
  184. package/project/.sdk/tm/scala/utility/Make.scala +3 -1
  185. package/project/.sdk/tm/scala/utility/Misc.scala +5 -8
  186. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Context.swift +3 -0
  187. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +122 -11
  188. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/DebugFeature.swift +1 -0
  189. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +9 -13
  190. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/TelemetryFeature.swift +1 -0
  191. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Fetcher.swift +4 -0
  192. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/MakeOptions.swift +5 -4
  193. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/ResultUtil.swift +4 -5
  194. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +79 -0
  195. package/project/.sdk/tm/ts/src/Context.ts +4 -0
  196. package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +115 -4
  197. package/project/.sdk/tm/ts/src/feature/cost/CostFeature.ts +3 -2
  198. package/project/.sdk/tm/ts/src/feature/debug/DebugFeature.ts +1 -0
  199. package/project/.sdk/tm/ts/src/feature/metrics/MetricsFeature.ts +1 -1
  200. package/project/.sdk/tm/ts/src/feature/paging/PagingFeature.ts +4 -4
  201. package/project/.sdk/tm/ts/src/feature/rbac/RbacFeature.ts +4 -4
  202. package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +14 -2
  203. package/project/.sdk/tm/ts/src/feature/telemetry/TelemetryFeature.ts +1 -0
  204. package/project/.sdk/tm/ts/src/feature/validate/ValidateFeature.ts +2 -1
  205. package/project/.sdk/tm/ts/src/utility/CleanUtility.ts +1 -1
  206. package/project/.sdk/tm/ts/src/utility/MakeOptionsUtility.ts +6 -6
  207. package/project/.sdk/tm/ts/test/feature/Corpus.test.ts +101 -2
  208. package/project/.sdk/tm/ts/test/feature/harness.ts +3 -3
  209. package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
  210. package/project/.sdk/tm/ts/test/pipeline.test.ts +1 -1
  211. package/project/.sdk/tm/zig/core/context.zig +5 -0
  212. package/project/.sdk/tm/zig/core/utility.zig +11 -9
  213. package/project/.sdk/tm/zig/feature/cache.zig +144 -13
  214. package/project/.sdk/tm/zig/feature/debug.zig +1 -0
  215. package/project/.sdk/tm/zig/feature/secrets.zig +6 -1
  216. package/project/.sdk/tm/zig/feature/telemetry.zig +1 -0
  217. package/project/.sdk/tm/zig/test/feature_test.zig +100 -0
  218. package/project/sdkgen-package.json +1 -1
@@ -846,12 +846,12 @@ class CleanTest extends TestCase
846
846
  // and is not changed by it.
847
847
  $config = ['options' => ['clean' => ['keys' => 'zzsens', 'values' => self::CANARY['config']]]];
848
848
  $util = $hooked->get_utility();
849
- $built = ($util->make_options)(new ${Name}Context([
849
+ $cfgctx = new ${Name}Context([
850
850
  'utility' => $util,
851
851
  'config' => $config,
852
852
  'options' => ['clean' => ['values' => self::CANARY['value']]],
853
- ], null));
854
- $cfgctx = new ${Name}Context(['options' => $built], null);
853
+ ], null);
854
+ $cfgctx->options = ($util->make_options)($cfgctx);
855
855
  $seeded = ($util->clean)($cfgctx, 'config ' . self::CANARY['config'] . ' caller ' . self::CANARY['value']);
856
856
  $sinks[] = ['name' => 'config-clean', 'text' => (string)$seeded];
857
857
  $this->assertSame('config ' . self::MASK . ' caller ' . self::MASK, $seeded);
@@ -899,6 +899,13 @@ class CleanTest extends TestCase
899
899
  $falias = $fclean($featured->get_root_ctx(), 'alias PLAINALIAS-m2n4b6v8');
900
900
  $frule = $fclean($featured->get_root_ctx(), 'rule PLAINRULE-k7j5h3g1');
901
901
 
902
+ // The clean registry holds every credential in plain form, and is no option.
903
+ $held = self::construct(['apikey' => self::CANARY['apikey']])->options_map();
904
+ $heldjson = (string)json_encode($held, JSON_PARTIAL_OUTPUT_ON_ERROR);
905
+ $this->assertSame(self::CANARY['apikey'], $held['apikey'] ?? null);
906
+ $this->assertSame([], self::leaks(preg_replace('/' . preg_quote(json_encode(self::CANARY['apikey']), '/') . '/',
907
+ '', $heldjson, 1)), 'options_map carries a credential beyond the option the caller set: ' . $heldjson);
908
+
902
909
  $leaked = [];
903
910
  $excerpt = '';
904
911
  foreach ($sinks as $s) {
@@ -613,12 +613,12 @@ class TestClean:
613
613
  # The generated config's own clean block is read beside the caller's,
614
614
  # and is not changed by it.
615
615
  cfgclean = {"keys": "zzsens", "values": CANARY["config"]}
616
- built = util.make_options(util.make_context({
616
+ cfgctx = util.make_context({
617
617
  "utility": util,
618
618
  "config": {"options": {"clean": cfgclean}},
619
619
  "options": {"clean": {"values": CANARY["value"]}},
620
- }, None))
621
- cfgctx = util.make_context({"options": built}, None)
620
+ }, None)
621
+ cfgctx.options = util.make_options(cfgctx)
622
622
  seeded = util.clean(cfgctx, "config " + CANARY["config"] + " caller " + CANARY["value"])
623
623
  sinks.append(("config-clean", seeded))
624
624
  assert seeded == "config " + MASK + " caller " + MASK, seeded
@@ -658,6 +658,13 @@ class TestClean:
658
658
  falias = fclean(froot, "alias PLAINALIAS-m2n4b6v8")
659
659
  frule = fclean(froot, "rule PLAINRULE-k7j5h3g1")
660
660
 
661
+ # The clean registry holds every credential in plain form, and is no option.
662
+ held = _construct({"apikey": CANARY["apikey"]}).options_map()
663
+ optsjson = json.dumps(held, default=str)
664
+ assert held.get("apikey") == CANARY["apikey"], optsjson
665
+ assert _leaks(optsjson.replace(json.dumps(CANARY["apikey"]), "", 1)) == [], \\
666
+ "options_map() carries a credential beyond the option the caller set: " + optsjson
667
+
661
668
  leaked = [(name, _leaks(text)) for name, text in sinks]
662
669
  leaked = [(name, found) for name, found in leaked if 0 < len(found)]
663
670
 
@@ -548,12 +548,12 @@ class ${Name}CleanTest < Minitest::Test
548
548
  # and is not changed by it.
549
549
  util = hooked.get_utility
550
550
  cfgclean = { "keys" => "zzsens", "values" => CANARY["config"] }
551
- built = util.make_options.call(util.make_context.call({
551
+ cfgctx = util.make_context.call({
552
552
  "utility" => util,
553
553
  "config" => { "options" => { "clean" => cfgclean } },
554
554
  "options" => { "clean" => { "values" => CANARY["value"] } },
555
- }, nil))
556
- cfgctx = util.make_context.call({ "options" => built }, nil)
555
+ }, nil)
556
+ cfgctx.options = util.make_options.call(cfgctx)
557
557
  seeded = util.clean.call(cfgctx, "config #{CANARY['config']} caller #{CANARY['value']}")
558
558
  sinks << { "name" => "config-clean", "text" => seeded.to_s }
559
559
  assert_equal "config #{MASK} caller #{MASK}", seeded
@@ -592,6 +592,13 @@ class ${Name}CleanTest < Minitest::Test
592
592
  falias = fclean.call(featured.get_root_ctx, "alias PLAINALIAS-m2n4b6v8")
593
593
  frule = fclean.call(featured.get_root_ctx, "rule PLAINRULE-k7j5h3g1")
594
594
 
595
+ # The clean registry holds every credential in plain form, and is no option.
596
+ held = construct({ "apikey" => CANARY["apikey"] }).options_map
597
+ heldjson = JSON.generate(held) rescue held.inspect
598
+ assert_equal CANARY["apikey"], held["apikey"]
599
+ assert_equal [], Sweep.leaks(heldjson.sub(CANARY["apikey"].to_json, "")),
600
+ "options_map carries a credential beyond the option the caller set: #{heldjson}"
601
+
595
602
  leaked = sinks
596
603
  .map { |s| [s["name"], Sweep.leaks(s["text"])] }
597
604
  .reject { |_, found| found.empty? }
@@ -870,6 +870,16 @@ fn clean_no_credential_leaves_the_sdk_in_any_form() {
870
870
  assert_eq!(getp(&raw, "ok"), Value::Bool(false), "a transport failure should fail direct()");
871
871
  push_value(&sinks, "direct", &raw);
872
872
 
873
+ // The clean registry holds every credential in plain form, and is no option.
874
+ let held = construct(jo(vec![("apikey", Value::str(CANARY_APIKEY))])).options_map();
875
+ let heldjson = vs::jsonify(&held, None);
876
+ assert_eq!(getp(&held, "apikey"), Value::str(CANARY_APIKEY));
877
+ assert!(
878
+ leaks(&heldjson.replacen(CANARY_APIKEY, "", 1)).is_empty(),
879
+ "options_map carries a credential beyond the option the caller set: {}",
880
+ heldjson
881
+ );
882
+
873
883
  let leaked: Vec<String> = sinks
874
884
  .borrow()
875
885
  .iter()
@@ -106,6 +106,32 @@ object SdkCleanTestMain {
106
106
  private def I(n: Int): java.lang.Integer = java.lang.Integer.valueOf(n)
107
107
  private def B(b: Boolean): java.lang.Boolean = java.lang.Boolean.valueOf(b)
108
108
 
109
+ // Every string a value holds, private fields included, as a debugger shows them.
110
+ private def dump(v: Any, depth: Int = 0): String =
111
+ if (v == null || 12 < depth) ""
112
+ else v match {
113
+ case s: String => s
114
+ case m: JMap[_, _] =>
115
+ val out = new StringBuilder("{")
116
+ val it = m.entrySet().iterator()
117
+ while (it.hasNext) {
118
+ val e = it.next()
119
+ out.append(dump(e.getKey, depth + 1)).append('=').append(dump(e.getValue, depth + 1)).append(' ')
120
+ }
121
+ out.append('}').toString
122
+ case l: java.lang.Iterable[_] =>
123
+ val out = new StringBuilder("[")
124
+ val it = l.iterator()
125
+ while (it.hasNext) out.append(dump(it.next(), depth + 1)).append(' ')
126
+ out.append(']').toString
127
+ case _ if v.getClass.getName.startsWith("java.") || v.getClass.getName.startsWith("scala.") =>
128
+ String.valueOf(v)
129
+ case _ =>
130
+ v.getClass.getDeclaredFields.filter(f => !Modifier.isStatic(f.getModifiers)).map { f =>
131
+ try { f.trySetAccessible(); dump(f.get(v), depth + 1) } catch { case _: Exception => "" }
132
+ }.mkString("{", " ", "}")
133
+ }
134
+
109
135
  private def hasFeature(name: String): Boolean = {
110
136
  val fm = Helpers.toMapAny(Config.sharedConfig().get("feature"))
111
137
  fm != null && fm.get(name) != null
@@ -597,6 +623,14 @@ object SdkCleanTestMain {
597
623
  val coded = probe.getUtility().clean(probe.getRootCtx(), new SdkError("code_" + CANARY_VALUE, "coded", null))
598
624
  collect(sinks, "coded", coded)
599
625
 
626
+ // The clean registry holds every credential in plain form, and is no option.
627
+ val held = construct(om("apikey" -> CANARY_APIKEY)).optionsMap()
628
+ val helddump = dump(held)
629
+ rep.check("clean.options-apikey", CANARY_APIKEY == held.get("apikey"), "optionsMap lost the caller's apikey")
630
+ rep.check("clean.options-registry",
631
+ leaks(helddump.replaceFirst(java.util.regex.Pattern.quote(CANARY_APIKEY), "")).isEmpty,
632
+ "optionsMap carries a credential beyond the option the caller set: " + helddump)
633
+
600
634
  val all = sinks.toArray(new Array[Sink](0)).toList
601
635
  val leaked = all.map(s => (s.name, leaks(s.text))).filter(l => l._2.nonEmpty)
602
636
 
@@ -659,9 +659,9 @@ ${candidateLines}
659
659
  // and is not changed by it.
660
660
  let config = vm(("options", .map(vm(("clean", .map(vm(
661
661
  ("keys", .string("zzsens")), ("values", .string(canaryConfig)))))))))
662
- let built = makeOptionsUtil(Context(
663
- ["config": config, "options": vm(("clean", .map(vm(("values", .string(canaryValue))))))], nil))
664
- let cfgctx = Context(["options": built], nil)
662
+ let cfgctx = Context(
663
+ ["config": config, "options": vm(("clean", .map(vm(("values", .string(canaryValue))))))], nil)
664
+ cfgctx.options = makeOptionsUtil(cfgctx)
665
665
  let seeded = cleanUtil(cfgctx, .string("config " + canaryConfig + " caller " + canaryValue)).asString ?? ""
666
666
  box.sinks.append(Sink(name: "config-clean", text: seeded))
667
667
  XCTAssertEqual(seeded, "config " + mask + " caller " + mask)
@@ -710,7 +710,8 @@ ${candidateLines}
710
710
  ("rules", .map(vm(("zztoken.load", .string("PLAINRULE-k7j5h3g1")))))))),
711
711
  ("test", .map(vm(("active", .bool(false)), ("entity", .map(record)))))))
712
712
  featopts.entries["entity"] = .map(alias)
713
- let fctx = Context(["options": makeOptionsUtil(Context(["options": featopts], nil))], nil)
713
+ let fctx = Context(["options": featopts], nil)
714
+ fctx.options = makeOptionsUtil(fctx)
714
715
  let fplain = cleanUtil(fctx, .string("kind PLAINSETTING-q8w2e4r6")).asString
715
716
  let ftoken = cleanUtil(fctx, .string("token FEATTOKEN-z9y8x7w6")).asString
716
717
  let frecord = cleanUtil(fctx, .string("record PLAINRECORD-t5r3e1w9")).asString
@@ -724,6 +725,16 @@ ${candidateLines}
724
725
  let rawerr = gp(raw, "err").asNative as? ${Name}Error
725
726
  box.sinks += ${Name}CleanTest.formsOf("direct", rawerr)
726
727
 
728
+ // The clean registry holds every credential in plain form, and is no option.
729
+ let heldopts = VMap()
730
+ heldopts.entries["apikey"] = .string(canaryApikey)
731
+ let held = ${Name}SDK(${Name}CleanTest.offline(heldopts)).optionsMap()
732
+ var helddump = ""
733
+ dump(held, to: &helddump, maxDepth: 16)
734
+ XCTAssertEqual(gp(held, "apikey"), .string(canaryApikey))
735
+ let heldrest = helddump.range(of: canaryApikey).map { helddump.replacingCharacters(in: $0, with: "") } ?? helddump
736
+ XCTAssertEqual(leaks(heldrest), [], "optionsMap carries a credential beyond the option the caller set: " + helddump)
737
+
727
738
  let leaked = box.sinks
728
739
  .map { (name: $0.name, found: leaks($0.text)) }
729
740
  .filter { !$0.found.isEmpty }
@@ -798,7 +809,7 @@ ${candidateLines}
798
809
 
799
810
  func testARegisteredValueUsedAsAPropertyNameIsMaskedCollisionsKept() {
800
811
  let cfg = makeCleanConfig(gp(SdkSchema.optspec, "clean"))
801
- let ctx = Context(["options": vm(("__derived__", .map(vm(("clean", .nat(cfg))))))], nil)
812
+ let ctx = Context(["cleancfg": cfg], nil)
802
813
  cleanAddUtil(ctx, .string("ZZVAL-abc123"))
803
814
  cleanAddUtil(ctx, .string("ZZVAL-xyz789"))
804
815
  let src = vm(("ZZVAL-abc123", .int(1)), ("ZZVAL-xyz789", .int(2)), ("plain", .int(3)))
@@ -814,7 +825,7 @@ ${candidateLines}
814
825
 
815
826
  func testCleanAddSensitiveRegistersEveryScalarUnderASensitiveName() {
816
827
  let cfg = makeCleanConfig(gp(SdkSchema.optspec, "clean"))
817
- let ctx = Context(["options": vm(("__derived__", .map(vm(("clean", .nat(cfg))))))], nil)
828
+ let ctx = Context(["cleancfg": cfg], nil)
818
829
  cleanAddSensitiveUtil(ctx, .map(vm(
819
830
  ("apikey", .map(vm(("value", .string("NESTED-SECRET-1"))))),
820
831
  ("headers", .map(vm(("X-Api-Token", .list(VList([.string("LISTED-SECRET-2")])))))),
@@ -912,8 +923,7 @@ ${candidateLines}
912
923
 
913
924
  func testBytesAreDescribedWhereverTheySit() {
914
925
  let cfg = makeCleanConfig(gp(SdkSchema.optspec, "clean"))
915
- let options = vm(("__derived__", .map(vm(("clean", .nat(cfg))))))
916
- let ctx = Context(["options": options], nil)
926
+ let ctx = Context(["cleancfg": cfg], nil)
917
927
  let bytes = ${Name}CleanTest.described(canaryContentBytes.count, nil)
918
928
 
919
929
  let live = Value.nat(Data(canaryContentBytes))
@@ -934,13 +944,13 @@ ${candidateLines}
934
944
  XCTAssertEqual(cleanUtil(ctx, .string(canaryContent)), .string(canaryContent))
935
945
 
936
946
  let offcfg = makeCleanConfig(.map(vm(("active", .bool(false)))))
937
- let offctx = Context(["options": vm(("__derived__", .map(vm(("clean", .nat(offcfg))))))], nil)
947
+ let offctx = Context(["cleancfg": offcfg], nil)
938
948
  XCTAssertEqual(cleanUtil(offctx, live), live)
939
949
 
940
950
  // By the model: only at a point marked binary is a string body bytes.
941
951
  func cleanedAt(_ declared: VMap, _ body: Value) -> Value {
942
952
  let pctx = Context([
943
- "options": options,
953
+ "cleancfg": cfg,
944
954
  "point": vm(("body", .map(declared))),
945
955
  "spec": Spec(vm(("body", body))),
946
956
  "reqdata": vm(("$body", body)),
@@ -472,9 +472,10 @@ describe('clean', () => {
472
472
  // and is not changed by it.
473
473
  const util: any = makeSdk(SCENARIOS[0], sinks).utility()
474
474
  const cfgclean = { keys: 'zzsens', values: CANARY.config }
475
- const built = util.makeOptions({ utility: util, config: { options: { clean: cfgclean } },
476
- options: { clean: { values: CANARY.value } } })
477
- const seeded = util.clean({ options: built }, 'config ' + CANARY.config + ' caller ' + CANARY.value)
475
+ const built: any = { utility: util, config: { options: { clean: cfgclean } },
476
+ options: { clean: { values: CANARY.value } } }
477
+ built.options = util.makeOptions(built)
478
+ const seeded = util.clean(built, 'config ' + CANARY.config + ' caller ' + CANARY.value)
478
479
  sinks.push({ name: 'config-clean', text: seeded })
479
480
 
480
481
  // A feature's name is not a field name: only the sensitive names inside
@@ -487,7 +488,7 @@ describe('clean', () => {
487
488
  rbac: { active: false, rules: { 'zztoken.load': 'PLAINRULE-k7j5h3g1' } },
488
489
  test: { active: false, entity: { zztoken: { ZZTOKEN01: { note: 'PLAINRECORD-t5r3e1w9' } } } },
489
490
  }, entity: { zztoken: { alias: { zzkey: 'PLAINALIAS-m2n4b6v8' } } } })
490
- const fctx = { options: featured._options }
491
+ const fctx = featured._rootctx
491
492
 
492
493
  // The raw path returns its failure rather than throwing it, a
493
494
  // DOMException's included.
@@ -499,6 +500,13 @@ describe('clean', () => {
499
500
  ok(false === raw.ok && null != raw.err, 'a transport failure should fail direct()')
500
501
  sinks.push(...forms('direct', raw.err))
501
502
 
503
+ // The clean registry holds every credential in plain form, and is no option.
504
+ const held = construct({ apikey: CANARY.apikey }).options()
505
+ const optsjson = JSON.stringify(held)
506
+ equal(held.apikey, CANARY.apikey)
507
+ deepStrictEqual(leaks(optsjson.replace(JSON.stringify(CANARY.apikey), '')), [],
508
+ 'options() carries a credential beyond the option the caller set: ' + optsjson)
509
+
502
510
  const leaked = sinks
503
511
  .map((s) => ({ name: s.name, found: leaks(s.text) }))
504
512
  .filter((s) => 0 < s.found.length)
@@ -532,7 +540,7 @@ describe('clean', () => {
532
540
  equal(header(notfound.spec.headers, 'x-custom-token'), MASK)
533
541
 
534
542
  equal(seeded, 'config ' + MASK + ' caller ' + MASK)
535
- deepStrictEqual(util.clean({ options: built }, { my_zzsens: 'x', other: 'y' }), { my_zzsens: MASK, other: 'y' })
543
+ deepStrictEqual(util.clean(built, { my_zzsens: 'x', other: 'y' }), { my_zzsens: MASK, other: 'y' })
536
544
  deepStrictEqual(cfgclean, { keys: 'zzsens', values: CANARY.config })
537
545
 
538
546
  equal(featured.utility().clean(fctx, 'kind PLAINSETTING-q8w2e4r6'), 'kind PLAINSETTING-q8w2e4r6')
@@ -768,6 +768,18 @@ test "clean: no credential leaves the SDK in any form" {
768
768
  try testing.expect(h.getp(direct, "ok") == .bool and !h.getp(direct, "ok").bool);
769
769
  sinks.value("direct", direct);
770
770
 
771
+ // The clean registry holds every credential in plain form, and is no option.
772
+ const held = sdk.SDK.new(offline(h.jo(&.{
773
+ .{ "apikey", h.vstr(CANARY_APIKEY) },
774
+ .{ "system", h.jo(&.{.{ "fetch", Transport.make(.ok) }}) },
775
+ }))).ok.options_map();
776
+ const heldjson = h.jsonify_compact(held);
777
+ try testing.expectEqualStrings(CANARY_APIKEY, h.get_str(held, "apikey") orelse "");
778
+ const heldat = std.mem.indexOf(u8, heldjson, CANARY_APIKEY) orelse heldjson.len;
779
+ const heldrest = fmt("{s}{s}", .{ heldjson[0..heldat], heldjson[@min(heldjson.len, heldat + CANARY_APIKEY.len)..] });
780
+ if (0 < leaks(heldrest).len) std.debug.print("clean: options_map carries {s}\\n", .{heldjson});
781
+ try testing.expect(0 == leaks(heldrest).len);
782
+
771
783
  var leaked: usize = 0;
772
784
  for (sinks.items.items) |s| {
773
785
  const found = leaks(s.text);
@@ -142,6 +142,7 @@ Context* context_new(CtxSpec cs, Context* basectx) {
142
142
  ctx->config = cs.config ? cs.config : (basectx ? basectx->config : voxgig_new_undef());
143
143
  ctx->entopts = cs.entopts ? cs.entopts : (basectx ? basectx->entopts : voxgig_new_undef());
144
144
  ctx->options = cs.options ? cs.options : (basectx ? basectx->options : voxgig_new_undef());
145
+ ctx->cleancfg = cs.cleancfg ? cs.cleancfg : (basectx ? basectx->cleancfg : voxgig_new_undef());
145
146
  ctx->entity = cs.entity ? cs.entity : (basectx ? basectx->entity : NULL);
146
147
  if (cs.shared) {
147
148
  ctx->shared = cs.shared;
@@ -393,6 +393,7 @@ typedef struct CtxSpec {
393
393
  voxgig_value* config;
394
394
  voxgig_value* entopts;
395
395
  voxgig_value* options;
396
+ voxgig_value* cleancfg;
396
397
  Entity* entity;
397
398
  voxgig_value* shared;
398
399
  voxgig_value* data;
@@ -433,6 +434,8 @@ struct Context {
433
434
  voxgig_value* config;
434
435
  voxgig_value* entopts;
435
436
  voxgig_value* options;
437
+ // The clean registry, beside the options rather than in them.
438
+ voxgig_value* cleancfg;
436
439
  Response* response;
437
440
  SdkResult* result;
438
441
  Spec* spec;
@@ -524,15 +527,15 @@ void** feature_plugins(const char* name, size_t* n);
524
527
  voxgig_value* clean_util(Context* ctx, voxgig_value* val);
525
528
  char* clean_str(Context* ctx, const char* val); // returns malloc'd
526
529
  // Register a secret value the SDK will mask from now on, through a context
527
- // or straight against the options map a feature holds.
530
+ // or straight against the clean registry a feature holds.
528
531
  void clean_add_util(Context* ctx, const char* value);
529
- void clean_add_opts(voxgig_value* options, const char* value);
532
+ void clean_add_cfg(voxgig_value* cleancfg, const char* value);
530
533
  bool clean_key_util(Context* ctx, const char* key);
531
534
  bool clean_secret_util(Context* ctx, const char* key, const char* value);
532
- voxgig_value* clean_opts(voxgig_value* options, voxgig_value* val);
535
+ voxgig_value* clean_cfg(voxgig_value* cleancfg, voxgig_value* val);
533
536
  voxgig_value* clean_make_config(voxgig_value* cleanopts);
534
537
  voxgig_value* clean_split_values(voxgig_value* values);
535
- void clean_add_sensitive_opts(voxgig_value* options, voxgig_value* val);
538
+ void clean_add_sensitive_cfg(voxgig_value* cleancfg, voxgig_value* val);
536
539
  void clean_add_sensitive_util(Context* ctx, voxgig_value* val);
537
540
  void clean_error_util(Context* ctx, PNError* err);
538
541
  char* clean_base64(const char* in); // returns malloc'd
@@ -1,18 +1,19 @@
1
- // Response caching for safe (read) requests (mirrors feature/cache.rs).
2
- // Wraps the active transport and serves a fresh cached snapshot instead of
3
- // hitting the network when the same method+URL was fetched within `ttl` ms
4
- // (default: 5000). Only successful (2xx) responses to cacheable methods
5
- // (default: GET) are stored, keyed by method+URL. The cache is bounded (`max`
6
- // entries, default 256, oldest evicted first) and every hit/miss/bypass is
7
- // counted. Bodies are snapshotted on capture so both the current caller and
8
- // later hits can re-read the JSON body repeatedly.
1
+ // Response caching for safe (read) requests, per client (mirrors
2
+ // feature/cache.rs): see the features reference for the key, the write
3
+ // invalidation and the Cache-Control rules.
4
+
5
+ // C has no HMAC: SipHash-2-4 under a random per-client key separates header
6
+ // sets within one store without holding a header value. It is not a
7
+ // cryptographic commitment.
9
8
 
10
9
  #include "sdk.h"
11
10
 
12
11
  #include <ctype.h>
12
+ #include <stdint.h>
13
13
  #include <stdio.h>
14
14
  #include <stdlib.h>
15
15
  #include <string.h>
16
+ #include <time.h>
16
17
 
17
18
  typedef struct {
18
19
  // string-keyed store (voxgig_map, insertion-ordered) mapping
@@ -20,6 +21,7 @@ typedef struct {
20
21
  voxgig_value* store;
21
22
  // FIFO insertion order of keys (may contain duplicates, mirrors rust Vec).
22
23
  voxgig_value* order;
24
+ uint8_t secret[16];
23
25
 
24
26
  // Activity tracking (mirrors the ts client._cache record).
25
27
  int64_t hit;
@@ -86,6 +88,218 @@ static bool option_list_match(voxgig_value* options, const char* key,
86
88
  return false;
87
89
  }
88
90
 
91
+ #define SIP_ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
92
+ #define SIP_ROUND(v0, v1, v2, v3) do { \
93
+ v0 += v1; v1 = SIP_ROTL(v1, 13); v1 ^= v0; v0 = SIP_ROTL(v0, 32); \
94
+ v2 += v3; v3 = SIP_ROTL(v3, 16); v3 ^= v2; \
95
+ v0 += v3; v3 = SIP_ROTL(v3, 21); v3 ^= v0; \
96
+ v2 += v1; v1 = SIP_ROTL(v1, 17); v1 ^= v2; v2 = SIP_ROTL(v2, 32); } while (0)
97
+
98
+ static uint64_t sip_le64(const uint8_t* p) {
99
+ uint64_t v = 0;
100
+ for (int i = 7; i >= 0; i--) v = (v << 8) | p[i];
101
+ return v;
102
+ }
103
+
104
+ uint64_t feature_cache_siphash24(const uint8_t key[16], const uint8_t* in, size_t len) {
105
+ uint64_t k0 = sip_le64(key), k1 = sip_le64(key + 8);
106
+ uint64_t v0 = 0x736f6d6570736575ULL ^ k0, v1 = 0x646f72616e646f6dULL ^ k1;
107
+ uint64_t v2 = 0x6c7967656e657261ULL ^ k0, v3 = 0x7465646279746573ULL ^ k1;
108
+ size_t full = len - (len % 8);
109
+ for (size_t i = 0; i < full; i += 8) {
110
+ uint64_t m = sip_le64(in + i);
111
+ v3 ^= m;
112
+ SIP_ROUND(v0, v1, v2, v3);
113
+ SIP_ROUND(v0, v1, v2, v3);
114
+ v0 ^= m;
115
+ }
116
+ uint64_t b = ((uint64_t)len) << 56;
117
+ for (size_t i = 0; i < len % 8; i++) b |= ((uint64_t)in[full + i]) << (8 * i);
118
+ v3 ^= b;
119
+ SIP_ROUND(v0, v1, v2, v3);
120
+ SIP_ROUND(v0, v1, v2, v3);
121
+ v0 ^= b;
122
+ v2 ^= 0xff;
123
+ for (int i = 0; i < 4; i++) SIP_ROUND(v0, v1, v2, v3);
124
+ return v0 ^ v1 ^ v2 ^ v3;
125
+ }
126
+
127
+ // The system's random source where it has one; otherwise rand(), which is
128
+ // not a cryptographic generator.
129
+ static void cache_secret(uint8_t out[16]) {
130
+ size_t got = 0;
131
+ FILE* f = fopen("/dev/urandom", "rb");
132
+ if (f) {
133
+ got = fread(out, 1, 16, f);
134
+ fclose(f);
135
+ }
136
+ uint64_t mix = (uint64_t)time(NULL) ^ (uint64_t)(uintptr_t)out;
137
+ for (size_t i = got; i < 16; i++) out[i] = (uint8_t)((rand() ^ (int)(mix >> (i % 8 * 8))) & 0xff);
138
+ }
139
+
140
+ static char* header_text(voxgig_value* v) {
141
+ char b[64];
142
+ if (voxgig_is_string(v)) return strdup(voxgig_as_string(v));
143
+ if (voxgig_is_int(v)) {
144
+ snprintf(b, sizeof(b), "%lld", (long long)voxgig_as_int(v));
145
+ return strdup(b);
146
+ }
147
+ if (voxgig_is_double(v)) {
148
+ double d = voxgig_as_double(v);
149
+ if (d == (double)(long long)d) snprintf(b, sizeof(b), "%lld", (long long)d);
150
+ else snprintf(b, sizeof(b), "%g", d);
151
+ return strdup(b);
152
+ }
153
+ if (voxgig_is_bool(v)) return strdup(voxgig_as_bool(v) ? "true" : "false");
154
+ return NULL;
155
+ }
156
+
157
+ typedef struct {
158
+ char* name;
159
+ char* value;
160
+ } HeaderPair;
161
+
162
+ static int pair_cmp(const void* a, const void* b) {
163
+ const HeaderPair* x = (const HeaderPair*)a;
164
+ const HeaderPair* y = (const HeaderPair*)b;
165
+ int c = strcmp(x->name, y->name);
166
+ return c != 0 ? c : strcmp(x->value, y->value);
167
+ }
168
+
169
+ static char* digest(const uint8_t secret[16], voxgig_value* headers) {
170
+ size_t n = 0, total = 0;
171
+ HeaderPair* pairs = NULL;
172
+ if (voxgig_is_map(headers)) {
173
+ voxgig_map* hm = voxgig_as_map(headers);
174
+ pairs = (HeaderPair*)calloc(hm->len + 1, sizeof(HeaderPair));
175
+ for (size_t i = 0; i < hm->len; i++) {
176
+ char* value = header_text(hm->entries[i].value);
177
+ if (!value) continue;
178
+ pairs[n].name = str_lower(hm->entries[i].key);
179
+ pairs[n].value = value;
180
+ total += strlen(pairs[n].name) + strlen(value) + 2;
181
+ n++;
182
+ }
183
+ qsort(pairs, n, sizeof(HeaderPair), pair_cmp);
184
+ }
185
+ char* text = (char*)calloc(total + 1, 1);
186
+ size_t at = 0;
187
+ for (size_t i = 0; i < n; i++) {
188
+ at += (size_t)sprintf(text + at, "%s:%s\n", pairs[i].name, pairs[i].value);
189
+ free(pairs[i].name);
190
+ free(pairs[i].value);
191
+ }
192
+ free(pairs);
193
+ char* hex = (char*)malloc(17);
194
+ snprintf(hex, 17, "%016llx", (unsigned long long)feature_cache_siphash24(secret, (const uint8_t*)text, at));
195
+ free(text);
196
+ return hex;
197
+ }
198
+
199
+ static char* url_scope(const char* url) {
200
+ size_t n = strcspn(url, "?#");
201
+ while (n > 0 && url[n - 1] == '/') n--;
202
+ char* r = (char*)malloc(n + 1);
203
+ memcpy(r, url, n);
204
+ r[n] = '\0';
205
+ return r;
206
+ }
207
+
208
+ static char* url_parent(const char* scope) {
209
+ const char* scheme = strstr(scope, "://");
210
+ size_t root = 0;
211
+ if (scheme) {
212
+ const char* slash = strchr(scheme + 3, '/');
213
+ if (!slash) return strdup(scope);
214
+ root = (size_t)(slash - scope);
215
+ }
216
+ const char* last = strrchr(scope, '/');
217
+ size_t cut = last ? (size_t)(last - scope) : 0;
218
+ if (cut < root) cut = root;
219
+ char* r = (char*)malloc(cut + 1);
220
+ memcpy(r, scope, cut);
221
+ r[cut] = '\0';
222
+ return r;
223
+ }
224
+
225
+ static bool scope_under(const char* scope, const char* prefix) {
226
+ size_t n = strlen(prefix);
227
+ return strncmp(scope, prefix, n) == 0 && (scope[n] == '\0' || scope[n] == '/');
228
+ }
229
+
230
+ static void drop_entry(CacheTrack* t, const char* key) {
231
+ voxgig_list* order = voxgig_as_list(t->order);
232
+ for (size_t i = order->len; i > 0; i--) {
233
+ if (strcmp(voxgig_as_string(order->items[i - 1]), key) == 0) voxgig_list_erase(order, i - 1);
234
+ }
235
+ voxgig_map_erase(voxgig_as_map(t->store), key);
236
+ }
237
+
238
+ static void invalidate(CacheTrack* t, const char* url) {
239
+ char* path = url_scope(url);
240
+ char* collection = url_parent(path);
241
+ voxgig_map* store = voxgig_as_map(t->store);
242
+ for (size_t i = store->len; i > 0; i--) {
243
+ const char* scope = get_str(store->entries[i - 1].value, "scope");
244
+ if (scope && (scope_under(scope, path) || strcmp(scope, collection) == 0)) {
245
+ char* key = strdup(store->entries[i - 1].key);
246
+ drop_entry(t, key);
247
+ free(key);
248
+ }
249
+ }
250
+ free(collection);
251
+ free(path);
252
+ }
253
+
254
+ // The configured ttl capped by the response's Cache-Control; zero when the
255
+ // response may not be stored.
256
+ static int64_t lifetime(voxgig_value* headers, int64_t ttl) {
257
+ char* control = NULL;
258
+ if (voxgig_is_map(headers)) {
259
+ voxgig_map* hm = voxgig_as_map(headers);
260
+ for (size_t i = 0; i < hm->len; i++) {
261
+ if (strcasecmp(hm->entries[i].key, "cache-control") == 0) {
262
+ free(control);
263
+ char* raw = header_text(hm->entries[i].value);
264
+ control = raw ? str_lower(raw) : NULL;
265
+ free(raw);
266
+ }
267
+ }
268
+ }
269
+ int64_t life = ttl;
270
+ if (!control) return life;
271
+ for (char* part = control; part; ) {
272
+ char* comma = strchr(part, ',');
273
+ if (comma) *comma = '\0';
274
+ char* next = comma ? comma + 1 : NULL;
275
+ while (isspace((unsigned char)*part)) part++;
276
+ size_t n = strlen(part);
277
+ while (n > 0 && isspace((unsigned char)part[n - 1])) part[--n] = '\0';
278
+ if (strcmp(part, "no-store") == 0 || strcmp(part, "private") == 0 ||
279
+ strcmp(part, "no-cache") == 0) {
280
+ free(control);
281
+ return 0;
282
+ }
283
+ if (strncmp(part, "max-age", 7) == 0) {
284
+ char* v = part + 7;
285
+ while (isspace((unsigned char)*v)) v++;
286
+ if (*v != '=') { part = next; continue; }
287
+ v++;
288
+ while (isspace((unsigned char)*v)) v++;
289
+ if (*v == '"') v++;
290
+ char* end = v;
291
+ while (isdigit((unsigned char)*end)) end++;
292
+ if (end != v && end - v < 16 && (*end == '\0' || (*end == '"' && end[1] == '\0'))) {
293
+ int64_t secs = (int64_t)strtoll(v, NULL, 10);
294
+ if (secs * 1000 < life) life = secs * 1000;
295
+ }
296
+ }
297
+ part = next;
298
+ }
299
+ free(control);
300
+ return life;
301
+ }
302
+
89
303
  // A body the transport could not read as JSON passes through, for the operation to report.
90
304
  static bool storable(voxgig_value* res) {
91
305
  int64_t status;
@@ -182,13 +396,18 @@ static voxgig_value* through(Fetcher* self, Context* ctx, const char* url,
182
396
  static const char* MDEF[] = {"GET"};
183
397
  bool cacheable = option_list_match(options, "methods", method, MDEF, 1, true);
184
398
  if (!cacheable) {
399
+ bool safe = strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0;
185
400
  free(method);
186
- return st->inner->fn(st->inner, ctx, url, fetchdef, err);
401
+ voxgig_value* passed = st->inner->fn(st->inner, ctx, url, fetchdef, err);
402
+ if (!safe) invalidate(track, url);
403
+ return passed;
187
404
  }
188
405
 
189
- size_t klen = strlen(method) + 1 + strlen(url) + 1;
406
+ char* hdigest = digest(track->secret, getp(fetchdef, "headers"));
407
+ size_t klen = strlen(method) + 1 + strlen(url) + 1 + strlen(hdigest) + 1;
190
408
  char* key = (char*)malloc(klen);
191
- snprintf(key, klen, "%s %s", method, url);
409
+ snprintf(key, klen, "%s %s %s", method, url, hdigest);
410
+ free(hdigest);
192
411
  free(method);
193
412
 
194
413
  int64_t now = fopt_now_call(options);
@@ -210,11 +429,21 @@ static voxgig_value* through(Fetcher* self, Context* ctx, const char* url,
210
429
 
211
430
  if (!e && storable(out) && json(out)) {
212
431
  voxgig_value* snap = snapshot(out);
213
- int64_t ttl = fopt_int(options, "ttl", 5000);
432
+ int64_t ttl = lifetime(getp(snap, "headers"), fopt_int(options, "ttl", 5000));
433
+ if (ttl <= 0) {
434
+ track->bypass += 1;
435
+ free(key);
436
+ *err = NULL;
437
+ return replay(snap);
438
+ }
439
+ drop_entry(track, key);
214
440
  evict(track, options);
215
- voxgig_value* newentry = cmap(2,
441
+ char* scope = url_scope(url);
442
+ voxgig_value* newentry = cmap(3,
216
443
  "expiry", v_num((double)(now + ttl)),
444
+ "scope", v_str(scope),
217
445
  "snap", snap);
446
+ free(scope);
218
447
  voxgig_map_set(voxgig_as_map(track->store), key, newentry);
219
448
  voxgig_list_push(voxgig_as_list(track->order), v_str(key));
220
449
  track->miss += 1;
@@ -244,6 +473,7 @@ static void cache_init(Feature* f, Context* ctx, voxgig_value* options) {
244
473
  st->inner = util->fetcher;
245
474
  st->options = options;
246
475
  st->track = cf->track;
476
+ cache_secret(cf->track->secret);
247
477
 
248
478
  Fetcher* wrapped = (Fetcher*)calloc(1, sizeof(Fetcher));
249
479
  wrapped->fn = through;