@yuneta/gobj-js 7.26.19 → 7.26.21

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.
@@ -3439,6 +3439,11 @@
3439
3439
  "sessionkey",
3440
3440
  "authdata"
3441
3441
  ];
3442
+ var SECRET_NAME_SEGMENTS = /* @__PURE__ */ new Set([
3443
+ "pass",
3444
+ "pin",
3445
+ "otp"
3446
+ ]);
3442
3447
  var NOT_SECRET_SEGMENTS = /* @__PURE__ */ new Set([
3443
3448
  "endpoint",
3444
3449
  "url",
@@ -3471,10 +3476,16 @@
3471
3476
  if (typeof name !== "string" || name.length < 3) return false;
3472
3477
  let lower = name.toLowerCase();
3473
3478
  let hit = false;
3474
- for (const part of SECRET_NAME_PARTS) if (lower.indexOf(part) >= 0) {
3479
+ for (const segment of lower.split(/[^a-z0-9]+/)) if (SECRET_NAME_SEGMENTS.has(segment)) {
3475
3480
  hit = true;
3476
3481
  break;
3477
3482
  }
3483
+ if (!hit) {
3484
+ for (const part of SECRET_NAME_PARTS) if (lower.indexOf(part) >= 0) {
3485
+ hit = true;
3486
+ break;
3487
+ }
3488
+ }
3478
3489
  if (!hit) {
3479
3490
  let joined = lower.replace(/[_\-. \t]/g, "");
3480
3491
  for (const part of SECRET_NAME_JOINED) if (joined.indexOf(part) >= 0) {
@@ -3512,32 +3523,250 @@
3512
3523
  }
3513
3524
  }
3514
3525
  /************************************************************
3515
- * A text (a command line) with the value of every "name=value"
3516
- * whose name is a secret's written as "********", quoted or not
3517
- * (and the "value" of a write-attr whose attribute names a
3518
- * secret). The rest as it is. Return null when there was nothing
3519
- * to mask. As the C kernel's mask_secrets_inline().
3526
+ * A credential by its SHAPE, whatever names it -- the C kernel's
3527
+ * jwt_len() / scheme_credential(), the shapes the agent's audit
3528
+ * record redacts, and the schemes of an HTTP Authorization:
3529
+ * - a JWT or a JWE: "eyJ" (the base64url of '{"') at the start
3530
+ * of a word, base64url, and a '.' after it -- three parts
3531
+ * joined by '.' (a JWS), five (a JWE), or fewer when a cap cut
3532
+ * it; the dots at the end of the run are not its own (a JWT
3533
+ * ends a sentence: "... eyJ.eyJ.sig.");
3534
+ * - the credential after the scheme word "Bearer" or "Basic"
3535
+ * (any case, a word of its own): scheme_credential().
3520
3536
  ************************************************************/
3537
+ var MASK_TOKEN_MIN = 8;
3538
+ var MASK_PROSE_MAX = 24;
3539
+ function is_ascii_alnum_code(code) {
3540
+ return code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122;
3541
+ }
3542
+ function is_ascii_letter_code(code) {
3543
+ return code >= 65 && code <= 90 || code >= 97 && code <= 122;
3544
+ }
3545
+ function is_ascii_upper_code(code) {
3546
+ return code >= 65 && code <= 90;
3547
+ }
3548
+ function is_base64url_code(code) {
3549
+ return is_ascii_alnum_code(code) || code === 45 || code === 95;
3550
+ }
3551
+ function ascii_case_equal(str, p, word) {
3552
+ if (str.length - p < word.length) return false;
3553
+ for (let i = 0; i < word.length; i++) {
3554
+ let code = str.charCodeAt(p + i);
3555
+ if (code >= 65 && code <= 90) code += 32;
3556
+ if (code !== word.charCodeAt(i)) return false;
3557
+ }
3558
+ return true;
3559
+ }
3560
+ function jwt_len(str, p) {
3561
+ if (str.length - p < 3 || str[p] !== "e" || str[p + 1] !== "y" || str[p + 2] !== "J") return 0;
3562
+ if (p > 0 && (is_base64url_code(str.charCodeAt(p - 1)) || str[p - 1] === ".")) return 0;
3563
+ let e = p;
3564
+ let dotted = false;
3565
+ while (e < str.length && (is_base64url_code(str.charCodeAt(e)) || str[e] === ".")) {
3566
+ if (str[e] === ".") dotted = true;
3567
+ e++;
3568
+ }
3569
+ if (!dotted) return 0;
3570
+ while (str[e - 1] === ".") e--;
3571
+ return e - p;
3572
+ }
3573
+ function scheme_word_len(str, p) {
3574
+ if (p > 0 && is_ascii_alnum_code(str.charCodeAt(p - 1))) return 0;
3575
+ for (const scheme of ["bearer", "basic"]) {
3576
+ const n = scheme.length;
3577
+ if (str.length - p > n && ascii_case_equal(str, p, scheme) && (str[p + n] === " " || str[p + n] === " ")) return n;
3578
+ }
3579
+ return 0;
3580
+ }
3581
+ function code_points(str, w, n) {
3582
+ let count = 0;
3583
+ for (let i = w; i < w + n; i++) {
3584
+ const code = str.charCodeAt(i);
3585
+ if (code < 56320 || code > 57343) count++;
3586
+ }
3587
+ return count;
3588
+ }
3589
+ function is_prose_word(str, w, n) {
3590
+ if (n === 0 || n >= MASK_PROSE_MAX) return false;
3591
+ let i = w;
3592
+ const end = w + n;
3593
+ while (true) {
3594
+ let r = i;
3595
+ let upper_rest = true;
3596
+ let lower_rest = true;
3597
+ while (r < end && is_ascii_letter_code(str.charCodeAt(r))) {
3598
+ if (r > i) {
3599
+ if (is_ascii_upper_code(str.charCodeAt(r))) lower_rest = false;
3600
+ else upper_rest = false;
3601
+ }
3602
+ r++;
3603
+ }
3604
+ if (r === i) return false;
3605
+ if (!lower_rest && !(upper_rest && is_ascii_upper_code(str.charCodeAt(i)))) return false;
3606
+ if (r === end) return true;
3607
+ if (str[r] !== "-" && str[r] !== "." && str[r] !== "/" || r + 1 === end) return false;
3608
+ i = r + 1;
3609
+ }
3610
+ }
3611
+ function base64_digit(code) {
3612
+ if (code >= 65 && code <= 90) return code - 65;
3613
+ if (code >= 97 && code <= 122) return code - 97 + 26;
3614
+ if (code >= 48 && code <= 57) return code - 48 + 52;
3615
+ if (code === 43) return 62;
3616
+ if (code === 47) return 63;
3617
+ return -1;
3618
+ }
3619
+ function is_basic_credential(str, w, n) {
3620
+ let bits = 0;
3621
+ let n_bits = 0;
3622
+ for (let i = 0; i < n; i++) {
3623
+ const v = base64_digit(str.charCodeAt(w + i));
3624
+ if (v < 0) return false;
3625
+ bits = (bits << 6 | v) & 16383;
3626
+ n_bits += 6;
3627
+ if (n_bits >= 8) {
3628
+ n_bits -= 8;
3629
+ if ((bits >> n_bits & 255) === 58) return true;
3630
+ }
3631
+ }
3632
+ return false;
3633
+ }
3634
+ var SCHEME_WORD_STOPS = " \r\n\"',;}]\0";
3635
+ function scheme_credential(str, p) {
3636
+ const n = scheme_word_len(str, p);
3637
+ if (!n) return [0, 0];
3638
+ const scheme = n === 5 ? "basic" : "bearer";
3639
+ let context = false;
3640
+ let t = p + n;
3641
+ while (t < str.length && (str[t] === " " || str[t] === " ")) t++;
3642
+ if (t < str.length && scheme_word_len(str, t) === n && ascii_case_equal(str, t, scheme)) {
3643
+ t += n;
3644
+ context = true;
3645
+ while (t < str.length && (str[t] === " " || str[t] === " ")) t++;
3646
+ }
3647
+ if (t >= str.length) return [0, 0];
3648
+ if (str[t] === "\"" || str[t] === "'") {
3649
+ let q = t + 1;
3650
+ while (q < str.length && str[q] !== str[t] && str[q] !== " " && str[q] !== " " && str[q] !== "\r" && str[q] !== "\n" && str[q] !== "\0") q++;
3651
+ if (q < str.length && str[q] === str[t] && q > t + 1) return [t - p, q + 1 - t];
3652
+ return [0, 0];
3653
+ }
3654
+ let e = t;
3655
+ while (e < str.length && SCHEME_WORD_STOPS.indexOf(str[e]) < 0) e++;
3656
+ if (e === t) return [0, 0];
3657
+ if (!context) {
3658
+ let w_end = e;
3659
+ while (w_end > t && str[w_end - 1] === ".") w_end--;
3660
+ const len = w_end - t;
3661
+ if (len === 0 || is_prose_word(str, t, len)) return [0, 0];
3662
+ if (n === 5) {
3663
+ if (!is_basic_credential(str, t, len)) return [0, 0];
3664
+ } else if (code_points(str, t, len) < MASK_TOKEN_MIN) return [0, 0];
3665
+ }
3666
+ return [t - p, e - t];
3667
+ }
3668
+ function shape_len(str, p) {
3669
+ const c = str[p];
3670
+ if (c === "e") return [0, jwt_len(str, p)];
3671
+ if (c === "b" || c === "B") return scheme_credential(str, p);
3672
+ return [0, 0];
3673
+ }
3674
+ function shape_len_7_26_9(str, p) {
3675
+ const c = str[p];
3676
+ if (c === "e") {
3677
+ if (str.length - p < 3 || str[p + 1] !== "y" || str[p + 2] !== "J") return [0, 0];
3678
+ if (p > 0 && (is_base64url_code(str.charCodeAt(p - 1)) || str[p - 1] === ".")) return [0, 0];
3679
+ let e = p;
3680
+ let dots = 0;
3681
+ while (e < str.length && (is_base64url_code(str.charCodeAt(e)) || str[e] === ".")) {
3682
+ if (str[e] === ".") dots++;
3683
+ e++;
3684
+ }
3685
+ while (str[e - 1] === ".") {
3686
+ e--;
3687
+ dots--;
3688
+ }
3689
+ return dots === 2 ? [0, e - p] : [0, 0];
3690
+ }
3691
+ if (c === "b" || c === "B") {
3692
+ if (str.length - p <= 6 || !ascii_case_equal(str, p, "bearer")) return [0, 0];
3693
+ if (p > 0 && is_ascii_alnum_code(str.charCodeAt(p - 1))) return [0, 0];
3694
+ let t = p + 6;
3695
+ if (str[t] !== " " && str[t] !== " ") return [0, 0];
3696
+ while (t < str.length && (str[t] === " " || str[t] === " ")) t++;
3697
+ let e = t;
3698
+ while (e < str.length && " \r\n'\",;}]\0".indexOf(str[e]) < 0) e++;
3699
+ return [t - p, e - t];
3700
+ }
3701
+ return [0, 0];
3702
+ }
3703
+ var COLON_VALUE_STOPS = " \r\n,;&)]}<>\"'\0";
3704
+ function colon_value_end(str, v) {
3705
+ if (v >= str.length) return v;
3706
+ const c = str[v];
3707
+ if (c === "\"" || c === "'") {
3708
+ let e = v + 1;
3709
+ while (e < str.length && str[e] !== c) {
3710
+ if (str[e] === "\\" && e + 1 < str.length) e++;
3711
+ e++;
3712
+ }
3713
+ return e < str.length ? e + 1 : str.length;
3714
+ }
3715
+ if (c === "{" || c === "[") {
3716
+ let depth = 0;
3717
+ let in_string = false;
3718
+ for (let e = v; e < str.length; e++) if (in_string) {
3719
+ if (str[e] === "\\" && e + 1 < str.length) e++;
3720
+ else if (str[e] === "\"") in_string = false;
3721
+ } else if (str[e] === "\"") in_string = true;
3722
+ else if (str[e] === "{" || str[e] === "[") depth++;
3723
+ else if (str[e] === "}" || str[e] === "]") {
3724
+ depth--;
3725
+ if (depth === 0) return e + 1;
3726
+ }
3727
+ return str.length;
3728
+ }
3729
+ if (c === "\\") {
3730
+ let q = v;
3731
+ while (q < str.length && str[q] === "\\") q++;
3732
+ if (q < str.length && (str[q] === "\"" || str[q] === "'")) {
3733
+ const level = q - v;
3734
+ let run = 0;
3735
+ for (let e = q + 1; e < str.length; e++) {
3736
+ if (str[e] === "\\") {
3737
+ run++;
3738
+ continue;
3739
+ }
3740
+ if (str[e] === str[q] && run === level) return e + 1;
3741
+ run = 0;
3742
+ }
3743
+ return str.length;
3744
+ }
3745
+ }
3746
+ let e = v;
3747
+ const n = scheme_word_len(str, v);
3748
+ if (n) {
3749
+ e = v + n;
3750
+ while (e < str.length && (str[e] === " " || str[e] === " ")) e++;
3751
+ if (e < str.length && (str[e] === "\"" || str[e] === "'")) return colon_value_end(str, e);
3752
+ }
3753
+ while (e < str.length && COLON_VALUE_STOPS.indexOf(str[e]) < 0) e++;
3754
+ return e;
3755
+ }
3521
3756
  var MASK_MAX_NAME = 128;
3522
3757
  var MASK_MAX_BYTES = 4194304;
3523
3758
  var MASK_TOO_LARGE = "<not shown: too large to mask>";
3524
- function mask_secrets_inline(str) {
3525
- if (typeof str !== "string" || str.indexOf("=") < 0) return null;
3526
- if (str.length > MASK_MAX_BYTES) return MASK_TOO_LARGE;
3527
- let value_is_secret = false;
3528
- let re_attr = /(^|[ \t])attribute=["']?([^ \t"']*)/g;
3529
- let m;
3530
- while ((m = re_attr.exec(str)) !== null) if (is_secret_name(m[2].slice(-128))) value_is_secret = true;
3531
- let parts = [];
3532
- let copied = 0;
3533
- let changed = false;
3759
+ function mark_range(marks, from, to) {
3760
+ for (let i = from; i < to; i++) marks[i] = 1;
3761
+ }
3762
+ function mark_params(str, value_is_secret, as_7_26_9, marks) {
3763
+ const is_blank_or_end = (i) => i >= str.length || str[i] === " " || str[i] === " " || str[i] === "\0";
3534
3764
  let outer = "";
3535
3765
  let word = 0;
3536
3766
  let p = 0;
3537
- const is_blank_or_end = (i) => i >= str.length || str[i] === " " || str[i] === " ";
3538
3767
  while (p < str.length) {
3539
- let c = str[p];
3540
- if (c === " " || c === " ") {
3768
+ const c = str[p];
3769
+ if (c === " " || c === " " || c === "\0") {
3541
3770
  p++;
3542
3771
  word = p;
3543
3772
  continue;
@@ -3546,43 +3775,124 @@
3546
3775
  if (!outer && (p === 0 || str[p - 1] === " " || str[p - 1] === " " || str[p - 1] === "=")) outer = c;
3547
3776
  else if (c === outer && is_blank_or_end(p + 1)) outer = "";
3548
3777
  }
3778
+ if (as_7_26_9) {
3779
+ const [offset, n] = shape_len_7_26_9(str, p);
3780
+ if (n) {
3781
+ p += offset + n;
3782
+ word = p;
3783
+ continue;
3784
+ }
3785
+ }
3549
3786
  if (c !== "=") {
3550
3787
  p++;
3551
3788
  continue;
3552
3789
  }
3553
- let name = str.substring(Math.max(word, p - MASK_MAX_NAME), p);
3790
+ const name_from = Math.max(word, p - MASK_MAX_NAME);
3791
+ const name = str.substring(name_from, p);
3554
3792
  word = p + 1;
3555
- if (!(is_secret_name(name) || value_is_secret && name.toLowerCase() === "value")) {
3793
+ if (!(is_secret_name(name) || value_is_secret && name.length === 5 && ascii_case_equal(str, name_from, "value"))) {
3556
3794
  p++;
3557
3795
  continue;
3558
3796
  }
3559
3797
  p++;
3560
- let quote = str[p] === "\"" || str[p] === "'" ? str[p] : "";
3561
3798
  let end;
3799
+ const quote = p < str.length && (str[p] === "\"" || str[p] === "'") ? str[p] : "";
3562
3800
  if (quote) {
3563
3801
  end = p + 1;
3564
- while (end < str.length && str[end] !== quote) end++;
3565
- if (str[end] === quote) end++;
3802
+ while (end < str.length && str[end] !== quote && str[end] !== "\0") end++;
3803
+ if (end < str.length && str[end] === quote) end++;
3566
3804
  } else end = inline_secret_value_end(str, p, outer);
3567
- if (end > p) {
3568
- parts.push(str.substring(copied, p));
3805
+ mark_range(marks, p, end);
3806
+ p = end;
3807
+ word = p;
3808
+ }
3809
+ }
3810
+ function mark_shapes(str, marks) {
3811
+ for (let p = 0; p < str.length; p++) {
3812
+ const c = str[p];
3813
+ if (c !== "e" && c !== "b" && c !== "B") continue;
3814
+ const [offset, n] = shape_len(str, p);
3815
+ if (n) mark_range(marks, p + offset, p + offset + n);
3816
+ }
3817
+ }
3818
+ function mark_labels(str, marks) {
3819
+ let word = 0;
3820
+ let p = 0;
3821
+ while (p < str.length) {
3822
+ const c = str[p];
3823
+ if (c === " " || c === " " || c === "\0" || c === "=") {
3824
+ p++;
3825
+ word = p;
3826
+ continue;
3827
+ }
3828
+ if (c !== ":") {
3829
+ p++;
3830
+ continue;
3831
+ }
3832
+ if (!is_secret_name(str.substring(Math.max(word, p - MASK_MAX_NAME), p))) {
3833
+ p++;
3834
+ word = p;
3835
+ continue;
3836
+ }
3837
+ let v = p + 1;
3838
+ while (v < str.length && (str[v] === " " || str[v] === " ")) v++;
3839
+ const e = colon_value_end(str, v);
3840
+ mark_range(marks, v, e);
3841
+ const n_scheme = scheme_word_len(str, v);
3842
+ if (n_scheme) p = v + n_scheme;
3843
+ else p = e > v ? e : p + 1;
3844
+ word = p;
3845
+ }
3846
+ }
3847
+ /************************************************************
3848
+ * A text (a command line) with the secrets that can be told
3849
+ * written as "********" (each run of them as one), the rest as it
3850
+ * is: the value of every "name=value" whose name is a secret's,
3851
+ * quoted or not (and the "value" of a write-attr whose attribute
3852
+ * names a secret); the same for a "name: value", its name in quotes
3853
+ * or not (a json, a header, a label: "password": x, Authorization:
3854
+ * Basic x, bearer: tok); and a JWT or the credential after "Bearer "
3855
+ * or "Basic ", wherever they are, whatever names them. Each rule
3856
+ * judges the text as it came. Return null when there was nothing to
3857
+ * mask. As the C kernel's mask_secrets_inline().
3858
+ ************************************************************/
3859
+ function mask_secrets_inline(str) {
3860
+ if (typeof str !== "string") return null;
3861
+ if (str.indexOf("=") < 0 && str.indexOf(":") < 0 && str.indexOf("eyJ") < 0 && !/bearer|basic/i.test(str)) return null;
3862
+ if (str.length > MASK_MAX_BYTES) return MASK_TOO_LARGE;
3863
+ let value_is_secret = false;
3864
+ let re_attr = /(^|[ \t])attribute=["']?([^ \t"'\u0000]*)/g;
3865
+ let m;
3866
+ while ((m = re_attr.exec(str)) !== null) if (is_secret_name(m[2].slice(-128))) value_is_secret = true;
3867
+ const marks = new Uint8Array(str.length);
3868
+ mark_params(str, value_is_secret, true, marks);
3869
+ mark_params(str, value_is_secret, false, marks);
3870
+ mark_shapes(str, marks);
3871
+ mark_labels(str, marks);
3872
+ let parts = [];
3873
+ let changed = false;
3874
+ let i = 0;
3875
+ while (i < str.length) {
3876
+ let j = i;
3877
+ if (marks[i]) {
3878
+ while (j < str.length && marks[j]) j++;
3569
3879
  parts.push("********");
3570
- copied = end;
3571
3880
  changed = true;
3881
+ } else {
3882
+ while (j < str.length && !marks[j]) j++;
3883
+ parts.push(str.substring(i, j));
3572
3884
  }
3573
- p = end;
3574
- word = p;
3885
+ i = j;
3575
3886
  }
3576
- if (!changed) return null;
3577
- parts.push(str.substring(copied));
3578
- return parts.join("");
3887
+ return changed ? parts.join("") : null;
3579
3888
  }
3580
3889
  /************************************************************
3581
3890
  * A json as a log or a trace may show it: at any depth, the
3582
3891
  * value of a key with a secret's name is "********", whatever
3583
3892
  * its type (not an absent one, a null or an empty string); so is
3584
3893
  * the "value" of a dict whose "attribute" names a secret; and a
3585
- * string is masked as mask_secrets_inline(). Return a masked
3894
+ * string, under any key, is masked as mask_secrets_inline() (a
3895
+ * JWT, a "Bearer" token, a secret name=value). Return a masked
3586
3896
  * copy, or `jn` itself when there was nothing to mask. As the C
3587
3897
  * kernel's json_mask_secrets().
3588
3898
  *
@@ -3611,7 +3921,7 @@
3611
3921
  return MASK_TOO_LARGE;
3612
3922
  }
3613
3923
  budget.left -= jn.length;
3614
- let masked = mask_secrets_inline(jn);
3924
+ let masked = mask_secrets_inline(jn.indexOf("\0") >= 0 ? jn.replace(/\u0000/g, " ") : jn);
3615
3925
  return masked !== null ? masked : jn;
3616
3926
  }
3617
3927
  let is_array = Array.isArray(jn);
@@ -7113,15 +7423,34 @@
7113
7423
  return gobj_save_persistent_attrs(gobj, attr);
7114
7424
  }
7115
7425
  /***************************************************************
7116
- * "set" as the C command reads it: TRUE / set / 1, FALSE / reset / 0
7426
+ * "set" as the C command reads it (trace_set_value() of c_yuno.c):
7427
+ * a boolean; an integer in the range of an int (true unless 0); or
7428
+ * text -- set / reset, a JSON integer -?(0|[1-9][0-9]*), or true /
7429
+ * false, yes / no, on / off, any case. null for anything else: a
7430
+ * real (2.5), " 1", "0x1", "1e3", "+5", "007" and "2.5" are refused,
7431
+ * as in C. Up to gobj-js 7.26.19 the text went through Number(),
7432
+ * which took all of them, and "yes" / "on" were refused.
7117
7433
  ***************************************************************/
7434
+ var INT_MIN = -2147483648;
7435
+ var INT_MAX = 2147483647;
7118
7436
  function parse_set(value) {
7119
7437
  if (value === true || value === false) return value;
7120
- let v = String(value === void 0 || value === null ? "" : value).toLowerCase();
7121
- if (v === "true" || v === "set") return true;
7122
- if (v === "false" || v === "reset") return false;
7123
- if (v === "" || isNaN(Number(v))) return null;
7124
- return Number(v) ? true : false;
7438
+ if (typeof value === "number") {
7439
+ if (!Number.isInteger(value) || value < INT_MIN || value > INT_MAX) return null;
7440
+ return value !== 0;
7441
+ }
7442
+ if (typeof value !== "string" || value === "") return null;
7443
+ let v = value.toLowerCase();
7444
+ if (v === "set") return true;
7445
+ if (v === "reset") return false;
7446
+ if (/^-?(0|[1-9][0-9]*)$/.test(value)) {
7447
+ let n = Number(value);
7448
+ if (n < INT_MIN || n > INT_MAX) return null;
7449
+ return n !== 0;
7450
+ }
7451
+ if (v === "true" || v === "yes" || v === "on") return true;
7452
+ if (v === "false" || v === "no" || v === "off") return false;
7453
+ return null;
7125
7454
  }
7126
7455
  function yuno_prefix() {
7127
7456
  return `${gobj_yuno_role()}^${gobj_yuno_name()}`;