@onlineapps/service-common 1.1.1 → 1.1.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onlineapps/service-common",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Common utilities for both infrastructure services and business services (JWT auth, Redis/Postgres clients, business errors, runtime config)",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -51,6 +51,11 @@ const {
51
51
  extractTenantContext,
52
52
  ROLES_VERSION_PREFIX
53
53
  } = require('./jwt');
54
+ const {
55
+ REDACTED_PLACEHOLDER,
56
+ sensitiveFieldsForOperation,
57
+ redactSensitiveDeep
58
+ } = require('./redactSensitive');
54
59
 
55
60
  module.exports = {
56
61
  // Infrastructure readiness utilities (used by both infrastructure and business services)
@@ -98,7 +103,13 @@ module.exports = {
98
103
  verifyAccessToken,
99
104
  createJwtValidator,
100
105
  extractTenantContext,
101
- ROLES_VERSION_PREFIX
106
+ ROLES_VERSION_PREFIX,
107
+
108
+ // Sensitive-input redaction for observability sinks (SecretBox F1.5)
109
+ // See: docs/architecture/secretbox.md §8
110
+ REDACTED_PLACEHOLDER,
111
+ sensitiveFieldsForOperation,
112
+ redactSensitiveDeep
102
113
  };
103
114
 
104
115
 
@@ -0,0 +1,87 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Sensitive-input redaction (SecretBox F1.5).
5
+ * Contract: api/docs/architecture/secretbox.md §8.
6
+ *
7
+ * Pure, dependency-free helpers used by every layer that logs, traces, or
8
+ * otherwise persists workflow/operation inputs (gateway, orchestrator, monitoring
9
+ * consumer). Single implementation — no duplication across layers.
10
+ *
11
+ * Operations declare sensitive input fields at operation level in operations.json:
12
+ * "sensitive_input_fields": ["value", ...]
13
+ * (NOT inside the JSON Schema — service-wrapper compiles input schemas with AJV
14
+ * strict:true, which throws on unknown keywords.)
15
+ *
16
+ * `redactSensitiveDeep` deep-clones the value and replaces any object key whose
17
+ * name is in `fieldNames` with the REDACTED placeholder, at any depth. It never
18
+ * mutates the input. Over-redaction (a sensitive field name matching elsewhere in
19
+ * the same payload) is acceptable and safe for observability sinks.
20
+ *
21
+ * IMPORTANT: this is for observability/log/trace copies only. The execution message
22
+ * that carries the value to its owning handler MUST NOT be redacted.
23
+ */
24
+
25
+ const REDACTED_PLACEHOLDER = '[REDACTED]';
26
+ const CIRCULAR_PLACEHOLDER = '[Circular]';
27
+
28
+ /**
29
+ * Extract the sensitive input field names for one operation from a service spec
30
+ * (the `registry:service:<name>:spec` shape, or a local operations.json object).
31
+ * @param {object} serviceSpec - object with `.operations[<op>]`.
32
+ * @param {string} operationName
33
+ * @returns {string[]} field names (empty array when none / not found).
34
+ */
35
+ function sensitiveFieldsForOperation(serviceSpec, operationName) {
36
+ const op =
37
+ serviceSpec &&
38
+ serviceSpec.operations &&
39
+ typeof serviceSpec.operations === 'object'
40
+ ? serviceSpec.operations[operationName]
41
+ : undefined;
42
+ const list = op && op.sensitive_input_fields;
43
+ return Array.isArray(list) ? list.filter((f) => typeof f === 'string' && f.length > 0) : [];
44
+ }
45
+
46
+ function _walk(node, fields, seen) {
47
+ if (node === null || typeof node !== 'object') return node;
48
+ // Do not recurse into non-plain objects; return a shallow copy where safe.
49
+ if (node instanceof Date) return new Date(node.getTime());
50
+ if (typeof Buffer !== 'undefined' && Buffer.isBuffer(node)) return node;
51
+ if (seen.has(node)) return CIRCULAR_PLACEHOLDER;
52
+ seen.add(node);
53
+
54
+ if (Array.isArray(node)) {
55
+ return node.map((el) => _walk(el, fields, seen));
56
+ }
57
+
58
+ const out = {};
59
+ for (const key of Object.keys(node)) {
60
+ if (fields.has(key)) {
61
+ out[key] = REDACTED_PLACEHOLDER;
62
+ } else {
63
+ out[key] = _walk(node[key], fields, seen);
64
+ }
65
+ }
66
+ return out;
67
+ }
68
+
69
+ /**
70
+ * Deep-clone `value`, replacing any object property named in `fieldNames` with
71
+ * the REDACTED placeholder. Does not mutate the input.
72
+ * @param {*} value - any JSON-ish value (object/array/primitive).
73
+ * @param {string[]|Set<string>} fieldNames - sensitive field names to redact.
74
+ * @returns {*} redacted deep clone (or the original value when there is nothing
75
+ * to redact — no sensitive fields means no mutation risk).
76
+ */
77
+ function redactSensitiveDeep(value, fieldNames) {
78
+ const fields = fieldNames instanceof Set ? fieldNames : new Set(fieldNames || []);
79
+ if (fields.size === 0) return value;
80
+ return _walk(value, fields, new WeakSet());
81
+ }
82
+
83
+ module.exports = {
84
+ REDACTED_PLACEHOLDER,
85
+ sensitiveFieldsForOperation,
86
+ redactSensitiveDeep
87
+ };