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
@@ -6,10 +6,15 @@ module Sentrace
6
6
  ParsedEnvelope = Data.define(:headers, :events, :discarded_item_types)
7
7
 
8
8
  MAX_HEADER_LINE_BYTES = 16 * 1024
9
+ MAX_JSON_NESTING = 64
9
10
 
10
11
  class << self
11
- def parse(raw_body)
12
- new.parse(raw_body)
12
+ def parse(
13
+ raw_body,
14
+ max_items: Configuration::DEFAULT_MAX_ENVELOPE_ITEMS,
15
+ max_events: Configuration::DEFAULT_MAX_EVENTS_PER_ENVELOPE
16
+ )
17
+ new(max_items: max_items, max_events: max_events).parse(raw_body)
13
18
  end
14
19
 
15
20
  def extract_header_dsn(raw_body)
@@ -20,13 +25,24 @@ module Sentrace
20
25
  first_line = newline_idx ? binary.byteslice(0, newline_idx) : binary
21
26
  return nil if first_line.bytesize > MAX_HEADER_LINE_BYTES
22
27
 
23
- parsed = JSON.parse(first_line.force_encoding(Encoding::UTF_8))
24
- parsed.is_a?(Hash) ? parsed["dsn"] : nil
25
- rescue JSON::ParserError, EncodingError
28
+ utf8 = first_line.force_encoding(Encoding::UTF_8)
29
+ return nil unless utf8.valid_encoding?
30
+
31
+ parsed = JSON.parse(utf8, max_nesting: MAX_JSON_NESTING)
32
+ parsed.is_a?(Hash) && parsed["dsn"].is_a?(String) ? parsed["dsn"] : nil
33
+ rescue JSON::ParserError, JSON::NestingError, EncodingError
26
34
  nil
27
35
  end
28
36
  end
29
37
 
38
+ def initialize(
39
+ max_items: Configuration::DEFAULT_MAX_ENVELOPE_ITEMS,
40
+ max_events: Configuration::DEFAULT_MAX_EVENTS_PER_ENVELOPE
41
+ )
42
+ @max_items = max_items
43
+ @max_events = max_events
44
+ end
45
+
30
46
  def parse(raw_body)
31
47
  if raw_body.nil? || raw_body.bytesize.zero?
32
48
  raise EnvelopeParseError, "Envelope body cannot be empty"
@@ -44,6 +60,7 @@ module Sentrace
44
60
  envelope_headers = parse_json_object(header_line, "envelope header")
45
61
  events = []
46
62
  discarded = []
63
+ item_count = 0
47
64
 
48
65
  while cursor < total
49
66
  # Skip trailing newline(s) at the end of an envelope
@@ -58,6 +75,11 @@ module Sentrace
58
75
  item_header_line, cursor = read_line(buffer, cursor, total)
59
76
  break if item_header_line.nil? || item_header_line.empty?
60
77
 
78
+ item_count += 1
79
+ if @max_items && item_count > @max_items
80
+ raise PayloadTooLargeError, "Envelope exceeds max_envelope_items (#{@max_items})"
81
+ end
82
+
61
83
  item_header = parse_json_object(item_header_line, "envelope item header")
62
84
  item_type = (item_header["type"] || "event").to_s
63
85
 
@@ -68,6 +90,9 @@ module Sentrace
68
90
  if event_hash["type"].to_s == "transaction"
69
91
  discarded << "transaction"
70
92
  else
93
+ if @max_events && events.size >= @max_events
94
+ raise PayloadTooLargeError, "Envelope exceeds max_events_per_envelope (#{@max_events})"
95
+ end
71
96
  events << event_hash
72
97
  end
73
98
  else
@@ -124,18 +149,22 @@ module Sentrace
124
149
  end
125
150
 
126
151
  def parse_json_object(raw_bytes, label)
152
+ if label != "event payload" && raw_bytes.bytesize > MAX_HEADER_LINE_BYTES
153
+ raise EnvelopeParseError, "Oversized #{label}"
154
+ end
155
+
127
156
  utf8 = raw_bytes.dup.force_encoding(Encoding::UTF_8)
