@voxgig/sdkgen 4.50.0 → 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.
- package/bin/voxgig-sdkgen +1 -1
- package/package.json +1 -1
- package/project/.sdk/model/feature/cache.aontu +6 -4
- package/project/.sdk/model/target/lua.aontu +6 -0
- package/project/.sdk/src/cmp/c/Config_c.ts +3 -134
- package/project/.sdk/src/cmp/c/TestClean_c.ts +14 -0
- package/project/.sdk/src/cmp/clojure/TestClean_clojure.ts +10 -5
- package/project/.sdk/src/cmp/cpp/TestClean_cpp.ts +13 -0
- package/project/.sdk/src/cmp/csharp/TestClean_csharp.ts +9 -0
- package/project/.sdk/src/cmp/elixir/TestClean_elixir.ts +11 -3
- package/project/.sdk/src/cmp/go/TestClean_go.ts +39 -3
- package/project/.sdk/src/cmp/java/TestClean_java.ts +8 -0
- package/project/.sdk/src/cmp/js/TestClean_js.ts +13 -5
- package/project/.sdk/src/cmp/kotlin/TestClean_kotlin.ts +22 -0
- package/project/.sdk/src/cmp/lua/TestClean_lua.ts +12 -3
- package/project/.sdk/src/cmp/ocaml/TestClean_ocaml.ts +18 -4
- package/project/.sdk/src/cmp/perl/TestClean_perl.ts +12 -3
- package/project/.sdk/src/cmp/php/TestClean_php.ts +10 -3
- package/project/.sdk/src/cmp/py/TestClean_py.ts +10 -3
- package/project/.sdk/src/cmp/rb/TestClean_rb.ts +10 -3
- package/project/.sdk/src/cmp/rust/TestClean_rust.ts +10 -0
- package/project/.sdk/src/cmp/scala/TestClean_scala.ts +34 -0
- package/project/.sdk/src/cmp/swift/TestClean_swift.ts +20 -10
- package/project/.sdk/src/cmp/ts/TestClean_ts.ts +13 -5
- package/project/.sdk/src/cmp/zig/TestClean_zig.ts +12 -0
- package/project/.sdk/tm/c/core/context.c +1 -0
- package/project/.sdk/tm/c/core/sdk.h +7 -4
- package/project/.sdk/tm/c/feature/cache.c +243 -13
- package/project/.sdk/tm/c/feature/secrets.c +27 -38
- package/project/.sdk/tm/c/tests/feature_test.c +104 -0
- package/project/.sdk/tm/c/utility/clean.c +26 -33
- package/project/.sdk/tm/c/utility/make_options.c +9 -8
- package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +14 -11
- package/project/.sdk/tm/clojure/src/sdk/client.clj +3 -1
- package/project/.sdk/tm/clojure/src/sdk/core.clj +19 -11
- package/project/.sdk/tm/clojure/src/sdk/features.clj +81 -10
- package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +33 -0
- package/project/.sdk/tm/clojure/test/sdk/test/pipeline.clj +1 -0
- package/project/.sdk/tm/cpp/Makefile +5 -0
- package/project/.sdk/tm/cpp/core/types.hpp +11 -4
- package/project/.sdk/tm/cpp/feature/audit.hpp +6 -7
- package/project/.sdk/tm/cpp/feature/cache.hpp +188 -9
- package/project/.sdk/tm/cpp/feature/secrets.hpp +12 -0
- package/project/.sdk/tm/cpp/test/feature_test.cpp +99 -0
- package/project/.sdk/tm/cpp/utility/pipeline.hpp +9 -7
- package/project/.sdk/tm/csharp/core/Context.cs +4 -0
- package/project/.sdk/tm/csharp/feature/CacheFeature.cs +126 -12
- package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +9 -27
- package/project/.sdk/tm/csharp/test/FeatureTest.cs +76 -0
- package/project/.sdk/tm/csharp/utility/Clean.cs +5 -10
- package/project/.sdk/tm/csharp/utility/Fetcher.cs +7 -1
- package/project/.sdk/tm/csharp/utility/MakeOptions.cs +5 -12
- package/project/.sdk/tm/elixir/lib/projectname/context.ex +2 -0
- package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +146 -30
- package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +10 -56
- package/project/.sdk/tm/elixir/lib/projectname/utility.ex +18 -12
- package/project/.sdk/tm/elixir/test/feature_test.exs +61 -0
- package/project/.sdk/tm/go/core/context.go +6 -0
- package/project/.sdk/tm/go/feature/cache_feature.go +131 -4
- package/project/.sdk/tm/go/feature/secrets_feature.go +17 -71
- package/project/.sdk/tm/go/test/feature_corpus_test.go +150 -2
- package/project/.sdk/tm/go/test/feature_test.go +61 -0
- package/project/.sdk/tm/go/utility/clean.go +5 -7
- package/project/.sdk/tm/go/utility/fetcher.go +11 -0
- package/project/.sdk/tm/go/utility/make_options.go +5 -7
- package/project/.sdk/tm/java/core/Context.java +9 -0
- package/project/.sdk/tm/java/feature/CacheFeature.java +123 -17
- package/project/.sdk/tm/java/feature/SecretsFeature.java +9 -92
- package/project/.sdk/tm/java/test/FeatureCorpusTest.java +141 -2
- package/project/.sdk/tm/java/test/FeatureTest.java +60 -0
- package/project/.sdk/tm/java/utility/Clean.java +6 -10
- package/project/.sdk/tm/java/utility/Fetcher.java +5 -1
- package/project/.sdk/tm/java/utility/MakeOptions.java +3 -1
- package/project/.sdk/tm/js/src/Context.js +4 -0
- package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +118 -11
- package/project/.sdk/tm/js/src/feature/cost/CostFeature.js +3 -2
- package/project/.sdk/tm/js/src/feature/metrics/MetricsFeature.js +1 -1
- package/project/.sdk/tm/js/src/feature/paging/PagingFeature.js +4 -4
- package/project/.sdk/tm/js/src/feature/rbac/RbacFeature.js +4 -4
- package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +16 -6
- package/project/.sdk/tm/js/src/feature/validate/ValidateFeature.js +2 -1
- package/project/.sdk/tm/js/src/utility/CleanUtility.js +1 -1
- package/project/.sdk/tm/js/src/utility/MakeOptionsUtility.js +6 -6
- package/project/.sdk/tm/js/test/feature/Corpus.test.js +100 -2
- package/project/.sdk/tm/js/test/feature/harness.js +3 -3
- package/project/.sdk/tm/js/test/pipeline.test.js +1 -1
- package/project/.sdk/tm/kotlin/core/Context.kt +3 -0
- package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +102 -7
- package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +10 -20
- package/project/.sdk/tm/kotlin/test/FeatureTest.kt +59 -0
- package/project/.sdk/tm/kotlin/utility/CoreUtil.kt +5 -5
- package/project/.sdk/tm/kotlin/utility/Fetcher.kt +4 -0
- package/project/.sdk/tm/kotlin/utility/MakeOptions.kt +3 -1
- package/project/.sdk/tm/lua/core/context.lua +6 -0
- package/project/.sdk/tm/lua/feature/cache_feature.lua +212 -15
- package/project/.sdk/tm/lua/feature/secrets_feature.lua +15 -80
- package/project/.sdk/tm/lua/test/feature_test.lua +81 -0
- package/project/.sdk/tm/lua/utility/clean.lua +6 -9
- package/project/.sdk/tm/lua/utility/fetcher.lua +9 -0
- package/project/.sdk/tm/lua/utility/make_options.lua +5 -5
- package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +21 -6
- package/project/.sdk/tm/ocaml/sdk_features.ml +106 -8
- package/project/.sdk/tm/ocaml/sdk_runtime.ml +13 -10
- package/project/.sdk/tm/ocaml/sdk_types.ml +2 -0
- package/project/.sdk/tm/ocaml/test/t_feature.ml +35 -1
- package/project/.sdk/tm/perl/core/context.pm +3 -0
- package/project/.sdk/tm/perl/feature/audit_feature.pm +4 -7
- package/project/.sdk/tm/perl/feature/cache_feature.pm +111 -15
- package/project/.sdk/tm/perl/feature/secrets_feature.pm +13 -18
- package/project/.sdk/tm/perl/t/feature.t +66 -0
- package/project/.sdk/tm/perl/t/feature_corpus.t +80 -4
- package/project/.sdk/tm/perl/utility/clean.pm +6 -8
- package/project/.sdk/tm/perl/utility/fetcher.pm +4 -0
- package/project/.sdk/tm/perl/utility/make_options.pm +5 -5
- package/project/.sdk/tm/php/core/Context.php +4 -0
- package/project/.sdk/tm/php/feature/CacheFeature.php +98 -13
- package/project/.sdk/tm/php/feature/SecretsFeature.php +18 -15
- package/project/.sdk/tm/php/test/FeatureCorpusTest.php +113 -3
- package/project/.sdk/tm/php/test/FeatureTest.php +68 -0
- package/project/.sdk/tm/php/utility/Clean.php +3 -10
- package/project/.sdk/tm/php/utility/Fetcher.php +8 -3
- package/project/.sdk/tm/php/utility/MakeOptions.php +5 -7
- package/project/.sdk/tm/py/pkg/core/context.py +4 -0
- package/project/.sdk/tm/py/pkg/feature/cache_feature.py +79 -13
- package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +15 -66
- package/project/.sdk/tm/py/pkg/utility/clean.py +4 -10
- package/project/.sdk/tm/py/pkg/utility/make_options.py +8 -9
- package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +23 -9
- package/project/.sdk/tm/py/test/test_feature.py +50 -0
- package/project/.sdk/tm/py/test/test_feature_corpus.py +88 -3
- package/project/.sdk/tm/rb/core/context.rb +4 -1
- package/project/.sdk/tm/rb/feature/audit_feature.rb +6 -5
- package/project/.sdk/tm/rb/feature/cache_feature.rb +75 -11
- package/project/.sdk/tm/rb/feature/secrets_feature.rb +9 -64
- package/project/.sdk/tm/rb/test/feature_corpus_test.rb +85 -3
- package/project/.sdk/tm/rb/test/feature_test.rb +74 -0
- package/project/.sdk/tm/rb/utility/clean.rb +6 -7
- package/project/.sdk/tm/rb/utility/fetcher.rb +6 -0
- package/project/.sdk/tm/rb/utility/make_options.rb +5 -5
- package/project/.sdk/tm/rust/core/context.rs +8 -0
- package/project/.sdk/tm/rust/feature/cache.rs +120 -3
- package/project/.sdk/tm/rust/feature/secrets.rs +36 -98
- package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +3 -3
- package/project/.sdk/tm/rust/tests/feature_test.rs +100 -0
- package/project/.sdk/tm/rust/utility/clean.rs +35 -43
- package/project/.sdk/tm/rust/utility/fetcher.rs +6 -0
- package/project/.sdk/tm/rust/utility/make_options.rs +8 -11
- package/project/.sdk/tm/scala/core/Context.scala +4 -0
- package/project/.sdk/tm/scala/feature/CacheFeature.scala +117 -11
- package/project/.sdk/tm/scala/feature/SecretsFeature.scala +11 -24
- package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +28 -0
- package/project/.sdk/tm/scala/utility/Fetcher.scala +5 -0
- package/project/.sdk/tm/scala/utility/Make.scala +3 -1
- package/project/.sdk/tm/scala/utility/Misc.scala +5 -8
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Context.swift +3 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +122 -11
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +9 -13
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Fetcher.swift +4 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/MakeOptions.swift +5 -4
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/ResultUtil.swift +4 -5
- package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +55 -0
- package/project/.sdk/tm/ts/src/Context.ts +4 -0
- package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +115 -4
- package/project/.sdk/tm/ts/src/feature/cost/CostFeature.ts +3 -2
- package/project/.sdk/tm/ts/src/feature/metrics/MetricsFeature.ts +1 -1
- package/project/.sdk/tm/ts/src/feature/paging/PagingFeature.ts +4 -4
- package/project/.sdk/tm/ts/src/feature/rbac/RbacFeature.ts +4 -4
- package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +14 -2
- package/project/.sdk/tm/ts/src/feature/validate/ValidateFeature.ts +2 -1
- package/project/.sdk/tm/ts/src/utility/CleanUtility.ts +1 -1
- package/project/.sdk/tm/ts/src/utility/MakeOptionsUtility.ts +6 -6
- package/project/.sdk/tm/ts/test/feature/Corpus.test.ts +101 -2
- package/project/.sdk/tm/ts/test/feature/harness.ts +3 -3
- package/project/.sdk/tm/ts/test/pipeline.test.ts +1 -1
- package/project/.sdk/tm/zig/core/context.zig +5 -0
- package/project/.sdk/tm/zig/core/utility.zig +11 -9
- package/project/.sdk/tm/zig/feature/cache.zig +144 -13
- package/project/.sdk/tm/zig/feature/secrets.zig +6 -1
- package/project/.sdk/tm/zig/test/feature_test.zig +65 -0
- 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
|
-
$
|
|
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 =
|
|
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
|
-
|
|
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.
|
|
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
|
-
|
|
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.
|
|
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
|
|
663
|
-
["config": config, "options": vm(("clean", .map(vm(("values", .string(canaryValue))))))], nil)
|
|
664
|
-
|
|
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":
|
|
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(["
|
|
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(["
|
|
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
|
|
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(["
|
|
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
|
-
"
|
|
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 =
|
|
476
|
-
options: { clean: { values: CANARY.value } } }
|
|
477
|
-
|
|
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 =
|
|
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(
|
|
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
|
|
530
|
+
// or straight against the clean registry a feature holds.
|
|
528
531
|
void clean_add_util(Context* ctx, const char* value);
|
|
529
|
-
void
|
|
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*
|
|
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
|
|
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
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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;
|