@yuneta/gobj-js 7.26.20 → 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.
@@ -3524,77 +3524,249 @@
3524
3524
  }
3525
3525
  /************************************************************
3526
3526
  * A credential by its SHAPE, whatever names it -- the C kernel's
3527
- * jwt_len() / bearer_token_len(), the shapes the agent's audit
3528
- * record redacts:
3529
- * - a JWT: "eyJ" (the base64url of '{"') at the start of a
3530
- * word, and three parts of base64url joined by '.'; the dots
3531
- * at the end of the run are not its own (a JWT ends a
3532
- * sentence: "... eyJ.eyJ.sig.");
3533
- * - the token after "Bearer " (any case, a word of its own).
3534
- ************************************************************/
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().
3536
+ ************************************************************/
3537
+ var MASK_TOKEN_MIN = 8;
3538
+ var MASK_PROSE_MAX = 24;
3535
3539
  function is_ascii_alnum_code(code) {
3536
3540
  return code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122;
3537
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
+ }
3538
3548
  function is_base64url_code(code) {
3539
3549
  return is_ascii_alnum_code(code) || code === 45 || code === 95;
3540
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
+ }
3541
3560
  function jwt_len(str, p) {
3542
3561
  if (str.length - p < 3 || str[p] !== "e" || str[p + 1] !== "y" || str[p + 2] !== "J") return 0;
3543
3562
  if (p > 0 && (is_base64url_code(str.charCodeAt(p - 1)) || str[p - 1] === ".")) return 0;
3544
3563
  let e = p;
3545
- let dots = 0;
3564
+ let dotted = false;
3546
3565
  while (e < str.length && (is_base64url_code(str.charCodeAt(e)) || str[e] === ".")) {
3547
- if (str[e] === ".") dots++;
3566
+ if (str[e] === ".") dotted = true;
3548
3567
  e++;
3549
3568
  }
3550
- while (str[e - 1] === ".") {
3551
- e--;
3552
- dots--;
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
+ }
3553
3631
  }
3554
- return dots === 2 ? e - p : 0;
3632
+ return false;
3555
3633
  }
3556
- var BEARER_STOPS = " \r\n'\",;}]\0";
3557
- function bearer_token(str, p) {
3558
- const word_len = 6;
3559
- if (str.length - p <= word_len || str.slice(p, p + word_len).toLowerCase() !== "bearer") return [0, 0];
3560
- if (p > 0 && is_ascii_alnum_code(str.charCodeAt(p - 1))) return [0, 0];
3561
- let t = p + word_len;
3562
- if (str[t] !== " " && str[t] !== " ") return [0, 0];
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;
3563
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
+ }
3564
3654
  let e = t;
3565
- while (e < str.length && BEARER_STOPS.indexOf(str[e]) < 0) e++;
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
+ }
3566
3666
  return [t - p, e - t];
3567
3667
  }
3568
- /************************************************************
3569
- * A text (a command line) with the value of every "name=value"
3570
- * whose name is a secret's written as "********", quoted or not
3571
- * (and the "value" of a write-attr whose attribute names a
3572
- * secret); and a JWT or the token after "Bearer ", wherever they
3573
- * are, whatever names them. The rest as it is. Return null when
3574
- * there was nothing to mask. As the C kernel's
3575
- * mask_secrets_inline().
3576
- ************************************************************/
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
+ }
3577
3756
  var MASK_MAX_NAME = 128;
3578
3757
  var MASK_MAX_BYTES = 4194304;
3579
3758
  var MASK_TOO_LARGE = "<not shown: too large to mask>";
3580
- function mask_secrets_inline(str) {
3581
- if (typeof str !== "string") return null;
3582
- if (str.indexOf("=") < 0 && str.indexOf("eyJ") < 0 && !/bearer/i.test(str)) return null;
3583
- if (str.length > MASK_MAX_BYTES) return MASK_TOO_LARGE;
3584
- let value_is_secret = false;
3585
- let re_attr = /(^|[ \t])attribute=["']?([^ \t"']*)/g;
3586
- let m;
3587
- while ((m = re_attr.exec(str)) !== null) if (is_secret_name(m[2].slice(-128))) value_is_secret = true;
3588
- let parts = [];
3589
- let copied = 0;
3590
- 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";
3591
3764
  let outer = "";
3592
3765
  let word = 0;
3593
3766
  let p = 0;
3594
- const is_blank_or_end = (i) => i >= str.length || str[i] === " " || str[i] === " ";
3595
3767
  while (p < str.length) {
3596
- let c = str[p];
3597
- if (c === " " || c === " ") {
3768
+ const c = str[p];
3769
+ if (c === " " || c === " " || c === "\0") {
3598
3770
  p++;
3599
3771
  word = p;
3600
3772
  continue;
@@ -3603,48 +3775,116 @@
3603
3775
  if (!outer && (p === 0 || str[p - 1] === " " || str[p - 1] === " " || str[p - 1] === "=")) outer = c;
3604
3776
  else if (c === outer && is_blank_or_end(p + 1)) outer = "";
3605
3777
  }
3606
- let shape = c === "e" ? jwt_len(str, p) : 0;
3607
- let token_offset = 0;
3608
- if (c === "b" || c === "B") [token_offset, shape] = bearer_token(str, p);
3609
- if (shape) {
3610
- parts.push(str.substring(copied, p + token_offset));
3611
- parts.push("********");
3612
- p += token_offset + shape;
3613
- copied = p;
3614
- changed = true;
3615
- word = p;
3616
- continue;
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
+ }
3617
3785
  }
3618
3786
  if (c !== "=") {
3619
3787
  p++;
3620
3788
  continue;
3621
3789
  }
3622
- 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);
3623
3792
  word = p + 1;
3624
- 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"))) {
3625
3794
  p++;
3626
3795
  continue;
3627
3796
  }
3628
3797
  p++;
3629
- let quote = str[p] === "\"" || str[p] === "'" ? str[p] : "";
3630
3798
  let end;
3799
+ const quote = p < str.length && (str[p] === "\"" || str[p] === "'") ? str[p] : "";
3631
3800
  if (quote) {
3632
3801
  end = p + 1;
3633
- while (end < str.length && str[end] !== quote) end++;
3634
- 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++;
3635
3804
  } else end = inline_secret_value_end(str, p, outer);
3636
- if (end > p) {
3637
- 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++;
3638
3879
  parts.push("********");
3639
- copied = end;
3640
3880
  changed = true;
3881
+ } else {
3882
+ while (j < str.length && !marks[j]) j++;
3883
+ parts.push(str.substring(i, j));
3641
3884
  }
3642
- p = end;
3643
- word = p;
3885
+ i = j;
3644
3886
  }
3645
- if (!changed) return null;
3646
- parts.push(str.substring(copied));
3647
- return parts.join("");
3887
+ return changed ? parts.join("") : null;
3648
3888
  }
3649
3889
  /************************************************************
3650
3890
  * A json as a log or a trace may show it: at any depth, the