railwatch 1.6.5 → 1.6.6
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +42 -0
- data/docs/configuration.md +9 -4
- data/docs/records.md +3 -3
- data/docs/security.md +17 -4
- data/lib/railwatch/context.rb +23 -1
- data/lib/railwatch/faraday.rb +2 -2
- data/lib/railwatch/middleware/request.rb +8 -3
- data/lib/railwatch/patches/net_http.rb +2 -2
- data/lib/railwatch/record.rb +10 -0
- data/lib/railwatch/redactor.rb +146 -5
- data/lib/railwatch/subscribers/exceptions.rb +53 -3
- data/lib/railwatch/subscribers/llm.rb +3 -1
- data/lib/railwatch/subscribers/logs.rb +31 -1
- data/lib/railwatch/version.rb +1 -1
- data/lib/railwatch.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1507bcba0e8de14fdce1f3dcc8665d9889d6f3eb79e2af1cbd9f194d5633f6ec
|
|
4
|
+
data.tar.gz: 18dba317ebaf697ae4d3734f32cd620623b049bbf6ab0bf16fd53510a7f29790
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1565a0b5b06ae0651fea72cdcaa859cd2a74d7a38c23b29ad8308996abb469ebd26d00b1e646072d17a5ac0ca43944761da9ab0915e1473d74fce719ca0eeed8
|
|
7
|
+
data.tar.gz: '06289cfc8cc0f0948cb44848c14ce49fe42ee6ce889fc5b5eafd14816db61a82ba31e5831568ac46810b9aaadd3a088528c80e73a0ae17fb38be216f2e7f40fb'
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,48 @@
|
|
|
6
6
|
filled in by the release commit, which is also the only commit that
|
|
7
7
|
touches lib/railwatch/version.rb and Gemfile.lock. See CONTRIBUTING.md. -->
|
|
8
8
|
|
|
9
|
+
## 1.6.6 (2026-11-23)
|
|
10
|
+
|
|
11
|
+
- Security: **an Active Storage signed id is filtered from recorded URLs.**
|
|
12
|
+
`/rails/active_storage/blobs/redirect/:signed_id/*filename` (and the
|
|
13
|
+
proxy and representation routes) kept the signed id in the request's
|
|
14
|
+
`path` and `url` and in later requests' `Referer`, and that path alone
|
|
15
|
+
downloads the file: signed ids do not expire by default and Active
|
|
16
|
+
Storage's controllers are public. A segment named `signed_id`,
|
|
17
|
+
`signed_blob_id` or `sgid` is now a credential like `:token`.
|
|
18
|
+
- Security: **Rails' own log lines no longer repeat the URL secrets the
|
|
19
|
+
request record removes.** `Redirected to <url>` carried a password-reset
|
|
20
|
+
token, URL userinfo and query values that `redirect_to` filters, and the
|
|
21
|
+
routing-error line carried the raw path. Every URL in a captured log
|
|
22
|
+
line is now cleaned the way the request record's are.
|
|
23
|
+
- Security: **`Authorization`, `Cookie`, `X-Api-Key` and other credential
|
|
24
|
+
header names are masked as Hash keys.** A headers Hash in an exception
|
|
25
|
+
local or a span attribute was sent whole, though the same names are
|
|
26
|
+
masked as request headers. The key filter now applies the header rules
|
|
27
|
+
too (not the identity names, such as `email`, which stay with the app's
|
|
28
|
+
own `filter_parameters`).
|
|
29
|
+
- Security: **library error text no longer carries what the sibling
|
|
30
|
+
records remove.** With `capture_sql_values` off, a database error's
|
|
31
|
+
message (sqlite3 appends the statement) had its SQL literals; a Faraday
|
|
32
|
+
error carried the URL's query string, in the exception record and in
|
|
33
|
+
`outgoing_request.error`. The statement is now masked as `query.sql` is,
|
|
34
|
+
and URLs in exception and outgoing-request error text are cleaned.
|
|
35
|
+
- Security: **`Rails.event` payloads, request parameters given as context,
|
|
36
|
+
and LLM tool arguments are filtered by key.** Event payloads now get
|
|
37
|
+
`redact_params` and the header rules, and a Struct or Data event is
|
|
38
|
+
filtered by member; an `ActionController::Parameters` context value was
|
|
39
|
+
sent as its `to_s`, every value in it; tool arguments and results
|
|
40
|
+
(`capture_llm_content`) were sent unfiltered.
|
|
41
|
+
- Security: **request headers that carry a URL are cleaned like
|
|
42
|
+
`Referer`.** `HX-Current-URL`, `X-Envoy-Original-Path`, `X-Forwarded-Uri`
|
|
43
|
+
and any other header whose value is a URL or path kept their query and
|
|
44
|
+
token segments. None keeps a path when the request's host is unreadable.
|
|
45
|
+
- Security: **webhook and bot-token secrets in an outgoing URL's path are
|
|
46
|
+
masked.** Slack webhook secrets, Telegram bot tokens, Discord webhook
|
|
47
|
+
tokens, and a segment following one the parameter filter names are now
|
|
48
|
+
`[FILTERED]` in `outgoing_request.url`. Other path credentials are still
|
|
49
|
+
sent; use `Railwatch.redact_outgoing_requests` for those.
|
|
50
|
+
|
|
9
51
|
## 1.6.5 (2026-11-23)
|
|
10
52
|
|
|
11
53
|
- Security: **a client's `X-Inertia` header no longer makes Railwatch read
|
data/docs/configuration.md
CHANGED
|
@@ -327,13 +327,18 @@ aliases such as `X-AuthToken`, `X-ApiToken`, `X-AccessToken`,
|
|
|
327
327
|
`redact_headers`. Ordinary diagnostic headers remain available.
|
|
328
328
|
|
|
329
329
|
Headers that carry a URL are kept, cleaned. `Referer` keeps its path,
|
|
330
|
-
and its query only with `capture_browser_query_strings`; `Origin`,
|
|
330
|
+
and its query only with `capture_browser_query_strings`; `Origin`, the
|
|
331
331
|
original URL a rewriting proxy passes on (`X-Original-Url`,
|
|
332
|
-
`X-Original-Uri`, `X-Rewrite-Url`
|
|
333
|
-
|
|
332
|
+
`X-Original-Uri`, `X-Rewrite-Url`, `X-Envoy-Original-Path`,
|
|
333
|
+
`X-Forwarded-Uri`, `X-Forwarded-Path`, `X-Original-Path`), htmx's
|
|
334
|
+
`HX-Current-URL`, and any other header whose whole value is a URL or a
|
|
335
|
+
path, keep no query. None keeps userinfo or a fragment. When the request's
|
|
336
|
+
own host could not be read, none keeps a path.
|
|
334
337
|
|
|
335
338
|
**Secrets in the path.** A dynamic route segment whose name contains
|
|
336
|
-
`token`, `secret`, `key`, `code`, `signature` or `password`,
|
|
339
|
+
`token`, `secret`, `key`, `code`, `signature` or `password`, a signed id
|
|
340
|
+
(`signed_id`, `signed_blob_id`, `sgid`: Active Storage's file URLs carry
|
|
341
|
+
one, and it downloads the file on its own), or that
|
|
337
342
|
`redact_params`/`filter_parameters` would filter as a parameter, is
|
|
338
343
|
replaced by `[FILTERED]` wherever its URL is recorded: the request's
|
|
339
344
|
`path` and `url`, `redirect_to`, the `Referer` and original-URL headers,
|
data/docs/records.md
CHANGED
|
@@ -318,7 +318,7 @@ lose the record.
|
|
|
318
318
|
| `fingerprint` | The parts that were hashed, up to 10 strings of 200 chars each — by default class + top in-app frame's file/line + normalized message. Always present, so the platform can show *why* an occurrence grouped where it did. |
|
|
319
319
|
| `fingerprint_source` | Where the fingerprint came from: `"default"`, `"report"` (`Railwatch.report(error, fingerprint: [...])`), `"error"` (the exception's own `#railwatch_fingerprint`), or `"resolver"` (a `Railwatch.fingerprint { }` block). |
|
|
320
320
|
| `class` | Exception class name. |
|
|
321
|
-
| `message` | Truncated to 4096 chars. |
|
|
321
|
+
| `message` | Truncated to 4096 chars. URLs in it are cleaned like a request's (no userinfo, query or fragment; this app's credential route segments filtered). With `capture_sql_values` off, a database error's message has the statement it repeats masked as `query.sql` is. The cause's message and the parent's `exception_preview` are cleaned the same way. |
|
|
322
322
|
| `handled` | Whether the error was rescued (`Rails.error.handle`) vs. unhandled (`Rails.error.report`/escaped). |
|
|
323
323
|
| `severity` | `:error`/`:warning`/etc., as a string. |
|
|
324
324
|
| `source` | Free-text source tag the raiser passed, e.g. `"application.active_job"`, `"application.action_cable"` (a channel action that raised), `"railwatch.middleware"`, `"action_controller.rescue_from"`, or `"browser"` for a JavaScript error (see below). |
|
|
@@ -510,12 +510,12 @@ never double-recorded.
|
|
|
510
510
|
| `group` | Hash of host + method. |
|
|
511
511
|
| `host` | Request host. |
|
|
512
512
|
| `method` | HTTP verb. |
|
|
513
|
-
| `url` | Scheme + host + path, with authority credentials, the entire query string, and fragment removed, truncated to 2048 chars. |
|
|
513
|
+
| `url` | Scheme + host + path, with authority credentials, the entire query string, and fragment removed, truncated to 2048 chars. Path segments a third party puts its credential in are `[FILTERED]`: a Slack webhook's secret (`hooks.slack.com`, its last segment, or `/services/T…/B…/<secret>` anywhere), a Telegram bot token (`/bot[FILTERED]/`), a Discord webhook token (`/webhooks/<id>/[FILTERED]`), and a segment that follows one the parameter filter names (`/api_key/[FILTERED]`). Any other credential in a path is kept; scrub it with `Railwatch.redact_outgoing_requests`. |
|
|
514
514
|
| `duration` | Microseconds. |
|
|
515
515
|
| `status_code` | Response status, 0 if the request errored before a response. |
|
|
516
516
|
| `request_size` | Bytes (Net::HTTP path only). |
|
|
517
517
|
| `response_size` | Bytes, from `Content-Length` or body size. |
|
|
518
|
-
| `error` | `"Class: message"`, truncated to 255 chars, if the request raised. |
|
|
518
|
+
| `error` | `"Class: message"`, truncated to 255 chars, if the request raised. URLs in the message are cleaned like `url`. |
|
|
519
519
|
| `response_body` | First 4 KiB of the response body, but only when `config.capture_response_body_on_error` is on (off by default) *and* the response was an error. A body that parses as a JSON object is run through the same parameter filter as request params and re-serialized; anything else is stored as it arrived. nil in every other case — including a connection failure, where there is no response (on the Net::HTTP path a body is read only if Net::HTTP already buffered it, so a response being streamed through `read_body` is never consumed; on the Faraday path the body is taken only once a status came back, so an outgoing request payload can never be filed as a response). |
|
|
520
520
|
| `in_transaction` | True when the execution had a database transaction (or savepoint) of its own open for this call, so the transaction and its locks were held for the whole round trip and a rollback cannot undo what the call did. Only transactions the execution began count: one already open when it started, such as a test's transactional fixture, does not. Active Record begins a transaction at its first statement, so a `transaction do` block that has not yet run one is not open. A job performed inline inherits its parent's. The `have_railwatch_http_in_transaction` matcher fails a spec on it. |
|
|
521
521
|
| `source` | App-code call site (Net::HTTP path only). |
|
data/docs/security.md
CHANGED
|
@@ -46,7 +46,11 @@ configuration and record specs:
|
|
|
46
46
|
converted with `inspect`, truncated to 200 characters, and parameter
|
|
47
47
|
filtered: by the local's name, and by key inside a local that is a Hash,
|
|
48
48
|
an Array or request parameters (so `attrs = { password: ... }` ships as
|
|
49
|
-
`[FILTERED]` under `password`). A
|
|
49
|
+
`[FILTERED]` under `password`). A key the header rules name as a
|
|
50
|
+
credential (`Authorization`, `Cookie`, `X-Api-Key`, `redact_headers`) is
|
|
51
|
+
masked too; span attributes, `Rails.event` payloads (Struct and Data
|
|
52
|
+
events by member), request parameters given as context, and LLM tool
|
|
53
|
+
arguments and results get the same key filter. A secret held in a plain String under a
|
|
50
54
|
name the filter does not match, or printed by another object's own
|
|
51
55
|
`inspect`, is sent as it is; leave this off where that can happen.
|
|
52
56
|
`Locals.inspect_value` rescues an `inspect` implementation that
|
|
@@ -55,9 +59,18 @@ configuration and record specs:
|
|
|
55
59
|
backtrace frames are recorded and sent to the configured server. Disable it if source context is outside the
|
|
56
60
|
application's telemetry policy.
|
|
57
61
|
|
|
58
|
-
Log record messages are sent
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
Log record messages are sent as supplied to `Rails.logger`, with one
|
|
63
|
+
exception: every URL in a line is cleaned the way the request record's own
|
|
64
|
+
URLs are (userinfo, query and fragment dropped, this app's credential route
|
|
65
|
+
segments filtered, known third-party credential paths masked), so Rails'
|
|
66
|
+
own `Redirected to <url>` and routing-error lines do not repeat what
|
|
67
|
+
`redirect_to` and `path` filter. At most 20 URLs per line are cleaned in
|
|
68
|
+
full; the rest keep only scheme and host. The same cleaning applies to
|
|
69
|
+
exception messages and `outgoing_request.error` (Faraday's `status 401 for
|
|
70
|
+
GET <url>`), and with `capture_sql_values` off a database error's message
|
|
71
|
+
has the statement it repeats masked as the `query` record masks it.
|
|
72
|
+
Railwatch does not attempt to parse and partially filter `key=value` text
|
|
73
|
+
because doing so would be incomplete and could give a false assurance. Applications must not
|
|
61
74
|
log secrets; `Railwatch.redact_logs` can implement an application-specific scrub,
|
|
62
75
|
and `Railwatch.reject_logs` or `RAILWATCH_IGNORE_LOGS=true` can omit log records.
|
|
63
76
|
|
data/lib/railwatch/context.rb
CHANGED
|
@@ -49,12 +49,34 @@ module Railwatch
|
|
|
49
49
|
# Context is application data -- an app that puts an API token or a
|
|
50
50
|
# password in it should get the same treatment request params get, rather
|
|
51
51
|
# than having it written verbatim onto every record built while it is set.
|
|
52
|
+
# A context value holding a params object anywhere -- the value itself,
|
|
53
|
+
# or nested in a Hash or Array -- is scrubbed by key first
|
|
54
|
+
# (Redactor#scrub), so the object is filtered as the Hash it holds: the
|
|
55
|
+
# parameter filter walks only Hash and Array, and JSON wrote a params
|
|
56
|
+
# object's #to_s, every value in it. Other values go to the parameter
|
|
57
|
+
# filter as they are, so a wide context keeps every key for #truncate.
|
|
52
58
|
def serialize(context)
|
|
53
|
-
|
|
59
|
+
redactor = Railwatch.redactor
|
|
60
|
+
filtered = redactor.params(context.transform_values { |value| holds_params_object?(value) ? redactor.scrub(value) : value })
|
|
54
61
|
json = JSON.generate(filtered)
|
|
55
62
|
json.bytesize > LIMIT ? truncate(filtered) : json
|
|
56
63
|
end
|
|
57
64
|
|
|
65
|
+
# Fails closed: a container the scan cannot finish (deeper than 5, or
|
|
66
|
+
# wider than 100 entries) counts as holding one, so its value is
|
|
67
|
+
# scrubbed rather than serialized unread.
|
|
68
|
+
def holds_params_object?(value, depth = 0)
|
|
69
|
+
return true if params_object?(value)
|
|
70
|
+
return false unless value.is_a?(Hash) || value.is_a?(Array)
|
|
71
|
+
return true if depth >= 5 || value.size > 100
|
|
72
|
+
|
|
73
|
+
(value.is_a?(Hash) ? value.each_value : value).any? { holds_params_object?(_1, depth + 1) }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def params_object?(value)
|
|
77
|
+
defined?(::ActionController::Parameters) && ::ActionController::Parameters === value
|
|
78
|
+
end
|
|
79
|
+
|
|
58
80
|
# An oversized context used to be byteslice'd, which cut the JSON
|
|
59
81
|
# mid-string or mid-object: the platform could not parse it, so the whole
|
|
60
82
|
# context was lost rather than most of it. Rebuild a smaller context
|
data/lib/railwatch/faraday.rb
CHANGED
|
@@ -50,9 +50,9 @@ module Railwatch
|
|
|
50
50
|
url = env.url
|
|
51
51
|
Railwatch.record(:outgoing_request, group: Record.group_hash(url.host, env.method.to_s.upcase),
|
|
52
52
|
timestamp: started_at, host: url.host, method: env.method.to_s.upcase,
|
|
53
|
-
url: Record.
|
|
53
|
+
url: Record.outgoing_url(url),
|
|
54
54
|
duration: Clock.micros_since(start), status_code: env.status.to_i,
|
|
55
|
-
error: error && "#{error.class}: #{error.message}"[0, 255],
|
|
55
|
+
error: error && "#{error.class}: #{Railwatch.redactor.filter_text_urls(error.message.to_s)}"[0, 255],
|
|
56
56
|
response_body: response_body(env),
|
|
57
57
|
in_transaction: Railwatch.execution&.in_transaction? || false)
|
|
58
58
|
end
|
|
@@ -395,10 +395,15 @@ module Railwatch
|
|
|
395
395
|
# Headers whose value is a URL, cleaned the way the request's own url
|
|
396
396
|
# is: the page the browser came from (its query only with
|
|
397
397
|
# capture_browser_query_strings, like a beacon URL), and the original
|
|
398
|
-
# URL a rewriting proxy (IIS, Azure
|
|
398
|
+
# URL a rewriting proxy (IIS, Azure, Envoy/Istio, Traefik forward
|
|
399
|
+
# auth) or a client library (htmx) passes on (never its query). Both
|
|
399
400
|
# have their route's sensitive segments filtered when they are this
|
|
400
401
|
# app's. Origin is only scheme, host and port, so it has no route.
|
|
401
|
-
|
|
402
|
+
# Any other header whose whole value is a URL or a path is cleaned
|
|
403
|
+
# like the named ones (URL_HEADER_VALUE).
|
|
404
|
+
PROXY_URL_HEADERS = %w[X-Original-Url X-Original-Uri X-Rewrite-Url X-Envoy-Original-Path X-Forwarded-Uri
|
|
405
|
+
X-Forwarded-Path X-Original-Path Hx-Current-Url].to_set.freeze
|
|
406
|
+
URL_HEADER_VALUE = %r{\A(?:[A-Za-z][A-Za-z0-9+.-]*://|/)\S*\z}
|
|
402
407
|
|
|
403
408
|
# A page URL when the request's own host could not be read: the router
|
|
404
409
|
# cannot be asked which of its segments are credentials (an absolute
|
|
@@ -425,7 +430,7 @@ module Railwatch
|
|
|
425
430
|
host ? redactor.browser_url(v, limit: 512, host: host) : origin_only(v)
|
|
426
431
|
elsif name == "Origin"
|
|
427
432
|
Record.url_without_sensitive_components(v, limit: 512)
|
|
428
|
-
elsif PROXY_URL_HEADERS.include?(name)
|
|
433
|
+
elsif PROXY_URL_HEADERS.include?(name) || URL_HEADER_VALUE.match?(v.to_s)
|
|
429
434
|
host ? redactor.filter_url(Record.url_without_sensitive_components(v, limit: v.to_s.length), host: host)[0, 512] : origin_only(v)
|
|
430
435
|
else
|
|
431
436
|
v.to_s[0, 512]
|
|
@@ -61,12 +61,12 @@ module Railwatch
|
|
|
61
61
|
timestamp: started_at,
|
|
62
62
|
host: host,
|
|
63
63
|
method: req.method,
|
|
64
|
-
url: Record.
|
|
64
|
+
url: Record.outgoing_url(url),
|
|
65
65
|
duration: Clock.micros_since(start),
|
|
66
66
|
status_code: response&.code.to_i,
|
|
67
67
|
request_size: (req.body || "").bytesize,
|
|
68
68
|
response_size: response ? (response["Content-Length"]&.to_i || response.body&.bytesize rescue nil) : nil,
|
|
69
|
-
error: error && "#{error.class}: #{error.message}"[0, 255],
|
|
69
|
+
error: error && "#{error.class}: #{Railwatch.redactor.filter_text_urls(error.message.to_s)}"[0, 255],
|
|
70
70
|
response_body: response_body(response, error),
|
|
71
71
|
in_transaction: Railwatch.execution&.in_transaction? || false,
|
|
72
72
|
source: Backtrace.caller_location(skip: 4))
|
data/lib/railwatch/record.rb
CHANGED
|
@@ -128,6 +128,16 @@ module Railwatch
|
|
|
128
128
|
url[0, limit]
|
|
129
129
|
end
|
|
130
130
|
|
|
131
|
+
# An outgoing request's URL: cleaned as above, then with the path
|
|
132
|
+
# segments a third party puts its credential in masked
|
|
133
|
+
# (Redactor#filter_outgoing_path: Slack and Discord webhooks, Telegram
|
|
134
|
+
# bot tokens). Shared by the Net::HTTP patch, Railwatch::Faraday and
|
|
135
|
+
# Railwatch.instrument_outgoing.
|
|
136
|
+
def outgoing_url(value, limit: 2048)
|
|
137
|
+
clean = url_without_sensitive_components(value, limit: value.to_s.length)
|
|
138
|
+
Railwatch.redactor.filter_outgoing_path(clean)[0, limit]
|
|
139
|
+
end
|
|
140
|
+
|
|
131
141
|
PLAUSIBLE_AUTHORITY = /\A(?:\[[0-9A-Fa-f:.]*\]|[^\s:\[\]@]*)(?::\d*)?\z/
|
|
132
142
|
|
|
133
143
|
# Drops "user:pass@" from the authority. A URL without a scheme is read
|
data/lib/railwatch/redactor.rb
CHANGED
|
@@ -29,14 +29,21 @@ module Railwatch
|
|
|
29
29
|
)
|
|
30
30
|
(?:-|\z)
|
|
31
31
|
}ix
|
|
32
|
+
# The names SENSITIVE_HEADER_NAME takes because they name a person, not
|
|
33
|
+
# because they hold a credential. A Hash key (#scrub) is masked by the
|
|
34
|
+
# header rules without these: an "email" key in a local is the app's
|
|
35
|
+
# data, which its own filter_parameters decides about.
|
|
36
|
+
IDENTITY_HEADER_NAME = /(?:\A|-)(?:(?:forwarded|remote)-?(?:user|email)|(?:user-?)?email|preferred-?username)(?:-|\z)/i
|
|
32
37
|
# The browser client's own session header: its value is the record's
|
|
33
38
|
# session_id, a random id for grouping visits, not a credential.
|
|
34
39
|
OWN_HEADERS = %w[x-railwatch-session].to_set.freeze
|
|
35
40
|
# A dynamic route segment with one of these in its name carries a
|
|
36
41
|
# credential: Rails 8's `resources :passwords, param: :token`, invite,
|
|
37
|
-
# magic-link and confirmation routes
|
|
42
|
+
# magic-link and confirmation routes, and Rails' signed ids -- Active
|
|
43
|
+
# Storage's :signed_id and :signed_blob_id, which download the file on
|
|
44
|
+
# their own, and a GlobalID's sgid. So does one the app's own
|
|
38
45
|
# filter_parameters (or redact_params) names.
|
|
39
|
-
SENSITIVE_PATH_PARAMETER = /token|secret|key|code|signature|password/i
|
|
46
|
+
SENSITIVE_PATH_PARAMETER = /token|secret|key|code|signature|password|(?:\A|_)(?:signed|sgid)(?:_|\z)/i
|
|
40
47
|
ROUTING_PATH_PARAMETERS = %i[controller action format].freeze
|
|
41
48
|
NO_SECRETS = [].freeze
|
|
42
49
|
# #scrub: nesting followed before a container is elided, which bounds the
|
|
@@ -53,6 +60,17 @@ module Railwatch
|
|
|
53
60
|
# element of an Array at the root. No filter looks at it.
|
|
54
61
|
SCRUB_NO_KEY = Object.new.freeze
|
|
55
62
|
URL_ORIGIN = %r{\A(?:[A-Za-z][A-Za-z0-9+.-]*:)?//[^/]*}
|
|
63
|
+
# A URL inside free text (a log line, an exception message): an absolute
|
|
64
|
+
# URL, or a path Rails quotes ('No route matches [GET] "/x"').
|
|
65
|
+
TEXT_URL = %r{\b[A-Za-z][A-Za-z0-9+.-]*://[^\s"'<>`]+|"/[^"\s]*"}
|
|
66
|
+
# URLs in one text cleaned in full; any past this keep their origin
|
|
67
|
+
# only, so a line built to hold thousands costs no route lookups.
|
|
68
|
+
TEXT_URL_LIMIT = 20
|
|
69
|
+
# A third-party URL whose path is the credential (#filter_outgoing_path).
|
|
70
|
+
TELEGRAM_BOT_SEGMENT = /\Abot\d+:[\w-]+\z/
|
|
71
|
+
SLACK_HOOKS_HOST = "hooks.slack.com"
|
|
72
|
+
SLACK_TEAM = /\AT[A-Z0-9]+\z/
|
|
73
|
+
SLACK_BOT = /\AB[A-Z0-9]+\z/
|
|
56
74
|
|
|
57
75
|
def initialize(config)
|
|
58
76
|
@config = config
|
|
@@ -189,6 +207,67 @@ module Railwatch
|
|
|
189
207
|
{}
|
|
190
208
|
end
|
|
191
209
|
|
|
210
|
+
# text (a log line, an exception message) with every URL in it cleaned
|
|
211
|
+
# the way the request record cleans its own: no userinfo, query or
|
|
212
|
+
# fragment, this app's credential route segments filtered, and the
|
|
213
|
+
# third-party credential paths #filter_outgoing_path knows masked. A
|
|
214
|
+
# quoted path keeps its quotes. Text with neither "://" nor '"/' is
|
|
215
|
+
# returned as it is, without a regexp. Fails closed.
|
|
216
|
+
def filter_text_urls(text)
|
|
217
|
+
return text unless text.is_a?(String) && (text.include?("://") || text.include?("\"/"))
|
|
218
|
+
|
|
219
|
+
text = text.scrub unless text.valid_encoding?
|
|
220
|
+
seen = 0
|
|
221
|
+
text.gsub(TEXT_URL) do |match|
|
|
222
|
+
seen += 1
|
|
223
|
+
if match.start_with?("\"")
|
|
224
|
+
path = match[1..-2]
|
|
225
|
+
%("#{seen > TEXT_URL_LIMIT ? FILTERED : clean_url(path)}")
|
|
226
|
+
else
|
|
227
|
+
# Prose punctuation after a URL ("(see https://x/reset/T/edit).")
|
|
228
|
+
# is not part of it: matched with it, the route never would be.
|
|
229
|
+
url, tail = split_trailing_punctuation(match)
|
|
230
|
+
(seen > TEXT_URL_LIMIT ? Record.url_without_sensitive_components(url[URL_ORIGIN].to_s, limit: 512) : clean_url(url)) + tail
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
rescue StandardError
|
|
234
|
+
FILTERED
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
# An outgoing URL (already without userinfo, query and fragment) with
|
|
238
|
+
# the path segments that are a third party's credential masked: a Slack
|
|
239
|
+
# incoming-webhook or workflow secret (hooks.slack.com, its last
|
|
240
|
+
# segment; /services/T<team>/B<bot>/<secret> on any host, as a proxy
|
|
241
|
+
# or a stub has it), a Telegram bot token (/bot<id>:<token>/, on any
|
|
242
|
+
# host, as
|
|
243
|
+
# self-hosted Bot API servers take it too), a Discord webhook token
|
|
244
|
+
# (/webhooks/<id>/<token>), and a segment that follows one the app's
|
|
245
|
+
# parameter filter names (/api_key/<key>, /token/<token>).
|
|
246
|
+
def filter_outgoing_path(url)
|
|
247
|
+
url = url.to_s
|
|
248
|
+
origin = url[URL_ORIGIN].to_s
|
|
249
|
+
path = url[origin.length..].to_s
|
|
250
|
+
return url unless path.start_with?("/")
|
|
251
|
+
|
|
252
|
+
# The host compared whole, never by suffix (a lookalike host only ever
|
|
253
|
+
# masks more here, but a suffix test reads as one that admits).
|
|
254
|
+
slack = (URI.parse(origin).host.to_s.downcase == SLACK_HOOKS_HOST rescue false)
|
|
255
|
+
segments = path.split("/", -1)
|
|
256
|
+
last = segments.rindex { |segment| !segment.empty? }
|
|
257
|
+
segments.each_with_index.map do |segment, i|
|
|
258
|
+
next segment if segment.empty?
|
|
259
|
+
next FILTERED if slack && i == last && i >= 3
|
|
260
|
+
next FILTERED if i >= 4 && segments[i - 3] == "services" && segments[i - 2].match?(SLACK_TEAM) && segments[i - 1].match?(SLACK_BOT)
|
|
261
|
+
next "bot#{FILTERED}" if TELEGRAM_BOT_SEGMENT.match?(segment)
|
|
262
|
+
next FILTERED if i >= 2 && segments[i - 2] == "webhooks" && segments[i - 1].match?(/\A\d+\z/)
|
|
263
|
+
next FILTERED if i >= 2 && !segments[i - 1].empty? && filtered_name?(unescape(segments[i - 1]))
|
|
264
|
+
|
|
265
|
+
segment
|
|
266
|
+
end.then { |filtered| origin + filtered.join("/") }
|
|
267
|
+
rescue StandardError
|
|
268
|
+
url.to_s[URL_ORIGIN].to_s
|
|
269
|
+
end
|
|
270
|
+
|
|
192
271
|
# For a value about to be turned into ONE string with #inspect (an
|
|
193
272
|
# exception local, a span attribute): a copy with every value under a
|
|
194
273
|
# filtered key masked. #params can only filter by the name the string is
|
|
@@ -238,6 +317,44 @@ module Railwatch
|
|
|
238
317
|
end
|
|
239
318
|
end
|
|
240
319
|
|
|
320
|
+
# One URL found in text: cleaned, then filtered as this app's when it
|
|
321
|
+
# is a path or names a host (the host it names is taken as this app's,
|
|
322
|
+
# which can only filter more), then masked as an outgoing URL.
|
|
323
|
+
def clean_url(url)
|
|
324
|
+
clean = Record.url_without_sensitive_components(url, limit: url.length)
|
|
325
|
+
host = clean.start_with?("/") ? nil : URI.parse(clean).host
|
|
326
|
+
filter_outgoing_path(filter_url(clean, host: host))
|
|
327
|
+
rescue URI::Error
|
|
328
|
+
clean ? clean[URL_ORIGIN].to_s : FILTERED
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
# The URL and the punctuation prose put after it: trailing . , ; : ! ?
|
|
332
|
+
# and quotes always, and a closing ) ] } that has no opener in the URL.
|
|
333
|
+
def split_trailing_punctuation(match)
|
|
334
|
+
url = match.dup
|
|
335
|
+
tail = +""
|
|
336
|
+
loop do
|
|
337
|
+
last = url[-1]
|
|
338
|
+
break if last.nil?
|
|
339
|
+
|
|
340
|
+
closer = { ")" => "(", "]" => "[", "}" => "{" }[last]
|
|
341
|
+
if ".,;:!?'\"".include?(last) || (closer && url.count(closer) < url.count(last))
|
|
342
|
+
tail.prepend(last)
|
|
343
|
+
url.chop!
|
|
344
|
+
else
|
|
345
|
+
break
|
|
346
|
+
end
|
|
347
|
+
end
|
|
348
|
+
[ url, tail ]
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
# Fails closed.
|
|
352
|
+
def filtered_name?(name)
|
|
353
|
+
param_filter.filter_param(name, name) != name
|
|
354
|
+
rescue StandardError
|
|
355
|
+
true
|
|
356
|
+
end
|
|
357
|
+
|
|
241
358
|
def shaped_secrets(path, entries)
|
|
242
359
|
secrets = nil
|
|
243
360
|
entries.each do |pattern, names, children|
|
|
@@ -320,15 +437,33 @@ module Railwatch
|
|
|
320
437
|
# Fails closed, like #params. A dotted filter ("billing.number") names
|
|
321
438
|
# the whole path, so a nested key is also tried with its parents. Any
|
|
322
439
|
# key is matched by its #to_s, as the parameter filter matches one
|
|
323
|
-
# (`{ 42 => ... }` against /\A42\z/).
|
|
440
|
+
# (`{ 42 => ... }` against /\A42\z/). A key the header rules name as a
|
|
441
|
+
# credential (#credential_key?) is masked too.
|
|
324
442
|
def scrub_key?(key, key_path)
|
|
325
443
|
name = key.to_s
|
|
326
|
-
|
|
444
|
+
credential_key?(name) ||
|
|
445
|
+
param_filter.filter_param(name, name) != name ||
|
|
327
446
|
(key_path != name && param_filter.filter_param(key_path, key_path) != key_path)
|
|
328
447
|
rescue StandardError
|
|
329
448
|
true
|
|
330
449
|
end
|
|
331
450
|
|
|
451
|
+
# A headers Hash an API client built ("Authorization", "X-Api-Key",
|
|
452
|
+
# "Cookie") holds the credentials the request record masks by the same
|
|
453
|
+
# names, so a key is matched against the header rules: redact_headers
|
|
454
|
+
# and SENSITIVE_HEADER_NAME, less its identity names. The rules are
|
|
455
|
+
# written for hyphens; Ruby keys use underscores.
|
|
456
|
+
# An identity part ("Remote-User") never shields a credential part in
|
|
457
|
+
# the same name ("X-Remote-User-Authorization"): the name is a
|
|
458
|
+
# credential when it still matches with its identity parts taken out.
|
|
459
|
+
def credential_key?(name)
|
|
460
|
+
header = name.tr("_", "-")
|
|
461
|
+
return false unless redact_header?(header)
|
|
462
|
+
return true if @header_keys.include?(header.downcase)
|
|
463
|
+
|
|
464
|
+
SENSITIVE_HEADER_NAME.match?(header.gsub(IDENTITY_HEADER_NAME, "-"))
|
|
465
|
+
end
|
|
466
|
+
|
|
332
467
|
# A value under a key no filter names, given to the app's Proc filters
|
|
333
468
|
# as the value itself: one that masks by what the value holds (a
|
|
334
469
|
# four-digit pin) cannot tell from the key's name alone. Called as the
|
|
@@ -371,7 +506,13 @@ module Railwatch
|
|
|
371
506
|
def sensitive_routes
|
|
372
507
|
return NO_SECRETS unless defined?(Rails) && Rails.application
|
|
373
508
|
|
|
374
|
-
routes
|
|
509
|
+
# Rails 8 draws routes on first use; a process that has served no
|
|
510
|
+
# request (a job worker) would otherwise see none, and filter nothing
|
|
511
|
+
# in a log line or an error message. Not during boot, when the routes
|
|
512
|
+
# may not be drawable yet.
|
|
513
|
+
app = Rails.application
|
|
514
|
+
app.reload_routes_unless_loaded if app.respond_to?(:reload_routes_unless_loaded) && app.initialized?
|
|
515
|
+
routes = app.routes.set.routes
|
|
375
516
|
key = [ routes.length, routes.last.object_id ]
|
|
376
517
|
return @sensitive_routes if @sensitive_routes_key == key
|
|
377
518
|
|
|
@@ -106,7 +106,7 @@ module Railwatch
|
|
|
106
106
|
exe = execution
|
|
107
107
|
if exe&.first_exception_observation?(error, handled)
|
|
108
108
|
exe.count(:exceptions)
|
|
109
|
-
exe.exception_preview ||= "#{error.class}: #{error
|
|
109
|
+
exe.exception_preview ||= "#{error.class}: #{message_text(error)}"[0, 255]
|
|
110
110
|
end
|
|
111
111
|
# An interactive `rails runner` -- typed, piped, or a script in /tmp --
|
|
112
112
|
# is an engineer at a shell, and their typo is not an issue. Their
|
|
@@ -138,14 +138,14 @@ module Railwatch
|
|
|
138
138
|
remember_fingerprint(error, parts) if fingerprint
|
|
139
139
|
rec = {
|
|
140
140
|
class: error.class.name,
|
|
141
|
-
message: error
|
|
141
|
+
message: message_text(error)[0, 4096],
|
|
142
142
|
handled: handled,
|
|
143
143
|
severity: severity.to_s,
|
|
144
144
|
source: source.to_s,
|
|
145
145
|
file: top[:file],
|
|
146
146
|
line: top[:line],
|
|
147
147
|
frames: frames,
|
|
148
|
-
cause: cause && { class: cause.class.name, message: cause
|
|
148
|
+
cause: cause && { class: cause.class.name, message: message_text(cause, statement_of: statement_error(cause) || error)[0, 1024] },
|
|
149
149
|
context: Context.serialized_with(context),
|
|
150
150
|
code: error_code(error),
|
|
151
151
|
sql_state: sql_state_for(error),
|
|
@@ -182,6 +182,56 @@ module Railwatch
|
|
|
182
182
|
nil
|
|
183
183
|
end
|
|
184
184
|
|
|
185
|
+
# The error's message as it is recorded. A library builds these, not
|
|
186
|
+
# the app, and repeats in them what the sibling records remove: the
|
|
187
|
+
# URL with its query (Faraday's "status 401 for GET <url>"), cleaned
|
|
188
|
+
# here as Redactor#filter_text_urls cleans a log line; and, with
|
|
189
|
+
# capture_sql_values off, the statement with its literals (sqlite3
|
|
190
|
+
# appends the whole statement to a prepare error), which is masked
|
|
191
|
+
# as the query record masks it. statement_of: the error whose #sql
|
|
192
|
+
# this message may repeat -- a StatementInvalid's cause is the
|
|
193
|
+
# driver's own error, which carries the same text.
|
|
194
|
+
# The longest database error message masked whole (#message_text).
|
|
195
|
+
SQL_MASK_LIMIT = 1_048_576
|
|
196
|
+
|
|
197
|
+
def message_text(error, statement_of: error)
|
|
198
|
+
# Twice the longest field it fills (message, 4096), which bounds the
|
|
199
|
+
# work and still sees the "@" of a userinfo straddling that cut.
|
|
200
|
+
text = error.message.to_s
|
|
201
|
+
# Masked before the cut: cut first, a long statement no longer
|
|
202
|
+
# matches whole and its literals would pass unmasked. A message too
|
|
203
|
+
# long to mask in bounded work is replaced by the error's class name:
|
|
204
|
+
# nothing says which part of it is free of the statement.
|
|
205
|
+
unless Railwatch.config.capture_sql_values
|
|
206
|
+
text = text.bytesize > SQL_MASK_LIMIT && statement_error(statement_of) ? statement_of.class.name.to_s : without_sql_literals(text, statement_of)
|
|
207
|
+
end
|
|
208
|
+
text = text[0, 8192]
|
|
209
|
+
Railwatch.redactor.filter_text_urls(text)
|
|
210
|
+
rescue StandardError
|
|
211
|
+
error.class.name.to_s
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# A database error's message with the statement masked by
|
|
215
|
+
# SqlNormalizer: the statement where the message repeats it whole,
|
|
216
|
+
# and every line after the first, where drivers echo it (sqlite3's
|
|
217
|
+
# appended statement, Postgres' "LINE 1:" excerpt). The first line is
|
|
218
|
+
# the driver's own description and is kept. Anything else is left.
|
|
219
|
+
def without_sql_literals(text, error)
|
|
220
|
+
return text unless statement_error(error)
|
|
221
|
+
|
|
222
|
+
adapter = (error.connection_pool.db_config.adapter rescue nil)
|
|
223
|
+
sql = error.sql.to_s
|
|
224
|
+
text = text.gsub(sql) { SqlNormalizer.normalize(sql, adapter: adapter) } unless sql.empty?
|
|
225
|
+
first, rest = text.split("\n", 2)
|
|
226
|
+
return text if rest.nil?
|
|
227
|
+
|
|
228
|
+
[ first, *rest.split("\n", -1).map { |line| SqlNormalizer.normalize(line, adapter: adapter) } ].join("\n")
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def statement_error(error)
|
|
232
|
+
error if defined?(::ActiveRecord::StatementInvalid) && error.is_a?(::ActiveRecord::StatementInvalid)
|
|
233
|
+
end
|
|
234
|
+
|
|
185
235
|
# The group hash `capture` would assign this error. Public so
|
|
186
236
|
# Railwatch.attach can file an attachment against the same issue without
|
|
187
237
|
# having to re-derive the bucketing rule (source snippets are skipped:
|
|
@@ -323,7 +323,9 @@ module Railwatch
|
|
|
323
323
|
return nil unless Railwatch.config.capture_llm_content
|
|
324
324
|
return nil if value.nil?
|
|
325
325
|
|
|
326
|
-
|
|
326
|
+
# Tool arguments and results are Hashes: filtered by key before they
|
|
327
|
+
# become one string, like a span attribute.
|
|
328
|
+
text = (value.is_a?(String) ? value : Railwatch.redactor.scrub(value)).to_s
|
|
327
329
|
return nil if text.empty?
|
|
328
330
|
# byteslice, not [0, n]: the cap bounds what is buffered and shipped,
|
|
329
331
|
# and 4096 characters of CJK is three times that in bytes. scrub
|
|
@@ -65,7 +65,7 @@ module Railwatch
|
|
|
65
65
|
level: "event",
|
|
66
66
|
message: name,
|
|
67
67
|
tags: Array(event[:tags]).map(&:to_s),
|
|
68
|
-
context:
|
|
68
|
+
context: Logs.event_context(event[:payload]),
|
|
69
69
|
source: event.dig(:source_location, :filepath) && "#{event[:source_location][:filepath].delete_prefix(Backtrace.app_root)}:#{event[:source_location][:lineno]}")
|
|
70
70
|
end
|
|
71
71
|
end
|
|
@@ -108,6 +108,12 @@ module Railwatch
|
|
|
108
108
|
return if text.start_with?("[railwatch]") || framework_noise?(text)
|
|
109
109
|
exe.count(:logs)
|
|
110
110
|
return unless recording?
|
|
111
|
+
# Rails' own lines repeat the URLs the request record cleans:
|
|
112
|
+
# "Redirected to <target>", the routing error's quoted path. So
|
|
113
|
+
# does any line an app writes with a URL in it. Cut to twice the
|
|
114
|
+
# stored length first, which bounds the work and still sees the "@"
|
|
115
|
+
# of a userinfo that straddles the final cut.
|
|
116
|
+
text = Railwatch.redactor.filter_text_urls(text[0, 16_384])
|
|
111
117
|
cfg = Railwatch.config
|
|
112
118
|
# One hash literal instead of kwargs-packing into Railwatch.record --
|
|
113
119
|
# every captured Rails.logger line goes through here.
|
|
@@ -126,6 +132,30 @@ module Railwatch
|
|
|
126
132
|
})
|
|
127
133
|
end
|
|
128
134
|
|
|
135
|
+
# A Rails.event payload as JSON, filtered by key like a span attribute
|
|
136
|
+
# (Redactor#scrub): Rails filters a Hash payload with filter_parameters
|
|
137
|
+
# only, not redact_params or the header rules, and does not filter an
|
|
138
|
+
# event object at all. An event object is read as the Hash of its
|
|
139
|
+
# members (a Struct, a Data) or of its #as_json first, so it is
|
|
140
|
+
# filtered by member name; one that gives no Hash is sent as "{}".
|
|
141
|
+
def event_context(payload)
|
|
142
|
+
redactor = Railwatch.redactor
|
|
143
|
+
unless payload.is_a?(Hash)
|
|
144
|
+
payload = if payload.is_a?(Struct) || (defined?(::Data) && payload.is_a?(::Data))
|
|
145
|
+
payload.to_h
|
|
146
|
+
elsif payload.respond_to?(:as_json)
|
|
147
|
+
payload.as_json
|
|
148
|
+
end
|
|
149
|
+
return "{}" unless payload.is_a?(Hash)
|
|
150
|
+
end
|
|
151
|
+
# Never cut: a cut JSON string cannot be parsed, so an oversized
|
|
152
|
+
# payload is sent as "{}" rather than as half an object.
|
|
153
|
+
json = JSON.generate(redactor.params(redactor.scrub(payload)))
|
|
154
|
+
json.bytesize > 8192 ? "{}" : json
|
|
155
|
+
rescue StandardError
|
|
156
|
+
"{}"
|
|
157
|
+
end
|
|
158
|
+
|
|
129
159
|
def current_tags
|
|
130
160
|
logger = Rails.logger
|
|
131
161
|
logger.respond_to?(:current_tags) ? logger.current_tags.map(&:to_s) : []
|
data/lib/railwatch/version.rb
CHANGED
data/lib/railwatch.rb
CHANGED
|
@@ -419,7 +419,7 @@ module Railwatch
|
|
|
419
419
|
host = (URI(url.to_s).host rescue nil)
|
|
420
420
|
record(:outgoing_request, group: Record.group_hash(host, method.to_s.upcase),
|
|
421
421
|
timestamp: started_at, host: host, method: method.to_s.upcase,
|
|
422
|
-
url: Record.
|
|
422
|
+
url: Record.outgoing_url(url),
|
|
423
423
|
duration: Clock.micros_since(start), status_code: result.status.to_i,
|
|
424
424
|
in_transaction: execution&.in_transaction? || false)
|
|
425
425
|
end
|