@open-mercato/telemetry 0.11.1-develop.8776.1.581e904cc8 → 0.11.1-develop.8784.1.e9d95a72f5
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/dist/facade/redact.js
CHANGED
|
@@ -6,19 +6,23 @@ function redactPii(text) {
|
|
|
6
6
|
}
|
|
7
7
|
const UUID_PATH_SEGMENT_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
8
8
|
const URL_ATTRIBUTE_KEYS = /* @__PURE__ */ new Set(["http.target", "http.url", "url.full", "url.path"]);
|
|
9
|
+
const QUOTE_ACCEPTANCE_ROUTE_PREFIXES = [["quote"], ["api", "sales", "quotes", "public"]];
|
|
9
10
|
function redactQuery(query) {
|
|
10
11
|
return query.split("&").map((parameter) => {
|
|
11
12
|
const separator = parameter.indexOf("=");
|
|
12
13
|
return separator < 0 ? parameter : `${parameter.slice(0, separator)}=[redacted]`;
|
|
13
14
|
}).join("&");
|
|
14
15
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
17
|
-
const decoded = segment.replace(
|
|
16
|
+
function decodePathSegment(segment) {
|
|
17
|
+
return segment.replace(
|
|
18
18
|
/%([0-9a-f]{2})/gi,
|
|
19
19
|
(_match, hex) => String.fromCharCode(Number.parseInt(hex, 16))
|
|
20
20
|
);
|
|
21
|
-
|
|
21
|
+
}
|
|
22
|
+
function redactPathSegment(segment, isQuoteCredential) {
|
|
23
|
+
if (/%(?![0-9a-f]{2})/i.test(segment)) return "[redacted]";
|
|
24
|
+
const decoded = decodePathSegment(segment);
|
|
25
|
+
return /^[A-Za-z0-9_-]{32,}$/.test(decoded) && (isQuoteCredential || !UUID_PATH_SEGMENT_RE.test(decoded)) ? "[redacted]" : segment;
|
|
22
26
|
}
|
|
23
27
|
function redactUrl(url) {
|
|
24
28
|
const fragmentIndex = url.indexOf("#");
|
|
@@ -28,7 +32,15 @@ function redactUrl(url) {
|
|
|
28
32
|
const authority = /^(?:[a-z][a-z0-9+.-]*:)?\/\/[^/]*/i.exec(path)?.[0] ?? "";
|
|
29
33
|
const userInfoEnd = authority.lastIndexOf("@");
|
|
30
34
|
const redactedAuthority = userInfoEnd < 0 ? authority : `${authority.slice(0, authority.indexOf("//") + 2)}${authority.slice(userInfoEnd + 1)}`;
|
|
31
|
-
const
|
|
35
|
+
const segments = path.slice(authority.length).split("/");
|
|
36
|
+
const routeStart = segments[0] === "" ? 1 : 0;
|
|
37
|
+
const quoteRoute = QUOTE_ACCEPTANCE_ROUTE_PREFIXES.find(
|
|
38
|
+
(prefix) => prefix.every((segment, index) => decodePathSegment(segments[routeStart + index] ?? "") === segment)
|
|
39
|
+
);
|
|
40
|
+
const quoteCredentialIndex = quoteRoute ? routeStart + quoteRoute.length : -1;
|
|
41
|
+
const redactedPath = segments.map(
|
|
42
|
+
(segment, index) => redactPathSegment(segment, index === quoteCredentialIndex)
|
|
43
|
+
).join("/");
|
|
32
44
|
const query = queryIndex < 0 ? "" : `?${redactQuery(withoutFragment.slice(queryIndex + 1))}`;
|
|
33
45
|
const fragment = fragmentIndex < 0 ? "" : "#[redacted]";
|
|
34
46
|
return `${redactedAuthority}${redactedPath}${query}${fragment}`;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/facade/redact.ts"],
|
|
4
|
-
"sourcesContent": ["import type { Attributes } from '../types'\n\n/**\n * Active redaction backstop for telemetry (Privacy section of the telemetry spec).\n *\n * Defense-in-depth over the don't-emit posture: callers are expected not to put\n * PII or secrets into errors/logs, but error messages and stack frames can still\n * pick them up a layer down (e.g. `Error: no user for jan.kowalski@example.com`,\n * or a dumped `Authorization: Bearer \u2026` header). This scrubs the highest-signal\n * identifiers \u2014 email addresses and auth tokens \u2014 from any text that ships to the\n * backend, without touching opaque UUIDs/ids (which we keep).\n *\n * Deliberately conservative (emails + auth tokens) to preserve debuggability;\n * extend the pattern set here if a new leak vector is found.\n */\n// Bound every component both to match the practical limits from RFC 5321 and\n// to keep scanning time linear for hostile strings with long runs of email\n// punctuation (CodeQL js/polynomial-redos).\nconst EMAIL_RE = /[A-Z0-9._%+-]{1,64}@[A-Z0-9.-]{1,253}\\.[A-Z]{2,63}/gi\n// `authorization: <value>` / `cookie: <value>` header dumps embedded in free text.\n// The value runs to the next separator (`;`/`,`/`}`) or newline, so it swallows a\n// `Bearer <token>` value in one pass (before the standalone-scheme rule below).\nconst SECRET_HEADER_RE = /\\b(authorization|proxy-authorization|cookie|set-cookie)(\\s*[:=]\\s*)([^\\n;,}]+)/gi\n// `Bearer <token>` / `Basic <base64>` / `ApiKey <key>` standing alone in free text (message or stack).\nconst AUTH_SCHEME_RE = /\\b(Bearer|Basic|ApiKey)\\s+[A-Za-z0-9._~+/=-]+/gi\n\nexport function redactPii(text: string): string {\n return text\n .replace(EMAIL_RE, '[redacted-email]')\n .replace(SECRET_HEADER_RE, (_match, name: string, separator: string) => `${name}${separator}[redacted]`)\n .replace(AUTH_SCHEME_RE, (_match, scheme: string) => `${scheme} [redacted]`)\n}\n\nconst UUID_PATH_SEGMENT_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i\nconst URL_ATTRIBUTE_KEYS = new Set(['http.target', 'http.url', 'url.full', 'url.path'])\n\nfunction redactQuery(query: string): string {\n return query.split('&').map((parameter) => {\n const separator = parameter.indexOf('=')\n return separator < 0 ? parameter : `${parameter.slice(0, separator)}=[redacted]`\n }).join('&')\n}\n\nfunction
|
|
5
|
-
"mappings": "AAkBA,MAAM,WAAW;AAIjB,MAAM,mBAAmB;AAEzB,MAAM,iBAAiB;AAEhB,SAAS,UAAU,MAAsB;AAC9C,SAAO,KACJ,QAAQ,UAAU,kBAAkB,EACpC,QAAQ,kBAAkB,CAAC,QAAQ,MAAc,cAAsB,GAAG,IAAI,GAAG,SAAS,YAAY,EACtG,QAAQ,gBAAgB,CAAC,QAAQ,WAAmB,GAAG,MAAM,aAAa;AAC/E;AAEA,MAAM,uBAAuB;AAC7B,MAAM,qBAAqB,oBAAI,IAAI,CAAC,eAAe,YAAY,YAAY,UAAU,CAAC;
|
|
4
|
+
"sourcesContent": ["import type { Attributes } from '../types'\n\n/**\n * Active redaction backstop for telemetry (Privacy section of the telemetry spec).\n *\n * Defense-in-depth over the don't-emit posture: callers are expected not to put\n * PII or secrets into errors/logs, but error messages and stack frames can still\n * pick them up a layer down (e.g. `Error: no user for jan.kowalski@example.com`,\n * or a dumped `Authorization: Bearer \u2026` header). This scrubs the highest-signal\n * identifiers \u2014 email addresses and auth tokens \u2014 from any text that ships to the\n * backend, without touching opaque UUIDs/ids (which we keep).\n *\n * Deliberately conservative (emails + auth tokens) to preserve debuggability;\n * extend the pattern set here if a new leak vector is found.\n */\n// Bound every component both to match the practical limits from RFC 5321 and\n// to keep scanning time linear for hostile strings with long runs of email\n// punctuation (CodeQL js/polynomial-redos).\nconst EMAIL_RE = /[A-Z0-9._%+-]{1,64}@[A-Z0-9.-]{1,253}\\.[A-Z]{2,63}/gi\n// `authorization: <value>` / `cookie: <value>` header dumps embedded in free text.\n// The value runs to the next separator (`;`/`,`/`}`) or newline, so it swallows a\n// `Bearer <token>` value in one pass (before the standalone-scheme rule below).\nconst SECRET_HEADER_RE = /\\b(authorization|proxy-authorization|cookie|set-cookie)(\\s*[:=]\\s*)([^\\n;,}]+)/gi\n// `Bearer <token>` / `Basic <base64>` / `ApiKey <key>` standing alone in free text (message or stack).\nconst AUTH_SCHEME_RE = /\\b(Bearer|Basic|ApiKey)\\s+[A-Za-z0-9._~+/=-]+/gi\n\nexport function redactPii(text: string): string {\n return text\n .replace(EMAIL_RE, '[redacted-email]')\n .replace(SECRET_HEADER_RE, (_match, name: string, separator: string) => `${name}${separator}[redacted]`)\n .replace(AUTH_SCHEME_RE, (_match, scheme: string) => `${scheme} [redacted]`)\n}\n\nconst UUID_PATH_SEGMENT_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i\nconst URL_ATTRIBUTE_KEYS = new Set(['http.target', 'http.url', 'url.full', 'url.path'])\nconst QUOTE_ACCEPTANCE_ROUTE_PREFIXES = [['quote'], ['api', 'sales', 'quotes', 'public']]\n\nfunction redactQuery(query: string): string {\n return query.split('&').map((parameter) => {\n const separator = parameter.indexOf('=')\n return separator < 0 ? parameter : `${parameter.slice(0, separator)}=[redacted]`\n }).join('&')\n}\n\nfunction decodePathSegment(segment: string): string {\n return segment.replace(/%([0-9a-f]{2})/gi, (_match, hex: string) =>\n String.fromCharCode(Number.parseInt(hex, 16)),\n )\n}\n\nfunction redactPathSegment(segment: string, isQuoteCredential: boolean): string {\n if (/%(?![0-9a-f]{2})/i.test(segment)) return '[redacted]'\n const decoded = decodePathSegment(segment)\n return /^[A-Za-z0-9_-]{32,}$/.test(decoded) && (isQuoteCredential || !UUID_PATH_SEGMENT_RE.test(decoded))\n ? '[redacted]'\n : segment\n}\n\nexport function redactUrl(url: string): string {\n const fragmentIndex = url.indexOf('#')\n const withoutFragment = fragmentIndex < 0 ? url : url.slice(0, fragmentIndex)\n const queryIndex = withoutFragment.indexOf('?')\n const path = queryIndex < 0 ? withoutFragment : withoutFragment.slice(0, queryIndex)\n const authority = /^(?:[a-z][a-z0-9+.-]*:)?\\/\\/[^/]*/i.exec(path)?.[0] ?? ''\n const userInfoEnd = authority.lastIndexOf('@')\n const redactedAuthority = userInfoEnd < 0\n ? authority\n : `${authority.slice(0, authority.indexOf('//') + 2)}${authority.slice(userInfoEnd + 1)}`\n const segments = path.slice(authority.length).split('/')\n const routeStart = segments[0] === '' ? 1 : 0\n const quoteRoute = QUOTE_ACCEPTANCE_ROUTE_PREFIXES.find((prefix) =>\n prefix.every((segment, index) => decodePathSegment(segments[routeStart + index] ?? '') === segment),\n )\n const quoteCredentialIndex = quoteRoute ? routeStart + quoteRoute.length : -1\n const redactedPath = segments.map((segment, index) =>\n redactPathSegment(segment, index === quoteCredentialIndex),\n ).join('/')\n const query = queryIndex < 0 ? '' : `?${redactQuery(withoutFragment.slice(queryIndex + 1))}`\n const fragment = fragmentIndex < 0 ? '' : '#[redacted]'\n return `${redactedAuthority}${redactedPath}${query}${fragment}`\n}\n\nexport function redactSpanName(name: string): string {\n return redactPii(name.split(/(\\s+)/).map((part) =>\n part.startsWith('/') || /^[a-z][a-z0-9+.-]{0,31}:\\/\\//i.test(part) ? redactUrl(part) : part,\n ).join(''))\n}\n\n// Attribute KEYS whose value is a secret regardless of content. Intentionally\n// specific enough not to clobber benign fields like `token_count`, while\n// still masking the exact key `token`.\nconst SECRET_KEY_RE =\n /(authorization|cookie|password|passwd|\\bpwd\\b|secret|api[-_]?key|access[-_]?token|refresh[-_]?token|id[-_]?token|auth[-_]?token|^token$|\\bbearer\\b|private[-_]?key|credential)/i\n\n/**\n * Redact a telemetry attribute bag before it ships: a value under a secret-looking\n * KEY (`authorization`, `set-cookie`, `client_secret`, `x-api-key`, \u2026) is masked\n * wholesale; other string values still pass through `redactPii` so an inline email\n * or auth token is caught too. Non-string values are left as-is.\n */\nexport function redactAttributes(attributes: Attributes): Attributes {\n const result: Attributes = {}\n for (const [key, value] of Object.entries(attributes)) {\n if (SECRET_KEY_RE.test(key)) result[key] = '[redacted]'\n else if (typeof value === 'string') {\n const cleaned = key === 'next.span_name'\n ? redactSpanName(value)\n : key === 'url.query' ? redactQuery(value) : URL_ATTRIBUTE_KEYS.has(key) ? redactUrl(value) : value\n result[key] = redactPii(cleaned)\n }\n else result[key] = value\n }\n return result\n}\n"],
|
|
5
|
+
"mappings": "AAkBA,MAAM,WAAW;AAIjB,MAAM,mBAAmB;AAEzB,MAAM,iBAAiB;AAEhB,SAAS,UAAU,MAAsB;AAC9C,SAAO,KACJ,QAAQ,UAAU,kBAAkB,EACpC,QAAQ,kBAAkB,CAAC,QAAQ,MAAc,cAAsB,GAAG,IA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,IAAI;AAChG,aAAO,GAAG,IAAI,UAAU,OAAO;AAAA,IACjC,MACK,QAAO,GAAG,IAAI;AAAA,EACrB;AACA,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/telemetry",
|
|
3
|
-
"version": "0.11.1-develop.
|
|
3
|
+
"version": "0.11.1-develop.8784.1.e9d95a72f5",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Vendor-neutral spans, metrics, and error reporting backed by a pluggable OTLP provider. Off by default; the telemetry runtime is loaded only when explicitly enabled.",
|
|
6
6
|
"type": "module",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@open-mercato/shared": "0.11.1-develop.
|
|
41
|
+
"@open-mercato/shared": "0.11.1-develop.8784.1.e9d95a72f5"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"react": ">=18.0.0"
|
|
@@ -109,6 +109,28 @@ describe('URL redaction', () => {
|
|
|
109
109
|
expect(redactUrl(`/reset/${'a'.repeat(31)}`)).toBe(`/reset/${'a'.repeat(31)}`)
|
|
110
110
|
})
|
|
111
111
|
|
|
112
|
+
it.each([
|
|
113
|
+
[`/quote/${uuid}`, '/quote/[redacted]'],
|
|
114
|
+
[`/api/sales/quotes/public/${uuid}/`, '/api/sales/quotes/public/[redacted]/'],
|
|
115
|
+
[`api/sales/quotes/public/${uuid}`, 'api/sales/quotes/public/[redacted]'],
|
|
116
|
+
[`https://example.com/%71uote/${uuid.replace(/-/g, '%2D')}?page=2#section`,
|
|
117
|
+
'https://example.com/%71uote/[redacted]?page=[redacted]#[redacted]'],
|
|
118
|
+
])('redacts quote credentials in URL paths', (url, expected) => {
|
|
119
|
+
expect(redactUrl(url)).toBe(expected)
|
|
120
|
+
expect(redactUrl(expected)).toBe(expected)
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it.each([
|
|
124
|
+
`/api/sales/quotes/${uuid}`,
|
|
125
|
+
`/api/sales/quotes/publications/${uuid}`,
|
|
126
|
+
`/quotes/${uuid}`,
|
|
127
|
+
`/records/quote/${uuid}`,
|
|
128
|
+
`/api/sales/quotes/public/[token]`,
|
|
129
|
+
'/quote/:token',
|
|
130
|
+
])('preserves record UUIDs and route templates in %s', (path) => {
|
|
131
|
+
expect(redactUrl(path)).toBe(path)
|
|
132
|
+
})
|
|
133
|
+
|
|
112
134
|
it('handles absolute, relative, encoded and malformed URLs without retaining values', () => {
|
|
113
135
|
expect(redactUrl(`https://example.com/reset/${token}?token=a%26b&flag#secret`)).toBe(
|
|
114
136
|
'https://example.com/reset/[redacted]?token=[redacted]&flag#[redacted]',
|
|
@@ -75,4 +75,35 @@ describe('redacting span processor', () => {
|
|
|
75
75
|
await provider.shutdown()
|
|
76
76
|
}
|
|
77
77
|
})
|
|
78
|
+
|
|
79
|
+
it('exports quote paths and span names with credentials redacted', async () => {
|
|
80
|
+
const processor = recordingProcessor()
|
|
81
|
+
const provider = new BasicTracerProvider({ spanProcessors: [createRedactingSpanProcessor(processor)] })
|
|
82
|
+
const quoteCredential = '00000000-0000-4000-8000-000000000001'
|
|
83
|
+
const quoteId = '00000000-0000-4000-8000-000000000002'
|
|
84
|
+
try {
|
|
85
|
+
const span = provider.getTracer('quote-redaction').startSpan(`GET /quote/${quoteCredential}`)
|
|
86
|
+
span.setAttributes({
|
|
87
|
+
'url.path': `/quote/${quoteCredential}`,
|
|
88
|
+
'http.target': `/api/sales/quotes/public/${quoteCredential}`,
|
|
89
|
+
'url.full': `https://example.com/api/sales/quotes/public/${quoteCredential}`,
|
|
90
|
+
'next.span_name': `GET /api/sales/quotes/public/${quoteCredential}`,
|
|
91
|
+
'quote.id': quoteId,
|
|
92
|
+
})
|
|
93
|
+
span.end()
|
|
94
|
+
|
|
95
|
+
expect(processor.onEnd.mock.calls[0][0]).toMatchObject({
|
|
96
|
+
name: 'GET /quote/[redacted]',
|
|
97
|
+
attributes: {
|
|
98
|
+
'url.path': '/quote/[redacted]',
|
|
99
|
+
'http.target': '/api/sales/quotes/public/[redacted]',
|
|
100
|
+
'url.full': 'https://example.com/api/sales/quotes/public/[redacted]',
|
|
101
|
+
'next.span_name': 'GET /api/sales/quotes/public/[redacted]',
|
|
102
|
+
'quote.id': quoteId,
|
|
103
|
+
},
|
|
104
|
+
})
|
|
105
|
+
} finally {
|
|
106
|
+
await provider.shutdown()
|
|
107
|
+
}
|
|
108
|
+
})
|
|
78
109
|
})
|
package/src/facade/redact.ts
CHANGED
|
@@ -33,6 +33,7 @@ export function redactPii(text: string): string {
|
|
|
33
33
|
|
|
34
34
|
const UUID_PATH_SEGMENT_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
35
35
|
const URL_ATTRIBUTE_KEYS = new Set(['http.target', 'http.url', 'url.full', 'url.path'])
|
|
36
|
+
const QUOTE_ACCEPTANCE_ROUTE_PREFIXES = [['quote'], ['api', 'sales', 'quotes', 'public']]
|
|
36
37
|
|
|
37
38
|
function redactQuery(query: string): string {
|
|
38
39
|
return query.split('&').map((parameter) => {
|
|
@@ -41,12 +42,16 @@ function redactQuery(query: string): string {
|
|
|
41
42
|
}).join('&')
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
function
|
|
45
|
-
|
|
46
|
-
const decoded = segment.replace(/%([0-9a-f]{2})/gi, (_match, hex: string) =>
|
|
45
|
+
function decodePathSegment(segment: string): string {
|
|
46
|
+
return segment.replace(/%([0-9a-f]{2})/gi, (_match, hex: string) =>
|
|
47
47
|
String.fromCharCode(Number.parseInt(hex, 16)),
|
|
48
48
|
)
|
|
49
|
-
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function redactPathSegment(segment: string, isQuoteCredential: boolean): string {
|
|
52
|
+
if (/%(?![0-9a-f]{2})/i.test(segment)) return '[redacted]'
|
|
53
|
+
const decoded = decodePathSegment(segment)
|
|
54
|
+
return /^[A-Za-z0-9_-]{32,}$/.test(decoded) && (isQuoteCredential || !UUID_PATH_SEGMENT_RE.test(decoded))
|
|
50
55
|
? '[redacted]'
|
|
51
56
|
: segment
|
|
52
57
|
}
|
|
@@ -61,7 +66,15 @@ export function redactUrl(url: string): string {
|
|
|
61
66
|
const redactedAuthority = userInfoEnd < 0
|
|
62
67
|
? authority
|
|
63
68
|
: `${authority.slice(0, authority.indexOf('//') + 2)}${authority.slice(userInfoEnd + 1)}`
|
|
64
|
-
const
|
|
69
|
+
const segments = path.slice(authority.length).split('/')
|
|
70
|
+
const routeStart = segments[0] === '' ? 1 : 0
|
|
71
|
+
const quoteRoute = QUOTE_ACCEPTANCE_ROUTE_PREFIXES.find((prefix) =>
|
|
72
|
+
prefix.every((segment, index) => decodePathSegment(segments[routeStart + index] ?? '') === segment),
|
|
73
|
+
)
|
|
74
|
+
const quoteCredentialIndex = quoteRoute ? routeStart + quoteRoute.length : -1
|
|
75
|
+
const redactedPath = segments.map((segment, index) =>
|
|
76
|
+
redactPathSegment(segment, index === quoteCredentialIndex),
|
|
77
|
+
).join('/')
|
|
65
78
|
const query = queryIndex < 0 ? '' : `?${redactQuery(withoutFragment.slice(queryIndex + 1))}`
|
|
66
79
|
const fragment = fragmentIndex < 0 ? '' : '#[redacted]'
|
|
67
80
|
return `${redactedAuthority}${redactedPath}${query}${fragment}`
|