@ciridae/telemetry 0.2.0 → 0.3.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.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import posthog from "posthog-js";
|
|
2
2
|
import { filterCapturedEvent, isSensitiveTelemetryPath } from "./filterCapturedEvent.js";
|
|
3
3
|
import { maskSensitiveUrlParameters, SENSITIVE_URL_PARAMETER_NAMES, } from "./maskSensitiveUrlParameters.js";
|
|
4
|
+
import { redactCapturedLog } from "./redactCapturedLog.js";
|
|
4
5
|
const POSTHOG_US_API_HOST = "https://us.i.posthog.com";
|
|
5
6
|
const POSTHOG_US_UI_HOST = "https://us.posthog.com";
|
|
6
7
|
const POSTHOG_EU_API_HOST = "https://eu.i.posthog.com";
|
|
@@ -36,6 +37,9 @@ export function createCiridaeTelemetry(configuration) {
|
|
|
36
37
|
source: "frontend",
|
|
37
38
|
...(configuration.projectId ? { project_id: configuration.projectId } : {}),
|
|
38
39
|
});
|
|
40
|
+
if (configuration.serviceName) {
|
|
41
|
+
posthog.logger.info("PostHog log ingestion canary", { telemetry_canary: true });
|
|
42
|
+
}
|
|
39
43
|
initialized = true;
|
|
40
44
|
for (const subscriber of initializationSubscribers)
|
|
41
45
|
subscriber();
|
|
@@ -154,6 +158,23 @@ function buildPostHogConfiguration(configuration) {
|
|
|
154
158
|
capture_dead_clicks: true,
|
|
155
159
|
capture_performance: { web_vitals: true },
|
|
156
160
|
capture_exceptions: true,
|
|
161
|
+
...(configuration.serviceName
|
|
162
|
+
? {
|
|
163
|
+
logs: {
|
|
164
|
+
serviceName: configuration.serviceName,
|
|
165
|
+
environment: configuration.environment || "development",
|
|
166
|
+
resourceAttributes: {
|
|
167
|
+
source: "frontend",
|
|
168
|
+
...(configuration.projectId ? { project_id: configuration.projectId } : {}),
|
|
169
|
+
},
|
|
170
|
+
...(configuration.serviceVersion
|
|
171
|
+
? { serviceVersion: configuration.serviceVersion }
|
|
172
|
+
: {}),
|
|
173
|
+
captureConsoleLogs: true,
|
|
174
|
+
beforeSend: redactCapturedLog,
|
|
175
|
+
},
|
|
176
|
+
}
|
|
177
|
+
: {}),
|
|
157
178
|
enable_heatmaps: true,
|
|
158
179
|
loaded: exposePostHogOnWindow,
|
|
159
180
|
session_recording: {
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { SENSITIVE_URL_PARAMETER_NAMES } from "./maskSensitiveUrlParameters.js";
|
|
2
|
+
const REDACTED_LOG_VALUE = "[REDACTED]";
|
|
3
|
+
const MAX_LOG_ATTRIBUTE_DEPTH = 8;
|
|
4
|
+
const sensitiveLogFieldNames = new Set([
|
|
5
|
+
...SENSITIVE_URL_PARAMETER_NAMES,
|
|
6
|
+
"authorization",
|
|
7
|
+
"cookie",
|
|
8
|
+
"credential",
|
|
9
|
+
"credentials",
|
|
10
|
+
"password",
|
|
11
|
+
"passwd",
|
|
12
|
+
"secret",
|
|
13
|
+
].map(normalizeFieldName));
|
|
14
|
+
const SENSITIVE_ASSIGNMENT_PATTERN = /(["']?(?:authorization|cookie|credentials?|password|passwd|secret|session(?:[_-]?id)?|api[_ -]?key|access[_ -]?token|refresh[_ -]?token|id[_ -]?token|token)["']?\s*[:=]\s*)(?:Bearer\s+[A-Za-z\d._~+/=-]+|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[^\s,;}\]]+)/gi;
|
|
15
|
+
const SENSITIVE_URL_PARAMETER_PATTERN = /([?&#](?:customToken|redirect_path|token|auth|email|code|state|id_token|access_token|refresh_token|session|session_id|api_key|key)=)[^&#\s]+/gi;
|
|
16
|
+
const BEARER_TOKEN_PATTERN = /\b(Bearer\s+)[A-Za-z\d._~+/=-]+/gi;
|
|
17
|
+
const JSON_WEB_TOKEN_PATTERN = /\beyJ[A-Za-z\d_-]+\.[A-Za-z\d_-]+\.[A-Za-z\d_-]*\b/g;
|
|
18
|
+
export function redactCapturedLog(record) {
|
|
19
|
+
return {
|
|
20
|
+
...record,
|
|
21
|
+
body: redactSensitiveText(record.body),
|
|
22
|
+
...(record.attributes
|
|
23
|
+
? { attributes: redactLogAttributes(record.attributes, new WeakSet(), 0) }
|
|
24
|
+
: {}),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function redactLogAttributes(attributes, seenObjects, depth) {
|
|
28
|
+
return Object.fromEntries(Object.entries(attributes).map(([name, value]) => [
|
|
29
|
+
name,
|
|
30
|
+
isSensitiveLogFieldName(name)
|
|
31
|
+
? REDACTED_LOG_VALUE
|
|
32
|
+
: redactLogAttributeValue(value, seenObjects, depth + 1),
|
|
33
|
+
]));
|
|
34
|
+
}
|
|
35
|
+
function redactLogAttributeValue(value, seenObjects, depth) {
|
|
36
|
+
if (typeof value === "string")
|
|
37
|
+
return redactSensitiveText(value);
|
|
38
|
+
if (value === null ||
|
|
39
|
+
value === undefined ||
|
|
40
|
+
typeof value === "number" ||
|
|
41
|
+
typeof value === "boolean") {
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
if (typeof value !== "object")
|
|
45
|
+
return redactSensitiveText(String(value));
|
|
46
|
+
if (depth > MAX_LOG_ATTRIBUTE_DEPTH || seenObjects.has(value))
|
|
47
|
+
return REDACTED_LOG_VALUE;
|
|
48
|
+
seenObjects.add(value);
|
|
49
|
+
try {
|
|
50
|
+
if (Array.isArray(value)) {
|
|
51
|
+
return value.map((entry) => redactLogAttributeValue(entry, seenObjects, depth + 1));
|
|
52
|
+
}
|
|
53
|
+
if (!isLogAttributeRecord(value))
|
|
54
|
+
return REDACTED_LOG_VALUE;
|
|
55
|
+
return redactLogAttributes(value, seenObjects, depth);
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
seenObjects.delete(value);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function redactSensitiveText(value) {
|
|
62
|
+
return value
|
|
63
|
+
.replace(SENSITIVE_ASSIGNMENT_PATTERN, `$1${REDACTED_LOG_VALUE}`)
|
|
64
|
+
.replace(SENSITIVE_URL_PARAMETER_PATTERN, `$1${REDACTED_LOG_VALUE}`)
|
|
65
|
+
.replace(BEARER_TOKEN_PATTERN, `$1${REDACTED_LOG_VALUE}`)
|
|
66
|
+
.replace(JSON_WEB_TOKEN_PATTERN, REDACTED_LOG_VALUE);
|
|
67
|
+
}
|
|
68
|
+
function isSensitiveLogFieldName(name) {
|
|
69
|
+
const normalizedName = normalizeFieldName(name);
|
|
70
|
+
return (sensitiveLogFieldNames.has(normalizedName) ||
|
|
71
|
+
normalizedName.endsWith("token") ||
|
|
72
|
+
normalizedName.endsWith("password") ||
|
|
73
|
+
normalizedName.endsWith("secret"));
|
|
74
|
+
}
|
|
75
|
+
function normalizeFieldName(name) {
|
|
76
|
+
return name.toLowerCase().replace(/[^a-z\d]/g, "");
|
|
77
|
+
}
|
|
78
|
+
function isLogAttributeRecord(value) {
|
|
79
|
+
return Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null;
|
|
80
|
+
}
|
package/package.json
CHANGED