pwn 0.5.798 → 0.5.799

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: 9fd12f22a3f9a9d4d6bf1188611efda40635ebe6a14f1d279c2a2a9bdc50e52e
4
- data.tar.gz: 1e6626db3bcb24d03d1ae169304d7bba96dca1cff32f14f74c362d547ad3f8ae
3
+ metadata.gz: c38e0a1f930f304cf9e00ac69c38f28c5430e300ee11e44c6c60cbc9873a882b
4
+ data.tar.gz: 202b5e1f81b967675b8bdbef028efec9b4d819eac46364a81d76560fcf270cf5
5
5
  SHA512:
6
- metadata.gz: 6a64e04125b968f0379a8dc6e838897ca3975ca5b992a0368f8d9758f2e9971145cd2e53d21ca6cbbd7b3f91a3c76b23245241199517f5596980229193a88c70
7
- data.tar.gz: 37c6dcfb41e00ddf1ce62ba958e54c0e6f24f931a5ca0a965c0c11299a36d7ab6bf999793b278704d8222e8ebd62cf98a56ee9855e9d48cfb15dbce7316c6326
6
+ metadata.gz: 43b187378cc6943a2c6783833de1422f89edda0c132dba35dd82a727cb328d52650c8f1e0b29535aa3e24d8d631751fce4128104571cf8d7529503c1b41ccd75
7
+ data.tar.gz: 53ca63d1f232d26b747dc11af49ceaac8c696f21d5ebf6d48c8f41444da2a5c7676c904472dc1cf5966956ac1f330cbecfd92d3910facee0a5abfbae4910c3ce
@@ -338,16 +338,17 @@ module PWN
338
338
 
339
339
  return if @stderr_redaction_overflow
340
340
 
341
- text = opts[:text].to_s
341
+ text = stderr_text(text: opts[:text])
342
342
  return if text.empty?
343
- return if spinner_frame?(text: text)
343
+ return if spinner_frame?(text: text) && (@stderr_redaction_buffer.to_s.empty? || !text.strip.empty?)
344
344
 
345
345
  Thread.current[:pwn_log_stderr] = true
346
346
  begin
347
347
  io = @debug_file
348
348
  return if io.nil? || (io.respond_to?(:closed?) && io.closed?)
349
349
 
