@tishlang/tish-format 2.36.0 → 2.37.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/tish-format CHANGED
Binary file
@@ -284,16 +284,45 @@ fn param_infer_program(program: Program) -> Program {
284
284
  // panic on / turn to NaN where JS keeps the value. Skip those fns (they stay fully boxed). Same
285
285
  // set `collect_native_fns` uses to refuse the M5 native form.
286
286
  let nonnumeric = crate::codegen::Codegen::fns_called_with_nonnumeric_arg(&program.statements);
287
+ // Soundness (#477): a fn referenced as a first-class VALUE — passed as a call ARGUMENT
288
+ // (`serve(port, handler)`), assigned, returned, or stored, i.e. any use that is NOT a direct
289
+ // `f(...)` call — escapes into the boxed `Value::Function` path and is later invoked via
290
+ // `.call(&[Value])` with arbitrary runtime Values (a `serve` handler is called with the request
291
+ // OBJECT). Such a fn's params CANNOT be soundly `: number`-shadowed: the escaping boxed closure
292
+ // unboxes arg0 as `match … Value::Number(n) => *n, _ => panic!("expected number")` and would panic
293
+ // on the first non-numeric call. `fns_called_with_nonnumeric_arg` only catches SYNTACTIC
294
+ // non-numeric call args — it can't see this value-escape — so exclude escaping fns explicitly.
295
+ // Over-approximation is safe: an escaping fn just stays fully boxed (correct, at worst slower).
296
+ let escaping = escaping_fn_names(&program.statements);
287
297
  Program {
288
298
  statements: program
289
299
  .statements
290
300
  .into_iter()
291
- .map(|s| pi_stmt(s, &nonnumeric))
301
+ .map(|s| pi_stmt(s, &nonnumeric, &escaping))
292
302
  .collect(),
293
303
  }
294
304
  }
295
305
 
