@dash0/sdk-web 0.18.4 → 0.19.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,7 +1,7 @@
1
1
  import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
2
  import { InitOptions, InstrumentationName } from "../types/options";
3
3
  import { PropagatorConfig, Vars } from "../vars";
4
- import { SERVICE_NAME } from "../semantic-conventions";
4
+ import { SERVICE_NAME, SERVICE_NAMESPACE } from "../semantic-conventions";
5
5
  import { init as initFun } from "./init";
6
6
 
7
7
  // Mock all the instrumentation modules
@@ -237,6 +237,25 @@ describe("init", () => {
237
237
  });
238
238
  });
239
239
 
240
+ describe("serviceNamespace", () => {
241
+ it("should add serviceNamespace to resource attributes when provided", async () => {
242
+ init({
243
+ ...baseOptions,
244
+ serviceNamespace: "my-namespace",
245
+ });
246
+
247
+ const namespaceAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAMESPACE);
248
+ expect(namespaceAttr?.value.stringValue).toBe("my-namespace");
249
+ });
250
+
251
+ it("should not add serviceNamespace to resource attributes when not provided", async () => {
252
+ init(baseOptions);
253
+
254
+ const namespaceAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAMESPACE);
255
+ expect(namespaceAttr).toBeUndefined();
256
+ });
257
+ });
258
+
240
259
  describe("serviceName fallback", () => {
241
260
  it("should use provided serviceName when it is valid", async () => {
242
261
  init({
@@ -278,6 +297,81 @@ describe("init", () => {
278
297
  expect(serviceNameAttr?.value.stringValue).toBe("test-hostname.example.com");
279
298
  });
280
299
 
300
+ it.each([
301
+ ["single quote", "evil';DROP TABLE users;--"],
302
+ ["double quote", 'svc"name'],
303
+ ["semicolon", "svc;injected"],
304
+ ["open brace", "svc${tpl}"],
305
+ ["close brace", "svc}name"],
306
+ ["less-than", "<script>"],
307
+ ["greater-than", "svc>name"],
308
+ ["embedded newline", "svc\nname"],
309
+ ["NUL byte", "svc\x00name"],
310
+ ])("should fallback to location.hostname by default when serviceName contains %s", async (_label, suspicious) => {
311
+ init({
312
+ ...baseOptions,
313
+ serviceName: suspicious,
314
+ });
315
+
316
+ const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
317
+ expect(serviceNameAttr?.value.stringValue).toBe("test-hostname.example.com");
318
+ });
319
+
320
+ it.each([
321
+ ["single quote", "evil';DROP TABLE users;--"],
322
+ ["double quote", 'svc"name'],
323
+ ["semicolon", "svc;injected"],
324
+ ["embedded newline", "svc\nname"],
325
+ ])(
326
+ "should also fallback when rejectSuspiciousServiceName is explicitly true and serviceName contains %s",
327
+ async (_label, suspicious) => {
328
+ init({
329
+ ...baseOptions,
330
+ serviceName: suspicious,
331
+ rejectSuspiciousServiceName: true,
332
+ });
333
+
334
+ const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
335
+ expect(serviceNameAttr?.value.stringValue).toBe("test-hostname.example.com");
336
+ }
337
+ );
338
+
339
+ it.each([
340
+ ["single quote", "evil';DROP TABLE users;--"],
341
+ ["semicolon", "svc;injected"],
342
+ ["less-than", "<script>"],
343
+ ])(
344
+ "should keep suspicious serviceName unchanged when rejectSuspiciousServiceName is explicitly false (%s)",
345
+ async (_label, suspicious) => {
346
+ init({
347
+ ...baseOptions,
348
+ serviceName: suspicious,
349
+ rejectSuspiciousServiceName: false,
350
+ });
351
+
352
+ const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
353
+ expect(serviceNameAttr?.value.stringValue).toBe(suspicious);
354
+ }
355
+ );
356
+
357
+ it.each([
358
+ ["forward slash", "myteam/myservice"],
359
+ ["backslash", "myteam\\myservice"],
360
+ ["spaces", "my service"],
361
+ ["dots and hyphens", "svc-name.v2"],
362
+ ])(
363
+ "should keep serviceName with allowed characters like %s under the default configuration",
364
+ async (_label, allowed) => {
365
+ init({
366
+ ...baseOptions,
367
+ serviceName: allowed,
368
+ });
369
+
370
+ const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
371
+ expect(serviceNameAttr?.value.stringValue).toBe(allowed);
372
+ }
373
+ );
374
+
281
375
  it("should fallback to 'unknown' when serviceName is empty and location.hostname is not available", async () => {
282
376
  vi.resetModules();
283
377
 
@@ -1,5 +1,6 @@
1
1
  // Resource Attribute Keys
2
2
  export const SERVICE_NAME = "service.name";
3
+ export const SERVICE_NAMESPACE = "service.namespace";
3
4
  export const SERVICE_VERSION = "service.version";
4
5
  export const DEPLOYMENT_ENVIRONMENT_NAME = "deployment.environment.name";
5
6
  export const DEPLOYMENT_NAME = "deployment.name";
@@ -6,6 +6,7 @@ export type InstrumentationName = "@dash0/navigation" | "@dash0/web-vitals" | "@
6
6
 
7
7
  export type InitOptions = {
8
8
  serviceName: string;
9
+ serviceNamespace?: string;
9
10
  serviceVersion?: string;
10
11
  environment?: string;
11
12
  deploymentName?: string;
@@ -16,6 +17,17 @@ export type InitOptions = {
16
17
  */
17
18
  additionalSignalAttributes?: Record<string, AttributeValueType | AnyValue>;
18
19
 
20
+ /**
21
+ * When enabled (the default), reject `serviceName` values that contain
22
+ * characters commonly associated with injection payloads (quotes, angle
23
+ * brackets, braces, semicolons, control characters) and fall back to
24
+ * `location.hostname`. This guards against automated security scanners or
25
+ * untrusted callers influencing the value sent to `init()`.
26
+ *
27
+ * Set to `false` to opt out and pass the `serviceName` through unchanged.
28
+ */
29
+ rejectSuspiciousServiceName?: boolean;
30
+
19
31
  /**
20
32
  * OTLP endpoints to which the generated telemetry should be sent to.
21
33
  */
@@ -14,4 +14,5 @@ export * from "./math";
14
14
  export * from "./origin";
15
15
  export * from "./url";
16
16
  export * from "./pick";
17
+ export * from "./sanitize";
17
18
  export * from "./wrap";
@@ -0,0 +1,13 @@
1
+ const SUSPICIOUS_CHARS = /['"<>{};\x00-\x1F\x7F]/;
2
+
3
+ /**
4
+ * Returns true if the value is safe to use as a `service.name` resource attribute.
5
+ *
6
+ * Rejects values containing characters commonly used in injection payloads:
7
+ * quotes, angle brackets, braces, semicolons, and C0 control characters / DEL.
8
+ * Forward and back slashes are intentionally permitted so names like
9
+ * `myteam/myservice` remain valid.
10
+ */
11
+ export function isSafeServiceName(value: string): boolean {
12
+ return !SUSPICIOUS_CHARS.test(value);
13
+ }
@@ -0,0 +1,60 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { isSafeServiceName } from "./sanitize";
3
+
4
+ describe("isSafeServiceName", () => {
5
+ describe("accepts safe values", () => {
6
+ it.each([
7
+ "my-service",
8
+ "my_service",
9
+ "my.service",
10
+ "MyService123",
11
+ "myteam/myservice",
12
+ "myteam\\myservice",
13
+ "service with spaces",
14
+ "service-name.v2",
15
+ "résumé-service",
16
+ "サービス",
17
+ "a",
18
+ ])("accepts %j", (value) => {
19
+ expect(isSafeServiceName(value)).toBe(true);
20
+ });
21
+
22
+ it("accepts the empty string (empty/whitespace handling lives elsewhere)", () => {
23
+ expect(isSafeServiceName("")).toBe(true);
24
+ });
25
+ });
26
+
27
+ describe("rejects suspicious characters", () => {
28
+ it.each([
29
+ ["single quote", "evil';DROP TABLE"],
30
+ ["double quote", 'say "hi"'],
31
+ ["semicolon", "a;b"],
32
+ ["open brace", "${injected}"],
33
+ ["close brace", "trailing}"],
34
+ ["less-than", "<script>"],
35
+ ["greater-than", "value>other"],
36
+ ])("rejects %s", (_label, value) => {
37
+ expect(isSafeServiceName(value)).toBe(false);
38
+ });
39
+
40
+ it("rejects NUL", () => {
41
+ expect(isSafeServiceName("a\x00b")).toBe(false);
42
+ });
43
+
44
+ it("rejects embedded newline (log injection)", () => {
45
+ expect(isSafeServiceName("line1\nline2")).toBe(false);
46
+ });
47
+
48
+ it("rejects embedded carriage return", () => {
49
+ expect(isSafeServiceName("a\rb")).toBe(false);
50
+ });
51
+
52
+ it("rejects embedded tab", () => {
53
+ expect(isSafeServiceName("a\tb")).toBe(false);
54
+ });
55
+
56
+ it("rejects DEL", () => {
57
+ expect(isSafeServiceName("a\x7Fb")).toBe(false);
58
+ });
59
+ });
60
+ });