@eliware/common 15.0.1 → 15.2.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/AGENTS.md +7 -5
- package/README.md +12 -12
- package/RELEASE_NOTES.md +46 -0
- package/docs/README.md +10 -4
- package/docs/api.md +35 -164
- package/docs/logging.md +66 -0
- package/docs/package.md +44 -30
- package/docs/paths.md +57 -0
- package/docs/process-errors.md +62 -0
- package/docs/redaction.md +115 -0
- package/docs/signals.md +67 -0
- package/docs/snowflake-ids.md +77 -0
- package/docs/troubleshooting.md +33 -29
- package/docs/usage.md +42 -138
- package/package.json +28 -8
- package/specs/README.md +6 -0
- package/specs/directives.yaml +11 -6
- package/specs/errors.yaml +23 -0
- package/specs/log.yaml +19 -0
- package/specs/path.yaml +18 -0
- package/specs/redact.yaml +20 -0
- package/specs/signals.yaml +16 -0
- package/specs/snowflake.yaml +18 -0
- package/src/errors/cleanup/abort-listener.mjs +19 -0
- package/src/errors/cleanup/attached-handlers.mjs +30 -0
- package/src/errors/cleanup/detached-state.mjs +8 -0
- package/src/errors/cleanup/finalize.mjs +5 -0
- package/src/errors/cleanup/listener.mjs +27 -0
- package/src/errors/cleanup.mjs +43 -0
- package/src/errors/constants.mjs +1 -0
- package/src/errors/event/handler.mjs +38 -0
- package/src/errors/event/one-shot.mjs +37 -0
- package/src/errors/event-handlers.mjs +33 -0
- package/src/errors/failure.mjs +10 -0
- package/src/errors/finalize.mjs +27 -0
- package/src/errors/handler-removal.mjs +18 -0
- package/src/errors/index.d.mts +28 -0
- package/src/errors/index.d.ts +28 -0
- package/src/errors/index.mjs +6 -0
- package/src/errors/logging/diagnostic.mjs +9 -0
- package/src/errors/logging/error-diagnostic.mjs +5 -0
- package/src/errors/logging/value-diagnostic.mjs +5 -0
- package/src/errors/logging.mjs +10 -0
- package/src/errors/registration/events.mjs +9 -0
- package/src/errors/registration/lifecycle.mjs +18 -0
- package/src/errors/registration/registry.mjs +5 -0
- package/src/errors/registration/setup-state.mjs +20 -0
- package/src/errors/registration/state.mjs +28 -0
- package/src/errors/registration.mjs +80 -0
- package/src/errors/setup/installation.mjs +52 -0
- package/src/errors/setup/rollback.mjs +35 -0
- package/src/errors/setup.mjs +7 -0
- package/src/errors/validation/events.mjs +17 -0
- package/src/errors/validation/logger.mjs +8 -0
- package/src/errors/validation/process-object.mjs +11 -0
- package/src/errors/validation/signal.mjs +9 -0
- package/src/index.d.ts +14 -14
- package/src/index.mjs +9 -7
- package/src/log/child-logger.mjs +5 -0
- package/src/log/configuration/build-logger.mjs +13 -0
- package/src/log/configuration/default-options.mjs +5 -0
- package/src/log/configuration/normalize-options.mjs +14 -0
- package/src/log/configuration/validate-options.mjs +16 -0
- package/src/log/create-logger.mjs +18 -0
- package/src/log/formats/json-record.mjs +19 -0
- package/src/log/formats/json.mjs +38 -0
- package/src/log/formats/message.mjs +13 -0
- package/src/log/formats/text.mjs +21 -0
- package/src/log/index.d.mts +74 -0
- package/src/log/index.d.ts +74 -0
- package/src/log/index.mjs +15 -0
- package/src/log/logger-levels.mjs +37 -0
- package/src/path/base.mjs +17 -0
- package/src/path/conversions.mjs +7 -0
- package/src/path/create-path-helpers.mjs +58 -0
- package/src/path/current-path.mjs +13 -0
- package/src/path/file-url-to-path.mjs +5 -0
- package/src/path/index.d.mts +66 -0
- package/src/path/index.d.ts +66 -0
- package/src/path/index.mjs +12 -0
- package/src/path/input-classification.mjs +11 -0
- package/src/path/operation/failure.mjs +12 -0
- package/src/path/operation/logging.mjs +24 -0
- package/src/redact/headers/inspect-header-input.mjs +17 -0
- package/src/redact/headers/redact/entries.mjs +33 -0
- package/src/redact/headers/redact/headers.mjs +12 -0
- package/src/redact/headers/valid-entries.mjs +15 -0
- package/src/redact/index.d.mts +49 -0
- package/src/redact/index.d.ts +49 -0
- package/src/redact/index.mjs +7 -0
- package/src/redact/inspection/add-truncation-marker.mjs +13 -0
- package/src/redact/inspection/copy-error-fields.mjs +20 -0
- package/src/redact/inspection/inspect/entries.mjs +15 -0
- package/src/redact/inspection/inspect/error-fields.mjs +44 -0
- package/src/redact/inspection/safe-entries.mjs +7 -0
- package/src/redact/limits/default-limits.mjs +6 -0
- package/src/redact/limits/normalize/output-limit.mjs +8 -0
- package/src/redact/limits/normalize/traversal-limits.mjs +12 -0
- package/src/redact/markers/default-marker.mjs +1 -0
- package/src/redact/policy/create/policy.mjs +9 -0
- package/src/redact/policy/create/readonly-set.mjs +31 -0
- package/src/redact/policy/default/key-names.mjs +23 -0
- package/src/redact/policy/default/policy.mjs +9 -0
- package/src/redact/policy/normalize/keys.mjs +13 -0
- package/src/redact/policy/normalize-policy.mjs +14 -0
- package/src/redact/serialization/safe-serialize.mjs +34 -0
- package/src/redact/serialization/serialize/array.mjs +13 -0
- package/src/redact/serialization/serialize/buffer.mjs +5 -0
- package/src/redact/serialization/serialize/error.mjs +12 -0
- package/src/redact/serialization/serialize/object.mjs +22 -0
- package/src/redact/serialization/serialize/primitive.mjs +21 -0
- package/src/redact/structured/redact/array/attempt.mjs +9 -0
- package/src/redact/structured/redact/array/index-name.mjs +12 -0
- package/src/redact/structured/redact/array/indices.mjs +18 -0
- package/src/redact/structured/redact/array/properties.mjs +60 -0
- package/src/redact/structured/redact/array/result.mjs +11 -0
- package/src/redact/structured/redact/array.mjs +26 -0
- package/src/redact/structured/redact/classify.mjs +13 -0
- package/src/redact/structured/redact/error-fields.mjs +6 -0
- package/src/redact/structured/redact/error.mjs +14 -0
- package/src/redact/structured/redact/object.mjs +23 -0
- package/src/redact/structured/redact/value.mjs +49 -0
- package/src/redact/text/apply/configured/rules.mjs +9 -0
- package/src/redact/text/apply/configured/text-redactions.mjs +11 -0
- package/src/redact/text/apply/redactions.mjs +17 -0
- package/src/redact/text/apply/text-rules.mjs +8 -0
- package/src/redact/text/boundary/detect-jwt.mjs +18 -0
- package/src/redact/text/boundary/finish.mjs +15 -0
- package/src/redact/text/boundary/input.mjs +10 -0
- package/src/redact/text/boundary/jwt.mjs +22 -0
- package/src/redact/text/boundary/prepare.mjs +16 -0
- package/src/redact/text/boundary/process.mjs +9 -0
- package/src/redact/text/boundary/redaction.mjs +20 -0
- package/src/redact/text/boundary/secrets.mjs +21 -0
- package/src/redact/text/limit-output.mjs +12 -0
- package/src/redact/text/patterns/secret-key-patterns.mjs +2 -0
- package/src/redact/text/redact-text.mjs +8 -0
- package/src/redact/text/replace-literal/matches.mjs +39 -0
- package/src/redact/text/replace-literal/secret.mjs +6 -0
- package/src/redact/text/replace-literal/secrets.mjs +18 -0
- package/src/redact/text/replace-literal/trie.mjs +36 -0
- package/src/redact/text/rules/built-in.mjs +8 -0
- package/src/redact/text/rules/redact/authorization.mjs +12 -0
- package/src/redact/text/rules/redact/jwt/pattern.mjs +2 -0
- package/src/redact/text/rules/redact/jwt.mjs +10 -0
- package/src/redact/text/rules/redact/key/configured-values.mjs +25 -0
- package/src/redact/text/rules/redact/key/material.mjs +9 -0
- package/src/redact/text/rules/redact/key/values.mjs +16 -0
- package/src/redact/text/rules/redact/provider-token.mjs +9 -0
- package/src/signals/index.d.mts +29 -0
- package/src/signals/index.d.ts +29 -0
- package/src/signals/index.mjs +1 -0
- package/src/signals/lifecycle/abort/aggregate-cleanup.mjs +21 -0
- package/src/signals/lifecycle/abort/cleanup.mjs +9 -0
- package/src/signals/lifecycle/abort/listener-registration.mjs +14 -0
- package/src/signals/lifecycle/abort/listener.mjs +16 -0
- package/src/signals/lifecycle/abort/registration-cleanup.mjs +14 -0
- package/src/signals/lifecycle/abort/registration.mjs +19 -0
- package/src/signals/lifecycle/before/exit-registration.mjs +36 -0
- package/src/signals/lifecycle/before/exit.mjs +33 -0
- package/src/signals/lifecycle/cleanup/process-handlers.mjs +27 -0
- package/src/signals/lifecycle/cleanup/registration.mjs +5 -0
- package/src/signals/lifecycle/cleanup.mjs +17 -0
- package/src/signals/lifecycle/defaults.mjs +1 -0
- package/src/signals/lifecycle/exit-policy.mjs +17 -0
- package/src/signals/lifecycle/handlers/rollback.mjs +14 -0
- package/src/signals/lifecycle/handlers.mjs +35 -0
- package/src/signals/lifecycle/hooks.mjs +19 -0
- package/src/signals/lifecycle/logging.mjs +11 -0
- package/src/signals/lifecycle/registration-policy.mjs +29 -0
- package/src/signals/lifecycle/registration.mjs +7 -0
- package/src/signals/lifecycle/shutdown.mjs +75 -0
- package/src/signals/lifecycle/validation/lifecycle-options.mjs +8 -0
- package/src/signals/lifecycle/validation/logger.mjs +9 -0
- package/src/signals/lifecycle/validation/options.mjs +19 -0
- package/src/signals/lifecycle/validation/registration-options.mjs +36 -0
- package/src/signals/lifecycle/validation/signals.mjs +9 -0
- package/src/signals/register.mjs +76 -0
- package/src/snowflake/constants.mjs +15 -0
- package/src/snowflake/generator-options.mjs +27 -0
- package/src/snowflake/generator.mjs +82 -0
- package/src/snowflake/index.d.mts +24 -0
- package/src/snowflake/index.d.ts +24 -0
- package/src/snowflake/index.mjs +8 -0
- package/src/snowflake/log-generation-failure.mjs +9 -0
- package/src/snowflake/logger-validation.mjs +10 -0
- package/src/snowflake/validation.mjs +18 -0
- package/src/errors.d.mts +0 -9
- package/src/errors.mjs +0 -4
- package/src/fs.mjs +0 -4
- package/src/path.d.mts +0 -12
- package/src/path.mjs +0 -22
- package/src/signals.mjs +0 -4
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
function inspectEntries(value, maxEntries = Infinity) {
|
|
2
|
+
try {
|
|
3
|
+
const keys = Object.keys(value);
|
|
4
|
+
const entries = [];
|
|
5
|
+
for (const key of keys) {
|
|
6
|
+
if (entries.length >= maxEntries) return { entries, keys, failed: false, truncated: true };
|
|
7
|
+
entries.push([key, value[key]]);
|
|
8
|
+
}
|
|
9
|
+
return { entries, keys, failed: false, truncated: false };
|
|
10
|
+
} catch {
|
|
11
|
+
return { entries: [], keys: [], failed: true, truncated: false };
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default inspectEntries;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import inspectEntries from "./entries.mjs";
|
|
2
|
+
|
|
3
|
+
function inspectErrorFields(error, maxKeys = Infinity) {
|
|
4
|
+
const inspected = inspectEntries(error, maxKeys);
|
|
5
|
+
if (inspected.failed) return { fields: [], failed: true, truncated: false };
|
|
6
|
+
const { entries, keys } = inspected;
|
|
7
|
+
const values = new Map(entries);
|
|
8
|
+
const enumerableKeys = new Set(keys);
|
|
9
|
+
const fields = [];
|
|
10
|
+
let retained = entries.length;
|
|
11
|
+
let truncated = false;
|
|
12
|
+
|
|
13
|
+
for (const key of ["name", "message", "stack"]) {
|
|
14
|
+
let value;
|
|
15
|
+
if (enumerableKeys.has(key)) {
|
|
16
|
+
if (!values.has(key)) {
|
|
17
|
+
truncated = true;
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
value = values.get(key);
|
|
21
|
+
} else {
|
|
22
|
+
if (retained >= maxKeys) {
|
|
23
|
+
truncated = true;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
value = error[key];
|
|
28
|
+
} catch {
|
|
29
|
+
truncated = true;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
retained++;
|
|
33
|
+
}
|
|
34
|
+
if (key === "stack" && !value) continue;
|
|
35
|
+
fields.push([key, value]);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
for (const [key, value] of entries) {
|
|
39
|
+
if (!["name", "message", "stack"].includes(key)) fields.push([key, value]);
|
|
40
|
+
}
|
|
41
|
+
return { fields, failed: false, truncated: truncated || inspected.truncated };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default inspectErrorFields;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { DEFAULT_LIMITS } from "../default-limits.mjs";
|
|
2
|
+
|
|
3
|
+
function normalizeOutputLimit(configured) {
|
|
4
|
+
const value = configured ?? DEFAULT_LIMITS.maxString;
|
|
5
|
+
return Number.isFinite(value) ? Math.max(0, Math.floor(value)) : DEFAULT_LIMITS.maxString;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export default normalizeOutputLimit;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DEFAULT_LIMITS } from "../default-limits.mjs";
|
|
2
|
+
|
|
3
|
+
function normalizeTraversalLimits(options = {}) {
|
|
4
|
+
const limits = { ...DEFAULT_LIMITS, ...options };
|
|
5
|
+
for (const name of ["maxDepth", "maxKeys", "maxArray"]) {
|
|
6
|
+
const value = options[name];
|
|
7
|
+
limits[name] = Number.isFinite(value) ? Math.max(0, Math.floor(value)) : DEFAULT_LIMITS[name];
|
|
8
|
+
}
|
|
9
|
+
return limits;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default normalizeTraversalLimits;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DEFAULT_REDACTION_MARKER = "[REDACTED]";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import normalizePolicy from "../normalize-policy.mjs";
|
|
2
|
+
import createReadonlySet from "./readonly-set.mjs";
|
|
3
|
+
|
|
4
|
+
function createPolicy(options = {}) {
|
|
5
|
+
const policy = normalizePolicy(options);
|
|
6
|
+
return Object.freeze({ ...policy, keys: createReadonlySet(policy.keys) });
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default createPolicy;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function createReadonlySet(values) {
|
|
2
|
+
const set = new Set(values);
|
|
3
|
+
let view;
|
|
4
|
+
view = Object.freeze({
|
|
5
|
+
get size() {
|
|
6
|
+
return set.size;
|
|
7
|
+
},
|
|
8
|
+
has(value) {
|
|
9
|
+
return set.has(value);
|
|
10
|
+
},
|
|
11
|
+
entries() {
|
|
12
|
+
return set.entries();
|
|
13
|
+
},
|
|
14
|
+
keys() {
|
|
15
|
+
return set.keys();
|
|
16
|
+
},
|
|
17
|
+
values() {
|
|
18
|
+
return set.values();
|
|
19
|
+
},
|
|
20
|
+
forEach(callback, thisArg) {
|
|
21
|
+
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
22
|
+
set.forEach((value) => Reflect.apply(callback, thisArg, [value, value, view]));
|
|
23
|
+
},
|
|
24
|
+
[Symbol.iterator]() {
|
|
25
|
+
return set[Symbol.iterator]();
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
return view;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default createReadonlySet;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const DEFAULT_SENSITIVE_KEYS = new Set([
|
|
2
|
+
"token",
|
|
3
|
+
"accesstoken",
|
|
4
|
+
"authtoken",
|
|
5
|
+
"authorization",
|
|
6
|
+
"cookie",
|
|
7
|
+
"set-cookie",
|
|
8
|
+
"proxy-authorization",
|
|
9
|
+
"password",
|
|
10
|
+
"passwd",
|
|
11
|
+
"secret",
|
|
12
|
+
"clientsecret",
|
|
13
|
+
"apikey",
|
|
14
|
+
"api-key",
|
|
15
|
+
"accesskey",
|
|
16
|
+
"privatekey",
|
|
17
|
+
"passphrase",
|
|
18
|
+
"credential",
|
|
19
|
+
"webhook",
|
|
20
|
+
"signature",
|
|
21
|
+
"tlskey",
|
|
22
|
+
"tlscert",
|
|
23
|
+
]);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DEFAULT_SENSITIVE_KEYS } from "./key-names.mjs";
|
|
2
|
+
import { DEFAULT_REDACTION_MARKER } from "../../markers/default-marker.mjs";
|
|
3
|
+
import createReadonlySet from "../create/readonly-set.mjs";
|
|
4
|
+
import normalizeSensitiveKeys from "../normalize/keys.mjs";
|
|
5
|
+
|
|
6
|
+
export const defaultPolicy = Object.freeze({
|
|
7
|
+
keys: createReadonlySet(normalizeSensitiveKeys(DEFAULT_SENSITIVE_KEYS)),
|
|
8
|
+
marker: DEFAULT_REDACTION_MARKER,
|
|
9
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
function normalizeSensitiveKeys(keys) {
|
|
2
|
+
const normalized = new Set();
|
|
3
|
+
for (const key of keys) {
|
|
4
|
+
try {
|
|
5
|
+
normalized.add(String(key).toLowerCase());
|
|
6
|
+
} catch {
|
|
7
|
+
// Ignore keys that cannot be converted safely.
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
return normalized;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default normalizeSensitiveKeys;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { defaultPolicy } from "./default/policy.mjs";
|
|
2
|
+
import normalizeSensitiveKeys from "./normalize/keys.mjs";
|
|
3
|
+
|
|
4
|
+
function normalizePolicy(options = {}) {
|
|
5
|
+
const keys = options.keys ?? options.redactKeys ?? defaultPolicy.keys;
|
|
6
|
+
return {
|
|
7
|
+
...defaultPolicy,
|
|
8
|
+
...options,
|
|
9
|
+
keys: normalizeSensitiveKeys(keys),
|
|
10
|
+
marker: options.marker ?? defaultPolicy.marker,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default normalizePolicy;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import normalizeTraversalLimits from "../limits/normalize/traversal-limits.mjs";
|
|
2
|
+
import normalizePolicy from "../policy/normalize-policy.mjs";
|
|
3
|
+
import serializeBuffer from "./serialize/buffer.mjs";
|
|
4
|
+
import serializeError from "./serialize/error.mjs";
|
|
5
|
+
import serializePrimitive from "./serialize/primitive.mjs";
|
|
6
|
+
import serializeArray from "./serialize/array.mjs";
|
|
7
|
+
import serializeObject from "./serialize/object.mjs";
|
|
8
|
+
|
|
9
|
+
function safeSerialize(value, options = {}) {
|
|
10
|
+
const policy = normalizePolicy(options);
|
|
11
|
+
const limits = normalizeTraversalLimits(options);
|
|
12
|
+
return serialize(value, policy, limits, new WeakSet(), 0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function serialize(value, policy, limits, seen, depth) {
|
|
16
|
+
if (depth > limits.maxDepth) return "[TRUNCATED]";
|
|
17
|
+
if (value === null || typeof value !== "object")
|
|
18
|
+
return serializePrimitive(value, limits.maxString);
|
|
19
|
+
if (seen.has(value)) return "[CIRCULAR]";
|
|
20
|
+
seen.add(value);
|
|
21
|
+
try {
|
|
22
|
+
if (typeof Buffer !== "undefined" && Buffer.isBuffer(value)) return serializeBuffer(value);
|
|
23
|
+
if (value instanceof Error)
|
|
24
|
+
return serializeError(value, policy, limits, seen, depth, serialize);
|
|
25
|
+
if (Array.isArray(value)) return serializeArray(value, policy, limits, seen, depth, serialize);
|
|
26
|
+
return serializeObject(value, policy, limits, seen, depth, serialize);
|
|
27
|
+
} catch {
|
|
28
|
+
return "[UNSERIALIZABLE]";
|
|
29
|
+
} finally {
|
|
30
|
+
seen.delete(value);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default safeSerialize;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
function serializeArray(value, policy, limits, seen, depth, serialize) {
|
|
2
|
+
const length = value.length;
|
|
3
|
+
const retained = Math.min(length, limits.maxArray);
|
|
4
|
+
const items = [];
|
|
5
|
+
items.length = retained;
|
|
6
|
+
for (let index = 0; index < retained; index++)
|
|
7
|
+
if (Object.hasOwn(value, index))
|
|
8
|
+
items[index] = serialize(value[index], policy, limits, seen, depth + 1);
|
|
9
|
+
if (length > retained) items.push("[TRUNCATED]");
|
|
10
|
+
return items;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default serializeArray;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import copyErrorFields from "../../inspection/copy-error-fields.mjs";
|
|
2
|
+
import redactErrorField from "../../structured/redact/error-fields.mjs";
|
|
3
|
+
|
|
4
|
+
function serializeError(error, policy, limits, seen, depth, serialize) {
|
|
5
|
+
return copyErrorFields(error, limits.maxKeys, (key, value) =>
|
|
6
|
+
redactErrorField(key, value, policy, (child) =>
|
|
7
|
+
serialize(child, policy, limits, seen, depth + 1),
|
|
8
|
+
),
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default serializeError;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import inspectEntries from "../../inspection/inspect/entries.mjs";
|
|
2
|
+
import addTruncationMarker from "../../inspection/add-truncation-marker.mjs";
|
|
3
|
+
|
|
4
|
+
function serializeObject(value, policy, limits, seen, depth, serialize) {
|
|
5
|
+
const output = {};
|
|
6
|
+
const inspected = inspectEntries(value, limits.maxKeys);
|
|
7
|
+
if (inspected.failed) return {};
|
|
8
|
+
for (const [key, child] of inspected.entries) {
|
|
9
|
+
Object.defineProperty(output, key, {
|
|
10
|
+
value: policy.keys.has(key.toLowerCase())
|
|
11
|
+
? policy.marker
|
|
12
|
+
: serialize(child, policy, limits, seen, depth + 1),
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
if (inspected.truncated) addTruncationMarker(output);
|
|
19
|
+
return output;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default serializeObject;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
function serializePrimitive(value, maxString) {
|
|
2
|
+
if (value === null) return null;
|
|
3
|
+
if (value === undefined) return "[Undefined]";
|
|
4
|
+
if (typeof value === "string") return boundRepresentation(value, maxString);
|
|
5
|
+
if (typeof value === "bigint") return boundRepresentation(`${value}n`, maxString);
|
|
6
|
+
if (typeof value === "function")
|
|
7
|
+
return boundRepresentation(`[Function: ${value.name || "anonymous"}]`, maxString);
|
|
8
|
+
if (typeof value === "symbol")
|
|
9
|
+
return boundRepresentation(`[Symbol: ${value.description ?? ""}]`, maxString);
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function boundRepresentation(value, maxString) {
|
|
14
|
+
if (value.length <= maxString) return value;
|
|
15
|
+
if (maxString <= 0) return "";
|
|
16
|
+
const marker = "...[TRUNCATED]";
|
|
17
|
+
if (maxString < marker.length) return "…";
|
|
18
|
+
return `${value.slice(0, maxString - marker.length)}${marker}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default serializePrimitive;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
function redactArrayIndices(value, policy, limits, redactChild) {
|
|
2
|
+
const length = value.length;
|
|
3
|
+
const retained = Math.min(length, limits.maxArray);
|
|
4
|
+
const retainedIndices = Math.min(retained, limits.maxKeys);
|
|
5
|
+
const items = [];
|
|
6
|
+
items.length = retainedIndices;
|
|
7
|
+
const truncated = length > retained || retained > retainedIndices;
|
|
8
|
+
|
|
9
|
+
for (let index = 0; index < retainedIndices; index++) {
|
|
10
|
+
if (!Object.hasOwn(value, index)) continue;
|
|
11
|
+
const sensitive = policy.keys.has(String(index).toLowerCase());
|
|
12
|
+
items[index] = sensitive ? policy.marker : redactChild(value[index]);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return { items, retainedIndices, truncated };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default redactArrayIndices;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import isArrayIndexName from "./index-name.mjs";
|
|
2
|
+
import attemptArrayOperation from "./attempt.mjs";
|
|
3
|
+
|
|
4
|
+
function redactArrayProperties(value, output, policy, limits, retainedEntries, redactChild) {
|
|
5
|
+
let truncated = false;
|
|
6
|
+
const properties = [];
|
|
7
|
+
const keyResult = attemptArrayOperation(
|
|
8
|
+
() => Reflect.ownKeys(value),
|
|
9
|
+
() => [],
|
|
10
|
+
);
|
|
11
|
+
if (keyResult.failed) return { truncated: true };
|
|
12
|
+
const { value: keys } = keyResult;
|
|
13
|
+
|
|
14
|
+
let retained = retainedEntries;
|
|
15
|
+
for (const key of keys) {
|
|
16
|
+
if (typeof key !== "string" || key === "length" || isArrayIndexName(key, value.length))
|
|
17
|
+
continue;
|
|
18
|
+
const sensitive = policy.keys.has(key.toLowerCase());
|
|
19
|
+
const descriptorResult = attemptArrayOperation(
|
|
20
|
+
() => Object.getOwnPropertyDescriptor(value, key),
|
|
21
|
+
() => undefined,
|
|
22
|
+
);
|
|
23
|
+
if (descriptorResult.failed) {
|
|
24
|
+
truncated = true;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
const { value: descriptor } = descriptorResult;
|
|
28
|
+
if (!descriptor || !descriptor.enumerable) continue;
|
|
29
|
+
if (retained >= limits.maxKeys) {
|
|
30
|
+
truncated = true;
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
let propertyValue;
|
|
34
|
+
if (!sensitive && !("value" in descriptor)) {
|
|
35
|
+
propertyValue = value[key];
|
|
36
|
+
if (typeof propertyValue === "function") continue;
|
|
37
|
+
} else if (!sensitive) {
|
|
38
|
+
propertyValue = descriptor.value;
|
|
39
|
+
if (typeof propertyValue === "function") continue;
|
|
40
|
+
}
|
|
41
|
+
properties.push({ key, propertyValue });
|
|
42
|
+
retained++;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const property of properties) {
|
|
46
|
+
const { key, propertyValue } = property;
|
|
47
|
+
const sensitive = policy.keys.has(key.toLowerCase());
|
|
48
|
+
const child = sensitive ? policy.marker : redactChild(propertyValue);
|
|
49
|
+
Object.defineProperty(output, key, {
|
|
50
|
+
value: child,
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return { truncated };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export default redactArrayProperties;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import attemptArrayOperation from "./attempt.mjs";
|
|
2
|
+
|
|
3
|
+
function createArrayResult(build) {
|
|
4
|
+
const result = attemptArrayOperation(build, () => ({ items: [], truncated: false }));
|
|
5
|
+
if (result.failed) return [];
|
|
6
|
+
const { items, truncated } = result.value;
|
|
7
|
+
if (truncated) items[items.length] = "[TRUNCATED]";
|
|
8
|
+
return items;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default createArrayResult;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import redactArrayProperties from "./array/properties.mjs";
|
|
2
|
+
import createArrayResult from "./array/result.mjs";
|
|
3
|
+
import redactArrayIndices from "./array/indices.mjs";
|
|
4
|
+
|
|
5
|
+
function redactArray(value, policy, limits, redactChild) {
|
|
6
|
+
return createArrayResult(() => {
|
|
7
|
+
const { items, retainedIndices, truncated } = redactArrayIndices(
|
|
8
|
+
value,
|
|
9
|
+
policy,
|
|
10
|
+
limits,
|
|
11
|
+
redactChild,
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
const properties = redactArrayProperties(
|
|
15
|
+
value,
|
|
16
|
+
items,
|
|
17
|
+
policy,
|
|
18
|
+
limits,
|
|
19
|
+
retainedIndices,
|
|
20
|
+
redactChild,
|
|
21
|
+
);
|
|
22
|
+
return { items, truncated: truncated || properties.truncated };
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default redactArray;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
function classifyValue(value) {
|
|
2
|
+
let isArray;
|
|
3
|
+
let isError;
|
|
4
|
+
try {
|
|
5
|
+
isArray = Array.isArray(value);
|
|
6
|
+
isError = value instanceof Error;
|
|
7
|
+
return { isArray, isError, failed: false };
|
|
8
|
+
} catch {
|
|
9
|
+
return { isArray, isError: false, failed: true };
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default classifyValue;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import copyErrorFields from "../../inspection/copy-error-fields.mjs";
|
|
2
|
+
import redactErrorField from "./error-fields.mjs";
|
|
3
|
+
|
|
4
|
+
function redactError(error, policy, redactChild, maxKeys = Infinity) {
|
|
5
|
+
try {
|
|
6
|
+
return copyErrorFields(error, maxKeys, (key, value) =>
|
|
7
|
+
redactErrorField(key, value, policy, redactChild),
|
|
8
|
+
);
|
|
9
|
+
} catch {
|
|
10
|
+
return {};
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default redactError;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import addTruncationMarker from "../../inspection/add-truncation-marker.mjs";
|
|
2
|
+
import inspectEntries from "../../inspection/inspect/entries.mjs";
|
|
3
|
+
|
|
4
|
+
function redactObject(value, policy, limits, redactChild) {
|
|
5
|
+
const output = {};
|
|
6
|
+
const inspected = inspectEntries(value, limits.maxKeys);
|
|
7
|
+
if (inspected.failed) return {};
|
|
8
|
+
|
|
9
|
+
for (const [key, child] of inspected.entries) {
|
|
10
|
+
const redactedChild = policy.keys.has(key.toLowerCase()) ? policy.marker : redactChild(child);
|
|
11
|
+
Object.defineProperty(output, key, {
|
|
12
|
+
value: redactedChild,
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (inspected.truncated) addTruncationMarker(output);
|
|
20
|
+
return output;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default redactObject;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import normalizePolicy from "../../policy/normalize-policy.mjs";
|
|
2
|
+
import redactError from "./error.mjs";
|
|
3
|
+
import redactArray from "./array.mjs";
|
|
4
|
+
import redactObject from "./object.mjs";
|
|
5
|
+
import normalizeTraversalLimits from "../../limits/normalize/traversal-limits.mjs";
|
|
6
|
+
import classifyValue from "./classify.mjs";
|
|
7
|
+
|
|
8
|
+
function redactValue(value, options = {}) {
|
|
9
|
+
const policy = normalizePolicy(options);
|
|
10
|
+
const limits = normalizeTraversalLimits(options);
|
|
11
|
+
return redact(value, policy, limits, new WeakMap(), 0);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function redact(value, policy, limits, seen, depth) {
|
|
15
|
+
if (depth > limits.maxDepth) return "[TRUNCATED]";
|
|
16
|
+
if (value === null || typeof value !== "object") return value;
|
|
17
|
+
if (seen.has(value)) return policy.circularMarker ?? "[CIRCULAR]";
|
|
18
|
+
const classification = classifyValue(value);
|
|
19
|
+
if (classification.failed) return classification.isArray === true ? [] : {};
|
|
20
|
+
const { isArray, isError } = classification;
|
|
21
|
+
if (isError) {
|
|
22
|
+
seen.set(value, true);
|
|
23
|
+
try {
|
|
24
|
+
return redactError(
|
|
25
|
+
value,
|
|
26
|
+
policy,
|
|
27
|
+
(child) => redact(child, policy, limits, seen, depth + 1),
|
|
28
|
+
limits.maxKeys,
|
|
29
|
+
);
|
|
30
|
+
} finally {
|
|
31
|
+
seen.delete(value);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
seen.set(value, true);
|
|
35
|
+
try {
|
|
36
|
+
if (isArray) {
|
|
37
|
+
return redactArray(value, policy, limits, (child) =>
|
|
38
|
+
redact(child, policy, limits, seen, depth + 1),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
return redactObject(value, policy, limits, (child) =>
|
|
42
|
+
redact(child, policy, limits, seen, depth + 1),
|
|
43
|
+
);
|
|
44
|
+
} finally {
|
|
45
|
+
seen.delete(value);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default redactValue;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import replaceLiteralSecrets from "../../replace-literal/secrets.mjs";
|
|
2
|
+
import redactConfiguredKeyValues from "../../rules/redact/key/configured-values.mjs";
|
|
3
|
+
|
|
4
|
+
function applyConfiguredTextRules(value, secrets, policy) {
|
|
5
|
+
const keyed = redactConfiguredKeyValues(value, policy.keys, policy.marker);
|
|
6
|
+
return replaceLiteralSecrets(keyed, secrets, policy.marker);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default applyConfiguredTextRules;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import limitOutput from "../../limit-output.mjs";
|
|
2
|
+
import normalizePolicy from "../../../policy/normalize-policy.mjs";
|
|
3
|
+
import applyConfiguredTextRules from "./rules.mjs";
|
|
4
|
+
|
|
5
|
+
function applyConfiguredTextRedactions(value, options) {
|
|
6
|
+
const policy = normalizePolicy(options);
|
|
7
|
+
const output = applyConfiguredTextRules(value, options.secrets, policy);
|
|
8
|
+
return limitOutput(output, options.maxOutputLength);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default applyConfiguredTextRedactions;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import applyConfiguredTextRedactions from "./configured/text-redactions.mjs";
|
|
2
|
+
import builtInRules from "../rules/built-in.mjs";
|
|
3
|
+
import redactKeyMaterial from "../rules/redact/key/material.mjs";
|
|
4
|
+
|
|
5
|
+
function createTextRedactor(options) {
|
|
6
|
+
return (input, secrets, processingLimit) => {
|
|
7
|
+
let output = redactKeyMaterial(input);
|
|
8
|
+
for (const rule of builtInRules) output = rule(output);
|
|
9
|
+
return applyConfiguredTextRedactions(output, {
|
|
10
|
+
...options,
|
|
11
|
+
secrets,
|
|
12
|
+
maxOutputLength: processingLimit,
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default createTextRedactor;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import redactJwtAtBoundary, { boundedJwtMarker } from "./jwt.mjs";
|
|
2
|
+
|
|
3
|
+
function detectBoundaryJwt(input, maxLength, processingLimit) {
|
|
4
|
+
const inputTruncated = input.length > maxLength;
|
|
5
|
+
if (!inputTruncated)
|
|
6
|
+
return { output: input, redacted: false, marker: boundedJwtMarker, inputTruncated };
|
|
7
|
+
return {
|
|
8
|
+
...redactJwtAtBoundary(
|
|
9
|
+
input.slice(0, processingLimit),
|
|
10
|
+
maxLength,
|
|
11
|
+
input.length > processingLimit,
|
|
12
|
+
),
|
|
13
|
+
marker: boundedJwtMarker,
|
|
14
|
+
inputTruncated,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default detectBoundaryJwt;
|