@dash0/sdk-web 0.18.5 → 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.
@@ -297,6 +297,81 @@ describe("init", () => {
297
297
  expect(serviceNameAttr?.value.stringValue).toBe("test-hostname.example.com");
298
298
  });
299
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
+
300
375
  it("should fallback to 'unknown' when serviceName is empty and location.hostname is not available", async () => {
301
376
  vi.resetModules();
302
377
 
@@ -17,6 +17,17 @@ export type InitOptions = {
17
17
  */
18
18
  additionalSignalAttributes?: Record<string, AttributeValueType | AnyValue>;
19
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
+
20
31
  /**
21
32
  * OTLP endpoints to which the generated telemetry should be sent to.
22
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
+ });