350
- @stderr_redaction_buffer = @stderr_redaction_buffer.to_s + text.gsub(/\e\[[0-9;]*m/, '')
350
+ # A write can end in the middle of a UTF-8 character or an ANSI escape.
351
+ @stderr_redaction_buffer = @stderr_redaction_buffer.to_s.b + text.b
351
352
  buffer = @stderr_redaction_buffer
352
353
  if buffer.bytesize > 65_536
353
354
  clean = PWN::Redaction.token(kind: 'stream', value: buffer)
@@ -355,6 +356,8 @@ module PWN
355
356
  else
356
357
  # A write may split a JWT/header; PEM bodies may span many writes.
357
358
  return unless buffer.end_with?("\n")
359
+
360
+ buffer = stderr_text(text: buffer).scrub.gsub(/\e\[[0-9;]*m/, '')
358
361
  return if buffer.match?(/-----BEGIN [A-Z ]*PRIVATE KEY-----/) && !buffer.match?(/-----END [A-Z ]*PRIVATE KEY-----/)
359
362
 
360
363
  clean = sanitize_debug_text(text: buffer)
@@ -373,10 +376,18 @@ module PWN
373
376
  clean
374
377
  end
375
378
 
376
- # Spinner frames stay on the TTY; never persist them in the RN log.
379
+ private_class_method def self.stderr_text(opts = {})
380
+ text = opts[:text].to_s
381
+ return text.dup.force_encoding(Encoding::UTF_8) if text.encoding == Encoding::BINARY
382
+ return text if text.encoding == Encoding::UTF_8
383
+
384
+ text.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
385
+ end
386
+
387
+ # Spinner frames stay on the TTY; an incomplete character isn't a frame.
377
388
  public_class_method def self.spinner_frame?(opts = {})
378
- raw = opts[:text].to_s
379
- return false if raw.empty?
389
+ raw = stderr_text(text: opts[:text])
390
+ return false if raw.empty? || !raw.valid_encoding?
380
391
 
381
392
  stripped = raw.gsub(/\e\[[0-9;?]*[A-Za-z]/, '').delete("\r").delete("\b")
382
393
  stripped = stripped.gsub(/[\u2800-\u28FF]/, '')
data/lib/pwn/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PWN
4
- VERSION = '0.5.798'
4
+ VERSION = '0.5.799'
5
5
  end
@@ -439,3 +439,34 @@ describe 'PWN::Plugins::Log toggle-trace step' do
439
439
  expect(PWN::Plugins::Log.debug_dir).to include('logs')
440
440
  end
441
441
  end
442
+
443
+ describe PWN::Plugins::Log::DebugStderrTee do
444
+ include_context 'pwn tmp sandbox'
445
+
446
+ let(:path) { File.join(@tmp, 'stderr.log') }
447
+ let(:original) { StringIO.new(+''.b) }
448
+ let(:stream) { described_class.new(orig: original) }
449
+
450
+ before { PWN::Plugins::Log.start_debug(path: path, tee: StringIO.new) }
451
+ after { PWN::Plugins::Log.stop_debug }
452
+
453
+ it 'preserves split UTF-8 on stderr and filters binary-tagged spinner frames from the log' do
454
+ parts = ["\r\e[1G⠴".b, "résumé: \xE2".b, "\x82\xAC".b, ' — done', "\n"]
455
+ parts.each { |part| expect(stream.write(part.freeze)).to eq(part.bytesize) }
456
+
457
+ expect(original.string).to eq(parts.map(&:b).join)
458
+ body = File.read(path, encoding: 'UTF-8')
459
+ expect(body).to include("résumé: € — done\n")
460
+ expect(body).not_to include('⠴', '�')
461
+ end
462
+
463
+ it 'replaces malformed stderr bytes in the log without losing the warning or exposing a split secret' do
464
+ stream.write("warning\xFF api_".b)
465
+ stream.write("key=synthetic-sensitive-value\n".b)
466
+
467
+ body = File.read(path, encoding: 'UTF-8')
468
+ expect(body).to be_valid_encoding
469
+ expect(body).to include('warning� [REDACTED:')
470
+ expect(body).not_to include('synthetic-sensitive-value')
471
+ end
472
+ end
@@ -27,18 +27,35 @@ describe 'regex-ReAct mission containment' do
27
27
  Pry.config.hooks.get_hook(:after_read, :pwn_ai_hook).call(request, pry)
28
28
  end
29
29
 
30
+ def namespace_pids(status)
31
+ status.to_s[/^NSpid:[ \t]+([1-9]\d*(?:[ \t]+[1-9]\d*)*)\n/, 1].to_s.split.map(&:to_i)
32
+ end
33
+
30
34
  def wait_for_descendant_pid(marker)
35
+ observer = namespace_pids(File.read('/proc/self/status'))
36
+ raise 'Cannot identify the test runner PID namespace' unless observer.last == Process.pid
37
+
31
38
  Timeout.timeout(10) do
32
39
  loop do
33
- text = File.read(marker) if File.file?(marker)
34
- pid = text.to_s[/^NSpid:[ \t]+([1-9]\d*)(?:[ \t]+\d+)+\n/, 1]
35
- return pid.to_i if pid
40
+ if File.file?(marker)
41
+ pids = namespace_pids(File.read(marker))
42
+ return pids.fetch(observer.size - 1) if pids.size > observer.size
43
+ end
36
44
 
37
45
  sleep 0.01
38
46
  end
39
47
  end
40
48
  end
41
49
 
50
+ # Pin the live task before interrupting it. A pidfd reports exit even if
51
+ # another process has yet to reap it, and never follows a recycled PID.
52
+ def open_pidfd(pid)
53
+ fd = PWN::AI::Agent::MissionNamespace.native('pidfd_open', [Fiddle::TYPE_INT, Fiddle::TYPE_INT]).call(pid, 0)
54
+ raise SystemCallError.new('pidfd_open for descendant', Fiddle.last_error) if fd.negative?
55
+
56
+ IO.new(fd)
57
+ end
58
+
42
59
  it 'waits for the descendant PID contents rather than an empty readiness file' do
43
60
  marker = File.join(@tmp, 'partial-ready')
44
61
  File.write(marker, '')
@@ -48,6 +65,7 @@ describe 'regex-ReAct mission containment' do
48
65
  File.write(marker, "NSpid:\t12345\t1\n")
49
66
  end
50
67
  allow(File).to receive(:read).and_call_original
68
+ allow(File).to receive(:read).with('/proc/self/status').and_return("NSpid:\t#{Process.pid}\n")
51
69
  allow(File).to receive(:read).with(marker).and_wrap_original do |original, path|
52
70
  text = original.call(path)
53
71
  read_empty << true if text.empty?
@@ -58,6 +76,31 @@ describe 'regex-ReAct mission containment' do
58
76
  writer&.kill&.join
59
77
  end
60
78
 
79
+ it 'selects the PID visible to a nested upgrade runner, not the outer procfs PID' do
80
+ marker = File.join(@tmp, 'nested-ready')
81
+ File.write(marker, "NSpid:\t12345\t82\t1\n")
82
+ allow(File).to receive(:read).and_call_original
83
+ allow(File).to receive(:read).with('/proc/self/status').and_return("NSpid:\t12000\t#{Process.pid}\n")
84
+ expect(wait_for_descendant_pid(marker)).to eq(82)
85
+ end
86
+
87
+ it 'distinguishes a live child from an exited child that has not been reaped' do
88
+ reader, release = IO.pipe
89
+ pid = fork do
90
+ release.close
91
+ reader.read(1)
92
+ exit! 0
93
+ end
94
+ handle = open_pidfd(pid)
95
+ expect(handle.wait_readable(0)).to be_nil
96
+ release.close
97
+ expect(handle.wait_readable(5)).to be_truthy
98
+ expect(Process.kill(0, pid)).to eq(1) # The old probe still sees the zombie.
99
+ ensure
100
+ [release, reader, handle].compact.each { |io| io.close unless io.closed? }
101
+ Process.wait(pid) if pid
102
+ end
103
+
61
104
  it 'fails closed before calling a provider when mission containment is unavailable' do
62
105
  allow(PWN::Plugins::AISandbox::MissionProcess).to receive(:new).and_raise(PWN::Plugins::AISandbox::NotStarted, 'enable user/PID namespaces')
63
106
  expect(PWN::AI::Ollama).not_to receive(:chat)
@@ -168,10 +211,11 @@ describe 'regex-ReAct mission containment' do
168
211
  nil
169
212
  end
170
213
  marker = File.join(@tmp, '.pwn', 'workspaces', 'runtime', 'pty-ready')
171
- host_pid = wait_for_descendant_pid(marker)
214
+ descendant = open_pidfd(wait_for_descendant_pid(marker))
215
+ expect(descendant.wait_readable(0)).to be_nil, 'descendant exited before PTY CTRL+C'
172
216
  writer.write("\x03")
173
217
  Timeout.timeout(10) { sleep 0.01 until output.include?('CANCELLED READY') }
174
- expect { Process.kill(0, host_pid) }.to raise_error(Errno::ESRCH)
218
+ expect(descendant.wait_readable(0)).to be_truthy, 'detached descendant still alive at CANCELLED READY'
175
219
  writer.write("second\n")
176
220
  Timeout.timeout(10) { Process.wait(pid) }
177
221
  pid = nil
@@ -192,7 +236,7 @@ describe 'regex-ReAct mission containment' do
192
236
  nil
193
237
  end
194
238
  end
195
- [reader, writer].compact.each { |io| io.close unless io.closed? }
239
+ [descendant, reader, writer].compact.each { |io| io.close unless io.closed? }
196
240
  drain&.join(5)
197
241
  end
198
242
 
@@ -209,11 +253,12 @@ describe 'regex-ReAct mission containment' do
209
253
  rescue Interrupt
210
254
  :cancelled
211
255
  end
212
- host_pid = wait_for_descendant_pid(marker)
256
+ descendant = open_pidfd(wait_for_descendant_pid(marker))
257
+ expect(descendant.wait_readable(0)).to be_nil, 'descendant exited before interrupt'
213
258
  worker.raise(Interrupt)
214
259
  expect(Timeout.timeout(10) { worker.value }).to eq(:cancelled)
215
260
  expect(repl).to have_received(:ready_tty!)
216
- expect { Process.kill(0, host_pid) }.to raise_error(Errno::ESRCH)
261
+ expect(descendant.wait_readable(0)).to be_truthy, 'detached descendant still alive when the terminal became ready'
217
262
  sleep 1.1
218
263
  expect(File).not_to exist(late)
219
264
  allow(PWN::AI::Ollama).to receive(:chat).and_return(id: 'second', choices: [{ content: 'second complete' }])
@@ -222,6 +267,7 @@ describe 'regex-ReAct mission containment' do
222
267
  expect(PWN::Env.dig(:ai, :ollama, :response_history, :id)).to eq('second')
223
268
  expect(request).to eq('nil')
224
269
  ensure
270
+ descendant&.close
225
271
  worker&.raise(Interrupt) if worker&.alive?
226
272
  worker&.join(10)
227
273
  end
@@ -15,7 +15,7 @@ RSpec.describe PWN::SAST::TestCaseEngine, 'formatter review safeguards' do
15
15
  FileUtils.mkdir_p(@sources)
16
16
  @lock_path = File.expand_path('../../../../Gemfile.lock', __dir__)
17
17
  @lock_before = File.binread(@lock_path)
18
- @js_beautify_bin = Gem.bin_path('js-beautify', 'js-beautify')
18
+
19
19
  saved_env = %w[BUNDLE_GEMFILE BUNDLE_LOCKFILE RUBYOPT BUNDLE_FROZEN].to_h { |key| [key, ENV.fetch(key, nil)] }
20
20
  original_path = ENV.fetch('PATH')
21
21
  # Fixture subprocesses must not inherit the checkout lock path. Clearing
@@ -150,19 +150,25 @@ RSpec.describe PWN::SAST::TestCaseEngine, 'formatter review safeguards' do
150
150
  name = 'source;printf proof>review-marker;#.js'
151
151
  source = 'var fixture={value:1};'
152
152
  File.write(File.join(@sources, name), source)
153
- commands_dir = File.join(@root, 'commands')
154
- FileUtils.mkdir_p(commands_dir)
155
153
  shell_commands = []
156
154
  allow(described_class).to receive(:`).and_wrap_original do |original, command|
157
155
  shell_commands << command
158
156
  original.call(command)
159
157
  end
160
- # Load the actual gem launcher by its original path: it resolves its
161
- # bundled parser relative to __FILE__, so symlinking that file breaks it.
162
- launcher = File.join(commands_dir, 'js-beautify')
163
- File.write(launcher, "#!#{RbConfig.ruby}\nload #{@js_beautify_bin.dump}\n")
164
- File.chmod(0o700, launcher)
165
- ENV['PATH'] = "#{commands_dir}:#{ENV.fetch('PATH')}"
158
+ # Exercise the installed launcher's argument handling, not its Python parser.
159
+ # Only the launcher child sees this PATH; it cannot fall back to host Python.
160
+ backend_dir = File.join(@root, 'backend')
161
+ Dir.mkdir(backend_dir)
162
+ backend = File.join(backend_dir, 'python')
163
+ File.write(backend, <<~RUBY)
164
+ #!#{RbConfig.ruby}
165
+ abort 'expected parser and stdin selector' unless ARGV.size == 2 && File.file?(ARGV.first) && ARGV.last == '-'
166
+ abort 'expected source bytes on stdin' unless STDIN.binmode.read == #{source.dump}
167
+ puts 'var fixture = { value: 1 };'
168
+ RUBY
169
+ File.chmod(0o700, backend)
170
+ launcher = Gem.bin_path('js-beautify', 'js-beautify')
171
+ synthetic_formatter("ENV['PATH'] = #{backend_dir.dump}\nload #{launcher.dump}")
166
172
 
167
173
  result = described_class.execute(options)
168
174
 
@@ -170,8 +176,8 @@ RSpec.describe PWN::SAST::TestCaseEngine, 'formatter review safeguards' do
170
176
  expect(shell_commands.join("\n")).not_to include(Shellwords.escape(name))
171
177
  expect(File.exist?(File.join(@sources, 'review-marker'))).to be(false)
172
178
  expect(result.length).to eq(1)
173
- expect(result.first[:filename][:entry_beautified]).to be(true)
174
- expect(result.first[:line_no_and_contents].first[:contents]).to include('var fixture = {')
179
+ expect(result.first[:filename]).to include(entry: "./#{name}", entry_beautified: true)
180
+ expect(result.first[:line_no_and_contents].first[:contents]).to eq("var fixture = { value: 1 };\n")
175
181
  expect(File.read(File.join(@sources, name))).to eq(source)
176
182
  expect(Digest::SHA256.file(@lock_path).hexdigest).to eq(Digest::SHA256.hexdigest(@lock_before))
177
183
  end
@@ -3227,6 +3227,7 @@
3227
3227
  {"messages":[{"role":"user","content":"PWN::Plugins::Log.spinner_frame? Usage"},{"role":"assistant","content":"`PWN::Plugins::Log.spinner_frame?`: "}]}
3228
3228
  {"messages":[{"role":"user","content":"PWN::Plugins::Log.start_debug Usage"},{"role":"assistant","content":"`PWN::Plugins::Log.start_debug`: "}]}
3229
3229
  {"messages":[{"role":"user","content":"PWN::Plugins::Log.start_trace! Usage"},{"role":"assistant","content":"`PWN::Plugins::Log.start_trace!`: "}]}
3230
+ {"messages":[{"role":"user","content":"PWN::Plugins::Log.stderr_text Usage"},{"role":"assistant","content":"`PWN::Plugins::Log.stderr_text`: "}]}
3230
3231
  {"messages":[{"role":"user","content":"PWN::Plugins::Log.stop_debug Usage"},{"role":"assistant","content":"`PWN::Plugins::Log.stop_debug`: "}]}
3231
3232
  {"messages":[{"role":"user","content":"PWN::Plugins::Log.stop_trace! Usage"},{"role":"assistant","content":"`PWN::Plugins::Log.stop_trace!`: "}]}
3232
3233
  {"messages":[{"role":"user","content":"PWN::Plugins::Log.trace_arguments Usage"},{"role":"assistant","content":"`PWN::Plugins::Log.trace_arguments`: "}]}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pwn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.798
4
+ version: 0.5.799
5
5
  platform: ruby
6
6
  authors:
7
7
  - 0day Inc.