sentrace 0.1.2 → 0.1.3

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.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -0
  3. data/app/assets/javascripts/sentrace.browser.js +101 -28
  4. data/app/assets/javascripts/sentrace.js +101 -28
  5. data/app/controllers/sentrace/application_controller.rb +4 -4
  6. data/app/controllers/sentrace/ingestion_controller.rb +61 -24
  7. data/app/controllers/sentrace/internal/base_controller.rb +12 -1
  8. data/app/controllers/sentrace/internal/events_controller.rb +8 -8
  9. data/app/controllers/sentrace/internal/issues_controller.rb +13 -13
  10. data/app/controllers/sentrace/internal/projects_controller.rb +15 -8
  11. data/app/controllers/sentrace/internal/stats_controller.rb +16 -1
  12. data/app/controllers/sentrace/mcp_controller.rb +9 -5
  13. data/app/models/sentrace/mcp_token.rb +16 -3
  14. data/app/models/sentrace/project_key.rb +24 -3
  15. data/config/routes.rb +1 -1
  16. data/lib/sentrace/breadcrumbs/active_support_logger.rb +6 -1
  17. data/lib/sentrace/configuration.rb +33 -3
  18. data/lib/sentrace/event_builder.rb +3 -0
  19. data/lib/sentrace/ingestion/envelope_parser.rb +36 -7
  20. data/lib/sentrace/ingestion/event_normalizer.rb +17 -4
  21. data/lib/sentrace/ingestion/processor.rb +5 -1
  22. data/lib/sentrace/ingestion/sanitizer.rb +79 -25
  23. data/lib/sentrace/issues/fingerprint.rb +13 -6
  24. data/lib/sentrace/mcp/environment_router.rb +2 -1
  25. data/lib/sentrace/mcp/local_backend.rb +9 -3
  26. data/lib/sentrace/mcp/remote_http_client.rb +65 -15
  27. data/lib/sentrace/mcp/server.rb +20 -5
  28. data/lib/sentrace/request_security.rb +126 -0
  29. data/lib/sentrace/storage_stats.rb +1 -1
  30. data/lib/sentrace/version.rb +1 -1
  31. data/lib/sentrace.rb +1 -0
  32. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b39aede2186aef924c2c97bef4bf6e9f8b3f70b7755d7d2d2b18f9f06ccd3c0
4
- data.tar.gz: 7000fd522b81b78f65506e8b70014e88f823ba3cc48c747cef424e5c455556f0
3
+ metadata.gz: a121723b653ec85c4437590c2e90ba9fa11f0708eeb23ed1d8cd6475887d1fdb
4
+ data.tar.gz: b5b94b41028258e22c91693f22ec8a57417cd5fa4659870b8093216ed018aa2c
5
5
  SHA512:
6
- metadata.gz: e921e3975683643b56bef51ca69dee03a9f9880d1428bb245b24105cf9291b546b7f74c558fa74465b8e17606a8e5370d2b558ca490dd35dd826a47d29ebd3e2
7
- data.tar.gz: 8a6e220a4e7676bb79219f7e733a9723194f034afb66c761b1fd3644bf55e79ee5f7d27e9290b4319a232d4c305561e3bbfe38046d0246d184ff58b3f11e186e
6
+ metadata.gz: 9a58a4b36fa578d9be0617bbd828ef41a3e10d216168af595448e95973e567f1dc2f28c66a83fd72103d6d3cf9ba4bab20d6a46bd6a31c2294fa9c9940c85e87
7
+ data.tar.gz: 3a3f50a51bc1d05f7c13560f580ba6f04f78df34f224b18e53d6ceb478dffaeb9c9e8be3eb216e0ae69e562efeac863e23b0c1642de4d9a5c357f551c634d056
data/README.md CHANGED
@@ -145,6 +145,8 @@ Configure Sentrace in `config/initializers/sentrace.rb`:
145
145
  | `orphan_issue_retention_period` | `7.days` | Grace period before deleting `sentrace_issues` with zero remaining events. |
146
146
  | `retention_batch_size` | `1000` | Maximum rows deleted per SQL statement during retention cleanup. |
147
147
  | `max_envelope_size` | `1.megabyte` | Pre-parse compressed and decompressed envelope byte ceiling (`413` if exceeded). |
