@voxgig/sdkgen 4.48.0 → 4.49.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 (174) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/dist/tsconfig.tsbuildinfo +1 -1
  3. package/dist/utility.js +9 -1
  4. package/dist/utility.js.map +1 -1
  5. package/package.json +1 -1
  6. package/project/.sdk/model/feature/secrets.aontu +3 -0
  7. package/project/.sdk/src/cmp/c/Config_c.ts +6 -1
  8. package/project/.sdk/src/cmp/c/fragment/Entity.fragment.c +0 -10
  9. package/project/.sdk/src/cmp/clojure/fragment/Entity.fragment.clj +0 -9
  10. package/project/.sdk/src/cmp/csharp/fragment/EntityBase.fragment.cs +0 -11
  11. package/project/.sdk/src/cmp/go/fragment/Entity.fragment.go +0 -7
  12. package/project/.sdk/src/cmp/java/fragment/EntityBase.fragment.java +0 -10
  13. package/project/.sdk/src/cmp/js/fragment/EntityBase.fragment.js +2 -11
  14. package/project/.sdk/src/cmp/kotlin/fragment/EntityBase.fragment.kt +0 -10
  15. package/project/.sdk/src/cmp/lua/fragment/Entity.fragment.lua +0 -11
  16. package/project/.sdk/src/cmp/ocaml/Entity_ocaml.ts +2 -10
  17. package/project/.sdk/src/cmp/perl/fragment/Entity.fragment.pm +0 -13
  18. package/project/.sdk/src/cmp/php/fragment/Entity.fragment.php +0 -10
  19. package/project/.sdk/src/cmp/py/fragment/Entity.fragment.py +0 -10
  20. package/project/.sdk/src/cmp/rb/fragment/Entity.fragment.rb +0 -10
  21. package/project/.sdk/src/cmp/rust/fragment/Entity.fragment.rs +0 -12
  22. package/project/.sdk/src/cmp/scala/fragment/EntityBase.fragment.scala +0 -10
  23. package/project/.sdk/src/cmp/swift/fragment/EntityBase.fragment.swift +0 -9
  24. package/project/.sdk/src/cmp/ts/fragment/EntityBase.fragment.ts +0 -7
  25. package/project/.sdk/src/cmp/zig/fragment/Entity.fragment.zig +1 -14
  26. package/project/.sdk/tm/c/feature/cache.c +16 -1
  27. package/project/.sdk/tm/c/feature/secrets.c +149 -18
  28. package/project/.sdk/tm/c/tests/feature/secrets/secrets_test.c +319 -70
  29. package/project/.sdk/tm/c/tests/feature_test.c +31 -0
  30. package/project/.sdk/tm/c/utility/make_spec.c +1 -1
  31. package/project/.sdk/tm/c/utility/prepare_body.c +64 -1
  32. package/project/.sdk/tm/c/utility/prepare_query.c +11 -6
  33. package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +74 -29
  34. package/project/.sdk/tm/clojure/src/sdk/core.clj +48 -10
  35. package/project/.sdk/tm/clojure/src/sdk/features.clj +16 -2
  36. package/project/.sdk/tm/clojure/test/sdk/test/feature/secrets.clj +191 -48
  37. package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +16 -0
  38. package/project/.sdk/tm/cpp/core/types.hpp +1 -11
  39. package/project/.sdk/tm/cpp/feature/cache.hpp +14 -1
  40. package/project/.sdk/tm/cpp/feature/secrets.hpp +133 -18
  41. package/project/.sdk/tm/cpp/test/feature/secrets/secrets_test.cpp +175 -8
  42. package/project/.sdk/tm/cpp/test/feature_test.cpp +27 -0
  43. package/project/.sdk/tm/cpp/utility/pipeline.hpp +52 -4
  44. package/project/.sdk/tm/csharp/core/Control.cs +0 -5
  45. package/project/.sdk/tm/csharp/core/Point.cs +6 -0
  46. package/project/.sdk/tm/csharp/feature/CacheFeature.cs +16 -0
  47. package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +77 -38
  48. package/project/.sdk/tm/csharp/test/FeatureTest.cs +29 -0
  49. package/project/.sdk/tm/csharp/test/feature/secrets/SecretsFeatureTest.cs +315 -68
  50. package/project/.sdk/tm/csharp/utility/MakeSpec.cs +2 -2
  51. package/project/.sdk/tm/csharp/utility/PrepareBody.cs +42 -0
  52. package/project/.sdk/tm/csharp/utility/PrepareQuery.cs +13 -2
  53. package/project/.sdk/tm/elixir/lib/projectname/entity_base.ex +0 -13
  54. package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +32 -10
  55. package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +78 -22
  56. package/project/.sdk/tm/elixir/lib/projectname/utility.ex +62 -6
  57. package/project/.sdk/tm/elixir/test/feature/secrets/secrets_test.exs +178 -19
  58. package/project/.sdk/tm/elixir/test/feature_test.exs +20 -0
  59. package/project/.sdk/tm/go/core/target.go +8 -0
  60. package/project/.sdk/tm/go/feature/cache_feature.go +14 -1
  61. package/project/.sdk/tm/go/feature/secrets_feature.go +110 -15
  62. package/project/.sdk/tm/go/test/feature/secrets/secrets_feature_test.go +373 -87
  63. package/project/.sdk/tm/go/test/feature_test.go +32 -0
  64. package/project/.sdk/tm/go/utility/media.go +2 -2
  65. package/project/.sdk/tm/go/utility/prepare_body.go +37 -1
  66. package/project/.sdk/tm/go/utility/prepare_query.go +10 -1
  67. package/project/.sdk/tm/java/core/Control.java +0 -5
  68. package/project/.sdk/tm/java/core/Point.java +7 -0
  69. package/project/.sdk/tm/java/feature/CacheFeature.java +12 -0
  70. package/project/.sdk/tm/java/feature/SecretsFeature.java +104 -34
  71. package/project/.sdk/tm/java/test/FeatureTest.java +25 -0
  72. package/project/.sdk/tm/java/test/PrimaryUtilityTest.java +39 -0
  73. package/project/.sdk/tm/java/test/feature/secrets/SecretsFeatureTest.java +267 -53
  74. package/project/.sdk/tm/java/utility/Fetcher.java +18 -2
  75. package/project/.sdk/tm/java/utility/MakeSpec.java +1 -1
  76. package/project/.sdk/tm/java/utility/Media.java +2 -2
  77. package/project/.sdk/tm/java/utility/PrepareBody.java +50 -0
  78. package/project/.sdk/tm/java/utility/PrepareQuery.java +11 -1
  79. package/project/.sdk/tm/js/src/Point.js +2 -0
  80. package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +13 -0
  81. package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +79 -29
  82. package/project/.sdk/tm/js/src/utility/MakeSpecUtility.js +1 -1
  83. package/project/.sdk/tm/js/src/utility/PrepareBodyUtility.js +29 -0
  84. package/project/.sdk/tm/js/src/utility/PrepareQueryUtility.js +5 -3
  85. package/project/.sdk/tm/js/test/feature/secrets/Secrets.test.js +282 -93
  86. package/project/.sdk/tm/js/test/feature.test.js +17 -0
  87. package/project/.sdk/tm/kotlin/core/Control.kt +0 -5
  88. package/project/.sdk/tm/kotlin/core/Point.kt +7 -0
  89. package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +12 -0
  90. package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +84 -24
  91. package/project/.sdk/tm/kotlin/test/FeatureTest.kt +24 -0
  92. package/project/.sdk/tm/kotlin/test/PrimaryUtilityTest.kt +35 -0
  93. package/project/.sdk/tm/kotlin/test/feature/secrets/SecretsTest.kt +236 -54
  94. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +13 -1
  95. package/project/.sdk/tm/kotlin/utility/MakeSpecUrl.kt +1 -1
  96. package/project/.sdk/tm/kotlin/utility/Prepare.kt +40 -10
  97. package/project/.sdk/tm/lua/feature/cache_feature.lua +20 -0
  98. package/project/.sdk/tm/lua/feature/secrets_feature.lua +92 -15
  99. package/project/.sdk/tm/lua/test/feature/secrets/secrets_feature_test.lua +208 -43
  100. package/project/.sdk/tm/lua/test/feature_test.lua +18 -0
  101. package/project/.sdk/tm/lua/utility/clean.lua +1 -0
  102. package/project/.sdk/tm/lua/utility/make_spec.lua +1 -1
  103. package/project/.sdk/tm/lua/utility/prepare_body.lua +40 -0
  104. package/project/.sdk/tm/lua/utility/prepare_query.lua +11 -1
  105. package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +100 -14
  106. package/project/.sdk/tm/ocaml/sdk_features.ml +7 -1
  107. package/project/.sdk/tm/ocaml/sdk_runtime.ml +40 -5
  108. package/project/.sdk/tm/ocaml/test/feature/secrets/t_secrets.ml +143 -7
  109. package/project/.sdk/tm/ocaml/test/t_feature.ml +15 -0
  110. package/project/.sdk/tm/perl/feature/cache_feature.pm +13 -0
  111. package/project/.sdk/tm/perl/feature/secrets_feature.pm +87 -32
  112. package/project/.sdk/tm/perl/t/feature/secrets/secrets.t +177 -24
  113. package/project/.sdk/tm/perl/t/feature.t +24 -0
  114. package/project/.sdk/tm/perl/utility/make_spec.pm +1 -1
  115. package/project/.sdk/tm/perl/utility/prepare_body.pm +38 -4
  116. package/project/.sdk/tm/perl/utility/prepare_query.pm +7 -1
  117. package/project/.sdk/tm/php/feature/CacheFeature.php +13 -0
  118. package/project/.sdk/tm/php/feature/SecretsFeature.php +79 -23
  119. package/project/.sdk/tm/php/test/FeatureTest.php +23 -0
  120. package/project/.sdk/tm/php/test/feature/secrets/SecretsTest.php +258 -48
  121. package/project/.sdk/tm/php/utility/MakeSpec.php +2 -2
  122. package/project/.sdk/tm/php/utility/PrepareBody.php +31 -0
  123. package/project/.sdk/tm/php/utility/PrepareQuery.php +9 -2
  124. package/project/.sdk/tm/py/pkg/feature/cache_feature.py +7 -1
  125. package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +90 -29
  126. package/project/.sdk/tm/py/pkg/utility/make_spec.py +1 -1
  127. package/project/.sdk/tm/py/pkg/utility/prepare_body.py +26 -0
  128. package/project/.sdk/tm/py/pkg/utility/prepare_query.py +7 -2
  129. package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +313 -80
  130. package/project/.sdk/tm/py/test/test_feature.py +18 -0
  131. package/project/.sdk/tm/rb/feature/cache_feature.rb +12 -0
  132. package/project/.sdk/tm/rb/feature/secrets_feature.rb +68 -21
  133. package/project/.sdk/tm/rb/test/feature/secrets/secrets_feature_test.rb +214 -43
  134. package/project/.sdk/tm/rb/test/feature_test.rb +20 -0
  135. package/project/.sdk/tm/rb/utility/make_spec.rb +1 -1
  136. package/project/.sdk/tm/rb/utility/prepare_body.rb +31 -3
  137. package/project/.sdk/tm/rb/utility/prepare_query.rb +7 -1
  138. package/project/.sdk/tm/rust/feature/cache.rs +15 -1
  139. package/project/.sdk/tm/rust/feature/secrets.rs +103 -23
  140. package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +357 -67
  141. package/project/.sdk/tm/rust/tests/feature_test.rs +41 -0
  142. package/project/.sdk/tm/rust/utility/prepare_body.rs +51 -0
  143. package/project/.sdk/tm/rust/utility/prepare_query.rs +12 -4
  144. package/project/.sdk/tm/scala/core/Control.scala +0 -5
  145. package/project/.sdk/tm/scala/core/Point.scala +3 -0
  146. package/project/.sdk/tm/scala/feature/CacheFeature.scala +12 -0
  147. package/project/.sdk/tm/scala/feature/SecretsFeature.scala +88 -22
  148. package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +61 -1
  149. package/project/.sdk/tm/scala/sdktest/feature/secrets/SecretsTestMain.scala +230 -73
  150. package/project/.sdk/tm/scala/utility/Fetcher.scala +13 -1
  151. package/project/.sdk/tm/scala/utility/MakeSpec.scala +1 -1
  152. package/project/.sdk/tm/scala/utility/Prepare.scala +53 -12
  153. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Control.swift +0 -5
  154. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Point.swift +3 -0
  155. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +11 -0
  156. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +76 -16
  157. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Make.swift +1 -1
  158. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Prepare.swift +36 -1
  159. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +21 -0
  160. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/feature/secrets/SecretsFeatureTest.swift +257 -73
  161. package/project/.sdk/tm/ts/src/Point.ts +3 -0
  162. package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +13 -0
  163. package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +77 -17
  164. package/project/.sdk/tm/ts/src/utility/PrepareBodyUtility.ts +29 -0
  165. package/project/.sdk/tm/ts/src/utility/PrepareQueryUtility.ts +5 -3
  166. package/project/.sdk/tm/ts/test/feature/secrets/Secrets.test.ts +282 -87
  167. package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
  168. package/project/.sdk/tm/zig/core/utility.zig +47 -4
  169. package/project/.sdk/tm/zig/feature/cache.zig +15 -1
  170. package/project/.sdk/tm/zig/feature/secrets.zig +109 -32
  171. package/project/.sdk/tm/zig/test/feature/secrets/secrets_test.zig +232 -9
  172. package/project/.sdk/tm/zig/test/feature_test.zig +33 -0
  173. package/project/sdkgen-package.json +1 -1
  174. package/src/utility.ts +12 -1
