@ciridae/telemetry 0.3.0 → 0.3.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.
@@ -14,6 +14,13 @@ export function createCiridaeTelemetry(configuration) {
14
14
  initializationSubscribers.add(subscriber);
15
15
  return () => initializationSubscribers.delete(subscriber);
16
16
  }
17
+ function registerEventAttribution() {
18
+ posthog.register({
19
+ environment: configuration.environment || "development",
20
+ source: "frontend",
21
+ ...(configuration.projectId ? { project_id: configuration.projectId } : {}),
22
+ });
23
+ }
17
24
  function resetPostHog() {
18
25
  if (!initialized)
19
26
  return;
@@ -23,6 +30,8 @@ export function createCiridaeTelemetry(configuration) {
23
30
  catch (error) {
24
31
  console.error("Error resetting PostHog:", error);
25
32
  }
33
+ // reset() clears registered properties along with the identity.
34
+ registerEventAttribution();
26
35
  }
27
36
  const productCapabilities = createPostHogProductCapabilities(() => initialized, subscribeToInitialization);
28
37
  return {
@@ -32,11 +41,7 @@ export function createCiridaeTelemetry(configuration) {
32
41
  if (!configuration.posthogApiKey.trim())
33
42
  return;
34
43
  posthog.init(configuration.posthogApiKey, buildPostHogConfiguration(configuration));
35
- posthog.register({
36
- environment: configuration.environment || "development",
37
- source: "frontend",
38
- ...(configuration.projectId ? { project_id: configuration.projectId } : {}),
39
- });
44
+ registerEventAttribution();
40
45
  if (configuration.serviceName) {
41
46
  posthog.logger.info("PostHog log ingestion canary", { telemetry_canary: true });
42
47
  }
@@ -197,7 +202,11 @@ function sameUser(current, next) {
197
202
  function getPostHogUiHost(apiHost, configuredUiHost) {
198
203
  if (configuredUiHost)
199
204
  return configuredUiHost;
200
- if (apiHost.replace(/\/+$/, "") === POSTHOG_EU_API_HOST)
205
+ let apiHostWithoutTrailingSlashes = apiHost;
206
+ while (apiHostWithoutTrailingSlashes.endsWith("/")) {
207
+ apiHostWithoutTrailingSlashes = apiHostWithoutTrailingSlashes.slice(0, -1);
208
+ }
209
+ if (apiHostWithoutTrailingSlashes === POSTHOG_EU_API_HOST)
201
210
  return POSTHOG_EU_UI_HOST;
202
211
  return POSTHOG_US_UI_HOST;
203
212
  }
@@ -11,10 +11,10 @@ const sensitiveLogFieldNames = new Set([
11
11
  "passwd",
12
12
  "secret",
13
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;
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+[\w.~+/=-]+|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[^\s,;}\]]+)/gi;
15
+ const URL_PARAMETER_PATTERN = /([?&#])([^=?&#\s]+)=([^&#\s]+)/g;
16
+ const BEARER_TOKEN_PATTERN = /\b(Bearer\s+)[\w.~+/=-]+/gi;
17
+ const JSON_WEB_TOKEN_PATTERN = /\beyJ[\w-]+\.[\w-]+\.[\w-]*\b/g;
18
18
  export function redactCapturedLog(record) {
19
19
  return {
20
20
  ...record,
@@ -61,17 +61,33 @@ function redactLogAttributeValue(value, seenObjects, depth) {
61
61
  function redactSensitiveText(value) {
62
62
  return value
63
63
  .replace(SENSITIVE_ASSIGNMENT_PATTERN, `$1${REDACTED_LOG_VALUE}`)
64
- .replace(SENSITIVE_URL_PARAMETER_PATTERN, `$1${REDACTED_LOG_VALUE}`)
64
+ .replace(URL_PARAMETER_PATTERN, redactSensitiveUrlParameter)
65
65
  .replace(BEARER_TOKEN_PATTERN, `$1${REDACTED_LOG_VALUE}`)
66
66
  .replace(JSON_WEB_TOKEN_PATTERN, REDACTED_LOG_VALUE);
67
67
  }
68
68
  function isSensitiveLogFieldName(name) {
69
69
  const normalizedName = normalizeFieldName(name);
70
70
  return (sensitiveLogFieldNames.has(normalizedName) ||
71
+ normalizedName.endsWith("apikey") ||
72
+ normalizedName.endsWith("authorization") ||
73
+ normalizedName.endsWith("cookie") ||
74
+ normalizedName.endsWith("credential") ||
75
+ normalizedName.endsWith("credentials") ||
71
76
  normalizedName.endsWith("token") ||
72
77
  normalizedName.endsWith("password") ||
73
78
  normalizedName.endsWith("secret"));
74
79
  }
80
+ function redactSensitiveUrlParameter(match, prefix, encodedName) {
81
+ try {
82
+ const decodedName = decodeURIComponent(encodedName);
83
+ if (!sensitiveLogFieldNames.has(normalizeFieldName(decodedName)))
84
+ return match;
85
+ return `${prefix}${encodedName}=${REDACTED_LOG_VALUE}`;
86
+ }
87
+ catch {
88
+ return match;
89
+ }
90
+ }
75
91
  function normalizeFieldName(name) {
76
92
  return name.toLowerCase().replace(/[^a-z\d]/g, "");
77
93
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ciridae/telemetry",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "engines": {