148
+ | `max_events_per_envelope` | `10` | Maximum error event items processed per HTTP envelope (`413` if exceeded). |
149
+ | `max_envelope_items` | `50` | Maximum total items (events + discarded telemetry) scanned per envelope (`413` if exceeded). |
148
150
  | `max_message_bytes` | `4.kilobytes` | Maximum UTF-8 byte size for normalized exception/event messages. |
149
151
  | `max_stack_frames` | `50` | Maximum stack frames retained per event (preserving innermost frames). |
150
152
  | `max_breadcrumbs` | `20` | Maximum trailing breadcrumbs retained per event. |
@@ -353,6 +355,14 @@ Reference benchmark on PostgreSQL 18 / Ruby 3.3.13 (`EVENTS=500`, 8 stack frames
353
355
 
354
356
  `Sentrace::Engine` also mounts `/internal/projects`, `/internal/issues`, `/internal/events`, and `/internal/stats` for administration and integration testing, protected by default with MCP bearer tokens or a host-provided `config.internal_authenticator` policy.
355
357
 
358
+ ### Built-in `/sentrace` Security Protections
359
+
360
+ - **Defensive response headers on all `/sentrace/*` routes**: `Cache-Control: no-store`, `X-Content-Type-Options: nosniff`, `X-Frame-Options: DENY`, `Content-Security-Policy: default-src 'none'; frame-ancestors 'none'`, and `Referrer-Policy: no-referrer`.
361
+ - **HTTPS + loopback peer verification on `/sentrace/mcp`**: Plain HTTP is permitted only when both `Host` and the peer IP (`remote_ip` / `REMOTE_ADDR`) are loopback (`127.0.0.0/8` / `::1`), blocking cleartext `Host: localhost` spoofing.
362
+ - **Cross-origin / DNS-rebinding & cookie-CSRF defense**: `/sentrace/mcp` and `/sentrace/internal/*` reject cross-origin `Origin`, `Origin: null`, and `Sec-Fetch-Site: cross-site` requests with `403 Forbidden`; state-changing cookie-authenticated requests on `/sentrace/internal/*` additionally require a verified same-origin `Origin`, `Content-Type: application/json`, or `X-CSRF-Token` / `X-Requested-With`.
363
+ - **Pre-body authentication & multi-item amplification caps**: `/sentrace/api/:project_id/envelope` validates `project_id` and header/query DSN credentials before reading `request.body`, enforces `max_envelope_size`, `max_events_per_envelope` (`10`), `max_envelope_items` (`50`), and JSON `max_nesting: 64`, and bounds breadcrumb timestamps and explicit fingerprints.
364
+ - **Recursive secret scrubbing & prompt-injection containment**: Redacts underscore and hyphenated credential keys (`api_key`, `api-key`, `private-key`, `secret-key`, bounded `ssn`/`otp`/`totp`), masks Bearer/Basic headers, URI passwords, JWTs, cloud/API tokens (`snt_mcp_`, `AKIA`, `ghp_`, `sk_live_`, `xoxb-`), PEM private keys, PostgreSQL dollar-quoted SQL literals, and Luhn PANs, and neutralizes embedded code fences inside MCP `investigate_issue` telemetry snapshots.
365
+
356
366
  ## Development Workflow (OpenSpec)
357
367
 
358
368
  This repository uses [OpenSpec](https://github.com/Fission-AI/OpenSpec) (`spec-driven` schema):
@@ -1,7 +1,7 @@
1
1
  (function (root) {
2
2
  "use strict";
3
3
  // --- dsn.ts ---
4
- const VERSION = "0.1.2";
4
+ const VERSION = "0.1.3";
5
5
  const SDK_NAME = "sentrace.javascript.browser";
6
6
  const SDK_CLIENT = `sentrace-js/${VERSION}`;
7
7
 
@@ -25,22 +25,37 @@
25
25
  const DEFAULT_SENSITIVE_QUERY_KEYS = [
26
26
  "password",
27
27
  "passwd",
28
+ "passcode",
28
29
  "secret",
30
+ "secret_key",
31
+ "secret-key",
32
+ "secretkey",
29
33
  "token",
30
34
  "access_token",
31
35
  "refresh_token",
32
36
  "id_token",
33
37
  "api_key",
38
+ "api-key",
34
39
  "apikey",
40
+ "private_key",
41
+ "private-key",
42
+ "privatekey",
43
+ "access_key",
44
+ "access-key",
45
+ "accesskey",
35
46
  "authorization",
36
47
  "cookie",
37
48
  "credit_card",
49
+ "credit-card",
38
50
  "card_number",
51
+ "card-number",
39
52
  "cvv",
40
53
  "cvc",
41
54
  "sentry_secret"
42
55
  ];
43
56
 
57
+ const SENSITIVE_BOUNDED_QUERY_KEY_REGEX = /(?:^|[_.-])(?:ssn|otp|totp)(?:$|[_.-])/;
58
+
44
59
  const AUTH_SESSION_EXACT_KEYS = [
45
60
  "session",
46
61
  "session_id",
@@ -52,6 +67,15 @@
52
67
  ];
53
68
 
54
69
  const AUTH_SESSION_KEY_REGEX = /^_(?:[a-z0-9_]+_)?session$|(?:^|[_.-])session[_.-]?token$/;
70
+ const SAFE_REQUEST_ID_REGEX = /^[A-Za-z0-9._:-]{1,128}$/;
71
+
72
+ function normalizeSafeRequestId(rawValue ) {
73
+ if (typeof rawValue !== "string") {
74
+ return undefined;
75
+ }
76
+ const trimmed = rawValue.trim();
77
+ return SAFE_REQUEST_ID_REGEX.test(trimmed) ? trimmed : undefined;
78
+ }
55
79
 
56
80
  function parseDsn(rawDsn ) {
57
81
  if (!rawDsn || typeof rawDsn !== "string") {
@@ -81,11 +105,25 @@
81
105
  }
82
106
 
83
107
  const projectId = segments.pop() ;
108
+ if (!/^[1-9]\d*$/.test(projectId)) {
109
+ return null;
110
+ }
111
+
112
+ let decodedPublicKey ;
113
+ try {
114
+ decodedPublicKey = decodeURIComponent(uri.username);
115
+ } catch (_err) {
116
+ return null;
117
+ }
118
+ if (!decodedPublicKey || /[\s\x00-\x1f\x7f]/.test(decodedPublicKey)) {
119
+ return null;
120
+ }
121
+
84
122
  const pathPrefix = segments.length > 0 ? `/${segments.join("/")}` : "";
85
123
 
86
124
  return {
87
125
  protocol,
88
- publicKey: decodeURIComponent(uri.username),
126
+ publicKey: decodedPublicKey,
89
127
  host: uri.hostname,
90
128
  port: uri.port || undefined,
91
129
  pathPrefix,
@@ -134,6 +172,9 @@
134
172
  if (AUTH_SESSION_EXACT_KEYS.indexOf(lower) !== -1 || AUTH_SESSION_KEY_REGEX.test(lower)) {
135
173
  return true;
136
174
  }
175
+ if (SENSITIVE_BOUNDED_QUERY_KEY_REGEX.test(lower)) {
176
+ return true;
177
+ }
137
178
  return redactPatterns.some((term) => lower.includes(term));
138
179
  };
139
180
 
@@ -385,6 +426,10 @@
385
426
  }
386
427
 
387
428
  // --- scope.ts ---
429
+ function isUnsafeScopeKey(key ) {
430
+ return key === "__proto__" || key === "constructor" || key === "prototype";
431
+ }
432
+
388
433
  class Scope {
389
434
 
390
435
  constructor() {
@@ -398,13 +443,16 @@
398
443
  }
399
444
 
400
445
  setTag(key , value ) {
401
- this.tags[String(key)] = String(value);
446
+ const normalizedKey = String(key);
447
+ if (!isUnsafeScopeKey(normalizedKey)) {
448
+ this.tags[normalizedKey] = String(value);
449
+ }
402
450
  return this;
403
451
  }
404
452
 
405
453
  setTags(tags ) {
406
454
  for (const [k, v] of Object.entries(tags)) {
407
- if (v !== undefined && v !== null) {
455
+ if (!isUnsafeScopeKey(k) && v !== undefined && v !== null) {
408
456
  this.tags[k] = String(v);
409
457
  }
410
458
  }
@@ -412,6 +460,9 @@
412
460
  }
413
461
 
414
462
  getTag(key ) {
463
+ if (isUnsafeScopeKey(key) || !Object.prototype.hasOwnProperty.call(this.tags, key)) {
464
+ return undefined;
465
+ }
415
466
  return this.tags[key];
416
467
  }
417
468
 
@@ -421,21 +472,36 @@
421
472
  }
422
473
 
423
474
  setContext(name , context ) {
475
+ if (isUnsafeScopeKey(name)) {
476
+ return this;
477
+ }
424
478
  if (context === null) {
425
479
  delete this.contexts[name];
426
480
  } else {
427
- this.contexts[name] = { ...context };
481
+ const cleaned = {};
482
+ for (const [k, v] of Object.entries(context)) {
483
+ if (!isUnsafeScopeKey(k)) {
484
+ cleaned[k] = v;
485
+ }
486
+ }
487
+ this.contexts[name] = cleaned;
428
488
  }
429
489
  return this;
430
490
  }
431
491
 
432
492
  setExtra(key , extra ) {
433
- this.extra[key] = extra;
493
+ if (!isUnsafeScopeKey(key)) {
494
+ this.extra[key] = extra;
495
+ }
434
496
  return this;
435
497
  }
436
498
 
437
499
  setExtras(extras ) {
438
- Object.assign(this.extra, extras);
500
+ for (const [k, v] of Object.entries(extras)) {
501
+ if (!isUnsafeScopeKey(k)) {
502
+ this.extra[k] = v;
503
+ }
504
+ }
439
505
  return this;
440
506
  }
441
507
 
@@ -560,11 +626,16 @@
560
626
 
561
627
  const mergedContexts = {};
562
628
  for (const [k, v] of Object.entries(this.contexts)) {
563
- mergedContexts[k] = { ...v };
629
+ if (!isUnsafeScopeKey(k)) {
630
+ mergedContexts[k] = { ...v };
631
+ }
564
632
  }
565
633
  if (event.contexts) {
566
634
  for (const [k, v] of Object.entries(event.contexts)) {
567
- mergedContexts[k] = { ...(mergedContexts[k] ? mergedContexts[k] : {}), ...v };
635
+ if (!isUnsafeScopeKey(k) && v && typeof v === "object") {
636
+ const existing = Object.prototype.hasOwnProperty.call(mergedContexts, k) ? mergedContexts[k] : undefined;
637
+ mergedContexts[k] = { ...(existing ? existing : {}), ...v };
638
+ }
568
639
  }
569
640
  }
570
641
  if (Object.keys(mergedContexts).length > 0) {
@@ -762,10 +833,10 @@
762
833
  const classPart =
763
834
  typeof el.className === "string" && el.className.trim()
764
835
  ? `.${el.className
765
- .trim()
766
- .split(/\s+/)
767
- .slice(0, 3)
768
- .join(".")}`
836
+ .trim()
837
+ .split(/\s+/)
838
+ .slice(0, 3)
839
+ .join(".")}`
769
840
  : "";
770
841
  const typeAttr = typeof el.getAttribute === "function" ? el.getAttribute("type") : null;
771
842
  const attrPart = typeAttr ? `[type="${typeAttr}"]` : "";
@@ -806,12 +877,12 @@
806
877
  const fallbackFrame =
807
878
  ev && ev.filename
808
879
  ? {
809
- filename: ev.filename,
810
- function: "?",
811
- lineno: typeof ev.lineno === "number" ? ev.lineno : undefined,
812
- colno: typeof ev.colno === "number" ? ev.colno : undefined,
813
- in_app: true
814
- }
880
+ filename: ev.filename,
881
+ function: "?",
882
+ lineno: typeof ev.lineno === "number" ? ev.lineno : undefined,
883
+ colno: typeof ev.colno === "number" ? ev.colno : undefined,
884
+ in_app: true
885
+ }
815
886
  : undefined;
816
887
 
817
888
  hooks.onError(err, {
@@ -970,8 +1041,7 @@
970
1041
  }
971
1042
  let requestId ;
972
1043
  if (response && response.headers && typeof response.headers.get === "function") {
973
- const rawHeader = response.headers.get("x-request-id");
974
- requestId = rawHeader !== null && rawHeader !== undefined ? rawHeader : undefined;
1044
+ requestId = normalizeSafeRequestId(response.headers.get("x-request-id"));
975
1045
  }
976
1046
  const status = response && typeof response.status === "number" ? response.status : undefined;
977
1047
 
@@ -1062,8 +1132,7 @@
1062
1132
  let requestId ;
1063
1133
  if (typeof getHeader === "function") {
1064
1134
  try {
1065
- const rawHeader = getHeader.call(this, "x-request-id");
1066
- requestId = rawHeader !== null && rawHeader !== undefined ? rawHeader : undefined;
1135
+ requestId = normalizeSafeRequestId(getHeader.call(this, "x-request-id"));
1067
1136
  } catch (_err) {
1068
1137
  requestId = undefined;
1069
1138
  }
@@ -1181,7 +1250,7 @@
1181
1250
  dsn: readMetaTagContent(doc, "sentrace-dsn"),
1182
1251
  environment: readMetaTagContent(doc, "sentrace-environment"),
1183
1252
  release: readMetaTagContent(doc, "sentrace-release"),
1184
- requestId: readMetaTagContent(doc, "sentrace-request-id")
1253
+ requestId: normalizeSafeRequestId(readMetaTagContent(doc, "sentrace-request-id"))
1185
1254
  };
1186
1255
  }
1187
1256
 
@@ -1197,7 +1266,7 @@
1197
1266
  const rawDoc = customDoc !== undefined && customDoc !== null ? customDoc : getGlobalObject().document;
1198
1267
  const doc = rawDoc ;
1199
1268
  if (!doc || typeof doc.addEventListener !== "function" || typeof doc.removeEventListener !== "function") {
1200
- return () => {};
1269
+ return () => { };
1201
1270
  }
1202
1271
 
1203
1272
  const onTurboVisit = (rawEvent ) => {
@@ -1238,7 +1307,9 @@
1238
1307
  const rawUrl =
1239
1308
  detail && detail.url ? String(detail.url) : detail && detail.request ? detail.request.url : undefined;
1240
1309
  const safeUrl = scrubUrl(rawUrl, hooks.redactFields, hooks.allowFields);
1241
- const requestId = readHeaderSafely(detail && detail.response ? detail.response.headers : undefined, "x-request-id");
1310
+ const requestId = normalizeSafeRequestId(
1311
+ readHeaderSafely(detail && detail.response ? detail.response.headers : undefined, "x-request-id")
1312
+ );
1242
1313
  if (requestId) {
1243
1314
  hooks.onRequestIdChange(requestId);
1244
1315
  }
@@ -1278,7 +1349,9 @@
1278
1349
  const response = ev && ev.detail ? ev.detail.response : undefined;
1279
1350
  const status = response ? response.status : undefined;
1280
1351
  const safeUrl = scrubUrl(response ? response.url : undefined, hooks.redactFields, hooks.allowFields);
1281
- const requestId = readHeaderSafely(response ? response.headers : undefined, "x-request-id");
1352
+ const requestId = normalizeSafeRequestId(
1353
+ readHeaderSafely(response ? response.headers : undefined, "x-request-id")
1354
+ );
1282
1355
  if (requestId) {
1283
1356
  hooks.onRequestIdChange(requestId);
1284
1357
  }
@@ -1330,7 +1403,7 @@
1330
1403
  captureFn
1331
1404
  ) {
1332
1405
  if (!application || typeof application !== "object") {
1333
- return () => {};
1406
+ return () => { };
1334
1407
  }
1335
1408
 
1336
1409
  let active = true;
@@ -1,6 +1,6 @@
1
1
  // Generated by scripts/build.mjs — zero-dependency ESM bundle
2
2
  // --- dsn.ts ---
3
- const VERSION = "0.1.2";
3
+ const VERSION = "0.1.3";
4
4
  const SDK_NAME = "sentrace.javascript.browser";
5
5
  const SDK_CLIENT = `sentrace-js/${VERSION}`;
6
6
 
@@ -24,22 +24,37 @@ function nullishCoalesce (value , fallback ) {
24
24
  const DEFAULT_SENSITIVE_QUERY_KEYS = [
25
25
  "password",
26
26
  "passwd",
27
+ "passcode",
27
28
  "secret",
29
+ "secret_key",
30
+ "secret-key",
31
+ "secretkey",
28
32
  "token",
29
33
  "access_token",
30
34
  "refresh_token",
31
35
  "id_token",
32
36
  "api_key",
37
+ "api-key",
33
38
  "apikey",
39
+ "private_key",
40
+ "private-key",
41
+ "privatekey",
42
+ "access_key",
43
+ "access-key",
44
+ "accesskey",
34
45
  "authorization",
35
46
  "cookie",
36
47
  "credit_card",
48
+ "credit-card",
37
49
  "card_number",
50
+ "card-number",
38
51
  "cvv",
39
52
  "cvc",
40
53
  "sentry_secret"
41
54
  ];
42
55
 
56
+ const SENSITIVE_BOUNDED_QUERY_KEY_REGEX = /(?:^|[_.-])(?:ssn|otp|totp)(?:$|[_.-])/;
57
+
43
58
  const AUTH_SESSION_EXACT_KEYS = [
44
59
  "session",
45
60
  "session_id",
@@ -51,6 +66,15 @@ const AUTH_SESSION_EXACT_KEYS = [
51
66
  ];
52
67
 
53
68
  const AUTH_SESSION_KEY_REGEX = /^_(?:[a-z0-9_]+_)?session$|(?:^|[_.-])session[_.-]?token$/;
69
+ const SAFE_REQUEST_ID_REGEX = /^[A-Za-z0-9._:-]{1,128}$/;
70
+
71
+ function normalizeSafeRequestId(rawValue ) {
72
+ if (typeof rawValue !== "string") {
73
+ return undefined;
74
+ }
75
+ const trimmed = rawValue.trim();
76
+ return SAFE_REQUEST_ID_REGEX.test(trimmed) ? trimmed : undefined;
77
+ }
54
78
 
55
79
  function parseDsn(rawDsn ) {
56
80
  if (!rawDsn || typeof rawDsn !== "string") {
@@ -80,11 +104,25 @@ function parseDsn(rawDsn ) {
80
104
  }
81
105
 
82
106
  const projectId = segments.pop() ;
107
+ if (!/^[1-9]\d*$/.test(projectId)) {
108
+ return null;
109
+ }
110
+
111
+ let decodedPublicKey ;
112
+ try {
113
+ decodedPublicKey = decodeURIComponent(uri.username);
114
+ } catch (_err) {
115
+ return null;
116
+ }
117
+ if (!decodedPublicKey || /[\s\x00-\x1f\x7f]/.test(decodedPublicKey)) {
118
+ return null;
119
+ }
120
+
83
121
  const pathPrefix = segments.length > 0 ? `/${segments.join("/")}` : "";
84
122
 
85
123
  return {
86
124
  protocol,
87
- publicKey: decodeURIComponent(uri.username),
125
+ publicKey: decodedPublicKey,
88
126
  host: uri.hostname,
89
127
  port: uri.port || undefined,
90
128
  pathPrefix,
@@ -133,6 +171,9 @@ function scrubUrl(
133
171
  if (AUTH_SESSION_EXACT_KEYS.indexOf(lower) !== -1 || AUTH_SESSION_KEY_REGEX.test(lower)) {
134
172
  return true;
135
173
  }
174
+ if (SENSITIVE_BOUNDED_QUERY_KEY_REGEX.test(lower)) {
175
+ return true;
176
+ }
136
177
  return redactPatterns.some((term) => lower.includes(term));
137
178
  };
138
179
 
@@ -384,6 +425,10 @@ function extractExceptionChain(
384
425
  }
385
426
 
386
427
  // --- scope.ts ---
428
+ function isUnsafeScopeKey(key ) {
429
+ return key === "__proto__" || key === "constructor" || key === "prototype";
430
+ }
431
+
387
432
  class Scope {
388
433
 
389
434
  constructor() {
@@ -397,13 +442,16 @@ class Scope {
397
442
  }
398
443
 
399
444
  setTag(key , value ) {
400
- this.tags[String(key)] = String(value);
445
+ const normalizedKey = String(key);
446
+ if (!isUnsafeScopeKey(normalizedKey)) {
447
+ this.tags[normalizedKey] = String(value);
448
+ }
401
449
  return this;
402
450
  }
403
451
 
404
452
  setTags(tags ) {
405
453
  for (const [k, v] of Object.entries(tags)) {
406
- if (v !== undefined && v !== null) {
454
+ if (!isUnsafeScopeKey(k) && v !== undefined && v !== null) {
407
455
  this.tags[k] = String(v);
408
456
  }
409
457
  }
@@ -411,6 +459,9 @@ class Scope {
411
459
  }
412
460
 
413
461
  getTag(key ) {
462
+ if (isUnsafeScopeKey(key) || !Object.prototype.hasOwnProperty.call(this.tags, key)) {
463
+ return undefined;
464
+ }
414
465
  return this.tags[key];
415
466
  }
416
467
 
@@ -420,21 +471,36 @@ class Scope {
420
471
  }
421
472
 
422
473
  setContext(name , context ) {
474
+ if (isUnsafeScopeKey(name)) {
475
+ return this;
476
+ }
423
477
  if (context === null) {
424
478
  delete this.contexts[name];
425
479
  } else {
426
- this.contexts[name] = { ...context };
480
+ const cleaned = {};
481
+ for (const [k, v] of Object.entries(context)) {
482
+ if (!isUnsafeScopeKey(k)) {
483
+ cleaned[k] = v;
484
+ }
485
+ }
486
+ this.contexts[name] = cleaned;
427
487
  }
428
488
  return this;
429
489
  }
430
490
 
431
491
  setExtra(key , extra ) {
432
- this.extra[key] = extra;
492
+ if (!isUnsafeScopeKey(key)) {
493
+ this.extra[key] = extra;
494
+ }
433
495
  return this;
434
496
  }
435
497
 
436
498
  setExtras(extras ) {
437
- Object.assign(this.extra, extras);
499
+ for (const [k, v] of Object.entries(extras)) {
500
+ if (!isUnsafeScopeKey(k)) {
501
+ this.extra[k] = v;
502
+ }
503
+ }
438
504
  return this;
439
505
  }
440
506
 
@@ -559,11 +625,16 @@ class Scope {
559
625
 
560
626
  const mergedContexts = {};
561
627
  for (const [k, v] of Object.entries(this.contexts)) {
562
- mergedContexts[k] = { ...v };
628
+ if (!isUnsafeScopeKey(k)) {
629
+ mergedContexts[k] = { ...v };
630
+ }
563
631
  }
564
632
  if (event.contexts) {
565
633
  for (const [k, v] of Object.entries(event.contexts)) {
566
- mergedContexts[k] = { ...(mergedContexts[k] ? mergedContexts[k] : {}), ...v };
634
+ if (!isUnsafeScopeKey(k) && v && typeof v === "object") {
635
+ const existing = Object.prototype.hasOwnProperty.call(mergedContexts, k) ? mergedContexts[k] : undefined;
636
+ mergedContexts[k] = { ...(existing ? existing : {}), ...v };
637
+ }
567
638
  }
568
639
  }
569
640
  if (Object.keys(mergedContexts).length > 0) {
@@ -761,10 +832,10 @@ function describeElement(target ) {
761
832
  const classPart =
762
833
  typeof el.className === "string" && el.className.trim()
763
834
  ? `.${el.className
764
- .trim()
765
- .split(/\s+/)
766
- .slice(0, 3)
767
- .join(".")}`
835
+ .trim()
836
+ .split(/\s+/)
837
+ .slice(0, 3)
838
+ .join(".")}`
768
839
  : "";
769
840
  const typeAttr = typeof el.getAttribute === "function" ? el.getAttribute("type") : null;
770
841
  const attrPart = typeAttr ? `[type="${typeAttr}"]` : "";
@@ -805,12 +876,12 @@ function installBrowserInstrumentation(hooks )
805
876
  const fallbackFrame =
806
877
  ev && ev.filename
807
878
  ? {
808
- filename: ev.filename,
809
- function: "?",
810
- lineno: typeof ev.lineno === "number" ? ev.lineno : undefined,
811
- colno: typeof ev.colno === "number" ? ev.colno : undefined,
812
- in_app: true
813
- }
879
+ filename: ev.filename,
880
+ function: "?",
881
+ lineno: typeof ev.lineno === "number" ? ev.lineno : undefined,
882
+ colno: typeof ev.colno === "number" ? ev.colno : undefined,
883
+ in_app: true
884
+ }
814
885
  : undefined;
815
886
 
816
887
  hooks.onError(err, {
@@ -969,8 +1040,7 @@ function installBrowserInstrumentation(hooks )
969
1040
  }
970
1041
  let requestId ;
971
1042
  if (response && response.headers && typeof response.headers.get === "function") {
972
- const rawHeader = response.headers.get("x-request-id");
973
- requestId = rawHeader !== null && rawHeader !== undefined ? rawHeader : undefined;
1043
+ requestId = normalizeSafeRequestId(response.headers.get("x-request-id"));
974
1044
  }
975
1045
  const status = response && typeof response.status === "number" ? response.status : undefined;
976
1046
 
@@ -1061,8 +1131,7 @@ function installBrowserInstrumentation(hooks )
1061
1131
  let requestId ;
1062
1132
  if (typeof getHeader === "function") {
1063
1133
  try {
1064
- const rawHeader = getHeader.call(this, "x-request-id");
1065
- requestId = rawHeader !== null && rawHeader !== undefined ? rawHeader : undefined;
1134
+ requestId = normalizeSafeRequestId(getHeader.call(this, "x-request-id"));
1066
1135
  } catch (_err) {
1067
1136
  requestId = undefined;
1068
1137
  }
@@ -1180,7 +1249,7 @@ function readRailsMeta(customDoc ) {
1180
1249
  dsn: readMetaTagContent(doc, "sentrace-dsn"),
1181
1250
  environment: readMetaTagContent(doc, "sentrace-environment"),
1182
1251
  release: readMetaTagContent(doc, "sentrace-release"),
1183
- requestId: readMetaTagContent(doc, "sentrace-request-id")
1252
+ requestId: normalizeSafeRequestId(readMetaTagContent(doc, "sentrace-request-id"))
1184
1253
  };
1185
1254
  }
1186
1255
 
@@ -1196,7 +1265,7 @@ function installRailsTurboInstrumentation(
1196
1265
  const rawDoc = customDoc !== undefined && customDoc !== null ? customDoc : getGlobalObject().document;
1197
1266
  const doc = rawDoc ;
1198
1267
  if (!doc || typeof doc.addEventListener !== "function" || typeof doc.removeEventListener !== "function") {
1199
- return () => {};
1268
+ return () => { };
1200
1269
  }
1201
1270
 
1202
1271
  const onTurboVisit = (rawEvent ) => {
@@ -1237,7 +1306,9 @@ function installRailsTurboInstrumentation(
1237
1306
  const rawUrl =
1238
1307
  detail && detail.url ? String(detail.url) : detail && detail.request ? detail.request.url : undefined;
1239
1308
  const safeUrl = scrubUrl(rawUrl, hooks.redactFields, hooks.allowFields);
1240
- const requestId = readHeaderSafely(detail && detail.response ? detail.response.headers : undefined, "x-request-id");
1309
+ const requestId = normalizeSafeRequestId(
1310
+ readHeaderSafely(detail && detail.response ? detail.response.headers : undefined, "x-request-id")
1311
+ );
1241
1312
  if (requestId) {
1242
1313
  hooks.onRequestIdChange(requestId);
1243
1314
  }
@@ -1277,7 +1348,9 @@ function installRailsTurboInstrumentation(
1277
1348
  const response = ev && ev.detail ? ev.detail.response : undefined;
1278
1349
  const status = response ? response.status : undefined;
1279
1350
  const safeUrl = scrubUrl(response ? response.url : undefined, hooks.redactFields, hooks.allowFields);
1280
- const requestId = readHeaderSafely(response ? response.headers : undefined, "x-request-id");
1351
+ const requestId = normalizeSafeRequestId(
1352
+ readHeaderSafely(response ? response.headers : undefined, "x-request-id")
1353
+ );
1281
1354
  if (requestId) {
1282
1355
  hooks.onRequestIdChange(requestId);
1283
1356
  }
@@ -1329,7 +1402,7 @@ function bindStimulusErrorHandler(
1329
1402
  captureFn
1330
1403
  ) {
1331
1404
  if (!application || typeof application !== "object") {
1332
- return () => {};
1405
+ return () => { };
1333
1406
  }
1334
1407
 
1335
1408
  let active = true;
@@ -1,11 +1,11 @@
1
1
  module Sentrace
2
2
  class ApplicationController < ActionController::API
3
- before_action :set_no_store_cache_control
4
- after_action :set_no_store_cache_control
3
+ before_action :apply_security_headers
4
+ after_action :apply_security_headers
5
5
 
6
6
  private
7
- def set_no_store_cache_control
8
- response.set_header("Cache-Control", "no-store")
7
+ def apply_security_headers
8
+ RequestSecurity.apply_security_headers!(response)
9
9
  end
10
10
  end
11
11
  end