@@ -84,6 +84,7 @@ type xchg = {
84
84
  xresponse : string;
85
85
  xstatuses : int list;
86
86
  xretries : int;
87
+ xsameorigin : bool;
87
88
  }
88
89
 
89
90
  (* The message an exception carries, whichever library raised it: sekreto's
@@ -138,6 +139,64 @@ let trim_slashes_left (s : string) : string =
138
139
  while !i < n && s.[!i] = '/' do incr i done;
139
140
  String.sub s !i (n - !i)
140
141
 
142
+ let redirect_max = 5
143
+
144
+ (* An absolute http or https url as its scheme, its authority and the rest. *)
145
+ let url_parts (url : string) : (string * string * string) option =
146
+ let scheme, start =
147
+ if String.starts_with ~prefix:"https://" (String.lowercase_ascii url) then ("https", 8)
148
+ else if String.starts_with ~prefix:"http://" (String.lowercase_ascii url) then ("http", 7)
149
+ else ("", 0) in
150
+ if 0 = start then None
151
+ else begin
152
+ let n = String.length url in
153
+ let rec stop i = if i >= n || String.contains "/?#" url.[i] then i else stop (i + 1) in
154
+ let e = stop start in
155
+ let auth = String.sub url start (e - start) in
156
+ if "" = auth || String.contains auth '@' then None
157
+ else Some (scheme, auth, String.sub url e (n - e))
158
+ end
159
+
160
+ (* Scheme, host and port, with the scheme's default port. *)
161
+ let url_origin (url : string) : string option =
162
+ match url_parts url with
163
+ | None -> None
164
+ | Some (scheme, auth, _) ->
165
+ let auth = String.lowercase_ascii auth in
166
+ let ported = match String.rindex_opt auth ':' with
167
+ | Some i -> not (String.contains_from auth i ']')
168
+ | None -> false in
169
+ Some (scheme ^ "://" ^ auth ^ (if ported then "" else if "https" = scheme then ":443" else ":80"))
170
+
171
+ let resolve_url (at : string) (location : string) : string option =
172
+ if None <> url_parts location then Some location
173
+ else match url_parts at with
174
+ | None -> None
175
+ | Some (scheme, auth, rest) ->
176
+ if String.starts_with ~prefix:"//" location then Some (scheme ^ ":" ^ location)
177
+ else if String.starts_with ~prefix:"/" location then Some (scheme ^ "://" ^ auth ^ location)
178
+ else if String.starts_with ~prefix:"?" location then
179
+ let cut = match String.index_from_opt rest 0 '?', String.index_from_opt rest 0 '#' with
180
+ | Some i, Some j -> min i j | Some i, None | None, Some i -> i
181
+ | None, None -> String.length rest in
182
+ Some (scheme ^ "://" ^ auth ^ String.sub rest 0 cut ^ location)
183
+ else
184
+ let path = match String.index_opt rest '?' with Some i -> String.sub rest 0 i | None -> rest in
185
+ let dir = match String.rindex_opt path '/' with
186
+ | Some i -> String.sub path 0 (i + 1) | None -> "/" in
187
+ Some (scheme ^ "://" ^ auth ^ dir ^ location)
188
+
189
+ (* Where a redirect keeping method and body (307 or 308) points, on the
190
+ * exchange's own origin. Any other would drop the body or leave the origin.
191
+ * The bundled transport answers no headers: only a supplied fetch is followed. *)
192
+ let moved (url : string) (at : string) (res : value) : string option =
193
+ match to_int (getp res "status"), getp (getp res "headers") "location" with
194
+ | (307 | 308), Str location when "" <> location ->
195
+ (match resolve_url at location with
196
+ | Some next when None <> url_origin next && url_origin next = url_origin url -> Some next
197
+ | _ -> None)
198
+ | _ -> None
199
+
141
200
  (* Build the feature. `plugins` are the plugin DEFINITIONS the model
142
201
  * selected for this feature, emitted by Config_ocaml from the catalogue's
143
202
  * active `plugin.def.ocaml` entries (`Sdk_config.feature_plugins "secrets"`).
@@ -164,6 +223,10 @@ let make ?(plugins : Defs.definition list = [])
164
223
  let cred = ref "" in
165
224
  (* The refresh credential the chain resolved, for every later purchase. *)
166
225
  let refresh = ref "" in
226
+ (* What prepare_auth was generated from: the config declares auth only
227
+ * where it places a credential, and Basic only where it composes one. *)
228
+ let declared = ref false in
229
+ let basic = ref false in
167
230
 
168
231
  let refuse (msg : string) =
169
232
  (match !initerr with None -> initerr := Some msg | Some _ -> ()) in
@@ -179,6 +242,18 @@ let make ?(plugins : Defs.definition list = [])
179
242
  let live_options () : value =
180
243
  match !client with Some c -> c.cl_options | None -> empty_map () in
181
244
 
245
+ (* prepare_auth's decision: auth the config declares, not suppressed by
246
+ * `auth: null`. *)
247
+ let authon () : bool =
248
+ !declared && (match getp (live_options ()) "auth" with Map _ -> true | _ -> false) in
249
+
250
+ (* Under Basic the token is the key half, as apikey is: prepare_auth's pair. *)
251
+ let pair (token : string) : string =
252
+ let secret = match getp (live_options ()) "secret" with Str s -> s | _ -> "" in
253
+ let joined = base64_encode (token ^ ":" ^ secret) in
254
+ register joined;
255
+ joined in
256
+
182
257
  let bucket () : value =
183
258
  match !client with
184
259
  | Some c -> track_bucket c "secrets" (fun () ->
@@ -197,11 +272,14 @@ let make ?(plugins : Defs.definition list = [])
197
272
  let name = match getpath_s opts "auth.name" with
198
273
  | Str s when s <> "" -> String.lowercase_ascii s
199
274
  | _ -> "authorization" in
200
- (match getp opts "auth" with
201
- | Map _ ->
202
- let prefix = match getpath_s opts "auth.prefix" with Str s -> s | _ -> "" in
203
- setp headers name (Str (if prefix <> "" then prefix ^ " " ^ token else token))
204
- | _ -> ignore (delprop headers (Str name)))
275
+ if authon () then begin
276
+ let value =
277
+ if !basic && (match getpath_s opts "auth.basic" with Bool b -> b | _ -> false)
278
+ then pair token else token in
279
+ let prefix = match getpath_s opts "auth.prefix" with Str s -> s | _ -> "" in
280
+ setp headers name (Str (if prefix <> "" then prefix ^ " " ^ value else value))
281
+ end
282
+ else ignore (delprop headers (Str name))
205
283
  | _ -> () in
206
284
 
207
285
  let spent (x : xchg) (res : value) : bool =
@@ -230,7 +308,7 @@ let make ?(plugins : Defs.definition list = [])
230
308
  ("method", Str x.xmethod);
231
309
  ("headers", jo [("content-type", Str "application/json")]);
232
310
  ("body", Str (jsonify (jo [(x.xrequest, Str !refresh)])));
233
- (* Never followed: a redirect would carry the refresh token on. *)
311
+ (* Never followed by the client: a redirect would carry the refresh token on. *)
234
312
  ("redirect", Str "manual")] in
235
313
  let fetch =
236
314
  match getpath_s opts "system.fetch" with
@@ -243,7 +321,13 @@ let make ?(plugins : Defs.definition list = [])
243
321
  ^ "HTTP client is not compiled; supply options.system.fetch or activate a "
244
322
  ^ "plugin group")
245
323
  | Some fetch ->
246
- (match fetch url fetchdef with
324
+ (* By option alone, and only while the exchange stays on its own origin. *)
325
+ let rec ask at hops =
326
+ let res = fetch at fetchdef in
327
+ match if x.xsameorigin && hops < redirect_max then moved url at res else None with
328
+ | Some next -> ask next (hops + 1)
329
+ | None -> res in
330
+ (match ask url 0 with
247
331
  | exception e -> Error (exn_message e)
248
332
  | res ->
249
333
  (match get_str res "__err__" with
@@ -293,6 +377,7 @@ let make ?(plugins : Defs.definition list = [])
293
377
  let resolve () : (unit, string) Stdlib.result =
294
378
  match !sek with
295
379
  | None -> Ok ()
380
+ | Some _ when not (authon ()) -> Ok ()
296
381
  | Some s ->
297
382
  bump_num (bucket ()) "resolves" 1.;
298
383
  (match Sekreto.tryget s !secretname with
@@ -325,13 +410,10 @@ let make ?(plugins : Defs.definition list = [])
325
410
  * one is spent. The retry rewrites the credential header IN PLACE on
326
411
  * the fetchdef, because the header carries the token that just failed. *)
327
412
  let withrefresh (x : xchg) fctx url fetchdef inner : (value * sdk_error option) =
328
- (* `auth: null` is the documented way to send NO credential, and
329
- * prepare_auth honours it by removing the header. A refusal of a
330
- * deliberately unauthenticated request is not an expired token and
331
- * cannot be fixed by buying one. *)
332
- match getp (live_options ()) "auth" with
333
- | Noval | Null -> inner fctx url fetchdef
334
- | _ ->
413
+ (* A refusal of an unauthenticated request is not an expired token, and
414
+ * retrying would transmit the credential that auth off withholds. *)
415
+ if not (authon ()) then inner fctx url fetchdef
416
+ else
335
417
  let rec attempt (n : int) =
336
418
  (* The credential THIS attempt goes out with, captured before it
337
419
  * leaves: it is what tells a stale refusal apart from a fresh one. *)
@@ -385,6 +467,9 @@ let make ?(plugins : Defs.definition list = [])
385
467
  if f.f_active then begin
386
468
  let c = cc ctx in
387
469
  client := Some c;
470
+ let declared_auth = getpath_s ctx.c_config "options.auth" in
471
+ declared := (match declared_auth with Map _ -> true | _ -> false);
472
+ basic := (match getp declared_auth "basic" with Bool b -> b | _ -> false);
388
473
  secretname := (let n = opt_str opts "name" ~default:"apikey" in if "" = n then "apikey" else n);
389
474
  cache := (getp opts "cache" <> Bool false);
390
475
 
@@ -403,6 +488,7 @@ let make ?(plugins : Defs.definition list = [])
403
488
  xresponse = opt_str xopts "response" ~default:"access_token";
404
489
  xstatuses = (if [] = statuses then [401] else statuses);
405
490
  xretries = opt_int xopts "retries" ~default:1;
491
+ xsameorigin = getp xopts "redirect" = Str "same-origin";
406
492
  }
407
493
  end else None);
408
494
 
@@ -276,6 +276,10 @@ let cache_feature () : feature =
276
276
  | Map _ when getp res "unreadable" = Bool true -> false
277
277
  | Map _ -> (match getp res "status" with Num n -> n >= 200. && n < 300. | _ -> false)
278
278
  | _ -> false in
279
+ let json res =
280
+ match header_ci (getp res "headers") "content-type" with
281
+ | Str t -> "" = t || is_json_media t
282
+ | _ -> true in
279
283
  let snapshot res =
280
284
  let data = match getp res "json" with Func _ as fn -> (try call_json fn with _ -> Noval) | _ -> Noval in
281
285
  let headers = empty_map () in
@@ -301,7 +305,9 @@ let cache_feature () : feature =
301
305
  track ctx "hit"; (replay (getp hit "snapshot"), None)
302
306
  | _ ->
303
307
  let (res, err) = inner ctx url fetchdef in
304
- if err = None && cacheable res then begin
308
+ (* A body that declares another type than JSON passes by, unread. *)
309
+ if err = None && cacheable res && not (json res) then (track ctx "bypass"; (res, err))
310
+ else if err = None && cacheable res then begin
305
311
  let snap = snapshot res in
306
312
  let ttl = opt_num !options "ttl" ~default:5000. in
307
313
  evict ();
@@ -875,8 +875,9 @@ let prepare_query_util (ctx : ctx) : value =
875
875
  | _ -> []
876
876
  in
877
877
  (* A header or cookie parameter travels in the headers, which
878
- * prepare_headers fills, unless a query parameter shares its name: then
879
- * both are sent. *)
878
+ * prepare_headers fills, and a field the point's body declares travels in
879
+ * the body, which prepare_body fills, unless a query parameter shares its
880
+ * name: then both are sent. *)
880
881
  let declared =
881
882
  match getp (getp ctx.c_point "args") "query" with
882
883
  | List r -> List.map (fun qd -> getp qd "name") !r
@@ -892,7 +893,9 @@ let prepare_query_util (ctx : ctx) : value =
892
893
  | List r -> List.map (fun cd -> getp cd "name") !r
893
894
  | _ -> []
894
895
  in
895
- let elsewhere = List.filter (fun n -> not (List.mem n declared)) (header_names @ cookie_names) in
896
+ let fields = match getp ctx.c_point "fields" with List r -> !r | _ -> [] in
897
+ let elsewhere =
898
+ List.filter (fun n -> not (List.mem n declared)) (header_names @ cookie_names @ fields) in
896
899
  let params = params @ arg_names @ elsewhere in
897
900
  let contains_param s = List.exists (fun v -> match v with Str x -> x = s | _ -> false) params in
898
901
  (* A query parameter travels under the name the definition gives it, its
@@ -921,8 +924,40 @@ let prepare_query_util (ctx : ctx) : value =
921
924
  (call_args ctx "query");
922
925
  out
923
926
 
927
+ (* A list, load or remove routed to a method with a body sends the match
928
+ * fields that body declares, never a path parameter, and never a header,
929
+ * cookie or query argument the body does not also declare as a field. *)
930
+ let match_body (ctx : ctx) (fields : value list) : value =
931
+ let point = ctx.c_point in
932
+ let arg_names kind keep =
933
+ match getp (getp point "args") kind with
934
+ | List r ->
935
+ List.filter_map (fun ad ->
936
+ match getp ad "name" with Str n when keep ad -> Some n | _ -> None) !r
937
+ | _ -> []
938
+ in
939
+ let unkept ad = match getp ad "field" with Bool true -> false | _ -> true in
940
+ let routed =
941
+ (match getp point "params" with
942
+ | List r -> List.filter_map (function Str s -> Some s | _ -> None) !r
943
+ | _ -> [])
944
+ @ arg_names "params" (fun _ -> true)
945
+ @ List.concat_map (fun kind -> arg_names kind unkept) ["header"; "cookie"; "query"]
946
+ in
947
+ let body = empty_map () in
948
+ List.iter (function
949
+ | Str name when name <> "$action" && not (List.mem name routed) ->
950
+ (match getp ctx.c_reqmatch name with
951
+ | Noval | Null -> ()
952
+ | v -> setp body name v)
953
+ | _ -> ()) fields;
954
+ body
955
+
924
956
  let prepare_body_util (ctx : ctx) : value =
925
- if ctx.c_op.op_input <> "data" then Noval
957
+ if ctx.c_op.op_input <> "data" then
958
+ (match getp ctx.c_point "fields" with
959
+ | List { contents = _ :: _ as fields } -> match_body ctx fields
960
+ | _ -> Noval)
926
961
  else if is_raw_request ctx.c_point then raw_body ctx.c_reqdata
927
962
  else (cu ctx).u_transform_request ctx
928
963
 
@@ -1383,7 +1418,7 @@ let make_spec_util (ctx : ctx) : (spec option * sdk_error option) =
1383
1418
  if get_str_d ctx.c_point "kind" "" = "graphql" then begin
1384
1419
  (* GraphQL addresses one endpoint: no path parts, no query string, and
1385
1420
  * the body carries the operation. prepare_body is skipped
1386
- * deliberately — it only emits a body for data-input ops, whereas
1421
+ * deliberately — it builds only a REST body, whereas
1387
1422
  * every GraphQL op posts one, including load/list/remove. *)
1388
1423
  sp.sp_body <- u.u_graphql_body ctx;
1389
1424
  sp.sp_path <- "";
@@ -213,15 +213,29 @@ let placed_of (r : recorder) (i : int) : value =
213
213
  | Some c -> placed_cred_of (getp c "fetchdef")
214
214
  | None -> Noval
215
215
 
216
- (* A header credential carries the SPEC's prefix, which a TEMPLATE cannot
217
- * know - so assert on the CREDENTIAL and let the prefix be whatever this
218
- * SDK's API declares. A query parameter and a cookie carry no prefix at all,
219
- * which the bare equality already covers. *)
220
- let credential_is (header : value) (token : string) : bool =
216
+ (* The header value prepare_auth writes for a token, in the form the DEFAULT
217
+ * client's auth gives: under Basic the token is the key half of the encoded
218
+ * pair, and a prefix is space-joined. *)
219
+ let credential ?(secret = "") (token : string) : string =
220
+ let opts = (Sdk_client.test ()).cl_options in
221
+ let value = match getpath_s opts "auth.basic" with
222
+ | Bool true -> base64_encode (token ^ ":" ^ secret)
223
+ | _ -> token in
224
+ match getpath_s opts "auth.prefix" with
225
+ | Str p when p <> "" -> p ^ " " ^ value
226
+ | _ -> value
227
+
228
+ let credential_is ?(secret = "") (header : value) (token : string) : bool =
221
229
  match header with
222
- | Str s -> s = token || Sdk_features.ends_with s (" " ^ token)
230
+ | Str s -> s = credential ~secret token
223
231
  | _ -> false
224
232
 
233
+ (* What prepare_auth placed: the header form, or the bare value in a query
234
+ * parameter or a cookie. *)
235
+ let placed_is (placed : value) (token : string) : bool =
236
+ if Sdk_prepare_auth.cred_where = "header" then credential_is placed token
237
+ else placed = Str token
238
+
225
239
  let absent (header : value) : bool = is_nullish header
226
240
 
227
241
  (* SUPPRESSION IS ABOUT EVERY BAG, not just the header. `auth: null` must
@@ -351,7 +365,7 @@ let () =
351
365
  what the generated prepare_auth placed - in the bag this API's
352
366
  scheme names, which for an apiKey-in-query SDK is the query string
353
367
  and not a header. *)
354
- check "carries OPTKEY01" (credential_is (placed_of r 0) "OPTKEY01"));
368
+ check "carries OPTKEY01" (placed_is (placed_of r 0) "OPTKEY01"));
355
369
 
356
370
  test "inactive.no_apikey_no_header" (fun () ->
357
371
  let (r, fetch) = recorder () in
@@ -592,6 +606,14 @@ let () =
592
606
  check "options.auth survives as a present null"
593
607
  (getprop_raw client.cl_options "auth" = Null));
594
608
 
609
+ test "chain.a_resolved_key_is_placed_as_prepare_auth_places_it" (fun () ->
610
+ let (r, fetch) = recorder () in
611
+ let client = live ~sdkopts:[("secret", Str "PASS01")] fetch
612
+ [("providers", ja [working "CHAINKEY05"])] in
613
+ check "ok" (res_ok (direct client));
614
+ check "carries CHAINKEY05, the secret kept"
615
+ (credential_is ~secret:"PASS01" (auth_of r 0) "CHAINKEY05"));
616
+
595
617
  test "auth.null_suppresses_an_explicit_apikey_too" (fun () ->
596
618
  let (r, fetch) = recorder () in
597
619
  let client = live ~sdkopts:[("auth", Null); ("apikey", Str "OPTKEY01")] fetch
@@ -710,6 +732,23 @@ let () =
710
732
  check_int "two API calls: the refusal and the retry" (List.length (api_calls r)) 2;
711
733
  check "the retry carries the fresh token" (credential_is (auth_of r 1) "ACCESS2"));
712
734
 
735
+ test "exchange.a_token_bought_on_a_refusal_is_placed_as_prepare_auth_places_it" (fun () ->
736
+ let (r, fetch) = recorder () in
737
+ (* Read as each request left: the retry rewrites the fetchdef it shares. *)
738
+ let sent = ref [] in
739
+ token_server r (fun token ->
740
+ sent := !sent @ [Str token];
741
+ if credential_is ~secret:"PASS01" (Str token) "ACCESS1" then 401 else 200);
742
+ let client = live ~sdkopts:[("secret", Str "PASS01")] fetch
743
+ [refresh_chain "R1"; ("name", Str "refresh_token"); xchg []] in
744
+ check "ok" (res_ok (direct client));
745
+ check_int "two API calls: the refusal and the retry" (List.length (api_calls r)) 2;
746
+ let nth i = match List.nth_opt !sent i with Some v -> v | None -> Noval in
747
+ check "the first carries ACCESS1, the secret kept"
748
+ (credential_is ~secret:"PASS01" (nth 0) "ACCESS1");
749
+ check "the retry carries ACCESS2, the secret kept"
750
+ (credential_is ~secret:"PASS01" (nth 1) "ACCESS2"));
751
+
713
752
  test "exchange.a_second_refusal_surfaces_rather_than_spinning" (fun () ->
714
753
  let (r, fetch) = recorder () in
715
754
  token_server r (fun _ -> 401);
@@ -911,6 +950,67 @@ let () =
911
950
  check (label ^ ": not a failed exchange: " ^ message) r) results;
912
951
  check_int "redirects refused" refused 4);
913
952
 
953
+ (* On system.fetch alone: the vendored HTTP client answers no headers, so
954
+ * no Location reaches the feature through it. *)
955
+ test "exchange.with_the_option_a_redirect_on_its_own_origin_is_followed_and_no_other" (fun () ->
956
+ let base = "http://secrets.test/api" in
957
+ let exchange = base ^ "/auth/token" and moved = base ^ "/moved/token" in
958
+ let away = "http://elsewhere.test/api/auth/token" in
959
+ let url_of c = match getp c "url" with Str u -> u | _ -> "" in
960
+ let on_origin u = String.starts_with ~prefix:"http://secrets.test/" u in
961
+ (* Each case: the status, the Location, and the url the exchange is asked
962
+ * again at; a Location of a query alone keeps the path that answered. *)
963
+ let cases = [(307, "/api/moved/token", Some moved); (308, moved, Some moved);
964
+ (307, "?step=2", Some (exchange ^ "?step=2")); (307, away, None);
965
+ (302, "/api/moved/token", None); (307, "/api/auth/token", None)] in
966
+ let results = List.map (fun (status, location, next) ->
967
+ let (r, fetch) = recorder () in
968
+ r.reply <- Some (fun _ url _ ->
969
+ if url = exchange then
970
+ jo [("status", vint_of status); ("statusText", Str "X");
971
+ ("headers", jo [("location", Str location)]); ("body", Str "");
972
+ ("json", json_thunk Noval)]
973
+ else if url = base ^ "/thing" then ok200 (jo [("id", Str "r01")])
974
+ else if on_origin url then ok200 (jo [("access_token", Str "MOVED01")])
975
+ else ok200 (jo [("access_token", Str "ELSEWHERE01")]));
976
+ let client = live ~base fetch [("name", Str "refresh_token");
977
+ xchg [("refresh", Str "REFRESH01");
978
+ ("redirect", Str "same-origin")]] in
979
+ let res = direct client in
980
+ let message = res_err res in
981
+ let asked = List.filter (fun c -> url_of c <> base ^ "/thing") !(r.calls) in
982
+ let manual = List.for_all (fun c ->
983
+ getp (getp c "fetchdef") "redirect" = Str "manual") asked in
984
+ let same = match asked with
985
+ | first :: rest -> List.for_all (fun c ->
986
+ getp (getp c "fetchdef") "method" = getp (getp first "fetchdef") "method"
987
+ && getp (getp c "fetchdef") "body" = getp (getp first "fetchdef") "body") rest
988
+ | [] -> false in
989
+ let urls = List.map url_of asked in
990
+ let want = "token exchange failed: " ^ string_of_int status ^ " from " ^ exchange in
991
+ let verdict =
992
+ if None <> next && res_ok res && manual && same
993
+ && Some urls = Option.map (fun n -> [exchange; n]) next then `Followed
994
+ else if None = next && manual && contains message want
995
+ && List.for_all (fun u -> u = exchange) urls then `Refused
996
+ else `Wrong (string_of_int status ^ " to " ^ location ^ ": " ^ message
997
+ ^ " " ^ String.concat "; " urls) in
998
+ (verdict, location, List.length urls,
999
+ List.length (List.filter (fun c -> not (on_origin (url_of c))) !(r.calls))))
1000
+ cases in
1001
+ let count v = List.length (List.filter (fun (w, _, _, _) -> w = v) results) in
1002
+ let elsewhere = List.fold_left (fun n (_, _, _, e) -> n + e) 0 results in
1003
+ let looped = List.fold_left (fun n (_, l, a, _) ->
1004
+ if l = "/api/auth/token" then a else n) 0 results in
1005
+ Printf.printf "secrets: exchange opt-in followed %d redirect(s) on its own origin, refused %d, %d elsewhere\n"
1006
+ (count `Followed) (count `Refused) elsewhere;
1007
+ check_int "the exchange reached another origin" elsewhere 0;
1008
+ List.iter (function (`Wrong m, _, _, _) -> check m false | _ -> ()) results;
1009
+ check_int "followed" (count `Followed) 3;
1010
+ check_int "refused" (count `Refused) 3;
1011
+ (* A redirect to itself stops at the hop limit. *)
1012
+ check_int "the loop's exchange count" looped 6);
1013
+
914
1014
  test "exchange.test_mode_buys_nothing" (fun () ->
915
1015
  (* A non-live client: the credential becomes the deterministic
916
1016
  * `test-<response>` and no purchase is attempted. Observed through
@@ -944,5 +1044,41 @@ let () =
944
1044
 
945
1045
  ()
946
1046
 
1047
+ (* Off by the config where it declares no auth, else by `auth: null`: either
1048
+ * way the feature places no credential, buys no token and retries nothing. *)
1049
+ let () =
1050
+ Testutil.test "secrets.auth_off.places_no_credential_and_buys_no_token" (fun () ->
1051
+ let off = if places_credential then [("auth", Null)] else [] in
1052
+ let secrets = ["OFFKEY01"; "OFFKEY02"; "OFFREFRESH01"; "ACCESS01"] in
1053
+ let carries (c : value) : bool =
1054
+ match getp (getp c "fetchdef") "headers" with
1055
+ | Map _ as h ->
1056
+ not (absent (Sdk_features.header_ci h credheader))
1057
+ || List.exists (fun k -> match getp h k with
1058
+ | Str v -> List.exists (contains v) secrets
1059
+ | _ -> false) (keysof h)
1060
+ | _ -> false in
1061
+ let sent = ref [] and bought = ref 0 in
1062
+ let refresh = ("providers", ja [memory ~name:"refresh" [("REFRESH_TOKEN", Str "OFFREFRESH01")]]) in
1063
+ List.iter (fun (what, sdkopts, fopts, status) ->
1064
+ let (r, fetch) = recorder () in
1065
+ r.reply <- Some (fun _ url _ ->
1066
+ if is_token url then ok200 (jo [("access_token", Str "ACCESS01")])
1067
+ else status_res status (jo [("id", Str "r01")]));
1068
+ let client = live ~sdkopts:(sdkopts @ off) fetch fopts in
1069
+ ignore (direct client);
1070
+ check_int (what ^ ": one request, not retried") (List.length (api_calls r)) 1;
1071
+ check_int (what ^ ": no token bought") (List.length (token_calls r)) 0;
1072
+ check (what ^ ": no credential sent") (not (List.exists carries (api_calls r)));
1073
+ sent := !sent @ api_calls r;
1074
+ bought := !bought + List.length (token_calls r))
1075
+ [ ("a key the chain resolves", [], [("providers", ja [working "OFFKEY01"])], 200);
1076
+ ("an explicit apikey", [("apikey", Str "OFFKEY02")], [], 200);
1077
+ ("a refusal", [], [refresh; ("name", Str "refresh_token");
1078
+ ("exchange", jo [("active", Bool true)])], 401) ];
1079
+ Printf.printf "secrets: auth off by %s: %d request(s), %d credential(s), %d token(s)\n"
1080
+ (if places_credential then "option" else "config")
1081
+ (List.length !sent) (List.length (List.filter carries !sent)) !bought)
1082
+
947
1083
  (* Printed from EXECUTIONS, for the generator's lane. *)
948
1084
  let () = Printf.printf "feature.secrets: ran %d check(s)\n" !ran
@@ -178,6 +178,21 @@ let () =
178
178
  ignore (op h ~op:"load" ~path:"/w" ()); ignore (op h ~op:"load" ~path:"/w" ());
179
179
  check_int "calls" (List.length !calls) 2;
180
180
  check_vnum "bypass" (getp (track h "cache") "bypass") 2.);
181
+ test "cache.non_json_body_passes_by_unread" (fun () ->
182
+ let sent = ref [] in
183
+ let (srv, calls) = recording_server ~reply:(fun _ _ ->
184
+ let res = make_response ~headers:[("content-type", Str "audio/wav")] 200 Noval in
185
+ sent := !sent @ [res];
186
+ (res, None)) () in
187
+ let h = make_client ~server:srv [("cache", empty_map ())] in
188
+ let a = op h ~op:"load" ~path:"/w/1/audio" () in
189
+ let b = op h ~op:"load" ~path:"/w/1/audio" () in
190
+ check_int "the body was stored" (List.length !calls) 2;
191
+ check "the cache handed on a copy it had read"
192
+ (match !sent with s :: _ -> a.or_response == s | [] -> false);
193
+ check "the second read was handed on as returned"
194
+ (match !sent with _ :: s :: _ -> b.or_response == s | _ -> false);
195
+ check_vnum "bypass" (getp (track h "cache") "bypass") 2.);
181
196
  test "cache.refetches_after_ttl" (fun () ->
182
197
  let c = make_clock () in
183
198
  let (srv, calls) = recording_server () in
@@ -20,6 +20,7 @@ BEGIN { $__dir = File::Basename::dirname(Cwd::abs_path(__FILE__)) }
20
20
  require(Cwd::abs_path("$__dir/../lib/Voxgig/Struct.pm"));
21
21
  require(Cwd::abs_path("$__dir/../core/helpers.pm"));
22
22
  require(Cwd::abs_path("$__dir/base_feature.pm"));
23
+ require(Cwd::abs_path("$__dir/../utility/media.pm"));
23
24
 
24
25
  package ProjectNameCacheFeature;
25
26
 
@@ -83,6 +84,12 @@ sub through {
83
84
 
84
85
  my ($res, $err) = $inner->($ctx, $url, $fetchdef);
85
86
 
87
+ # A body that declares another type than JSON passes by, unread.
88
+ if (!defined $err && $self->_cacheable($res) && !_json($res)) {
89
+ $self->_track('bypass');
90
+ return ($res, $err);
91
+ }
92
+
86
93
  if (!defined $err && $self->_cacheable($res)) {
87
94
  my $snapshot = $self->_snapshot($res);
88
95
  my $ttl = defined $self->{options}{ttl} ? $self->{options}{ttl} : 5000;
@@ -110,6 +117,12 @@ sub _cacheable {
110
117
  return ($status >= 200 && $status < 300) ? 1 : 0;
111
118
  }
112
119
 
120
+ sub _json {
121
+ my ($res) = @_;
122
+ my $type = ProjectNameUtilities::header_value($res->{headers}, 'content-type');
123
+ return '' eq $type || ProjectNameUtilities::is_json_media($type);
124
+ }
125
+
113
126
  sub _snapshot {
114
127
  my ($self, $res) = @_;
115
128
  my $data;