sshkit-sudo-next 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa2b9bfd304eb63502003564ba3dd7e5f0c8bf113eaed7cb1333d50daff10af7
4
- data.tar.gz: f1ea910c20e0c19eccd3a948ba4951ca7af6ad84a41aa90931a80c12a50fccd8
3
+ metadata.gz: a92ec3b6dbb3ef5951d570a4bf270a7a3e9a3d3469c8d450aa8634ff55413d15
4
+ data.tar.gz: 180082aeebbe9e8e7f2fd21600948dbd65c4d9f40cacc19cd78c5793b050d858
5
5
  SHA512:
6
- metadata.gz: fc1b4037bc29776399e780dc7f04f830571ae70435ee995cc7f081adf3b01e9d953702817fa00edc4d2574243732f96a87723863ce4750881e1d63bd57517afe
7
- data.tar.gz: 617afadec49af0f29fa9e95e1531766bbc061054d42b2917005a1bb3c562355820b7edc237baab48ed461dd01b3ee07e71a7e83aecdcd95cce241b4242179fa3
6
+ metadata.gz: cfdd4da51d358cb29ba2e7599d986bc164a396c48bc28c1c55a0d8d111b6db152df07262855cacc53f1ead2cca0cdb26571f826517f6177a3684390e3073e377
7
+ data.tar.gz: 66ca3f13ed1efbf7d09fd98ed023d55180bfee885adec5a3d1e09fc2af83780e8067533267fe35b76542c9e67bdbe777fc5797c88959e784bddf87ecd114e5be
@@ -10,5 +10,22 @@ module SSHKit
10
10
  return yield if env_string.empty?
11
11
  "( export #{env_string} ; #{yield} )"
12
12
  end
13
+
14
+ # The SudoNetssh backend stores redacted output, while the interaction handler must see the raw
15
+ # output (Command#on_stdout/on_stderr would pass it the redacted one).
16
+ #
17
+ def interact(stream_name, data, channel)
18
+ call_interaction_handler(stream_name, data, channel)
19
+ end
20
+
21
+ def store_stdout(data)
22
+ @stdout = data
23
+ @full_stdout += data
24
+ end
25
+
26
+ def store_stderr(data)
27
+ @stderr = data
28
+ @full_stderr += data
29
+ end
13
30
  end
14
31
  end
@@ -1,28 +1,48 @@
1
1
  module SSHKit
2
2
  module Sudo
3
- # Slightly simplified version of the original:
3
+ # Sends the password when a sudo prompt is the last thing a command printed.
4
4
  #
5
- # - assumed that there is a password
6
- # - assumes the same pwd for the hosts
7
- # - replaced the class/dead methods with constants
8
- # - removed the InteractionHandler subclass
5
+ # Output is matched per command/stream over a rolling tail, so prompts split across SSH chunks
6
+ # are still recognized. The match is anchored at the end of the output (sudo waits for input
7
+ # after printing the prompt), so ordinary output mentioning "password" (e.g. Chef's
8
+ # "Disabling password aging … [15:19:27…") doesn't trigger a send; a spurious send would be
9
+ # echoed by the PTY into the output.
9
10
  #
10
11
  class PasswordSendingInteractionHandler
11
12
  WRONG_PASSWORD_MESSAGE_REGEX = /Sorry.*\stry\sagain/
12
- PASSWORD_PROMPT_REGEX = /[Pp]assword.*:/
13
13
 
14
- def initialize(servers_password)
14
+ # Classic sudo, and sudo-rs. Custom prompts (`sudo -p`, PAM's "Password:") can be configured via
15
+ # the backend's `password_prompt_regex`; prefer patterns that can't match ordinary output.
16
+ DEFAULT_PASSWORD_PROMPT_REGEX = /\[sudo\] password for [^:\s]+:|\[sudo: authenticate\] Password:/
17
+
18
+ TAIL_SIZE = 256
19
+
20
+ def initialize(servers_password, prompt_regex: nil)
15
21
  @servers_password = servers_password
