@ogcio/o11y-sdk-react 0.2.0 → 0.4.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/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.4.0](https://github.com/ogcio/o11y/compare/@ogcio/o11y-sdk-react@v0.3.0...@ogcio/o11y-sdk-react@v0.4.0) (2025-09-02)
4
+
5
+
6
+ ### Features
7
+
8
+ * o11y react-sdk IP pii redaction AB[#30758](https://github.com/ogcio/o11y/issues/30758) ([#196](https://github.com/ogcio/o11y/issues/196)) ([d6bb83f](https://github.com/ogcio/o11y/commit/d6bb83fa425ffdf3fb023ec62caeef070995d07a))
9
+
10
+
11
+ ### Miscellaneous Chores
12
+
13
+ * **deps:** bump the root-deps group across 1 directory with 16 updates ([#193](https://github.com/ogcio/o11y/issues/193)) ([c523565](https://github.com/ogcio/o11y/commit/c523565a6ba7f327473e8d93afc7d532ac220457))
14
+ * **deps:** bump the root-deps group across 3 directories with 34 updates ([#188](https://github.com/ogcio/o11y/issues/188)) ([c0059f3](https://github.com/ogcio/o11y/commit/c0059f33cbb5c39cf6df0f4640604796dbbd3f57))
15
+
16
+ ## [0.3.0](https://github.com/ogcio/o11y/compare/@ogcio/o11y-sdk-react@v0.2.0...@ogcio/o11y-sdk-react@v0.3.0) (2025-07-31)
17
+
18
+
19
+ ### Features
20
+
21
+ * **sdk-react:** pii detection ([#180](https://github.com/ogcio/o11y/issues/180)) ([57ddf2f](https://github.com/ogcio/o11y/commit/57ddf2f4792207e62ea6bf9ea797bc652942db06))
22
+ * update AWS secrets manager connection AB[#29565](https://github.com/ogcio/o11y/issues/29565) ([#166](https://github.com/ogcio/o11y/issues/166)) ([962c03d](https://github.com/ogcio/o11y/commit/962c03de2da4060a81aa603c355f3d60cafe3c30))
23
+
3
24
  ## [0.2.0](https://github.com/ogcio/o11y/compare/@ogcio/o11y-sdk-react@v0.1.0-beta.7...@ogcio/o11y-sdk-react@v0.2.0) (2025-06-24)
4
25
 
5
26
 
@@ -33,6 +33,21 @@ export interface FaroSDKConfig extends SDKConfig {
33
33
  * @example http://localhost:9000
34
34
  */
35
35
  corsTraceHeaders?: string;
36
+ /**
37
+ * Enable/Disable PII detection for GDPR data
38
+ */
39
+ detection?: {
40
+ /**
41
+ * Redact email addresses
42
+ * @default true
43
+ */
44
+ email?: boolean;
45
+ /**
46
+ * Redact ip addresses
47
+ * @default true
48
+ */
49
+ ip?: boolean;
50
+ };
36
51
  }
37
52
  export type SDKCollectorMode = "single" | "batch";
38
53
  export type SDKLogLevel = "NONE" | "ERROR" | "WARN" | "INFO" | "DEBUG" | "VERBOSE" | "ALL";
@@ -1,3 +1,3 @@
1
- import type { FaroSDKConfig } from "./index.js";
2
1
  import { type Faro } from "@grafana/faro-react";
2
+ import type { FaroSDKConfig } from "./index.js";
3
3
  export default function buildFaroInstrumentation(config?: FaroSDKConfig): Faro | undefined;
@@ -1,10 +1,12 @@
1
1
  import { ErrorsInstrumentation, getWebInstrumentations, initializeFaro, WebVitalsInstrumentation, } from "@grafana/faro-react";
2
+ import { TracingInstrumentation } from "@grafana/faro-web-tracing";
2
3
  import { diag, DiagConsoleLogger, DiagLogLevel } from "@opentelemetry/api";
3
4
  import { W3CTraceContextPropagator } from "@opentelemetry/core";
5
+ import { DocumentLoadInstrumentation } from "@opentelemetry/instrumentation-document-load";
4
6
  import { FetchInstrumentation } from "@opentelemetry/instrumentation-fetch";
5
7
  import { UserInteractionInstrumentation } from "@opentelemetry/instrumentation-user-interaction";
6
- import { DocumentLoadInstrumentation } from "@opentelemetry/instrumentation-document-load";
7
- import { TracingInstrumentation } from "@grafana/faro-web-tracing";
8
+ import { _beforeSend } from "./internals/hooks.js";
9
+ import { redactors, } from "./internals/redaction/redactors/index.js";
8
10
  export default function buildFaroInstrumentation(config) {
9
11
  if (!config) {
10
12
  console.warn("observability config not set. Skipping Faro OpenTelemetry instrumentation.");
@@ -18,6 +20,11 @@ export default function buildFaroInstrumentation(config) {
18
20
  console.error("collectorUrl does not use a valid format. Skipping Faro OpenTelemetry instrumentation.");
19
21
  return;
20
22
  }
23
+ const redactorsChain = Object.entries(redactors)
24
+ .filter(([key]) => {
25
+ return config.detection?.[key] !== false;
26
+ })
27
+ .map(([_, value]) => value);
21
28
  try {
22
29
  diag.setLogger(new DiagConsoleLogger(), config.diagLogLevel
23
30
  ? DiagLogLevel[config.diagLogLevel]
@@ -30,6 +37,7 @@ export default function buildFaroInstrumentation(config) {
30
37
  batching: {
31
38
  enabled: !(config.collectorMode === "single"),
32
39
  },
40
+ beforeSend: _beforeSend(redactorsChain),
33
41
  instrumentations: [
34
42
  ...getWebInstrumentations({
35
43
  captureConsole: true,
@@ -0,0 +1,3 @@
1
+ import { APIEvent, TransportItem } from "@grafana/faro-web-sdk";
2
+ import { Redactor } from "./redaction/redactors/index.js";
3
+ export declare const _beforeSend: (redactors: Redactor[]) => (item: TransportItem<APIEvent>) => TransportItem<APIEvent> | null;
@@ -0,0 +1,34 @@
1
+ import { _cleanStringPII } from "./redaction/pii-detection.js";
2
+ function _cleanObjectPII(data, type, redactors) {
3
+ if (!data) {
4
+ return;
5
+ }
6
+ if (Array.isArray(data)) {
7
+ data.forEach((item, index) => {
8
+ if (typeof item === "string") {
9
+ data[index] = _cleanStringPII(data[index], type, redactors);
10
+ return;
11
+ }
12
+ if (typeof item === "object") {
13
+ _cleanObjectPII(item, type, redactors);
14
+ }
15
+ });
16
+ }
17
+ if (typeof data === "object") {
18
+ for (const key in data) {
19
+ if (typeof data[key] === "string") {
20
+ data[key] = _cleanStringPII(data[key], type, redactors);
21
+ }
22
+ if (typeof data[key] === "object") {
23
+ _cleanObjectPII(data[key], type, redactors);
24
+ }
25
+ }
26
+ }
27
+ }
28
+ export const _beforeSend = (redactors) => (item) => {
29
+ if (redactors.length === 0) {
30
+ return item;
31
+ }
32
+ _cleanObjectPII(item, item.type, redactors);
33
+ return item;
34
+ };
@@ -0,0 +1,19 @@
1
+ import { TransportItemType } from "@grafana/faro-web-sdk";
2
+ import { Redactor } from "./redactors/index.js";
3
+ /**
4
+ * Cleans a string by redacting configured PIIs and emitting metrics for redacted values.
5
+ *
6
+ * If the string is URL-encoded, it will be decoded before redaction.
7
+ * Metrics are emitted for:
8
+ * - each domain found in redacted email addresses.
9
+ * - IPv4|IPv6 addresses redacted.
10
+ *
11
+ * @template T
12
+ *
13
+ * @param {string} value - The input value to sanitize.
14
+ * @param {"trace" | "log"} source - The source context of the input, used in metrics.
15
+ * @param {Redactor[]} redactors - The string processors containing the redaction logic.
16
+ *
17
+ * @returns {T} The cleaned string with any configured PII replaced by `[REDACTED PII_TYPE]`.
18
+ */
19
+ export declare function _cleanStringPII(value: string, source: TransportItemType, redactors: Redactor[]): string;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Checks whether a string contains URI-encoded components.
3
+ *
4
+ * @param {string} value - The string to inspect.
5
+ * @returns {boolean} `true` if the string is encoded, `false` otherwise.
6
+ */
7
+ function _containsEncodedComponents(value) {
8
+ try {
9
+ return decodeURI(value) !== decodeURIComponent(value);
10
+ }
11
+ catch {
12
+ return false;
13
+ }
14
+ }
15
+ /**
16
+ * Cleans a string by redacting configured PIIs and emitting metrics for redacted values.
17
+ *
18
+ * If the string is URL-encoded, it will be decoded before redaction.
19
+ * Metrics are emitted for:
20
+ * - each domain found in redacted email addresses.
21
+ * - IPv4|IPv6 addresses redacted.
22
+ *
23
+ * @template T
24
+ *
25
+ * @param {string} value - The input value to sanitize.
26
+ * @param {"trace" | "log"} source - The source context of the input, used in metrics.
27
+ * @param {Redactor[]} redactors - The string processors containing the redaction logic.
28
+ *
29
+ * @returns {T} The cleaned string with any configured PII replaced by `[REDACTED PII_TYPE]`.
30
+ */
31
+ export function _cleanStringPII(value, source, redactors) {
32
+ let kind = "string";
33
+ let decodedValue = value;
34
+ if (_containsEncodedComponents(value)) {
35
+ decodedValue = decodeURIComponent(value);
36
+ kind = "url";
37
+ }
38
+ return redactors.reduce((redactedValue, currentRedactor) => currentRedactor(redactedValue, source, kind), decodedValue);
39
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Redacts provided input and collects metadata metrics about redacted email domains,
3
+ * data source and kind.
4
+ *
5
+ * @param {string} value The input string potentially containing email addresses.
6
+ * @returns {string} the redacted value
7
+ */
8
+ export declare const emailRedactor: (value: string, source: string, kind: string) => string;
@@ -0,0 +1,55 @@
1
+ import { faro } from "@grafana/faro-web-sdk";
2
+ const EMAIL_REGEX = /[\p{L}\p{N}._%+-]+@((?:[\p{L}\p{N}-]+\.)+[\p{L}]{2,})/giu;
3
+ /**
4
+ * Redacts all email addresses in the input string and collects metadata.
5
+ *
6
+ * @param {string} value The input string potentially containing email addresses.
7
+ * @returns {{
8
+ * redacted: string,
9
+ * count: number,
10
+ * domains: Record<string, number>
11
+ * }}
12
+ *
13
+ * An object containing:
14
+ * - `redacted`: the string with email addresses replaced by `[REDACTED EMAIL]`
15
+ * - `count`: total number of email addresses redacted
16
+ * - `domains`: a map of domain names to the number of times they were redacted
17
+ */
18
+ function _redactEmails(value) {
19
+ let count = 0;
20
+ const domains = {};
21
+ const redacted = value.replace(EMAIL_REGEX, (_, domain) => {
22
+ count++;
23
+ domains[domain] = (domains[domain] || 0) + 1;
24
+ return "[REDACTED EMAIL]";
25
+ });
26
+ return { redacted, count, domains };
27
+ }
28
+ /**
29
+ * Redacts provided input and collects metadata metrics about redacted email domains,
30
+ * data source and kind.
31
+ *
32
+ * @param {string} value The input string potentially containing email addresses.
33
+ * @returns {string} the redacted value
34
+ */
35
+ export const emailRedactor = (value, source, kind) => {
36
+ const { redacted, count, domains } = _redactEmails(value);
37
+ if (count > 0) {
38
+ for (const [domain, domainCount] of Object.entries(domains)) {
39
+ faro.api.pushMeasurement({
40
+ type: "faro_o11y_pii_redaction",
41
+ values: {
42
+ redacted: domainCount,
43
+ },
44
+ }, {
45
+ context: {
46
+ pii_type: "email",
47
+ redaction_source: source,
48
+ pii_email_domain: domain,
49
+ pii_format: kind,
50
+ },
51
+ });
52
+ }
53
+ }
54
+ return redacted;
55
+ };
@@ -0,0 +1,4 @@
1
+ import { type FaroSDKConfig } from "../../../index.js";
2
+ export type Redactor = (value: string, source: string, kind: string) => string;
3
+ export type RedactorKeys = keyof NonNullable<FaroSDKConfig["detection"]>;
4
+ export declare const redactors: Record<RedactorKeys, Redactor>;
@@ -0,0 +1,6 @@
1
+ import { emailRedactor } from "./email.js";
2
+ import { ipRedactor } from "./ip.js";
3
+ export const redactors = {
4
+ email: emailRedactor,
5
+ ip: ipRedactor,
6
+ };
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Redacts provided input and collects metadata metrics about redacted IPs,
3
+ * data source and kind.
4
+ *
5
+ * @param {string} value The input string potentially containing IP addresses.
6
+ * @param {string} source The source of the attribute being redacted (log, span, metric).
7
+ * @param {string} kind The type of the data structure containing the PII
8
+ * @returns {string} the redacted value
9
+ */
10
+ export declare const ipRedactor: (value: string, source: string, kind: string) => string;
@@ -0,0 +1,61 @@
1
+ import { faro } from "@grafana/faro-web-sdk";
2
+ // Generous IP address matchers (might match some invalid addresses like 192.168.01.1)
3
+ const IPV4_REGEX = /(?<!\d)(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}(?!\d)/gi;
4
+ const IPV6_REGEX = /(?<![0-9a-f:])((?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,7}:|:(?::[0-9A-Fa-f]{1,4}){1,7}|(?:[0-9A-Fa-f]{1,4}:){1,6}:[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,5}(?::[0-9A-Fa-f]{1,4}){1,2}|(?:[0-9A-Fa-f]{1,4}:){1,4}(?::[0-9A-Fa-f]{1,4}){1,3}|(?:[0-9A-Fa-f]{1,4}:){1,3}(?::[0-9A-Fa-f]{1,4}){1,4}|(?:[0-9A-Fa-f]{1,4}:){1,2}(?::[0-9A-Fa-f]{1,4}){1,5}|[0-9A-Fa-f]{1,4}:(?::[0-9A-Fa-f]{1,4}){1,6}|:(?::[0-9A-Fa-f]{1,4}){1,7}:?|(?:[0-9A-Fa-f]{1,4}:){1,4}:(?:25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3})(?![0-9a-f:])/gi;
5
+ /**
6
+ * Redacts all ip addresses in the input string and collects metadata.
7
+ *
8
+ * @param {string} value The input string potentially containing ip addresses.
9
+ * @returns {{
10
+ * redacted: string,
11
+ * count: number,
12
+ * domains: Record<string, number>
13
+ * }}
14
+ *
15
+ * An object containing:
16
+ * - `redacted`: the string with IP addresses replaced by `[REDACTED IPV*]`
17
+ * - `counters`: total number of addresses redacted by IPv* type
18
+ * - `domains`: a map of domain names to the number of times they were redacted
19
+ */
20
+ function _redactIps(value) {
21
+ const counters = {};
22
+ const redacted = value
23
+ .replace(IPV4_REGEX, () => {
24
+ counters["IPv4"] = (counters["IPv4"] || 0) + 1;
25
+ return "[REDACTED IPV4]";
26
+ })
27
+ .replace(IPV6_REGEX, () => {
28
+ counters["IPv4"] = (counters["IPv4"] || 0) + 1;
29
+ return "[REDACTED IPV6]";
30
+ });
31
+ return { redacted, counters };
32
+ }
33
+ /**
34
+ * Redacts provided input and collects metadata metrics about redacted IPs,
35
+ * data source and kind.
36
+ *
37
+ * @param {string} value The input string potentially containing IP addresses.
38
+ * @param {string} source The source of the attribute being redacted (log, span, metric).
39
+ * @param {string} kind The type of the data structure containing the PII
40
+ * @returns {string} the redacted value
41
+ */
42
+ export const ipRedactor = (value, source, kind) => {
43
+ const { redacted, counters } = _redactIps(value);
44
+ Object.entries(counters).forEach(([type, counter]) => {
45
+ if (counter > 0) {
46
+ faro.api.pushMeasurement({
47
+ type: "faro_o11y_pii_redaction",
48
+ values: {
49
+ redacted: counter,
50
+ },
51
+ }, {
52
+ context: {
53
+ pii_type: type,
54
+ redaction_source: source,
55
+ pii_format: kind,
56
+ },
57
+ });
58
+ }
59
+ });
60
+ return redacted;
61
+ };
@@ -3,7 +3,7 @@ export default defineConfig({
3
3
  test: {
4
4
  globals: true,
5
5
  watch: false,
6
- include: ["**/test/*.test.ts"],
6
+ include: ["**/*.test.ts"],
7
7
  exclude: ["**/fixtures/**", "**/dist/**"],
8
8
  poolOptions: {
9
9
  threads: {
package/lib/index.ts CHANGED
@@ -34,6 +34,21 @@ export interface FaroSDKConfig extends SDKConfig {
34
34
  * @example http://localhost:9000
35
35
  */
36
36
  corsTraceHeaders?: string;
37
+ /**
38
+ * Enable/Disable PII detection for GDPR data
39
+ */
40
+ detection?: {
41
+ /**
42
+ * Redact email addresses
43
+ * @default true
44
+ */
45
+ email?: boolean;
46
+ /**
47
+ * Redact ip addresses
48
+ * @default true
49
+ */
50
+ ip?: boolean;
51
+ };
37
52
  }
38
53
 
39
54
  export type SDKCollectorMode = "single" | "batch";
@@ -1,4 +1,3 @@
1
- import type { FaroSDKConfig } from "./index.js";
2
1
  import {
3
2
  ErrorsInstrumentation,
4
3
  getWebInstrumentations,
@@ -6,12 +5,18 @@ import {
6
5
  WebVitalsInstrumentation,
7
6
  type Faro,
8
7
  } from "@grafana/faro-react";
8
+ import { TracingInstrumentation } from "@grafana/faro-web-tracing";
9
9
  import { diag, DiagConsoleLogger, DiagLogLevel } from "@opentelemetry/api";
10
10
  import { W3CTraceContextPropagator } from "@opentelemetry/core";
11
+ import { DocumentLoadInstrumentation } from "@opentelemetry/instrumentation-document-load";
11
12
  import { FetchInstrumentation } from "@opentelemetry/instrumentation-fetch";
12
13
  import { UserInteractionInstrumentation } from "@opentelemetry/instrumentation-user-interaction";
13
- import { DocumentLoadInstrumentation } from "@opentelemetry/instrumentation-document-load";
14
- import { TracingInstrumentation } from "@grafana/faro-web-tracing";
14
+ import type { FaroSDKConfig } from "./index.js";
15
+ import { _beforeSend } from "./internals/hooks.js";
16
+ import {
17
+ RedactorKeys,
18
+ redactors,
19
+ } from "./internals/redaction/redactors/index.js";
15
20
 
16
21
  export default function buildFaroInstrumentation(
17
22
  config?: FaroSDKConfig,
@@ -37,6 +42,11 @@ export default function buildFaroInstrumentation(
37
42
  return;
38
43
  }
39
44
 
45
+ const redactorsChain = Object.entries(redactors)
46
+ .filter(([key]) => {
47
+ return config.detection?.[key as RedactorKeys] !== false;
48
+ })
49
+ .map(([_, value]) => value);
40
50
  try {
41
51
  diag.setLogger(
42
52
  new DiagConsoleLogger(),
@@ -53,11 +63,11 @@ export default function buildFaroInstrumentation(
53
63
  batching: {
54
64
  enabled: !(config.collectorMode === "single"),
55
65
  },
66
+ beforeSend: _beforeSend(redactorsChain),
56
67
  instrumentations: [
57
68
  ...getWebInstrumentations({
58
69
  captureConsole: true,
59
70
  }),
60
-
61
71
  new ErrorsInstrumentation(),
62
72
  new WebVitalsInstrumentation(),
63
73
  new TracingInstrumentation({
@@ -0,0 +1,55 @@
1
+ import {
2
+ APIEvent,
3
+ TransportItem,
4
+ TransportItemType,
5
+ } from "@grafana/faro-web-sdk";
6
+ import { _cleanStringPII } from "./redaction/pii-detection.js";
7
+ import { Redactor } from "./redaction/redactors/index.js";
8
+
9
+ function _cleanObjectPII<
10
+ T extends string | Record<string, unknown> | unknown[] | unknown,
11
+ >(data: T, type: TransportItemType, redactors: Redactor[]): void {
12
+ if (!data) {
13
+ return;
14
+ }
15
+
16
+ if (Array.isArray(data)) {
17
+ data.forEach((item, index) => {
18
+ if (typeof item === "string") {
19
+ data[index] = _cleanStringPII(data[index], type, redactors);
20
+ return;
21
+ }
22
+
23
+ if (typeof item === "object") {
24
+ _cleanObjectPII<T>(item, type, redactors);
25
+ }
26
+ });
27
+ }
28
+
29
+ if (typeof data === "object") {
30
+ for (const key in data) {
31
+ if (typeof data[key] === "string") {
32
+ data[key] = _cleanStringPII(
33
+ data[key],
34
+ type,
35
+ redactors,
36
+ ) as (typeof data)[typeof key];
37
+ }
38
+
39
+ if (typeof data[key] === "object") {
40
+ _cleanObjectPII(data[key], type, redactors);
41
+ }
42
+ }
43
+ }
44
+ }
45
+
46
+ export const _beforeSend =
47
+ (redactors: Redactor[]) =>
48
+ (item: TransportItem<APIEvent>): TransportItem<APIEvent> | null => {
49
+ if (redactors.length === 0) {
50
+ return item;
51
+ }
52
+
53
+ _cleanObjectPII(item, item.type, redactors);
54
+ return item;
55
+ };
@@ -0,0 +1,52 @@
1
+ import { TransportItemType } from "@grafana/faro-web-sdk";
2
+ import { Redactor } from "./redactors/index.js";
3
+
4
+ /**
5
+ * Checks whether a string contains URI-encoded components.
6
+ *
7
+ * @param {string} value - The string to inspect.
8
+ * @returns {boolean} `true` if the string is encoded, `false` otherwise.
9
+ */
10
+ function _containsEncodedComponents(value: string) {
11
+ try {
12
+ return decodeURI(value) !== decodeURIComponent(value);
13
+ } catch {
14
+ return false;
15
+ }
16
+ }
17
+
18
+ /**
19
+ * Cleans a string by redacting configured PIIs and emitting metrics for redacted values.
20
+ *
21
+ * If the string is URL-encoded, it will be decoded before redaction.
22
+ * Metrics are emitted for:
23
+ * - each domain found in redacted email addresses.
24
+ * - IPv4|IPv6 addresses redacted.
25
+ *
26
+ * @template T
27
+ *
28
+ * @param {string} value - The input value to sanitize.
29
+ * @param {"trace" | "log"} source - The source context of the input, used in metrics.
30
+ * @param {Redactor[]} redactors - The string processors containing the redaction logic.
31
+ *
32
+ * @returns {T} The cleaned string with any configured PII replaced by `[REDACTED PII_TYPE]`.
33
+ */
34
+ export function _cleanStringPII(
35
+ value: string,
36
+ source: TransportItemType,
37
+ redactors: Redactor[],
38
+ ): string {
39
+ let kind: "string" | "url" = "string";
40
+ let decodedValue = value;
41
+
42
+ if (_containsEncodedComponents(value)) {
43
+ decodedValue = decodeURIComponent(value);
44
+ kind = "url";
45
+ }
46
+
47
+ return redactors.reduce(
48
+ (redactedValue: string, currentRedactor): string =>
49
+ currentRedactor(redactedValue, source, kind),
50
+ decodedValue,
51
+ );
52
+ }
@@ -0,0 +1,68 @@
1
+ import { faro } from "@grafana/faro-web-sdk";
2
+
3
+ const EMAIL_REGEX = /[\p{L}\p{N}._%+-]+@((?:[\p{L}\p{N}-]+\.)+[\p{L}]{2,})/giu;
4
+
5
+ /**
6
+ * Redacts all email addresses in the input string and collects metadata.
7
+ *
8
+ * @param {string} value The input string potentially containing email addresses.
9
+ * @returns {{
10
+ * redacted: string,
11
+ * count: number,
12
+ * domains: Record<string, number>
13
+ * }}
14
+ *
15
+ * An object containing:
16
+ * - `redacted`: the string with email addresses replaced by `[REDACTED EMAIL]`
17
+ * - `count`: total number of email addresses redacted
18
+ * - `domains`: a map of domain names to the number of times they were redacted
19
+ */
20
+ function _redactEmails(value: string): {
21
+ redacted: string;
22
+ count: number;
23
+ domains: Record<string, number>;
24
+ } {
25
+ let count = 0;
26
+ const domains: Record<string, number> = {};
27
+
28
+ const redacted = value.replace(EMAIL_REGEX, (_, domain) => {
29
+ count++;
30
+ domains[domain] = (domains[domain] || 0) + 1;
31
+ return "[REDACTED EMAIL]";
32
+ });
33
+
34
+ return { redacted, count, domains };
35
+ }
36
+
37
+ /**
38
+ * Redacts provided input and collects metadata metrics about redacted email domains,
39
+ * data source and kind.
40
+ *
41
+ * @param {string} value The input string potentially containing email addresses.
42
+ * @returns {string} the redacted value
43
+ */
44
+ export const emailRedactor = (value: string, source: string, kind: string) => {
45
+ const { redacted, count, domains } = _redactEmails(value);
46
+
47
+ if (count > 0) {
48
+ for (const [domain, domainCount] of Object.entries(domains)) {
49
+ faro.api.pushMeasurement(
50
+ {
51
+ type: "faro_o11y_pii_redaction",
52
+ values: {
53
+ redacted: domainCount,
54
+ },
55
+ },
56
+ {
57
+ context: {
58
+ pii_type: "email",
59
+ redaction_source: source,
60
+ pii_email_domain: domain,
61
+ pii_format: kind,
62
+ },
63
+ },
64
+ );
65
+ }
66
+ }
67
+ return redacted;
68
+ };
@@ -0,0 +1,12 @@
1
+ import { type FaroSDKConfig } from "../../../index.js";
2
+ import { emailRedactor } from "./email.js";
3
+ import { ipRedactor } from "./ip.js";
4
+
5
+ export type Redactor = (value: string, source: string, kind: string) => string;
6
+
7
+ export type RedactorKeys = keyof NonNullable<FaroSDKConfig["detection"]>;
8
+
9
+ export const redactors: Record<RedactorKeys, Redactor> = {
10
+ email: emailRedactor,
11
+ ip: ipRedactor,
12
+ };