Skip to content

Commit 862c328

Browse files
committed
Remove sensitive output from trace
1 parent 9b4c8ea commit 862c328

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Changes since the last non-beta release.
1414

1515
_Please add entries here for your pull requests that have not yet been released._
1616

17+
### Changed
18+
19+
- Redact sensitive data (Authorization headers, tokens) from `--trace` output. [PR 261](https://github.com/shakacode/control-plane-flow/pull/261) by [Sergey Tarasov](https://github.com/dzirtusss).
20+
1721
## [4.1.1] - 2025-03-14
1822

1923

lib/core/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def initialize(args, options, required_options)
2525
return unless trace_mode
2626

2727
ControlplaneApiDirect.trace = trace_mode
28-
Shell.warn("Trace mode is enabled, this will print sensitive information to the console.")
28+
Shell.warn("Trace mode is enabled. Sensitive data is redacted, but please review output before sharing.")
2929
end
3030

3131
def org

lib/core/controlplane_api_direct.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# frozen_string_literal: true
22

3+
class RedactedDebugOutput
4+
SAFE_HEADERS = %w[Content-Type Content-Length Accept Host Date Cache-Control Connection].freeze
5+
HEADER_REGEX = /^([A-Za-z\-]+): (.+)$/.freeze
6+
7+
def <<(msg)
8+
$stdout << redact(msg)
9+
end
10+
11+
private
12+
13+
def redact(msg)
14+
msg.lines.map { |line| redact_line(line) }.join
15+
end
16+
17+
def redact_line(line)
18+
match = line.match(HEADER_REGEX)
19+
return line.gsub(/[\w\-._]{50,}/, "[REDACTED]") unless match
20+
21+
SAFE_HEADERS.any? { |h| h.casecmp(match[1]).zero? } ? line : "#{match[1]}: [REDACTED]\n"
22+
end
23+
end
24+
325
class ControlplaneApiDirect
426
API_METHODS = {
527
get: Net::HTTP::Get,
@@ -37,7 +59,7 @@ def call(url, method:, host: :api, body: nil) # rubocop:disable Metrics/MethodLe
3759

3860
http = Net::HTTP.new(uri.hostname, uri.port)
3961
http.use_ssl = uri.scheme == "https"
40-
http.set_debug_output($stdout) if trace
62+
http.set_debug_output(RedactedDebugOutput.new) if trace
4163

4264
response = http.start { |ht| ht.request(request) }
4365

0 commit comments

Comments
 (0)