22
+ # Anchored at the end of the output, also for custom patterns.
23
+ @prompt_regex = /(?:#{prompt_regex || DEFAULT_PASSWORD_PROMPT_REGEX})[ \t]*\z/
24
+ @tails = {}
16
25
  end
17
26
 
18
27
  def on_data(command, stream_name, data, channel)
19
- raise "Wrong password!" if data =~ WRONG_PASSWORD_MESSAGE_REGEX
28
+ key = [command.object_id, stream_name]
29
+ tail = (@tails[key] || "".b) + data.b
30
+ tail = tail.byteslice(-TAIL_SIZE, TAIL_SIZE) if tail.bytesize > TAIL_SIZE
31
+ text = tail.dup.force_encoding(Encoding::UTF_8).scrub
32
+
33
+ raise "Wrong password!" if text =~ WRONG_PASSWORD_MESSAGE_REGEX
20
34
 
21
- if data =~ PASSWORD_PROMPT_REGEX
22
- pass = @servers_password
23
- channel.send_data(pass)
35
+ if text =~ @prompt_regex
36
+ @tails.delete(key)
37
+ channel.send_data(@servers_password)
38
+ else
39
+ @tails[key] = tail
24
40
  end
25
41
  end
42
+
43
+ def forget(command)
44
+ @tails.delete_if { |(command_id, _), _| command_id == command.object_id }
45
+ end
26
46
  end
27
47
  end
28
48
  end
@@ -0,0 +1,50 @@
1
+ module SSHKit
2
+ module Sudo
3
+ # Replaces a secret in a chunked byte stream, including occurrences split across chunks: a
4
+ # trailing fragment that could be the start of the secret is held back until the next chunk
5
+ # (or #flush) decides it.
6
+ #
7
+ class StreamRedactor
8
+ REPLACEMENT = "[REDACTED]".b
9
+
10
+ def initialize(secret)
11
+ @secret = secret.to_s.b
12
+ @pending = "".b
13
+ end
14
+
15
+ def redact(data)
16
+ return data if @secret.empty?
17
+
18
+ rest = @pending + data.b
19
+ redacted = "".b
20
+
21
+ while (index = rest.index(@secret))
22
+ redacted << rest.byteslice(0, index) << REPLACEMENT
23
+ rest = rest.byteslice(index + @secret.bytesize, rest.bytesize)
24
+ end
25
+
26
+ held = held_back_size(rest)
27
+ @pending = rest.byteslice(rest.bytesize - held, held)
28
+
29
+ redacted << rest.byteslice(0, rest.bytesize - held)
30
+ end
31
+
32
+ # A fragment pending at the end of the stream may be a truncated echo of the secret.
33
+ #
34
+ def flush
35
+ pending, @pending = @pending, "".b
36
+ pending.empty? ? pending : REPLACEMENT.dup
37
+ end
38
+
39
+ private
40
+
41
+ def held_back_size(buffer)
42
+ [@secret.bytesize - 1, buffer.bytesize].min.downto(1) do |size|
43
+ return size if buffer.end_with?(@secret.byteslice(0, size))
44
+ end
45
+
46
+ 0
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,5 +1,6 @@
1
1
  require 'sshkit'
2
2
  require_relative 'password_sending_interaction_handler'
3
+ require_relative 'stream_redactor'
3
4
  require_relative 'command'
4
5
 
5
6
  module SSHKit
@@ -8,12 +9,11 @@ module SSHKit
8
9
  # can be implemented, if needed, as Configuration attributes.
9
10
  #
10
11
  class SudoNetssh < Netssh
11
- PASSWORD_PROMPT_REGEX = /\[sudo\] password for \S+\:/
12
12
 
13
13
  SKIP_STDOUT_LOGGING_PATTERNS = [
14
14
  /^\r\n$/,
15
- PASSWORD_PROMPT_REGEX, # Skipping this removes context from the wrong password prompt, but it's
16
- # still understandable.
15
+ # Skipping this removes context from the wrong password prompt, but it's still understandable.
16
+ SSHKit::Sudo::PasswordSendingInteractionHandler::DEFAULT_PASSWORD_PROMPT_REGEX,
17
17
  ]
18
18
 
19
19
  class << self
@@ -30,7 +30,7 @@ module SSHKit
30
30
  #
31
31
  def config
32
32
  @config ||= Class.new(Netssh::Configuration) do
33
- attr_accessor :owner, :servers_password, :commands_log
33
+ attr_accessor :owner, :servers_password, :commands_log, :password_prompt_regex
34
34
  end.new
35
35
  end
36
36
  end
@@ -38,14 +38,20 @@ module SSHKit
38
38
  def initialize(*args, &block)
39
39
  super
40
40
 
41
- @interaction_handler = SSHKit::Sudo::PasswordSendingInteractionHandler.new(self.class.config.servers_password + "\n")
41
+ # A single snapshot, so that the sent and the redacted passwords can't diverge.
42
+ @password = normalize_password(self.class.config.servers_password)
43
+ @prompt_regex = self.class.config.password_prompt_regex ||
44
+ SSHKit::Sudo::PasswordSendingInteractionHandler::DEFAULT_PASSWORD_PROMPT_REGEX
45
+ @interaction_handler = SSHKit::Sudo::PasswordSendingInteractionHandler.new(
46
+ @password + "\n", prompt_regex: @prompt_regex
47
+ )
42
48
  end
43
49
 
44
50
  def capture(*args)
45
51
  # To ensure that we clean out the sudo part when the results are returned,
46
52
  # otherwise the commands will be corrupt.
47
53
  #
48
- super.gsub(PASSWORD_PROMPT_REGEX, '')
54
+ super.gsub(/(?:#{@prompt_regex})[ \t]*/, '')
49
55
  end
50
56
 
51
57
  # Required because the uploaded file is owned by the SSH user, not the owner.
@@ -64,6 +70,30 @@ module SSHKit
64
70
 
65
71
  private
66
72
 
73
+ # The configured value may carry trailing newlines (once, one was required).
74
+ #
75
+ def normalize_password(value)
76
+ password = value.to_s.sub(/[\r\n]+\z/, '')
77
+ raise ArgumentError, "The servers password can't contain newlines" if password =~ /[\r\n]/
78
+
79
+ password
80
+ end
81
+
82
+ def handle_stdout(cmd, data)
83
+ return if data.empty?
84
+
85
+ cmd.store_stdout(data)
86
+ skip_stdout_logging = SKIP_STDOUT_LOGGING_PATTERNS.any? { |pattern| data =~ pattern }
87
+ output.log_command_data(cmd, :stdout, data) unless skip_stdout_logging
88
+ end
89
+
90
+ def handle_stderr(cmd, data)
91
+ return if data.empty?
92
+
93
+ cmd.store_stderr(data)
94
+ output.log_command_data(cmd, :stderr, data)
95
+ end
96
+
67
97
  def command(args, options)
68
98
  options[:interaction_handler] ||= @interaction_handler
69
99
 
@@ -93,50 +123,60 @@ module SSHKit
93
123
  output.log_command_start(cmd)
94
124
  cmd.started = true
95
125
  exit_status = nil
96
- with_ssh do |ssh|
97
- ssh.open_channel do |chan|
98
- chan.request_pty
99
- prepared_command = cmd.to_command
100
-
101
- if self.class.config.commands_log
102
- IO.write(self.class.config.commands_log, prepared_command + "\n", mode: 'a')
103
- end
104
-
105
- chan.exec prepared_command do |_ch, _success|
106
- chan.on_data do |ch, data|
107
- cmd.on_stdout(ch, data)
108
- skip_stdout_logging = SKIP_STDOUT_LOGGING_PATTERNS.any? { |pattern| data =~ pattern }
109
- output.log_command_data(cmd, :stdout, data) unless skip_stdout_logging
110
- end
111
- chan.on_extended_data do |ch, _type, data|
112
- cmd.on_stderr(ch, data)
113
- output.log_command_data(cmd, :stderr, data)
126
+ # Defense in depth: the password must never reach logs, captures or exception messages,
127
+ # even if echoed back (PTY echo), so output is redacted before SSHKit stores or logs it.
128
+ # The interaction handler is fed the raw output, on the live channel.
129
+ stdout_redactor = SSHKit::Sudo::StreamRedactor.new(@password)
130
+ stderr_redactor = SSHKit::Sudo::StreamRedactor.new(@password)
131
+ begin
132
+ with_ssh do |ssh|
133
+ ssh.open_channel do |chan|
134
+ chan.request_pty
135
+ prepared_command = cmd.to_command
136
+
137
+ if self.class.config.commands_log
138
+ IO.write(self.class.config.commands_log, prepared_command + "\n", mode: 'a')
114
139
  end
115
- chan.on_request("exit-status") do |_ch, data|
116
- exit_status = data.read_long
117
- end
118
- #chan.on_request("exit-signal") do |ch, data|
119
- # # TODO: This gets called if the program is killed by a signal
120
- # # might also be a worthwhile thing to report
121
- # exit_signal = data.read_string.to_i
122
- # warn ">>> " + exit_signal.inspect
123
- # output.log_command_killed(cmd, exit_signal)
124
- #end
125
- chan.on_open_failed do |_ch|
126
- # TODO: What do do here?
127
- # I think we should raise something
128
- end
129
- chan.on_process do |_ch|
130
- # TODO: I don't know if this is useful
131
- end
132
- chan.on_eof do |_ch|
133
- # TODO: chan sends EOF before the exit status has been
134
- # writtend
140
+
141
+ chan.exec prepared_command do |_ch, _success|
142
+ chan.on_data do |ch, data|
143
+ cmd.interact(:stdout, data, ch)
144
+ handle_stdout(cmd, stdout_redactor.redact(data))
145
+ end
146
+ chan.on_extended_data do |ch, _type, data|
147
+ cmd.interact(:stderr, data, ch)
148
+ handle_stderr(cmd, stderr_redactor.redact(data))
149
+ end
150
+ chan.on_request("exit-status") do |_ch, data|
151
+ exit_status = data.read_long
152
+ end
153
+ #chan.on_request("exit-signal") do |ch, data|
154
+ # # TODO: This gets called if the program is killed by a signal
155
+ # # might also be a worthwhile thing to report
156
+ # exit_signal = data.read_string.to_i
157
+ # warn ">>> " + exit_signal.inspect
158
+ # output.log_command_killed(cmd, exit_signal)
159
+ #end
160
+ chan.on_open_failed do |_ch|
161
+ # TODO: What do do here?
162
+ # I think we should raise something
163
+ end
164
+ chan.on_process do |_ch|
165
+ # TODO: I don't know if this is useful
166
+ end
167
+ chan.on_eof do |_ch|
168
+ # TODO: chan sends EOF before the exit status has been
169
+ # writtend
170
+ end
135
171
  end
172
+ chan.wait
136
173
  end
137
- chan.wait
174
+ ssh.loop
138
175
  end
139
- ssh.loop
176
+ handle_stdout(cmd, stdout_redactor.flush)
177
+ handle_stderr(cmd, stderr_redactor.flush)
178
+ ensure
179
+ @interaction_handler.forget(cmd)
140
180
  end
141
181
  # Set exit_status and log the result upon completion
142
182
  if exit_status
@@ -1,5 +1,5 @@
1
1
  module SSHKit
2
2
  module Sudo
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
@@ -6,12 +6,11 @@ require 'sshkit/sudo/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "sshkit-sudo-next"
8
8
  spec.version = SSHKit::Sudo::VERSION
9
- spec.date = "2026-04-27"
9
+ spec.date = "2026-06-11"
10
10
  spec.authors = ["Saverio Miroddi"]
11
11
  spec.email = ["saverio.pub2@gmail.com"]
12
12
  spec.summary = %q{SSHKit extension, for sudo operation with password input.}
13
- spec.description = %q{SSHKit extension, for sudo operation with password input.}
14
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/64kramsystem/sshkit-sudo-next"
15
14
  spec.license = "MIT"
16
15
 
17
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -19,9 +18,9 @@ Gem::Specification.new do |spec|
19
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
19
  spec.require_paths = ["lib"]
21
20
 
22
- spec.required_ruby_version = ">= 1.9.3"
21
+ spec.required_ruby_version = ">= 2.5"
23
22
 
24
- spec.add_dependency "sshkit", "~> 1.20.0"
23
+ spec.add_dependency "sshkit", "~> 1.25.0"
25
24
  spec.add_development_dependency "bundler", "~> 1.7"
26
25
  spec.add_development_dependency "rake", "~> 10.0"
27
26
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sshkit-sudo-next
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Saverio Miroddi
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-04-27 00:00:00.000000000 Z
10
+ date: 2026-06-11 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: sshkit
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - "~>"
17
17
  - !ruby/object:Gem::Version
18
- version: 1.20.0
18
+ version: 1.25.0
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
- version: 1.20.0
25
+ version: 1.25.0
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: bundler
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -51,7 +51,6 @@ dependencies:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
53
  version: '10.0'
54
- description: SSHKit extension, for sudo operation with password input.
55
54
  email:
56
55
  - saverio.pub2@gmail.com
57
56
  executables: []
@@ -66,10 +65,11 @@ files:
66
65
  - lib/sshkit/sudo.rb
67
66
  - lib/sshkit/sudo/command.rb
68
67
  - lib/sshkit/sudo/password_sending_interaction_handler.rb
68
+ - lib/sshkit/sudo/stream_redactor.rb
69
69
  - lib/sshkit/sudo/sudo_netssh.rb
70
70
  - lib/sshkit/sudo/version.rb
71
71
  - sshkit-sudo-next.gemspec
72
- homepage: ''
72
+ homepage: https://github.com/64kramsystem/sshkit-sudo-next
73
73
  licenses:
74
74
  - MIT
75
75
  metadata: {}
@@ -80,14 +80,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - ">="
82
82
  - !ruby/object:Gem::Version
83
- version: 1.9.3
83
+ version: '2.5'
84
84
  required_rubygems_version: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - ">="
87
87
  - !ruby/object:Gem::Version
88
88
  version: '0'
89
89
  requirements: []
90
- rubygems_version: 4.0.8
90
+ rubygems_version: 4.0.20
91
91
  specification_version: 4
92
92
  summary: SSHKit extension, for sudo operation with password input.
93
93
  test_files: []