@socprime/logtotal-sanitizer 0.2.0-beta.0 → 0.2.0-beta.1

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/cli.js CHANGED
@@ -1571,13 +1571,15 @@ var hostnamesRule = {
1571
1571
  "host",
1572
1572
  "fqdn",
1573
1573
  "domain",
1574
+ "computer",
1574
1575
  "computerName",
1575
1576
  "serverName",
1576
1577
  "nodeName",
1577
1578
  "dnsName",
1578
1579
  "machineName",
1579
1580
  "targetHost"
1580
- ]
1581
+ ],
1582
+ jsonKeyContains: ["computer", "domainname"]
1581
1583
  };
1582
1584
 
1583
1585
  // src/rules/definitions/ipAddresses.ts
@@ -2366,7 +2368,8 @@ var usersRule = {
2366
2368
  "surname",
2367
2369
  "userId",
2368
2370
  "uid"
2369
- ]
2371
+ ],
2372
+ jsonKeyContains: ["owner", "username"]
2370
2373
  };
2371
2374
 
2372
2375
  // src/rules/registry.ts
@@ -2862,27 +2865,44 @@ function looksLikeJson(line) {
2862
2865
  function normalizeKey2(key) {
2863
2866
  return key.toLowerCase().replace(/[-_]/g, "");
2864
2867
  }
2865
- function buildJsonKeyIndex(rules) {
2866
- const index = /* @__PURE__ */ new Map();
2868
+ function buildJsonKeyMatcher(rules) {
2869
+ const exact = /* @__PURE__ */ new Map();
2870
+ const contains = [];
2867
2871
  for (const rule of rules) {
2868
2872
  for (const key of rule.jsonKeys ?? []) {
2869
2873
  const normalized = normalizeKey2(key);
2870
- if (!index.has(normalized)) {
2871
- index.set(normalized, rule);
2874
+ if (!exact.has(normalized)) {
2875
+ exact.set(normalized, rule);
2872
2876
  }
2873
2877
  }
2878
+ for (const fragment of rule.jsonKeyContains ?? []) {
2879
+ contains.push({ fragment: normalizeKey2(fragment), rule });
2880
+ }
2881
+ }
2882
+ return { exact, contains };
2883
+ }
2884
+ function resolveJsonKeyRule(matcher, key) {
2885
+ const normalized = normalizeKey2(key);
2886
+ const exact = matcher.exact.get(normalized);
2887
+ if (exact) {
2888
+ return exact;
2889
+ }
2890
+ for (const entry of matcher.contains) {
2891
+ if (normalized.includes(entry.fragment)) {
2892
+ return entry.rule;
2893
+ }
2874
2894
  }
2875
- return index;
2895
+ return void 0;
2876
2896
  }
2877
- var jsonKeyIndexCache = /* @__PURE__ */ new WeakMap();
2878
- function jsonKeyIndex(rules) {
2879
- const cached = jsonKeyIndexCache.get(rules);
2897
+ var jsonKeyMatcherCache = /* @__PURE__ */ new WeakMap();
2898
+ function jsonKeyMatcher(rules) {
2899
+ const cached = jsonKeyMatcherCache.get(rules);
2880
2900
  if (cached) {
2881
2901
  return cached;
2882
2902
  }
2883
- const index = buildJsonKeyIndex(rules);
2884
- jsonKeyIndexCache.set(rules, index);
2885
- return index;
2903
+ const matcher = buildJsonKeyMatcher(rules);
2904
+ jsonKeyMatcherCache.set(rules, matcher);
2905
+ return matcher;
2886
2906
  }
2887
2907
  function mergeCounts(target, source) {
2888
2908
  for (const [id, count] of Object.entries(source)) {
@@ -2972,7 +2992,7 @@ function redactJsonLine(line, ctx, options = {}) {
2972
2992
  const withSegments = options.withSegments ?? false;
2973
2993
  const withMatches = options.withMatches ?? false;
2974
2994
  const collectOriginals = withMatches || withSegments;
2975
- const keyIndex = jsonKeyIndex(ctx.rules);
2995
+ const keyMatcher = jsonKeyMatcher(ctx.rules);
2976
2996
  const counts = {};
2977
2997
  const matches = [];
2978
2998
  const originals = [];
@@ -2995,7 +3015,7 @@ function redactJsonLine(line, ctx, options = {}) {
2995
3015
  };
2996
3016
  const redactValue = (value, key) => {
2997
3017
  if (typeof value === "string") {
2998
- const fieldRule = key === void 0 ? void 0 : keyIndex.get(normalizeKey2(key));
3018
+ const fieldRule = key === void 0 ? void 0 : resolveJsonKeyRule(keyMatcher, key);
2999
3019
  if (fieldRule) {
3000
3020
  if (isAllowed(ctx.allow, fieldRule.id, value)) {
3001
3021
  return value;
@@ -3220,6 +3240,14 @@ function validateRule(rule) {
3220
3240
  requireText(key, id, `jsonKeys[${index}]`);
3221
3241
  }
3222
3242
  }
3243
+ if (rule.jsonKeyContains !== void 0) {
3244
+ if (!Array.isArray(rule.jsonKeyContains)) {
3245
+ throw new InvalidRuleError(`Rule "${id}": \`jsonKeyContains\` must be an array of strings.`);
3246
+ }
3247
+ for (const [index, fragment] of rule.jsonKeyContains.entries()) {
3248
+ requireText(fragment, id, `jsonKeyContains[${index}]`);
3249
+ }
3250
+ }
3223
3251
  if (rule.validate !== void 0 && typeof rule.validate !== "function") {
3224
3252
  throw new InvalidRuleError(`Rule "${id}": \`validate\` must be a function.`);
3225
3253
  }