128
157
  unless utf8.valid_encoding?
129
158
  raise EnvelopeParseError, "Invalid UTF-8 encoding in #{label}"
130
159
  end
131
160
 
132
- parsed = JSON.parse(utf8)
161
+ parsed = JSON.parse(utf8, max_nesting: MAX_JSON_NESTING)
133
162
  unless parsed.is_a?(Hash)
134
163
  raise EnvelopeParseError, "Expected JSON object for #{label}"
135
164
  end
136
165
 
137
166
  parsed
138
- rescue JSON::ParserError
167
+ rescue JSON::ParserError, JSON::NestingError
139
168
  raise EnvelopeParseError, "Invalid JSON in #{label}"
140
169
  end
141
170
  end
@@ -25,6 +25,8 @@ module Sentrace
25
25
  EVENT_ID_HEX = /\A[0-9a-f]{32}\z/
26
26
  SAFE_REQUEST_ID_REGEX = /\A[A-Za-z0-9._:-]{1,128}\z/
27
27
  ISSUE_MESSAGE_MAX_CHARS = 1024
28
+ MAX_FINGERPRINT_ITEMS = 32
29
+ MAX_FINGERPRINT_PART_BYTES = 256
28
30
 
29
31
  LEVEL_MAP = {
30
32
  "debug" => :debug,
@@ -232,7 +234,7 @@ module Sentrace
232
234
 
233
235
  items.select { |b| b.is_a?(Hash) }.last(@config.max_breadcrumbs).map do |crumb|
234
236
  entry = {
235
- "timestamp" => crumb["timestamp"]&.to_s,
237
+ "timestamp" => normalize_breadcrumb_timestamp(crumb["timestamp"]),
236
238
  "type" => truncate_chars(crumb["type"], 64),
237
239
  "category" => truncate_chars(crumb["category"], 128),
238
240
  "level" => truncate_chars(crumb["level"], 32),
@@ -343,12 +345,23 @@ module Sentrace
343
345
  end
344
346
  end
345
347
 
348
+ def normalize_breadcrumb_timestamp(raw_timestamp)
349
+ case raw_timestamp
350
+ when Numeric
351
+ truncate_chars(raw_timestamp.to_s, 64)
352
+ when String
353
+ truncate_chars(raw_timestamp, 64)
354
+ end
355
+ end
356
+
346
357
  def extract_fingerprint(raw_fingerprint)
347
358
  return nil unless raw_fingerprint.is_a?(Array)
348
359
 
349
- cleaned = raw_fingerprint.filter_map do |part|
350
- str = part.to_s.strip
351
- str unless str.empty?
360
+ cleaned = raw_fingerprint.first(MAX_FINGERPRINT_ITEMS).filter_map do |part|
361
+ next unless part.is_a?(String) || part.is_a?(Symbol) || part.is_a?(Numeric)
362
+
363
+ str = self.class.truncate_utf8_bytes(part.to_s.strip, MAX_FINGERPRINT_PART_BYTES)
364
+ str unless str.nil? || str.empty?
352
365
  end
353
366
 
354
367
  cleaned.empty? ? nil : cleaned
@@ -23,7 +23,11 @@ module Sentrace
23
23
  end
24
24
 
25
25
  def process_raw_envelope(raw_body, project:, received_at: Time.current)
26
- parsed = EnvelopeParser.parse(raw_body)
26
+ parsed = EnvelopeParser.parse(
27
+ raw_body,
28
+ max_items: @config.max_envelope_items,
29
+ max_events: @config.max_events_per_envelope
30
+ )
27
31
  accepted_ids = []
28
32
  duplicate_ids = []
29
33
 
@@ -1,11 +1,13 @@
1
1
  require "json"
2
2
  require "openssl"
3
+ require "securerandom"
3
4
  require "uri"
4
5
 
5
6
  module Sentrace
6
7
  module Ingestion
7
8
  class Sanitizer
8
9
  REDACTED = "[REDACTED]"
10
+ FALLBACK_SECRET_MUTEX = Mutex.new
9
11
 
10
12
  FORBIDDEN_CONTEXT_KEYS = %w[
11
13
  env
@@ -20,22 +22,43 @@ module Sentrace
20
22
  ALWAYS_REDACT_PATTERNS = %w[
21
23
  password
22
24
  passwd
25
+ passcode
23
26
  secret
27
+ secret_key
28
+ secret-key
29
+ secretkey
24
30
  token
25
31
  authorization
26
32
  cookie
27
33
  credit_card
34
+ credit-card
28
35
  card_number
36
+ card-number
29
37
  cvv
30
38
  cvc
31
39
  api_key
40
+ api-key
32
41
  apikey
42
+ private_key
43
+ private-key
44
+ privatekey
45
+ access_key
46
+ access-key
47
+ accesskey
33
48
  ip_address
49
+ ip-address
34
50
  remote_addr
51
+ remote-addr
35
52
  remote_ip
53
+ remote-ip
36
54
  client_ip
55
+ client-ip
56
+ x_forwarded_for
57
+ x-forwarded-for
37
58
  ].freeze
38
59
 
60
+ SENSITIVE_BOUNDED_KEY_REGEX = /(?:^|[_.-])(?:ssn|otp|totp)(?:$|[_.-])/
61
+
39
62
  AUTH_SESSION_EXACT_KEYS = %w[
40
63
  session
41
64
  session_id
@@ -48,29 +71,17 @@ module Sentrace
48
71
 
49
72
  AUTH_SESSION_KEY_REGEX = /\A_(?:[a-z0-9_]+_)?session\z|(?:^|[_.-])session[_.-]?token\z/
50
73
 
51
- FORBIDDEN_USER_FIELD_PATTERNS = %w[
52
- password
53
- passwd
54
- secret
55
- token
56
- authorization
57
- cookie
58
- credit_card
59
- card_number
60
- cvv
61
- cvc
62
- api_key
63
- apikey
64
- ip_address
65
- remote_addr
66
- remote_ip
67
- client_ip
68
- ].freeze
74
+ FORBIDDEN_USER_FIELD_PATTERNS = ALWAYS_REDACT_PATTERNS
69
75
 
70
76
  MAX_RETAINED_USER_FIELDS = 16
71
77
  MAX_USER_FIELD_BYTES = 256
72
78
 
79
+ PEM_PRIVATE_KEY_REGEX = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 ]*PRIVATE KEY-----/
73
80
  BEARER_HEADER_REGEX = /\bBearer\s+[A-Za-z0-9\-._~+\/]+=*/i
81
+ BASIC_AUTH_HEADER_REGEX = /\bBasic\s+[A-Za-z0-9+\/]{8,}={0,2}/i
82
+ URI_USERINFO_REGEX = %r{\b([a-z][a-z0-9+.-]*://)[^/\s@]*:[^/\s@]*(@)}i
83
+ RAW_JWT_REGEX = /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/
84
+ KNOWN_SECRET_TOKEN_REGEX = /\b(?:snt_mcp_[A-Za-z0-9_-]{16,}|(?:AKIA|ASIA)[0-9A-Z]{16}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{16,}|xox[baprs]-[A-Za-z0-9-]{16,})\b/
74
85
  PAN_CANDIDATE_REGEX = /\b(?:\d[ -]*?){13,19}\b/
75
86
 
76
87
  class << self
@@ -81,6 +92,12 @@ module Sentrace
81
92
  def sanitize_url(raw_url, raw_query_string = nil, config: Sentrace.configuration)
82
93
  new(config: config).sanitize_url(raw_url, raw_query_string)
83
94
  end
95
+
96
+ def ephemeral_fallback_secret
97
+ FALLBACK_SECRET_MUTEX.synchronize do
98
+ @ephemeral_fallback_secret ||= SecureRandom.hex(32)
99
+ end
100
+ end
84
101
  end
85
102
 
86
103
  def initialize(config: Sentrace.configuration, secret_base: nil)
@@ -215,6 +232,7 @@ module Sentrace
215
232
  downcased = field_name.to_s.strip.downcase
216
233
  return true if downcased.empty? || downcased == "id_hash"
217
234
  return true if FORBIDDEN_CONTEXT_KEYS.include?(downcased) || auth_session_key?(downcased)
235
+ return true if SENSITIVE_BOUNDED_KEY_REGEX.match?(downcased)
218
236
 
219
237
  FORBIDDEN_USER_FIELD_PATTERNS.any? { |pat| downcased.include?(pat) }
220
238
  end
@@ -239,7 +257,7 @@ module Sentrace
239
257
  if defined?(Rails) && Rails.application && Rails.application.respond_to?(:secret_key_base) && Rails.application.secret_key_base.present?
240
258
  Rails.application.secret_key_base
241
259
  else
242
- "sentrace-default-local-secret"
260
+ self.class.ephemeral_fallback_secret
243
261
  end
244
262
  end
245
263
 
@@ -303,6 +321,7 @@ module Sentrace
303
321
  downcased = key.to_s.strip.downcase
304
322
  return false if @allow_fields.key?(downcased)
305
323
  return true if auth_session_key?(downcased)
324
+ return true if SENSITIVE_BOUNDED_KEY_REGEX.match?(downcased)
306
325
 
307
326
  @redact_terms.any? { |term| downcased.include?(term) }
308
327
  end
@@ -314,7 +333,14 @@ module Sentrace
314
333
  def scrub_text(text)
315
334
  return text unless text.is_a?(String)
316
335
 
317
- cleaned = text.gsub(BEARER_HEADER_REGEX, "Bearer #{REDACTED}")
336
+ cleaned = text
337
+ .gsub(PEM_PRIVATE_KEY_REGEX, REDACTED)
338
+ .gsub(BEARER_HEADER_REGEX, "Bearer #{REDACTED}")
339
+ .gsub(BASIC_AUTH_HEADER_REGEX, "Basic #{REDACTED}")
340
+ .gsub(URI_USERINFO_REGEX, "\\1#{REDACTED}\\2")
341
+ .gsub(RAW_JWT_REGEX, REDACTED)
342
+ .gsub(KNOWN_SECRET_TOKEN_REGEX, REDACTED)
343
+
318
344
  cleaned.gsub(PAN_CANDIDATE_REGEX) do |match|
319
345
  digits = match.gsub(/\D/, "")
320
346
  luhn_valid?(digits) ? REDACTED : match
@@ -374,17 +400,28 @@ module Sentrace
374
400
  bounded["user"] = bounded_user.empty? ? nil : bounded_user
375
401
  end
376
402
 
377
- if bounded.dig("exception", "frames").is_a?(Array)
378
- bounded["exception"] = bounded["exception"].merge(
379
- "frames" => bounded.dig("exception", "frames").last(@config.max_stack_frames)
380
- )
403
+ if bounded["exception"].is_a?(Hash)
404
+ exc = bounded["exception"].dup
405
+ if exc["value"].is_a?(String)
406
+ exc["value"] = EventNormalizer.truncate_utf8_bytes(exc["value"], @config.max_message_bytes)
407
+ end
408
+ if exc["frames"].is_a?(Array)
409
+ exc["frames"] = exc["frames"].last(@config.max_stack_frames)
410
+ end
411
+ bounded["exception"] = exc
381
412
  end
382
413
 
383
414
  if bounded["contexts"].is_a?(Hash)
384
415
  kept = {}
385
416
  bounded["contexts"].each do |k, v|
386
417
  break if kept.size >= @config.max_context_entries
387
- candidate = kept.merge(k.to_s => v)
418
+
419
+ key_str = EventNormalizer.truncate_utf8_bytes(k.to_s, 64)
420
+ next if key_str.nil? || key_str.empty?
421
+ next if FORBIDDEN_CONTEXT_KEYS.include?(key_str.strip.downcase)
422
+ next unless v.is_a?(Hash)
423
+
424
+ candidate = kept.merge(key_str => bound_post_sanitizer_value(v, depth: 0))
388
425
  kept = candidate if JSON.generate(candidate).bytesize <= @config.max_context_bytes
389
426
  end
390
427
  bounded["contexts"] = kept
@@ -393,6 +430,23 @@ module Sentrace
393
430
  bounded
394
431
  end
395
432
 
433
+ def bound_post_sanitizer_value(value, depth:)
434
+ return "[TRUNCATED]" if depth > 3
435
+
436
+ case value
437
+ when Hash
438
+ value.first(16).to_h do |k, v|
439
+ [ EventNormalizer.truncate_utf8_bytes(k.to_s, 64), bound_post_sanitizer_value(v, depth: depth + 1) ]
440
+ end
441
+ when Array
442
+ value.first(16).map { |v| bound_post_sanitizer_value(v, depth: depth + 1) }
443
+ when Numeric, TrueClass, FalseClass, NilClass
444
+ value
445
+ else
446
+ EventNormalizer.truncate_utf8_bytes(value.to_s, 512)
447
+ end
448
+ end
449
+
396
450
  def deep_dup(obj)
397
451
  case obj
398
452
  when Hash
@@ -5,13 +5,15 @@ module Sentrace
5
5
  module Issues
6
6
  class Fingerprint
7
7
  DEFAULT_PLACEHOLDER = "{{ default }}"
8
+ MAX_FILENAME_INPUT_BYTES = 2048
9
+ MAX_MESSAGE_INPUT_BYTES = 8192
8
10
 
9
11
  GEM_PATH_REGEX = %r{(?:\A|/)gems/([^/]+?)(?:-\d[\w.-]*)?/(.+)\z}
10
12
  RELEASE_PATH_REGEX = %r{(?:\A|/)(?:releases/[^/]+|current)/(.+)\z}
11
13
  APP_ROOT_SUBDIR_REGEX = %r{(?:\A|/)((?:app|config|db|lib)/.+)\z}
12
14
  HTTP_ORIGIN_REGEX = %r{\A(?:https?|file)://[^/]+}i
13
15
  BUNDLER_SCHEME_REGEX = %r{\A(?:webpack(?:-internal)?|vite|turbopack):///*}i
14
- JS_ASSET_DIGEST_REGEX = /[-.](?:[0-9a-f]{8,64}|(?=[A-Za-z0-9]*\d)(?=[A-Za-z0-9]*[A-Za-z])[A-Za-z0-9]{8,32})(?:\.digested)?(?=\.(?:m?js|cjs|jsx|tsx?|map)\z)/i
16
+ JS_ASSET_DIGEST_REGEX = /[-.](?:[0-9a-f]{8,64}|(?=[A-Za-z0-9]{0,31}\d)(?=[A-Za-z0-9]{0,31}[A-Za-z])[A-Za-z0-9]{8,32})(?:\.digested)?(?=\.(?:m?js|cjs|jsx|tsx?|map)\z)/i
15
17
  ANONYMOUS_FUNCTION_NAMES = [ "?", "<anonymous>" ].freeze
16
18
 
17
19
  OBJECT_ADDRESS_REGEX = /0x[0-9a-f]{6,}/i
@@ -30,7 +32,7 @@ module Sentrace
30
32
  def normalize_filename(raw_filename)
31
33
  return "" if raw_filename.nil?
32
34
 
33
- cleaned = raw_filename.to_s.strip
35
+ cleaned = Ingestion::EventNormalizer.truncate_utf8_bytes(raw_filename, MAX_FILENAME_INPUT_BYTES).to_s.strip
34
36
  return "" if cleaned.empty?
35
37
 
36
38
  cleaned = cleaned
@@ -57,7 +59,7 @@ module Sentrace
57
59
  def normalize_message(raw_message)
58
60
  return "" if raw_message.nil?
59
61
 
60
- raw_message.to_s
62
+ Ingestion::EventNormalizer.truncate_utf8_bytes(raw_message, MAX_MESSAGE_INPUT_BYTES).to_s
61
63
  .gsub(ISO_TIMESTAMP_REGEX, "<timestamp>")
62
64
  .gsub(UUID_REGEX, "<uuid>")
63
65
  .gsub(OBJECT_ADDRESS_REGEX, "<hex>")
@@ -96,9 +98,14 @@ module Sentrace
96
98
 
97
99
  return nil unless fp.is_a?(Array)
98
100
 
99
- cleaned = fp.filter_map do |item|
100
- str = item.to_s.strip
101
- str unless str.empty?
101
+ cleaned = fp.first(Ingestion::EventNormalizer::MAX_FINGERPRINT_ITEMS).filter_map do |item|
102
+ next unless item.is_a?(String) || item.is_a?(Symbol) || item.is_a?(Numeric)
103
+
104
+ str = Ingestion::EventNormalizer.truncate_utf8_bytes(
105
+ item.to_s.strip,
106
+ Ingestion::EventNormalizer::MAX_FINGERPRINT_PART_BYTES
107
+ )
108
+ str unless str.nil? || str.empty?
102
109
  end
103
110
  cleaned.empty? ? nil : cleaned
104
111
  end
@@ -254,10 +254,11 @@ module Sentrace
254
254
  end
255
255
 
256
256
  def build_environment_entry(env_def, in_worker_thread:)
257
+ safe_url = env_def.url ? Ingestion::Sanitizer.sanitize_url(env_def.url) : nil
257
258
  {
258
259
  "name" => env_def.name.to_s,
259
260
  "transport" => env_def.transport.to_s,
260
- "url" => env_def.url,
261
+ "url" => safe_url,
261
262
  "timeout" => env_def.timeout,
262
263
  "status" => environment_status(env_def, in_worker_thread: in_worker_thread)
263
264
  }.compact
@@ -7,6 +7,7 @@ module Sentrace
7
7
  class LocalBackend
8
8
  DEFAULT_LIMIT = 25
9
9
  MAX_LIMIT = 100
10
+ MAX_CURSOR_BYTES = 512
10
11
  RECENT_EVENTS_PREVIEW_LIMIT = 5
11
12
 
12
13
  TREND_BUCKET_SQL = {
@@ -430,13 +431,18 @@ module Sentrace
430
431
  def decode_cursor(raw_cursor)
431
432
  return nil if raw_cursor.nil? || raw_cursor.to_s.strip.empty?
432
433
 
433
- decoded = JSON.parse(Base64.urlsafe_decode64(raw_cursor.to_s.strip))
434
- unless decoded.is_a?(Hash) && decoded["ts"].is_a?(String) && decoded["id"].is_a?(Integer)
434
+ stripped = raw_cursor.to_s.strip
435
+ if stripped.bytesize > MAX_CURSOR_BYTES
436
+ raise McpInvalidParamsError, "Malformed pagination cursor"
437
+ end
438
+
439
+ decoded = JSON.parse(Base64.urlsafe_decode64(stripped), max_nesting: 8)
440
+ unless decoded.is_a?(Hash) && decoded["ts"].is_a?(String) && decoded["id"].is_a?(Integer) && decoded["id"].positive?
435
441
  raise McpInvalidParamsError, "Invalid pagination cursor"
436
442
  end
437
443
 
438
444
  { ts: Time.iso8601(decoded["ts"]).utc, id: decoded["id"] }
439
- rescue ArgumentError, JSON::ParserError
445
+ rescue ArgumentError, JSON::ParserError, JSON::NestingError
440
446
  raise McpInvalidParamsError, "Malformed pagination cursor"
441
447
  end
442
448
  end
@@ -21,6 +21,8 @@ module Sentrace
21
21
 
22
22
  REQUEST_CONTENT_TYPE = "application/json".freeze
23
23
  REQUEST_ACCEPT = "application/json, text/event-stream".freeze
24
+ MAX_REMOTE_RESPONSE_BYTES = 4 * 1024 * 1024
25
+ MAX_JSON_NESTING = 64
24
26
 
25
27
  def initialize(http_requester: nil)
26
28
  @http_requester = http_requester || method(:perform_net_http_post)
@@ -60,7 +62,10 @@ module Sentrace
60
62
 
61
63
  case response.status
62
64
  when 200..299
63
- parsed = JSON.parse(response.body.to_s)
65
+ raw_body = response.body.to_s
66
+ return "protocol_error" if raw_body.bytesize > MAX_REMOTE_RESPONSE_BYTES
67
+
68
+ parsed = JSON.parse(raw_body, max_nesting: MAX_JSON_NESTING)
64
69
  parsed.is_a?(Hash) && parsed.key?("result") ? "connected" : "protocol_error"
65
70
  when 401, 403
66
71
  "authentication_failed"
@@ -69,7 +74,7 @@ module Sentrace
69
74
  end
70
75
  rescue McpRemoteError => e
71
76
  e.category
72
- rescue JSON::ParserError
77
+ rescue JSON::ParserError, JSON::NestingError
73
78
  "protocol_error"
74
79
  end
75
80
 
@@ -120,7 +125,8 @@ module Sentrace
120
125
 
121
126
  def validate_endpoint!(env_def)
122
127
  raw_url = env_def.url.to_s.strip
123
- if raw_url.empty? || env_def.token.to_s.strip.empty?
128
+ raw_token = env_def.token.to_s.strip
129
+ if raw_url.empty? || raw_token.empty?
124
130
  raise McpRemoteError.new(
125
131
  "Remote MCP URL or bearer token is not configured for environment #{env_def.name}",
126
132
  environment: env_def.name,
@@ -128,8 +134,16 @@ module Sentrace
128
134
  )
129
135
  end
130
136
 
137
+ if raw_token.match?(/[\s\x00-\x1F\x7F]/)
138
+ raise McpRemoteError.new(
139
+ "Invalid MCP bearer token format for environment #{env_def.name}",
140
+ environment: env_def.name,
141
+ category: "invalid_endpoint"
142
+ )
143
+ end
144
+
131
145
  uri = URI.parse(raw_url)
132
- unless uri.is_a?(URI::HTTP) && uri.host && !uri.host.empty?
146
+ unless uri.is_a?(URI::HTTP) && uri.host && !uri.host.empty? && uri.userinfo.nil? && uri.fragment.nil?
133
147
  raise McpRemoteError.new(
134
148
  "Invalid MCP endpoint URL for environment #{env_def.name}",
135
149
  environment: env_def.name,
@@ -137,7 +151,7 @@ module Sentrace
137
151
  )
138
152
  end
139
153
 
140
- if uri.scheme != "https" && !Configuration::LOOPBACK_HOSTS.include?(uri.host.downcase)
154
+ if uri.scheme != "https" && !RequestSecurity.loopback_host?(uri.host)
141
155
  raise McpRemoteError.new(
142
156
  "HTTPS is required for remote environment #{env_def.name}",
143
157
  environment: env_def.name,
@@ -159,7 +173,7 @@ module Sentrace
159
173
  "Content-Type" => REQUEST_CONTENT_TYPE,
160
174
  "Accept" => REQUEST_ACCEPT
161
175
  }
162
- token = env_def.token.to_s
176
+ token = env_def.token.to_s.strip
163
177
  headers["Authorization"] = "Bearer #{token}" unless token.empty?
164
178
  headers
165
179
  end
@@ -198,7 +212,15 @@ module Sentrace
198
212
  end
199
213
 
200
214
  def parse_json_rpc_result!(env_def, raw_body)
201
- parsed = JSON.parse(raw_body)
215
+ if raw_body.bytesize > MAX_REMOTE_RESPONSE_BYTES
216
+ raise McpRemoteError.new(
217
+ "Response from environment #{env_def.name} exceeded #{MAX_REMOTE_RESPONSE_BYTES} bytes",
218
+ environment: env_def.name,
219
+ category: "protocol_error"
220
+ )
221
+ end
222
+
223
+ parsed = JSON.parse(raw_body, max_nesting: MAX_JSON_NESTING)
202
224
  unless parsed.is_a?(Hash)
203
225
  raise McpRemoteError.new(
204
226
  "Malformed JSON-RPC envelope from environment #{env_def.name}",
@@ -240,7 +262,7 @@ module Sentrace
240
262
  end
241
263
 
242
264
  normalize_environment_attribution(structured, env_def.name.to_s)
243
- rescue JSON::ParserError
265
+ rescue JSON::ParserError, JSON::NestingError
244
266
  raise McpRemoteError.new(
245
267
  "Invalid JSON response from environment #{env_def.name}",
246
268
  environment: env_def.name,
@@ -272,9 +294,10 @@ module Sentrace
272
294
 
273
295
  first_text = content_list.find { |item| item.is_a?(Hash) && item["type"] == "text" && item["text"].is_a?(String) }
274
296
  return nil unless first_text
297
+ return nil if first_text["text"].bytesize > MAX_REMOTE_RESPONSE_BYTES
275
298
 
276
- JSON.parse(first_text["text"])
277
- rescue JSON::ParserError
299
+ JSON.parse(first_text["text"], max_nesting: MAX_JSON_NESTING)
300
+ rescue JSON::ParserError, JSON::NestingError
278
301
  nil
279
302
  end
280
303
 
@@ -311,8 +334,13 @@ module Sentrace
311
334
 
312
335
  def perform_net_http_post(uri:, headers:, body:, timeout:)
313
336
  require "net/http"
314
- http = Net::HTTP.new(uri.host, uri.port)
337
+ target_host = uri.respond_to?(:hostname) && uri.hostname ? uri.hostname : uri.host
338
+ http = Net::HTTP.new(target_host, uri.port)
315
339
  http.use_ssl = (uri.scheme == "https")
340
+ if http.use_ssl?
341
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
342
+ http.min_version = OpenSSL::SSL::TLS1_2_VERSION if http.respond_to?(:min_version=)
343
+ end
316
344
  http.open_timeout = timeout
317
345
  http.read_timeout = timeout
318
346
  http.write_timeout = timeout if http.respond_to?(:write_timeout=)
@@ -322,12 +350,34 @@ module Sentrace
322
350
  request = Net::HTTP::Post.new(uri.request_uri, headers)
323
351
  request.body = body
324
352
 
325
- response = http.start { |conn| conn.request(request) }
353
+ status_code = nil
354
+ response_headers = nil
355
+ body_buffer = "".b
356
+
357
+ http.start do |conn|
358
+ conn.request(request) do |response|
359
+ status_code = response.code.to_i
360
+ response_headers = response.each_header.to_h
361
+ response.read_body do |chunk|
362
+ body_buffer << chunk.b
363
+ if body_buffer.bytesize > MAX_REMOTE_RESPONSE_BYTES
364
+ raise McpRemoteError.new(
365
+ "Remote MCP response exceeded #{MAX_REMOTE_RESPONSE_BYTES} bytes",
366
+ environment: target_host,
367
+ category: "protocol_error"
368
+ )
369
+ end
370
+ end
371
+ end
372
+ end
373
+
326
374
  HttpResponse.new(
327
- status: response.code.to_i,
328
- headers: response.each_header.to_h,
329
- body: response.body.to_s
375
+ status: status_code,
376
+ headers: response_headers || {},
377
+ body: body_buffer.force_encoding(Encoding::UTF_8)
330
378
  )
379
+ rescue McpRemoteError => e
380
+ raise e
331
381
  end
332
382
  end
333
383
  end