296
- fn pi_stmt(s: Statement, nonnumeric: &HashSet<String>) -> Statement {
306
+ /// Names of fns referenced as a first-class VALUE anywhere in the program — any use of the name that
307
+ /// is NOT a direct `f(...)` call (a bare-ident call argument like `serve(port, handler)`, an
308
+ /// assignment, a return, storage in an object/array). Such a fn is reachable via the boxed
309
+ /// `Value::Function` `.call(&[Value])` path with arbitrary runtime Values, so its params must stay
310
+ /// boxed and are never `: number`-shadowed. Backed by the same `fn_name_uses` escape counter the
311
+ /// inliner uses. See `param_infer_program`. (#477)
312
+ fn escaping_fn_names(statements: &[Statement]) -> HashSet<String> {
313
+ let mut out = HashSet::new();
314
+ for s in statements {
315
+ if let Statement::FunDecl { name, .. } = s {
316
+ let (_, others) = fn_name_uses(statements, name.as_ref());
317
+ if others > 0 {
318
+ out.insert(name.to_string());
319
+ }
320
+ }
321
+ }
322
+ out
323
+ }
324
+
325
+ fn pi_stmt(s: Statement, nonnumeric: &HashSet<String>, escaping: &HashSet<String>) -> Statement {
297
326
  if let Statement::FunDecl {
298
327
  async_,
299
328
  name,
@@ -305,8 +334,10 @@ fn pi_stmt(s: Statement, nonnumeric: &HashSet<String>) -> Statement {
305
334
  span,
306
335
  } = s
307
336
  {
308
- // A fn reached with a non-numeric arg somewhere: leave its params dynamic (boxed).
309
- if nonnumeric.contains(name.as_ref()) {
337
+ // A fn reached with a non-numeric arg somewhere, OR escaping as a first-class value
338
+ // (#477 — a `serve` handler / stored callback invoked via the boxed path with arbitrary
339
+ // Values): leave its params dynamic (boxed).
340
+ if nonnumeric.contains(name.as_ref()) || escaping.contains(name.as_ref()) {
310
341
  return Statement::FunDecl {
311
342
  async_,
312
343
  name,
@@ -3680,6 +3711,12 @@ pub fn analyze_aggregate(program: &Program) -> AggregateAnalysis {
3680
3711
  // would otherwise emit a `match … panic!("expected number")` coercion on the String. Same set the
3681
3712
  // M4 `param_infer` path uses (infer.rs:273).
3682
3713
  let nonnumeric = crate::codegen::Codegen::fns_called_with_nonnumeric_arg(&program.statements);
3714
+ // Soundness (#477): a fn referenced as a first-class VALUE (a `serve` handler / stored callback)
3715
+ // is invoked via the boxed `Value::Function` path with arbitrary runtime Values, so — exactly as
3716
+ // in `param_infer_program` — its params must NOT get an S-0 `: number` shadow either. Without this
3717
+ // an UNUSED param (vacuously "numeric" under `pus_stmt`) would be stamped `: number` and the
3718
+ // escaping boxed closure would `panic!("expected number")` on the request object.
3719
+ let escaping = escaping_fn_names(&program.statements);
3683
3720
 
3684
3721
  // ---- S-0 + S-A: per top-level function, find numeric params and a return shape.
3685
3722
  for s in &program.statements {
@@ -3731,7 +3768,10 @@ pub fn analyze_aggregate(program: &Program) -> AggregateAnalysis {
3731
3768
  }
3732
3769
  }
3733
3770
  }
3734
- if !numeric_params.is_empty() && !nonnumeric.contains(name.as_ref()) {
3771
+ if !numeric_params.is_empty()
3772
+ && !nonnumeric.contains(name.as_ref())
3773
+ && !escaping.contains(name.as_ref())
3774
+ {
3735
3775
  analysis.fn_numeric_params.insert(name.to_string(), numeric_params);
3736
3776
  }
3737
3777
  // S-A: a single all-f64 object-literal return shape ⇒ a struct alias.
@@ -4677,6 +4717,54 @@ mod param_infer_tests {
4677
4717
  let src = "fn g(x) { let r = x; return r + \"!\" }";
4678
4718
  assert_eq!(inferred_param(src, "g", "x"), None);
4679
4719
  }
4720
+
4721
+ /// Final annotation name (if any) for `fn <fn_name>`'s `<param>` after the FULL pipeline
4722
+ /// (`infer_program`: M4 `param_infer` + local/struct + S-0 aggregate).
4723
+ fn end_to_end_param_ann(src: &str, fn_name: &str, param: &str) -> Option<String> {
4724
+ let prog = infer_program(&parse(src).unwrap());
4725
+ for s in &prog.statements {
4726
+ if let Statement::FunDecl { name, params, .. } = s {
4727
+ if name.as_ref() == fn_name {
4728
+ for p in params {
4729
+ if let FunParam::Simple(tp) = p {
4730
+ if tp.name.as_ref() == param {
4731
+ return tp.type_ann.as_ref().map(|a| match a {
4732
+ TypeAnnotation::Simple(s, _) => s.to_string(),
4733
+ _ => "<complex>".to_string(),
4734
+ });
4735
+ }
4736
+ }
4737
+ }
4738
+ }
4739
+ }
4740
+ }
4741
+ None
4742
+ }
4743
+
4744
+ #[test]
4745
+ fn escaping_value_fn_param_stays_boxed_477() {
4746
+ // #477: `handler` is passed as a call ARGUMENT (escapes as a first-class value, exactly like a
4747
+ // `serve` handler). Its `req` param is UNUSED and the body is all-numeric — so BOTH the M4
4748
+ // `param_infer` and the S-0 aggregate paths would otherwise stamp it `: number`. Because the
4749
+ // fn is invoked via the boxed `Value::Function` path with arbitrary runtime Values, `req` MUST
4750
+ // stay dynamic across the WHOLE pipeline (else the boxed closure panics "expected number" on
4751
+ // the request object). Locks both promotion sites.
4752
+ let src = "fn handler(req) { let s = 200; return s } \
4753
+ fn callWith(f, a) { return f(a) } \
4754
+ console.log(callWith(handler, { method: \"GET\" }))";
4755
+ assert_eq!(end_to_end_param_ann(src, "handler", "req"), None);
4756
+ }
4757
+
4758
+ #[test]
4759
+ fn non_escaping_numeric_fn_keeps_native_fast_path_477() {
4760
+ // CONTROL (no over-broad regression): a fn only ever called DIRECTLY with numbers still gets
4761
+ // its `: number` native fast path — the #477 fix narrows promotion to escaping fns only.
4762
+ let src = "fn square(n) { return n * n } console.log(square(7))";
4763
+ assert_eq!(
4764
+ end_to_end_param_ann(src, "square", "n").as_deref(),
4765
+ Some("number")
4766
+ );
4767
+ }
4680
4768
  }
4681
4769
 
4682
4770
  #[cfg(test)]
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "tishlang_fmt"
3
- version = "2.36.0"
3
+ version = "2.37.0"
4
4
  edition = "2021"
5
5
  description = "Opinionated formatter for Tish source (parse → pretty-print)"
6
6
  license-file = { workspace = true }
@@ -79,7 +79,7 @@ ws = [
79
79
  tishlang_core = { path = "../tish_core", version = ">=0.1" }
80
80
  tishlang_builtins = { path = "../tish_builtins", version = ">=0.1" }
81
81
  tokio = { version = "1.50.0", features = ["rt-multi-thread", "macros", "time", "sync"], optional = true }
82
- reqwest = { version = "0.13.2", default-features = false, features = ["rustls", "json", "stream"], optional = true }
82
+ reqwest = { version = "0.13.2", default-features = false, features = ["rustls", "json", "stream", "multipart"], optional = true }
83
83
  futures = { version = "0.3.32", optional = true }
84
84
  bytes = { version = "1", optional = true }
85
85
  tiny_http = { version = "0.12.0", optional = true }
@@ -111,6 +111,57 @@ pub(crate) fn extract_body(options: Option<&Value>) -> Option<String> {
111
111
  })
112
112
  }
113
113
 
114
+ /// A single `multipart/form-data` part extracted from a `fetch` options object's `multipart` array.
115
+ pub(crate) enum MultipartPart {
116
+ /// A text field: `{ name, value }`.
117
+ Text { name: String, value: String },
118
+ /// A file field read from disk (binary-safe): `{ name, path, filename?, contentType? }`.
119
+ File {
120
+ name: String,
121
+ path: String,
122
+ filename: Option<String>,
123
+ content_type: Option<String>,
124
+ },
125
+ }
126
+
127
+ /// Extract the `multipart` option — an array of part descriptors — from a `fetch` options object.
128
+ /// Each part is `{ name, value }` (text) or `{ name, path, filename?, contentType? }` (file, read as
129
+ /// bytes so binary uploads work). Returns `None` when there is no usable `multipart` array, so callers
130
+ /// fall back to `body`. A part missing `name`, or with neither `path` nor `value`, is skipped.
131
+ pub(crate) fn extract_multipart(options: Option<&Value>) -> Option<Vec<MultipartPart>> {
132
+ let field = options.and_then(|v| match v {
133
+ Value::Object(obj) => obj.borrow().strings.get("multipart").cloned(),
134
+ _ => None,
135
+ })?;
136
+ let items = match field {
137
+ Value::Array(a) => a.borrow().clone(),
138
+ _ => return None,
139
+ };
140
+ let mut parts = Vec::with_capacity(items.len());
141
+ for item in items {
142
+ let obj = match &item {
143
+ Value::Object(o) => o.borrow(),
144
+ _ => continue,
145
+ };
146
+ let name = match obj.strings.get("name") {
147
+ Some(v) => v.to_display_string(),
148
+ None => continue,
149
+ };
150
+ let filename = obj.strings.get("filename").map(|v| v.to_display_string());
151
+ let content_type = obj.strings.get("contentType").map(|v| v.to_display_string());
152
+ if let Some(path) = obj.strings.get("path").map(|v| v.to_display_string()) {
153
+ parts.push(MultipartPart::File { name, path, filename, content_type });
154
+ } else if let Some(value) = obj.strings.get("value").map(|v| v.to_display_string()) {
155
+ parts.push(MultipartPart::Text { name, value });
156
+ }
157
+ }
158
+ if parts.is_empty() {
159
+ None
160
+ } else {
161
+ Some(parts)
162
+ }
163
+ }
164
+
114
165
  pub(crate) fn build_error_response(error: &str) -> Value {
115
166
  let mut obj: ObjectMap = ObjectMap::with_capacity(2);
116
167
  obj.insert(Arc::from("error"), Value::String(error.into()));
@@ -10,7 +10,10 @@ use futures::Stream;
10
10
  use futures::StreamExt;
11
11
  use tishlang_core::{NativeFn, ObjectMap, TishOpaque, TishPromise, Value};
12
12
 
13
- use crate::http::{build_error_response, extract_body, extract_headers, extract_method};
13
+ use crate::http::{
14
+ build_error_response, extract_body, extract_headers, extract_method, extract_multipart,
15
+ MultipartPart,
16
+ };
14
17
 
15
18
  // --- Promises (Send payloads only; Value built on awaiting thread) ---
16
19
 
@@ -479,6 +482,7 @@ async fn send_request_parts(
479
482
  method: String,
480
483
  headers: Vec<(String, String)>,
481
484
  body: Option<String>,
485
+ multipart: Option<Vec<MultipartPart>>,
482
486
  ) -> Result<reqwest::Response, String> {
483
487
  if fetch_block_private() {
484
488
  ssrf_preflight(&url).await?;
@@ -496,7 +500,39 @@ async fn send_request_parts(
496
500
  for (key, value) in headers {
497
501
  req = req.header(key, value);
498
502
  }
499
- if let Some(body) = body {
503
+ // A `multipart` option takes precedence over `body` — it builds a multipart/form-data request
504
+ // (reqwest sets the Content-Type + boundary). File parts are read as bytes, so binary uploads work.
505
+ if let Some(parts) = multipart {
506
+ let mut form = reqwest::multipart::Form::new();
507
+ for part in parts {
508
+ match part {
509
+ MultipartPart::Text { name, value } => {
510
+ form = form.text(name, value);
511
+ }
512
+ MultipartPart::File {
513
+ name,
514
+ path,
515
+ filename,
516
+ content_type,
517
+ } => {
518
+ let bytes = std::fs::read(&path)
519
+ .map_err(|e| format!("multipart file '{path}': {e}"))?;
520
+ let fname = filename.unwrap_or_else(|| {
521
+ std::path::Path::new(&path)
522
+ .file_name()
523
+ .map(|s| s.to_string_lossy().into_owned())
524
+ .unwrap_or_else(|| name.clone())
525
+ });
526
+ let mut p = reqwest::multipart::Part::bytes(bytes).file_name(fname);
527
+ if let Some(ct) = content_type {
528
+ p = p.mime_str(&ct).map_err(|e| e.to_string())?;
529
+ }
530
+ form = form.part(name, p);
531
+ }
532
+ }
533
+ }
534
+ req = req.multipart(form);
535
+ } else if let Some(body) = body {
500
536
  req = req.body(body);
501
537
  }
502
538
  req.send().await.map_err(|e| e.to_string())
@@ -517,10 +553,11 @@ pub fn fetch_promise_from_args(args: Vec<Value>) -> Value {
517
553
  let method = extract_method(args.get(1));
518
554
  let headers = extract_headers(args.get(1));
519
555
  let body = extract_body(args.get(1));
556
+ let multipart = extract_multipart(args.get(1));
520
557
  let (tx, rx) = tokio::sync::oneshot::channel();
521
558
  crate::http::RUNTIME.with(|rt| {
522
559
  rt.spawn(async move {
523
- let r = send_request_parts(url, method, headers, body).await;
560
+ let r = send_request_parts(url, method, headers, body, multipart).await;
524
561
  let _ = tx.send(r);
525
562
  });
526
563
  });
@@ -541,7 +578,13 @@ pub fn fetch_all_promise_from_args(args: Vec<Value>) -> Value {
541
578
  }
542
579
  };
543
580
  #[allow(clippy::type_complexity)]
544
- let mut parts: Vec<(String, String, Vec<(String, String)>, Option<String>)> = Vec::new();
581
+ let mut parts: Vec<(
582
+ String,
583
+ String,
584
+ Vec<(String, String)>,
585
+ Option<String>,
586
+ Option<Vec<MultipartPart>>,
587
+ )> = Vec::new();
545
588
  for req in requests {
546
589
  let (url, opt) = match &req {
547
590
  Value::String(s) => (s.to_string(), None),
@@ -576,14 +619,15 @@ pub fn fetch_all_promise_from_args(args: Vec<Value>) -> Value {
576
619
  let method = extract_method(opt.as_ref());
577
620
  let headers = extract_headers(opt.as_ref());
578
621
  let body = extract_body(opt.as_ref());
579
- parts.push((url, method, headers, body));
622
+ let multipart = extract_multipart(opt.as_ref());
623
+ parts.push((url, method, headers, body, multipart));
580
624
  }
581
625
  let (tx, rx) = tokio::sync::oneshot::channel();
582
626
  crate::http::RUNTIME.with(|rt| {
583
627
  rt.spawn(async move {
584
628
  let futs: Vec<_> = parts
585
629
  .into_iter()
586
- .map(|(url, m, h, b)| send_request_parts(url, m, h, b))
630
+ .map(|(url, m, h, b, mp)| send_request_parts(url, m, h, b, mp))
587
631
  .collect();
588
632
  let results = futures::future::join_all(futs).await;
589
633
  let mapped: Vec<Result<reqwest::Response, String>> = results.into_iter().collect();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tishlang/tish-format",
3
- "version": "2.36.0",
3
+ "version": "2.37.0",
4
4
  "description": "Tish code formatter",
5
5
  "license": "PIF",
6
6
  "repository": {
Binary file
Binary file
Binary file
Binary file
Binary file